Changeset 15 in flair-src for trunk/lib/FlairSimulator/src/Parser.cpp


Ignore:
Timestamp:
04/08/16 15:40:57 (8 years ago)
Author:
Bayard Gildas
Message:

sources reformatted with flair-format-dir script

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/FlairSimulator/src/Parser.cpp

    r10 r15  
    2323#include "GenericObject.h"
    2424
    25 
    2625using namespace irr;
    2726using namespace irr::core;
     
    3130using namespace flair::simulator;
    3231
    33 //todo: getMeshVect doit plutot donner point3D et euler
    34 //adaptr le xml et la dtd
    35 
    36 namespace flair
    37 {
    38 namespace simulator
    39 {
    40 Parser::Parser(Simulator* parent,int app_width, int app_height,int scene_width, int scene_height,std::string media_path, std::string xmlFile): Gui(parent,"Parser",app_width,app_height,scene_width,scene_height, media_path)
    41 {
    42         this->media_path = media_path;
    43         this->parent = parent;
    44         xmlNode        *root_element = NULL;
    45         doc = xmlReadFile(xmlFile.c_str(), NULL, 0);
    46         xmlDtdPtr dtd = NULL;
    47         dtd = xmlParseDTD(NULL,  (const xmlChar *)(media_path.append("/scene.dtd").c_str()));
    48 
    49         if (dtd == NULL)
    50         {
    51                 //xmlGenericError(xmlGenericErrorContext,
    52                 Err("Could not parse DTD scene.dtd\n");
    53         }
    54         else
    55         {
    56                 xmlValidCtxt cvp;
    57                 cvp.userData = (void *) stderr;
    58                 cvp.error    = (xmlValidityErrorFunc) fprintf;
    59                 cvp.warning  = (xmlValidityWarningFunc) fprintf;
    60                 if (!xmlValidateDtd(&cvp, doc, dtd))
    61                 {
    62                         //xmlGenericError(xmlGenericErrorContext,
    63                         Err("Document does not validate against scene.dtd\n");
    64                 }
    65         }
    66 
    67         if (doc == NULL)
    68         {
    69                 Err("error: could not parse file %s\n", xmlFile.c_str());
    70         }
    71         else
    72         {
    73                 /*
    74                  * Get the root element node
    75                  */
    76                 root_element = xmlDocGetRootElement(doc);
    77                 processElements(root_element);
    78     }
    79 
    80 }
    81 
    82 Parser::~Parser()
    83 {
    84         if(doc!=NULL)
    85         {
    86                 /*
    87                  * free the document
    88                  */
    89                 xmlFreeDoc(doc);;
    90         }
    91         /*
    92          *Free the global variables that may
    93          *have been allocated by the parser.
    94          */
    95         xmlCleanupParser();
     32// todo: getMeshVect doit plutot donner point3D et euler
     33// adaptr le xml et la dtd
     34
     35namespace flair {
     36namespace simulator {
     37Parser::Parser(Simulator *parent, int app_width, int app_height,
     38               int scene_width, int scene_height, std::string media_path,
     39               std::string xmlFile)
     40    : Gui(parent, "Parser", app_width, app_height, scene_width, scene_height,
     41          media_path) {
     42  this->media_path = media_path;
     43  this->parent = parent;
     44  xmlNode *root_element = NULL;
     45  doc = xmlReadFile(xmlFile.c_str(), NULL, 0);
     46  xmlDtdPtr dtd = NULL;
     47  dtd = xmlParseDTD(NULL,
     48                    (const xmlChar *)(media_path.append("/scene.dtd").c_str()));
     49
     50  if (dtd == NULL) {
     51    // xmlGenericError(xmlGenericErrorContext,
     52    Err("Could not parse DTD scene.dtd\n");
     53  } else {
     54    xmlValidCtxt cvp;
     55    cvp.userData = (void *)stderr;
     56    cvp.error = (xmlValidityErrorFunc)fprintf;
     57    cvp.warning = (xmlValidityWarningFunc)fprintf;
     58    if (!xmlValidateDtd(&cvp, doc, dtd)) {
     59      // xmlGenericError(xmlGenericErrorContext,
     60      Err("Document does not validate against scene.dtd\n");
     61    }
     62  }
     63
     64  if (doc == NULL) {
     65    Err("error: could not parse file %s\n", xmlFile.c_str());
     66  } else {
     67    /*
     68     * Get the root element node
     69     */
     70    root_element = xmlDocGetRootElement(doc);
     71    processElements(root_element);
     72  }
     73}
     74
     75Parser::~Parser() {
     76  if (doc != NULL) {
     77    /*
     78     * free the document
     79     */
     80    xmlFreeDoc(doc);
     81    ;
     82  }
     83  /*
     84   *Free the global variables that may
     85   *have been allocated by the parser.
     86   */
     87  xmlCleanupParser();
    9688}
    9789
    9890/* Recursive function that prints the XML structure */
    99 void Parser::processElements(xmlNode * a_node)
    100 {
    101         xmlNode *cur_node = NULL;
    102         for (cur_node = a_node; cur_node; cur_node = cur_node->next)
    103         {
    104                 if(cur_node->type == XML_ELEMENT_NODE)
    105                 {
    106                         if(xmlStrEqual(cur_node->name, (xmlChar*) "params"))
    107                         {
    108                                 processParams(cur_node->children);
    109                         }
    110                         else if(xmlStrEqual(cur_node->name, (xmlChar*) "objects"))
    111                         {
    112                                 processObjects(cur_node->children);
    113                         }
    114                         else
    115                         {
    116                                 processElements(cur_node->children);
    117                         }
    118                 }
    119         }
    120 }
    121 
    122 void Parser::processObjects(xmlNode * a_node)
    123 {
    124         FILE* fp;
    125         std::string fileName;
    126         xmlNode *cur_node = NULL;
    127 
    128         const IGeometryCreator *geo;
    129 
    130     geo=getGui()->getSceneManager()->getGeometryCreator();
    131 
    132         for (cur_node = a_node; cur_node; cur_node = cur_node->next)
    133         {
    134                 if(xmlStrEqual(cur_node->name, (xmlChar*) "mesh"))
    135                 {
    136                         fileName=this->media_path;
    137                         fp = NULL;
    138                         fp = fopen( fileName.append((char*)xmlGetProp(cur_node,(xmlChar*)"model")).c_str(), "rb" );
    139                         if(fp!=NULL){
    140                                 GenericObject* object = new GenericObject(parent,"Object", getSceneManager());
    141                                 object->setMesh(getGui()->getMesh((char*)xmlGetProp(cur_node,(xmlChar*)"model")));
    142                                 object->setPosition(getMeshVect(cur_node->children,(xmlChar*)"position"));
    143                                 object->setRotation(getMeshVect(cur_node->children,(xmlChar*)"rotation"));
    144                                 object->setScale(getMeshVect(cur_node->children,(xmlChar*)"scale"));
    145                                 object->render();
    146                         }else{
    147                                 Err("FATAL ERROR : File %s doesn't exist !\r\n",(char*)xmlGetProp(cur_node,(xmlChar*)"model"));
    148                         }
    149                 }else if(xmlStrEqual(cur_node->name, (xmlChar*) "cylinder")){
    150             GenericObject* object = new GenericObject(parent,"Object", getSceneManager());
    151             object->setMesh(geo->createCylinderMesh(ToIrrlichtScale(atof((char*)xmlGetProp(cur_node,(xmlChar*)"radius"))),
    152                                                     ToIrrlichtScale(atof((char*)xmlGetProp(cur_node,(xmlChar*)"length"))),
    153                                                     atof((char*)xmlGetProp(cur_node,(xmlChar*)"tesselation")),
    154                                                     SColor(100, 255, 100, 100)));
    155             object->setPosition(getMeshVect(cur_node->children,(xmlChar*)"position"));
    156             object->setRotation(getMeshVect(cur_node->children,(xmlChar*)"rotation"));
    157             object->setScale(getMeshVect(cur_node->children,(xmlChar*)"scale"));
    158             //object->setMaterialTexture(0,getTexture("/home/apeiron/igep/uav_dev_svn/trunk/media/nskinbl.jpg"));
    159                     object->setMaterialType( video::EMT_SOLID );
    160                     object->render();
    161                 }else if(xmlStrEqual(cur_node->name, (xmlChar*) "eight")){
    162             GenericObject* object = new GenericObject(parent,"Object", getSceneManager());
    163             object->setMesh(geo->createCubeMesh(vector3df(atof((char*)xmlGetProp(cur_node,(xmlChar*)"length")),atof((char*)xmlGetProp(cur_node,(xmlChar*)"width")),atof((char*)xmlGetProp(cur_node,(xmlChar*)"eight")))));
    164             object->setPosition(getMeshVect(cur_node->children,(xmlChar*)"position"));
    165             object->setRotation(getMeshVect(cur_node->children,(xmlChar*)"rotation"));
    166             object->setScale(getMeshVect(cur_node->children,(xmlChar*)"scale"));
    167                     object->setMaterialType( video::EMT_TRANSPARENT_ALPHA_CHANNEL );
    168                     //object->getMaterial(0).Textures[0] = getTexture("/home/apeiron/igep/uav_dev_svn/trunk/media/nskinbl.jpg");
    169                     object->setMaterialFlag(EMF_LIGHTING, false);
    170                     object->render();
    171         }
    172         }
    173 }
    174 
    175 void Parser::processParams(xmlNode * a_node)
    176 {
    177 
    178         xmlNode *cur_node = NULL;
    179         for (cur_node = a_node; cur_node; cur_node = cur_node->next)
    180         {
    181                 if(xmlStrEqual(cur_node->name, (xmlChar*) "archive"))
    182                 {
    183                         std::string file=media_path + "/" + (char*)xmlGetProp(cur_node,(xmlChar*)"path");
    184                         getDevice()->getFileSystem()->addFileArchive(file.c_str());
    185                 }
    186                 else if(xmlStrEqual(cur_node->name, (xmlChar*) "mesh"))
    187                 {
    188                         setMesh((char*)xmlGetProp(cur_node,(xmlChar*)"model"),getSceneVect(cur_node->children,(xmlChar*)"position"),getSceneVect(cur_node->children,(xmlChar*)"rotation"),getSceneVect(cur_node->children,(xmlChar*)"scale",true));
    189 
    190                 }
    191         }
    192 }
    193 //todo rendre un vector3D framework
    194 //retirer irr::core::vector3df ToIrrlichtCoordinates(irr::core::vector3df vect);
    195 vector3df Parser::getMeshVect(xmlNode * mesh_node, xmlChar * param)
    196 {
    197         xmlNode *cur_node = NULL;
    198         for (cur_node = mesh_node; cur_node; cur_node = cur_node->next)
    199         {
    200                 if(xmlStrEqual(cur_node->name, param)){
    201                         return vector3df(atof((char*)xmlGetProp(cur_node,(xmlChar*)"x")),atof((char*)xmlGetProp(cur_node,(xmlChar*)"y")),atof((char*)xmlGetProp(cur_node,(xmlChar*)"z")));
    202                 }
    203         }
    204         return vector3df(0,0,0);
    205 }
    206 
    207 vector3df Parser::getSceneVect(xmlNode * mesh_node, xmlChar * param, bool isScale)
    208 {
    209         xmlNode *cur_node = NULL;
    210         for (cur_node = mesh_node; cur_node; cur_node = cur_node->next)
    211         {
    212                 if(xmlStrEqual(cur_node->name, param)){
    213                         if(isScale){
    214                                 return vector3df(atof((char*)xmlGetProp(cur_node,(xmlChar*)"x")),atof((char*)xmlGetProp(cur_node,(xmlChar*)"z")),atof((char*)xmlGetProp(cur_node,(xmlChar*)"y")));
    215                         }
    216                         return vector3df(atof((char*)xmlGetProp(cur_node,(xmlChar*)"x")),-atof((char*)xmlGetProp(cur_node,(xmlChar*)"z")),atof((char*)xmlGetProp(cur_node,(xmlChar*)"y")));
    217                 }
    218         }
    219         return vector3df(0,0,0);
     91void Parser::processElements(xmlNode *a_node) {
     92  xmlNode *cur_node = NULL;
     93  for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
     94    if (cur_node->type == XML_ELEMENT_NODE) {
     95      if (xmlStrEqual(cur_node->name, (xmlChar *)"params")) {
     96        processParams(cur_node->children);
     97      } else if (xmlStrEqual(cur_node->name, (xmlChar *)"objects")) {
     98        processObjects(cur_node->children);
     99      } else {
     100        processElements(cur_node->children);
     101      }
     102    }
     103  }
     104}
     105
     106void Parser::processObjects(xmlNode *a_node) {
     107  FILE *fp;
     108  std::string fileName;
     109  xmlNode *cur_node = NULL;
     110
     111  const IGeometryCreator *geo;
     112
     113  geo = getGui()->getSceneManager()->getGeometryCreator();
     114
     115  for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
     116    if (xmlStrEqual(cur_node->name, (xmlChar *)"mesh")) {
     117      fileName = this->media_path;
     118      fp = NULL;
     119      fp = fopen(fileName.append((char *)xmlGetProp(
     120                                     cur_node, (xmlChar *)"model")).c_str(),
     121                 "rb");
     122      if (fp != NULL) {
     123        GenericObject *object =
     124            new GenericObject(parent, "Object", getSceneManager());
     125        object->setMesh(getGui()->getMesh(
     126            (char *)xmlGetProp(cur_node, (xmlChar *)"model")));
     127        object->setPosition(
     128            getMeshVect(cur_node->children, (xmlChar *)"position"));
     129        object->setRotation(
     130            getMeshVect(cur_node->children, (xmlChar *)"rotation"));
     131        object->setScale(getMeshVect(cur_node->children, (xmlChar *)"scale"));
     132        object->render();
     133      } else {
     134        Err("FATAL ERROR : File %s doesn't exist !\r\n",
     135            (char *)xmlGetProp(cur_node, (xmlChar *)"model"));
     136      }
     137    } else if (xmlStrEqual(cur_node->name, (xmlChar *)"cylinder")) {
     138      GenericObject *object =
     139          new GenericObject(parent, "Object", getSceneManager());
     140      object->setMesh(geo->createCylinderMesh(
     141          ToIrrlichtScale(
     142              atof((char *)xmlGetProp(cur_node, (xmlChar *)"radius"))),
     143          ToIrrlichtScale(
     144              atof((char *)xmlGetProp(cur_node, (xmlChar *)"length"))),
     145          atof((char *)xmlGetProp(cur_node, (xmlChar *)"tesselation")),
     146          SColor(100, 255, 100, 100)));
     147      object->setPosition(
     148          getMeshVect(cur_node->children, (xmlChar *)"position"));
     149      object->setRotation(
     150          getMeshVect(cur_node->children, (xmlChar *)"rotation"));
     151      object->setScale(getMeshVect(cur_node->children, (xmlChar *)"scale"));
     152      // object->setMaterialTexture(0,getTexture("/home/apeiron/igep/uav_dev_svn/trunk/media/nskinbl.jpg"));
     153      object->setMaterialType(video::EMT_SOLID);
     154      object->render();
     155    } else if (xmlStrEqual(cur_node->name, (xmlChar *)"eight")) {
     156      GenericObject *object =
     157          new GenericObject(parent, "Object", getSceneManager());
     158      object->setMesh(geo->createCubeMesh(
     159          vector3df(atof((char *)xmlGetProp(cur_node, (xmlChar *)"length")),
     160                    atof((char *)xmlGetProp(cur_node, (xmlChar *)"width")),
     161                    atof((char *)xmlGetProp(cur_node, (xmlChar *)"eight")))));
     162      object->setPosition(
     163          getMeshVect(cur_node->children, (xmlChar *)"position"));
     164      object->setRotation(
     165          getMeshVect(cur_node->children, (xmlChar *)"rotation"));
     166      object->setScale(getMeshVect(cur_node->children, (xmlChar *)"scale"));
     167      object->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL);
     168      // object->getMaterial(0).Textures[0] =
     169      // getTexture("/home/apeiron/igep/uav_dev_svn/trunk/media/nskinbl.jpg");
     170      object->setMaterialFlag(EMF_LIGHTING, false);
     171      object->render();
     172    }
     173  }
     174}
     175
     176void Parser::processParams(xmlNode *a_node) {
     177
     178  xmlNode *cur_node = NULL;
     179  for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
     180    if (xmlStrEqual(cur_node->name, (xmlChar *)"archive")) {
     181      std::string file =
     182          media_path + "/" + (char *)xmlGetProp(cur_node, (xmlChar *)"path");
     183      getDevice()->getFileSystem()->addFileArchive(file.c_str());
     184    } else if (xmlStrEqual(cur_node->name, (xmlChar *)"mesh")) {
     185      setMesh((char *)xmlGetProp(cur_node, (xmlChar *)"model"),
     186              getSceneVect(cur_node->children, (xmlChar *)"position"),
     187              getSceneVect(cur_node->children, (xmlChar *)"rotation"),
     188              getSceneVect(cur_node->children, (xmlChar *)"scale", true));
     189    }
     190  }
     191}
     192// todo rendre un vector3D framework
     193// retirer irr::core::vector3df ToIrrlichtCoordinates(irr::core::vector3df
     194// vect);
     195vector3df Parser::getMeshVect(xmlNode *mesh_node, xmlChar *param) {
     196  xmlNode *cur_node = NULL;
     197  for (cur_node = mesh_node; cur_node; cur_node = cur_node->next) {
     198    if (xmlStrEqual(cur_node->name, param)) {
     199      return vector3df(atof((char *)xmlGetProp(cur_node, (xmlChar *)"x")),
     200                       atof((char *)xmlGetProp(cur_node, (xmlChar *)"y")),
     201                       atof((char *)xmlGetProp(cur_node, (xmlChar *)"z")));
     202    }
     203  }
     204  return vector3df(0, 0, 0);
     205}
     206
     207vector3df Parser::getSceneVect(xmlNode *mesh_node, xmlChar *param,
     208                               bool isScale) {
     209  xmlNode *cur_node = NULL;
     210  for (cur_node = mesh_node; cur_node; cur_node = cur_node->next) {
     211    if (xmlStrEqual(cur_node->name, param)) {
     212      if (isScale) {
     213        return vector3df(atof((char *)xmlGetProp(cur_node, (xmlChar *)"x")),
     214                         atof((char *)xmlGetProp(cur_node, (xmlChar *)"z")),
     215                         atof((char *)xmlGetProp(cur_node, (xmlChar *)"y")));
     216      }
     217      return vector3df(atof((char *)xmlGetProp(cur_node, (xmlChar *)"x")),
     218                       -atof((char *)xmlGetProp(cur_node, (xmlChar *)"z")),
     219                       atof((char *)xmlGetProp(cur_node, (xmlChar *)"y")));
     220    }
     221  }
     222  return vector3df(0, 0, 0);
    220223}
    221224
    222225} // end namespace simulator
    223226} // end namespace flair
    224 #endif //GL
     227#endif // GL
Note: See TracChangeset for help on using the changeset viewer.