source: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellsParser.cpp @ 590

Revision 590, 15.7 KB checked in by mattausch, 18 years ago (diff)

implemented some code for merge history loading

Line 
1// ---------------------------------------------------------------------------
2//  Includes for all the program files to see
3// ---------------------------------------------------------------------------
4#include <string.h>
5#include <stdlib.h>
6#include <iostream>
7using namespace std;
8#include <xercesc/util/PlatformUtils.hpp>
9
10// ---------------------------------------------------------------------------
11//  Includes
12// ---------------------------------------------------------------------------
13#include <xercesc/framework/StdInInputSource.hpp>
14#include <xercesc/parsers/SAXParser.hpp>
15#include <xercesc/util/OutOfMemoryException.hpp>
16
17// ---------------------------------------------------------------------------
18//  Includes
19// ---------------------------------------------------------------------------
20#include <xercesc/sax/AttributeList.hpp>
21#include <xercesc/sax/SAXParseException.hpp>
22#include <xercesc/sax/SAXException.hpp>
23
24#include "ViewCellsParser.h"
25
26#include "ViewCellsParserXerces.h"
27#include "Mesh.h"
28#include "VspBspTree.h"
29#include "ViewCellBsp.h"
30#include "VspKdTree.h"
31#include "ViewCellsManager.h"
32
33// ---------------------------------------------------------------------------
34//  Local data
35//
36//  doNamespaces
37//      Indicates whether namespace processing should be enabled or not.
38//      The default is no, but -n overrides that.
39//
40//  doSchema
41//      Indicates whether schema processing should be enabled or not.
42//      The default is no, but -s overrides that.
43//
44//  schemaFullChecking
45//      Indicates whether full schema constraint checking should be enabled or not.
46//      The default is no, but -s overrides that.
47//
48//  valScheme
49//      Indicates what validation scheme to use. It defaults to 'auto', but
50//      can be set via the -v= command.
51// ---------------------------------------------------------------------------
52static bool     doNamespaces       = false;
53static bool     doSchema           = false;
54static bool     schemaFullChecking = false;
55static SAXParser::ValSchemes    valScheme       = SAXParser::Val_Auto;
56
57
58
59
60
61// ---------------------------------------------------------------------------
62//  StdInParseHandlers: Constructors and Destructor
63// ---------------------------------------------------------------------------
64ViewCellsParseHandlers::ViewCellsParseHandlers(ObjectContainer *objects):
65  mElementCount(0)
66  , mAttrCount(0)
67  , mCharacterCount(0)
68  , mSpaceCount(0)
69  , mViewCellsManager(NULL)
70  , mVspBspTree(NULL)
71  , mBspTree(NULL)
72{
73        mObjects = objects;
74}
75
76
77ViewCellsParseHandlers::~ViewCellsParseHandlers()
78{
79}
80
81
82// ---------------------------------------------------------------------------
83//  StdInParseHandlers: Implementation of the SAX DocumentHandler interface
84// ---------------------------------------------------------------------------
85
86
87void ViewCellsParseHandlers::endElement(const XMLCh* const name)
88{
89  StrX lname(name);
90  string element(lname.LocalForm());
91  if (element == "Interior")
92          EndBspInterior();
93  if (element == "ViewCells")
94          EndViewCells();
95}
96
97
98void ViewCellsParseHandlers::EndBspInterior()
99{
100        // go one up in the tree
101        if (mCurrentBspNode->GetParent())
102        {       cout << "]";
103                mCurrentBspNode = mCurrentBspNode->GetParent();
104        }
105}
106
107
108inline bool vlt(ViewCell *v1, ViewCell *v2)
109{
110        return v1->mId < v2->mId;
111}
112
113
114void ViewCellsParseHandlers::EndViewCells()
115{
116        // sort view cells to help associating view cells according to their id
117        stable_sort(mViewCells.begin(), mViewCells.end(), vlt);
118}
119
120
121
122void ViewCellsParseHandlers::StartHierarchy(AttributeList&  attributes)
123{
124        int len = attributes.getLength();
125 
126        for (int i = 0; i < len; ++ i)
127        {
128                string attrName(StrX(attributes.getName(i)).LocalForm());
129               
130                if (attrName == "name")
131                {
132                        StrX attrValue(attributes.getValue(i));
133                       
134                        const char *ptr = attrValue.LocalForm();
135                       
136                        CreateViewCellsManager(ptr);
137                }
138        }
139}
140
141
142void ViewCellsParseHandlers::startBspElement(string element,
143                                                                                         AttributeList& attributes)
144{
145        if (element == "Interior")
146        {
147                cout << "[";
148                StartBspInterior(attributes);
149        }
150
151        if (element == "Leaf")
152        {
153                StartBspLeaf(attributes);
154        }
155}
156
157
158void ViewCellsParseHandlers::startElement(const XMLCh* const name,
159                                                                                  AttributeList& attributes)
160{
161        StrX lname(name);
162        string element(lname.LocalForm());
163
164        // decides the used view cell hierarchy
165        if (element == "Hierarchy")
166        {
167                cout << "h";
168                StartHierarchy(attributes);
169        }
170       
171        // decides the used view cell hierarchy
172        if (element == "ViewSpaceBox")
173        {
174                cout << "vsb";
175                StartViewSpaceBox(attributes);
176        }
177
178        if (element == "ViewCell")
179        {
180                cout << "v";
181                StartViewCell(attributes);
182        }
183
184        // use different methods for the given view cell hierarchy types
185        if (mViewCellsManager)
186        {
187                switch (mViewCellsManager->GetType())
188                {
189                case ViewCellsManager::BSP:
190                case ViewCellsManager::VSP_BSP:
191                        startBspElement(element, attributes);
192                        break;
193       
194                default:
195                        Debug << "not implemented" << endl;
196                        break;
197                }
198        }
199
200        ++ mElementCount;
201        mAttrCount += attributes.getLength();
202}
203
204
205inline bool ilt(Intersectable *obj1, Intersectable *obj2)
206{
207        return obj1->mId < obj2->mId;
208}
209
210
211void ViewCellsParseHandlers::StartViewCell(AttributeList&  attributes)
212{
213        int len = attributes.getLength();
214        vector<int> objIndices;
215 
216//hack!!
217       
218        ViewCell *viewCell = new ViewCellInterior();//mViewCellsManager->GenerateViewCell();
219        viewCell->mIsActive = true;
220
221        mViewCells.push_back(viewCell);
222       
223        for (int i = 0; i < len; ++ i)
224        {
225                string attrName(StrX(attributes.getName(i)).LocalForm());
226               
227                if (attrName == "pvs")
228                {
229                        StrX attrValue(attributes.getValue(i));
230                       
231                        // handle coordIndex
232                        objIndices.clear();
233                        const char *ptr = attrValue.LocalForm();
234                        char *endptr;
235                       
236                        while (1)
237                        {
238                                int index = strtol(ptr, &endptr, 10);
239
240                                if (ptr == endptr)
241                                        break;
242
243                                objIndices.push_back(index);
244
245                                ptr = endptr;
246                        }
247
248                        //TODO: find objects and add them to pvs
249                        // TODO: get view cell with specified id
250                        MeshInstance dummyInst(NULL);
251
252                        vector<int>::const_iterator it, it_end = objIndices.end();
253                        for (it = objIndices.begin(); it != it_end; ++ it)
254                        {
255                                const int objId = *it; 
256                                dummyInst.SetId(objId);
257
258                                ObjectContainer::iterator oit =
259                                  lower_bound(mObjects->begin(), mObjects->end(), &dummyInst, ilt);
260                               
261                                Intersectable *obj = *oit;
262                               
263                                if (obj->GetId() == objId)
264                                {
265                                  // $$JB we should store a float a per object which corresponds
266                                  // to sumof pdfs, i.e. its relative visibility
267                                  // temporarily set to 1.0f
268                                        viewCell->GetPvs().AddSample(obj, 1.0f);
269                                }
270                                else
271                                {
272                                        Debug << "error: object does not exist" << endl;
273                                }
274                        }
275                }
276                else if (attrName == "id")
277                {
278                        StrX attrValue(attributes.getValue(i));
279                       
280                        const char *ptr = attrValue.LocalForm();
281                        char *endptr = NULL;
282                        const int id = strtol(ptr, &endptr, 10);
283
284                        viewCell->SetId(id);
285                }
286        }
287}
288
289
290void ViewCellsParseHandlers::StartViewSpaceBox(AttributeList& attributes)
291{
292        int len = attributes.getLength();
293
294        Vector3 bmin, bmax;
295
296        for (int i = 0; i < len; ++ i)
297        {
298                string attrName(StrX(attributes.getName(i)).LocalForm());
299                StrX attrValue(attributes.getValue(i));
300                const char *ptr = attrValue.LocalForm();
301
302                if (attrName == "min")
303                {
304                        sscanf(ptr, "%f %f %f",
305                                   &bmin.x, &bmin.y, &bmin.z);
306                }
307                else if (attrName == "max")
308                {
309                        sscanf(ptr, "%f %f %f",
310                                   &bmax.x, &bmax.y, &bmax.z);
311                }
312        }
313
314        mViewSpaceBox = AxisAlignedBox3(bmin, bmax);
315
316        Debug << "view space box: " << mViewSpaceBox << endl;
317}
318
319
320void ViewCellsParseHandlers::StartBspLeaf(AttributeList& attributes)
321{
322        BspLeaf * leaf =
323                new BspLeaf(dynamic_cast<BspInterior *>(mCurrentBspNode), NULL);
324
325        if (mCurrentBspNode) // replace front or (if not NULL) back child
326        {
327                dynamic_cast<BspInterior *>(mCurrentBspNode)->ReplaceChildLink(NULL, leaf);
328        }
329        else
330        {
331                if (mViewCellsManager->GetType() == ViewCellsManager::BSP)
332                {
333                        mBspTree->mRoot = leaf;
334                }
335                else if (mViewCellsManager->GetType() == ViewCellsManager::VSP_BSP)
336                {
337                        mVspBspTree->mRoot = leaf;
338                }
339        }
340
341        //-- find associated view cell
342        int viewCellId;
343       
344        int len = attributes.getLength();
345         
346        for (int i = 0; i < len; ++ i)
347        {
348                string attrName(StrX(attributes.getName(i)).LocalForm());
349                StrX attrValue(attributes.getValue(i));
350
351                const char *ptr = attrValue.LocalForm();
352                char *endptr = NULL;
353
354                if (attrName == "viewCellId")
355                {
356                        viewCellId = strtol(ptr, &endptr, 10);
357                }
358        }
359
360       
361        if (viewCellId >= 0) // valid view cell
362        {
363                // TODO: get view cell with specified id
364                ViewCellInterior dummyVc;
365                dummyVc.SetId(viewCellId);
366
367                ViewCellContainer::iterator vit =
368                        lower_bound(mViewCells.begin(), mViewCells.end(), &dummyVc, vlt);
369                       
370                ViewCellInterior *viewCell = dynamic_cast<ViewCellInterior *>(*vit);
371
372                // giant hack!!
373                BspViewCell *l = dynamic_cast<BspViewCell *>(mViewCellsManager->GenerateViewCell());
374                viewCell->SetupChildLink(l);
375
376                if (viewCell->GetId() == viewCellId)
377                {
378                        leaf->SetViewCell(l);
379                        l->mLeaf = leaf;
380                }
381                else
382                {
383                        Debug << "error: view cell does not exist" << endl;
384                }
385        }
386        else
387        {
388                if (mViewCellsManager->GetType() == ViewCellsManager::VSP_BSP)
389                {
390                        leaf->SetViewCell(mVspBspTree->GetOrCreateOutOfBoundsCell());
391                        leaf->SetTreeValid(false);
392                        mVspBspTree->PropagateUpValidity(leaf);
393                }
394        }
395}
396
397
398void ViewCellsParseHandlers::StartBspInterior(AttributeList& attributes)
399{
400        Plane3 plane;
401        int len = attributes.getLength();
402
403        for (int i = 0; i < len; ++ i)
404        {
405                string attrName(StrX(attributes.getName(i)).LocalForm());
406                StrX attrValue(attributes.getValue(i));
407                const char *ptr = attrValue.LocalForm();
408
409                if (attrName == "plane")
410                {
411                        sscanf(ptr, "%f %f %f %f",
412                                   &plane.mNormal.x, &plane.mNormal.y, &plane.mNormal.z, &plane.mD);
413                }
414        }
415
416        BspInterior* interior = new BspInterior(plane);
417       
418        if (mCurrentBspNode) // replace NULL child of parent with current node
419        {
420                BspInterior *current = dynamic_cast<BspInterior *>(mCurrentBspNode);
421
422                current->ReplaceChildLink(NULL, interior);
423                interior->SetParent(current);
424        }
425        else
426        {
427                if (mViewCellsManager->GetType() == ViewCellsManager::BSP)
428                {
429                        mBspTree->mRoot = interior;
430                }
431                else
432                {
433                        mVspBspTree->mRoot = interior;
434                }
435        }
436
437        mCurrentBspNode = interior;
438}
439
440
441
442void ViewCellsParseHandlers::CreateViewCellsManager(const char *name)
443{
444        if (strcmp(name, "bspTree") == 0)
445        {
446                Debug << "view cell type: Bsp" << endl;
447
448                mBspTree = new BspTree();
449                mBspTree->mBox = mViewSpaceBox;
450                mCurrentBspNode = mBspTree->GetRoot();
451
452                mViewCellsManager = new BspViewCellsManager(mBspTree);
453        }
454        else if (strcmp(name, "vspBspTree") == 0)
455        {
456                Debug << "view cell type: VspBsp" << endl;
457
458                mVspBspTree = new VspBspTree();
459                mCurrentBspNode = mVspBspTree->GetRoot();
460
461                mViewCellsManager = new VspBspViewCellsManager(mVspBspTree);
462
463                mVspBspTree->mBox = mViewSpaceBox;
464                Debug << "creating vsp bsp view cells manager" << endl;
465        }
466        else if (strcmp(name, "vspKdTree") == 0)
467        {
468                // TODO
469                mVspKdTree = new VspKdTree();   
470                mViewCellsManager = new VspKdViewCellsManager(mVspKdTree);
471        }
472        else
473        {
474                cerr<<"Wrong view cells type" << name << endl;
475                exit(1);
476        }
477
478        mViewCellsManager->SetViewSpaceBox(mViewSpaceBox);
479}
480
481
482void ViewCellsParseHandlers::characters(const XMLCh* const chars,
483                                                                                const unsigned int length)
484{
485        mCharacterCount += length;
486}
487
488
489void ViewCellsParseHandlers::ignorableWhitespace(const XMLCh* const chars,
490                                                                                                 const unsigned int length)
491{
492        mSpaceCount += length;
493}
494
495
496void ViewCellsParseHandlers::resetDocument()
497{
498        mAttrCount = 0;
499        mCharacterCount = 0;
500        mElementCount = 0;
501        mSpaceCount = 0;
502}
503
504
505// ---------------------------------------------------------------------------
506//  StdInParseHandlers: Overrides of the SAX ErrorHandler interface
507// ---------------------------------------------------------------------------
508void
509ViewCellsParseHandlers::error(const SAXParseException& e)
510{
511  XERCES_STD_QUALIFIER cerr << "\nError at (file " << StrX(e.getSystemId())
512                            << ", line " << e.getLineNumber()
513                            << ", char " << e.getColumnNumber()
514                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
515}
516
517void
518ViewCellsParseHandlers::fatalError(const SAXParseException& e)
519{
520  XERCES_STD_QUALIFIER cerr << "\nFatal Error at (file " << StrX(e.getSystemId())
521                            << ", line " << e.getLineNumber()
522                            << ", char " << e.getColumnNumber()
523                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
524}
525
526void
527ViewCellsParseHandlers::warning(const SAXParseException& e)
528{
529  XERCES_STD_QUALIFIER cerr << "\nWarning at (file " << StrX(e.getSystemId())
530                            << ", line " << e.getLineNumber()
531                            << ", char " << e.getColumnNumber()
532                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
533}
534
535
536bool ViewCellsParser::ParseFile(const string filename,
537                                                                ViewCellsManager **viewCells,
538                                                                ObjectContainer *objects)
539{
540  // Initialize the XML4C system
541  try {
542    XMLPlatformUtils::Initialize();
543  }
544 
545  catch (const XMLException& toCatch)
546    {
547      XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
548                                << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
549      return false;
550    }
551 
552 
553  //
554  //  Create a SAX parser object. Then, according to what we were told on
555  //  the command line, set the options.
556  //
557  SAXParser* parser = new SAXParser;
558  parser->setValidationScheme(valScheme);
559  parser->setDoNamespaces(doNamespaces);
560  parser->setDoSchema(doSchema);
561  parser->setValidationSchemaFullChecking(schemaFullChecking);
562 
563
564  //
565  //  Create our SAX handler object and install it on the parser, as the
566  //  document and error handler. We are responsible for cleaning them
567  //  up, but since its just stack based here, there's nothing special
568  //  to do.
569  //
570  ViewCellsParseHandlers handler(objects);
571  parser->setDocumentHandler(&handler);
572  parser->setErrorHandler(&handler);
573 
574  unsigned long duration;
575  int errorCount = 0;
576  // create a faux scope so that 'src' destructor is called before
577  // XMLPlatformUtils::Terminate
578  {
579    //
580    //  Kick off the parse and catch any exceptions. Create a standard
581    //  input input source and tell the parser to parse from that.
582    //
583    //    StdInInputSource src;
584    try
585      {
586        const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
587        parser->parse(filename.c_str());
588        const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
589        duration = endMillis - startMillis;
590        errorCount = parser->getErrorCount();
591      }
592    catch (const OutOfMemoryException&)
593      {
594        XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
595        errorCount = 2;
596        return false;
597      }
598    catch (const XMLException& e)
599      {
600        XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
601                                  << StrX(e.getMessage())
602                                  << "\n" << XERCES_STD_QUALIFIER endl;
603        errorCount = 1;
604        return false;
605      }
606
607   
608    // Print out the stats that we collected and time taken
609    if (!errorCount) {
610      XERCES_STD_QUALIFIER cout << filename << ": " << duration << " ms ("
611                                << handler.GetElementCount() << " elems, "
612                                << handler.GetAttrCount() << " attrs, "
613                                << handler.GetSpaceCount() << " spaces, "
614                                << handler.GetCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
615    }
616  }
617 
618  //
619  //  Delete the parser itself.  Must be done prior to calling Terminate, below.
620  //
621  delete parser;
622 
623  XMLPlatformUtils::Terminate();
624 
625   // assign new view cells manager
626  *viewCells = handler.mViewCellsManager;
627
628  if (errorCount > 0)
629    return false;
630  else
631    return true;
632}
Note: See TracBrowser for help on using the repository browser.