File: ctmconv.cpp

package info (click to toggle)
openctm 1.0.3%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,704 kB
  • sloc: ansic: 9,241; cpp: 5,199; python: 735; makefile: 29
file content (234 lines) | stat: -rw-r--r-- 7,931 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
//-----------------------------------------------------------------------------
// Product:     OpenCTM tools
// File:        ctmconv.cpp
// Description: 3D file format conversion tool. The program can be used to
//              convert various 3D file formats to and from the OpenCTM file
//              format, and also for conversion between other formats.
//-----------------------------------------------------------------------------
// Copyright (c) 2009-2010 Marcus Geelnard
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
//     1. The origin of this software must not be misrepresented; you must not
//     claim that you wrote the original software. If you use this software
//     in a product, an acknowledgment in the product documentation would be
//     appreciated but is not required.
//
//     2. Altered source versions must be plainly marked as such, and must not
//     be misrepresented as being the original software.
//
//     3. This notice may not be removed or altered from any source
//     distribution.
//-----------------------------------------------------------------------------

#include <stdexcept>
#include <vector>
#include <iostream>
#include <list>
#include <string>
#include <cctype>
#include "systimer.h"
#include "convoptions.h"
#include "mesh.h"
#include "meshio.h"

using namespace std;


//-----------------------------------------------------------------------------
// PreProcessMesh()
//-----------------------------------------------------------------------------
static void PreProcessMesh(Mesh &aMesh, Options &aOptions)
{
  // Nothing to do?
  if((aOptions.mScale == 1.0f) && (aOptions.mUpAxis == uaZ) &&
     (!aOptions.mFlipTriangles) && (!aOptions.mCalcNormals))
    return;

  // Create 3x3 transformation matrices for the vertices and the normals
  Vector3 vX, vY, vZ;
  Vector3 nX, nY, nZ;
  switch(aOptions.mUpAxis)
  {
    case uaX:
      nX = Vector3(0.0f, 0.0f, 1.0f);
      nY = Vector3(0.0f, 1.0f, 0.0f);
      nZ = Vector3(-1.0f, 0.0f, 0.0f);
      break;
    case uaY:
      nX = Vector3(1.0f, 0.0f, 0.0f);
      nY = Vector3(0.0f, 0.0f, 1.0f);
      nZ = Vector3(0.0f, -1.0f, 0.0f);
      break;
    case uaZ:
      nX = Vector3(1.0f, 0.0f, 0.0f);
      nY = Vector3(0.0f, 1.0f, 0.0f);
      nZ = Vector3(0.0f, 0.0f, 1.0f);
      break;
    case uaNX:
      nX = Vector3(0.0f, 0.0f, -1.0f);
      nY = Vector3(0.0f, 1.0f, 0.0f);
      nZ = Vector3(1.0f, 0.0f, 0.0f);
      break;
    case uaNY:
      nX = Vector3(1.0f, 0.0f, 0.0f);
      nY = Vector3(0.0f, 0.0f, -1.0f);
      nZ = Vector3(0.0f, 1.0f, 0.0f);
      break;
    case uaNZ:
      nX = Vector3(-1.0f, 0.0f, 0.0f);
      nY = Vector3(0.0f, 1.0f, 0.0f);
      nZ = Vector3(0.0f, 0.0f, -1.0f);
      break;
  }
  vX = nX * aOptions.mScale;
  vY = nY * aOptions.mScale;
  vZ = nZ * aOptions.mScale;

  cout << "Processing... " << flush;
  SysTimer timer;
  timer.Push();

  // Update all vertex coordinates
  for(CTMuint i = 0; i < aMesh.mVertices.size(); ++ i)
    aMesh.mVertices[i] = vX * aMesh.mVertices[i].x +
                         vY * aMesh.mVertices[i].y +
                         vZ * aMesh.mVertices[i].z;

  // Update all normals
  if(aMesh.HasNormals() && !aOptions.mNoNormals)
  {
    for(CTMuint i = 0; i < aMesh.mNormals.size(); ++ i)
      aMesh.mNormals[i] = nX * aMesh.mNormals[i].x +
                          nY * aMesh.mNormals[i].y +
                          nZ * aMesh.mNormals[i].z;
  }

  // Flip trianlges?
  if(aOptions.mFlipTriangles)
  {
    CTMuint triCount = aMesh.mIndices.size() / 3;
    for(CTMuint i = 0; i < triCount; ++ i)
    {
      CTMuint tmp = aMesh.mIndices[i * 3];
      aMesh.mIndices[i * 3] = aMesh.mIndices[i * 3 + 1];
      aMesh.mIndices[i * 3 + 1] = tmp;
    }
  }

  // Calculate normals?
  if((!aOptions.mNoNormals) && aOptions.mCalcNormals &&
     (!aMesh.HasNormals()))
    aMesh.CalculateNormals();

  double dt = timer.PopDelta();
  cout << 1000.0 * dt << " ms" << endl;
}


//-----------------------------------------------------------------------------
// main()
//-----------------------------------------------------------------------------
int main(int argc, char ** argv)
{
  // Get file names and options
  Options opt;
  string inFile;
  string outFile;
  try
  {
    if(argc < 3)
      throw runtime_error("Too few arguments.");
    inFile = string(argv[1]);
    outFile = string(argv[2]);
    opt.GetFromArgs(argc, argv, 3);
  }
  catch(exception &e)
  {
    cout << "Error: " << e.what() << endl << endl;
    cout << "Usage: " << argv[0] << " infile outfile [options]" << endl << endl;
    cout << "Options:" << endl;
    cout << endl << " Data manipulation (all formats)" << endl;
    cout << "  --scale arg     Scale the mesh by a scalar factor." << endl;
    cout << "  --upaxis arg    Set up axis (X, Y, Z, -X, -Y, -Z). If != Z, the mesh will" << endl;
    cout << "                  be flipped." << endl;
    cout << "  --flip          Flip triangle orientation." << endl;
    cout << "  --calc-normals  If the source file does not contain any normals, calculate" << endl;
    cout << "                  them." << endl;
    cout << "  --no-normals    Do not export normals." << endl;
    cout << "  --no-texcoords  Do not export texture coordinates." << endl;
    cout << "  --no-colors     Do not export vertex colors." << endl;
    cout << endl << " OpenCTM output" << endl;
    cout << "  --method arg    Select compression method (RAW, MG1, MG2)" << endl;
    cout << "  --level arg     Set the compression level (0 - 9)" << endl;
    cout << endl << " OpenCTM MG2 method" << endl;
    cout << "  --vprec arg     Set vertex precision" << endl;
    cout << "  --vprecrel arg  Set vertex precision, relative method" << endl;
    cout << "  --nprec arg     Set normal precision" << endl;
    cout << "  --tprec arg     Set texture map precision" << endl;
    cout << "  --cprec arg     Set color precision" << endl;
    cout << endl << " Miscellaneous" << endl;
    cout << "  --comment arg   Set the file comment (default is to use the comment" << endl;
    cout << "                  from the input file, if any)." << endl;
    cout << "  --texfile arg   Set the texture file name reference for the texture" << endl;
    cout << "                  (default is to use the texture file name reference" << endl;
    cout << "                  from the input file, if any)." << endl;

    // Show supported formats
    cout << endl << "Supported file formats:" << endl << endl;
    list<string> formatList;
    SupportedFormats(formatList);
    for(list<string>::iterator i = formatList.begin(); i != formatList.end(); ++ i)
      cout << "  " << (*i) << endl;
    cout << endl;

    return 0;
  }

  try
  {
    // Define mesh
    Mesh mesh;

    // Create a timer instance
    SysTimer timer;
    double dt;

    // Load input file
    cout << "Loading " << inFile << "... " << flush;
    timer.Push();
    ImportMesh(inFile.c_str(), &mesh);
    dt = timer.PopDelta();
    cout << 1000.0 * dt << " ms" << endl;

    // Manipulate the mesh
    PreProcessMesh(mesh, opt);

    // Override comment?
    if(opt.mComment.size() > 0)
      mesh.mComment = opt.mComment;

    // Override texture file name?
    if(opt.mTexFileName.size() > 0)
      mesh.mTexFileName = opt.mTexFileName;

    // Save output file
    cout << "Saving " << outFile << "... " << flush;
    timer.Push();
    ExportMesh(outFile.c_str(), &mesh, opt);
    dt = timer.PopDelta();
    cout << 1000.0 * dt << " ms" << endl;
  }
  catch(exception &e)
  {
    cout << "Error: " << e.what() << endl;
  }

  return 0;
}