File: wrl.cpp

package info (click to toggle)
openctm 1.0.3%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,692 kB
  • sloc: ansic: 9,241; cpp: 5,199; python: 735; makefile: 30
file content (305 lines) | stat: -rw-r--r-- 7,990 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
298
299
300
301
302
303
304
305
//-----------------------------------------------------------------------------
// Product:     OpenCTM tools
// File:        wrl.cpp
// Description: Implementation of the VRML 2.0 file format importer/exporter.
//-----------------------------------------------------------------------------
// 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 <fstream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <list>
#include "wrl.h"
#include "common.h"

using namespace std;


/// VRML reader class
class VRMLReader {
  private:
    /// Read buffer
    char * mBuffer;
    int mBufSize;
    int mBufActual;
    int mBufPos;
    bool mEndOfFile;

    /// File comment(s)
    string mComment;

    /// Read one character from the file stream - via the read buffer
    char GetNextChar()
    {
      if(!mStream)
        throw runtime_error("VRML input stream undefined.");
      if(mBufPos >= mBufActual)
      {
        mBufPos = 0;
        if(!mStream->eof())
        {
          mStream->read(mBuffer, mBufSize);
          mBufActual = mStream->gcount();
        }
        else
          mBufActual = 0;
        mEndOfFile = (mBufActual < 1);
      }
      if(!mEndOfFile)
      {
        char c = mBuffer[mBufPos];
        ++ mBufPos;
        return c;
      }
      else
        return ' ';
    }

    /// Read the next line from file
    string GetNextLine()
    {
      // Read the token (until the next new line)
      stringstream sstr;
      char c = GetNextChar();
      while((!mEndOfFile) && (!IsEOL(c)))
      {
        sstr << c;
        c = GetNextChar();
      }
      return sstr.str();
    }

    /// Read a line comment from the stream, and append it to the global
    /// comment string
    void ReadComment()
    {
      // Extract the line comment
      string newComment = TrimString(GetNextLine());

      // Append the line comment to the global comment string
      if(newComment.size() > 0)
      {
        if(mComment.size() > 0)
          mComment = mComment + string(" ");
        mComment = mComment + newComment;
      }
    }

    /// Read the next token from file (skip comments and whitespaces)
    string GetNextToken()
    {
      char c = ' ';

      // Iterate until we find the first character of a token
      while(!mEndOfFile)
      {
        while(!mEndOfFile)
        {
          c = GetNextChar();
          if(!IsWhiteSpace(c))
            break;
        }

        // End of file?
        if(mEndOfFile)
          return string("");

        // Was this a comment?
        if(c == '#')
          ReadComment();
        else
          break;
      }

      // Read the token (until the next white space)
      stringstream sstr;
      while((!mEndOfFile) && (!IsWhiteSpace(c)))
      {
        sstr << c;
        c = GetNextChar();
      }
      return sstr.str();
    }

  public:
    /// Constructor
    VRMLReader()
    {
      // Init read buffer
      mBufSize = 1024;
      mBuffer = new char[mBufSize];
      mBufActual = 0;
      mBufPos = 0;
      mEndOfFile = false;

      // Clear state
      mStream = 0;
      mComment = string("");
    }

    /// Destructor
    ~VRMLReader()
    {
      delete [] mBuffer;
    }

    /// Read the mesh from a file
    void ReadMesh(Mesh * aMesh)
    {
      // Read the header
      string header = GetNextLine();
      if(header.substr(0, 10) != string("#VRML V2.0"))
        throw runtime_error("Not a valid VRML 2.0 file.");

      // Read the rest of the file
      while(!mEndOfFile)
      {
        string token = GetNextToken();
        if(token == string("geometry"))
        {
          token = GetNextToken();
          if(token == string("IndexedFaceSet"))
          {
            // ...FIXME
          }
        }
      }

      // Set mesh comment (if any)
      aMesh->mComment = mComment;
    }

    /// Input file stream
    ifstream * mStream;
};


/// Import a mesh from a VRML 2.0 file.
void Import_WRL(const char * aFileName, Mesh * aMesh)
{
  // FIXME: The import functionality has not yet been fully implemented
  throw runtime_error("VRML import is not yet supported.");

  // Clear the mesh
  aMesh->Clear();

  // Open the input file
  ifstream f(aFileName, ios_base::in);
  if(f.fail())
    throw runtime_error("Could not open input file.");

  // Initialize the reader object
  VRMLReader reader;
  reader.mStream = &f;

  // Read the entire file...
  reader.ReadMesh(aMesh);

  // Close the input file
  f.close();
}

/// Export a mesh to a VRML 2.0 file.
void Export_WRL(const char * aFileName, Mesh * aMesh, Options &aOptions)
{
  // Open the output file
  ofstream f(aFileName, ios_base::out);
  if(f.fail())
    throw runtime_error("Could not open output file.");

  // Set floating point precision
  f << setprecision(8);

  // Write VRML file header ID
  f << "#VRML V2.0 utf8" << endl;

  // Write comment
  if(aMesh->mComment.size() > 0)
  {
    stringstream sstr(aMesh->mComment);
    sstr.seekg(0);
    while(!sstr.eof())
    {
      string line;
      getline(sstr, line);
      line = TrimString(line);
      if(line.size() > 0)
        f << "# " << line << endl;
    }
  }
  f << endl;

  // Write shape header
  f << "Group {" << endl;
  f << "\tchildren [" << endl;
  f << "\t\tShape {" << endl;
  f << "\t\t\tappearance Appearance {" << endl;
  f << "\t\t\t\tmaterial Material {" << endl;
  f << "\t\t\t\t\tdiffuseColor 1.0 1.0 1.0" << endl;
  f << "\t\t\t\t\tambientIntensity 0.2" << endl;
  f << "\t\t\t\t\tspecularColor 0.8 0.8 0.8" << endl;
  f << "\t\t\t\t\tshininess 0.4" << endl;
  f << "\t\t\t\t\ttransparency 0" << endl;
  f << "\t\t\t\t}" << endl;
  f << "\t\t\t}" << endl;
  f << "\t\t\tgeometry IndexedFaceSet {" << endl;
  f << "\t\t\t\tccw TRUE" << endl;
  f << "\t\t\t\tsolid FALSE" << endl;

  // Write vertices
  f << "\t\t\t\tcoord DEF co Coordinate {" << endl;
  f << "\t\t\t\t\tpoint [" << endl;
  for(unsigned int i = 0; i < aMesh->mVertices.size(); ++ i)
  {
    f << "\t\t\t\t\t\t" <<
         aMesh->mVertices[i].x << " " <<
         aMesh->mVertices[i].y << " " <<
         aMesh->mVertices[i].z << "," << endl;
  }
  f << "\t\t\t\t\t]" << endl;
  f << "\t\t\t\t}" << endl;

  // Write faces
  f << "\t\t\t\tcoordIndex [" << endl;
  unsigned int triCount = aMesh->mIndices.size() / 3;
  for(unsigned int i = 0; i < triCount; ++ i)
  {
    f << "\t\t\t\t\t" <<
         aMesh->mIndices[i * 3] << ", " <<
         aMesh->mIndices[i * 3 + 1] << ", " <<
         aMesh->mIndices[i * 3 + 2] << ", -1," << endl;
  }
  f << "\t\t\t\t]" << endl;

  // Write shape footer
  f << "\t\t\t}" << endl;
  f << "\t\t}" << endl;
  f << "\t]" << endl;
  f << "}" << endl;

  // Close the output file
  f.close();
}