source: GTP/trunk/Lib/Vis/Preprocessing/src/ObjectsParser.cpp @ 2115

Revision 2115, 11.3 KB checked in by mattausch, 17 years ago (diff)

changed pvs loading: loading objects in a first pass

Line 
1// ---------------------------------------------------------------------------
2//  Includes for all the program files to see
3// ---------------------------------------------------------------------------
4#include <string.h>
5#include <stdlib.h>
6#include <iostream>
7//#include <ext/algorithm>
8
9using namespace std;
10#include <xercesc/util/PlatformUtils.hpp>
11
12// ---------------------------------------------------------------------------
13//  Includes
14// ---------------------------------------------------------------------------
15
16#include <xercesc/framework/StdInInputSource.hpp>
17#include <xercesc/parsers/SAXParser.hpp>
18#include <xercesc/util/OutOfMemoryException.hpp>
19
20// ---------------------------------------------------------------------------
21//  Includes
22// ---------------------------------------------------------------------------
23
24#include <xercesc/sax/AttributeList.hpp>
25#include <xercesc/sax/SAXParseException.hpp>
26#include <xercesc/sax/SAXException.hpp>
27
28#include "ObjectsParser.h"
29
30#include "ObjectsParserXerces.h"
31#include "Mesh.h"
32#include "ViewCellsManager.h"
33#include "GzFileInputSource.h"
34#include "BvHierarchy.h"
35
36
37namespace GtpVisibilityPreprocessor {
38
39
40// ---------------------------------------------------------------------------
41//  Local data
42//
43//  doNamespaces
44//      Indicates whether namespace processing should be enabled or not.
45//      The default is no, but -n overrides that.
46//
47//  doSchema
48//      Indicates whether schema processing should be enabled or not.
49//      The default is no, but -s overrides that.
50//
51//  schemaFullChecking
52//      Indicates whether full schema constraint checking should be enabled or not.
53//      The default is no, but -s overrides that.
54//
55//  valScheme
56//      Indicates what validation scheme to use. It defaults to 'auto', but
57//      can be set via the -v= command.
58// ---------------------------------------------------------------------------
59
60static bool     doNamespaces       = false;
61static bool     doSchema           = false;
62static bool     schemaFullChecking = false;
63static SAXParser::ValSchemes    valScheme       = SAXParser::Val_Auto;
64
65
66inline static bool ilt(Intersectable *obj1, Intersectable *obj2)
67{
68        return obj1->mId < obj2->mId;
69}
70
71
72// ---------------------------------------------------------------------------
73//  StdInParseHandlers: Constructors and Destructor
74// ---------------------------------------------------------------------------
75
76ObjectsParseHandlers::ObjectsParseHandlers(ObjectContainer &pvsObjects,
77                                                                                   const ObjectContainer &preprocessorObjects
78                                                                                   ):
79  mElementCount(0)
80  , mAttrCount(0)
81  , mCharacterCount(0)
82  , mSpaceCount(0)
83  , mPvsObjects(pvsObjects)
84  , mPreprocessorObjects(preprocessorObjects)
85  , mIsObjectSpaceHierarchy(false)
86{
87        // sort objects so we can search in them
88        //if (!is_sorted(mPvsObjects.begin(), mPvsObjects.end(), ilt))
89                sort(mPvsObjects.begin(), mPvsObjects.end(), ilt);
90
91        //if (!is_sorted(mPreprocessorObjects.begin(), mPreprocessorObjects.end(), ilt))
92                //sort(mPreprocessorObjects.begin(), mPreprocessorObjects.end(), ilt);
93}
94
95
96ObjectsParseHandlers::~ObjectsParseHandlers()
97{
98}
99
100
101// ---------------------------------------------------------------------------
102//  StdInParseHandlers: Implementation of the SAX DocumentHandler interface
103// ---------------------------------------------------------------------------
104
105
106void ObjectsParseHandlers::endElement(const XMLCh* const name)
107{
108        StrX lname(name);
109        string element(lname.LocalForm());
110
111        if (element == "ObjectSpaceHierarchy")
112        {
113                EndObjectSpaceHierarchy();
114        }
115}
116
117
118void ObjectsParseHandlers::EndObjectSpaceHierarchy()
119{
120        mIsObjectSpaceHierarchy = false;
121}
122
123
124inline static bool vlt(ViewCell *v1, ViewCell *v2)
125{
126        return v1->mId < v2->mId;
127}
128
129
130void ObjectsParseHandlers::StartBvhElement(string element,
131                                                                                   AttributeList& attributes)
132{
133        if (element == "Leaf")
134        {
135                //cout << "l";
136                StartBvhLeaf(attributes);
137        }
138}
139
140
141void ObjectsParseHandlers::StartObjectSpaceHierarchyElement(const std::string &element,
142                                                                                                                        AttributeList& attributes)
143{
144        //-- use cell type according to the chosen method
145        StartBvhElement(element, attributes);
146}
147
148
149void ObjectsParseHandlers::startElement(const XMLCh* const name,
150                                                                                AttributeList& attributes)
151{
152        StrX lname(name);
153        string element(lname.LocalForm());
154
155        // decides about the view cell hierarchy
156        if (element == "ObjectSpaceHierarchy")
157        {
158                cout << "\nparsing object space hierarchy" << endl;
159                Debug << "\nparsing object space hierarchy" << endl;
160
161                mIsObjectSpaceHierarchy = true;
162        }
163       
164        // parse view space hierarchy
165        if (mIsObjectSpaceHierarchy)
166        {
167                StartObjectSpaceHierarchyElement(element, attributes);
168        }
169
170        ++ mElementCount;
171        mAttrCount += attributes.getLength();
172}
173
174
175
176void ObjectsParseHandlers::characters(const XMLCh* const chars,
177                                                                                const unsigned int length)
178{
179        mCharacterCount += length;
180}
181
182
183void ObjectsParseHandlers::ignorableWhitespace(const XMLCh* const chars,
184                                                                                                 const unsigned int length)
185{
186        mSpaceCount += length;
187}
188
189
190void ObjectsParseHandlers::resetDocument()
191{
192        mAttrCount = 0;
193        mCharacterCount = 0;
194        mElementCount = 0;
195        mSpaceCount = 0;
196}
197
198void ObjectsParseHandlers::StartBvhLeaf(AttributeList& attributes)
199{
200        const int len = attributes.getLength();
201        Vector3 minBox, maxBox;
202
203        ObjectContainer objects;
204
205        for (int i = 0; i < len; ++ i)
206        {
207                string attrName(StrX(attributes.getName(i)).LocalForm());
208                StrX attrValue(attributes.getValue(i));
209                const char *ptr = attrValue.LocalForm();
210
211                if (attrName == "min")
212                {
213                        sscanf(ptr, "%f %f %f", &minBox.x, &minBox.y, &minBox.z);
214                }
215                if (attrName == "max")
216                {
217                        sscanf(ptr, "%f %f %f", &maxBox.x, &maxBox.y, &maxBox.z);
218                }
219                /*if (attrName == "objects")
220                {
221                        StartBvhLeafObjects(objects, ptr);
222                }*/
223        }
224
225        AxisAlignedBox3 box = AxisAlignedBox3(minBox, maxBox);
226
227        BvhLeaf *leaf = new BvhLeaf(box, NULL, (int)objects.size());
228
229        //leaf->mObjects = objects;
230        //BvHierarchy::AssociateObjectsWithLeaf(leaf);
231
232        // new pvs object
233        mPvsObjects.push_back(leaf);
234}
235
236
237void ObjectsParseHandlers::StartBvhLeafObjects(ObjectContainer &objects,
238                                                                                           const char *ptr)
239{
240        vector<int> objIndices;
241        char *endptr;
242                       
243        while (1)
244        {
245                const int index = strtol(ptr, &endptr, 10);
246                if (ptr == endptr) break;
247
248                objIndices.push_back(index);
249                ptr = endptr;
250        }
251
252        MeshInstance dummyInst(NULL);
253
254        vector<int>::const_iterator it, it_end = objIndices.end();
255
256        for (it = objIndices.begin(); it != it_end; ++ it)
257        {
258                const int objId = *it; 
259                dummyInst.SetId(objId);
260
261                ObjectContainer::const_iterator oit =
262                        lower_bound(mPreprocessorObjects.begin(),
263                                                mPreprocessorObjects.end(),
264                                                (Intersectable *)&dummyInst,
265                                                ilt);   
266                                                       
267                if ((oit != mPreprocessorObjects.end()) && ((*oit)->GetId() == objId))
268                {
269                        objects.push_back(*oit);
270                        Debug << "x";
271                }
272                else
273                {
274                        Debug << "y";
275                        //cerr << "StartBvhLeafObjects error: object with id " << objId << " does not exist" << endl;
276                }
277        }
278}
279
280
281
282// ---------------------------------------------------------------------------
283//  StdInParseHandlers: Overrides of the SAX ErrorHandler interface
284// ---------------------------------------------------------------------------
285
286
287void
288ObjectsParseHandlers::error(const SAXParseException& e)
289{
290  XERCES_STD_QUALIFIER cerr << "\nError at (file " << StrX(e.getSystemId())
291                            << ", line " << e.getLineNumber()
292                            << ", char " << e.getColumnNumber()
293                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
294}
295
296void
297ObjectsParseHandlers::fatalError(const SAXParseException& e)
298{
299  XERCES_STD_QUALIFIER cerr << "\nFatal Error at (file " << StrX(e.getSystemId())
300                            << ", line " << e.getLineNumber()
301                            << ", char " << e.getColumnNumber()
302                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
303}
304
305void
306ObjectsParseHandlers::warning(const SAXParseException& e)
307{
308  XERCES_STD_QUALIFIER cerr << "\nWarning at (file " << StrX(e.getSystemId())
309                            << ", line " << e.getLineNumber()
310                            << ", char " << e.getColumnNumber()
311                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
312}
313
314
315bool ObjectsParser::ParseObjects(const string &filename,
316                                                                 ObjectContainer &pvsObjects,
317                                                                 const ObjectContainer &preprocessorObjects)
318{
319        // Initialize the XML4C system
320        try {
321                XMLPlatformUtils::Initialize();
322        }
323
324        catch (const XMLException& toCatch)
325        {
326                XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
327                        << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
328                return false;
329        }
330
331        //  cout<<"parsing started"<<endl<<flush;
332
333        //
334        //  Create a SAX parser object. Then, according to what we were told on
335        //  the command line, set the options.
336        //
337        SAXParser* parser = new SAXParser;
338        parser->setValidationScheme(valScheme);
339        parser->setDoNamespaces(doNamespaces);
340        parser->setDoSchema(doSchema);
341        parser->setValidationSchemaFullChecking(schemaFullChecking);
342
343
344        //
345        //  Create our SAX handler object and install it on the parser, as the
346        //  document and error handler. We are responsible for cleaning them
347        //  up, but since its just stack based here, there's nothing special
348        //  to do.
349        //
350        ObjectsParseHandlers handler(pvsObjects, preprocessorObjects);
351        parser->setDocumentHandler(&handler);
352        parser->setErrorHandler(&handler);
353
354        unsigned long duration;
355        int errorCount = 0;
356        // create a faux scope so that 'src' destructor is called before
357        // XMLPlatformUtils::Terminate
358        {
359                //
360                //  Kick off the parse and catch any exceptions. Create a standard
361                //  input input source and tell the parser to parse from that.
362                //
363                //    StdInInputSource src;
364                try
365                {
366                        const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
367
368#if USE_GZLIB
369                        XMLCh *myFilePath = XMLString::transcode(filename.c_str());
370
371                        GzFileInputSource isource(myFilePath);
372                        parser->parse(isource);
373#else
374                        parser->parse(filename.c_str());
375
376#endif
377
378                        const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
379                        duration = endMillis - startMillis;
380                        errorCount = parser->getErrorCount();
381                }
382                catch (const OutOfMemoryException&)
383                {
384                        XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
385                        errorCount = 2;
386                        return false;
387                }
388                catch (const XMLException& e)
389                {
390                        XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
391                                << StrX(e.getMessage())
392                                << "\n" << XERCES_STD_QUALIFIER endl;
393                        errorCount = 1;
394                        return false;
395                }
396
397
398                // Print out the stats that we collected and time taken
399                if (!errorCount)
400                {
401                        XERCES_STD_QUALIFIER cerr << filename << ": " << duration << " ms ("
402                                << handler.GetElementCount() << " elems, "
403                                << handler.GetAttrCount() << " attrs, "
404                                << handler.GetSpaceCount() << " spaces, "
405                                << handler.GetCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
406                }
407        }
408
409        cout << "parsed - will delete the parser" << endl << flush;
410        //
411        //  Delete the parser itself.  Must be done prior to calling Terminate, below.
412        //
413        delete parser;
414
415        XMLPlatformUtils::Terminate();
416
417        //-- assign new view cells manager
418        //*viewCells = handler.mViewCellsManager;
419
420        if (errorCount > 0)
421                return false;
422        else
423                return true;
424}
425
426}
Note: See TracBrowser for help on using the repository browser.