source: flair-src/trunk/lib/FlairSimulator/src/Parser.cpp@ 137

Last change on this file since 137 was 134, checked in by Sanahuja Guillaume, 7 years ago

correction bugs parser

File size: 8.7 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5// created: 2013/08/02
6// filename: Parser.cpp
7//
8// author: César Richard
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: classe chargeant un XML decrivant une map
14//
15/*********************************************************************/
16#ifdef GL
17
18#include "Parser.h"
19#include "Simulator.h"
20#include "FixedCamera.h"
21#include "GenericObject.h"
22#include <vector3d.h>
23#include <Vector3D.h>
24#include <IrrlichtDevice.h>
25#include <IFileSystem.h>
26#include <IAnimatedMesh.h>
27#include <IGeometryCreator.h>
28#include <ISceneManager.h>
29
30using namespace irr;
31using namespace irr::core;
32using namespace irr::scene;
33using namespace irr::video;
34using namespace flair::core;
35using namespace flair::simulator;
36
37// todo: getMeshVect doit plutot donner point3D et euler
38// adaptr le xml et la dtd
39
40namespace flair {
41namespace simulator {
42Parser::Parser(Simulator *parent, int app_width, int app_height,
43 int scene_width, int scene_height, std::string media_path,
44 std::string xmlFile)
45 : Gui(parent, "Parser", app_width, app_height, scene_width, scene_height,
46 media_path) {
47 this->media_path = media_path;
48 this->parent = parent;
49 xmlNode *root_element = NULL;
50 doc = xmlReadFile(xmlFile.c_str(), NULL, 0);
51 xmlDtdPtr dtd = NULL;
52 dtd = xmlParseDTD(NULL,
53 (const xmlChar *)(media_path.append("/scene.dtd").c_str()));
54
55 if (dtd == NULL) {
56 // xmlGenericError(xmlGenericErrorContext,
57 Err("Could not parse DTD scene.dtd\n");
58 } else {
59 xmlValidCtxt cvp;
60 cvp.userData = (void *)stderr;
61 cvp.error = (xmlValidityErrorFunc)fprintf;
62 cvp.warning = (xmlValidityWarningFunc)fprintf;
63 if (!xmlValidateDtd(&cvp, doc, dtd)) {
64 // xmlGenericError(xmlGenericErrorContext,
65 Err("Document does not validate against scene.dtd\n");
66 }
67 }
68
69 if (doc == NULL) {
70 Err("error: could not parse file %s\n", xmlFile.c_str());
71 } else {
72 /*
73 * Get the root element node
74 */
75 root_element = xmlDocGetRootElement(doc);
76 processElements(root_element);
77 }
78}
79
80Parser::~Parser() {
81 if (doc != NULL) {
82 /*
83 * free the document
84 */
85 xmlFreeDoc(doc);
86 ;
87 }
88 /*
89 *Free the global variables that may
90 *have been allocated by the parser.
91 */
92 xmlCleanupParser();
93}
94
95/* Recursive function that prints the XML structure */
96void Parser::processElements(xmlNode *a_node) {
97 xmlNode *cur_node = NULL;
98 for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
99 if (cur_node->type == XML_ELEMENT_NODE) {
100 if (xmlStrEqual(cur_node->name, (xmlChar *)"params")) {
101 processParams(cur_node->children);
102 } else if (xmlStrEqual(cur_node->name, (xmlChar *)"objects")) {
103 processObjects(cur_node->children);
104 } else {
105 processElements(cur_node->children);
106 }
107 }
108 }
109}
110
111void Parser::processObjects(xmlNode *a_node) {
112 xmlNode *cur_node = NULL;
113
114 for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
115 if (xmlStrEqual(cur_node->name, (xmlChar *)"mesh")) {
116 FILE *fp;
117 std::string fileName = this->media_path+"/";
118 fp = NULL;
119 fp = fopen(fileName.append((char *)xmlGetProp(
120 cur_node, (xmlChar *)"model")).c_str(),"rb");
121 if (fp != NULL) {
122 GenericObject *object =
123 new GenericObject(parent, getGui()->getMesh((char *)xmlGetProp(cur_node, (xmlChar *)"model")));
124 object->setPosition(
125 getMeshVect(cur_node->children, (xmlChar *)"position"));
126 object->setRotation(
127 getMeshVect(cur_node->children, (xmlChar *)"rotation"));
128 object->setScale(getMeshVect(cur_node->children, (xmlChar *)"scale"));
129 } else {
130 Err("FATAL ERROR : File %s %s doesn't exist !\r\n",
131 (char *)xmlGetProp(cur_node, (xmlChar *)"model"),fileName.c_str());
132 }
133 } else if (xmlStrEqual(cur_node->name, (xmlChar *)"cylinder")) {
134 const IGeometryCreator *geo;
135 geo = getGui()->getSceneManager()->getGeometryCreator();
136 IMesh* mesh=geo->createCylinderMesh(
137 ToIrrlichtScale(atof((char *)xmlGetProp(cur_node, (xmlChar *)"radius"))),
138 ToIrrlichtScale(atof((char *)xmlGetProp(cur_node, (xmlChar *)"length"))),
139 atof((char *)xmlGetProp(cur_node, (xmlChar *)"tesselation")),
140 SColor(100, 255, 100, 100));
141 GenericObject *object = new GenericObject(parent, mesh);
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 } else if (xmlStrEqual(cur_node->name, (xmlChar *)"eight")) {
146 const IGeometryCreator *geo;
147 geo = getGui()->getSceneManager()->getGeometryCreator();
148 IMesh* mesh=geo->createCubeMesh(
149 vector3df(atof((char *)xmlGetProp(cur_node, (xmlChar *)"length")),
150 atof((char *)xmlGetProp(cur_node, (xmlChar *)"width")),
151 atof((char *)xmlGetProp(cur_node, (xmlChar *)"eight"))));
152
153 GenericObject *object =
154 new GenericObject(parent, mesh);
155 object->setPosition(
156 getMeshVect(cur_node->children, (xmlChar *)"position"));
157 object->setRotation(
158 getMeshVect(cur_node->children, (xmlChar *)"rotation"));
159 object->setScale(getMeshVect(cur_node->children, (xmlChar *)"scale"));
160 }
161 }
162}
163
164void Parser::processParams(xmlNode *a_node) {
165
166 xmlNode *cur_node = NULL;
167 for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
168 if (xmlStrEqual(cur_node->name, (xmlChar *)"archive")) {
169 std::string file =
170 media_path + "/" + (char *)xmlGetProp(cur_node, (xmlChar *)"path");
171 getDevice()->getFileSystem()->addFileArchive(file.c_str());
172 } else if (xmlStrEqual(cur_node->name, (xmlChar *)"mesh")) {
173 setMesh((char *)xmlGetProp(cur_node, (xmlChar *)"model"),
174 getSceneVect(cur_node->children, (xmlChar *)"position"),
175 getSceneVect(cur_node->children, (xmlChar *)"rotation"),
176 getSceneVect(cur_node->children, (xmlChar *)"scale", true));
177 } else if (xmlStrEqual(cur_node->name, (xmlChar *)"camera")) {
178 FixedCamera* fixedCamera=new FixedCamera(std::string((char *)xmlGetProp(cur_node, (xmlChar *)"name")),
179 getMeshVector3D(cur_node->children, (xmlChar *)"position"),
180 getMeshVector3D(cur_node->children, (xmlChar *)"lookat"));
181 }
182 }
183}
184// todo rendre un vector3D framework
185// retirer irr::core::vector3df ToIrrlichtCoordinates(irr::core::vector3df
186// vect);
187vector3df Parser::getMeshVect(xmlNode *mesh_node, xmlChar *param) {
188 xmlNode *cur_node = NULL;
189 for (cur_node = mesh_node; cur_node; cur_node = cur_node->next) {
190 if (xmlStrEqual(cur_node->name, param)) {
191 return vector3df(atof((char *)xmlGetProp(cur_node, (xmlChar *)"x")),
192 atof((char *)xmlGetProp(cur_node, (xmlChar *)"y")),
193 atof((char *)xmlGetProp(cur_node, (xmlChar *)"z")));
194 }
195 }
196 return vector3df(0, 0, 0);
197}
198
199Vector3D Parser::getMeshVector3D(xmlNode *mesh_node, xmlChar *param) {
200 xmlNode *cur_node = NULL;
201 for (cur_node = mesh_node; cur_node; cur_node = cur_node->next) {
202 if (xmlStrEqual(cur_node->name, param)) {
203 return Vector3D(atof((char *)xmlGetProp(cur_node, (xmlChar *)"x")),
204 atof((char *)xmlGetProp(cur_node, (xmlChar *)"y")),
205 atof((char *)xmlGetProp(cur_node, (xmlChar *)"z")));
206 }
207 }
208 return Vector3D(0, 0, 0);
209}
210
211vector3df Parser::getSceneVect(xmlNode *mesh_node, xmlChar *param,
212 bool isScale) {
213 xmlNode *cur_node = NULL;
214 for (cur_node = mesh_node; cur_node; cur_node = cur_node->next) {
215 if (xmlStrEqual(cur_node->name, param)) {
216 if (isScale) {
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 return vector3df(atof((char *)xmlGetProp(cur_node, (xmlChar *)"x")),
222 -atof((char *)xmlGetProp(cur_node, (xmlChar *)"z")),
223 atof((char *)xmlGetProp(cur_node, (xmlChar *)"y")));
224 }
225 }
226 return vector3df(0, 0, 0);
227}
228
229} // end namespace simulator
230} // end namespace flair
231#endif // GL
Note: See TracBrowser for help on using the repository browser.