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

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