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

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