source: flair-src/branches/sanscv/lib/FlairSimulator/src/Parser.cpp@ 328

Last change on this file since 328 was 324, checked in by Sanahuja Guillaume, 5 years ago

removing opencv dependency

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