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 |
|
---|
34 |
|
---|
35 | namespace GtpVisibilityPreprocessor {
|
---|
36 |
|
---|
37 |
|
---|
38 | // ---------------------------------------------------------------------------
|
---|
39 | // Local data
|
---|
40 | //
|
---|
41 | // doNamespaces
|
---|
42 | // Indicates whether namespace processing should be enabled or not.
|
---|
43 | // The default is no, but -n overrides that.
|
---|
44 | //
|
---|
45 | // doSchema
|
---|
46 | // Indicates whether schema processing should be enabled or not.
|
---|
47 | // The default is no, but -s overrides that.
|
---|
48 | //
|
---|
49 | // schemaFullChecking
|
---|
50 | // Indicates whether full schema constraint checking should be enabled or not.
|
---|
51 | // The default is no, but -s overrides that.
|
---|
52 | //
|
---|
53 | // valScheme
|
---|
54 | // Indicates what validation scheme to use. It defaults to 'auto', but
|
---|
55 | // can be set via the -v= command.
|
---|
56 | // ---------------------------------------------------------------------------
|
---|
57 | static bool doNamespaces = false;
|
---|
58 | static bool doSchema = false;
|
---|
59 | static bool schemaFullChecking = false;
|
---|
60 | static SAXParser::ValSchemes valScheme = SAXParser::Val_Auto;
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 |
|
---|
65 |
|
---|
66 | // ---------------------------------------------------------------------------
|
---|
67 | // StdInParseHandlers: Constructors and Destructor
|
---|
68 | // ---------------------------------------------------------------------------
|
---|
69 | ViewCellsParseHandlers::ViewCellsParseHandlers(ObjectContainer *objects,
|
---|
70 | BoundingBoxConverter *bconverter):
|
---|
71 | mElementCount(0)
|
---|
72 | , mAttrCount(0)
|
---|
73 | , mCharacterCount(0)
|
---|
74 | , mSpaceCount(0)
|
---|
75 | , mViewCellsManager(NULL)
|
---|
76 | , mVspBspTree(NULL)
|
---|
77 | , mBspTree(NULL)
|
---|
78 | , mViewCellsTree(NULL)
|
---|
79 | , mParseViewCells(true)
|
---|
80 | , mCurrentViewCell(NULL)
|
---|
81 | , mCurrentBspNode(NULL)
|
---|
82 | , mObjects(objects)
|
---|
83 | , mBoundingBoxConverter(bconverter)
|
---|
84 | {
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | ViewCellsParseHandlers::~ViewCellsParseHandlers()
|
---|
89 | {
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | // ---------------------------------------------------------------------------
|
---|
94 | // StdInParseHandlers: Implementation of the SAX DocumentHandler interface
|
---|
95 | // ---------------------------------------------------------------------------
|
---|
96 |
|
---|
97 |
|
---|
98 | void ViewCellsParseHandlers::endElement(const XMLCh* const name)
|
---|
99 | {
|
---|
100 | StrX lname(name);
|
---|
101 | string element(lname.LocalForm());
|
---|
102 |
|
---|
103 | if (element == "ViewCells")
|
---|
104 | EndViewCells();
|
---|
105 |
|
---|
106 | if (element == "BoundingBoxes")
|
---|
107 | EndBoundingBoxes();
|
---|
108 |
|
---|
109 | // inside the view cell description
|
---|
110 | if (mParseViewCells)
|
---|
111 | {
|
---|
112 | if (element == "Interior")
|
---|
113 | EndViewCellInterior();
|
---|
114 | }
|
---|
115 | else
|
---|
116 | {
|
---|
117 | if (element == "Interior")
|
---|
118 | EndBspInterior();
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | void ViewCellsParseHandlers::EndBspInterior()
|
---|
124 | {
|
---|
125 | // go one up in the tree
|
---|
126 | if (mCurrentBspNode->GetParent())
|
---|
127 | { Debug << "]";
|
---|
128 | mCurrentBspNode = mCurrentBspNode->GetParent();
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | void ViewCellsParseHandlers::EndViewCellInterior()
|
---|
134 | {
|
---|
135 | // go one up in the tree
|
---|
136 | if (mCurrentViewCell->GetParent())
|
---|
137 | { Debug << "]";
|
---|
138 | mCurrentViewCell = mCurrentViewCell->GetParent();
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | inline static bool vlt(ViewCell *v1, ViewCell *v2)
|
---|
144 | {
|
---|
145 | return v1->mId < v2->mId;
|
---|
146 | }
|
---|
147 |
|
---|
148 |
|
---|
149 | void ViewCellsParseHandlers::EndViewCells()
|
---|
150 | {
|
---|
151 | // sort view cells to help associating view cells according to their id
|
---|
152 | stable_sort(mViewCells.begin(), mViewCells.end(), vlt);
|
---|
153 | mParseViewCells = false;
|
---|
154 | }
|
---|
155 |
|
---|
156 |
|
---|
157 | void ViewCellsParseHandlers::EndBoundingBoxes()
|
---|
158 | {
|
---|
159 | // all bounding boxes gathered in this step =>
|
---|
160 | // associate object ids with bounding boxes
|
---|
161 | long startTime = GetTime();
|
---|
162 |
|
---|
163 | if (mBoundingBoxConverter)
|
---|
164 | mBoundingBoxConverter->IdentifyObjects(mIBoundingBoxes, *mObjects);
|
---|
165 |
|
---|
166 | Debug << "\nconverted bounding boxes to objects in "
|
---|
167 | << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 | void ViewCellsParseHandlers::StartHierarchy(AttributeList& attributes)
|
---|
172 | {
|
---|
173 | int len = attributes.getLength();
|
---|
174 |
|
---|
175 | for (int i = 0; i < len; ++ i)
|
---|
176 | {
|
---|
177 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
178 |
|
---|
179 | if (attrName == "name")
|
---|
180 | {
|
---|
181 | StrX attrValue(attributes.getValue(i));
|
---|
182 |
|
---|
183 | const char *ptr = attrValue.LocalForm();
|
---|
184 |
|
---|
185 | //-- the view cells manager is created here
|
---|
186 | CreateViewCellsManager(ptr);
|
---|
187 | }
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | void ViewCellsParseHandlers::startBspElement(string element,
|
---|
193 | AttributeList& attributes)
|
---|
194 | {
|
---|
195 | if (element == "Interior")
|
---|
196 | {
|
---|
197 | Debug << "[";
|
---|
198 | StartBspInterior(attributes);
|
---|
199 | }
|
---|
200 |
|
---|
201 | if (element == "Leaf")
|
---|
202 | {
|
---|
203 | Debug << "l";
|
---|
204 | StartBspLeaf(attributes);
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | void ViewCellsParseHandlers::startElement(const XMLCh* const name,
|
---|
210 | AttributeList& attributes)
|
---|
211 | {
|
---|
212 | StrX lname(name);
|
---|
213 | string element(lname.LocalForm());
|
---|
214 |
|
---|
215 |
|
---|
216 | if (element == "ViewCells")
|
---|
217 | {
|
---|
218 | Debug << "parsing view cells" << endl;
|
---|
219 | mParseViewCells = true;
|
---|
220 | }
|
---|
221 |
|
---|
222 | // decides about the view cell hierarchy
|
---|
223 | if (element == "HierarchyType")
|
---|
224 | //if (element == "Hierarchy")
|
---|
225 | {
|
---|
226 | //Debug << "parsing spatial hierarchy" << endl;
|
---|
227 | //mParseViewCells = false;
|
---|
228 | StartHierarchy(attributes);
|
---|
229 | }
|
---|
230 |
|
---|
231 | // decides the used view cell hierarchy
|
---|
232 | if (element == "ViewSpaceBox")
|
---|
233 | {
|
---|
234 | Debug << "v";
|
---|
235 | StartViewSpaceBox(attributes);
|
---|
236 | }
|
---|
237 |
|
---|
238 | // decides the used view cell hierarchy
|
---|
239 | if (element == "BoundingBox")
|
---|
240 | {
|
---|
241 | Debug << "b";
|
---|
242 | StartBoundingBox(attributes);
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | if (!mParseViewCells)
|
---|
247 | {
|
---|
248 | //-- use different methods for the given view cell hierarchy types
|
---|
249 | if (mViewCellsManager)
|
---|
250 | {
|
---|
251 | switch (mViewCellsManager->GetType())
|
---|
252 | {
|
---|
253 | case ViewCellsManager::BSP:
|
---|
254 | case ViewCellsManager::VSP_BSP:
|
---|
255 | startBspElement(element, attributes);
|
---|
256 | break;
|
---|
257 |
|
---|
258 | default:
|
---|
259 | Debug << "not implemented" << endl;
|
---|
260 | break;
|
---|
261 | }
|
---|
262 | }
|
---|
263 | }
|
---|
264 | else
|
---|
265 | {
|
---|
266 | // interiors + leaves interpreted view cells else
|
---|
267 | if (element == "Interior")
|
---|
268 | {
|
---|
269 | Debug << "[";
|
---|
270 | StartViewCellInterior(attributes);
|
---|
271 | }
|
---|
272 |
|
---|
273 | if (element == "Leaf")
|
---|
274 | {
|
---|
275 | Debug << "l";
|
---|
276 | StartViewCellLeaf(attributes);
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | ++ mElementCount;
|
---|
281 | mAttrCount += attributes.getLength();
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 | inline bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
286 | {
|
---|
287 | return obj1->mId < obj2->mId;
|
---|
288 | }
|
---|
289 |
|
---|
290 |
|
---|
291 | void ViewCellsParseHandlers::StartViewCell(ViewCell *viewCell, AttributeList& attributes)
|
---|
292 | {
|
---|
293 | int len = attributes.getLength();
|
---|
294 | vector<int> objIndices;
|
---|
295 |
|
---|
296 | for (int i = 0; i < len; ++ i)
|
---|
297 | {
|
---|
298 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
299 |
|
---|
300 | if (attrName == "pvs")
|
---|
301 | {
|
---|
302 | StrX attrValue(attributes.getValue(i));
|
---|
303 |
|
---|
304 | // handle coordIndex
|
---|
305 | objIndices.clear();
|
---|
306 | const char *ptr = attrValue.LocalForm();
|
---|
307 | char *endptr;
|
---|
308 |
|
---|
309 | while (1)
|
---|
310 | {
|
---|
311 | int index = strtol(ptr, &endptr, 10);
|
---|
312 |
|
---|
313 | if (ptr == endptr)
|
---|
314 | break;
|
---|
315 |
|
---|
316 | objIndices.push_back(index);
|
---|
317 |
|
---|
318 | ptr = endptr;
|
---|
319 | }
|
---|
320 |
|
---|
321 | //TODO: find objects and add them to pvs
|
---|
322 | // TODO: get view cell with specified id
|
---|
323 | MeshInstance dummyInst(NULL);
|
---|
324 |
|
---|
325 | vector<int>::const_iterator it, it_end = objIndices.end();
|
---|
326 | for (it = objIndices.begin(); it != it_end; ++ it)
|
---|
327 | {
|
---|
328 | const int objId = *it;
|
---|
329 | dummyInst.SetId(objId);
|
---|
330 |
|
---|
331 | ObjectContainer::iterator oit =
|
---|
332 | lower_bound(mObjects->begin(), mObjects->end(), (Intersectable *)&dummyInst, ilt);
|
---|
333 |
|
---|
334 |
|
---|
335 | if ((oit != mObjects->end()) && ((*oit)->GetId() == objId))
|
---|
336 | {
|
---|
337 | // $$JB we should store a float a per object which corresponds
|
---|
338 | // to sumof pdfs, i.e. its relative visibility
|
---|
339 | // temporarily set to 1.0f
|
---|
340 | viewCell->GetPvs().AddSample(*oit, 1.0f);
|
---|
341 | }
|
---|
342 | else
|
---|
343 | {
|
---|
344 | Debug << "error: object with id " << objId << " does not exist" << endl;
|
---|
345 | }
|
---|
346 | }
|
---|
347 | }
|
---|
348 | else if (attrName == "id")
|
---|
349 | {
|
---|
350 | StrX attrValue(attributes.getValue(i));
|
---|
351 |
|
---|
352 | const char *ptr = attrValue.LocalForm();
|
---|
353 | char *endptr = NULL;
|
---|
354 | const int id = strtol(ptr, &endptr, 10);
|
---|
355 |
|
---|
356 | viewCell->SetId(id);
|
---|
357 | }
|
---|
358 | /*else if (attrName == "active")
|
---|
359 | {
|
---|
360 | StrX attrValue(attributes.getValue(i));
|
---|
361 |
|
---|
362 | const char *ptr = attrValue.LocalForm();
|
---|
363 | char *endptr = NULL;
|
---|
364 | const bool isActive = (bool)strtol(ptr, &endptr, 10);
|
---|
365 |
|
---|
366 | if (isActive)
|
---|
367 | viewCell->SetActive();
|
---|
368 | }*/
|
---|
369 | else if (attrName == "mergecost")
|
---|
370 | {
|
---|
371 | StrX attrValue(attributes.getValue(i));
|
---|
372 |
|
---|
373 | const char *ptr = attrValue.LocalForm();
|
---|
374 | char *endptr = NULL;
|
---|
375 | const float cost = (float)strtod(ptr, &endptr);
|
---|
376 |
|
---|
377 | viewCell->SetMergeCost(cost);
|
---|
378 | }
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 | void ViewCellsParseHandlers::StartViewSpaceBox(AttributeList& attributes)
|
---|
384 | {
|
---|
385 | int len = attributes.getLength();
|
---|
386 |
|
---|
387 | Vector3 bmin, bmax;
|
---|
388 |
|
---|
389 | for (int i = 0; i < len; ++ i)
|
---|
390 | {
|
---|
391 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
392 | StrX attrValue(attributes.getValue(i));
|
---|
393 | const char *ptr = attrValue.LocalForm();
|
---|
394 |
|
---|
395 | if (attrName == "min")
|
---|
396 | {
|
---|
397 | sscanf(ptr, "%f %f %f",
|
---|
398 | &bmin.x, &bmin.y, &bmin.z);
|
---|
399 | }
|
---|
400 | else if (attrName == "max")
|
---|
401 | {
|
---|
402 | sscanf(ptr, "%f %f %f",
|
---|
403 | &bmax.x, &bmax.y, &bmax.z);
|
---|
404 | }
|
---|
405 | }
|
---|
406 |
|
---|
407 | mViewSpaceBox = AxisAlignedBox3(bmin, bmax);
|
---|
408 |
|
---|
409 | Debug << "\nview space box: " << mViewSpaceBox << endl;
|
---|
410 | }
|
---|
411 |
|
---|
412 |
|
---|
413 | void ViewCellsParseHandlers::StartBoundingBox(AttributeList& attributes)
|
---|
414 | {
|
---|
415 | int len = attributes.getLength();
|
---|
416 |
|
---|
417 | Vector3 bmin, bmax;
|
---|
418 | int id;
|
---|
419 |
|
---|
420 | for (int i = 0; i < len; ++ i)
|
---|
421 | {
|
---|
422 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
423 | StrX attrValue(attributes.getValue(i));
|
---|
424 | const char *ptr = attrValue.LocalForm();
|
---|
425 |
|
---|
426 |
|
---|
427 | if (attrName == "id")
|
---|
428 | {
|
---|
429 | sscanf(ptr, "%d", &id);
|
---|
430 | }
|
---|
431 |
|
---|
432 | if (attrName == "min")
|
---|
433 | {
|
---|
434 | sscanf(ptr, "%f %f %f",
|
---|
435 | &bmin.x, &bmin.y, &bmin.z);
|
---|
436 | }
|
---|
437 | else if (attrName == "max")
|
---|
438 | {
|
---|
439 | sscanf(ptr, "%f %f %f",
|
---|
440 | &bmax.x, &bmax.y, &bmax.z);
|
---|
441 | }
|
---|
442 | }
|
---|
443 |
|
---|
444 | AxisAlignedBox3 box(bmin, bmax);
|
---|
445 | mIBoundingBoxes.push_back(IndexedBoundingBox(id, box));
|
---|
446 |
|
---|
447 | //Debug << "bbox: " << box << endl;
|
---|
448 | }
|
---|
449 |
|
---|
450 |
|
---|
451 | void ViewCellsParseHandlers::StartBspLeaf(AttributeList& attributes)
|
---|
452 | {
|
---|
453 | BspLeaf * leaf =
|
---|
454 | new BspLeaf(dynamic_cast<BspInterior *>(mCurrentBspNode), NULL);
|
---|
455 |
|
---|
456 | if (mCurrentBspNode) // replace front or (if not NULL) back child
|
---|
457 | {
|
---|
458 | dynamic_cast<BspInterior *>(mCurrentBspNode)->ReplaceChildLink(NULL, leaf);
|
---|
459 | }
|
---|
460 | else
|
---|
461 | {
|
---|
462 | if (mViewCellsManager->GetType() == ViewCellsManager::BSP)
|
---|
463 | {
|
---|
464 | mBspTree->mRoot = leaf;
|
---|
465 | }
|
---|
466 | else if (mViewCellsManager->GetType() == ViewCellsManager::VSP_BSP)
|
---|
467 | {
|
---|
468 | mVspBspTree->mRoot = leaf;
|
---|
469 | }
|
---|
470 | }
|
---|
471 |
|
---|
472 | //-- find associated view cell
|
---|
473 | int viewCellId;
|
---|
474 |
|
---|
475 | int len = attributes.getLength();
|
---|
476 |
|
---|
477 | for (int i = 0; i < len; ++ i)
|
---|
478 | {
|
---|
479 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
480 | StrX attrValue(attributes.getValue(i));
|
---|
481 |
|
---|
482 | const char *ptr = attrValue.LocalForm();
|
---|
483 | char *endptr = NULL;
|
---|
484 |
|
---|
485 | if (attrName == "viewCellId")
|
---|
486 | {
|
---|
487 | viewCellId = strtol(ptr, &endptr, 10);
|
---|
488 | }
|
---|
489 | }
|
---|
490 |
|
---|
491 |
|
---|
492 | if (viewCellId >= 0) // valid view cell
|
---|
493 | {
|
---|
494 | // TODO: get view cell with specified id
|
---|
495 | ViewCellInterior dummyVc;
|
---|
496 | dummyVc.SetId(viewCellId);
|
---|
497 |
|
---|
498 | ViewCellContainer::iterator vit =
|
---|
499 | lower_bound(mViewCells.begin(), mViewCells.end(), &dummyVc, vlt);
|
---|
500 |
|
---|
501 | BspViewCell *viewCell = dynamic_cast<BspViewCell *>(*vit);
|
---|
502 |
|
---|
503 |
|
---|
504 | if (viewCell->GetId() == viewCellId)
|
---|
505 | {
|
---|
506 | leaf->SetViewCell(viewCell);
|
---|
507 | viewCell->mLeaf = leaf;
|
---|
508 | }
|
---|
509 | else
|
---|
510 | {
|
---|
511 | Debug << "error: view cell does not exist" << endl;
|
---|
512 | }
|
---|
513 | }
|
---|
514 | else
|
---|
515 | {
|
---|
516 | // add to invalid view space
|
---|
517 | if (mViewCellsManager->GetType() == ViewCellsManager::VSP_BSP)
|
---|
518 | {
|
---|
519 | leaf->SetViewCell(mVspBspTree->GetOrCreateOutOfBoundsCell());
|
---|
520 | leaf->SetTreeValid(false);
|
---|
521 | mVspBspTree->PropagateUpValidity(leaf);
|
---|
522 | }
|
---|
523 | }
|
---|
524 | }
|
---|
525 |
|
---|
526 |
|
---|
527 | void ViewCellsParseHandlers::StartBspInterior(AttributeList& attributes)
|
---|
528 | {
|
---|
529 | Plane3 plane;
|
---|
530 | int len = attributes.getLength();
|
---|
531 |
|
---|
532 | for (int i = 0; i < len; ++ i)
|
---|
533 | {
|
---|
534 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
535 | StrX attrValue(attributes.getValue(i));
|
---|
536 | const char *ptr = attrValue.LocalForm();
|
---|
537 |
|
---|
538 | if (attrName == "plane")
|
---|
539 | {
|
---|
540 | sscanf(ptr, "%f %f %f %f",
|
---|
541 | &plane.mNormal.x, &plane.mNormal.y, &plane.mNormal.z, &plane.mD);
|
---|
542 | }
|
---|
543 | }
|
---|
544 |
|
---|
545 | BspInterior* interior = new BspInterior(plane);
|
---|
546 |
|
---|
547 | if (mCurrentBspNode) // replace NULL child of parent with current node
|
---|
548 | {
|
---|
549 | BspInterior *current = dynamic_cast<BspInterior *>(mCurrentBspNode);
|
---|
550 |
|
---|
551 | current->ReplaceChildLink(NULL, interior);
|
---|
552 | interior->SetParent(current);
|
---|
553 | }
|
---|
554 | else
|
---|
555 | {
|
---|
556 | if (mViewCellsManager->GetType() == ViewCellsManager::BSP)
|
---|
557 | {
|
---|
558 | mBspTree->mRoot = interior;
|
---|
559 | }
|
---|
560 | else
|
---|
561 | {
|
---|
562 | mVspBspTree->mRoot = interior;
|
---|
563 | }
|
---|
564 | }
|
---|
565 |
|
---|
566 | mCurrentBspNode = interior;
|
---|
567 | }
|
---|
568 |
|
---|
569 |
|
---|
570 | void ViewCellsParseHandlers::StartViewCellLeaf(AttributeList& attributes)
|
---|
571 | {
|
---|
572 | BspViewCell *viewCell = new BspViewCell();
|
---|
573 |
|
---|
574 | if (mCurrentViewCell) // replace front or (if not NULL) back child
|
---|
575 | {
|
---|
576 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(mCurrentViewCell);
|
---|
577 | interior->SetupChildLink(viewCell);
|
---|
578 | }
|
---|
579 | else // root
|
---|
580 | {
|
---|
581 | mViewCellsTree->SetRoot(viewCell);
|
---|
582 | }
|
---|
583 |
|
---|
584 | StartViewCell(viewCell, attributes);
|
---|
585 |
|
---|
586 | // collect leaves
|
---|
587 | mViewCells.push_back(viewCell);
|
---|
588 | }
|
---|
589 |
|
---|
590 |
|
---|
591 | void ViewCellsParseHandlers::StartViewCellInterior(AttributeList& attributes)
|
---|
592 | {
|
---|
593 | ViewCellInterior* interior = new ViewCellInterior();
|
---|
594 |
|
---|
595 | if (mCurrentViewCell) // replace NULL child of parent with current node
|
---|
596 | {
|
---|
597 | ViewCellInterior *current = dynamic_cast<ViewCellInterior *>(mCurrentViewCell);
|
---|
598 |
|
---|
599 | current->SetupChildLink(interior);
|
---|
600 | }
|
---|
601 | else
|
---|
602 | {
|
---|
603 | mViewCellsTree->SetRoot(interior);
|
---|
604 | }
|
---|
605 |
|
---|
606 | mCurrentViewCell = interior;
|
---|
607 |
|
---|
608 | StartViewCell(interior, attributes);
|
---|
609 | }
|
---|
610 |
|
---|
611 |
|
---|
612 |
|
---|
613 | void ViewCellsParseHandlers::CreateViewCellsManager(const char *name)
|
---|
614 | {
|
---|
615 | if (strcmp(name, "bspTree") == 0)
|
---|
616 | {
|
---|
617 | Debug << "view cell type: Bsp" << endl;
|
---|
618 |
|
---|
619 | mBspTree = new BspTree();
|
---|
620 | mBspTree->mBox = mViewSpaceBox;
|
---|
621 | //mCurrentBspNode = mBspTree->GetRoot();
|
---|
622 |
|
---|
623 | mViewCellsManager = new BspViewCellsManager(mBspTree);
|
---|
624 | }
|
---|
625 | else if (strcmp(name, "vspBspTree") == 0) //
|
---|
626 | {
|
---|
627 | Debug << "view cell type: VspBsp" << endl;
|
---|
628 |
|
---|
629 | mVspBspTree = new VspBspTree();
|
---|
630 | //mCurrentBspNode = mVspBspTree->GetRoot();
|
---|
631 | mViewCellsManager = new VspBspViewCellsManager(mVspBspTree);
|
---|
632 |
|
---|
633 | mVspBspTree->mBox = mViewSpaceBox;
|
---|
634 | }
|
---|
635 | else if (strcmp(name, "vspOspTree") == 0)
|
---|
636 | {
|
---|
637 | Debug << "view cell type: VspOsp" << endl;
|
---|
638 |
|
---|
639 | mVspTree = new VspTree();
|
---|
640 | mOspTree = new OspTree();
|
---|
641 |
|
---|
642 | //mCurrentBspNode = mVspBspTree->GetRoot();
|
---|
643 | mViewCellsManager = new VspOspViewCellsManager(mVspTree, mOspTree);
|
---|
644 |
|
---|
645 | mVspTree->mBoundingBox = mViewSpaceBox;
|
---|
646 | }
|
---|
647 |
|
---|
648 |
|
---|
649 | mViewCellsTree = mViewCellsManager->GetViewCellsTree();
|
---|
650 | mViewCellsManager->SetViewSpaceBox(mViewSpaceBox);
|
---|
651 | }
|
---|
652 |
|
---|
653 |
|
---|
654 | void ViewCellsParseHandlers::characters(const XMLCh* const chars,
|
---|
655 | const unsigned int length)
|
---|
656 | {
|
---|
657 | mCharacterCount += length;
|
---|
658 | }
|
---|
659 |
|
---|
660 |
|
---|
661 | void ViewCellsParseHandlers::ignorableWhitespace(const XMLCh* const chars,
|
---|
662 | const unsigned int length)
|
---|
663 | {
|
---|
664 | mSpaceCount += length;
|
---|
665 | }
|
---|
666 |
|
---|
667 |
|
---|
668 | void ViewCellsParseHandlers::resetDocument()
|
---|
669 | {
|
---|
670 | mAttrCount = 0;
|
---|
671 | mCharacterCount = 0;
|
---|
672 | mElementCount = 0;
|
---|
673 | mSpaceCount = 0;
|
---|
674 | }
|
---|
675 |
|
---|
676 |
|
---|
677 | // ---------------------------------------------------------------------------
|
---|
678 | // StdInParseHandlers: Overrides of the SAX ErrorHandler interface
|
---|
679 | // ---------------------------------------------------------------------------
|
---|
680 | void
|
---|
681 | ViewCellsParseHandlers::error(const SAXParseException& e)
|
---|
682 | {
|
---|
683 | XERCES_STD_QUALIFIER cerr << "\nError at (file " << StrX(e.getSystemId())
|
---|
684 | << ", line " << e.getLineNumber()
|
---|
685 | << ", char " << e.getColumnNumber()
|
---|
686 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
687 | }
|
---|
688 |
|
---|
689 | void
|
---|
690 | ViewCellsParseHandlers::fatalError(const SAXParseException& e)
|
---|
691 | {
|
---|
692 | XERCES_STD_QUALIFIER cerr << "\nFatal Error at (file " << StrX(e.getSystemId())
|
---|
693 | << ", line " << e.getLineNumber()
|
---|
694 | << ", char " << e.getColumnNumber()
|
---|
695 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
696 | }
|
---|
697 |
|
---|
698 | void
|
---|
699 | ViewCellsParseHandlers::warning(const SAXParseException& e)
|
---|
700 | {
|
---|
701 | XERCES_STD_QUALIFIER cerr << "\nWarning at (file " << StrX(e.getSystemId())
|
---|
702 | << ", line " << e.getLineNumber()
|
---|
703 | << ", char " << e.getColumnNumber()
|
---|
704 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
705 | }
|
---|
706 |
|
---|
707 |
|
---|
708 | bool ViewCellsParser::ParseFile(const string filename,
|
---|
709 | ViewCellsManager **viewCells,
|
---|
710 | ObjectContainer *objects,
|
---|
711 | BoundingBoxConverter *bconverter)
|
---|
712 | {
|
---|
713 | // Initialize the XML4C system
|
---|
714 | try {
|
---|
715 | XMLPlatformUtils::Initialize();
|
---|
716 | }
|
---|
717 |
|
---|
718 | catch (const XMLException& toCatch)
|
---|
719 | {
|
---|
720 | XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
|
---|
721 | << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
722 | return false;
|
---|
723 | }
|
---|
724 |
|
---|
725 | //
|
---|
726 | // Create a SAX parser object. Then, according to what we were told on
|
---|
727 | // the command line, set the options.
|
---|
728 | //
|
---|
729 | SAXParser* parser = new SAXParser;
|
---|
730 | parser->setValidationScheme(valScheme);
|
---|
731 | parser->setDoNamespaces(doNamespaces);
|
---|
732 | parser->setDoSchema(doSchema);
|
---|
733 | parser->setValidationSchemaFullChecking(schemaFullChecking);
|
---|
734 |
|
---|
735 |
|
---|
736 | //
|
---|
737 | // Create our SAX handler object and install it on the parser, as the
|
---|
738 | // document and error handler. We are responsible for cleaning them
|
---|
739 | // up, but since its just stack based here, there's nothing special
|
---|
740 | // to do.
|
---|
741 | //
|
---|
742 | ViewCellsParseHandlers handler(objects, bconverter);
|
---|
743 | parser->setDocumentHandler(&handler);
|
---|
744 | parser->setErrorHandler(&handler);
|
---|
745 |
|
---|
746 | unsigned long duration;
|
---|
747 | int errorCount = 0;
|
---|
748 | // create a faux scope so that 'src' destructor is called before
|
---|
749 | // XMLPlatformUtils::Terminate
|
---|
750 | {
|
---|
751 | //
|
---|
752 | // Kick off the parse and catch any exceptions. Create a standard
|
---|
753 | // input input source and tell the parser to parse from that.
|
---|
754 | //
|
---|
755 | // StdInInputSource src;
|
---|
756 | try
|
---|
757 | {
|
---|
758 | const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
759 | #if ZIPPED_VIEWCELLS
|
---|
760 | XMLCh *myFilePath = XMLString::transcode(filename.c_str());
|
---|
761 |
|
---|
762 | GzFileInputSource isource(myFilePath);
|
---|
763 | parser->parse(isource);
|
---|
764 |
|
---|
765 | #else
|
---|
766 | parser->parse(filename.c_str());
|
---|
767 | #endif
|
---|
768 |
|
---|
769 | const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
770 | duration = endMillis - startMillis;
|
---|
771 | errorCount = parser->getErrorCount();
|
---|
772 | }
|
---|
773 | catch (const OutOfMemoryException&)
|
---|
774 | {
|
---|
775 | XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
|
---|
776 | errorCount = 2;
|
---|
777 | return false;
|
---|
778 | }
|
---|
779 | catch (const XMLException& e)
|
---|
780 | {
|
---|
781 | XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
|
---|
782 | << StrX(e.getMessage())
|
---|
783 | << "\n" << XERCES_STD_QUALIFIER endl;
|
---|
784 | errorCount = 1;
|
---|
785 | return false;
|
---|
786 | }
|
---|
787 |
|
---|
788 |
|
---|
789 | // Print out the stats that we collected and time taken
|
---|
790 | if (!errorCount) {
|
---|
791 | XERCES_STD_QUALIFIER cerr << filename << ": " << duration << " ms ("
|
---|
792 | << handler.GetElementCount() << " elems, "
|
---|
793 | << handler.GetAttrCount() << " attrs, "
|
---|
794 | << handler.GetSpaceCount() << " spaces, "
|
---|
795 | << handler.GetCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
|
---|
796 | }
|
---|
797 | }
|
---|
798 |
|
---|
799 | //
|
---|
800 | // Delete the parser itself. Must be done prior to calling Terminate, below.
|
---|
801 | //
|
---|
802 | delete parser;
|
---|
803 |
|
---|
804 | XMLPlatformUtils::Terminate();
|
---|
805 |
|
---|
806 | //-- assign new view cells manager
|
---|
807 | *viewCells = handler.mViewCellsManager;
|
---|
808 |
|
---|
809 | if (errorCount > 0)
|
---|
810 | return false;
|
---|
811 | else
|
---|
812 | return true;
|
---|
813 | }
|
---|
814 |
|
---|
815 | } |
---|