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 |
|
---|
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 |
|
---|
32 | // ---------------------------------------------------------------------------
|
---|
33 | // Local data
|
---|
34 | //
|
---|
35 | // doNamespaces
|
---|
36 | // Indicates whether namespace processing should be enabled or not.
|
---|
37 | // The default is no, but -n overrides that.
|
---|
38 | //
|
---|
39 | // doSchema
|
---|
40 | // Indicates whether schema processing should be enabled or not.
|
---|
41 | // The default is no, but -s overrides that.
|
---|
42 | //
|
---|
43 | // schemaFullChecking
|
---|
44 | // Indicates whether full schema constraint checking should be enabled or not.
|
---|
45 | // The default is no, but -s overrides that.
|
---|
46 | //
|
---|
47 | // valScheme
|
---|
48 | // Indicates what validation scheme to use. It defaults to 'auto', but
|
---|
49 | // can be set via the -v= command.
|
---|
50 | // ---------------------------------------------------------------------------
|
---|
51 | static bool doNamespaces = false;
|
---|
52 | static bool doSchema = false;
|
---|
53 | static bool schemaFullChecking = false;
|
---|
54 | static SAXParser::ValSchemes valScheme = SAXParser::Val_Auto;
|
---|
55 |
|
---|
56 | #define ROTATE_SCENE 0
|
---|
57 |
|
---|
58 |
|
---|
59 | static void RotateMesh(Mesh *mesh)
|
---|
60 | {
|
---|
61 | VertexContainer::iterator it, it_end = mesh->mVertices.end();
|
---|
62 |
|
---|
63 | const float angle = 30.0f * PI / 180.0f;
|
---|
64 | const Matrix4x4 rot = RotationYMatrix(angle);
|
---|
65 |
|
---|
66 | for (it = mesh->mVertices.begin(); it != it_end; ++ it)
|
---|
67 | {
|
---|
68 | (*it) = rot * (*it);
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | // ---------------------------------------------------------------------------
|
---|
73 | // StdInParseHandlers: Constructors and Destructor
|
---|
74 | // ---------------------------------------------------------------------------
|
---|
75 | X3dParseHandlers::X3dParseHandlers(SceneGraphNode *root, const bool loadPolygonsAsMeshes):
|
---|
76 | mElementCount(0)
|
---|
77 | , mAttrCount(0)
|
---|
78 | , mCharacterCount(0)
|
---|
79 | , mSpaceCount(0)
|
---|
80 | , mLoadPolygonsAsMeshes(loadPolygonsAsMeshes)
|
---|
81 | {
|
---|
82 | mCurrentNode = root;
|
---|
83 | // this matrix should never be removed from stack
|
---|
84 | //mTransformations.push(IdentityMatrix());
|
---|
85 | }
|
---|
86 |
|
---|
87 | X3dParseHandlers::~X3dParseHandlers()
|
---|
88 | {
|
---|
89 | if (!mTransformations.empty())
|
---|
90 | cout << "error: transformation stack size: " << (int)mTransformations.size() << endl;
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | // ---------------------------------------------------------------------------
|
---|
95 | // StdInParseHandlers: Implementation of the SAX DocumentHandler interface
|
---|
96 | // ---------------------------------------------------------------------------
|
---|
97 | void X3dParseHandlers::endElement(const XMLCh* const name)
|
---|
98 | {
|
---|
99 | StrX lname(name);
|
---|
100 | string element(lname.LocalForm());
|
---|
101 |
|
---|
102 | if (element == "Shape")
|
---|
103 | EndShape();
|
---|
104 | if (1)
|
---|
105 | if (element == "Transform")
|
---|
106 | EndTransform();
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 |
|
---|
111 |
|
---|
112 | void X3dParseHandlers::ApplyTransformation(Mesh *mesh,
|
---|
113 | const Matrix4x4 &m) const
|
---|
114 | {
|
---|
115 | VertexContainer::iterator it, it_end = mesh->mVertices.end();
|
---|
116 |
|
---|
117 | for (it = mesh->mVertices.begin(); it != it_end; ++ it)
|
---|
118 | {
|
---|
119 | (*it) = m * (*it);
|
---|
120 | }
|
---|
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 | ApplyTransformation(mesh, m);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | void X3dParseHandlers::StartTransform(AttributeList& attributes)
|
---|
136 | {
|
---|
137 | Matrix4x4 currentTransform = IdentityMatrix();
|
---|
138 |
|
---|
139 | const int len = attributes.getLength();
|
---|
140 |
|
---|
141 | for (int i = 0; i < len; ++ i)
|
---|
142 | {
|
---|
143 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
144 |
|
---|
145 | StrX attrValue(attributes.getValue(i));
|
---|
146 | const char *ptr = attrValue.LocalForm();
|
---|
147 |
|
---|
148 | if (attrName == "rotation")
|
---|
149 | {
|
---|
150 | Vector3 axis;
|
---|
151 | float angle;
|
---|
152 |
|
---|
153 | if (sscanf(ptr, "%f %f %f %f", &axis.x, &axis.y, &axis.z, &angle) == 4)
|
---|
154 | {
|
---|
155 | Matrix4x4 rot = RotationAxisMatrix(axis, angle);
|
---|
156 | currentTransform *= rot;
|
---|
157 | }
|
---|
158 | }
|
---|
159 | else if (attrName == "translation")
|
---|
160 | {
|
---|
161 | Vector3 transl;
|
---|
162 |
|
---|
163 | if (sscanf(ptr, "%f %f %f %f", &transl.x, &transl.y, &transl.z) == 3)
|
---|
164 | {
|
---|
165 | Matrix4x4 tm = TranslationMatrix(transl);
|
---|
166 |
|
---|
167 | currentTransform *= tm;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | mTransformations.push(currentTransform);
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | void X3dParseHandlers::EndTransform()
|
---|
177 | {
|
---|
178 | mTransformations.pop();
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | void
|
---|
183 | X3dParseHandlers::EndShape()
|
---|
184 | {
|
---|
185 | if (mLoadPolygonsAsMeshes)
|
---|
186 | {
|
---|
187 | vector<VertexIndexContainer>::const_iterator it, it_end = mCurrentVertexIndices.end();
|
---|
188 |
|
---|
189 | for (it = mCurrentVertexIndices.begin(); it != it_end; ++ it)
|
---|
190 | {
|
---|
191 | // only one face per mesh
|
---|
192 | Mesh *mesh = new Mesh();
|
---|
193 |
|
---|
194 | VertexIndexContainer vc;
|
---|
195 |
|
---|
196 | // add vertices
|
---|
197 | for (int i = 0; i < (int)(*it).size(); ++ i)
|
---|
198 | {
|
---|
199 | mesh->mVertices.push_back(mCurrentVertices[(*it)[i]]);
|
---|
200 | vc.push_back(i);
|
---|
201 | }
|
---|
202 |
|
---|
203 | mesh->mFaces.push_back(new Face(vc));
|
---|
204 |
|
---|
205 | // NOTE: should rather be written into trafo of mesh instance
|
---|
206 | ApplyTransformations(mTransformations, mesh);
|
---|
207 |
|
---|
208 | mesh->Preprocess();
|
---|
209 | // make an instance of this mesh
|
---|
210 | MeshInstance *mi = new MeshInstance(mesh);
|
---|
211 | mCurrentNode->mGeometry.push_back(mi);
|
---|
212 |
|
---|
213 | }
|
---|
214 |
|
---|
215 | delete mCurrentMesh;
|
---|
216 | mCurrentVertices.clear();
|
---|
217 | mCurrentVertexIndices.clear();
|
---|
218 | }
|
---|
219 | else
|
---|
220 | {
|
---|
221 | if (mCurrentMesh->mFaces.size())
|
---|
222 | {
|
---|
223 | // should rather be written into the transformation
|
---|
224 | // of a mesh instance
|
---|
225 | ApplyTransformations(mTransformations, mCurrentMesh);
|
---|
226 |
|
---|
227 | mCurrentMesh->Preprocess();
|
---|
228 | // make an instance of this mesh
|
---|
229 | MeshInstance *mi = new MeshInstance(mCurrentMesh);
|
---|
230 | mCurrentNode->mGeometry.push_back(mi);
|
---|
231 | // set the object id to a unique value
|
---|
232 | //mi->SetId(mCurrentObjectId ++);
|
---|
233 | }
|
---|
234 | else
|
---|
235 | {
|
---|
236 | cout<<"X";
|
---|
237 | delete mCurrentMesh;
|
---|
238 | }
|
---|
239 |
|
---|
240 | mCurrentMesh = NULL;
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 |
|
---|
245 | void
|
---|
246 | X3dParseHandlers::StartIndexedFaceSet(
|
---|
247 | AttributeList& attributes)
|
---|
248 | {
|
---|
249 | int len = attributes.getLength();
|
---|
250 | int i;
|
---|
251 | VertexIndexContainer vertices;
|
---|
252 |
|
---|
253 | for (i=0; i < len; i++) {
|
---|
254 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
255 | if (attrName == "coordIndex") {
|
---|
256 | StrX attrValue(attributes.getValue(i));
|
---|
257 | // handle coordIndex
|
---|
258 | vertices.clear();
|
---|
259 | const char *ptr = attrValue.LocalForm();
|
---|
260 | char *endptr;
|
---|
261 | while(1) {
|
---|
262 | int index = strtol(ptr, &endptr, 10);
|
---|
263 | if (ptr == endptr || index == -1) {
|
---|
264 | if (vertices.size() > 2) {
|
---|
265 | Face *face = new Face(vertices);
|
---|
266 |
|
---|
267 | if (!mLoadPolygonsAsMeshes)
|
---|
268 | {
|
---|
269 | mCurrentMesh->mFaces.push_back(face);
|
---|
270 | }
|
---|
271 | else
|
---|
272 | // every polygon is a mesh
|
---|
273 | {
|
---|
274 | mCurrentVertexIndices.push_back(vertices);
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | vertices.clear();
|
---|
279 | if (ptr == endptr)
|
---|
280 | break;
|
---|
281 | } else {
|
---|
282 | vertices.push_back(index);
|
---|
283 | }
|
---|
284 | ptr = endptr;
|
---|
285 | }
|
---|
286 | }
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | void
|
---|
291 | X3dParseHandlers::StartMaterial(
|
---|
292 | AttributeList& attributes)
|
---|
293 | {
|
---|
294 | int len = attributes.getLength();
|
---|
295 | int i;
|
---|
296 | if (!mCurrentMesh->mMaterial)
|
---|
297 | mCurrentMesh->mMaterial = new Material;
|
---|
298 | for (i=0; i < len; i++) {
|
---|
299 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
300 | StrX attrValue(attributes.getValue(i));
|
---|
301 | const char *ptr = attrValue.LocalForm();
|
---|
302 | if (attrName == "diffuseColor") {
|
---|
303 | float r, g, b;
|
---|
304 | if (sscanf(ptr, "%f %f %f", &r, &g, &b) == 3)
|
---|
305 | mCurrentMesh->mMaterial->mDiffuseColor = RgbColor(r, g, b);
|
---|
306 | }
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | void
|
---|
311 | X3dParseHandlers::StartCoordinate(
|
---|
312 | AttributeList& attributes)
|
---|
313 | {
|
---|
314 | int len = attributes.getLength();
|
---|
315 |
|
---|
316 | int i;
|
---|
317 | VertexContainer vertices;
|
---|
318 |
|
---|
319 | for (i=0; i < len; i++)
|
---|
320 | {
|
---|
321 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
322 |
|
---|
323 | if (attrName == "point")
|
---|
324 | {
|
---|
325 | StrX attrValue(attributes.getValue(i));
|
---|
326 |
|
---|
327 |
|
---|
328 | const char *ptr = attrValue.LocalForm();
|
---|
329 | char *endptr;
|
---|
330 |
|
---|
331 |
|
---|
332 | while (1)
|
---|
333 | {
|
---|
334 | float x = (float)strtod(ptr, &endptr);
|
---|
335 |
|
---|
336 | if (ptr == endptr)
|
---|
337 | break;
|
---|
338 |
|
---|
339 | ptr = endptr;
|
---|
340 |
|
---|
341 | float y = (float)strtod(ptr, &endptr);
|
---|
342 |
|
---|
343 | if (ptr == endptr)
|
---|
344 | break;
|
---|
345 |
|
---|
346 | ptr = endptr;
|
---|
347 |
|
---|
348 | float z = (float)strtod(ptr, &endptr);
|
---|
349 | if (ptr == endptr)
|
---|
350 | break;
|
---|
351 |
|
---|
352 | ptr = endptr;
|
---|
353 |
|
---|
354 | if (*ptr == ',')
|
---|
355 | ptr ++;
|
---|
356 |
|
---|
357 | Vector3 v(x, y, z);
|
---|
358 | vertices.push_back(v);
|
---|
359 | }
|
---|
360 | if (mLoadPolygonsAsMeshes)
|
---|
361 | {
|
---|
362 | mCurrentVertices = vertices;
|
---|
363 | }
|
---|
364 | else
|
---|
365 | {
|
---|
366 | mCurrentMesh->mVertices = vertices;
|
---|
367 | }
|
---|
368 | }
|
---|
369 | }
|
---|
370 | }
|
---|
371 |
|
---|
372 |
|
---|
373 | void
|
---|
374 | X3dParseHandlers::startElement(const XMLCh* const name,
|
---|
375 | AttributeList& attributes)
|
---|
376 | {
|
---|
377 | StrX lname(name);
|
---|
378 | string element(lname.LocalForm());
|
---|
379 |
|
---|
380 | if (element == "IndexedFaceSet") {
|
---|
381 | // create a new mesh node in the scene graph
|
---|
382 | StartIndexedFaceSet(attributes);
|
---|
383 | }
|
---|
384 |
|
---|
385 | if (element == "Shape") {
|
---|
386 | cout << "+";
|
---|
387 | mCurrentMesh = new Mesh;
|
---|
388 | }
|
---|
389 |
|
---|
390 | if (element == "Coordinate") {
|
---|
391 | if (mCurrentMesh)
|
---|
392 | StartCoordinate(attributes);
|
---|
393 | }
|
---|
394 |
|
---|
395 | if (element == "Material") {
|
---|
396 | StartMaterial(attributes);
|
---|
397 | }
|
---|
398 | if (1)
|
---|
399 | if (element == "Transform") {
|
---|
400 | StartTransform(attributes);
|
---|
401 | }
|
---|
402 |
|
---|
403 | mElementCount++;
|
---|
404 | mAttrCount += attributes.getLength();
|
---|
405 | }
|
---|
406 |
|
---|
407 | void
|
---|
408 | X3dParseHandlers::characters(const XMLCh* const chars,
|
---|
409 | const unsigned int length)
|
---|
410 | {
|
---|
411 | mCharacterCount += length;
|
---|
412 | }
|
---|
413 |
|
---|
414 | void
|
---|
415 | X3dParseHandlers::ignorableWhitespace(const XMLCh* const chars,
|
---|
416 | const unsigned int length)
|
---|
417 | {
|
---|
418 | mSpaceCount += length;
|
---|
419 | }
|
---|
420 |
|
---|
421 | void
|
---|
422 | X3dParseHandlers::resetDocument()
|
---|
423 | {
|
---|
424 | mAttrCount = 0;
|
---|
425 | mCharacterCount = 0;
|
---|
426 | mElementCount = 0;
|
---|
427 | mSpaceCount = 0;
|
---|
428 | }
|
---|
429 |
|
---|
430 |
|
---|
431 |
|
---|
432 | // ---------------------------------------------------------------------------
|
---|
433 | // StdInParseHandlers: Overrides of the SAX ErrorHandler interface
|
---|
434 | // ---------------------------------------------------------------------------
|
---|
435 | void
|
---|
436 | X3dParseHandlers::error(const SAXParseException& e)
|
---|
437 | {
|
---|
438 | XERCES_STD_QUALIFIER cerr << "\nError at (file " << StrX(e.getSystemId())
|
---|
439 | << ", line " << e.getLineNumber()
|
---|
440 | << ", char " << e.getColumnNumber()
|
---|
441 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
442 | }
|
---|
443 |
|
---|
444 | void
|
---|
445 | X3dParseHandlers::fatalError(const SAXParseException& e)
|
---|
446 | {
|
---|
447 | XERCES_STD_QUALIFIER cerr << "\nFatal Error at (file " << StrX(e.getSystemId())
|
---|
448 | << ", line " << e.getLineNumber()
|
---|
449 | << ", char " << e.getColumnNumber()
|
---|
450 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
451 | }
|
---|
452 |
|
---|
453 | void
|
---|
454 | X3dParseHandlers::warning(const SAXParseException& e)
|
---|
455 | {
|
---|
456 | XERCES_STD_QUALIFIER cerr << "\nWarning at (file " << StrX(e.getSystemId())
|
---|
457 | << ", line " << e.getLineNumber()
|
---|
458 | << ", char " << e.getColumnNumber()
|
---|
459 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
460 | }
|
---|
461 |
|
---|
462 |
|
---|
463 | bool
|
---|
464 | X3dParser::ParseFile(const string filename,
|
---|
465 | SceneGraphNode **root,
|
---|
466 | const bool loadPolygonsAsMeshes)
|
---|
467 | {
|
---|
468 | // Initialize the XML4C system
|
---|
469 | try {
|
---|
470 | XMLPlatformUtils::Initialize();
|
---|
471 | }
|
---|
472 |
|
---|
473 | catch (const XMLException& toCatch)
|
---|
474 | {
|
---|
475 | XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
|
---|
476 | << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
477 | return false;
|
---|
478 | }
|
---|
479 |
|
---|
480 |
|
---|
481 | //
|
---|
482 | // Create a SAX parser object. Then, according to what we were told on
|
---|
483 | // the command line, set the options.
|
---|
484 | //
|
---|
485 | SAXParser* parser = new SAXParser;
|
---|
486 | parser->setValidationScheme(valScheme);
|
---|
487 | parser->setDoNamespaces(doNamespaces);
|
---|
488 | parser->setDoSchema(doSchema);
|
---|
489 | parser->setValidationSchemaFullChecking(schemaFullChecking);
|
---|
490 |
|
---|
491 |
|
---|
492 | //
|
---|
493 | // Create our SAX handler object and install it on the parser, as the
|
---|
494 | // document and error handler. We are responsible for cleaning them
|
---|
495 | // up, but since its just stack based here, there's nothing special
|
---|
496 | // to do.
|
---|
497 | //
|
---|
498 | *root = new SceneGraphNode;
|
---|
499 | X3dParseHandlers handler(*root, loadPolygonsAsMeshes);
|
---|
500 | parser->setDocumentHandler(&handler);
|
---|
501 | parser->setErrorHandler(&handler);
|
---|
502 |
|
---|
503 | unsigned long duration;
|
---|
504 | int errorCount = 0;
|
---|
505 | // create a faux scope so that 'src' destructor is called before
|
---|
506 | // XMLPlatformUtils::Terminate
|
---|
507 | {
|
---|
508 | //
|
---|
509 | // Kick off the parse and catch any exceptions. Create a standard
|
---|
510 | // input input source and tell the parser to parse from that.
|
---|
511 | //
|
---|
512 | // StdInInputSource src;
|
---|
513 | try
|
---|
514 | {
|
---|
515 | const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
516 | parser->parse(filename.c_str());
|
---|
517 | const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
518 | duration = endMillis - startMillis;
|
---|
519 | errorCount = parser->getErrorCount();
|
---|
520 | }
|
---|
521 | catch (const OutOfMemoryException&)
|
---|
522 | {
|
---|
523 | XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
|
---|
524 | errorCount = 2;
|
---|
525 | return false;
|
---|
526 | }
|
---|
527 | catch (const XMLException& e)
|
---|
528 | {
|
---|
529 | XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
|
---|
530 | << StrX(e.getMessage())
|
---|
531 | << "\n" << XERCES_STD_QUALIFIER endl;
|
---|
532 | errorCount = 1;
|
---|
533 | return false;
|
---|
534 | }
|
---|
535 |
|
---|
536 |
|
---|
537 | // Print out the stats that we collected and time taken
|
---|
538 | if (!errorCount) {
|
---|
539 | XERCES_STD_QUALIFIER cout << filename << ": " << duration << " ms ("
|
---|
540 | << handler.GetElementCount() << " elems, "
|
---|
541 | << handler.GetAttrCount() << " attrs, "
|
---|
542 | << handler.GetSpaceCount() << " spaces, "
|
---|
543 | << handler.GetCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
|
---|
544 | }
|
---|
545 | }
|
---|
546 |
|
---|
547 | //
|
---|
548 | // Delete the parser itself. Must be done prior to calling Terminate, below.
|
---|
549 | //
|
---|
550 | delete parser;
|
---|
551 |
|
---|
552 | XMLPlatformUtils::Terminate();
|
---|
553 |
|
---|
554 | if (errorCount > 0)
|
---|
555 | return false;
|
---|
556 | else
|
---|
557 | return true;
|
---|
558 | }
|
---|
559 |
|
---|
560 |
|
---|
561 |
|
---|
562 | /************************************************************************/
|
---|
563 | /* class X3dViewCellsParseHandlers implementation */
|
---|
564 | /************************************************************************/
|
---|
565 |
|
---|
566 |
|
---|
567 | // ---------------------------------------------------------------------------
|
---|
568 | // StdInParseHandlers: Constructors and Destructor
|
---|
569 | // ---------------------------------------------------------------------------
|
---|
570 | X3dViewCellsParseHandlers::X3dViewCellsParseHandlers(ViewCellsManager *viewCellsManager,
|
---|
571 | const float viewCellHeight):
|
---|
572 | mElementCount(0),
|
---|
573 | mAttrCount(0),
|
---|
574 | mCharacterCount(0),
|
---|
575 | mSpaceCount(0),
|
---|
576 | mViewCellsManager(viewCellsManager),
|
---|
577 | mViewCellHeight(viewCellHeight)
|
---|
578 | {
|
---|
579 | }
|
---|
580 |
|
---|
581 | X3dViewCellsParseHandlers::~X3dViewCellsParseHandlers()
|
---|
582 | {
|
---|
583 | }
|
---|
584 |
|
---|
585 |
|
---|
586 | // ---------------------------------------------------------------------------
|
---|
587 | // StdInParseHandlers: Implementation of the SAX DocumentHandler interface
|
---|
588 | // ---------------------------------------------------------------------------
|
---|
589 | void X3dViewCellsParseHandlers::endElement(const XMLCh* const name)
|
---|
590 | {
|
---|
591 | StrX lname(name);
|
---|
592 | string element(lname.LocalForm());
|
---|
593 |
|
---|
594 | if (element == "Shape")
|
---|
595 | EndShape();
|
---|
596 | }
|
---|
597 |
|
---|
598 | void
|
---|
599 | X3dViewCellsParseHandlers::EndShape()
|
---|
600 | {
|
---|
601 | }
|
---|
602 |
|
---|
603 | void
|
---|
604 | X3dViewCellsParseHandlers::StartIndexedFaceSet(
|
---|
605 | AttributeList& attributes)
|
---|
606 | {
|
---|
607 | int len = attributes.getLength();
|
---|
608 | int i;
|
---|
609 | // clear previous vertex indices
|
---|
610 | mCurrentVertexIndices.clear();
|
---|
611 | for (i=0; i < len; i++)
|
---|
612 | {
|
---|
613 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
614 |
|
---|
615 | if (attrName == "coordIndex")
|
---|
616 | {
|
---|
617 | StrX attrValue(attributes.getValue(i));
|
---|
618 |
|
---|
619 | // handle coordIndex
|
---|
620 | const char *ptr = attrValue.LocalForm();
|
---|
621 | char *endptr;
|
---|
622 |
|
---|
623 | while (1)
|
---|
624 | {
|
---|
625 | int index = strtol(ptr, &endptr, 10);
|
---|
626 |
|
---|
627 | if (ptr == endptr)
|
---|
628 | break;
|
---|
629 |
|
---|
630 | if (index != -1)
|
---|
631 | {
|
---|
632 | mCurrentVertexIndices.push_back(index);
|
---|
633 | }
|
---|
634 |
|
---|
635 | ptr = endptr;
|
---|
636 | }
|
---|
637 | }
|
---|
638 | }
|
---|
639 | }
|
---|
640 |
|
---|
641 |
|
---|
642 | void
|
---|
643 | X3dViewCellsParseHandlers::StartCoordinate(AttributeList& attributes)
|
---|
644 | {
|
---|
645 | int len = attributes.getLength();
|
---|
646 |
|
---|
647 | VertexContainer vertices;
|
---|
648 | int i;
|
---|
649 | for (i=0; i < len; i++)
|
---|
650 | {
|
---|
651 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
652 |
|
---|
653 | if (attrName == "point")
|
---|
654 | {
|
---|
655 | StrX attrValue(attributes.getValue(i));
|
---|
656 |
|
---|
657 | const char *ptr = attrValue.LocalForm();
|
---|
658 |
|
---|
659 | char *endptr;
|
---|
660 |
|
---|
661 | while (1)
|
---|
662 | {
|
---|
663 | float x = (float)strtod(ptr, &endptr);
|
---|
664 |
|
---|
665 | if (ptr == endptr)
|
---|
666 | break;
|
---|
667 | ptr = endptr;
|
---|
668 |
|
---|
669 | float y = (float)(float)strtod(ptr, &endptr);
|
---|
670 |
|
---|
671 |
|
---|
672 | if (ptr == endptr)
|
---|
673 | break;
|
---|
674 | ptr = endptr;
|
---|
675 |
|
---|
676 | float z = (float)(float)strtod(ptr, &endptr);
|
---|
677 |
|
---|
678 | if (ptr == endptr)
|
---|
679 | break;
|
---|
680 |
|
---|
681 | ptr = endptr;
|
---|
682 | if (*ptr == ',')
|
---|
683 | ptr++;
|
---|
684 |
|
---|
685 | Vector3 v(x, y, z);
|
---|
686 | vertices.push_back(v);
|
---|
687 | }
|
---|
688 | }
|
---|
689 | }
|
---|
690 |
|
---|
691 | for (i = 0; i < mCurrentVertexIndices.size(); i += 3)
|
---|
692 | {
|
---|
693 | Triangle3 baseTri(vertices[mCurrentVertexIndices[i + 0]],
|
---|
694 | vertices[mCurrentVertexIndices[i + 1]],
|
---|
695 | vertices[mCurrentVertexIndices[i + 2]]);
|
---|
696 |
|
---|
697 | // create view cell from base triangle
|
---|
698 | mViewCellsManager->AddViewCell(
|
---|
699 | mViewCellsManager->ExtrudeViewCell(baseTri,
|
---|
700 | mViewCellHeight));
|
---|
701 | }
|
---|
702 | }
|
---|
703 |
|
---|
704 |
|
---|
705 | void
|
---|
706 | X3dViewCellsParseHandlers::startElement(const XMLCh* const name,
|
---|
707 | AttributeList& attributes)
|
---|
708 | {
|
---|
709 | StrX lname(name);
|
---|
710 | string element(lname.LocalForm());
|
---|
711 |
|
---|
712 | if (element == "IndexedFaceSet") {
|
---|
713 | // create the viewcells from individual triangles
|
---|
714 | StartIndexedFaceSet(attributes);
|
---|
715 | }
|
---|
716 |
|
---|
717 | if (element == "Coordinate") {
|
---|
718 | // add coordinates to the triangles
|
---|
719 | StartCoordinate(attributes);
|
---|
720 | }
|
---|
721 | // do nothing
|
---|
722 | //if (element == "Shape") {}
|
---|
723 | // ignore material
|
---|
724 | //if (element == "Material") {}
|
---|
725 |
|
---|
726 | ++ mElementCount;
|
---|
727 | mAttrCount += attributes.getLength();
|
---|
728 | }
|
---|
729 |
|
---|
730 | void
|
---|
731 | X3dViewCellsParseHandlers::characters(const XMLCh* const chars,
|
---|
732 | const unsigned int length)
|
---|
733 | {
|
---|
734 | mCharacterCount += length;
|
---|
735 | }
|
---|
736 |
|
---|
737 | void
|
---|
738 | X3dViewCellsParseHandlers::ignorableWhitespace(const XMLCh* const chars,
|
---|
739 | const unsigned int length)
|
---|
740 | {
|
---|
741 | mSpaceCount += length;
|
---|
742 | }
|
---|
743 |
|
---|
744 | void
|
---|
745 | X3dViewCellsParseHandlers::resetDocument()
|
---|
746 | {
|
---|
747 | mAttrCount = 0;
|
---|
748 | mCharacterCount = 0;
|
---|
749 | mElementCount = 0;
|
---|
750 | mSpaceCount = 0;
|
---|
751 | }
|
---|
752 |
|
---|
753 |
|
---|
754 |
|
---|
755 | // ---------------------------------------------------------------------------
|
---|
756 | // StdInParseHandlers: Overrides of the SAX ErrorHandler interface
|
---|
757 | // ---------------------------------------------------------------------------
|
---|
758 | void
|
---|
759 | X3dViewCellsParseHandlers::error(const SAXParseException& e)
|
---|
760 | {
|
---|
761 | XERCES_STD_QUALIFIER cerr << "\nError at (file " << StrX(e.getSystemId())
|
---|
762 | << ", line " << e.getLineNumber()
|
---|
763 | << ", char " << e.getColumnNumber()
|
---|
764 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
765 | }
|
---|
766 |
|
---|
767 | void
|
---|
768 | X3dViewCellsParseHandlers::fatalError(const SAXParseException& e)
|
---|
769 | {
|
---|
770 | XERCES_STD_QUALIFIER cerr << "\nFatal Error at (file " << StrX(e.getSystemId())
|
---|
771 | << ", line " << e.getLineNumber()
|
---|
772 | << ", char " << e.getColumnNumber()
|
---|
773 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
774 | }
|
---|
775 |
|
---|
776 | void
|
---|
777 | X3dViewCellsParseHandlers::warning(const SAXParseException& e)
|
---|
778 | {
|
---|
779 | XERCES_STD_QUALIFIER cerr << "\nWarning at (file " << StrX(e.getSystemId())
|
---|
780 | << ", line " << e.getLineNumber()
|
---|
781 | << ", char " << e.getColumnNumber()
|
---|
782 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
783 | }
|
---|
784 |
|
---|
785 |
|
---|
786 | bool
|
---|
787 | X3dParser::ParseFile(const string filename, ViewCellsManager &viewCells)
|
---|
788 | {
|
---|
789 | // Initialize the XML4C system
|
---|
790 | try {
|
---|
791 | XMLPlatformUtils::Initialize();
|
---|
792 | }
|
---|
793 |
|
---|
794 | catch (const XMLException& toCatch)
|
---|
795 | {
|
---|
796 | XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
|
---|
797 | << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
798 | return false;
|
---|
799 | }
|
---|
800 |
|
---|
801 |
|
---|
802 | //
|
---|
803 | // Create a SAX parser object. Then, according to what we were told on
|
---|
804 | // the command line, set the options.
|
---|
805 | //
|
---|
806 | SAXParser* parser = new SAXParser;
|
---|
807 | parser->setValidationScheme(valScheme);
|
---|
808 | parser->setDoNamespaces(doNamespaces);
|
---|
809 | parser->setDoSchema(doSchema);
|
---|
810 | parser->setValidationSchemaFullChecking(schemaFullChecking);
|
---|
811 |
|
---|
812 |
|
---|
813 | //
|
---|
814 | // Create our SAX handler object and install it on the parser, as the
|
---|
815 | // document and error handler. We are responsible for cleaning them
|
---|
816 | // up, but since its just stack based here, there's nothing special
|
---|
817 | // to do.
|
---|
818 | //
|
---|
819 | X3dViewCellsParseHandlers handler(&viewCells, mViewCellHeight);
|
---|
820 | parser->setDocumentHandler(&handler);
|
---|
821 | parser->setErrorHandler(&handler);
|
---|
822 |
|
---|
823 | unsigned long duration;
|
---|
824 | int errorCount = 0;
|
---|
825 | // create a faux scope so that 'src' destructor is called before
|
---|
826 | // XMLPlatformUtils::Terminate
|
---|
827 | {
|
---|
828 | //
|
---|
829 | // Kick off the parse and catch any exceptions. Create a standard
|
---|
830 | // input input source and tell the parser to parse from that.
|
---|
831 | //
|
---|
832 | // StdInInputSource src;
|
---|
833 | try
|
---|
834 | {
|
---|
835 | const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
836 | parser->parse(filename.c_str());
|
---|
837 | const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
838 | duration = endMillis - startMillis;
|
---|
839 | errorCount = parser->getErrorCount();
|
---|
840 | }
|
---|
841 | catch (const OutOfMemoryException&)
|
---|
842 | {
|
---|
843 | XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
|
---|
844 | errorCount = 2;
|
---|
845 | return false;
|
---|
846 | }
|
---|
847 | catch (const XMLException& e)
|
---|
848 | {
|
---|
849 | XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
|
---|
850 | << StrX(e.getMessage())
|
---|
851 | << "\n" << XERCES_STD_QUALIFIER endl;
|
---|
852 | errorCount = 1;
|
---|
853 | return false;
|
---|
854 | }
|
---|
855 |
|
---|
856 |
|
---|
857 | // Print out the stats that we collected and time taken
|
---|
858 | if (!errorCount) {
|
---|
859 | XERCES_STD_QUALIFIER cout << filename << ": " << duration << " ms ("
|
---|
860 | << handler.GetElementCount() << " elems, "
|
---|
861 | << handler.GetAttrCount() << " attrs, "
|
---|
862 | << handler.GetSpaceCount() << " spaces, "
|
---|
863 | << handler.GetCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
|
---|
864 | }
|
---|
865 | }
|
---|
866 |
|
---|
867 | //
|
---|
868 | // Delete the parser itself. Must be done prior to calling Terminate, below.
|
---|
869 | //
|
---|
870 | delete parser;
|
---|
871 |
|
---|
872 | XMLPlatformUtils::Terminate();
|
---|
873 |
|
---|
874 | if (errorCount > 0)
|
---|
875 | return false;
|
---|
876 | else
|
---|
877 | return true;
|
---|
878 | }
|
---|