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

Revision 182, 10.2 KB checked in by mattausch, 19 years ago (diff)

the code is compiling under .net now

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