[2113] | 1 | // ---------------------------------------------------------------------------
|
---|
| 2 | // Includes for all the program files to see
|
---|
| 3 | // ---------------------------------------------------------------------------
|
---|
| 4 | #include <string.h>
|
---|
| 5 | #include <stdlib.h>
|
---|
| 6 | #include <iostream>
|
---|
| 7 | #include <xercesc/util/PlatformUtils.hpp>
|
---|
| 8 |
|
---|
| 9 | // ---------------------------------------------------------------------------
|
---|
| 10 | // Includes
|
---|
| 11 | // ---------------------------------------------------------------------------
|
---|
[2115] | 12 |
|
---|
[2113] | 13 | #include <xercesc/framework/StdInInputSource.hpp>
|
---|
| 14 | #include <xercesc/parsers/SAXParser.hpp>
|
---|
| 15 | #include <xercesc/util/OutOfMemoryException.hpp>
|
---|
| 16 |
|
---|
| 17 | // ---------------------------------------------------------------------------
|
---|
| 18 | // Includes
|
---|
| 19 | // ---------------------------------------------------------------------------
|
---|
[2115] | 20 |
|
---|
[2113] | 21 | #include <xercesc/sax/AttributeList.hpp>
|
---|
| 22 | #include <xercesc/sax/SAXParseException.hpp>
|
---|
| 23 | #include <xercesc/sax/SAXException.hpp>
|
---|
| 24 |
|
---|
[2115] | 25 | #include "ObjectsParser.h"
|
---|
[2113] | 26 |
|
---|
[2115] | 27 | #include "ObjectsParserXerces.h"
|
---|
[2113] | 28 | #include "Mesh.h"
|
---|
| 29 | #include "ViewCellsManager.h"
|
---|
| 30 | #include "GzFileInputSource.h"
|
---|
| 31 | #include "BvHierarchy.h"
|
---|
| 32 |
|
---|
| 33 |
|
---|
[2176] | 34 | using namespace std;
|
---|
| 35 |
|
---|
[2113] | 36 | namespace GtpVisibilityPreprocessor {
|
---|
| 37 |
|
---|
| 38 |
|
---|
| 39 | // ---------------------------------------------------------------------------
|
---|
| 40 | // Local data
|
---|
| 41 | //
|
---|
| 42 | // doNamespaces
|
---|
| 43 | // Indicates whether namespace processing should be enabled or not.
|
---|
| 44 | // The default is no, but -n overrides that.
|
---|
| 45 | //
|
---|
| 46 | // doSchema
|
---|
| 47 | // Indicates whether schema processing should be enabled or not.
|
---|
| 48 | // The default is no, but -s overrides that.
|
---|
| 49 | //
|
---|
| 50 | // schemaFullChecking
|
---|
| 51 | // Indicates whether full schema constraint checking should be enabled or not.
|
---|
| 52 | // The default is no, but -s overrides that.
|
---|
| 53 | //
|
---|
| 54 | // valScheme
|
---|
| 55 | // Indicates what validation scheme to use. It defaults to 'auto', but
|
---|
| 56 | // can be set via the -v= command.
|
---|
| 57 | // ---------------------------------------------------------------------------
|
---|
[2115] | 58 |
|
---|
[2113] | 59 | static bool doNamespaces = false;
|
---|
| 60 | static bool doSchema = false;
|
---|
| 61 | static bool schemaFullChecking = false;
|
---|
| 62 | static SAXParser::ValSchemes valScheme = SAXParser::Val_Auto;
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
| 66 | {
|
---|
| 67 | return obj1->mId < obj2->mId;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | // ---------------------------------------------------------------------------
|
---|
| 72 | // StdInParseHandlers: Constructors and Destructor
|
---|
| 73 | // ---------------------------------------------------------------------------
|
---|
[2115] | 74 |
|
---|
| 75 | ObjectsParseHandlers::ObjectsParseHandlers(ObjectContainer &pvsObjects,
|
---|
| 76 | const ObjectContainer &preprocessorObjects
|
---|
| 77 | ):
|
---|
[2113] | 78 | mElementCount(0)
|
---|
| 79 | , mAttrCount(0)
|
---|
| 80 | , mCharacterCount(0)
|
---|
| 81 | , mSpaceCount(0)
|
---|
| 82 | , mPvsObjects(pvsObjects)
|
---|
| 83 | , mPreprocessorObjects(preprocessorObjects)
|
---|
[2115] | 84 | , mIsObjectSpaceHierarchy(false)
|
---|
[2113] | 85 | {
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 |
|
---|
[2115] | 89 | ObjectsParseHandlers::~ObjectsParseHandlers()
|
---|
[2113] | 90 | {
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 | // ---------------------------------------------------------------------------
|
---|
| 95 | // StdInParseHandlers: Implementation of the SAX DocumentHandler interface
|
---|
| 96 | // ---------------------------------------------------------------------------
|
---|
| 97 |
|
---|
| 98 |
|
---|
[2115] | 99 | void ObjectsParseHandlers::endElement(const XMLCh* const name)
|
---|
[2113] | 100 | {
|
---|
| 101 | StrX lname(name);
|
---|
| 102 | string element(lname.LocalForm());
|
---|
| 103 |
|
---|
| 104 | if (element == "ObjectSpaceHierarchy")
|
---|
| 105 | {
|
---|
| 106 | EndObjectSpaceHierarchy();
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 |
|
---|
[2115] | 111 | void ObjectsParseHandlers::EndObjectSpaceHierarchy()
|
---|
[2113] | 112 | {
|
---|
[2115] | 113 | mIsObjectSpaceHierarchy = false;
|
---|
[2113] | 114 | }
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 | inline static bool vlt(ViewCell *v1, ViewCell *v2)
|
---|
| 118 | {
|
---|
| 119 | return v1->mId < v2->mId;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 |
|
---|
[2115] | 123 | void ObjectsParseHandlers::StartBvhElement(string element,
|
---|
| 124 | AttributeList& attributes)
|
---|
[2113] | 125 | {
|
---|
| 126 | if (element == "Leaf")
|
---|
| 127 | {
|
---|
| 128 | StartBvhLeaf(attributes);
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 |
|
---|
[2115] | 133 | void ObjectsParseHandlers::StartObjectSpaceHierarchyElement(const std::string &element,
|
---|
[2113] | 134 | AttributeList& attributes)
|
---|
| 135 | {
|
---|
| 136 | //-- use cell type according to the chosen method
|
---|
[2115] | 137 | StartBvhElement(element, attributes);
|
---|
[2113] | 138 | }
|
---|
| 139 |
|
---|
| 140 |
|
---|
[2115] | 141 | void ObjectsParseHandlers::startElement(const XMLCh* const name,
|
---|
| 142 | AttributeList& attributes)
|
---|
[2113] | 143 | {
|
---|
| 144 | StrX lname(name);
|
---|
| 145 | string element(lname.LocalForm());
|
---|
| 146 |
|
---|
| 147 | // decides about the view cell hierarchy
|
---|
| 148 | if (element == "ObjectSpaceHierarchy")
|
---|
| 149 | {
|
---|
| 150 | cout << "\nparsing object space hierarchy" << endl;
|
---|
| 151 | Debug << "\nparsing object space hierarchy" << endl;
|
---|
| 152 |
|
---|
[2115] | 153 | mIsObjectSpaceHierarchy = true;
|
---|
[2113] | 154 | }
|
---|
| 155 |
|
---|
| 156 | // parse view space hierarchy
|
---|
[2115] | 157 | if (mIsObjectSpaceHierarchy)
|
---|
[2113] | 158 | {
|
---|
| 159 | StartObjectSpaceHierarchyElement(element, attributes);
|
---|
| 160 | }
|
---|
[2115] | 161 |
|
---|
[2113] | 162 | ++ mElementCount;
|
---|
| 163 | mAttrCount += attributes.getLength();
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 |
|
---|
| 167 |
|
---|
[2115] | 168 | void ObjectsParseHandlers::characters(const XMLCh* const chars,
|
---|
[2113] | 169 | const unsigned int length)
|
---|
| 170 | {
|
---|
| 171 | mCharacterCount += length;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 |
|
---|
[2115] | 175 | void ObjectsParseHandlers::ignorableWhitespace(const XMLCh* const chars,
|
---|
[2113] | 176 | const unsigned int length)
|
---|
| 177 | {
|
---|
| 178 | mSpaceCount += length;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 |
|
---|
[2115] | 182 | void ObjectsParseHandlers::resetDocument()
|
---|
[2113] | 183 | {
|
---|
| 184 | mAttrCount = 0;
|
---|
| 185 | mCharacterCount = 0;
|
---|
| 186 | mElementCount = 0;
|
---|
| 187 | mSpaceCount = 0;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[2119] | 190 |
|
---|
[2115] | 191 | void ObjectsParseHandlers::StartBvhLeaf(AttributeList& attributes)
|
---|
[2113] | 192 | {
|
---|
| 193 | const int len = attributes.getLength();
|
---|
| 194 | Vector3 minBox, maxBox;
|
---|
| 195 |
|
---|
| 196 | ObjectContainer objects;
|
---|
| 197 |
|
---|
| 198 | for (int i = 0; i < len; ++ i)
|
---|
| 199 | {
|
---|
| 200 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
| 201 | StrX attrValue(attributes.getValue(i));
|
---|
| 202 | const char *ptr = attrValue.LocalForm();
|
---|
| 203 |
|
---|
| 204 | if (attrName == "min")
|
---|
| 205 | {
|
---|
| 206 | sscanf(ptr, "%f %f %f", &minBox.x, &minBox.y, &minBox.z);
|
---|
| 207 | }
|
---|
| 208 | if (attrName == "max")
|
---|
| 209 | {
|
---|
| 210 | sscanf(ptr, "%f %f %f", &maxBox.x, &maxBox.y, &maxBox.z);
|
---|
| 211 | }
|
---|
[2119] | 212 | if (attrName == "objects")
|
---|
[2113] | 213 | {
|
---|
| 214 | StartBvhLeafObjects(objects, ptr);
|
---|
[2119] | 215 | }
|
---|
[2113] | 216 | }
|
---|
| 217 |
|
---|
| 218 | AxisAlignedBox3 box = AxisAlignedBox3(minBox, maxBox);
|
---|
| 219 |
|
---|
[2115] | 220 | BvhLeaf *leaf = new BvhLeaf(box, NULL, (int)objects.size());
|
---|
[2113] | 221 |
|
---|
[2119] | 222 | leaf->mObjects = objects;
|
---|
| 223 | BvHierarchy::AssociateObjectsWithLeaf(leaf);
|
---|
[2113] | 224 |
|
---|
[2115] | 225 | // new pvs object
|
---|
| 226 | mPvsObjects.push_back(leaf);
|
---|
[2113] | 227 | }
|
---|
| 228 |
|
---|
| 229 |
|
---|
[2115] | 230 | void ObjectsParseHandlers::StartBvhLeafObjects(ObjectContainer &objects,
|
---|
| 231 | const char *ptr)
|
---|
[2113] | 232 | {
|
---|
| 233 | vector<int> objIndices;
|
---|
| 234 | char *endptr;
|
---|
| 235 |
|
---|
| 236 | while (1)
|
---|
| 237 | {
|
---|
| 238 | const int index = strtol(ptr, &endptr, 10);
|
---|
| 239 | if (ptr == endptr) break;
|
---|
| 240 |
|
---|
| 241 | objIndices.push_back(index);
|
---|
| 242 | ptr = endptr;
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | MeshInstance dummyInst(NULL);
|
---|
| 246 |
|
---|
| 247 | vector<int>::const_iterator it, it_end = objIndices.end();
|
---|
| 248 |
|
---|
| 249 | for (it = objIndices.begin(); it != it_end; ++ it)
|
---|
| 250 | {
|
---|
| 251 | const int objId = *it;
|
---|
[2123] | 252 |
|
---|
| 253 | #if 0
|
---|
| 254 | // assume there is is no id missing
|
---|
| 255 | objects.push_back(mPreprocessorObjects[objId]);
|
---|
| 256 | #else
|
---|
[2113] | 257 | dummyInst.SetId(objId);
|
---|
| 258 |
|
---|
[2115] | 259 | ObjectContainer::const_iterator oit =
|
---|
| 260 | lower_bound(mPreprocessorObjects.begin(),
|
---|
| 261 | mPreprocessorObjects.end(),
|
---|
[2113] | 262 | (Intersectable *)&dummyInst,
|
---|
| 263 | ilt);
|
---|
| 264 |
|
---|
[2115] | 265 | if ((oit != mPreprocessorObjects.end()) && ((*oit)->GetId() == objId))
|
---|
[2113] | 266 | {
|
---|
| 267 | objects.push_back(*oit);
|
---|
| 268 | }
|
---|
[2123] | 269 | #endif
|
---|
[2113] | 270 | }
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 |
|
---|
| 274 |
|
---|
| 275 | // ---------------------------------------------------------------------------
|
---|
| 276 | // StdInParseHandlers: Overrides of the SAX ErrorHandler interface
|
---|
| 277 | // ---------------------------------------------------------------------------
|
---|
| 278 |
|
---|
| 279 |
|
---|
| 280 | void
|
---|
[2115] | 281 | ObjectsParseHandlers::error(const SAXParseException& e)
|
---|
[2113] | 282 | {
|
---|
| 283 | XERCES_STD_QUALIFIER cerr << "\nError at (file " << StrX(e.getSystemId())
|
---|
| 284 | << ", line " << e.getLineNumber()
|
---|
| 285 | << ", char " << e.getColumnNumber()
|
---|
| 286 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | void
|
---|
[2115] | 290 | ObjectsParseHandlers::fatalError(const SAXParseException& e)
|
---|
[2113] | 291 | {
|
---|
| 292 | XERCES_STD_QUALIFIER cerr << "\nFatal Error at (file " << StrX(e.getSystemId())
|
---|
| 293 | << ", line " << e.getLineNumber()
|
---|
| 294 | << ", char " << e.getColumnNumber()
|
---|
| 295 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | void
|
---|
[2115] | 299 | ObjectsParseHandlers::warning(const SAXParseException& e)
|
---|
[2113] | 300 | {
|
---|
| 301 | XERCES_STD_QUALIFIER cerr << "\nWarning at (file " << StrX(e.getSystemId())
|
---|
| 302 | << ", line " << e.getLineNumber()
|
---|
| 303 | << ", char " << e.getColumnNumber()
|
---|
| 304 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 |
|
---|
[2115] | 308 | bool ObjectsParser::ParseObjects(const string &filename,
|
---|
| 309 | ObjectContainer &pvsObjects,
|
---|
| 310 | const ObjectContainer &preprocessorObjects)
|
---|
[2113] | 311 | {
|
---|
[2115] | 312 | // Initialize the XML4C system
|
---|
| 313 | try {
|
---|
| 314 | XMLPlatformUtils::Initialize();
|
---|
| 315 | }
|
---|
[2113] | 316 |
|
---|
[2115] | 317 | catch (const XMLException& toCatch)
|
---|
| 318 | {
|
---|
| 319 | XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
|
---|
| 320 | << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
| 321 | return false;
|
---|
| 322 | }
|
---|
[2113] | 323 |
|
---|
[2115] | 324 | // cout<<"parsing started"<<endl<<flush;
|
---|
[2113] | 325 |
|
---|
[2115] | 326 | //
|
---|
| 327 | // Create a SAX parser object. Then, according to what we were told on
|
---|
| 328 | // the command line, set the options.
|
---|
| 329 | //
|
---|
| 330 | SAXParser* parser = new SAXParser;
|
---|
| 331 | parser->setValidationScheme(valScheme);
|
---|
| 332 | parser->setDoNamespaces(doNamespaces);
|
---|
| 333 | parser->setDoSchema(doSchema);
|
---|
| 334 | parser->setValidationSchemaFullChecking(schemaFullChecking);
|
---|
| 335 |
|
---|
| 336 |
|
---|
| 337 | //
|
---|
| 338 | // Create our SAX handler object and install it on the parser, as the
|
---|
| 339 | // document and error handler. We are responsible for cleaning them
|
---|
| 340 | // up, but since its just stack based here, there's nothing special
|
---|
| 341 | // to do.
|
---|
| 342 | //
|
---|
| 343 | ObjectsParseHandlers handler(pvsObjects, preprocessorObjects);
|
---|
| 344 | parser->setDocumentHandler(&handler);
|
---|
| 345 | parser->setErrorHandler(&handler);
|
---|
| 346 |
|
---|
| 347 | unsigned long duration;
|
---|
| 348 | int errorCount = 0;
|
---|
| 349 | // create a faux scope so that 'src' destructor is called before
|
---|
| 350 | // XMLPlatformUtils::Terminate
|
---|
[2113] | 351 | {
|
---|
[2115] | 352 | //
|
---|
| 353 | // Kick off the parse and catch any exceptions. Create a standard
|
---|
| 354 | // input input source and tell the parser to parse from that.
|
---|
| 355 | //
|
---|
| 356 | // StdInInputSource src;
|
---|
| 357 | try
|
---|
| 358 | {
|
---|
| 359 | const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
[2113] | 360 |
|
---|
| 361 | #if USE_GZLIB
|
---|
[2115] | 362 | XMLCh *myFilePath = XMLString::transcode(filename.c_str());
|
---|
| 363 |
|
---|
| 364 | GzFileInputSource isource(myFilePath);
|
---|
| 365 | parser->parse(isource);
|
---|
[2113] | 366 | #else
|
---|
[2115] | 367 | parser->parse(filename.c_str());
|
---|
[2113] | 368 |
|
---|
| 369 | #endif
|
---|
[2130] | 370 |
|
---|
[2115] | 371 | const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
| 372 | duration = endMillis - startMillis;
|
---|
| 373 | errorCount = parser->getErrorCount();
|
---|
| 374 | }
|
---|
| 375 | catch (const OutOfMemoryException&)
|
---|
| 376 | {
|
---|
| 377 | XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
|
---|
| 378 | errorCount = 2;
|
---|
| 379 | return false;
|
---|
| 380 | }
|
---|
| 381 | catch (const XMLException& e)
|
---|
| 382 | {
|
---|
[2113] | 383 | XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
|
---|
[2115] | 384 | << StrX(e.getMessage())
|
---|
| 385 | << "\n" << XERCES_STD_QUALIFIER endl;
|
---|
[2113] | 386 | errorCount = 1;
|
---|
| 387 | return false;
|
---|
[2115] | 388 | }
|
---|
[2113] | 389 |
|
---|
[2115] | 390 |
|
---|
| 391 | // Print out the stats that we collected and time taken
|
---|
| 392 | if (!errorCount)
|
---|
| 393 | {
|
---|
| 394 | XERCES_STD_QUALIFIER cerr << filename << ": " << duration << " ms ("
|
---|
[2113] | 395 | << handler.GetElementCount() << " elems, "
|
---|
| 396 | << handler.GetAttrCount() << " attrs, "
|
---|
| 397 | << handler.GetSpaceCount() << " spaces, "
|
---|
| 398 | << handler.GetCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
|
---|
[2115] | 399 | }
|
---|
| 400 | }
|
---|
[2113] | 401 |
|
---|
[2115] | 402 | cout << "parsed - will delete the parser" << endl << flush;
|
---|
| 403 | //
|
---|
| 404 | // Delete the parser itself. Must be done prior to calling Terminate, below.
|
---|
| 405 | //
|
---|
| 406 | delete parser;
|
---|
| 407 |
|
---|
| 408 | XMLPlatformUtils::Terminate();
|
---|
| 409 |
|
---|
| 410 | //-- assign new view cells manager
|
---|
| 411 | //*viewCells = handler.mViewCellsManager;
|
---|
| 412 |
|
---|
| 413 | if (errorCount > 0)
|
---|
| 414 | return false;
|
---|
| 415 | else
|
---|
| 416 | return true;
|
---|
[2113] | 417 | }
|
---|
| 418 |
|
---|
| 419 | }
|
---|