VRUT::RDFParser Class Reference

Class for parsing RDF files. More...

#include <rdfparser.h>

List of all members.

Public Member Functions

 RDFParser (wxInputStream *_inputStream, SCENE_ID _sceneID, const wxString _scenePath, Module *_module)
 ~RDFParser ()
 Class destructor.
bool Parse (const wxString &rootUid)

Private Member Functions

wxString readValue (TextParser &textParser)
 Read value from varName = value.
float readFloat (TextParser &textParser)
 Read float from "varName = value".
bool parseASCII (GeometryNode *node, std::vector< VECTOR3 > *verts, std::vector< GeometryTriangles::Indice > *indices)
 Parse from ASCII source.

Private Attributes

wxInputStream * inputStream
 Input stream.
SCENE_ID sceneID
 Scene container ID to be built.
wxString fileName
 Path to scene resources, can be archive filename.
Modulemodule
 Parent module's interface.


Detailed Description

Class for parsing RDF files.

Definition at line 26 of file rdfparser.h.


Constructor & Destructor Documentation

RDFParser::RDFParser ( wxInputStream *  _inputStream,
SCENE_ID  _sceneID,
const wxString  _scenePath,
Module _module 
)

Class constructor

Parameters:
[in] _inputStream Stream to read data from
[in] _sceneID Scene ID
[in] _scenePath Path to scene resources (can be an archive filename)
[in] _module Parent module with messaging interface

Definition at line 21 of file rdfparser.cpp.

00022 : inputStream(_inputStream), sceneID(_sceneID), module(_module)
00023 {
00024        fileName = wxFileName(_scenePath).GetName();
00025 }

RDFParser::~RDFParser (  ) 

Class destructor.

Is called by wxWidgets itself on wxApp end but it doesn't work in dynamic library

Definition at line 28 of file rdfparser.cpp.

00029 {
00031 }


Member Function Documentation

wxString VRUT::RDFParser::readValue ( TextParser textParser  )  [inline, private]

Read value from varName = value.

Definition at line 39 of file rdfparser.h.

00040               {
00041                      if (textParser.GetChar() != '=')
00042                             LOGERROR(wxT("<RDFParser>Wrong format for readValue."));
00043                      return textParser.GetWxString();
00044               }

float VRUT::RDFParser::readFloat ( TextParser textParser  )  [inline, private]

Read float from "varName = value".

Definition at line 47 of file rdfparser.h.

00048               {
00049                      if (textParser.GetChar() != '=')
00050                             LOGERROR(wxT("<RDFParser>Wrong format for readValue."));
00051                      float val=0;
00052                      if (!textParser.ParseFloat(&val))
00053                             LOGERROR(wxT("<RDFParser>float expected."));
00054                      return val;
00055               }

bool RDFParser::parseASCII ( GeometryNode node,
std::vector< VECTOR3 > *  verts,
std::vector< GeometryTriangles::Indice > *  indices 
) [private]

Parse from ASCII source.

parse

Definition at line 33 of file rdfparser.cpp.

00034 {
00035        TextParser textParser(inputStream, 10000, 1000);
00036 
00038        const char *keywords[]={"[MDI_HEADER]", "[UNITS]", "[MODEL]", "[OFFSET]", "[NODES]", "[ELEMENTS]", ""};
00039        while (!textParser.IsEmpty())
00040        {
00041               while (textParser.NextCharIs('$'))
00042                      textParser.GetWxLine();//skip comment
00043               switch (textParser.KeywordsSwitch(keywords))
00044               {
00045               case 0://[MDI_HEADER]
00046                      {
00047                             const char *keywordsH[]={"FILE_TYPE", "FILE_VERSION", "FILE_FORMAT", "(COMMENTS)", "{comment_string)", ""};
00048                             while (!textParser.IsEmpty() && !textParser.NextCharIs('['))
00049                             {
00050                                    if (textParser.NextCharIs('$'))
00051                                    {
00052                                           textParser.GetWxLine();//skip comment
00053                                           continue;
00054                                    }
00055                                    switch (textParser.KeywordsSwitch(keywordsH))
00056                                    {
00057                                    case 0://FILE_TYPE
00058                                           if (readValue(textParser)!=wxT("'rdf'"))
00059                                                  LOGWARNING(wxT("<RDFParser>Wrong file type."));
00060                                           break;
00061                                    case 1://FILE_VERSION
00062                                           if (readValue(textParser)!=wxT("5.00"))
00063                                                  LOGWARNING(wxT("<RDFParser>Not tested file version."));
00064                                           break;
00065                                    case 2://FILE_FORMAT
00066                                           if (readValue(textParser)!=wxT("'ASCII'"))
00067                                                  LOGWARNING(wxT("<RDFParser>Wrong file format."));
00068                                           break;
00069                                    case 3://(COMMENTS)
00070                                           break;
00071                                    case 4://{comment_string)
00072                                           textParser.GetWxLine();//skip comment
00073                                           textParser.GetWxLine();//skip comment
00074                                           textParser.GetWxLine();//skip comment
00075                                           break;
00076                                    default:
00077                                           LOGERROR(wxT("<RDFParser>Unknown keyword: ")+textParser.GetWxLine());
00078                                    }
00079                             }
00080                             break;
00081                      }
00082               case 1://[UNITS]
00083                      {
00084                             const char *keywordsU[]={"LENGTH", "FORCE", "ANGLE", "MASS", "TIME", ""};
00085                             while (!textParser.IsEmpty() && !textParser.NextCharIs('['))
00086                             {
00087                                    if (textParser.NextCharIs('$'))
00088                                    {
00089                                           textParser.GetWxLine();//skip comment
00090                                           continue;
00091                                    }
00092                                    switch (textParser.KeywordsSwitch(keywordsU))
00093                                    {
00094                                    case 0://LENGTH
00095 
00096                                           if (readValue(textParser)!=wxT("'mm'"))
00097                                                  LOGWARNING(wxT("<RDFParser>Wrong length units."));
00098                                           break;
00099                                    case 1://FORCE
00100                                    case 2://ANGLE
00101                                    case 3://MASS
00102                                    case 4://TIME
00103                                           textParser.GetWxLine();//skip it
00104                                           break;
00105                                    default:
00106                                           LOGERROR(wxT("<RDFParser>Unknown keyword: ")+textParser.GetWxLine());
00107                                    }
00108                             }
00109                             break;
00110                      }
00111               case 2://[MODEL]
00112                      {
00113                             const char *keywordsM[]={"METHOD", "FUNCTION_NAME", ""};
00114                             while (!textParser.IsEmpty() && !textParser.NextCharIs('['))
00115                             {
00116                                    if (textParser.NextCharIs('$'))
00117                                    {
00118                                           textParser.GetWxLine();//skip comment
00119                                           continue;
00120                                    }
00121                                    switch (textParser.KeywordsSwitch(keywordsM))
00122                                    {
00123                                    case 0://METHOD
00124 
00125                                           if (readValue(textParser)!=wxT("'3D'"))
00126                                                  LOGWARNING(wxT("<RDFParser>Wrong method."));
00127                                           break;
00128                                    case 1://FUNCTION_NAME
00129                                           textParser.GetWxLine();//skip it
00130                                           break;
00131                                    default:
00132                                           LOGERROR(wxT("<RDFParser>Unknown keyword: ")+textParser.GetWxLine());
00133                                    }
00134                             }
00135                             break;
00136                      }
00137 
00138               case 3://[OFFSET]
00139                      {
00140                             float x=0, y=0, z=0;
00141                             const char *keywordsO[]={"X", "Y", "Z", ""};
00142                             while (!textParser.IsEmpty() && !textParser.NextCharIs('['))
00143                             {
00144                                    if (textParser.NextCharIs('$'))
00145                                    {
00146                                           textParser.GetWxLine();//skip comment
00147                                           continue;
00148                                    }
00149                                    switch (textParser.KeywordsSwitch(keywordsO))
00150                                    {
00151                                    case 0://X
00152                                           x=readFloat(textParser);
00153                                           break;
00154                                    case 1://Y
00155                                           y=readFloat(textParser);
00156                                           break;
00157                                    case 2://Z
00158                                           z=readFloat(textParser);
00159                                           break;
00160                                    default:
00161                                           LOGERROR(wxT("<RDFParser>Unknown keyword: ")+textParser.GetWxLine());
00162                                    }
00163                             }
00164                             //TODO: transform
00165                             break;
00166                      }
00167               case 4://[NODES]
00168                      {
00169                             long index=0;
00170                             while (!textParser.IsEmpty() && !textParser.NextCharIs('['))
00171                             {
00172                                    if (textParser.NextCharIs('$'))
00173                                    {
00174                                           textParser.GetWxLine();//skip comment
00175                                           continue;
00176                                    }
00177                                    if (!textParser.ParseLong(&index))
00178                                    {
00179                                           textParser.GetWxLine();
00180                                           continue;
00181                                    }
00182                                    if (index!=verts->size()+1)
00183                                           LOGERROR(wxT("<RDFParser>Wrong index of vertex."));
00184                                    vector3 vertex;
00185                                    if (textParser.ParseVector(vertex._v, 3, true)!=3)
00186                                           LOGERROR(wxT("<RDFParser>Wrong format of vertex."));
00187                                    verts->push_back(vertex);
00188                             }
00189                             break;
00190                      }
00191               case 5://[ELEMENTS]
00192                      {
00193                             size_t maxVerts=verts->size();
00194                             while (!textParser.IsEmpty() && !textParser.NextCharIs('['))
00195                             {
00196                                    if (textParser.NextCharIs('$'))
00197                                    {
00198                                           textParser.GetWxLine();//skip comment
00199                                           continue;
00200                                    }
00201                                    long i, j, k, t;
00202                                    if (!textParser.ParseLong(&i))
00203                                    {
00204                                           textParser.GetWxLine();
00205                                           continue;
00206                                    }
00207                                    if (!textParser.ParseLong(&j))
00208                                    {
00209                                           textParser.GetWxLine();
00210                                           continue;
00211                                    }
00212                                    if (!textParser.ParseLong(&k))
00213                                    {
00214                                           textParser.GetWxLine();
00215                                           continue;
00216                                    }
00217                                    if (!textParser.ParseLong(&t))
00218                                    {
00219                                           textParser.GetWxLine();
00220                                           continue;
00221                                    }
00222                                    if (t!=1)
00223                                           LOGERROR(wxT("<RDFParser>Wrong number of indices."));
00224                                    if ((i>maxVerts) || (j>maxVerts) || (k>maxVerts))
00225                                           LOGERROR(wxT("<RDFParser>Indice out of vertices count."));
00226                                    indices->push_back(i-1);
00227                                    indices->push_back(j-1);
00228                                    indices->push_back(k-1);
00229                             }
00230                             break;
00231                      }
00232               default:
00233                      LOGERROR(wxT("<RDFParser>Unexpected token: '" + textParser.GetWxLine() + wxT("'")));
00234                      return false;
00235               }
00236        }
00237        return true;
00238 }

bool RDFParser::Parse ( const wxString &  rootUid  ) 

Parse RDF stream and build scene graph

Parameters:
[in] rootUid Uid of node that will be root of imported scene
Returns:
true if successful

Add new geometry node

Create new mesh

prepare

Assign mesh

Definition at line 240 of file rdfparser.cpp.

00241 {
00242        wxCommandEvent evName = Event::GET_EVT_SCENE_NODE_NAME_SET(sceneID, rootUid, fileName);
00243        module->PostToKernel(evName);
00244 
00245        //prepare material
00246        Material * material = new Material(wxT("RDFDefault"));
00247        material->diffuse = VECTOR4(0.5, 0.5, 0.5, 1);
00248        material->specular = VECTOR4(0.5, 0.5, 0.5, 1);
00249        material->flags = Material::DOUBLE_SIDED;
00250        wxCommandEvent evMat = Event::GET_EVT_SCENE_MATERIAL_ADD(sceneID, material);
00251        module->PostToKernel(evMat);
00252 
00253        //prepare geometry
00255        wxString nodeName = fileName + wxT("_node");
00256        GeometryNode * node = new GeometryNode(nodeName, nodeName);
00257        wxCommandEvent evNode = Event::GET_EVT_SCENE_NODE_INSERT(sceneID, node, rootUid);
00258        module->PostToKernel(evNode);
00259        wxCommandEvent evMatSet = Event::GET_EVT_SCENE_NODE_MATERIAL_SET(sceneID, nodeName, material->name);
00260        module->PostToKernel(evMatSet);
00261 
00263        GeometryTriangles *geometry = new GeometryTriangles(nodeName);
00264 
00266        std::vector<VECTOR3> * verts = &(geometry->vertices);
00267        std::vector<GeometryTriangles::Indice> * indices = &(geometry->indices);
00268        std::pair<GeometryTriangles::Indice, GeometryTriangles::PRIMITIVE_TYPE> indType((GeometryTriangles::Indice)indices->size(), GeometryTriangles::TRI_LIST);
00269        geometry->triDescList.push_back(indType);
00270 
00271        //parse
00272        if (!parseASCII(node, verts, indices))
00273        {
00274               LOGERROR(wxT("<RDFParser>Scene not loaded properly"));
00275               return false;
00276        }
00277        else
00278        {
00280               wxCommandEvent evGeom = Event::GET_EVT_SCENE_GEOMETRY_ADD(sceneID, geometry);
00281               module->PostToKernel(evGeom);
00282               wxCommandEvent evGeomSet = Event::GET_EVT_SCENE_NODE_GEOMETRY_SET(sceneID, nodeName, geometry->GetName());
00283               module->PostToKernel(evGeomSet);
00284        }
00285        return true;
00286 }


Member Data Documentation

wxInputStream* VRUT::RDFParser::inputStream [private]

Input stream.

Definition at line 30 of file rdfparser.h.

Scene container ID to be built.

Definition at line 32 of file rdfparser.h.

wxString VRUT::RDFParser::fileName [private]

Path to scene resources, can be archive filename.

Definition at line 34 of file rdfparser.h.

Parent module's interface.

Definition at line 36 of file rdfparser.h.


The documentation for this class was generated from the following files:

Generated on Tue Mar 10 14:41:40 2009 for VRUT by  doxygen 1.5.5