1 | // ---------------------------------------------------------------------------
|
---|
2 | // Includes for all the program files to see
|
---|
3 | // ---------------------------------------------------------------------------
|
---|
4 | #include <string.h>
|
---|
5 | #include <stdlib.h>
|
---|
6 | #include <iostream>
|
---|
7 | using namespace std;
|
---|
8 | #include <xercesc/util/PlatformUtils.hpp>
|
---|
9 |
|
---|
10 | // ---------------------------------------------------------------------------
|
---|
11 | // Includes
|
---|
12 | // ---------------------------------------------------------------------------
|
---|
13 | #include <xercesc/framework/StdInInputSource.hpp>
|
---|
14 | #include <xercesc/parsers/SAXParser.hpp>
|
---|
15 | #include <xercesc/util/OutOfMemoryException.hpp>
|
---|
16 |
|
---|
17 | // ---------------------------------------------------------------------------
|
---|
18 | // Includes
|
---|
19 | // ---------------------------------------------------------------------------
|
---|
20 | #include <xercesc/sax/AttributeList.hpp>
|
---|
21 | #include <xercesc/sax/SAXParseException.hpp>
|
---|
22 | #include <xercesc/sax/SAXException.hpp>
|
---|
23 |
|
---|
24 | #include "ViewCellsParser.h"
|
---|
25 |
|
---|
26 | #include "ViewCellsParserXerces.h"
|
---|
27 | #include "Mesh.h"
|
---|
28 | #include "VspBspTree.h"
|
---|
29 | #include "ViewCellBsp.h"
|
---|
30 | #include "ViewCellsManager.h"
|
---|
31 | #include "GzFileInputSource.h"
|
---|
32 | #include "VspOspTree.h"
|
---|
33 | #include "KdTree.h"
|
---|
34 |
|
---|
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 | 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 |
|
---|
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 | ViewCellsParseHandlers::ViewCellsParseHandlers(ObjectContainer *objects,
|
---|
76 | BoundingBoxConverter *bconverter):
|
---|
77 | mElementCount(0)
|
---|
78 | , mAttrCount(0)
|
---|
79 | , mCharacterCount(0)
|
---|
80 | , mSpaceCount(0)
|
---|
81 | , mViewCellsManager(NULL)
|
---|
82 | , mVspBspTree(NULL)
|
---|
83 | , mBspTree(NULL)
|
---|
84 | , mViewCellsTree(NULL)
|
---|
85 | , mCurrentTask(PARSE_OPTIONS)
|
---|
86 | , mCurrentViewCell(NULL)
|
---|
87 | , mCurrentBspNode(NULL)
|
---|
88 | , mCurrentVspNode(NULL)
|
---|
89 | , mCurrentKdNode(NULL)
|
---|
90 | , mObjects(objects)
|
---|
91 | , mBoundingBoxConverter(bconverter)
|
---|
92 | {
|
---|
93 | std::stable_sort(mObjects->begin(), mObjects->end(), ilt);
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | ViewCellsParseHandlers::~ViewCellsParseHandlers()
|
---|
98 | {
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | // ---------------------------------------------------------------------------
|
---|
103 | // StdInParseHandlers: Implementation of the SAX DocumentHandler interface
|
---|
104 | // ---------------------------------------------------------------------------
|
---|
105 |
|
---|
106 |
|
---|
107 | void ViewCellsParseHandlers::endElement(const XMLCh* const name)
|
---|
108 | {
|
---|
109 | StrX lname(name);
|
---|
110 | string element(lname.LocalForm());
|
---|
111 |
|
---|
112 | if (element == "ViewCells")
|
---|
113 | EndViewCells();
|
---|
114 |
|
---|
115 | if (element == "BoundingBoxes")
|
---|
116 | EndBoundingBoxes();
|
---|
117 |
|
---|
118 | // inside the view cell description
|
---|
119 | if (mCurrentTask == PARSE_VIEWCELLS)
|
---|
120 | {
|
---|
121 | if (element == "Interior")
|
---|
122 | EndViewCellInterior();
|
---|
123 | }
|
---|
124 | else
|
---|
125 | {
|
---|
126 | if (element == "Interior")
|
---|
127 | EndBspInterior();
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | void ViewCellsParseHandlers::EndBspInterior()
|
---|
133 | {
|
---|
134 | // go one up in the tree
|
---|
135 | if (mCurrentBspNode->GetParent())
|
---|
136 | { Debug << "]";
|
---|
137 | mCurrentBspNode = mCurrentBspNode->GetParent();
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | void ViewCellsParseHandlers::EndVspInterior()
|
---|
143 | {
|
---|
144 | // go one up in the tree
|
---|
145 | if (mCurrentVspNode->GetParent())
|
---|
146 | { Debug << "]";
|
---|
147 | mCurrentVspNode = mCurrentVspNode->GetParent();
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | void ViewCellsParseHandlers::EndViewCellInterior()
|
---|
152 | {
|
---|
153 | // go one up in the tree
|
---|
154 | if (mCurrentViewCell->GetParent())
|
---|
155 | { Debug << "]";
|
---|
156 | mCurrentViewCell = mCurrentViewCell->GetParent();
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | inline static bool vlt(ViewCell *v1, ViewCell *v2)
|
---|
162 | {
|
---|
163 | return v1->mId < v2->mId;
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | void ViewCellsParseHandlers::EndViewCells()
|
---|
168 | {
|
---|
169 | // sort view cells to help associating view cells according to their id
|
---|
170 | stable_sort(mViewCells.begin(), mViewCells.end(), vlt);
|
---|
171 |
|
---|
172 | // not parsing view cells anymore
|
---|
173 | mCurrentTask = PARSE_OPTIONS;
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | void ViewCellsParseHandlers::EndBoundingBoxes()
|
---|
178 | {
|
---|
179 | // all bounding boxes gathered in this step =>
|
---|
180 | // associate object ids with bounding boxes
|
---|
181 | long startTime = GetTime();
|
---|
182 |
|
---|
183 | if (mBoundingBoxConverter)
|
---|
184 | mBoundingBoxConverter->IdentifyObjects(mIBoundingBoxes, *mObjects);
|
---|
185 |
|
---|
186 | Debug << "\nconverted bounding boxes to objects in "
|
---|
187 | << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | void ViewCellsParseHandlers::StartHierarchy(AttributeList& attributes)
|
---|
192 | {
|
---|
193 | int len = attributes.getLength();
|
---|
194 |
|
---|
195 | for (int i = 0; i < len; ++ i)
|
---|
196 | {
|
---|
197 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
198 |
|
---|
199 | if (attrName == "name")
|
---|
200 | {
|
---|
201 | StrX attrValue(attributes.getValue(i));
|
---|
202 |
|
---|
203 | const char *ptr = attrValue.LocalForm();
|
---|
204 |
|
---|
205 | //-- the view cells manager is created here
|
---|
206 | CreateViewCellsManager(ptr);
|
---|
207 | }
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | void ViewCellsParseHandlers::StartBspElement(string element,
|
---|
213 | AttributeList& attributes)
|
---|
214 | {
|
---|
215 | if (element == "Interior")
|
---|
216 | {
|
---|
217 | Debug << "[";
|
---|
218 | StartBspInterior(attributes);
|
---|
219 | }
|
---|
220 |
|
---|
221 | if (element == "Leaf")
|
---|
222 | {
|
---|
223 | Debug << "l";
|
---|
224 | StartBspLeaf(attributes);
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | void ViewCellsParseHandlers::StartVspElement(string element,
|
---|
230 | AttributeList& attributes)
|
---|
231 | {
|
---|
232 | if (element == "Interior")
|
---|
233 | {
|
---|
234 | Debug << "[";
|
---|
235 | StartVspInterior(attributes);
|
---|
236 | }
|
---|
237 |
|
---|
238 | if (element == "Leaf")
|
---|
239 | {
|
---|
240 | Debug << "l";
|
---|
241 | StartVspLeaf(attributes);
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | void ViewCellsParseHandlers::StartOspElement(string element,
|
---|
247 | AttributeList& attributes)
|
---|
248 | {
|
---|
249 | if (element == "Interior")
|
---|
250 | {
|
---|
251 | Debug << "[";
|
---|
252 | StartOspInterior(attributes);
|
---|
253 | }
|
---|
254 |
|
---|
255 | if (element == "Leaf")
|
---|
256 | {
|
---|
257 | Debug << "l";
|
---|
258 | StartOspLeaf(attributes);
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | void ViewCellsParseHandlers::StartViewSpacePartitionElement(const std::string &element,
|
---|
263 | AttributeList& attributes)
|
---|
264 | {
|
---|
265 | if (!mViewCellsManager)
|
---|
266 | return;
|
---|
267 |
|
---|
268 | //-- use cell type according to the chosen view cell manager
|
---|
269 | switch (mViewCellsManager->GetType())
|
---|
270 | {
|
---|
271 | case ViewCellsManager::BSP:
|
---|
272 | case ViewCellsManager::VSP_BSP:
|
---|
273 | StartBspElement(element, attributes);
|
---|
274 | break;
|
---|
275 | case ViewCellsManager::VSP_OSP:
|
---|
276 | StartVspElement(element, attributes);
|
---|
277 | break;
|
---|
278 | default:
|
---|
279 | Debug << "not implemented" << endl;
|
---|
280 | break;
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 | void ViewCellsParseHandlers::StartObjectSpacePartitionElement(const std::string &element,
|
---|
286 | AttributeList& attributes)
|
---|
287 | {
|
---|
288 | if (!mViewCellsManager)
|
---|
289 | return;
|
---|
290 |
|
---|
291 | //-- use cell type according to the chosen view cell manager
|
---|
292 | switch (mViewCellsManager->GetType())
|
---|
293 | {
|
---|
294 | case ViewCellsManager::VSP_OSP:
|
---|
295 | StartOspElement(element, attributes);
|
---|
296 | break;
|
---|
297 | default:
|
---|
298 | Debug << "not implemented" << endl;
|
---|
299 | break;
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 |
|
---|
304 | void ViewCellsParseHandlers::StartViewCellHierarchyElement(const std::string &element,
|
---|
305 | AttributeList& attributes)
|
---|
306 | {
|
---|
307 | // interiors + leaves interpreted view cells else
|
---|
308 | if (element == "Interior")
|
---|
309 | {
|
---|
310 | Debug << "[";
|
---|
311 | StartViewCellInterior(attributes);
|
---|
312 | }
|
---|
313 |
|
---|
314 | if (element == "Leaf")
|
---|
315 | {
|
---|
316 | Debug << "l";
|
---|
317 | StartViewCellLeaf(attributes);
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 |
|
---|
322 | void ViewCellsParseHandlers::startElement(const XMLCh* const name,
|
---|
323 | AttributeList& attributes)
|
---|
324 | {
|
---|
325 | StrX lname(name);
|
---|
326 | string element(lname.LocalForm());
|
---|
327 |
|
---|
328 | if (element == "ViewCells")
|
---|
329 | {
|
---|
330 | cout << "parsing view cells" << endl;
|
---|
331 | mCurrentTask = PARSE_VIEWCELLS;
|
---|
332 | }
|
---|
333 |
|
---|
334 | // decides about the view cells manager type
|
---|
335 | if (element == "Hierarchy")
|
---|
336 | {
|
---|
337 | cout << "parsing view cells manager type" << endl;
|
---|
338 | StartHierarchy(attributes);
|
---|
339 | }
|
---|
340 |
|
---|
341 | // decides about the view cell hierarchy
|
---|
342 | if (element == "ViewSpacePartition")
|
---|
343 | {
|
---|
344 | //StartViewSpacePartition(attributes);
|
---|
345 | cout << "parsing view space partition" << endl;
|
---|
346 | mCurrentTask = PARSE_VSP;
|
---|
347 | }
|
---|
348 |
|
---|
349 | // decides about the view cell hierarchy
|
---|
350 | if (element == "ObjectSpacePartition")
|
---|
351 | {
|
---|
352 | //StartObjectSpacePartition(attributes);
|
---|
353 | cout << "parsing object space partition" << endl;
|
---|
354 | mCurrentTask = PARSE_OSP;
|
---|
355 | }
|
---|
356 |
|
---|
357 | // decides the used view cell hierarchy
|
---|
358 | if (element == "ViewSpaceBox")
|
---|
359 | {
|
---|
360 | Debug << "v";
|
---|
361 | StartViewSpaceBox(attributes);
|
---|
362 | }
|
---|
363 |
|
---|
364 | // decides the used view cell hierarchy
|
---|
365 | if (element == "BoundingBox")
|
---|
366 | {
|
---|
367 | Debug << "b";
|
---|
368 | StartBoundingBox(attributes);
|
---|
369 | }
|
---|
370 |
|
---|
371 | // parse view space partition
|
---|
372 | switch (mCurrentTask)
|
---|
373 | {
|
---|
374 | case PARSE_VSP:
|
---|
375 | StartViewSpacePartitionElement(element, attributes);
|
---|
376 | break;
|
---|
377 | case PARSE_OSP:
|
---|
378 | StartObjectSpacePartitionElement(element, attributes);
|
---|
379 | break;
|
---|
380 | case PARSE_VIEWCELLS:
|
---|
381 | StartViewCellHierarchyElement(element, attributes);
|
---|
382 | break;
|
---|
383 | default:
|
---|
384 | break;
|
---|
385 | }
|
---|
386 |
|
---|
387 | ++ mElementCount;
|
---|
388 | mAttrCount += attributes.getLength();
|
---|
389 | }
|
---|
390 |
|
---|
391 |
|
---|
392 | void ViewCellsParseHandlers::StartViewCell(ViewCell *viewCell,
|
---|
393 | AttributeList& attributes)
|
---|
394 | {
|
---|
395 | int len = attributes.getLength();
|
---|
396 | vector<int> objIndices;
|
---|
397 |
|
---|
398 | for (int i = 0; i < len; ++ i)
|
---|
399 | {
|
---|
400 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
401 |
|
---|
402 | if (attrName == "pvs")
|
---|
403 | {
|
---|
404 | StrX attrValue(attributes.getValue(i));
|
---|
405 |
|
---|
406 | // handle coordIndex
|
---|
407 | objIndices.clear();
|
---|
408 | const char *ptr = attrValue.LocalForm();
|
---|
409 | char *endptr;
|
---|
410 |
|
---|
411 | while (1)
|
---|
412 | {
|
---|
413 | int index = strtol(ptr, &endptr, 10);
|
---|
414 |
|
---|
415 | if (ptr == endptr)
|
---|
416 | break;
|
---|
417 |
|
---|
418 | objIndices.push_back(index);
|
---|
419 |
|
---|
420 | ptr = endptr;
|
---|
421 | }
|
---|
422 |
|
---|
423 | //TODO: find objects and add them to pvs
|
---|
424 | // TODO: get view cell with specified id
|
---|
425 | MeshInstance dummyInst(NULL);
|
---|
426 |
|
---|
427 | vector<int>::const_iterator it, it_end = objIndices.end();
|
---|
428 | for (it = objIndices.begin(); it != it_end; ++ it)
|
---|
429 | {
|
---|
430 | const int objId = *it;
|
---|
431 | dummyInst.SetId(objId);
|
---|
432 |
|
---|
433 | ObjectContainer::iterator oit =
|
---|
434 | lower_bound(mObjects->begin(), mObjects->end(), (Intersectable *)&dummyInst, ilt);
|
---|
435 |
|
---|
436 | if ((oit != mObjects->end()) && ((*oit)->GetId() == objId))
|
---|
437 | {
|
---|
438 | // $$JB we should store a float a per object which corresponds
|
---|
439 | // to sumof pdfs, i.e. its relative visibility
|
---|
440 | // temporarily set to 1.0f
|
---|
441 | viewCell->GetPvs().AddSample(*oit, 1.0f);
|
---|
442 | }
|
---|
443 | else
|
---|
444 | {
|
---|
445 | Debug << "error: object with id " << objId << " does not exist" << endl;
|
---|
446 | }
|
---|
447 | }
|
---|
448 | }
|
---|
449 | else if (attrName == "id")
|
---|
450 | {
|
---|
451 | StrX attrValue(attributes.getValue(i));
|
---|
452 |
|
---|
453 | const char *ptr = attrValue.LocalForm();
|
---|
454 | char *endptr = NULL;
|
---|
455 | const int id = strtol(ptr, &endptr, 10);
|
---|
456 |
|
---|
457 | viewCell->SetId(id);
|
---|
458 | }
|
---|
459 | /*else if (attrName == "active")
|
---|
460 | {
|
---|
461 | StrX attrValue(attributes.getValue(i));
|
---|
462 |
|
---|
463 | const char *ptr = attrValue.LocalForm();
|
---|
464 | char *endptr = NULL;
|
---|
465 | const bool isActive = (bool)strtol(ptr, &endptr, 10);
|
---|
466 |
|
---|
467 | if (isActive)
|
---|
468 | viewCell->SetActive();
|
---|
469 | }*/
|
---|
470 | else if (attrName == "mergecost")
|
---|
471 | {
|
---|
472 | StrX attrValue(attributes.getValue(i));
|
---|
473 |
|
---|
474 | const char *ptr = attrValue.LocalForm();
|
---|
475 | char *endptr = NULL;
|
---|
476 | const float cost = (float)strtod(ptr, &endptr);
|
---|
477 |
|
---|
478 | viewCell->SetMergeCost(cost);
|
---|
479 | }
|
---|
480 | }
|
---|
481 | }
|
---|
482 |
|
---|
483 |
|
---|
484 | void ViewCellsParseHandlers::StartViewSpaceBox(AttributeList& attributes)
|
---|
485 | {
|
---|
486 | int len = attributes.getLength();
|
---|
487 |
|
---|
488 | Vector3 bmin, bmax;
|
---|
489 |
|
---|
490 | for (int i = 0; i < len; ++ i)
|
---|
491 | {
|
---|
492 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
493 | StrX attrValue(attributes.getValue(i));
|
---|
494 | const char *ptr = attrValue.LocalForm();
|
---|
495 |
|
---|
496 | if (attrName == "min")
|
---|
497 | {
|
---|
498 | sscanf(ptr, "%f %f %f",
|
---|
499 | &bmin.x, &bmin.y, &bmin.z);
|
---|
500 | }
|
---|
501 | else if (attrName == "max")
|
---|
502 | {
|
---|
503 | sscanf(ptr, "%f %f %f",
|
---|
504 | &bmax.x, &bmax.y, &bmax.z);
|
---|
505 | }
|
---|
506 | }
|
---|
507 |
|
---|
508 | mViewSpaceBox = AxisAlignedBox3(bmin, bmax);
|
---|
509 |
|
---|
510 | Debug << "\nview space box: " << mViewSpaceBox << endl;
|
---|
511 | }
|
---|
512 |
|
---|
513 |
|
---|
514 | void ViewCellsParseHandlers::StartBoundingBox(AttributeList& attributes)
|
---|
515 | {
|
---|
516 | int len = attributes.getLength();
|
---|
517 |
|
---|
518 | Vector3 bmin, bmax;
|
---|
519 | int id;
|
---|
520 |
|
---|
521 | for (int i = 0; i < len; ++ i)
|
---|
522 | {
|
---|
523 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
524 | StrX attrValue(attributes.getValue(i));
|
---|
525 | const char *ptr = attrValue.LocalForm();
|
---|
526 |
|
---|
527 |
|
---|
528 | if (attrName == "id")
|
---|
529 | {
|
---|
530 | sscanf(ptr, "%d", &id);
|
---|
531 | }
|
---|
532 |
|
---|
533 | if (attrName == "min")
|
---|
534 | {
|
---|
535 | sscanf(ptr, "%f %f %f",
|
---|
536 | &bmin.x, &bmin.y, &bmin.z);
|
---|
537 | }
|
---|
538 | else if (attrName == "max")
|
---|
539 | {
|
---|
540 | sscanf(ptr, "%f %f %f",
|
---|
541 | &bmax.x, &bmax.y, &bmax.z);
|
---|
542 | }
|
---|
543 | }
|
---|
544 |
|
---|
545 | AxisAlignedBox3 box(bmin, bmax);
|
---|
546 | mIBoundingBoxes.push_back(IndexedBoundingBox(id, box));
|
---|
547 |
|
---|
548 | //Debug << "bbox: " << box << endl;
|
---|
549 | }
|
---|
550 |
|
---|
551 |
|
---|
552 | void ViewCellsParseHandlers::StartBspLeaf(AttributeList& attributes)
|
---|
553 | {
|
---|
554 | BspLeaf * leaf =
|
---|
555 | new BspLeaf(dynamic_cast<BspInterior *>(mCurrentBspNode), NULL);
|
---|
556 |
|
---|
557 | if (mCurrentBspNode) // replace front or (if not NULL) back child
|
---|
558 | {
|
---|
559 | dynamic_cast<BspInterior *>(mCurrentBspNode)->ReplaceChildLink(NULL, leaf);
|
---|
560 | }
|
---|
561 | else
|
---|
562 | {
|
---|
563 | if (mViewCellsManager->GetType() == ViewCellsManager::BSP)
|
---|
564 | {
|
---|
565 | mBspTree->mRoot = leaf;
|
---|
566 | }
|
---|
567 | else if (mViewCellsManager->GetType() == ViewCellsManager::VSP_BSP)
|
---|
568 | {
|
---|
569 | mVspBspTree->mRoot = leaf;
|
---|
570 | }
|
---|
571 | }
|
---|
572 |
|
---|
573 | //-- find associated view cell
|
---|
574 | int viewCellId;
|
---|
575 |
|
---|
576 | int len = attributes.getLength();
|
---|
577 |
|
---|
578 | for (int i = 0; i < len; ++ i)
|
---|
579 | {
|
---|
580 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
581 | StrX attrValue(attributes.getValue(i));
|
---|
582 |
|
---|
583 | const char *ptr = attrValue.LocalForm();
|
---|
584 | char *endptr = NULL;
|
---|
585 |
|
---|
586 | if (attrName == "viewCellId")
|
---|
587 | {
|
---|
588 | viewCellId = strtol(ptr, &endptr, 10);
|
---|
589 | }
|
---|
590 | }
|
---|
591 |
|
---|
592 |
|
---|
593 | if (viewCellId >= 0) // valid view cell
|
---|
594 | {
|
---|
595 | // TODO: get view cell with specified id
|
---|
596 | ViewCellInterior dummyVc;
|
---|
597 | dummyVc.SetId(viewCellId);
|
---|
598 |
|
---|
599 | ViewCellContainer::iterator vit =
|
---|
600 | lower_bound(mViewCells.begin(), mViewCells.end(), &dummyVc, vlt);
|
---|
601 |
|
---|
602 | BspViewCell *viewCell = dynamic_cast<BspViewCell *>(*vit);
|
---|
603 |
|
---|
604 | if (viewCell->GetId() == viewCellId)
|
---|
605 | {
|
---|
606 | leaf->SetViewCell(viewCell);
|
---|
607 | viewCell->mLeaf = leaf;
|
---|
608 | }
|
---|
609 | else
|
---|
610 | {
|
---|
611 | Debug << "error: view cell does not exist" << endl;
|
---|
612 | }
|
---|
613 | }
|
---|
614 | else
|
---|
615 | {
|
---|
616 | // add to invalid view space
|
---|
617 | if (mViewCellsManager->GetType() == ViewCellsManager::VSP_BSP)
|
---|
618 | {
|
---|
619 | leaf->SetViewCell(mVspBspTree->GetOrCreateOutOfBoundsCell());
|
---|
620 | leaf->SetTreeValid(false);
|
---|
621 | mVspBspTree->PropagateUpValidity(leaf);
|
---|
622 | }
|
---|
623 | }
|
---|
624 | }
|
---|
625 |
|
---|
626 |
|
---|
627 | void ViewCellsParseHandlers::StartBspInterior(AttributeList& attributes)
|
---|
628 | {
|
---|
629 | Plane3 plane;
|
---|
630 | int len = attributes.getLength();
|
---|
631 |
|
---|
632 | for (int i = 0; i < len; ++ i)
|
---|
633 | {
|
---|
634 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
635 | StrX attrValue(attributes.getValue(i));
|
---|
636 | const char *ptr = attrValue.LocalForm();
|
---|
637 |
|
---|
638 | if (attrName == "plane")
|
---|
639 | {
|
---|
640 | sscanf(ptr, "%f %f %f %f",
|
---|
641 | &plane.mNormal.x, &plane.mNormal.y, &plane.mNormal.z, &plane.mD);
|
---|
642 | }
|
---|
643 | }
|
---|
644 |
|
---|
645 | BspInterior* interior = new BspInterior(plane);
|
---|
646 |
|
---|
647 | if (mCurrentBspNode) // replace NULL child of parent with current node
|
---|
648 | {
|
---|
649 | BspInterior *current = dynamic_cast<BspInterior *>(mCurrentBspNode);
|
---|
650 |
|
---|
651 | current->ReplaceChildLink(NULL, interior);
|
---|
652 | interior->SetParent(current);
|
---|
653 | }
|
---|
654 | else
|
---|
655 | {
|
---|
656 | if (mViewCellsManager->GetType() == ViewCellsManager::BSP)
|
---|
657 | {
|
---|
658 | mBspTree->mRoot = interior;
|
---|
659 | }
|
---|
660 | else
|
---|
661 | {
|
---|
662 | mVspBspTree->mRoot = interior;
|
---|
663 | }
|
---|
664 | }
|
---|
665 |
|
---|
666 | mCurrentBspNode = interior;
|
---|
667 | }
|
---|
668 |
|
---|
669 |
|
---|
670 | void ViewCellsParseHandlers::StartViewCellLeaf(AttributeList& attributes)
|
---|
671 | {
|
---|
672 | BspViewCell *viewCell = new BspViewCell();
|
---|
673 |
|
---|
674 | if (mCurrentViewCell) // replace front or (if not NULL) back child
|
---|
675 | {
|
---|
676 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(mCurrentViewCell);
|
---|
677 | interior->SetupChildLink(viewCell);
|
---|
678 | }
|
---|
679 | else // root
|
---|
680 | {
|
---|
681 | mViewCellsTree->SetRoot(viewCell);
|
---|
682 | }
|
---|
683 |
|
---|
684 | StartViewCell(viewCell, attributes);
|
---|
685 |
|
---|
686 | // collect leaves
|
---|
687 | mViewCells.push_back(viewCell);
|
---|
688 | }
|
---|
689 |
|
---|
690 |
|
---|
691 | void ViewCellsParseHandlers::StartViewCellInterior(AttributeList& attributes)
|
---|
692 | {
|
---|
693 | ViewCellInterior* interior = new ViewCellInterior();
|
---|
694 |
|
---|
695 | if (mCurrentViewCell) // replace NULL child of parent with current node
|
---|
696 | {
|
---|
697 | ViewCellInterior *current = dynamic_cast<ViewCellInterior *>(mCurrentViewCell);
|
---|
698 |
|
---|
699 | current->SetupChildLink(interior);
|
---|
700 | }
|
---|
701 | else
|
---|
702 | {
|
---|
703 | mViewCellsTree->SetRoot(interior);
|
---|
704 | }
|
---|
705 |
|
---|
706 | mCurrentViewCell = interior;
|
---|
707 |
|
---|
708 | StartViewCell(interior, attributes);
|
---|
709 | }
|
---|
710 |
|
---|
711 |
|
---|
712 |
|
---|
713 | void ViewCellsParseHandlers::CreateViewCellsManager(const char *name)
|
---|
714 | {
|
---|
715 | if (strcmp(name, "bspTree") == 0)
|
---|
716 | {
|
---|
717 | Debug << "view cell type: Bsp" << endl;
|
---|
718 |
|
---|
719 | mBspTree = new BspTree();
|
---|
720 | mBspTree->mBox = mViewSpaceBox;
|
---|
721 | //mCurrentBspNode = mBspTree->GetRoot();
|
---|
722 |
|
---|
723 | mViewCellsManager = new BspViewCellsManager(mBspTree);
|
---|
724 | }
|
---|
725 | else if (strcmp(name, "vspBspTree") == 0) //
|
---|
726 | {
|
---|
727 | Debug << "view cell type: VspBsp" << endl;
|
---|
728 |
|
---|
729 | mVspBspTree = new VspBspTree();
|
---|
730 | //mCurrentBspNode = mVspBspTree->GetRoot();
|
---|
731 | mViewCellsManager = new VspBspViewCellsManager(mVspBspTree);
|
---|
732 |
|
---|
733 | mVspBspTree->mBox = mViewSpaceBox;
|
---|
734 | }
|
---|
735 | else if (strcmp(name, "vspOspTree") == 0)
|
---|
736 | {
|
---|
737 | Debug << "view cell type: VspOsp" << endl;
|
---|
738 |
|
---|
739 | mVspTree = new VspTree();
|
---|
740 | mOspTree = new OspTree();
|
---|
741 |
|
---|
742 | // hack
|
---|
743 | mViewCellsManager = new VspOspViewCellsManager(mVspTree, mOspTree);
|
---|
744 |
|
---|
745 | mVspTree->mBoundingBox = mViewSpaceBox;
|
---|
746 | }
|
---|
747 |
|
---|
748 |
|
---|
749 | mViewCellsTree = mViewCellsManager->GetViewCellsTree();
|
---|
750 | mViewCellsManager->SetViewSpaceBox(mViewSpaceBox);
|
---|
751 | }
|
---|
752 |
|
---|
753 |
|
---|
754 | void ViewCellsParseHandlers::characters(const XMLCh* const chars,
|
---|
755 | const unsigned int length)
|
---|
756 | {
|
---|
757 | mCharacterCount += length;
|
---|
758 | }
|
---|
759 |
|
---|
760 |
|
---|
761 | void ViewCellsParseHandlers::ignorableWhitespace(const XMLCh* const chars,
|
---|
762 | const unsigned int length)
|
---|
763 | {
|
---|
764 | mSpaceCount += length;
|
---|
765 | }
|
---|
766 |
|
---|
767 |
|
---|
768 | void ViewCellsParseHandlers::resetDocument()
|
---|
769 | {
|
---|
770 | mAttrCount = 0;
|
---|
771 | mCharacterCount = 0;
|
---|
772 | mElementCount = 0;
|
---|
773 | mSpaceCount = 0;
|
---|
774 | }
|
---|
775 |
|
---|
776 |
|
---|
777 | void ViewCellsParseHandlers::StartVspLeaf(AttributeList& attributes)
|
---|
778 | {
|
---|
779 | VspLeaf * leaf =
|
---|
780 | new VspLeaf(dynamic_cast<VspInterior *>(mCurrentVspNode), NULL);
|
---|
781 |
|
---|
782 | if (mCurrentVspNode) // replace front or (if not NULL) back child
|
---|
783 | {
|
---|
784 | dynamic_cast<VspInterior *>(mCurrentVspNode)->ReplaceChildLink(NULL, leaf);
|
---|
785 | }
|
---|
786 | else
|
---|
787 | {
|
---|
788 | mVspTree->mRoot = leaf;
|
---|
789 | }
|
---|
790 |
|
---|
791 | //-- find associated view cell
|
---|
792 | int viewCellId;
|
---|
793 |
|
---|
794 | int len = attributes.getLength();
|
---|
795 |
|
---|
796 | for (int i = 0; i < len; ++ i)
|
---|
797 | {
|
---|
798 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
799 | StrX attrValue(attributes.getValue(i));
|
---|
800 |
|
---|
801 | const char *ptr = attrValue.LocalForm();
|
---|
802 | char *endptr = NULL;
|
---|
803 |
|
---|
804 | if (attrName == "viewCellId")
|
---|
805 | {
|
---|
806 | viewCellId = strtol(ptr, &endptr, 10);
|
---|
807 | }
|
---|
808 | }
|
---|
809 |
|
---|
810 | if (viewCellId >= 0) // valid view cell
|
---|
811 | {
|
---|
812 | // TODO: get view cell with specified id
|
---|
813 | ViewCellInterior dummyVc;
|
---|
814 | dummyVc.SetId(viewCellId);
|
---|
815 |
|
---|
816 | ViewCellContainer::iterator vit =
|
---|
817 | lower_bound(mViewCells.begin(), mViewCells.end(), &dummyVc, vlt);
|
---|
818 |
|
---|
819 | VspViewCell *viewCell = dynamic_cast<VspViewCell *>(*vit);
|
---|
820 |
|
---|
821 | if (viewCell->GetId() == viewCellId)
|
---|
822 | {
|
---|
823 | leaf->SetViewCell(viewCell);
|
---|
824 | viewCell->mLeaf = leaf;
|
---|
825 | }
|
---|
826 | else
|
---|
827 | {
|
---|
828 | Debug << "error: view cell does not exist" << endl;
|
---|
829 | }
|
---|
830 | }
|
---|
831 | else
|
---|
832 | {
|
---|
833 | // add to invalid view space
|
---|
834 | leaf->SetViewCell(mVspTree->GetOrCreateOutOfBoundsCell());
|
---|
835 | leaf->SetTreeValid(false);
|
---|
836 | mVspTree->PropagateUpValidity(leaf);
|
---|
837 | }
|
---|
838 | }
|
---|
839 |
|
---|
840 |
|
---|
841 | void ViewCellsParseHandlers::StartVspInterior(AttributeList& attributes)
|
---|
842 | {
|
---|
843 | AxisAlignedPlane plane;
|
---|
844 | int len = attributes.getLength();
|
---|
845 |
|
---|
846 | for (int i = 0; i < len; ++ i)
|
---|
847 | {
|
---|
848 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
849 | StrX attrValue(attributes.getValue(i));
|
---|
850 | const char *ptr = attrValue.LocalForm();
|
---|
851 |
|
---|
852 | if (attrName == "plane")
|
---|
853 | {
|
---|
854 | sscanf(ptr, "%d %f",
|
---|
855 | &plane.mAxis, &plane.mPosition);
|
---|
856 | }
|
---|
857 | }
|
---|
858 |
|
---|
859 | VspInterior* interior = new VspInterior(plane);
|
---|
860 |
|
---|
861 | if (mCurrentVspNode) // replace NULL child of parent with current node
|
---|
862 | {
|
---|
863 | VspInterior *current = dynamic_cast<VspInterior *>(mCurrentVspNode);
|
---|
864 |
|
---|
865 | current->ReplaceChildLink(NULL, interior);
|
---|
866 | interior->SetParent(current);
|
---|
867 | }
|
---|
868 | else
|
---|
869 | {
|
---|
870 | mVspTree->mRoot = interior;
|
---|
871 | }
|
---|
872 |
|
---|
873 | mCurrentVspNode = interior;
|
---|
874 | }
|
---|
875 |
|
---|
876 |
|
---|
877 | void ViewCellsParseHandlers::StartOspInterior(AttributeList& attributes)
|
---|
878 | {
|
---|
879 | AxisAlignedPlane plane;
|
---|
880 | int len = attributes.getLength();
|
---|
881 |
|
---|
882 | for (int i = 0; i < len; ++ i)
|
---|
883 | {
|
---|
884 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
885 | StrX attrValue(attributes.getValue(i));
|
---|
886 | const char *ptr = attrValue.LocalForm();
|
---|
887 |
|
---|
888 | if (attrName == "plane")
|
---|
889 | {
|
---|
890 | sscanf(ptr, "%d %f",
|
---|
891 | &plane.mAxis, &plane.mPosition);
|
---|
892 | }
|
---|
893 | }
|
---|
894 |
|
---|
895 | KdInterior* interior = new KdInterior(NULL);
|
---|
896 |
|
---|
897 | interior->mAxis = plane.mAxis;
|
---|
898 | interior->mPosition = plane.mPosition;
|
---|
899 |
|
---|
900 | if (mCurrentKdNode) // replace NULL child of parent with current node
|
---|
901 | {
|
---|
902 | KdInterior *parent = dynamic_cast<KdInterior *>(mCurrentKdNode);
|
---|
903 | parent->ReplaceChildLink(NULL, interior);
|
---|
904 | interior->mParent = parent;
|
---|
905 | }
|
---|
906 | else
|
---|
907 | {
|
---|
908 | mOspTree->mRoot = interior;
|
---|
909 | }
|
---|
910 |
|
---|
911 | mCurrentKdNode = interior;
|
---|
912 | }
|
---|
913 |
|
---|
914 |
|
---|
915 | void ViewCellsParseHandlers::StartOspLeaf(AttributeList& attributes)
|
---|
916 | {
|
---|
917 | KdLeaf * leaf =
|
---|
918 | new KdLeaf(dynamic_cast<KdInterior *>(mCurrentKdNode), NULL);
|
---|
919 |
|
---|
920 | if (mCurrentKdNode) // replace front or (if not NULL) back child
|
---|
921 | {
|
---|
922 | dynamic_cast<KdInterior *>(mCurrentKdNode)->ReplaceChildLink(NULL, leaf);
|
---|
923 | }
|
---|
924 | else
|
---|
925 | {
|
---|
926 | mOspTree->mRoot = leaf;
|
---|
927 | }
|
---|
928 | }
|
---|
929 |
|
---|
930 |
|
---|
931 |
|
---|
932 |
|
---|
933 | // ---------------------------------------------------------------------------
|
---|
934 | // StdInParseHandlers: Overrides of the SAX ErrorHandler interface
|
---|
935 | // ---------------------------------------------------------------------------
|
---|
936 |
|
---|
937 |
|
---|
938 | void
|
---|
939 | ViewCellsParseHandlers::error(const SAXParseException& e)
|
---|
940 | {
|
---|
941 | XERCES_STD_QUALIFIER cerr << "\nError at (file " << StrX(e.getSystemId())
|
---|
942 | << ", line " << e.getLineNumber()
|
---|
943 | << ", char " << e.getColumnNumber()
|
---|
944 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
945 | }
|
---|
946 |
|
---|
947 | void
|
---|
948 | ViewCellsParseHandlers::fatalError(const SAXParseException& e)
|
---|
949 | {
|
---|
950 | XERCES_STD_QUALIFIER cerr << "\nFatal Error at (file " << StrX(e.getSystemId())
|
---|
951 | << ", line " << e.getLineNumber()
|
---|
952 | << ", char " << e.getColumnNumber()
|
---|
953 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
954 | }
|
---|
955 |
|
---|
956 | void
|
---|
957 | ViewCellsParseHandlers::warning(const SAXParseException& e)
|
---|
958 | {
|
---|
959 | XERCES_STD_QUALIFIER cerr << "\nWarning at (file " << StrX(e.getSystemId())
|
---|
960 | << ", line " << e.getLineNumber()
|
---|
961 | << ", char " << e.getColumnNumber()
|
---|
962 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
963 | }
|
---|
964 |
|
---|
965 |
|
---|
966 | bool ViewCellsParser::ParseFile(const string filename,
|
---|
967 | ViewCellsManager **viewCells,
|
---|
968 | ObjectContainer *objects,
|
---|
969 | BoundingBoxConverter *bconverter)
|
---|
970 | {
|
---|
971 | // Initialize the XML4C system
|
---|
972 | try {
|
---|
973 | XMLPlatformUtils::Initialize();
|
---|
974 | }
|
---|
975 |
|
---|
976 | catch (const XMLException& toCatch)
|
---|
977 | {
|
---|
978 | XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
|
---|
979 | << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
980 | return false;
|
---|
981 | }
|
---|
982 |
|
---|
983 | //
|
---|
984 | // Create a SAX parser object. Then, according to what we were told on
|
---|
985 | // the command line, set the options.
|
---|
986 | //
|
---|
987 | SAXParser* parser = new SAXParser;
|
---|
988 | parser->setValidationScheme(valScheme);
|
---|
989 | parser->setDoNamespaces(doNamespaces);
|
---|
990 | parser->setDoSchema(doSchema);
|
---|
991 | parser->setValidationSchemaFullChecking(schemaFullChecking);
|
---|
992 |
|
---|
993 |
|
---|
994 | //
|
---|
995 | // Create our SAX handler object and install it on the parser, as the
|
---|
996 | // document and error handler. We are responsible for cleaning them
|
---|
997 | // up, but since its just stack based here, there's nothing special
|
---|
998 | // to do.
|
---|
999 | //
|
---|
1000 | ViewCellsParseHandlers handler(objects, bconverter);
|
---|
1001 | parser->setDocumentHandler(&handler);
|
---|
1002 | parser->setErrorHandler(&handler);
|
---|
1003 |
|
---|
1004 | unsigned long duration;
|
---|
1005 | int errorCount = 0;
|
---|
1006 | // create a faux scope so that 'src' destructor is called before
|
---|
1007 | // XMLPlatformUtils::Terminate
|
---|
1008 | {
|
---|
1009 | //
|
---|
1010 | // Kick off the parse and catch any exceptions. Create a standard
|
---|
1011 | // input input source and tell the parser to parse from that.
|
---|
1012 | //
|
---|
1013 | // StdInInputSource src;
|
---|
1014 | try
|
---|
1015 | {
|
---|
1016 | const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
1017 | #if USE_GZLIB
|
---|
1018 | XMLCh *myFilePath = XMLString::transcode(filename.c_str());
|
---|
1019 |
|
---|
1020 | GzFileInputSource isource(myFilePath);
|
---|
1021 | parser->parse(isource);
|
---|
1022 |
|
---|
1023 | #else
|
---|
1024 | parser->parse(filename.c_str());
|
---|
1025 | #endif
|
---|
1026 |
|
---|
1027 | const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
1028 | duration = endMillis - startMillis;
|
---|
1029 | errorCount = parser->getErrorCount();
|
---|
1030 | }
|
---|
1031 | catch (const OutOfMemoryException&)
|
---|
1032 | {
|
---|
1033 | XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
|
---|
1034 | errorCount = 2;
|
---|
1035 | return false;
|
---|
1036 | }
|
---|
1037 | catch (const XMLException& e)
|
---|
1038 | {
|
---|
1039 | XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
|
---|
1040 | << StrX(e.getMessage())
|
---|
1041 | << "\n" << XERCES_STD_QUALIFIER endl;
|
---|
1042 | errorCount = 1;
|
---|
1043 | return false;
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 |
|
---|
1047 | // Print out the stats that we collected and time taken
|
---|
1048 | if (!errorCount) {
|
---|
1049 | XERCES_STD_QUALIFIER cerr << filename << ": " << duration << " ms ("
|
---|
1050 | << handler.GetElementCount() << " elems, "
|
---|
1051 | << handler.GetAttrCount() << " attrs, "
|
---|
1052 | << handler.GetSpaceCount() << " spaces, "
|
---|
1053 | << handler.GetCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
|
---|
1054 | }
|
---|
1055 | }
|
---|
1056 |
|
---|
1057 | //
|
---|
1058 | // Delete the parser itself. Must be done prior to calling Terminate, below.
|
---|
1059 | //
|
---|
1060 | delete parser;
|
---|
1061 |
|
---|
1062 | XMLPlatformUtils::Terminate();
|
---|
1063 |
|
---|
1064 | //-- assign new view cells manager
|
---|
1065 | *viewCells = handler.mViewCellsManager;
|
---|
1066 |
|
---|
1067 | if (errorCount > 0)
|
---|
1068 | return false;
|
---|
1069 | else
|
---|
1070 | return true;
|
---|
1071 | }
|
---|
1072 |
|
---|
1073 | } |
---|