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

Revision 1284, 30.0 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        //-- use cell type according to the chosen method
314        switch (mObjectSpaceHierarchyType)
315        {
316                case OSP:
317                        StartOspElement(element, attributes);
318                        break;
319                case BVH:
320                        StartBvhElement(element, attributes);
321                        break;
322                default:
323                        Debug << "not implemented" << endl;
324                        break;
325        }
326}
327
328
329void ViewCellsParseHandlers::StartViewCellHierarchyElement(const std::string &element,
330                                                                                                                   AttributeList& attributes)
331{
332        // interiors + leaves interpreted view cells else
333        if (element == "Interior")
334        {
335                cout << "[";
336                StartViewCellInterior(attributes);
337        }
338
339        if (element == "Leaf")
340        {
341                cout << "l";
342                StartViewCellLeaf(attributes);
343        }
344}
345
346
347void ViewCellsParseHandlers::startElement(const XMLCh* const name,
348                                                                                  AttributeList& attributes)
349{
350        StrX lname(name);
351        string element(lname.LocalForm());
352
353        if (element == "ViewCells")
354        {
355                cout << "\nparsing view cells" << endl;
356               
357                mCurrentState = PARSE_VIEWCELLS;
358
359                // create new view cells hierarchy
360                mViewCellsTree = new ViewCellsTree();
361        }
362
363        // decides about the view cells manager type
364        if (element == "Hierarchy")
365        {
366                cout << "parsing view cells manager type" << endl;
367                StartHierarchy(attributes);
368        }
369
370        // decides about the view cell hierarchy
371        if (element == "ViewSpaceHierarchy")
372        {               
373                cout << "\nparsing view space hierarchy" << endl;
374                mCurrentState = PARSE_VIEWSPACE_HIERARCHY;
375                StartViewSpaceHierarchy(attributes);
376        }
377
378        // decides about the view cell hierarchy
379        if (element == "ObjectSpaceHierarchy")
380        {
381                cout << "\nparsing object space hierarchy" << endl;
382                mCurrentState = PARSE_OBJECTSPACE_HIERARCHY;
383                StartObjectSpaceHierarchy(attributes);
384        }
385       
386        // decides the used view cell hierarchy
387        if (element == "BoundingBox")
388        {
389                cout << "b";
390                StartBoundingBox(attributes);
391        }
392
393        // parse view space hierarchy
394        switch (mCurrentState)
395        {
396        case PARSE_VIEWSPACE_HIERARCHY:
397                StartViewSpaceHierarchyElement(element, attributes);
398                break;
399        case PARSE_OBJECTSPACE_HIERARCHY:
400                StartObjectSpaceHierarchyElement(element, attributes);
401                break;
402        case PARSE_VIEWCELLS:
403                StartViewCellHierarchyElement(element, attributes);
404                break;
405        default:
406                break;
407        }
408       
409        ++ mElementCount;
410        mAttrCount += attributes.getLength();
411}
412
413
414void ViewCellsParseHandlers::ParseViewCellPvs(ViewCell *viewCell,
415                                                                                          AttributeList&  attributes)
416{
417        int len = attributes.getLength();
418        vector<int> objIndices;
419
420        for (int i = 0; i < len; ++ i)
421        {
422                string attrName(StrX(attributes.getName(i)).LocalForm());
423               
424                if (attrName == "pvs")
425                {
426                        StrX attrValue(attributes.getValue(i));
427                       
428                        // handle coordIndex
429                        objIndices.clear();
430                        const char *ptr = attrValue.LocalForm();
431                        char *endptr;
432                       
433                        while (1)
434                        {
435                                int index = strtol(ptr, &endptr, 10);
436
437                                if (ptr == endptr)
438                                        break;
439
440                                objIndices.push_back(index);
441
442                                ptr = endptr;
443                        }
444
445                        //TODO: find objects and add them to pvs
446                        // TODO: get view cell with specified id
447                        MeshInstance dummyInst(NULL);
448
449                        vector<int>::const_iterator it, it_end = objIndices.end();
450                        for (it = objIndices.begin(); it != it_end; ++ it)
451                        {
452                                const int objId = *it; 
453                                dummyInst.SetId(objId);
454
455                                ObjectContainer::iterator oit =
456                                        lower_bound(mObjects->begin(), mObjects->end(), (Intersectable *)&dummyInst, ilt);     
457                                                       
458                                if ((oit != mObjects->end()) && ((*oit)->GetId() == objId))
459                                {
460                                        // $$JB we should store a float a per object which corresponds
461                                        // to sumof pdfs, i.e. its relative visibility
462                                        // temporarily set to 1.0f
463                                        viewCell->GetPvs().AddSample(*oit, 1.0f);                               
464                                }
465                                else
466                                {
467                                        cout << "error: object with id " << objId << " does not exist" << endl;
468                                }
469                        }
470                }
471                else if (attrName == "id")
472                {
473                        StrX attrValue(attributes.getValue(i));
474                       
475                        const char *ptr = attrValue.LocalForm();
476                        char *endptr = NULL;
477                        const int id = strtol(ptr, &endptr, 10);
478
479                        viewCell->SetId(id);
480                }
481                /*else if (attrName == "active")
482                {
483                        StrX attrValue(attributes.getValue(i));
484                       
485                        const char *ptr = attrValue.LocalForm();
486                        char *endptr = NULL;
487                        const bool isActive = (bool)strtol(ptr, &endptr, 10);
488
489                        if (isActive)
490                                viewCell->SetActive();
491                }*/
492                else if (attrName == "mergecost")
493                {
494                        StrX attrValue(attributes.getValue(i));
495                       
496                        const char *ptr = attrValue.LocalForm();
497                        char *endptr = NULL;
498                        const float cost = (float)strtod(ptr, &endptr);
499
500                        viewCell->SetMergeCost(cost);
501                }
502        }
503}
504
505
506void ViewCellsParseHandlers::StartViewSpaceHierarchy(AttributeList& attributes)
507{
508        int len = attributes.getLength();
509
510        Vector3 bmin, bmax;
511
512        for (int i = 0; i < len; ++ i)
513        {
514                string attrName(StrX(attributes.getName(i)).LocalForm());
515                StrX attrValue(attributes.getValue(i));
516                const char *ptr = attrValue.LocalForm();
517
518                // hierarchy type
519                if (attrName == "type")
520                {
521                        if (strcmp(ptr, "bsp") == 0)
522                        {
523                                cout << "\nview space hierarchy: Bsp" << endl;
524                                mViewSpaceHierarchyType = BSP;
525                        }
526                        else if (strcmp(ptr, "vsp") == 0)
527                        {
528                                cout << "\nview space hierarchy: Vsp" << endl;
529                                mViewSpaceHierarchyType = VSP;
530                        }
531                }
532                else if (attrName == "min") // the view space extent
533                {
534                        sscanf(ptr, "%f %f %f",
535                                   &bmin.x, &bmin.y, &bmin.z);
536                }
537                else if (attrName == "max")
538                {
539                        sscanf(ptr, "%f %f %f",
540                                   &bmax.x, &bmax.y, &bmax.z);
541                }
542        }
543
544        mViewSpaceBox = AxisAlignedBox3(bmin, bmax);
545
546        // create the hierarchy based on this information
547        CreateViewSpaceHierarchy();
548}
549
550
551void ViewCellsParseHandlers::StartObjectSpaceHierarchy(AttributeList& attributes)
552{
553        int len = attributes.getLength();
554
555        Vector3 bmin, bmax;
556cout << "here3" << endl;
557        for (int i = 0; i < len; ++ i)
558        {
559                string attrName(StrX(attributes.getName(i)).LocalForm());
560                StrX attrValue(attributes.getValue(i));
561                const char *ptr = attrValue.LocalForm();
562
563                // hierarchy type
564                if (attrName == "type")
565                {
566                        if (strcmp(ptr, "osp") == 0)
567                        {
568                                cout << "\nobject space hierarchy: Osp" << endl;
569                                mHierarchyManager =
570                                        new HierarchyManager(mVspTree, HierarchyManager::KD_BASED_OBJ_SUBDIV);
571                        }
572                        else if (strcmp(ptr, "bvh") == 0)
573                        {
574                                cout << "\nobject space hierarchy: Bvh" << endl;
575                                mHierarchyManager =
576                                        new HierarchyManager(mVspTree, HierarchyManager::BV_BASED_OBJ_SUBDIV);
577                        }
578                }
579        }
580}
581
582
583
584void ViewCellsParseHandlers::StartBoundingBox(AttributeList& attributes)
585{
586        int len = attributes.getLength();
587
588        Vector3 bmin, bmax;
589        int id;
590
591        for (int i = 0; i < len; ++ i)
592        {
593                string attrName(StrX(attributes.getName(i)).LocalForm());
594                StrX attrValue(attributes.getValue(i));
595                const char *ptr = attrValue.LocalForm();
596
597
598                if (attrName == "id")
599                {
600                        sscanf(ptr, "%d", &id);
601                }
602                if (attrName == "min")
603                {
604                        sscanf(ptr, "%f %f %f", &bmin.x, &bmin.y, &bmin.z);
605                }
606                else if (attrName == "max")
607                {
608                        sscanf(ptr, "%f %f %f", &bmax.x, &bmax.y, &bmax.z);
609                }
610        }
611
612        AxisAlignedBox3 box(bmin, bmax);
613        mIBoundingBoxes.push_back(IndexedBoundingBox(id, box));
614        //cout << "bbox: " << box << endl;
615}
616
617
618void ViewCellsParseHandlers::StartBspLeaf(AttributeList& attributes)
619{
620        BspLeaf * leaf =
621                new BspLeaf(dynamic_cast<BspInterior *>(mCurrentBspNode), NULL);
622
623        if (mCurrentBspNode) // replace front or (if not NULL) back child
624        {
625                dynamic_cast<BspInterior *>(mCurrentBspNode)->ReplaceChildLink(NULL, leaf);
626        }
627        else
628        {
629                mVspBspTree->mRoot = leaf;
630        }
631
632        //-- find associated view cell
633
634        int viewCellId;
635       
636        int len = attributes.getLength();
637         
638        for (int i = 0; i < len; ++ i)
639        {
640                string attrName(StrX(attributes.getName(i)).LocalForm());
641                StrX attrValue(attributes.getValue(i));
642
643                const char *ptr = attrValue.LocalForm();
644                char *endptr = NULL;
645
646                if (attrName == "viewCellId")
647                {
648                        viewCellId = strtol(ptr, &endptr, 10);
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                        // create new view cell for bsp nodes
666                        leaf->SetViewCell(viewCell);
667                        viewCell->mLeaf = leaf;
668                }
669                else
670                {
671                        cout << "error: view cell does not exist" << endl;
672                }
673        }
674        else
675        {
676                // add to invalid view space
677                if (mViewSpaceHierarchyType== ViewCellsManager::VSP_BSP)
678                {
679                        leaf->SetViewCell(mVspBspTree->GetOrCreateOutOfBoundsCell());
680                        leaf->SetTreeValid(false);
681                        mVspBspTree->PropagateUpValidity(leaf);
682                }
683        }
684}
685
686
687void ViewCellsParseHandlers::StartBspInterior(AttributeList& attributes)
688{
689        Plane3 plane;
690        int len = attributes.getLength();
691
692        for (int i = 0; i < len; ++ i)
693        {
694                string attrName(StrX(attributes.getName(i)).LocalForm());
695                StrX attrValue(attributes.getValue(i));
696                const char *ptr = attrValue.LocalForm();
697
698                if (attrName == "plane")
699                {
700                        sscanf(ptr, "%f %f %f %f",
701                                   &plane.mNormal.x, &plane.mNormal.y, &plane.mNormal.z, &plane.mD);
702                }
703        }
704
705        BspInterior* interior = new BspInterior(plane);
706       
707        if (mCurrentBspNode) // replace NULL child of parent with current node
708        {
709                BspInterior *current = dynamic_cast<BspInterior *>(mCurrentBspNode);
710
711                current->ReplaceChildLink(NULL, interior);
712                interior->SetParent(current);
713        }
714        else
715        {
716                mVspBspTree->mRoot = interior;
717        }
718
719        mCurrentBspNode = interior;
720}
721
722
723void ViewCellsParseHandlers::StartViewCellLeaf(AttributeList& attributes)
724{
725        ViewCellLeaf *viewCell = new ViewCellLeaf();
726
727        if (mCurrentViewCell) // replace front or (if not NULL) back child
728        {
729                ViewCellInterior *interior =
730                        dynamic_cast<ViewCellInterior *>(mCurrentViewCell);
731                interior->SetupChildLink(viewCell);
732        }
733        else // root
734        {
735                mViewCellsTree->SetRoot(viewCell);
736        }
737
738        ParseViewCellPvs(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        cout << "here7"<<endl;
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        ParseViewCellPvs(interior, attributes);
763}
764
765
766void ViewCellsParseHandlers::CreateViewSpaceHierarchy()
767{
768        ViewCellContainer::iterator it, it_end = mViewCells.end();
769        Debug << endl;
770        for (it = mViewCells.begin(); it != it_end; ++ it)
771        {
772                Debug << (*it)->GetId() << " ";
773        }
774Debug << endl;
775        if (mViewSpaceHierarchyType == BSP)
776        {
777                cout << "hierarchy type: Bsp" << endl;
778
779                mVspBspTree = new VspBspTree();
780
781                // set view space box
782                mVspBspTree->mBox = mViewSpaceBox;
783
784                ViewCellContainer::iterator vit, vit_end = mViewCells.end();
785
786                // reset view cells using the current node type
787                for (vit = mViewCells.begin(); vit != vit_end; ++ vit)
788                {
789                        ViewCell *vc = *vit;
790
791                        BspViewCell *bspVc = new BspViewCell();
792                        bspVc->SetPvs(vc->GetPvs());
793                        bspVc->SetId(vc->GetId());
794
795                        if (vc->IsRoot())
796                        {
797                                mViewCellsTree->mRoot = bspVc;
798                        }
799                        else
800                        {
801                vc->GetParent()->SetupChildLink(bspVc);
802                                vc->GetParent()->RemoveChildLink(vc);
803                        }
804
805                        DEL_PTR(vc);
806                        (*vit) = bspVc;
807                }
808
809        }
810        else if (mViewSpaceHierarchyType == VSP)
811        {
812                cout << "hierarchy type: Vsp" << endl;
813
814                mVspTree = new VspTree();
815
816                // set view space box
817                mVspTree->mBoundingBox = mViewSpaceBox;
818
819                ViewCellContainer::iterator vit, vit_end = mViewCells.end();
820
821                // reset view cells using the current node type
822                for (vit = mViewCells.begin(); vit != vit_end; ++ vit)
823                {
824                        ViewCell *vc = *vit;
825
826                        VspViewCell *vspVc = new VspViewCell();
827                        vspVc->SetPvs(vc->GetPvs());
828                        vspVc->SetId(vc->GetId());
829
830                        if (vc->IsRoot())
831                        {
832                                mViewCellsTree->mRoot = vspVc;
833                        }
834                        else
835                        {
836                vc->GetParent()->SetupChildLink(vspVc);
837                                vc->GetParent()->RemoveChildLink(vc);
838                        }
839                       
840                        DEL_PTR(vc);
841                        (*vit) = vspVc;
842                }
843
844                // if object space hierarchy already constructed
845                if (mHierarchyManager)
846                {
847                        mHierarchyManager->mVspTree = mVspTree;
848                        mVspTree->mHierarchyManager = mHierarchyManager;
849                }
850        }
851        Debug << "************************" << endl;
852        it_end = mViewCells.end();
853        for (it = mViewCells.begin(); it != it_end; ++ it)
854        {
855                Debug << (*it)->GetId() << " ";
856        }
857        Debug << endl;
858        cout << "\nview space box: " << mViewSpaceBox << endl;
859}
860
861
862void ViewCellsParseHandlers::CreateViewCellsManager(/*const char *name*/)
863{
864        if (mViewSpaceHierarchyType == BSP) //
865        {
866                Debug << "creating view cells manager: VspBsp" << endl;
867
868                //mVspBspTree = new VspBspTree();
869                mViewCellsManager = new VspBspViewCellsManager(mViewCellsTree, mVspBspTree);
870        }
871        else if (mViewSpaceHierarchyType == VSP)
872        {
873                Debug << "creating view cells manager: VspOsp" << endl;
874
875                // hack
876                mViewCellsManager = new VspOspViewCellsManager(mViewCellsTree, mHierarchyManager);
877        }
878
879        mViewCellsManager->SetViewSpaceBox(mViewSpaceBox);
880}
881
882
883void ViewCellsParseHandlers::characters(const XMLCh* const chars,
884                                                                                const unsigned int length)
885{
886        mCharacterCount += length;
887}
888
889
890void ViewCellsParseHandlers::ignorableWhitespace(const XMLCh* const chars,
891                                                                                                 const unsigned int length)
892{
893        mSpaceCount += length;
894}
895
896
897void ViewCellsParseHandlers::resetDocument()
898{
899        mAttrCount = 0;
900        mCharacterCount = 0;
901        mElementCount = 0;
902        mSpaceCount = 0;
903}
904
905
906void ViewCellsParseHandlers::StartVspLeaf(AttributeList& attributes)
907{
908        VspLeaf * leaf =
909                new VspLeaf(dynamic_cast<VspInterior *>(mCurrentVspNode), NULL);
910
911        if (mCurrentVspNode) // replace front or (if not NULL) back child
912        {
913                dynamic_cast<VspInterior *>(mCurrentVspNode)->ReplaceChildLink(NULL, leaf);
914        }
915        else
916        {
917                mVspTree->mRoot = leaf;
918        }
919
920        //-- find associated view cell
921        int viewCellId;
922       
923        int len = attributes.getLength();
924         
925        for (int i = 0; i < len; ++ i)
926        {
927                string attrName(StrX(attributes.getName(i)).LocalForm());
928                StrX attrValue(attributes.getValue(i));
929
930                const char *ptr = attrValue.LocalForm();
931                char *endptr = NULL;
932
933                if (attrName == "viewCellId")
934                {
935                        viewCellId = strtol(ptr, &endptr, 10);
936                }
937        }
938       
939        if (viewCellId >= 0) // valid view cell
940        {
941                // TODO: get view cell with specified id
942                ViewCellInterior dummyVc;
943                dummyVc.SetId(viewCellId);
944
945                cout << "\nsearching view cell with id " << viewCellId << endl;
946
947                ViewCellContainer::iterator vit =
948                        lower_bound(mViewCells.begin(), mViewCells.end(), &dummyVc, vlt);
949                       
950                if (vit == mViewCells.end())
951                        cout << "error: view cell " << viewCellId << " not found" << endl;
952
953                VspViewCell *viewCell = dynamic_cast<VspViewCell *>(*vit);
954       
955                if (viewCell->GetId() == viewCellId)
956                {
957                        leaf->SetViewCell(viewCell);
958                        viewCell->mLeaf = leaf;
959                }
960                else
961                {
962                        cout << "error: view cell does not exist" << endl;
963                }
964        }
965        else
966        {
967                // add to invalid view space
968                leaf->SetViewCell(mVspTree->GetOrCreateOutOfBoundsCell());
969                leaf->SetTreeValid(false);
970                mVspTree->PropagateUpValidity(leaf);
971        }
972}
973
974
975void ViewCellsParseHandlers::StartVspInterior(AttributeList& attributes)
976{
977        AxisAlignedPlane plane;
978        int len = attributes.getLength();
979
980        for (int i = 0; i < len; ++ i)
981        {
982                string attrName(StrX(attributes.getName(i)).LocalForm());
983                StrX attrValue(attributes.getValue(i));
984                const char *ptr = attrValue.LocalForm();
985
986                if (attrName == "plane")
987                {
988                        sscanf(ptr, "%d %f", &plane.mAxis, &plane.mPosition);
989                }
990        }
991
992        VspInterior* interior = new VspInterior(plane);
993       
994        if (mCurrentVspNode) // replace NULL child of parent with current node
995        {
996                VspInterior *current = dynamic_cast<VspInterior *>(mCurrentVspNode);
997
998                current->ReplaceChildLink(NULL, interior);
999                interior->SetParent(current);
1000        }
1001        else
1002        {
1003                mVspTree->mRoot = interior;
1004        }
1005
1006        mCurrentVspNode = interior;
1007}
1008
1009
1010void ViewCellsParseHandlers::StartOspInterior(AttributeList& attributes)
1011{
1012        AxisAlignedPlane plane;
1013        int len = attributes.getLength();
1014
1015        for (int i = 0; i < len; ++ i)
1016        {
1017                string attrName(StrX(attributes.getName(i)).LocalForm());
1018                StrX attrValue(attributes.getValue(i));
1019                const char *ptr = attrValue.LocalForm();
1020
1021                if (attrName == "plane")
1022                {
1023                        sscanf(ptr, "%d %f",
1024                                   &plane.mAxis, &plane.mPosition);
1025                }
1026        }
1027
1028        KdInterior* interior = new KdInterior(NULL);
1029       
1030        interior->mAxis = plane.mAxis;
1031        interior->mPosition = plane.mPosition;
1032
1033        if (mCurrentKdNode) // replace NULL child of parent with current node
1034        {
1035                KdInterior *parent = dynamic_cast<KdInterior *>(mCurrentKdNode);
1036                parent->ReplaceChildLink(NULL, interior);
1037                interior->mParent = parent;
1038        }
1039        else
1040        {
1041                mHierarchyManager->mOspTree->mRoot = interior;
1042        }
1043
1044        mCurrentKdNode = interior;
1045}
1046
1047
1048void ViewCellsParseHandlers::StartOspLeaf(AttributeList& attributes)
1049{
1050        KdLeaf * leaf =
1051                new KdLeaf(dynamic_cast<KdInterior *>(mCurrentKdNode), NULL);
1052
1053        if (mCurrentKdNode) // replace front or (if not NULL) back child
1054        {
1055                dynamic_cast<KdInterior *>(mCurrentKdNode)->ReplaceChildLink(NULL, leaf);
1056        }
1057        else
1058        {
1059                mHierarchyManager->mOspTree->mRoot = leaf;
1060        }
1061}
1062
1063
1064void ViewCellsParseHandlers::StartBvhLeaf(AttributeList& attributes)
1065{/*
1066        KdLeaf * leaf =
1067                new KdLeaf(dynamic_cast<KdInterior *>(mCurrentKdNode), NULL);
1068
1069        if (mCurrentKdNode) // replace front or (if not NULL) back child
1070        {
1071                dynamic_cast<KdInterior *>(mCurrentKdNode)->ReplaceChildLink(NULL, leaf);
1072        }
1073        else
1074        {
1075                mHierarchyManager->mBvHierarchy->mRoot = leaf;
1076        }*/
1077}
1078
1079
1080void ViewCellsParseHandlers::StartBvhInterior(AttributeList& attributes)
1081{/*
1082        AxisAlignedPlane plane;
1083        int len = attributes.getLength();
1084
1085        for (int i = 0; i < len; ++ i)
1086        {
1087                string attrName(StrX(attributes.getName(i)).LocalForm());
1088                StrX attrValue(attributes.getValue(i));
1089                const char *ptr = attrValue.LocalForm();
1090
1091                if (attrName == "plane")
1092                {
1093                        sscanf(ptr, "%d %f",
1094                                   &plane.mAxis, &plane.mPosition);
1095                }
1096        }
1097
1098        KdInterior* interior = new KdInterior(NULL);
1099       
1100        interior->mAxis = plane.mAxis;
1101        interior->mPosition = plane.mPosition;
1102
1103        if (mCurrentKdNode) // replace NULL child of parent with current node
1104        {
1105                KdInterior *parent = dynamic_cast<KdInterior *>(mCurrentKdNode);
1106                parent->ReplaceChildLink(NULL, interior);
1107                interior->mParent = parent;
1108        }
1109        else
1110        {
1111                mOspTree->mRoot = interior;
1112        }
1113
1114        mCurrentKdNode = interior;*/
1115}
1116
1117
1118
1119// ---------------------------------------------------------------------------
1120//  StdInParseHandlers: Overrides of the SAX ErrorHandler interface
1121// ---------------------------------------------------------------------------
1122
1123
1124void
1125ViewCellsParseHandlers::error(const SAXParseException& e)
1126{
1127  XERCES_STD_QUALIFIER cerr << "\nError at (file " << StrX(e.getSystemId())
1128                            << ", line " << e.getLineNumber()
1129                            << ", char " << e.getColumnNumber()
1130                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
1131}
1132
1133void
1134ViewCellsParseHandlers::fatalError(const SAXParseException& e)
1135{
1136  XERCES_STD_QUALIFIER cerr << "\nFatal Error at (file " << StrX(e.getSystemId())
1137                            << ", line " << e.getLineNumber()
1138                            << ", char " << e.getColumnNumber()
1139                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
1140}
1141
1142void
1143ViewCellsParseHandlers::warning(const SAXParseException& e)
1144{
1145  XERCES_STD_QUALIFIER cerr << "\nWarning at (file " << StrX(e.getSystemId())
1146                            << ", line " << e.getLineNumber()
1147                            << ", char " << e.getColumnNumber()
1148                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
1149}
1150
1151
1152bool ViewCellsParser::ParseViewCellsFile(const string filename,
1153                                                                                 ViewCellsManager **viewCells,
1154                                                                                 ObjectContainer *objects,
1155                                                                                 BoundingBoxConverter *bconverter)
1156{
1157  // Initialize the XML4C system
1158  try {
1159    XMLPlatformUtils::Initialize();
1160  }
1161 
1162  catch (const XMLException& toCatch)
1163    {
1164      XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
1165                                << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
1166      return false;
1167    }
1168 
1169  //
1170  //  Create a SAX parser object. Then, according to what we were told on
1171  //  the command line, set the options.
1172  //
1173  SAXParser* parser = new SAXParser;
1174  parser->setValidationScheme(valScheme);
1175  parser->setDoNamespaces(doNamespaces);
1176  parser->setDoSchema(doSchema);
1177  parser->setValidationSchemaFullChecking(schemaFullChecking);
1178 
1179
1180  //
1181  //  Create our SAX handler object and install it on the parser, as the
1182  //  document and error handler. We are responsible for cleaning them
1183  //  up, but since its just stack based here, there's nothing special
1184  //  to do.
1185  //
1186  ViewCellsParseHandlers handler(objects, bconverter);
1187  parser->setDocumentHandler(&handler);
1188  parser->setErrorHandler(&handler);
1189 
1190  unsigned long duration;
1191  int errorCount = 0;
1192  // create a faux scope so that 'src' destructor is called before
1193  // XMLPlatformUtils::Terminate
1194  {
1195    //
1196    //  Kick off the parse and catch any exceptions. Create a standard
1197    //  input input source and tell the parser to parse from that.
1198    //
1199    //    StdInInputSource src;
1200    try
1201        {
1202        const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
1203
1204#if USE_GZLIB
1205        XMLCh *myFilePath = XMLString::transcode(filename.c_str());
1206       
1207        GzFileInputSource isource(myFilePath);
1208        parser->parse(isource);
1209#else
1210        parser->parse(filename.c_str());
1211#endif
1212
1213        const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
1214        duration = endMillis - startMillis;
1215        errorCount = parser->getErrorCount();
1216      }
1217    catch (const OutOfMemoryException&)
1218      {
1219        XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
1220        errorCount = 2;
1221        return false;
1222      }
1223    catch (const XMLException& e)
1224      {
1225                        XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
1226                                  << StrX(e.getMessage())
1227                                  << "\n" << XERCES_STD_QUALIFIER endl;
1228                        errorCount = 1;
1229                        return false;
1230      }
1231
1232   
1233    // Print out the stats that we collected and time taken
1234    if (!errorCount) {
1235                XERCES_STD_QUALIFIER cerr << filename << ": " << duration << " ms ("
1236                                << handler.GetElementCount() << " elems, "
1237                                << handler.GetAttrCount() << " attrs, "
1238                                << handler.GetSpaceCount() << " spaces, "
1239                                << handler.GetCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
1240    }
1241  }
1242 
1243  //
1244  //  Delete the parser itself.  Must be done prior to calling Terminate, below.
1245  //
1246  delete parser;
1247 
1248  XMLPlatformUtils::Terminate();
1249 
1250  //-- assign new view cells manager
1251  *viewCells = handler.mViewCellsManager;
1252 
1253  if (errorCount > 0)
1254    return false;
1255  else
1256    return true;
1257}
1258
1259}
Note: See TracBrowser for help on using the repository browser.