00001
00011 #ifndef __VRMLPARSER__H__
00012 #define __VRMLPARSER__H__
00013
00014 #include <wx/hashmap.h>
00015 #include <cmath>
00016 #include <wx/tokenzr.h>
00017 #include <wx/zstream.h>
00018 #include "../../core/src/scenemanager.h"
00019 #include "../../core/src/geometrytriangles.h"
00020 #include "../../core/src/module.h"
00021 #include "../../core/src/textparser.h"
00022 #include "../../core/src/material.h"
00023 #include "../../core/src/geometrytriangles.h"
00024 #include "../../core/src/geometrynode.h"
00025 #include "../../core/src/camera.h"
00026
00027
00028 namespace VRUT
00029 {
00030 class Module;
00031
00033 class VRMLParser
00034 {
00035 private:
00036
00038 TextParser * textParser;
00040 SCENE_ID sceneID;
00042 wxString scenePath;
00044 Module * module;
00046 bool fromVRUT;
00047
00048 WX_DECLARE_STRING_HASH_MAP(bool, NodeUidsList);
00050 NodeUidsList nodeUidsList;
00051
00054 NodeUidsList& getNodeUidsList(){
00055 return nodeUidsList;
00056 }
00057
00058 WX_DECLARE_STRING_HASH_MAP(std::vector<VECTOR3> *,DefNamesListV3);
00060 DefNamesListV3 defNamesListV3;
00061 WX_DECLARE_STRING_HASH_MAP(std::vector<VECTOR2> *,DefNamesListV2);
00063 DefNamesListV2 defNamesListV2;
00064 WX_DECLARE_STRING_HASH_MAP(wxString, DefNamesListSN);
00066 DefNamesListSN defNamesListSN;
00067 WX_DECLARE_STRING_HASH_MAP(Material * ,DefNamesListM);
00069 DefNamesListM defNamesListM;
00070 WX_DECLARE_STRING_HASH_MAP(wxString ,DefNamesListG);
00072 DefNamesListG defNamesListG;
00073
00075 struct Node{
00076 protected:
00077 friend class VRMLParser;
00079 wxString nodeName;
00081 wxString defName;
00083 SceneNode* sceneNode;
00085 bool remove;
00087 static VRMLParser *vrmlp;
00088
00089 public:
00094 Node(wxString name, SceneNode* sn, wxString defName = wxT("")):nodeName(name),sceneNode(sn),defName(defName){remove = true;}
00097 virtual void addToScene (wxString parentUid) = 0;
00098 };
00099
00101 struct LNode : public Node {
00103 Light* lparam;
00108 LNode(wxString name, LightNode* sn, wxString defName = wxT("")):Node(name,sn,defName){ }
00110 ~LNode(){
00111 if (remove) {
00112 delete lparam;
00113 }
00114 }
00117 void setParam( Light* node){
00118 lparam = node;
00119 }
00122 void addToScene(wxString parentUid){
00123
00124 DefNamesListSN::iterator it = vrmlp->defNamesListSN.find(nodeName);
00125 if ( it != vrmlp->defNamesListSN.end()) {
00126 wxCommandEvent ev1 = Event::GET_EVT_SCENE_NODE_COPY(vrmlp->sceneID, it->second, parentUid);
00127 return;
00128 }
00129 wxCommandEvent ev = Event::GET_EVT_SCENE_NODE_INSERT(vrmlp->sceneID, static_cast<LightNode*> (sceneNode) , parentUid);
00130 vrmlp->module->PostToKernel(ev);
00131 wxCommandEvent ev2 = Event::GET_EVT_SCENE_NODE_LIGHT_SET(vrmlp->sceneID, nodeName, lparam);
00132 vrmlp->module->PostToKernel(ev2);
00133
00134 if ( nodeName != wxT("")) {
00135 vrmlp->defNamesListSN.insert(DefNamesListSN::value_type(nodeName, defName ));
00136 remove = false;
00137 }
00138 }
00139 };
00141 struct GNode : public Node {
00143 VRUT::Material * mparam;
00145 wxString mname;
00147 wxString mdef;
00149 Geometry* gparam;
00151 wxString gname;
00153 wxString gdef;
00158 GNode(wxString name, GeometryNode* sn, wxString defName = wxT("")):Node(name,sn,defName){
00159 remove = true;
00160 }
00165 void setMaterial( VRUT::Material * mat, wxString name = wxT(""), wxString dn = wxT("")){ mparam = mat; mname = name; mdef=dn; }
00170 void setGeometry(Geometry* g, wxString name = wxT(""), wxString dn = wxT("")) { gparam = g; gname = name; gdef = dn;}
00172 ~GNode(){
00173 if (remove) {
00174 delete mparam;
00175 delete gparam;
00176 }
00177 }
00179 void addToScene(wxString parentUid){
00180
00181 DefNamesListSN::iterator it = vrmlp->defNamesListSN.find(nodeName);
00182 if ( it != vrmlp->defNamesListSN.end()) {
00183 wxCommandEvent ev1 = Event::GET_EVT_SCENE_NODE_COPY(vrmlp->sceneID, it->second, parentUid);
00184 return;
00185 }
00186 wxCommandEvent ev = Event::GET_EVT_SCENE_NODE_INSERT(vrmlp->sceneID, static_cast<GeometryNode*> (sceneNode), parentUid);
00187 vrmlp->module->PostToKernel(ev);
00188
00189 DefNamesListM::iterator itm;
00190 if ( (mdef != wxT("") && (itm = vrmlp->defNamesListM.find(mdef)) != vrmlp->defNamesListM.end()) || mparam == NULL ){
00191 wxCommandEvent ev1 = Event::GET_EVT_SCENE_NODE_MATERIAL_SET(vrmlp->sceneID, nodeName, itm->second->name);
00192 vrmlp->module->PostToKernel(ev1);
00193 } else {
00194 wxCommandEvent ev0 = Event::GET_EVT_SCENE_MATERIAL_ADD(vrmlp->sceneID, mparam);
00195 vrmlp->module->PostToKernel(ev0);
00196 wxCommandEvent ev1 = Event::GET_EVT_SCENE_NODE_MATERIAL_SET(vrmlp->sceneID, nodeName, mname);
00197 vrmlp->module->PostToKernel(ev1);
00198 }
00199 DefNamesListG::iterator itg;
00200 if ( (gdef != wxT("") && (itg = vrmlp->defNamesListG.find(gdef)) != vrmlp->defNamesListG.end()) || gparam == NULL){
00201 wxCommandEvent ev3 = Event::GET_EVT_SCENE_NODE_GEOMETRY_SET(vrmlp->sceneID, nodeName, itm->second->name);
00202 vrmlp->module->PostToKernel(ev3);
00203 } else {
00204 wxCommandEvent ev2 = Event::GET_EVT_SCENE_GEOMETRY_ADD(vrmlp->sceneID, gparam);
00205 vrmlp->module->PostToKernel(ev2);
00206 wxCommandEvent ev3 = Event::GET_EVT_SCENE_NODE_GEOMETRY_SET(vrmlp->sceneID, nodeName, gname);
00207 vrmlp->module->PostToKernel(ev3);
00208 }
00209
00210 if ( nodeName != wxT("")) {
00211 vrmlp->defNamesListSN.insert(DefNamesListSN::value_type(nodeName, defName ));
00212 remove = false;
00213 }
00214 }
00215 };
00217 struct ANode : public Node {
00219 MATRIX mat;
00221 std::vector<Node*> children;
00222 typedef std::vector<Node*> Children;
00227 ANode (wxString name, SceneNode* sn, wxString defName = wxT("")):Node(name,sn,defName){
00228 }
00230 ~ANode(){
00231 if (remove) {
00232 for ( Children::iterator it = children.begin(); it != children.end(); it++)
00233 delete *it;
00234 }
00235 }
00238 void setParam(MATRIX m){
00239 mat = m;
00240 }
00243 void setParam( Node* node){
00244 children.push_back(node);
00245 }
00248 void addToScene (wxString parentUid){
00249
00250 DefNamesListSN::iterator it = vrmlp->defNamesListSN.find(defName);
00251 if ( it != vrmlp->defNamesListSN.end()) {
00252 wxCommandEvent ev1 = Event::GET_EVT_SCENE_NODE_COPY(vrmlp->sceneID, it->second, parentUid);
00253 return;
00254 }
00255
00256 wxCommandEvent ev = Event::GET_EVT_SCENE_NODE_TRANSFORM(vrmlp->sceneID, nodeName, mat);
00257 vrmlp->module->PostToKernel(ev);
00258 for ( Children::iterator it = children.begin(); it != children.end(); it++)
00259 (*it)->addToScene(nodeName);
00260 }
00261 };
00262
00263
00264 WX_DECLARE_STRING_HASH_MAP(unsigned, DefNamesListSNDindex);
00266 DefNamesListSNDindex defNamesListSNDindex;
00268 std::vector<Node*> defNamesListSND;
00269
00270 friend struct ANode;
00271 friend struct GNode;
00272 friend struct LNode;
00274 enum ERROR_MSG
00275 {
00276 EXPECTED,
00277 UNEXPECTED,
00278 VECTOR,
00279 MATERIAL
00280 };
00282 enum WARN_MSG
00283 {
00284 TRIANGULATE,
00285 DISABLED,
00286 UNSUPPORTED,
00287 UNSUPPORTED_ATTR
00288 };
00291 void logWarnMsg(WARN_MSG msg, const wxString& name);
00297 bool logErrorMsg(ERROR_MSG msg, const wxString & line, const wxChar * expected = NULL);
00301 const wxString getUniqueUid(const wxString & uid);
00304 bool externproto();
00309 bool node(const wxString & parentUid, bool just_define);
00312 bool proto();
00315 bool routeStatement();
00318 bool statement(const wxString & parentUid);
00321 bool URLList();
00325 bool parseModel( const wxString & rootUid);
00330 bool Anchor(const wxString & parentUid, bool just_define);
00339 bool Appearance(const wxString & parentUid, VRUT::Material * material, VECTOR2& translation, float& rotation, VECTOR2& scale, VECTOR2& center);
00344 bool Billboard(const wxString & parentUid, bool just_define);
00348 bool Box(GeometryTriangles* geometry );
00353 bool Collision(const wxString & parentUid, bool just_define);
00357 bool DirectionalLight(Light* light);
00363 bool ElevationGrid(const wxString & parentUid, GeometryTriangles * geometry, bool just_define);
00369 bool Extrusion(const wxString& parentUid, GeometryTriangles* geometry);
00374 bool Group(const wxString & parentUid, bool just_define);
00379 bool IndexedFaceSet(const wxString & parentUid, GeometryTriangles* geometry);
00384 bool IndexedLineSet(const wxString & parentUid, GeometryTriangles* geometry);
00388 bool Inline(const wxString & parentUid);
00392 bool ImageTexture(VRUT::Material * material);
00397 bool LOD(const wxString & parentUid, bool just_define);
00401 bool Material(VRUT::Material * material);
00405 bool PointLight(Light* light);
00410 bool PointSet(const wxString & parentUid, GeometryTriangles* geometry);
00416 bool Shape(const wxString & nodeUid, bool just_define, GNode* gnode = NULL);
00420 bool SpotLight(Light* light);
00425 bool Switch(const wxString & parentUid, bool just_define);
00431 bool Transform(const wxString & parentUid, bool just_define, ANode* anode = NULL);
00435 bool Viewpoint(MATRIX& mat, float & field);
00438 inline void skip(char z){
00439 while ( textParser->GetChar("") != z) { }
00440 return;
00441 }
00445 inline bool skip_stack(char z, char inv_z){
00446 int count = 1;
00447 char p;
00448 while ( count ) {
00449 p = textParser->GetChar("");
00450 if ( p == z) count--;
00451 if ( p == inv_z) count++;
00452 if ( textParser->IsEmpty() ) return false;
00453 }
00454 return true;
00455 }
00456
00463 bool parseFArray3(std::vector<VECTOR3>* array, const char* VRMLnode, const char* VRMLfield, bool skip = false);
00470 bool parseFArray2(std::vector<VECTOR2>* array, const char* VRMLnode, const char* VRMLfield, bool skip = false);
00476 bool parseIndices(std::vector<GeometryTriangles::Indice> * indices, size_t & count, size_t count_exp);
00483 bool parseIndicesTSL(std::vector<GeometryTriangles::Indice> * indices, size_t & count, size_t count_exp);
00487 void computeNormals(VRUT::GeometryTriangles* geometry, float & creaseAngle);
00490 float inline abs(const float& x){
00491 if ( x < 0 ) return -x;
00492 return x;
00493 }
00494
00495 public:
00501 VRMLParser(wxInputStream * _inputStream, SCENE_ID _sceneID, const wxString _scenePath, Module * _module);
00503 ~VRMLParser();
00504
00508 bool Parse(const wxString & rootUid, bool teselate, NodeUidsList* nnl);
00509 };
00510
00511 };
00512
00513
00514
00515 #endif
00516