[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 |
|
---|
[726] | 59 | static void RotateMesh(Mesh *mesh)
|
---|
| 60 | {
|
---|
| 61 | VertexContainer::iterator it, it_end = mesh->mVertices.end();
|
---|
[170] | 62 |
|
---|
[726] | 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 |
|
---|
[170] | 72 | // ---------------------------------------------------------------------------
|
---|
| 73 | // StdInParseHandlers: Constructors and Destructor
|
---|
| 74 | // ---------------------------------------------------------------------------
|
---|
[657] | 75 | X3dParseHandlers::X3dParseHandlers(SceneGraphNode *root, const bool loadPolygonsAsMeshes):
|
---|
[176] | 76 | mElementCount(0)
|
---|
| 77 | , mAttrCount(0)
|
---|
| 78 | , mCharacterCount(0)
|
---|
| 79 | , mSpaceCount(0)
|
---|
[657] | 80 | , mLoadPolygonsAsMeshes(loadPolygonsAsMeshes)
|
---|
[162] | 81 | {
|
---|
[176] | 82 | mCurrentNode = root;
|
---|
[712] | 83 | // this matrix should never be removed from stack
|
---|
| 84 | //mTransformations.push(IdentityMatrix());
|
---|
[162] | 85 | }
|
---|
| 86 |
|
---|
[170] | 87 | X3dParseHandlers::~X3dParseHandlers()
|
---|
| 88 | {
|
---|
[712] | 89 | if (!mTransformations.empty())
|
---|
| 90 | cout << "error: transformation stack size: " << (int)mTransformations.size() << endl;
|
---|
[170] | 91 | }
|
---|
[162] | 92 |
|
---|
[170] | 93 |
|
---|
| 94 | // ---------------------------------------------------------------------------
|
---|
| 95 | // StdInParseHandlers: Implementation of the SAX DocumentHandler interface
|
---|
| 96 | // ---------------------------------------------------------------------------
|
---|
| 97 | void X3dParseHandlers::endElement(const XMLCh* const name)
|
---|
[162] | 98 | {
|
---|
[170] | 99 | StrX lname(name);
|
---|
[176] | 100 | string element(lname.LocalForm());
|
---|
[694] | 101 |
|
---|
[170] | 102 | if (element == "Shape")
|
---|
| 103 | EndShape();
|
---|
[712] | 104 | if (1)
|
---|
| 105 | if (element == "Transform")
|
---|
| 106 | EndTransform();
|
---|
[170] | 107 | }
|
---|
[162] | 108 |
|
---|
[694] | 109 |
|
---|
[693] | 110 |
|
---|
| 111 |
|
---|
[712] | 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 |
|
---|
[170] | 182 | void
|
---|
| 183 | X3dParseHandlers::EndShape()
|
---|
| 184 | {
|
---|
[658] | 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 |
|
---|
[660] | 194 | VertexIndexContainer vc;
|
---|
| 195 |
|
---|
[658] | 196 | // add vertices
|
---|
| 197 | for (int i = 0; i < (int)(*it).size(); ++ i)
|
---|
| 198 | {
|
---|
| 199 | mesh->mVertices.push_back(mCurrentVertices[(*it)[i]]);
|
---|
[660] | 200 | vc.push_back(i);
|
---|
[658] | 201 | }
|
---|
| 202 |
|
---|
[660] | 203 | mesh->mFaces.push_back(new Face(vc));
|
---|
[712] | 204 |
|
---|
| 205 | // NOTE: should rather be written into trafo of mesh instance
|
---|
| 206 | ApplyTransformations(mTransformations, mesh);
|
---|
| 207 |
|
---|
[658] | 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
|
---|
[693] | 220 | {
|
---|
[658] | 221 | if (mCurrentMesh->mFaces.size())
|
---|
| 222 | {
|
---|
[712] | 223 | // should rather be written into the transformation
|
---|
| 224 | // of a mesh instance
|
---|
| 225 | ApplyTransformations(mTransformations, mCurrentMesh);
|
---|
| 226 |
|
---|
[658] | 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 | }
|
---|
[694] | 239 |
|
---|
[658] | 240 | mCurrentMesh = NULL;
|
---|
| 241 | }
|
---|
[170] | 242 | }
|
---|
[712] | 243 |
|
---|
| 244 |
|
---|
[170] | 245 | void
|
---|
| 246 | X3dParseHandlers::StartIndexedFaceSet(
|
---|
[439] | 247 | AttributeList& attributes)
|
---|
[170] | 248 | {
|
---|
| 249 | int len = attributes.getLength();
|
---|
| 250 | int i;
|
---|
| 251 | VertexIndexContainer vertices;
|
---|
[162] | 252 |
|
---|
[170] | 253 | for (i=0; i < len; i++) {
|
---|
[176] | 254 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
[170] | 255 | if (attrName == "coordIndex") {
|
---|
| 256 | StrX attrValue(attributes.getValue(i));
|
---|
| 257 | // handle coordIndex
|
---|
| 258 | vertices.clear();
|
---|
[176] | 259 | const char *ptr = attrValue.LocalForm();
|
---|
[170] | 260 | char *endptr;
|
---|
| 261 | while(1) {
|
---|
| 262 | int index = strtol(ptr, &endptr, 10);
|
---|
| 263 | if (ptr == endptr || index == -1) {
|
---|
| 264 | if (vertices.size() > 2) {
|
---|
[657] | 265 | Face *face = new Face(vertices);
|
---|
| 266 |
|
---|
[658] | 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 | }
|
---|
[657] | 277 |
|
---|
[170] | 278 | vertices.clear();
|
---|
| 279 | if (ptr == endptr)
|
---|
| 280 | break;
|
---|
| 281 | } else {
|
---|
| 282 | vertices.push_back(index);
|
---|
| 283 | }
|
---|
| 284 | ptr = endptr;
|
---|
[508] | 285 | }
|
---|
[170] | 286 | }
|
---|
[162] | 287 | }
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[170] | 290 | void
|
---|
| 291 | X3dParseHandlers::StartMaterial(
|
---|
| 292 | AttributeList& attributes)
|
---|
[162] | 293 | {
|
---|
[170] | 294 | int len = attributes.getLength();
|
---|
| 295 | int i;
|
---|
[176] | 296 | if (!mCurrentMesh->mMaterial)
|
---|
| 297 | mCurrentMesh->mMaterial = new Material;
|
---|
[170] | 298 | for (i=0; i < len; i++) {
|
---|
[176] | 299 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
[170] | 300 | StrX attrValue(attributes.getValue(i));
|
---|
[176] | 301 | const char *ptr = attrValue.LocalForm();
|
---|
[170] | 302 | if (attrName == "diffuseColor") {
|
---|
| 303 | float r, g, b;
|
---|
| 304 | if (sscanf(ptr, "%f %f %f", &r, &g, &b) == 3)
|
---|
[176] | 305 | mCurrentMesh->mMaterial->mDiffuseColor = RgbColor(r, g, b);
|
---|
[170] | 306 | }
|
---|
| 307 | }
|
---|
[162] | 308 | }
|
---|
| 309 |
|
---|
[170] | 310 | void
|
---|
| 311 | X3dParseHandlers::StartCoordinate(
|
---|
| 312 | AttributeList& attributes)
|
---|
[162] | 313 | {
|
---|
[657] | 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 |
|
---|
[694] | 332 | while (1)
|
---|
[657] | 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 | }
|
---|
[658] | 360 | if (mLoadPolygonsAsMeshes)
|
---|
| 361 | {
|
---|
| 362 | mCurrentVertices = vertices;
|
---|
| 363 | }
|
---|
| 364 | else
|
---|
| 365 | {
|
---|
| 366 | mCurrentMesh->mVertices = vertices;
|
---|
| 367 | }
|
---|
[657] | 368 | }
|
---|
| 369 | }
|
---|
[162] | 370 | }
|
---|
| 371 |
|
---|
[170] | 372 |
|
---|
| 373 | void
|
---|
| 374 | X3dParseHandlers::startElement(const XMLCh* const name,
|
---|
[439] | 375 | AttributeList& attributes)
|
---|
[162] | 376 | {
|
---|
[170] | 377 | StrX lname(name);
|
---|
[176] | 378 | string element(lname.LocalForm());
|
---|
[170] | 379 |
|
---|
| 380 | if (element == "IndexedFaceSet") {
|
---|
| 381 | // create a new mesh node in the scene graph
|
---|
| 382 | StartIndexedFaceSet(attributes);
|
---|
| 383 | }
|
---|
[694] | 384 |
|
---|
[170] | 385 | if (element == "Shape") {
|
---|
[694] | 386 | cout << "+";
|
---|
[658] | 387 | mCurrentMesh = new Mesh;
|
---|
[170] | 388 | }
|
---|
| 389 |
|
---|
| 390 | if (element == "Coordinate") {
|
---|
[176] | 391 | if (mCurrentMesh)
|
---|
[170] | 392 | StartCoordinate(attributes);
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | if (element == "Material") {
|
---|
| 396 | StartMaterial(attributes);
|
---|
| 397 | }
|
---|
[712] | 398 | if (1)
|
---|
| 399 | if (element == "Transform") {
|
---|
| 400 | StartTransform(attributes);
|
---|
| 401 | }
|
---|
[170] | 402 |
|
---|
[176] | 403 | mElementCount++;
|
---|
| 404 | mAttrCount += attributes.getLength();
|
---|
[162] | 405 | }
|
---|
| 406 |
|
---|
[170] | 407 | void
|
---|
| 408 | X3dParseHandlers::characters(const XMLCh* const chars,
|
---|
| 409 | const unsigned int length)
|
---|
[162] | 410 | {
|
---|
[176] | 411 | mCharacterCount += length;
|
---|
[162] | 412 | }
|
---|
| 413 |
|
---|
[170] | 414 | void
|
---|
| 415 | X3dParseHandlers::ignorableWhitespace(const XMLCh* const chars,
|
---|
| 416 | const unsigned int length)
|
---|
[162] | 417 | {
|
---|
[176] | 418 | mSpaceCount += length;
|
---|
[162] | 419 | }
|
---|
| 420 |
|
---|
[170] | 421 | void
|
---|
| 422 | X3dParseHandlers::resetDocument()
|
---|
[162] | 423 | {
|
---|
[176] | 424 | mAttrCount = 0;
|
---|
| 425 | mCharacterCount = 0;
|
---|
| 426 | mElementCount = 0;
|
---|
| 427 | mSpaceCount = 0;
|
---|
[162] | 428 | }
|
---|
| 429 |
|
---|
[170] | 430 |
|
---|
| 431 |
|
---|
| 432 | // ---------------------------------------------------------------------------
|
---|
| 433 | // StdInParseHandlers: Overrides of the SAX ErrorHandler interface
|
---|
| 434 | // ---------------------------------------------------------------------------
|
---|
| 435 | void
|
---|
| 436 | X3dParseHandlers::error(const SAXParseException& e)
|
---|
[162] | 437 | {
|
---|
[170] | 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 | }
|
---|
[162] | 443 |
|
---|
[170] | 444 | void
|
---|
| 445 | X3dParseHandlers::fatalError(const SAXParseException& e)
|
---|
[162] | 446 | {
|
---|
[170] | 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;
|
---|
[162] | 451 | }
|
---|
| 452 |
|
---|
[170] | 453 | void
|
---|
| 454 | X3dParseHandlers::warning(const SAXParseException& e)
|
---|
[162] | 455 | {
|
---|
[170] | 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;
|
---|
[162] | 460 | }
|
---|
| 461 |
|
---|
[170] | 462 |
|
---|
| 463 | bool
|
---|
| 464 | X3dParser::ParseFile(const string filename,
|
---|
[657] | 465 | SceneGraphNode **root,
|
---|
| 466 | const bool loadPolygonsAsMeshes)
|
---|
[170] | 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;
|
---|
[657] | 499 | X3dParseHandlers handler(*root, loadPolygonsAsMeshes);
|
---|
[170] | 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 | }
|
---|
[176] | 535 |
|
---|
[170] | 536 |
|
---|
| 537 | // Print out the stats that we collected and time taken
|
---|
| 538 | if (!errorCount) {
|
---|
| 539 | XERCES_STD_QUALIFIER cout << filename << ": " << duration << " ms ("
|
---|
[176] | 540 | << handler.GetElementCount() << " elems, "
|
---|
| 541 | << handler.GetAttrCount() << " attrs, "
|
---|
| 542 | << handler.GetSpaceCount() << " spaces, "
|
---|
| 543 | << handler.GetCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
|
---|
[170] | 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;
|
---|
[162] | 558 | }
|
---|
| 559 |
|
---|
[170] | 560 |
|
---|
[260] | 561 |
|
---|
[508] | 562 | /************************************************************************/
|
---|
| 563 | /* class X3dViewCellsParseHandlers implementation */
|
---|
| 564 | /************************************************************************/
|
---|
[260] | 565 |
|
---|
| 566 |
|
---|
| 567 | // ---------------------------------------------------------------------------
|
---|
| 568 | // StdInParseHandlers: Constructors and Destructor
|
---|
| 569 | // ---------------------------------------------------------------------------
|
---|
[439] | 570 | X3dViewCellsParseHandlers::X3dViewCellsParseHandlers(ViewCellsManager *viewCellsManager,
|
---|
[657] | 571 | const float viewCellHeight):
|
---|
[490] | 572 | mElementCount(0),
|
---|
| 573 | mAttrCount(0),
|
---|
| 574 | mCharacterCount(0),
|
---|
| 575 | mSpaceCount(0),
|
---|
| 576 | mViewCellsManager(viewCellsManager),
|
---|
| 577 | mViewCellHeight(viewCellHeight)
|
---|
[260] | 578 | {
|
---|
| 579 | }
|
---|
| 580 |
|
---|
[261] | 581 | X3dViewCellsParseHandlers::~X3dViewCellsParseHandlers()
|
---|
[260] | 582 | {
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 |
|
---|
| 586 | // ---------------------------------------------------------------------------
|
---|
| 587 | // StdInParseHandlers: Implementation of the SAX DocumentHandler interface
|
---|
| 588 | // ---------------------------------------------------------------------------
|
---|
[261] | 589 | void X3dViewCellsParseHandlers::endElement(const XMLCh* const name)
|
---|
[260] | 590 | {
|
---|
| 591 | StrX lname(name);
|
---|
| 592 | string element(lname.LocalForm());
|
---|
[694] | 593 |
|
---|
[260] | 594 | if (element == "Shape")
|
---|
| 595 | EndShape();
|
---|
| 596 | }
|
---|
| 597 |
|
---|
| 598 | void
|
---|
[261] | 599 | X3dViewCellsParseHandlers::EndShape()
|
---|
[260] | 600 | {
|
---|
| 601 | }
|
---|
| 602 |
|
---|
| 603 | void
|
---|
[261] | 604 | X3dViewCellsParseHandlers::StartIndexedFaceSet(
|
---|
[260] | 605 | AttributeList& attributes)
|
---|
| 606 | {
|
---|
| 607 | int len = attributes.getLength();
|
---|
| 608 | int i;
|
---|
[262] | 609 | // clear previous vertex indices
|
---|
| 610 | mCurrentVertexIndices.clear();
|
---|
[260] | 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 |
|
---|
[261] | 623 | while (1)
|
---|
[260] | 624 | {
|
---|
| 625 | int index = strtol(ptr, &endptr, 10);
|
---|
| 626 |
|
---|
[261] | 627 | if (ptr == endptr)
|
---|
| 628 | break;
|
---|
| 629 |
|
---|
| 630 | if (index != -1)
|
---|
[260] | 631 | {
|
---|
[261] | 632 | mCurrentVertexIndices.push_back(index);
|
---|
[260] | 633 | }
|
---|
| 634 |
|
---|
| 635 | ptr = endptr;
|
---|
| 636 | }
|
---|
| 637 | }
|
---|
| 638 | }
|
---|
| 639 | }
|
---|
| 640 |
|
---|
| 641 |
|
---|
| 642 | void
|
---|
[439] | 643 | X3dViewCellsParseHandlers::StartCoordinate(AttributeList& attributes)
|
---|
[260] | 644 | {
|
---|
| 645 | int len = attributes.getLength();
|
---|
| 646 |
|
---|
| 647 | VertexContainer vertices;
|
---|
[364] | 648 | int i;
|
---|
| 649 | for (i=0; i < len; i++)
|
---|
[260] | 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 | {
|
---|
[469] | 663 | float x = (float)strtod(ptr, &endptr);
|
---|
[260] | 664 |
|
---|
| 665 | if (ptr == endptr)
|
---|
| 666 | break;
|
---|
| 667 | ptr = endptr;
|
---|
| 668 |
|
---|
[469] | 669 | float y = (float)(float)strtod(ptr, &endptr);
|
---|
[260] | 670 |
|
---|
| 671 |
|
---|
| 672 | if (ptr == endptr)
|
---|
| 673 | break;
|
---|
| 674 | ptr = endptr;
|
---|
| 675 |
|
---|
[469] | 676 | float z = (float)(float)strtod(ptr, &endptr);
|
---|
[260] | 677 |
|
---|
| 678 | if (ptr == endptr)
|
---|
| 679 | break;
|
---|
| 680 |
|
---|
| 681 | ptr = endptr;
|
---|
| 682 | if (*ptr == ',')
|
---|
| 683 | ptr++;
|
---|
| 684 |
|
---|
| 685 | Vector3 v(x, y, z);
|
---|
[262] | 686 | vertices.push_back(v);
|
---|
[260] | 687 | }
|
---|
| 688 | }
|
---|
| 689 | }
|
---|
[261] | 690 |
|
---|
[333] | 691 | for (i = 0; i < mCurrentVertexIndices.size(); i += 3)
|
---|
[439] | 692 | {
|
---|
[312] | 693 | Triangle3 baseTri(vertices[mCurrentVertexIndices[i + 0]],
|
---|
| 694 | vertices[mCurrentVertexIndices[i + 1]],
|
---|
| 695 | vertices[mCurrentVertexIndices[i + 2]]);
|
---|
[261] | 696 |
|
---|
[262] | 697 | // create view cell from base triangle
|
---|
[490] | 698 | mViewCellsManager->AddViewCell(
|
---|
| 699 | mViewCellsManager->ExtrudeViewCell(baseTri,
|
---|
| 700 | mViewCellHeight));
|
---|
[439] | 701 | }
|
---|
[260] | 702 | }
|
---|
| 703 |
|
---|
| 704 |
|
---|
| 705 | void
|
---|
[261] | 706 | X3dViewCellsParseHandlers::startElement(const XMLCh* const name,
|
---|
[439] | 707 | AttributeList& attributes)
|
---|
[260] | 708 | {
|
---|
| 709 | StrX lname(name);
|
---|
| 710 | string element(lname.LocalForm());
|
---|
| 711 |
|
---|
| 712 | if (element == "IndexedFaceSet") {
|
---|
[261] | 713 | // create the viewcells from individual triangles
|
---|
[260] | 714 | StartIndexedFaceSet(attributes);
|
---|
| 715 | }
|
---|
[261] | 716 |
|
---|
[260] | 717 | if (element == "Coordinate") {
|
---|
[261] | 718 | // add coordinates to the triangles
|
---|
[260] | 719 | StartCoordinate(attributes);
|
---|
| 720 | }
|
---|
[508] | 721 | // do nothing
|
---|
| 722 | //if (element == "Shape") {}
|
---|
[261] | 723 | // ignore material
|
---|
| 724 | //if (element == "Material") {}
|
---|
[260] | 725 |
|
---|
[508] | 726 | ++ mElementCount;
|
---|
[260] | 727 | mAttrCount += attributes.getLength();
|
---|
| 728 | }
|
---|
| 729 |
|
---|
| 730 | void
|
---|
[261] | 731 | X3dViewCellsParseHandlers::characters(const XMLCh* const chars,
|
---|
[260] | 732 | const unsigned int length)
|
---|
| 733 | {
|
---|
| 734 | mCharacterCount += length;
|
---|
| 735 | }
|
---|
| 736 |
|
---|
| 737 | void
|
---|
[261] | 738 | X3dViewCellsParseHandlers::ignorableWhitespace(const XMLCh* const chars,
|
---|
[260] | 739 | const unsigned int length)
|
---|
| 740 | {
|
---|
| 741 | mSpaceCount += length;
|
---|
| 742 | }
|
---|
| 743 |
|
---|
| 744 | void
|
---|
[261] | 745 | X3dViewCellsParseHandlers::resetDocument()
|
---|
[260] | 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
|
---|
[261] | 759 | X3dViewCellsParseHandlers::error(const SAXParseException& e)
|
---|
[260] | 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
|
---|
[261] | 768 | X3dViewCellsParseHandlers::fatalError(const SAXParseException& e)
|
---|
[260] | 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
|
---|
[261] | 777 | X3dViewCellsParseHandlers::warning(const SAXParseException& e)
|
---|
[260] | 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
|
---|
[439] | 787 | X3dParser::ParseFile(const string filename, ViewCellsManager &viewCells)
|
---|
[260] | 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 | //
|
---|
[312] | 819 | X3dViewCellsParseHandlers handler(&viewCells, mViewCellHeight);
|
---|
[260] | 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;
|
---|
[333] | 878 | }
|
---|