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

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