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

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