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

Revision 1221, 25.6 KB checked in by mattausch, 18 years ago (diff)

added intel ray tracing

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