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

Revision 2119, 11.3 KB checked in by mattausch, 17 years ago (diff)
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);
90Debug << "here333";
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                StartBvhLeaf(attributes);
136        }
137}
138
139
140void ObjectsParseHandlers::StartObjectSpaceHierarchyElement(const std::string &element,
141                                                                                                                        AttributeList& attributes)
142{
143        //-- use cell type according to the chosen method
144        StartBvhElement(element, attributes);
145}
146
147
148void ObjectsParseHandlers::startElement(const XMLCh* const name,
149                                                                                AttributeList& attributes)
150{
151        StrX lname(name);
152        string element(lname.LocalForm());
153
154        // decides about the view cell hierarchy
155        if (element == "ObjectSpaceHierarchy")
156        {
157                cout << "\nparsing object space hierarchy" << endl;
158                Debug << "\nparsing object space hierarchy" << endl;
159
160                mIsObjectSpaceHierarchy = true;
161        }
162       
163        // parse view space hierarchy
164        if (mIsObjectSpaceHierarchy)
165        {
166                StartObjectSpaceHierarchyElement(element, attributes);
167        }
168
169        ++ mElementCount;
170        mAttrCount += attributes.getLength();
171}
172
173
174
175void ObjectsParseHandlers::characters(const XMLCh* const chars,
176                                                                                const unsigned int length)
177{
178        mCharacterCount += length;
179}
180
181
182void ObjectsParseHandlers::ignorableWhitespace(const XMLCh* const chars,
183                                                                                                 const unsigned int length)
184{
185        mSpaceCount += length;
186}
187
188
189void ObjectsParseHandlers::resetDocument()
190{
191        mAttrCount = 0;
192        mCharacterCount = 0;
193        mElementCount = 0;
194        mSpaceCount = 0;
195}
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                        Debug<<"here93" << endl;
373                        parser->parse(isource);
374#else
375                        parser->parse(filename.c_str());
376
377#endif
378Debug<<"here932" << endl;
379                        const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
380                        duration = endMillis - startMillis;
381                        errorCount = parser->getErrorCount();
382                }
383                catch (const OutOfMemoryException&)
384                {
385                        XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
386                        errorCount = 2;
387                        return false;
388                }
389                catch (const XMLException& e)
390                {
391                        XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
392                                << StrX(e.getMessage())
393                                << "\n" << XERCES_STD_QUALIFIER endl;
394                        errorCount = 1;
395                        return false;
396                }
397
398
399                // Print out the stats that we collected and time taken
400                if (!errorCount)
401                {
402                        XERCES_STD_QUALIFIER cerr << filename << ": " << duration << " ms ("
403                                << handler.GetElementCount() << " elems, "
404                                << handler.GetAttrCount() << " attrs, "
405                                << handler.GetSpaceCount() << " spaces, "
406                                << handler.GetCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
407                }
408        }
409
410        cout << "parsed - will delete the parser" << endl << flush;
411        //
412        //  Delete the parser itself.  Must be done prior to calling Terminate, below.
413        //
414        delete parser;
415
416        XMLPlatformUtils::Terminate();
417
418        //-- assign new view cells manager
419        //*viewCells = handler.mViewCellsManager;
420
421        if (errorCount > 0)
422                return false;
423        else
424                return true;
425}
426
427}
Note: See TracBrowser for help on using the repository browser.