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