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