1 | // ---------------------------------------------------------------------------
|
---|
2 | // Includes for all the program files to see
|
---|
3 | // ---------------------------------------------------------------------------
|
---|
4 | #include <string.h>
|
---|
5 | #include <stdlib.h>
|
---|
6 | #include <iostream>
|
---|
7 | using namespace std;
|
---|
8 | #include <xercesc/util/PlatformUtils.hpp>
|
---|
9 |
|
---|
10 | // ---------------------------------------------------------------------------
|
---|
11 | // Includes
|
---|
12 | // ---------------------------------------------------------------------------
|
---|
13 | #include <xercesc/framework/StdInInputSource.hpp>
|
---|
14 | #include <xercesc/parsers/SAXParser.hpp>
|
---|
15 | #include <xercesc/util/OutOfMemoryException.hpp>
|
---|
16 | #include <xercesc/util/BinFileInputStream.hpp>
|
---|
17 | // ---------------------------------------------------------------------------
|
---|
18 | // Includes
|
---|
19 | // ---------------------------------------------------------------------------
|
---|
20 | #include <xercesc/sax/AttributeList.hpp>
|
---|
21 | #include <xercesc/sax/SAXParseException.hpp>
|
---|
22 | #include <xercesc/sax/SAXException.hpp>
|
---|
23 |
|
---|
24 | #include "X3dParser.h"
|
---|
25 |
|
---|
26 | #include "X3dParserXerces.h"
|
---|
27 | #include "Mesh.h"
|
---|
28 | #include "SceneGraph.h"
|
---|
29 | #include "Triangle3.h"
|
---|
30 | #include "ViewCellsManager.h"
|
---|
31 | #include "ResourceManager.h"
|
---|
32 | #include <assert.h>
|
---|
33 |
|
---|
34 | namespace GtpVisibilityPreprocessor {
|
---|
35 |
|
---|
36 | float X3dParser::DEFAULT_VIEWCELL_HEIGHT = 5.0f;
|
---|
37 |
|
---|
38 | // ---------------------------------------------------------------------------
|
---|
39 | // Local data
|
---|
40 | //
|
---|
41 | // doNamespaces
|
---|
42 | // Indicates whether namespace processing should be enabled or not.
|
---|
43 | // The default is no, but -n overrides that.
|
---|
44 | //
|
---|
45 | // doSchema
|
---|
46 | // Indicates whether schema processing should be enabled or not.
|
---|
47 | // The default is no, but -s overrides that.
|
---|
48 | //
|
---|
49 | // schemaFullChecking
|
---|
50 | // Indicates whether full schema constraint checking should be enabled or not.
|
---|
51 | // The default is no, but -s overrides that.
|
---|
52 | //
|
---|
53 | // valScheme
|
---|
54 | // Indicates what validation scheme to use. It defaults to 'auto', but
|
---|
55 | // can be set via the -v= command.
|
---|
56 | // ---------------------------------------------------------------------------
|
---|
57 | static bool doNamespaces = false;
|
---|
58 | static bool doSchema = false;
|
---|
59 | static bool schemaFullChecking = false;
|
---|
60 | static SAXParser::ValSchemes valScheme = SAXParser::Val_Auto;
|
---|
61 |
|
---|
62 | #define ROTATE_SCENE 0
|
---|
63 |
|
---|
64 | static int sUniqueMeshIdx = 0;
|
---|
65 |
|
---|
66 | // hack: rotate mesh by n degrees
|
---|
67 | static void RotateMesh(Mesh *mesh, const float angle)
|
---|
68 | {
|
---|
69 | VertexContainer::iterator it, it_end = mesh->mVertices.end();
|
---|
70 |
|
---|
71 | const float angleRad = angle * PI / 180.0f;
|
---|
72 | const Matrix4x4 rot = RotationYMatrix(angleRad);
|
---|
73 |
|
---|
74 | for (it = mesh->mVertices.begin(); it != it_end; ++ it)
|
---|
75 | {
|
---|
76 | (*it) = rot * (*it);
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | // ---------------------------------------------------------------------------
|
---|
81 | // StdInParseHandlers: Constructors and Destructor
|
---|
82 | // ---------------------------------------------------------------------------
|
---|
83 | X3dParseHandlers::X3dParseHandlers(SceneGraphNode *root,
|
---|
84 | const bool loadPolygonsAsMeshes):
|
---|
85 | mElementCount(0)
|
---|
86 | , mAttrCount(0)
|
---|
87 | , mCharacterCount(0)
|
---|
88 | , mSpaceCount(0)
|
---|
89 | , mLoadPolygonsAsMeshes(loadPolygonsAsMeshes)
|
---|
90 | , mCurrentMesh(NULL)
|
---|
91 | {
|
---|
92 | mCurrentNode = root;
|
---|
93 |
|
---|
94 | // this matrix should never be removed from stack
|
---|
95 | //mTransformations.push(IdentityMatrix());
|
---|
96 | }
|
---|
97 |
|
---|
98 | X3dParseHandlers::~X3dParseHandlers()
|
---|
99 | {
|
---|
100 | assert(mTransformations.empty());
|
---|
101 | if (0 && !mTransformations.empty())
|
---|
102 | cout << "error: transformation stack size: " << (int)mTransformations.size() << endl;
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | // ---------------------------------------------------------------------------
|
---|
107 | // StdInParseHandlers: Implementation of the SAX DocumentHandler interface
|
---|
108 | // ---------------------------------------------------------------------------
|
---|
109 |
|
---|
110 | void X3dParseHandlers::endElement(const XMLCh* const name)
|
---|
111 | {
|
---|
112 | StrX lname(name);
|
---|
113 | string element(lname.LocalForm());
|
---|
114 |
|
---|
115 | // only create new mesh instance if define mechanism was not used
|
---|
116 | if (element == "Shape")
|
---|
117 | EndShape();
|
---|
118 |
|
---|
119 | if (element == "Transform")
|
---|
120 | EndTransform();
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 | void X3dParseHandlers::ApplyTransformations(TrafoStack trafos, Mesh *mesh) const
|
---|
125 | {
|
---|
126 | while (!trafos.empty())
|
---|
127 | {
|
---|
128 | const Matrix4x4 m = trafos.top();
|
---|
129 | trafos.pop();
|
---|
130 |
|
---|
131 | mesh->ApplyTransformation(m);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | void X3dParseHandlers::ApplyTransformations(TrafoStack trafos,
|
---|
137 | TransformedMeshInstance *mi) const
|
---|
138 | {
|
---|
139 | while (!trafos.empty())
|
---|
140 | {
|
---|
141 | const Matrix4x4 m = trafos.top();
|
---|
142 | trafos.pop();
|
---|
143 |
|
---|
144 | mi->ApplyWorldTransform(m);
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | void X3dParseHandlers::StartTransform(AttributeList& attributes)
|
---|
150 | {
|
---|
151 | Matrix4x4 currentTransform = IdentityMatrix();
|
---|
152 |
|
---|
153 | const int len = attributes.getLength();
|
---|
154 |
|
---|
155 | Matrix4x4 *rotm = NULL;
|
---|
156 | Matrix4x4 *scalem = NULL;
|
---|
157 | Matrix4x4 *translm = NULL;
|
---|
158 |
|
---|
159 | for (int i = 0; i < len; ++ i)
|
---|
160 | {
|
---|
161 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
162 |
|
---|
163 | StrX attrValue(attributes.getValue(i));
|
---|
164 | const char *ptr = attrValue.LocalForm();
|
---|
165 |
|
---|
166 | if (attrName == "rotation")
|
---|
167 | {
|
---|
168 | Vector3 axis;
|
---|
169 | float angle;
|
---|
170 |
|
---|
171 | if (sscanf(ptr, "%f %f %f %f", &axis.x, &axis.y, &axis.z, &angle) == 4)
|
---|
172 | {
|
---|
173 | rotm = new Matrix4x4(RotationAxisMatrix(axis, angle));
|
---|
174 | }
|
---|
175 | }
|
---|
176 | else if (attrName == "translation")
|
---|
177 | {
|
---|
178 | Vector3 transl;
|
---|
179 |
|
---|
180 | if (sscanf(ptr, "%f %f %f", &transl.x, &transl.y, &transl.z) == 3)
|
---|
181 | {
|
---|
182 | translm = new Matrix4x4(TranslationMatrix(transl));
|
---|
183 | }
|
---|
184 | }
|
---|
185 | else if (attrName == "scale")
|
---|
186 | {
|
---|
187 | Vector3 scale;
|
---|
188 |
|
---|
189 | if (sscanf(ptr, "%f %f %f", &scale.x, &scale.y, &scale.z) == 3)
|
---|
190 | {
|
---|
191 | scalem = new Matrix4x4(ScaleMatrix(scale.x, scale.y, scale.z));
|
---|
192 | }
|
---|
193 | }
|
---|
194 | // todo: scale orientation
|
---|
195 | }
|
---|
196 |
|
---|
197 | if (scalem)
|
---|
198 | currentTransform *= (*scalem);
|
---|
199 | if (rotm)
|
---|
200 | currentTransform *= (*rotm);
|
---|
201 | if (translm)
|
---|
202 | currentTransform *= (*translm);
|
---|
203 |
|
---|
204 | DEL_PTR(scalem);
|
---|
205 | DEL_PTR(rotm);
|
---|
206 | DEL_PTR(translm);
|
---|
207 |
|
---|
208 | mTransformations.push(currentTransform);
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | void X3dParseHandlers::EndTransform()
|
---|
213 | {
|
---|
214 | mTransformations.pop();
|
---|
215 | }
|
---|
216 |
|
---|
217 |
|
---|
218 | void X3dParseHandlers::EndShape()
|
---|
219 | {
|
---|
220 | //-- if shape is merely a definition => don't create particular mesh instance
|
---|
221 | if (!mCurrentMesh || mIsMeshDefinition)
|
---|
222 | {
|
---|
223 | return;
|
---|
224 | }
|
---|
225 |
|
---|
226 |
|
---|
227 | //-- each polygon is one single mesh
|
---|
228 | if (mLoadPolygonsAsMeshes)
|
---|
229 | {
|
---|
230 | cout << "m";
|
---|
231 | FaceContainer::const_iterator fit, fit_end = mCurrentMesh->mFaces.end();
|
---|
232 |
|
---|
233 | for (fit = mCurrentMesh->mFaces.begin(); fit != fit_end; ++ fit)
|
---|
234 | {
|
---|
235 | cout << "f";
|
---|
236 | Face *face = *fit;
|
---|
237 |
|
---|
238 | // we create a new mesh for each face from the current mesh
|
---|
239 | Mesh *mesh = MeshManager::GetSingleton()->CreateResource();
|
---|
240 |
|
---|
241 | VertexIndexContainer::const_iterator vit, vit_end = face->mVertexIndices.end();
|
---|
242 |
|
---|
243 | int i = 0;
|
---|
244 | // dummy vertex indices container
|
---|
245 | VertexIndexContainer vcIndices;
|
---|
246 |
|
---|
247 | for (vit = face->mVertexIndices.begin(); vit != vit_end; ++ vit, ++ i)
|
---|
248 | {
|
---|
249 | cout << "i";
|
---|
250 | const int index = (*vit);
|
---|
251 |
|
---|
252 | // add vertices
|
---|
253 | mesh->mVertices.push_back(mCurrentMesh->mVertices[index]);
|
---|
254 |
|
---|
255 | // indices don't make much sense if mesh == face,
|
---|
256 | // but we need them anyway ...
|
---|
257 | vcIndices.push_back(i);
|
---|
258 | }
|
---|
259 |
|
---|
260 | mesh->mFaces.push_back(new Face(vcIndices));
|
---|
261 |
|
---|
262 | // write transformations directly in mesh
|
---|
263 | // note: could be transformed in parent mesh, save some transformations
|
---|
264 | ApplyTransformations(mTransformations, mesh);
|
---|
265 |
|
---|
266 | mesh->Preprocess();
|
---|
267 |
|
---|
268 | if (mesh->mFaces.empty())
|
---|
269 | {
|
---|
270 | cout << "error: empy mesh" << endl;
|
---|
271 | }
|
---|
272 | else
|
---|
273 | {
|
---|
274 | // make an instance of this mesh
|
---|
275 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
276 | mCurrentNode->mGeometry.push_back(mi);
|
---|
277 |
|
---|
278 | if (mCurrentMaterial)
|
---|
279 | {
|
---|
280 | // HACK: add the material to the mesh directly if no material yet
|
---|
281 | if (!mCurrentMesh->mMaterial)
|
---|
282 | mCurrentMesh->mMaterial = mCurrentMaterial;
|
---|
283 | }
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | // this mesh is not needed, unless it is used as a definition
|
---|
288 | if (!mUsingMeshDefinition)
|
---|
289 | {
|
---|
290 | MeshManager::GetSingleton()->DestroyEntry(mCurrentMesh->GetId());
|
---|
291 | }
|
---|
292 | }
|
---|
293 | else // default usage: create a mesh instance from the current mesh
|
---|
294 | {
|
---|
295 | MeshInstance *mi;
|
---|
296 |
|
---|
297 | if (!mUsingMeshDefinition)
|
---|
298 | {
|
---|
299 | // make an instance of this mesh
|
---|
300 | mi = new MeshInstance(mCurrentMesh);
|
---|
301 |
|
---|
302 | // this mesh is used only once => write transformations directly into it
|
---|
303 | ApplyTransformations(mTransformations, mCurrentMesh);
|
---|
304 | }
|
---|
305 | else
|
---|
306 | {
|
---|
307 | // make an instance of this mesh
|
---|
308 | TransformedMeshInstance *tmi = new TransformedMeshInstance(mCurrentMesh);
|
---|
309 |
|
---|
310 | // apply transformation on the instance of the mesh
|
---|
311 | ApplyTransformations(mTransformations, tmi);
|
---|
312 | mi = tmi;
|
---|
313 | }
|
---|
314 |
|
---|
315 | if (mCurrentMaterial)
|
---|
316 | {
|
---|
317 | // HACK: add the material to the mesh directly if no material yet
|
---|
318 | if (!mCurrentMesh->mMaterial)
|
---|
319 | {
|
---|
320 | mCurrentMesh->mMaterial = mCurrentMaterial;
|
---|
321 | }
|
---|
322 | else // add material to the instance
|
---|
323 | {
|
---|
324 | mi->SetMaterial(mCurrentMaterial);
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | // create local mesh kd tree
|
---|
329 | mCurrentMesh->Preprocess();
|
---|
330 |
|
---|
331 | if (mCurrentMesh->mFaces.empty())
|
---|
332 | {
|
---|
333 | cout << "warning: empy mesh!!" << endl;
|
---|
334 | delete mi;
|
---|
335 | }
|
---|
336 | else
|
---|
337 | {
|
---|
338 | // add to scene graph
|
---|
339 | mCurrentNode->mGeometry.push_back(mi);
|
---|
340 | }
|
---|
341 |
|
---|
342 | // reset current mesh
|
---|
343 | mCurrentMesh = NULL;
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 |
|
---|
348 | void X3dParseHandlers::StartIndexedFaceSet(AttributeList& attributes)
|
---|
349 | {
|
---|
350 | //-- indexedfaceset corresponds to Mesh in our implementation
|
---|
351 | const int len = attributes.getLength();
|
---|
352 |
|
---|
353 | VertexIndexContainer vertices;
|
---|
354 |
|
---|
355 | mIsMeshDefinition = false;
|
---|
356 | mUsingMeshDefinition = false;
|
---|
357 |
|
---|
358 | for (int i = 0; i < len; ++ i)
|
---|
359 | {
|
---|
360 | const string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
361 |
|
---|
362 | //-- we use an already defined mesh
|
---|
363 | if (attrName == "USE")
|
---|
364 | {
|
---|
365 | StrX attrValue(attributes.getValue(i));
|
---|
366 | const char *meshName = attrValue.LocalForm();
|
---|
367 |
|
---|
368 | mUsingMeshDefinition = true;
|
---|
369 |
|
---|
370 | // retrieve mesh from mesh container
|
---|
371 | const int meshIdx = mMeshDefinitions[meshName];
|
---|
372 |
|
---|
373 | mCurrentMesh =
|
---|
374 | MeshManager::GetSingleton()->FindEntry(meshIdx);
|
---|
375 |
|
---|
376 | //Debug << "retrieving mesh definition: " << mCurrentMeshName << endl;
|
---|
377 | cout << "u";
|
---|
378 | }
|
---|
379 | else if (attrName == "DEF") //-- a definition of a mesh
|
---|
380 | {
|
---|
381 | const StrX attrValue(attributes.getValue(i));
|
---|
382 | const char *meshName = attrValue.LocalForm();
|
---|
383 |
|
---|
384 | // this is only a definition, don't create actual instance
|
---|
385 | mIsMeshDefinition = true;
|
---|
386 |
|
---|
387 | //-- create new mesh definition
|
---|
388 | mCurrentMesh = MeshManager::GetSingleton()->CreateResource();
|
---|
389 |
|
---|
390 | // store the mesh defination in a lookup table
|
---|
391 | mMeshDefinitions[meshName] = mCurrentMesh->GetId();
|
---|
392 | cout << "d";
|
---|
393 | }
|
---|
394 |
|
---|
395 | //-- read coordinate indices for current mesh
|
---|
396 | else if (attrName == "coordIndex")
|
---|
397 | {
|
---|
398 | StrX attrValue(attributes.getValue(i));
|
---|
399 | const char *ptr = attrValue.LocalForm();
|
---|
400 |
|
---|
401 | //-- immediate use: create a new mesh using a generic name
|
---|
402 | if (!mCurrentMesh)
|
---|
403 | {
|
---|
404 | mCurrentMesh = MeshManager::GetSingleton()->CreateResource();
|
---|
405 | }
|
---|
406 |
|
---|
407 | // handle coordIndex
|
---|
408 | vertices.clear();
|
---|
409 |
|
---|
410 |
|
---|
411 | char *endptr;
|
---|
412 |
|
---|
413 | while (1)
|
---|
414 | {
|
---|
415 | int index = strtol(ptr, &endptr, 10);
|
---|
416 |
|
---|
417 | if (ptr == endptr || index == -1)
|
---|
418 | {
|
---|
419 | if (vertices.size() > 2)
|
---|
420 | {
|
---|
421 | Face *face = new Face(vertices);
|
---|
422 | mCurrentMesh->mFaces.push_back(face);
|
---|
423 | }
|
---|
424 |
|
---|
425 | vertices.clear();
|
---|
426 |
|
---|
427 | if (ptr == endptr)
|
---|
428 | break;
|
---|
429 |
|
---|
430 | }
|
---|
431 | else
|
---|
432 | {
|
---|
433 | vertices.push_back(index);
|
---|
434 | }
|
---|
435 | ptr = endptr;
|
---|
436 | }
|
---|
437 | }
|
---|
438 | }
|
---|
439 | }
|
---|
440 |
|
---|
441 |
|
---|
442 | void
|
---|
443 | X3dParseHandlers::StartMaterial(AttributeList& attributes)
|
---|
444 | {
|
---|
445 | const int len = attributes.getLength();
|
---|
446 |
|
---|
447 | mCurrentMaterial = MaterialManager::GetSingleton()->CreateResource();
|
---|
448 |
|
---|
449 | for (int i = 0; i < len; ++ i)
|
---|
450 | {
|
---|
451 | const string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
452 |
|
---|
453 | const StrX attrValue(attributes.getValue(i));
|
---|
454 | const char *ptr = attrValue.LocalForm();
|
---|
455 |
|
---|
456 |
|
---|
457 | //-- we use an already defined material
|
---|
458 | if (attrName == "USE")
|
---|
459 | {
|
---|
460 | //mUsingMaterialDefinition = true;
|
---|
461 | string matName(ptr);
|
---|
462 |
|
---|
463 | // retrieve mesh from mesh container
|
---|
464 | const int matIdx = mMaterialDefinitions[matName];
|
---|
465 |
|
---|
466 | mCurrentMaterial =
|
---|
467 | MaterialManager::GetSingleton()->FindEntry(matIdx);
|
---|
468 |
|
---|
469 | //Debug << "retrieving mesh definition: " << mCurrentMeshName << endl;
|
---|
470 | cout << "u";
|
---|
471 | }
|
---|
472 | else if (attrName == "DEF") //-- a definition of a material
|
---|
473 | {
|
---|
474 | //mIsMaterialDefinition = true;
|
---|
475 | string matName(ptr);
|
---|
476 |
|
---|
477 | //-- create new material definition
|
---|
478 | mCurrentMaterial = MaterialManager::GetSingleton()->CreateResource();
|
---|
479 | // store the mesh defination in a lookup table
|
---|
480 | mMaterialDefinitions[matName] = mCurrentMaterial->GetId();
|
---|
481 | cout << "d";
|
---|
482 | }
|
---|
483 | // TODO: support not only diffuse material
|
---|
484 | else if (attrName == "diffuseColor")
|
---|
485 | {
|
---|
486 | float r, g, b;
|
---|
487 |
|
---|
488 | if (sscanf(ptr, "%f %f %f", &r, &g, &b) == 3)
|
---|
489 | mCurrentMaterial->mDiffuseColor = RgbColor(r, g, b);
|
---|
490 | }
|
---|
491 | }
|
---|
492 | }
|
---|
493 |
|
---|
494 |
|
---|
495 | void
|
---|
496 | X3dParseHandlers::StartCoordinate(AttributeList& attributes)
|
---|
497 | {
|
---|
498 | const int len = attributes.getLength();
|
---|
499 |
|
---|
500 | int i;
|
---|
501 | VertexContainer vertices;
|
---|
502 |
|
---|
503 | for (i=0; i < len; i++)
|
---|
504 | {
|
---|
505 | const string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
506 |
|
---|
507 | if (attrName == "point")
|
---|
508 | {
|
---|
509 | StrX attrValue(attributes.getValue(i));
|
---|
510 |
|
---|
511 |
|
---|
512 | const char *ptr = attrValue.LocalForm();
|
---|
513 | char *endptr;
|
---|
514 |
|
---|
515 |
|
---|
516 | while (1)
|
---|
517 | {
|
---|
518 | float x = (float)strtod(ptr, &endptr);
|
---|
519 |
|
---|
520 | if (ptr == endptr)
|
---|
521 | break;
|
---|
522 |
|
---|
523 | ptr = endptr;
|
---|
524 |
|
---|
525 | float y = (float)strtod(ptr, &endptr);
|
---|
526 |
|
---|
527 | if (ptr == endptr)
|
---|
528 | break;
|
---|
529 |
|
---|
530 | ptr = endptr;
|
---|
531 |
|
---|
532 | float z = (float)strtod(ptr, &endptr);
|
---|
533 | if (ptr == endptr)
|
---|
534 | break;
|
---|
535 |
|
---|
536 | ptr = endptr;
|
---|
537 |
|
---|
538 | if (*ptr == ',')
|
---|
539 | ptr ++;
|
---|
540 |
|
---|
541 | Vector3 v(x, y, z);
|
---|
542 | vertices.push_back(v);
|
---|
543 | }
|
---|
544 |
|
---|
545 | // substitute vertices into current mesh
|
---|
546 | mCurrentMesh->mVertices = vertices;
|
---|
547 | }
|
---|
548 | }
|
---|
549 | }
|
---|
550 |
|
---|
551 |
|
---|
552 | void
|
---|
553 | X3dParseHandlers::startElement(const XMLCh* const name,
|
---|
554 | AttributeList& attributes)
|
---|
555 | {
|
---|
556 | StrX lname(name);
|
---|
557 | string element(lname.LocalForm());
|
---|
558 |
|
---|
559 | if (element == "IndexedFaceSet") {
|
---|
560 | // create a new mesh node in the scene graph
|
---|
561 | StartIndexedFaceSet(attributes);
|
---|
562 | }
|
---|
563 |
|
---|
564 | if (element == "Shape") {
|
---|
565 | cout << "+";
|
---|
566 |
|
---|
567 | // reset current shape values
|
---|
568 | mCurrentMesh = NULL;
|
---|
569 | mCurrentMaterial = NULL;
|
---|
570 | mCurrentVertexIndices.clear();
|
---|
571 | //mCurrentVertices.clear();
|
---|
572 | }
|
---|
573 |
|
---|
574 | if (element == "Coordinate") {
|
---|
575 | StartCoordinate(attributes);
|
---|
576 | }
|
---|
577 |
|
---|
578 | // todo
|
---|
579 | if (element == "Material") {
|
---|
580 | StartMaterial(attributes);
|
---|
581 | }
|
---|
582 |
|
---|
583 | if (element == "Transform") {
|
---|
584 | StartTransform(attributes);
|
---|
585 | }
|
---|
586 |
|
---|
587 | ++ mElementCount;
|
---|
588 | mAttrCount += attributes.getLength();
|
---|
589 | }
|
---|
590 |
|
---|
591 | void
|
---|
592 | X3dParseHandlers::characters(const XMLCh* const chars,
|
---|
593 | const unsigned int length)
|
---|
594 | {
|
---|
595 | mCharacterCount += length;
|
---|
596 | }
|
---|
597 |
|
---|
598 | void
|
---|
599 | X3dParseHandlers::ignorableWhitespace(const XMLCh* const chars,
|
---|
600 | const unsigned int length)
|
---|
601 | {
|
---|
602 | mSpaceCount += length;
|
---|
603 | }
|
---|
604 |
|
---|
605 | void
|
---|
606 | X3dParseHandlers::resetDocument()
|
---|
607 | {
|
---|
608 | mAttrCount = 0;
|
---|
609 | mCharacterCount = 0;
|
---|
610 | mElementCount = 0;
|
---|
611 | mSpaceCount = 0;
|
---|
612 | }
|
---|
613 |
|
---|
614 |
|
---|
615 | // ---------------------------------------------------------------------------
|
---|
616 | // StdInParseHandlers: Overrides of the SAX ErrorHandler interface
|
---|
617 | // ---------------------------------------------------------------------------
|
---|
618 |
|
---|
619 |
|
---|
620 | void
|
---|
621 | X3dParseHandlers::error(const SAXParseException& e)
|
---|
622 | {
|
---|
623 | XERCES_STD_QUALIFIER cerr << "\nError at (file " << StrX(e.getSystemId())
|
---|
624 | << ", line " << e.getLineNumber()
|
---|
625 | << ", char " << e.getColumnNumber()
|
---|
626 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
627 | }
|
---|
628 |
|
---|
629 | void
|
---|
630 | X3dParseHandlers::fatalError(const SAXParseException& e)
|
---|
631 | {
|
---|
632 | XERCES_STD_QUALIFIER cerr << "\nFatal Error at (file " << StrX(e.getSystemId())
|
---|
633 | << ", line " << e.getLineNumber()
|
---|
634 | << ", char " << e.getColumnNumber()
|
---|
635 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
636 | }
|
---|
637 |
|
---|
638 | void
|
---|
639 | X3dParseHandlers::warning(const SAXParseException& e)
|
---|
640 | {
|
---|
641 | XERCES_STD_QUALIFIER cerr << "\nWarning at (file " << StrX(e.getSystemId())
|
---|
642 | << ", line " << e.getLineNumber()
|
---|
643 | << ", char " << e.getColumnNumber()
|
---|
644 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
645 | }
|
---|
646 |
|
---|
647 | /*************************************************************************/
|
---|
648 | /* X3dParser implementation */
|
---|
649 | /*******************+*****************************************************/
|
---|
650 |
|
---|
651 | X3dParser::X3dParser(): mViewCellHeight(DEFAULT_VIEWCELL_HEIGHT)
|
---|
652 | {}
|
---|
653 |
|
---|
654 |
|
---|
655 | bool
|
---|
656 | X3dParser::ParseFile(const string filename,
|
---|
657 | SceneGraphNode *root,
|
---|
658 | const bool loadPolygonsAsMeshes,
|
---|
659 | vector<FaceParentInfo> *parents)
|
---|
660 | {
|
---|
661 | // Initialize the XML4C system
|
---|
662 | try {
|
---|
663 | XMLPlatformUtils::Initialize();
|
---|
664 | }
|
---|
665 |
|
---|
666 | catch (const XMLException& toCatch)
|
---|
667 | {
|
---|
668 | XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
|
---|
669 | << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
670 | return false;
|
---|
671 | }
|
---|
672 |
|
---|
673 |
|
---|
674 | //
|
---|
675 | // Create a SAX parser object. Then, according to what we were told on
|
---|
676 | // the command line, set the options.
|
---|
677 | //
|
---|
678 | SAXParser* parser = new SAXParser;
|
---|
679 | parser->setValidationScheme(valScheme);
|
---|
680 | parser->setDoNamespaces(doNamespaces);
|
---|
681 | parser->setDoSchema(doSchema);
|
---|
682 | parser->setValidationSchemaFullChecking(schemaFullChecking);
|
---|
683 |
|
---|
684 |
|
---|
685 | //
|
---|
686 | // Create our SAX handler object and install it on the parser, as the
|
---|
687 | // document and error handler. We are responsible for cleaning them
|
---|
688 | // up, but since its just stack based here, there's nothing special
|
---|
689 | // to do.
|
---|
690 | //
|
---|
691 | X3dParseHandlers handler(root, loadPolygonsAsMeshes);
|
---|
692 | parser->setDocumentHandler(&handler);
|
---|
693 | parser->setErrorHandler(&handler);
|
---|
694 |
|
---|
695 | unsigned long duration;
|
---|
696 | int errorCount = 0;
|
---|
697 | // create a faux scope so that 'src' destructor is called before
|
---|
698 | // XMLPlatformUtils::Terminate
|
---|
699 | {
|
---|
700 | //
|
---|
701 | // Kick off the parse and catch any exceptions. Create a standard
|
---|
702 | // input input source and tell the parser to parse from that.
|
---|
703 | //
|
---|
704 | // StdInInputSource src;
|
---|
705 | try
|
---|
706 | {
|
---|
707 | const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
708 | parser->parse(filename.c_str());
|
---|
709 |
|
---|
710 | const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
711 | duration = endMillis - startMillis;
|
---|
712 | errorCount = parser->getErrorCount();
|
---|
713 | }
|
---|
714 | catch (const OutOfMemoryException&)
|
---|
715 | {
|
---|
716 | XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
|
---|
717 | errorCount = 2;
|
---|
718 | return false;
|
---|
719 | }
|
---|
720 | catch (const XMLException& e)
|
---|
721 | {
|
---|
722 | XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
|
---|
723 | << StrX(e.getMessage())
|
---|
724 | << "\n" << XERCES_STD_QUALIFIER endl;
|
---|
725 | errorCount = 1;
|
---|
726 | return false;
|
---|
727 | }
|
---|
728 |
|
---|
729 |
|
---|
730 | // Print out the stats that we collected and time taken
|
---|
731 | if (!errorCount) {
|
---|
732 | XERCES_STD_QUALIFIER cout << filename << ": " << duration << " ms ("
|
---|
733 | << handler.GetElementCount() << " elems, "
|
---|
734 | << handler.GetAttrCount() << " attrs, "
|
---|
735 | << handler.GetSpaceCount() << " spaces, "
|
---|
736 | << handler.GetCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
|
---|
737 | }
|
---|
738 | }
|
---|
739 |
|
---|
740 | //
|
---|
741 | // Delete the parser itself. Must be done prior to calling Terminate, below.
|
---|
742 | //
|
---|
743 | delete parser;
|
---|
744 |
|
---|
745 | XMLPlatformUtils::Terminate();
|
---|
746 |
|
---|
747 | if (errorCount > 0)
|
---|
748 | return false;
|
---|
749 | else
|
---|
750 | return true;
|
---|
751 | }
|
---|
752 |
|
---|
753 |
|
---|
754 |
|
---|
755 | /************************************************************************/
|
---|
756 | /* class X3dViewCellsParseHandlers implementation */
|
---|
757 | /************************************************************************/
|
---|
758 |
|
---|
759 |
|
---|
760 | // ---------------------------------------------------------------------------
|
---|
761 | // StdInParseHandlers: Constructors and Destructor
|
---|
762 | // ---------------------------------------------------------------------------
|
---|
763 | X3dViewCellsParseHandlers::X3dViewCellsParseHandlers(ViewCellsManager *viewCellsManager,
|
---|
764 | const float viewCellHeight):
|
---|
765 | mElementCount(0),
|
---|
766 | mAttrCount(0),
|
---|
767 | mCharacterCount(0),
|
---|
768 | mSpaceCount(0),
|
---|
769 | mViewCellsManager(viewCellsManager),
|
---|
770 | mViewCellHeight(viewCellHeight)
|
---|
771 | {
|
---|
772 | }
|
---|
773 |
|
---|
774 | X3dViewCellsParseHandlers::~X3dViewCellsParseHandlers()
|
---|
775 | {
|
---|
776 | }
|
---|
777 |
|
---|
778 |
|
---|
779 | // ---------------------------------------------------------------------------
|
---|
780 | // StdInParseHandlers: Implementation of the SAX DocumentHandler interface
|
---|
781 | // ---------------------------------------------------------------------------
|
---|
782 | void X3dViewCellsParseHandlers::endElement(const XMLCh* const name)
|
---|
783 | {
|
---|
784 | StrX lname(name);
|
---|
785 |
|
---|
786 | string element(lname.LocalForm());
|
---|
787 |
|
---|
788 | if (element == "Shape")
|
---|
789 | EndShape();
|
---|
790 | }
|
---|
791 |
|
---|
792 | void
|
---|
793 | X3dViewCellsParseHandlers::EndShape()
|
---|
794 | {
|
---|
795 | // currently processing no shape
|
---|
796 | }
|
---|
797 |
|
---|
798 | void
|
---|
799 | X3dViewCellsParseHandlers::StartIndexedFaceSet(
|
---|
800 | AttributeList& attributes)
|
---|
801 | {
|
---|
802 | int len = attributes.getLength();
|
---|
803 | int i;
|
---|
804 |
|
---|
805 | for (i=0; i < len; i++)
|
---|
806 | {
|
---|
807 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
808 |
|
---|
809 |
|
---|
810 | if (attrName == "coordIndex")
|
---|
811 | {
|
---|
812 | StrX attrValue(attributes.getValue(i));
|
---|
813 |
|
---|
814 | // handle coordIndex
|
---|
815 | const char *ptr = attrValue.LocalForm();
|
---|
816 | char *endptr;
|
---|
817 |
|
---|
818 | while (1)
|
---|
819 | {
|
---|
820 | int index = strtol(ptr, &endptr, 10);
|
---|
821 |
|
---|
822 | if (ptr == endptr)
|
---|
823 | break;
|
---|
824 |
|
---|
825 | if (index != -1)
|
---|
826 | {
|
---|
827 | mCurrentVertexIndices.push_back(index);
|
---|
828 | }
|
---|
829 |
|
---|
830 | ptr = endptr;
|
---|
831 | }
|
---|
832 | }
|
---|
833 | }
|
---|
834 | }
|
---|
835 |
|
---|
836 |
|
---|
837 | void
|
---|
838 | X3dViewCellsParseHandlers::StartCoordinate(AttributeList& attributes)
|
---|
839 | {
|
---|
840 | int len = attributes.getLength();
|
---|
841 |
|
---|
842 | VertexContainer vertices;
|
---|
843 | int i;
|
---|
844 | for (i=0; i < len; i++)
|
---|
845 | {
|
---|
846 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
847 |
|
---|
848 | if (attrName == "point")
|
---|
849 | {
|
---|
850 | StrX attrValue(attributes.getValue(i));
|
---|
851 |
|
---|
852 | const char *ptr = attrValue.LocalForm();
|
---|
853 |
|
---|
854 | char *endptr;
|
---|
855 |
|
---|
856 | while (1)
|
---|
857 | {
|
---|
858 | float x = (float)strtod(ptr, &endptr);
|
---|
859 |
|
---|
860 | if (ptr == endptr)
|
---|
861 | break;
|
---|
862 | ptr = endptr;
|
---|
863 |
|
---|
864 | float y = (float)(float)strtod(ptr, &endptr);
|
---|
865 |
|
---|
866 |
|
---|
867 | if (ptr == endptr)
|
---|
868 | break;
|
---|
869 | ptr = endptr;
|
---|
870 |
|
---|
871 | float z = (float)(float)strtod(ptr, &endptr);
|
---|
872 |
|
---|
873 | if (ptr == endptr)
|
---|
874 | break;
|
---|
875 |
|
---|
876 | ptr = endptr;
|
---|
877 | if (*ptr == ',')
|
---|
878 | ptr++;
|
---|
879 |
|
---|
880 | Vector3 v(x, y, z);
|
---|
881 | vertices.push_back(v);
|
---|
882 | }
|
---|
883 | }
|
---|
884 | }
|
---|
885 |
|
---|
886 | for (i = 0; i < mCurrentVertexIndices.size(); i += 3)
|
---|
887 | {
|
---|
888 | Triangle3 baseTri(vertices[mCurrentVertexIndices[i + 0]],
|
---|
889 | vertices[mCurrentVertexIndices[i + 1]],
|
---|
890 | vertices[mCurrentVertexIndices[i + 2]]);
|
---|
891 |
|
---|
892 | // create view cell from base triangle
|
---|
893 | mViewCellsManager->AddViewCell(
|
---|
894 | mViewCellsManager->ExtrudeViewCell(baseTri,
|
---|
895 | mViewCellHeight));
|
---|
896 | }
|
---|
897 | }
|
---|
898 |
|
---|
899 |
|
---|
900 | void
|
---|
901 | X3dViewCellsParseHandlers::startElement(const XMLCh* const name,
|
---|
902 | AttributeList& attributes)
|
---|
903 | {
|
---|
904 | StrX lname(name);
|
---|
905 | string element(lname.LocalForm());
|
---|
906 |
|
---|
907 | if (element == "IndexedFaceSet") {
|
---|
908 | // create the viewcells from individual triangles
|
---|
909 | StartIndexedFaceSet(attributes);
|
---|
910 | }
|
---|
911 |
|
---|
912 | if (element == "Coordinate") {
|
---|
913 | // add coordinates to the triangles
|
---|
914 | StartCoordinate(attributes);
|
---|
915 | }
|
---|
916 | // do nothing
|
---|
917 | //if (element == "Shape") {}
|
---|
918 | // ignore material
|
---|
919 | //if (element == "Material") {}
|
---|
920 |
|
---|
921 | ++ mElementCount;
|
---|
922 | mAttrCount += attributes.getLength();
|
---|
923 | }
|
---|
924 |
|
---|
925 | void
|
---|
926 | X3dViewCellsParseHandlers::characters(const XMLCh* const chars,
|
---|
927 | const unsigned int length)
|
---|
928 | {
|
---|
929 | mCharacterCount += length;
|
---|
930 | }
|
---|
931 |
|
---|
932 | void
|
---|
933 | X3dViewCellsParseHandlers::ignorableWhitespace(const XMLCh* const chars,
|
---|
934 | const unsigned int length)
|
---|
935 | {
|
---|
936 | mSpaceCount += length;
|
---|
937 | }
|
---|
938 |
|
---|
939 | void
|
---|
940 | X3dViewCellsParseHandlers::resetDocument()
|
---|
941 | {
|
---|
942 | mAttrCount = 0;
|
---|
943 | mCharacterCount = 0;
|
---|
944 | mElementCount = 0;
|
---|
945 | mSpaceCount = 0;
|
---|
946 | }
|
---|
947 |
|
---|
948 |
|
---|
949 |
|
---|
950 | // ---------------------------------------------------------------------------
|
---|
951 | // StdInParseHandlers: Overrides of the SAX ErrorHandler interface
|
---|
952 | // ---------------------------------------------------------------------------
|
---|
953 | void
|
---|
954 | X3dViewCellsParseHandlers::error(const SAXParseException& e)
|
---|
955 | {
|
---|
956 | XERCES_STD_QUALIFIER cerr << "\nError at (file " << StrX(e.getSystemId())
|
---|
957 | << ", line " << e.getLineNumber()
|
---|
958 | << ", char " << e.getColumnNumber()
|
---|
959 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
960 | }
|
---|
961 |
|
---|
962 | void
|
---|
963 | X3dViewCellsParseHandlers::fatalError(const SAXParseException& e)
|
---|
964 | {
|
---|
965 | XERCES_STD_QUALIFIER cerr << "\nFatal Error at (file " << StrX(e.getSystemId())
|
---|
966 | << ", line " << e.getLineNumber()
|
---|
967 | << ", char " << e.getColumnNumber()
|
---|
968 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
969 | }
|
---|
970 |
|
---|
971 | void
|
---|
972 | X3dViewCellsParseHandlers::warning(const SAXParseException& e)
|
---|
973 | {
|
---|
974 | XERCES_STD_QUALIFIER cerr << "\nWarning at (file " << StrX(e.getSystemId())
|
---|
975 | << ", line " << e.getLineNumber()
|
---|
976 | << ", char " << e.getColumnNumber()
|
---|
977 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
978 | }
|
---|
979 |
|
---|
980 |
|
---|
981 | bool
|
---|
982 | X3dParser::ParseFile(const string filename, ViewCellsManager &viewCells)
|
---|
983 | {
|
---|
984 | // Initialize the XML4C system
|
---|
985 | try {
|
---|
986 | XMLPlatformUtils::Initialize();
|
---|
987 | }
|
---|
988 |
|
---|
989 | catch (const XMLException& toCatch)
|
---|
990 | {
|
---|
991 | XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
|
---|
992 | << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
993 | return false;
|
---|
994 | }
|
---|
995 |
|
---|
996 |
|
---|
997 | //
|
---|
998 | // Create a SAX parser object. Then, according to what we were told on
|
---|
999 | // the command line, set the options.
|
---|
1000 | //
|
---|
1001 | SAXParser* parser = new SAXParser;
|
---|
1002 | parser->setValidationScheme(valScheme);
|
---|
1003 | parser->setDoNamespaces(doNamespaces);
|
---|
1004 | parser->setDoSchema(doSchema);
|
---|
1005 | parser->setValidationSchemaFullChecking(schemaFullChecking);
|
---|
1006 |
|
---|
1007 |
|
---|
1008 | //
|
---|
1009 | // Create our SAX handler object and install it on the parser, as the
|
---|
1010 | // document and error handler. We are responsible for cleaning them
|
---|
1011 | // up, but since its just stack based here, there's nothing special
|
---|
1012 | // to do.
|
---|
1013 | //
|
---|
1014 | X3dViewCellsParseHandlers handler(&viewCells, mViewCellHeight);
|
---|
1015 | parser->setDocumentHandler(&handler);
|
---|
1016 | parser->setErrorHandler(&handler);
|
---|
1017 |
|
---|
1018 | unsigned long duration;
|
---|
1019 | int errorCount = 0;
|
---|
1020 | // create a faux scope so that 'src' destructor is called before
|
---|
1021 | // XMLPlatformUtils::Terminate
|
---|
1022 | {
|
---|
1023 | //
|
---|
1024 | // Kick off the parse and catch any exceptions. Create a standard
|
---|
1025 | // input input source and tell the parser to parse from that.
|
---|
1026 | //
|
---|
1027 | // StdInInputSource src;
|
---|
1028 | try
|
---|
1029 | {
|
---|
1030 | const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
1031 | //GzBinFileInputStream str(filename.c_str());
|
---|
1032 |
|
---|
1033 | parser->parse(filename.c_str());
|
---|
1034 | const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
1035 | duration = endMillis - startMillis;
|
---|
1036 | errorCount = parser->getErrorCount();
|
---|
1037 | }
|
---|
1038 | catch (const OutOfMemoryException&)
|
---|
1039 | {
|
---|
1040 | XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
|
---|
1041 | errorCount = 2;
|
---|
1042 | return false;
|
---|
1043 | }
|
---|
1044 | catch (const XMLException& e)
|
---|
1045 | {
|
---|
1046 | XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
|
---|
1047 | << StrX(e.getMessage())
|
---|
1048 | << "\n" << XERCES_STD_QUALIFIER endl;
|
---|
1049 | errorCount = 1;
|
---|
1050 | return false;
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 |
|
---|
1054 | // Print out the stats that we collected and time taken
|
---|
1055 | if (!errorCount) {
|
---|
1056 | XERCES_STD_QUALIFIER cout << filename << ": " << duration << " ms ("
|
---|
1057 | << handler.GetElementCount() << " elems, "
|
---|
1058 | << handler.GetAttrCount() << " attrs, "
|
---|
1059 | << handler.GetSpaceCount() << " spaces, "
|
---|
1060 | << handler.GetCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
|
---|
1061 | }
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | //
|
---|
1065 | // Delete the parser itself. Must be done prior to calling Terminate, below.
|
---|
1066 | //
|
---|
1067 | delete parser;
|
---|
1068 |
|
---|
1069 | XMLPlatformUtils::Terminate();
|
---|
1070 |
|
---|
1071 | if (errorCount > 0)
|
---|
1072 | return false;
|
---|
1073 | else
|
---|
1074 | return true;
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 |
|
---|
1078 |
|
---|
1079 |
|
---|
1080 | }
|
---|