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 "IntersectableWrapper.h"
|
---|
33 | #include <assert.h>
|
---|
34 |
|
---|
35 |
|
---|
36 | namespace GtpVisibilityPreprocessor {
|
---|
37 |
|
---|
38 | float X3dParser::DEFAULT_VIEWCELL_HEIGHT = 5.0f;
|
---|
39 |
|
---|
40 | // ---------------------------------------------------------------------------
|
---|
41 | // Local data
|
---|
42 | //
|
---|
43 | // doNamespaces
|
---|
44 | // Indicates whether namespace processing should be enabled or not.
|
---|
45 | // The default is no, but -n overrides that.
|
---|
46 | //
|
---|
47 | // doSchema
|
---|
48 | // Indicates whether schema processing should be enabled or not.
|
---|
49 | // The default is no, but -s overrides that.
|
---|
50 | //
|
---|
51 | // schemaFullChecking
|
---|
52 | // Indicates whether full schema constraint checking should be enabled or not.
|
---|
53 | // The default is no, but -s overrides that.
|
---|
54 | //
|
---|
55 | // valScheme
|
---|
56 | // Indicates what validation scheme to use. It defaults to 'auto', but
|
---|
57 | // can be set via the -v= command.
|
---|
58 | // ---------------------------------------------------------------------------
|
---|
59 | static bool doNamespaces = false;
|
---|
60 | static bool doSchema = false;
|
---|
61 | static bool schemaFullChecking = false;
|
---|
62 | static SAXParser::ValSchemes valScheme = SAXParser::Val_Auto;
|
---|
63 |
|
---|
64 | #define ROTATE_SCENE 0
|
---|
65 |
|
---|
66 | static int sUniqueMeshIdx = 0;
|
---|
67 |
|
---|
68 | // hack: rotate mesh by n degrees
|
---|
69 | static void RotateMesh(Mesh *mesh, const float angle)
|
---|
70 | {
|
---|
71 | VertexContainer::iterator it, it_end = mesh->mVertices.end();
|
---|
72 |
|
---|
73 | const float angleRad = angle * PI / 180.0f;
|
---|
74 | const Matrix4x4 rot = RotationYMatrix(angleRad);
|
---|
75 |
|
---|
76 | for (it = mesh->mVertices.begin(); it != it_end; ++ it)
|
---|
77 | {
|
---|
78 | (*it) = rot * (*it);
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | // ---------------------------------------------------------------------------
|
---|
83 | // StdInParseHandlers: Constructors and Destructor
|
---|
84 | // ---------------------------------------------------------------------------
|
---|
85 | X3dParseHandlers::X3dParseHandlers(SceneGraphNode *root,
|
---|
86 | const bool loadMeshes):
|
---|
87 | mElementCount(0)
|
---|
88 | , mAttrCount(0)
|
---|
89 | , mCharacterCount(0)
|
---|
90 | , mSpaceCount(0)
|
---|
91 | , mLoadMeshes(loadMeshes)
|
---|
92 | , mCurrentMesh(NULL)
|
---|
93 | {
|
---|
94 | mCurrentNode = root;
|
---|
95 |
|
---|
96 | // this matrix should never be removed from stack
|
---|
97 | //mTransformations.push(IdentityMatrix());
|
---|
98 | }
|
---|
99 |
|
---|
100 | X3dParseHandlers::~X3dParseHandlers()
|
---|
101 | {
|
---|
102 | assert(mTransformations.empty());
|
---|
103 | if (0 && !mTransformations.empty())
|
---|
104 | cout << "error: transformation stack size: " << (int)mTransformations.size() << endl;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | // ---------------------------------------------------------------------------
|
---|
109 | // StdInParseHandlers: Implementation of the SAX DocumentHandler interface
|
---|
110 | // ---------------------------------------------------------------------------
|
---|
111 |
|
---|
112 | void X3dParseHandlers::endElement(const XMLCh* const name)
|
---|
113 | {
|
---|
114 | StrX lname(name);
|
---|
115 | string element(lname.LocalForm());
|
---|
116 |
|
---|
117 | // only create new mesh instance if define mechanism was not used
|
---|
118 | if (element == "Shape")
|
---|
119 | EndShape();
|
---|
120 |
|
---|
121 | if (element == "Transform")
|
---|
122 | EndTransform();
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | void X3dParseHandlers::ApplyTransformations(TrafoStack trafos, Mesh *mesh) const
|
---|
127 | {
|
---|
128 | while (!trafos.empty())
|
---|
129 | {
|
---|
130 | const Matrix4x4 m = trafos.top();
|
---|
131 | trafos.pop();
|
---|
132 |
|
---|
133 | mesh->ApplyTransformation(m);
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | void X3dParseHandlers::ApplyTransformations(TrafoStack trafos,
|
---|
139 | TransformedMeshInstance *mi) const
|
---|
140 | {
|
---|
141 | while (!trafos.empty())
|
---|
142 | {
|
---|
143 | const Matrix4x4 m = trafos.top();
|
---|
144 | trafos.pop();
|
---|
145 |
|
---|
146 | mi->ApplyWorldTransform(m);
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | void X3dParseHandlers::StartTransform(AttributeList& attributes)
|
---|
152 | {
|
---|
153 | Matrix4x4 currentTransform = IdentityMatrix();
|
---|
154 |
|
---|
155 | const int len = attributes.getLength();
|
---|
156 |
|
---|
157 | Matrix4x4 *rotm = NULL;
|
---|
158 | Matrix4x4 *scalem = NULL;
|
---|
159 | Matrix4x4 *translm = NULL;
|
---|
160 |
|
---|
161 | for (int i = 0; i < len; ++ i)
|
---|
162 | {
|
---|
163 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
164 |
|
---|
165 | StrX attrValue(attributes.getValue(i));
|
---|
166 | const char *ptr = attrValue.LocalForm();
|
---|
167 |
|
---|
168 | if (attrName == "rotation")
|
---|
169 | {
|
---|
170 | Vector3 axis;
|
---|
171 | float angle;
|
---|
172 |
|
---|
173 | if (sscanf(ptr, "%f %f %f %f", &axis.x, &axis.y, &axis.z, &angle) == 4)
|
---|
174 | {
|
---|
175 | rotm = new Matrix4x4(RotationAxisMatrix(axis, angle));
|
---|
176 | }
|
---|
177 | }
|
---|
178 | else if (attrName == "translation")
|
---|
179 | {
|
---|
180 | Vector3 transl;
|
---|
181 |
|
---|
182 | if (sscanf(ptr, "%f %f %f", &transl.x, &transl.y, &transl.z) == 3)
|
---|
183 | {
|
---|
184 | translm = new Matrix4x4(TranslationMatrix(transl));
|
---|
185 | }
|
---|
186 | }
|
---|
187 | else if (attrName == "scale")
|
---|
188 | {
|
---|
189 | Vector3 scale;
|
---|
190 |
|
---|
191 | if (sscanf(ptr, "%f %f %f", &scale.x, &scale.y, &scale.z) == 3)
|
---|
192 | {
|
---|
193 | scalem = new Matrix4x4(ScaleMatrix(scale.x, scale.y, scale.z));
|
---|
194 | }
|
---|
195 | }
|
---|
196 | // todo: scale orientation
|
---|
197 | }
|
---|
198 |
|
---|
199 | if (scalem)
|
---|
200 | currentTransform *= (*scalem);
|
---|
201 | if (rotm)
|
---|
202 | currentTransform *= (*rotm);
|
---|
203 | if (translm)
|
---|
204 | currentTransform *= (*translm);
|
---|
205 |
|
---|
206 | DEL_PTR(scalem);
|
---|
207 | DEL_PTR(rotm);
|
---|
208 | DEL_PTR(translm);
|
---|
209 |
|
---|
210 | mTransformations.push(currentTransform);
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | void X3dParseHandlers::EndTransform()
|
---|
215 | {
|
---|
216 | mTransformations.pop();
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | void X3dParseHandlers::EndShape()
|
---|
221 | {
|
---|
222 | //////////////
|
---|
223 | //-- shape is only a definition =>
|
---|
224 | //-- don't create particular mesh instance
|
---|
225 |
|
---|
226 | if (!mCurrentMesh || mIsMeshDefinition)
|
---|
227 | {
|
---|
228 | return;
|
---|
229 | }
|
---|
230 |
|
---|
231 | if (!mLoadMeshes)
|
---|
232 | {
|
---|
233 | ///////////////////
|
---|
234 | //-- load data as single triangles instead of whole meshes
|
---|
235 |
|
---|
236 | cout << "m";
|
---|
237 | Mesh tempMesh(*mCurrentMesh);
|
---|
238 | ApplyTransformations(mTransformations, &tempMesh);
|
---|
239 |
|
---|
240 | FaceContainer::const_iterator fit, fit_begin = tempMesh.mFaces.begin(),
|
---|
241 | fit_end = tempMesh.mFaces.end();
|
---|
242 |
|
---|
243 | for (fit = fit_begin; fit != fit_end; ++ fit)
|
---|
244 | {
|
---|
245 | //cout << "f";
|
---|
246 | // triangulate the faces
|
---|
247 | Face *face = *fit;
|
---|
248 | vector<Triangle3> triangles;
|
---|
249 | Polygon3 poly(face, &tempMesh);
|
---|
250 | poly.Triangulate(triangles);
|
---|
251 |
|
---|
252 | vector<Triangle3>::const_iterator tit, tit_end = triangles.end();
|
---|
253 |
|
---|
254 | for (tit = triangles.begin(); tit != tit_end; ++ tit)
|
---|
255 | {
|
---|
256 | if ((*tit).CheckValidity())
|
---|
257 | {
|
---|
258 | TriangleIntersectable *ti = new TriangleIntersectable(*tit);
|
---|
259 | //cout << "t: " << (*tit) << endl;
|
---|
260 | mCurrentNode->mGeometry.push_back(ti);
|
---|
261 | }
|
---|
262 | else
|
---|
263 | {
|
---|
264 | cout << "error tri:\n" << (*tit) << endl;
|
---|
265 | }
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | // this mesh is not needed, unless it is used as a definition
|
---|
270 | if (!mUsingMeshDefinition)
|
---|
271 | {
|
---|
272 | MeshManager::GetSingleton()->DestroyEntry(mCurrentMesh->GetId());
|
---|
273 | }
|
---|
274 | }
|
---|
275 | else // default usage: create a mesh instance from the current mesh
|
---|
276 | {
|
---|
277 | MeshInstance *mi;
|
---|
278 |
|
---|
279 | if (!mUsingMeshDefinition)
|
---|
280 | {
|
---|
281 | // make an instance of this mesh
|
---|
282 | mi = new MeshInstance(mCurrentMesh);
|
---|
283 |
|
---|
284 | // this mesh is used only once => write transformations directly into it
|
---|
285 | ApplyTransformations(mTransformations, mCurrentMesh);
|
---|
286 | }
|
---|
287 | else
|
---|
288 | {
|
---|
289 | // make an instance of this mesh
|
---|
290 | TransformedMeshInstance *tmi = new TransformedMeshInstance(mCurrentMesh);
|
---|
291 |
|
---|
292 | // apply transformation on the instance of the mesh
|
---|
293 | ApplyTransformations(mTransformations, tmi);
|
---|
294 | mi = tmi;
|
---|
295 | }
|
---|
296 |
|
---|
297 | if (mCurrentMaterial)
|
---|
298 | {
|
---|
299 | // HACK: add the material to the mesh directly if no material yet
|
---|
300 | if (!mCurrentMesh->mMaterial)
|
---|
301 | {
|
---|
302 | mCurrentMesh->mMaterial = mCurrentMaterial;
|
---|
303 | }
|
---|
304 | else // add material to the instance
|
---|
305 | {
|
---|
306 | mi->SetMaterial(mCurrentMaterial);
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | // create local mesh kd tree
|
---|
311 | mCurrentMesh->Preprocess();
|
---|
312 |
|
---|
313 | if (mCurrentMesh->mFaces.empty())
|
---|
314 | {
|
---|
315 | cout << "warning: empy mesh!!" << endl;
|
---|
316 | delete mi;
|
---|
317 | }
|
---|
318 | else
|
---|
319 | {
|
---|
320 | // add to scene graph
|
---|
321 | mCurrentNode->mGeometry.push_back(mi);
|
---|
322 | }
|
---|
323 |
|
---|
324 | // reset current mesh
|
---|
325 | mCurrentMesh = NULL;
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 |
|
---|
330 | void X3dParseHandlers::StartIndexedFaceSet(AttributeList& attributes)
|
---|
331 | {
|
---|
332 | //-- indexedfaceset corresponds to Mesh in our implementation
|
---|
333 | const int len = attributes.getLength();
|
---|
334 |
|
---|
335 | VertexIndexContainer vertices;
|
---|
336 |
|
---|
337 | mIsMeshDefinition = false;
|
---|
338 | mUsingMeshDefinition = false;
|
---|
339 |
|
---|
340 | for (int i = 0; i < len; ++ i)
|
---|
341 | {
|
---|
342 | const string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
343 |
|
---|
344 | //-- we use an already defined mesh
|
---|
345 | if (attrName == "USE")
|
---|
346 | {
|
---|
347 | StrX attrValue(attributes.getValue(i));
|
---|
348 | const char *meshName = attrValue.LocalForm();
|
---|
349 |
|
---|
350 | mUsingMeshDefinition = true;
|
---|
351 |
|
---|
352 | // retrieve mesh from mesh container
|
---|
353 | const int meshIdx = mMeshDefinitions[meshName];
|
---|
354 |
|
---|
355 | mCurrentMesh =
|
---|
356 | MeshManager::GetSingleton()->FindEntry(meshIdx);
|
---|
357 |
|
---|
358 | //Debug << "retrieving mesh definition: " << mCurrentMeshName << endl;
|
---|
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 | }
|
---|