source: GTP/trunk/Lib/Vis/Preprocessing/src/ViewCellsParser.cpp @ 1262

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