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