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

Revision 2176, 10.9 KB checked in by mattausch, 17 years ago (diff)

removed using namespace std from .h

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