source: trunk/VUT/GtpVisibilityPreprocessor/src/X3dParser.cpp @ 170

Revision 170, 10.1 KB checked in by bittner, 19 years ago (diff)

mesh kd tree added

RevLine 
[170]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>
[162]9
[170]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
[162]24#include "X3dParser.h"
25
[170]26#include "X3dParserXerces.h"
27#include "Mesh.h"
28#include "SceneGraph.h"
[162]29
[170]30
31
32// ---------------------------------------------------------------------------
33//  Local data
34//
35//  doNamespaces
36//      Indicates whether namespace processing should be enabled or not.
37//      The default is no, but -n overrides that.
38//
39//  doSchema
40//      Indicates whether schema processing should be enabled or not.
41//      The default is no, but -s overrides that.
42//
43//  schemaFullChecking
44//      Indicates whether full schema constraint checking should be enabled or not.
45//      The default is no, but -s overrides that.
46//
47//  valScheme
48//      Indicates what validation scheme to use. It defaults to 'auto', but
49//      can be set via the -v= command.
50// ---------------------------------------------------------------------------
51static bool     doNamespaces       = false;
52static bool     doSchema           = false;
53static bool     schemaFullChecking = false;
54static SAXParser::ValSchemes    valScheme       = SAXParser::Val_Auto;
55
56
57
58
59
60// ---------------------------------------------------------------------------
61//  StdInParseHandlers: Constructors and Destructor
62// ---------------------------------------------------------------------------
63X3dParseHandlers::X3dParseHandlers(SceneGraphNode *root) :
64  fElementCount(0)
65  , fAttrCount(0)
66  , fCharacterCount(0)
67  , fSpaceCount(0)
[162]68{
[170]69  currentNode = root;
[162]70}
71
[170]72X3dParseHandlers::~X3dParseHandlers()
73{
74}
[162]75
[170]76
77// ---------------------------------------------------------------------------
78//  StdInParseHandlers: Implementation of the SAX DocumentHandler interface
79// ---------------------------------------------------------------------------
80void X3dParseHandlers::endElement(const XMLCh* const name)
[162]81{
[170]82  StrX lname(name);
83  string element(lname.localForm());
84  if (element == "Shape")
85    EndShape();
86}
[162]87
[170]88void
89X3dParseHandlers::EndShape()
90{
91  currentMesh->Preprocess();
92  // make an instance of this mesh
93  MeshInstance *mi = new MeshInstance(currentMesh);
94  currentNode->mGeometry.push_back(mi);
95  currentMesh = NULL;
96}
[162]97
[170]98void
99X3dParseHandlers::StartIndexedFaceSet(
100                                      AttributeList&  attributes)
101{
102  int len = attributes.getLength();
103  int i;
104  VertexIndexContainer vertices;
[162]105 
[170]106  for (i=0; i < len; i++) {
107    string attrName(StrX(attributes.getName(i)).localForm());
108    if (attrName == "coordIndex") {
109      StrX attrValue(attributes.getValue(i));
110      // handle coordIndex
111      vertices.clear();
112      const char *ptr = attrValue.localForm();
113      char *endptr;
114      while(1) {
115        int index = strtol(ptr, &endptr, 10);
116        if (ptr == endptr || index == -1) {
117          if (vertices.size() > 2) {
118            Face *face = new Face(vertices);
119            currentMesh->mFaces.push_back(face);
120          }
121          vertices.clear();
122          if (ptr == endptr)
123            break;
124        } else {
125          vertices.push_back(index);
126        }
127        ptr = endptr;
128      }
129    }
[162]130  }
131}
132
[170]133void
134X3dParseHandlers::StartMaterial(
135                                AttributeList&  attributes)
[162]136{
[170]137  int len = attributes.getLength();
138  int i;
139  if (!currentMesh->mMaterial)
140    currentMesh->mMaterial = new Material;
141  for (i=0; i < len; i++) {
142    string attrName(StrX(attributes.getName(i)).localForm());
143    StrX attrValue(attributes.getValue(i));
144    const char *ptr = attrValue.localForm();
145    if (attrName == "diffuseColor") {
146      float r, g, b;
147      if (sscanf(ptr, "%f %f %f", &r, &g, &b) == 3)
148        currentMesh->mMaterial->mDiffuseColor = RgbColor(r, g, b);
149    }
150  }
[162]151}
152
[170]153void
154X3dParseHandlers::StartCoordinate(
155                                  AttributeList&  attributes)
[162]156{
[170]157  int len = attributes.getLength();
158  int i;
159  VertexContainer vertices;
160  for (i=0; i < len; i++) {
161    string attrName(StrX(attributes.getName(i)).localForm());
162    if (attrName == "point") {
163      StrX attrValue(attributes.getValue(i));
164      const char *ptr = attrValue.localForm();
165      char *endptr;
166      while(1) {
167        float x = strtod(ptr, &endptr);
168        if (ptr == endptr)
169          break;
170        ptr = endptr;
171        float y = strtod(ptr, &endptr);
172        if (ptr == endptr)
173          break;
174        ptr = endptr;
175        float z = strtod(ptr, &endptr);
176        if (ptr == endptr)
177          break;
178        ptr = endptr;
179        if (*ptr == ',')
180          ptr++;
181        Vector3 v(x, y, z);
182        vertices.push_back(v);
183      }
184      currentMesh->mVertices = vertices;
185    }
186  }
[162]187}
188
[170]189
190void
191X3dParseHandlers::startElement(const XMLCh* const name,
192                               AttributeList&  attributes)
[162]193{
[170]194  StrX lname(name);
195  string element(lname.localForm());
196 
197  if (element == "IndexedFaceSet") {
198    // create a new mesh node in the scene graph
199    StartIndexedFaceSet(attributes);
200  }
201
202  if (element == "Shape") {
203    cout<<"+";
204    currentMesh = new Mesh;
205  }
206 
207  if (element == "Coordinate") {
208    if (currentMesh)
209      StartCoordinate(attributes);
210  }
211 
212  if (element == "Material") {
213    StartMaterial(attributes);
214  }
215
216  fElementCount++;
217  fAttrCount += attributes.getLength();
[162]218}
219
[170]220void
221X3dParseHandlers::characters(const XMLCh* const chars,
222                             const unsigned int length)
[162]223{
[170]224  fCharacterCount += length;
[162]225}
226
[170]227void
228X3dParseHandlers::ignorableWhitespace(const XMLCh* const chars,
229                                      const unsigned int length)
[162]230{
[170]231  fSpaceCount += length;
[162]232}
233
[170]234void
235X3dParseHandlers::resetDocument()
[162]236{
[170]237  fAttrCount = 0;
238  fCharacterCount = 0;
239  fElementCount = 0;
240  fSpaceCount = 0;
[162]241}
242
[170]243
244
245// ---------------------------------------------------------------------------
246//  StdInParseHandlers: Overrides of the SAX ErrorHandler interface
247// ---------------------------------------------------------------------------
248void
249X3dParseHandlers::error(const SAXParseException& e)
[162]250{
[170]251  XERCES_STD_QUALIFIER cerr << "\nError at (file " << StrX(e.getSystemId())
252                            << ", line " << e.getLineNumber()
253                            << ", char " << e.getColumnNumber()
254                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
255}
[162]256
[170]257void
258X3dParseHandlers::fatalError(const SAXParseException& e)
[162]259{
[170]260  XERCES_STD_QUALIFIER cerr << "\nFatal Error at (file " << StrX(e.getSystemId())
261                            << ", line " << e.getLineNumber()
262                            << ", char " << e.getColumnNumber()
263                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
[162]264}
265
[170]266void
267X3dParseHandlers::warning(const SAXParseException& e)
[162]268{
[170]269  XERCES_STD_QUALIFIER cerr << "\nWarning at (file " << StrX(e.getSystemId())
270                            << ", line " << e.getLineNumber()
271                            << ", char " << e.getColumnNumber()
272                            << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
[162]273}
274
[170]275
276bool
277X3dParser::ParseFile(const string filename,
278                     SceneGraphNode **root)
279{
280  // Initialize the XML4C system
281  try {
282    XMLPlatformUtils::Initialize();
283  }
284 
285  catch (const XMLException& toCatch)
286    {
287      XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
288                                << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
289      return false;
290    }
291 
292 
293  //
294  //  Create a SAX parser object. Then, according to what we were told on
295  //  the command line, set the options.
296  //
297  SAXParser* parser = new SAXParser;
298  parser->setValidationScheme(valScheme);
299  parser->setDoNamespaces(doNamespaces);
300  parser->setDoSchema(doSchema);
301  parser->setValidationSchemaFullChecking(schemaFullChecking);
302 
303
304  //
305  //  Create our SAX handler object and install it on the parser, as the
306  //  document and error handler. We are responsible for cleaning them
307  //  up, but since its just stack based here, there's nothing special
308  //  to do.
309  //
310  *root = new SceneGraphNode;
311  X3dParseHandlers handler(*root);
312  parser->setDocumentHandler(&handler);
313  parser->setErrorHandler(&handler);
314 
315  unsigned long duration;
316  int errorCount = 0;
317  // create a faux scope so that 'src' destructor is called before
318  // XMLPlatformUtils::Terminate
319  {
320    //
321    //  Kick off the parse and catch any exceptions. Create a standard
322    //  input input source and tell the parser to parse from that.
323    //
324    //    StdInInputSource src;
325    try
326      {
327        const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
328        parser->parse(filename.c_str());
329        const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
330        duration = endMillis - startMillis;
331        errorCount = parser->getErrorCount();
332      }
333    catch (const OutOfMemoryException&)
334      {
335        XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
336        errorCount = 2;
337        return false;
338      }
339    catch (const XMLException& e)
340      {
341        XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
342                                  << StrX(e.getMessage())
343                                  << "\n" << XERCES_STD_QUALIFIER endl;
344        errorCount = 1;
345        return false;
346      }
347   
348    // Print out the stats that we collected and time taken
349    if (!errorCount) {
350      XERCES_STD_QUALIFIER cout << filename << ": " << duration << " ms ("
351                                << handler.getElementCount() << " elems, "
352                                << handler.getAttrCount() << " attrs, "
353                                << handler.getSpaceCount() << " spaces, "
354                                << handler.getCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
355    }
356  }
357 
358  //
359  //  Delete the parser itself.  Must be done prior to calling Terminate, below.
360  //
361  delete parser;
362 
363  XMLPlatformUtils::Terminate();
364 
365  if (errorCount > 0)
366    return false;
367  else
368    return true;
[162]369}
370
[170]371
Note: See TracBrowser for help on using the repository browser.