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

Revision 1486, 33.4 KB checked in by mattausch, 18 years ago (diff)

worked on guided visibility sampling

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