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

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

modifs pour template vectors

File size: 9.2 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(int app_width, int app_height,
43 int scene_width, int scene_height, std::string media_path,
44 std::string xmlFile)
45 : Gui("Parser", app_width, app_height, scene_width, scene_height,
46 media_path) {
47 this->media_path = media_path;
48 xmlNode *root_element = NULL;
49 doc = xmlReadFile(xmlFile.c_str(), NULL, 0);
50 xmlDtdPtr dtd = NULL;
51 dtd = xmlParseDTD(NULL,
52 (const xmlChar *)(media_path.append("/scene.dtd").c_str()));
53
54 if (dtd == NULL) {
55 // xmlGenericError(xmlGenericErrorContext,
56 Err("Could not parse DTD scene.dtd\n");
57 } else {
58 xmlValidCtxt cvp;
59 cvp.userData = (void *)stderr;
60 cvp.error = (xmlValidityErrorFunc)fprintf;
61 cvp.warning = (xmlValidityWarningFunc)fprintf;
62 if (!xmlValidateDtd(&cvp, doc, dtd)) {
63 // xmlGenericError(xmlGenericErrorContext,
64 Err("Document does not validate against scene.dtd\n");
65 }
66 }
67
68 if (doc == NULL) {
69 Err("error: could not parse file %s\n", xmlFile.c_str());
70 } else {
71 /*
72 * Get the root element node
73 */
74 root_element = xmlDocGetRootElement(doc);
75 processElements(root_element);
76 }
77}
78
79Parser::~Parser() {
80 if (doc != NULL) {
81 /*
82 * free the document
83 */
84 xmlFreeDoc(doc);
85 ;
86 }
87 /*
88 *Free the global variables that may
89 *have been allocated by the parser.
90 */
91 xmlCleanupParser();
92}
93
94/* Recursive function that prints the XML structure */
95void Parser::processElements(xmlNode *a_node) {
96 xmlNode *cur_node = NULL;
97 for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
98 if (cur_node->type == XML_ELEMENT_NODE) {
99 if (xmlStrEqual(cur_node->name, (xmlChar *)"params")) {
100 processParams(cur_node->children);
101 } else if (xmlStrEqual(cur_node->name, (xmlChar *)"objects")) {
102 processObjects(cur_node->children);
103 } else {
104 processElements(cur_node->children);
105 }
106 }
107 }
108}
109
110void Parser::processObjects(xmlNode *a_node) {
111 xmlNode *cur_node = NULL;
112
113 for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
114 if (xmlStrEqual(cur_node->name, (xmlChar *)"mesh")) {
115 FILE *fp;
116 std::string fileName = this->media_path+"/";
117 fp = NULL;
118 fp = fopen(fileName.append((char *)xmlGetProp(
119 cur_node, (xmlChar *)"model")).c_str(),"rb");
120 if (fp != NULL) {
121 GenericObject *object =
122 new GenericObject(getGui()->getMesh((char *)xmlGetProp(cur_node, (xmlChar *)"model")));
123 object->setPosition(
124 getMeshVect(cur_node->children, (xmlChar *)"position"));
125 object->setRotation(
126 getMeshVect(cur_node->children, (xmlChar *)"rotation"));
127 object->setScale(getMeshVect(cur_node->children, (xmlChar *)"scale"));
128 } else {
129 Err("FATAL ERROR : File %s %s doesn't exist !\r\n",
130 (char *)xmlGetProp(cur_node, (xmlChar *)"model"),fileName.c_str());
131 }
132 } else if (xmlStrEqual(cur_node->name, (xmlChar *)"cylinder")) {
133 const IGeometryCreator *geo;
134 geo = getGui()->getSceneManager()->getGeometryCreator();
135 IMesh* mesh=geo->createCylinderMesh(
136 ToIrrlichtScale(atof((char *)xmlGetProp(cur_node, (xmlChar *)"radius"))),
137 ToIrrlichtScale(atof((char *)xmlGetProp(cur_node, (xmlChar *)"length"))),
138 atof((char *)xmlGetProp(cur_node, (xmlChar *)"tesselation")),
139 getScolor(cur_node->children));
140 //SColor(100, 255, 100, 100));
141 GenericObject *object = new GenericObject(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 =new GenericObject(mesh);
154 object->setPosition(
155 getMeshVect(cur_node->children, (xmlChar *)"position"));
156 object->setRotation(
157 getMeshVect(cur_node->children, (xmlChar *)"rotation"));
158 object->setScale(getMeshVect(cur_node->children, (xmlChar *)"scale"));
159 }
160 }
161}
162
163void Parser::processParams(xmlNode *a_node) {
164
165 xmlNode *cur_node = NULL;
166 for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
167 if (xmlStrEqual(cur_node->name, (xmlChar *)"archive")) {
168 std::string file =
169 media_path + "/" + (char *)xmlGetProp(cur_node, (xmlChar *)"path");
170 getDevice()->getFileSystem()->addFileArchive(file.c_str());
171 } else if (xmlStrEqual(cur_node->name, (xmlChar *)"mesh")) {
172 setMesh((char *)xmlGetProp(cur_node, (xmlChar *)"model"),
173 getSceneVect(cur_node->children, (xmlChar *)"position"),
174 getSceneVect(cur_node->children, (xmlChar *)"rotation"),
175 getSceneVect(cur_node->children, (xmlChar *)"scale", true));
176 } else if (xmlStrEqual(cur_node->name, (xmlChar *)"camera")) {
177 FixedCamera* fixedCamera=new FixedCamera(std::string((char *)xmlGetProp(cur_node, (xmlChar *)"name")),
178 getMeshVector3D(cur_node->children, (xmlChar *)"position"),
179 getMeshVector3D(cur_node->children, (xmlChar *)"lookat"));
180 }
181 }
182}
183
184SColor Parser::getScolor(xmlNode *mesh_node) {
185 xmlNode *cur_node = NULL;
186 for (cur_node = mesh_node; cur_node; cur_node = cur_node->next) {
187 if (xmlStrEqual(cur_node->name, (xmlChar *)"color")) {
188 return SColor(atoi((char *)xmlGetProp(cur_node, (xmlChar *)"a")),
189 atoi((char *)xmlGetProp(cur_node, (xmlChar *)"r")),
190 atoi((char *)xmlGetProp(cur_node, (xmlChar *)"g")),
191 atoi((char *)xmlGetProp(cur_node, (xmlChar *)"b")));
192 }
193 }
194 return SColor(255,0, 0, 0);
195}
196
197// todo rendre un vector3D framework
198// retirer irr::core::vector3df ToIrrlichtCoordinates(irr::core::vector3df
199// vect);
200vector3df Parser::getMeshVect(xmlNode *mesh_node, xmlChar *param) {
201 xmlNode *cur_node = NULL;
202 for (cur_node = mesh_node; cur_node; cur_node = cur_node->next) {
203 if (xmlStrEqual(cur_node->name, param)) {
204 return vector3df(atof((char *)xmlGetProp(cur_node, (xmlChar *)"x")),
205 atof((char *)xmlGetProp(cur_node, (xmlChar *)"y")),
206 atof((char *)xmlGetProp(cur_node, (xmlChar *)"z")));
207 }
208 }
209 return vector3df(0, 0, 0);
210}
211
212Vector3Df Parser::getMeshVector3D(xmlNode *mesh_node, xmlChar *param) {
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 return Vector3Df(atof((char *)xmlGetProp(cur_node, (xmlChar *)"x")),
217 atof((char *)xmlGetProp(cur_node, (xmlChar *)"y")),
218 atof((char *)xmlGetProp(cur_node, (xmlChar *)"z")));
219 }
220 }
221 return Vector3Df(0, 0, 0);
222}
223
224vector3df Parser::getSceneVect(xmlNode *mesh_node, xmlChar *param,
225 bool isScale) {
226 xmlNode *cur_node = NULL;
227 for (cur_node = mesh_node; cur_node; cur_node = cur_node->next) {
228 if (xmlStrEqual(cur_node->name, param)) {
229 if (isScale) {
230 return vector3df(atof((char *)xmlGetProp(cur_node, (xmlChar *)"x")),
231 atof((char *)xmlGetProp(cur_node, (xmlChar *)"z")),
232 atof((char *)xmlGetProp(cur_node, (xmlChar *)"y")));
233 }
234 return vector3df(atof((char *)xmlGetProp(cur_node, (xmlChar *)"x")),
235 -atof((char *)xmlGetProp(cur_node, (xmlChar *)"z")),
236 atof((char *)xmlGetProp(cur_node, (xmlChar *)"y")));
237 }
238 }
239 return vector3df(0, 0, 0);
240}
241
242} // end namespace simulator
243} // end namespace flair
244#endif // GL
Note: See TracBrowser for help on using the repository browser.