File: io_options.cc

package info (click to toggle)
openmesh 11.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,080 kB
  • sloc: cpp: 56,379; ansic: 5,600; perl: 1,374; sh: 119; makefile: 18
file content (297 lines) | stat: -rw-r--r-- 8,450 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <iostream>
#include <iterator>
// -------------------- OpenMesh
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <OpenMesh/Tools/Utils/getopt.h>

// ----------------------------------------------------------------------------

using namespace OpenMesh;

// ----------------------------------------------------------------------------

typedef TriMesh_ArrayKernelT<>  MyMesh;

// ----------------------------------------------------------------------------

#define CHKROPT( Option ) \
  std::cout << "  provides " << #Option \
            << (ropt.check(IO::Options:: Option)?": yes\n":": no\n")

#define CHKWOPT( Option ) \
  std::cout << "  write " << #Option \
            << (wopt.check(IO::Options:: Option)?": yes\n":": no\n")

#define MESHOPT( msg, tf ) \
  std::cout << "  " << msg << ": " << ((tf)?"yes\n":"no\n")

// ----------------------------------------------------------------------------

void parse_commandline( int _argc, char **_argv, MyMesh& _mesh,
                        IO::Options &ropt, IO::Options &wopt );

void usage_and_exit(int xcode);

// ----------------------------------------------------------------------------

int main(int argc, char **argv)
{
  MyMesh  mesh;
  IO::Options ropt, wopt;

  // -------------------- evaluate commandline

  parse_commandline( argc, argv, mesh, ropt, wopt );

  // -------------------- read mesh

  if ( ! IO::read_mesh(mesh,argv[optind], ropt))
  {
    std::cerr << "Error loading mesh from file " << argv[optind] << std::endl;
    return 1;
  }

  // -------------------- show options

  std::cout << "File " << argv[optind] << std::endl;

  std::cout << "  is binary: " 
            << (ropt.check(IO::Options::Binary) ? " yes\n" : " no\n");

  std::cout << "  byte order: ";
  if (ropt.check(IO::Options::Swap))
    std::cout << "swapped\n";
  else if (ropt.check(IO::Options::LSB))
    std::cout << "little endian\n";
  else if (ropt.check(IO::Options::MSB))
    std::cout << "big endian\n";
  else
    std::cout << "don't care\n";
  
  std::cout << "  provides VertexNormal"
            << ( // strange layout for doxygen
                ropt.check(IO::Options::VertexNormal) 
                ? ": yes\n":": no\n");
  CHKROPT( VertexColor    );
  CHKROPT( VertexTexCoord );
  CHKROPT( FaceNormal     );
  CHKROPT( FaceColor      );


  // -------------------- mesh stats

  std::cout << "# Vertices: " << mesh.n_vertices() << std::endl;
  std::cout << "# Edges   : " << mesh.n_faces() << std::endl;
  std::cout << "# Faces   : " << mesh.n_faces() << std::endl;


  // -------------------- show write options

  std::cout << "Selected write options:\n";
  std::cout << "  use binary: " 
            << (wopt.check(IO::Options::Binary) ? " yes\n" : " no\n");

  std::cout << "  byte order: ";
  if (wopt.check(IO::Options::Swap))
    std::cout << "swapped\n";
  else if (wopt.check(IO::Options::LSB))
    std::cout << "little endian\n";
  else if (wopt.check(IO::Options::MSB))
    std::cout << "big endian\n";
  else
    std::cout << "don't care\n";
  
  std::cout << "  write VertexNormal"
            << (wopt.check(IO::Options::VertexNormal) ? ": yes\n":": no\n");
  CHKWOPT( VertexColor    );
  CHKWOPT( VertexTexCoord );
  CHKWOPT( FaceNormal     );
  CHKWOPT( FaceColor      );

  // -------------------- show mesh capabilities

  std::cout << "Mesh supports\n";
  MESHOPT("vertex normals", mesh.has_vertex_normals());
  MESHOPT("vertex colors", mesh.has_vertex_colors());
  MESHOPT("texcoords", mesh.has_vertex_texcoords2D());
  MESHOPT("face normals", mesh.has_face_normals());
  MESHOPT("face colors", mesh.has_face_colors());

  // -------------------- write mesh

  std::cout << "Write mesh to " << argv[optind+1] << "..";
  if ( !IO::write_mesh( mesh, argv[optind+1], wopt ) )
  {
    std::cerr << "Error" << std::endl;
    std::cerr << "Possible reasons:\n";
    std::cerr << "1. Chosen format cannot handle an option!\n";
    std::cerr << "2. Mesh does not provide necessary information!\n";
    std::cerr << "3. Or simply cannot open file for writing!\n";
    return 1;
  }
  else
    std::cout << "Ok.\n";

  return 0;
}

// ----------------------------------------------------------------------------

void parse_commandline( int _argc, char **_argv, MyMesh& _mesh,
                        IO::Options &ropt, IO::Options &wopt )
{
  int c;
  while ((c=getopt(_argc, _argv, "bhsBF:LMSV:X:"))!=-1)
  {
    switch(c) 
    {
      // -------------------- read options

      // force binary input
      case 'b': 
        ropt += IO::Options::Binary; 
        break;

      // force swapping the byte order, when reading a binary file
      case 's': 
        ropt += IO::Options::Swap; 
        break;

      // -------------------- write options

      // Write binary variant of format if possible
      case 'B': 
        wopt += IO::Options::Binary; 
        break;

      // 
      case 'F':
        for(size_t i=0; optarg[i]; ++i)
          switch(optarg[i]) {
            case 'n' : wopt += IO::Options::FaceNormal; break;
            case 'c' : wopt += IO::Options::FaceColor; break;
          }
        break;

      // Use little endian when writing binary data
      case 'L': 
        wopt += IO::Options::LSB; 
        break;

      // Use big endian when writing binary data
      case 'M': 
        wopt += IO::Options::MSB; 
        break;

      // Swap byte order when writing binary data
      case 'S': 
        wopt += IO::Options::Swap; 
        break;

      //
      case 'V':
      {
        for(size_t i=0; optarg[i]; ++i)
          switch(optarg[i]) {
            case 'n' : // dont't change layout!!
              wopt += IO::Options::VertexNormal; 
              break;
            case 't' : wopt += IO::Options::VertexTexCoord; break;
            case 'c' : wopt += IO::Options::VertexColor; break;
          }
        break;
      }

      // -------------------- request mesh' standard properties
      case 'X':
      {        
        char entity='\0';
        for(size_t i=0; optarg[i]; ++i)
          switch(optarg[i]) {
            case 'v': 
            case 'f': entity = optarg[i]; break;
            case 'n':
              switch(entity) {
                case 'v': _mesh.request_vertex_normals(); break;
                case 'f': _mesh.request_face_normals(); break;
              }
              break;
            case 'c':
              switch(entity) {
                case 'v': _mesh.request_vertex_colors(); break;
                case 'f': _mesh.request_face_colors(); break;
              }
              break;
            case 't':
              switch(entity) {
                case 'v': _mesh.request_vertex_texcoords2D(); break;
              }
              break;
          }
        break;
      }
      
      // -------------------- help
      case 'h':
        usage_and_exit(0);
      default:
        usage_and_exit(1);
    }
  }

  if ( _argc-optind != 2)
    usage_and_exit(1);
}


// ----------------------------------------------------------------------------

void usage_and_exit(int xcode)
{
  std::ostream &os = xcode ? std::cerr : std::cout;

  os << "Usage: io_options [Options] <input> <output>\n" 
     << std::endl;
  os << "  Read and write a mesh, using OpenMesh::IO::Options\n"
     << std::endl;
  os << "Options:\n"
     << std::endl;
  os << "a) read options\n"
     << std::endl
     << "  -b\n"
     << "\tAssume input file is a binary file\n"
     << std::endl
     << "  -s\n"
     << "\tSwap byte order when reading a binary file!\n"
     << std::endl;
  os << "b) write options\n"
     << std::endl
     << "  -B\n"
     << "\tWrite binary data\n"
     << std::endl
     << "  -S\n"
     << "\tSwap byte order, when writing binary data\n"
     << std::endl
     << "  -M/-L\n"
     << "\tUse MSB/LSB byte ordering, when writing binary data\n"
     << std::endl
     << "  -V{n|t|c}\n"
     << "\tWrite vertex normals, texcoords, and/or colors\n"
     << std::endl
     << "  -F{n|c}\n"
     << "\tWrite face normals, and/or colors\n"
     << std::endl;
  os << "c) Mesh properties\n"
     << std::endl
     << "  -Xv{n|c|t}\n"
     << "\tRequest vertex property normals|colors|texcoords\n"
     << std::endl
     << "  -Xf{n|c}\n"
     << "\tRequest face property normals|colors\n"
     << std::endl;
  exit(xcode);
}

// end of file
// ============================================================================