V-ART
xmlscene.cpp
Go to the documentation of this file.
1 
5 #include "vart/xmlscene.h"
6 #include "vart/meshobject.h"
7 #include "vart/dof.h"
8 #include "vart/sphere.h"
9 #include "vart/cylinder.h"
10 #include "vart/biaxialjoint.h"
11 #include "vart/polyaxialjoint.h"
12 #include "vart/uniaxialjoint.h"
13 #include "vart/file.h"
14 #include <xercesc/dom/DOMNodeList.hpp>
15 #include <xercesc/dom/DOMNamedNodeMap.hpp>
16 #include <sstream>
17 
18 using XERCES_CPP_NAMESPACE::DOMNodeList;
19 using XERCES_CPP_NAMESPACE::DOMNode;
20 using XERCES_CPP_NAMESPACE::DOMNamedNodeMap;
21 using namespace std;
22 
24 {
25 }
26 
27 bool VART::XmlScene::LoadFromFile(const std::string& fileName)
28 {
29  meshMap::iterator iterMesh;
30  meshObjMap::iterator iterMeshObj;
31 
32  if (ParseFile(fileName))
33  { //XML parser sucessfull
34  //~ istringstream stream;
35  //...load the elements of the scene,
36  bool result = LoadScene( VART::File::GetPathFromString(fileName) );
37  // Terminate Xerces
38  Terminate();
39  // ...delete unused mesh objects and clear the maps.
40  for(iterMesh = mapMesh.begin(); iterMesh != mapMesh.end(); ++iterMesh)
41  {
42  for(iterMeshObj = mapMeshObj.begin(); iterMeshObj != mapMeshObj.end(); ++iterMeshObj)
43  delete iterMeshObj->second;
44  mapMeshObj.clear();
45  }
46  mapMesh.clear();
47  return result;
48  }
49 
50  else
51  { //the parser has failled!
52  Terminate();
53  return false;
54  }
55 }
56 
57 bool VART::XmlScene::LoadScene(const std::string& basePath)
58 // File contents come from xerces' structure at documentPtr. The "basePath" is used to fill in
59 // relative names in the XML file.
60 {
61  VART::SceneNode* nodePtr;
62  DOMNodeList* sceneList = documentPtr->getElementsByTagName(XercesString("scene"));
63  unsigned int j;
64 
65  // The scene can have cameras and sceneNodes (lights and other objects).
66  for (unsigned int i = 0; i < sceneList->getLength(); i++)
67  {
68  DOMNodeList* nodesL = sceneList->item(i)->getChildNodes();
69  for(j = 0; j<nodesL->getLength(); j++)
70  {
71  if (TempCString(nodesL->item(j)->getNodeName()) == "camera")
72  { // FixMe: a LoadCamera method must be implemented
73  }
74  else if (TempCString(nodesL->item(j)->getNodeName()) == "node")
75  {
76  // Recursively reads a scene node
77  nodePtr = LoadSceneNode(nodesL->item(j), basePath);
78  if (nodePtr == NULL)
79  return false;
80 
81  // Is it a light?
82  VART::Light* lightPtr = dynamic_cast<VART::Light*>(nodePtr);
83  if (lightPtr)
84  AddLight(lightPtr);
85  else
86  {
87  AddObject(nodePtr);
88  }
89  }
90  }
91  }
92  return true;
93 }
94 
95 VART::MeshObject* VART::XmlScene::LoadMeshFromFile(string filen, string type, string meshName)
96 {
97  meshMap::iterator iterMesh;
98  meshObjMap::iterator iterMeshObj;
99  list<VART::MeshObject*> meshObjectList;
100  list<VART::MeshObject*>::iterator iter;
101  VART::MeshObject* finalMesh;
102  VART::MeshObject* result = NULL;
103  finalMesh = new VART::MeshObject;
104  finalMesh->autoDelete = true;
105  iterMesh = mapMesh.find(filen);
106  int i=0;
107 
108  if(iterMesh != mapMesh.end())
109  {//The file has been loaded
110  iterMeshObj = mapMeshObj.find(meshName);
111  if(iterMeshObj != mapMeshObj.end())
112  {//The mesh has been loaded
113  i++;
114  finalMesh = iterMeshObj->second;
115  mapMeshObj.erase(iterMeshObj);
116  return finalMesh;
117  }
118 
119  else
120  {
121  cerr<<"Error! Mesh "<<meshName<<" not found in file "<<filen<<endl;
122  return NULL;
123  }
124  }
125 
126  else
127  {
128  //The file hasn't been loaded
129  if(type == "obj")
130  {
131  meshObjectList.clear();
132  VART::MeshObject::ReadFromOBJ(filen, &meshObjectList);
133  for (iter = meshObjectList.begin(); iter != meshObjectList.end(); ++iter)
134  {
135  if ((*iter)->GetDescription() == meshName)
136  {
137  result=*iter;
138  }
139  else
140  {
141  finalMesh = *iter;
142  mapMeshObj[finalMesh->GetDescription()] = finalMesh;
143  }
144  }
145  mapMesh[filen] = mapMeshObj;
146  if (result == NULL)
147  cerr << "Error: No mesh object named " << meshName << " was found!" << endl;
148  //result->bBoxShow = true;
149  return result;
150  }
151  else
152  {
153  //FixMe. Must load others types of file.
154  }
155  //result->bBoxShow = true;
156  return result;
157  }
158 }
159 
160 VART::SceneNode* VART::XmlScene::LoadSceneNode(DOMNode* sceneList, const std::string& basePath)
161 {
162  istringstream stream;
163  int numChildren;
164  int currentChild = 1;
165  VART::SceneNode* result;
166  int rTemp, gTemp, bTemp, aTemp;
167  string descrStr;
168  DOMNodeList* listNode = sceneList->getChildNodes();
169  numChildren = listNode->getLength();
170 
171  //The sceneNodes can be (joints, tranformations, geometry, lights, etc.)
172 
173  if (TempCString(listNode->item(1)->getNodeName()) == "bezier")
174  { // FixMe: this part must be implemented
175  }
176  else if (TempCString(listNode->item(1)->getNodeName()) == "sphere")
177  {
178  VART::Sphere* spherePtr = new VART::Sphere;
179  float radius;
180  unsigned int i;
181  DOMNodeList* childNodes = listNode->item(1)->getChildNodes();
182  DOMNamedNodeMap* attrAux;
183 
184  spherePtr->autoDelete=true;
185  //The sphere is defined by a radius and a material.
186  for(i = 0; i<childNodes->getLength(); i++)
187  {
188  if (TempCString(childNodes->item(i)->getNodeName()) == "radius")
189  {
190  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
191  TempCString radiusStr(attrPt->getNamedItem(XercesString("value"))->getNodeValue());
192 
193  radiusStr.GetValue(&radius);
194  spherePtr->SetRadius(radius);
195  }
196  else if (TempCString(childNodes->item(i)->getNodeName()) == "material")
197  {
198  unsigned char r, g, b;
199  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
200  TempCString redStr(attrPt->getNamedItem(XercesString("r"))->getNodeValue());
201  TempCString greenStr(attrPt->getNamedItem(XercesString("g"))->getNodeValue());
202  TempCString blueStr(attrPt->getNamedItem(XercesString("b"))->getNodeValue());
203 
204  redStr.GetValue(&rTemp);
205  r = static_cast<unsigned char>(rTemp);
206  greenStr.GetValue(&gTemp);
207  g = static_cast<unsigned char>(gTemp);
208  blueStr.GetValue(&bTemp);
209  b = static_cast<unsigned char>(bTemp);
210  spherePtr->SetMaterial(Material(Color(r,g,b)));
211  }
212  }
213  attrAux = listNode->item(1)->getAttributes();
214  descrStr = TempCString(attrAux->getNamedItem(XercesString("description"))->getNodeValue());
215  spherePtr->SetDescription(descrStr);
216  result = spherePtr;
217  }
218 
219  else if (TempCString(listNode->item(1)->getNodeName()) == "cylinder")
220  {
221  //The cylinder is defined by a radius, a height and a material.
222  VART::Cylinder* cylinderPtr = new VART::Cylinder;
223  float radius;
224  float height;
225  unsigned int i;
226  DOMNodeList* childNodes = listNode->item(1)->getChildNodes();
227  DOMNamedNodeMap* attrAux;
228 
229  cylinderPtr->autoDelete=true;
230 
231  for(i = 0; i<childNodes->getLength(); i++)
232  {
233  if (TempCString(childNodes->item(i)->getNodeName()) == "radius")
234  {
235  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
236  TempCString radiusStr(attrPt->getNamedItem(XercesString("value"))->getNodeValue());
237 
238  radiusStr.GetValue(&radius);
239  cylinderPtr->SetRadius(radius);
240  }
241  else if (TempCString(childNodes->item(i)->getNodeName()) == "height")
242  {
243  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
244  TempCString heightStr(attrPt->getNamedItem(XercesString("value"))->getNodeValue());
245 
246  heightStr.GetValue(&height);
247  cylinderPtr->SetHeight(height);
248  }
249  else if (TempCString(childNodes->item(i)->getNodeName()) == "material")
250  {
251  unsigned char r, g, b;
252  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
253  TempCString redStr(attrPt->getNamedItem(XercesString("r"))->getNodeValue());
254  TempCString greenStr(attrPt->getNamedItem(XercesString("g"))->getNodeValue());
255  TempCString blueStr(attrPt->getNamedItem(XercesString("b"))->getNodeValue());
256 
257  redStr.GetValue(&rTemp);
258  r = static_cast<unsigned char>(rTemp);
259  greenStr.GetValue(&gTemp);
260  g = static_cast<unsigned char>(gTemp);
261  blueStr.GetValue(&bTemp);
262  b = static_cast<unsigned char>(bTemp);
263  cylinderPtr->SetMaterial(Material(Color(r,g,b)));
264  }
265  }
266  attrAux = listNode->item(1)->getAttributes();
267  descrStr = TempCString(attrAux->getNamedItem(XercesString("description"))->getNodeValue());
268  cylinderPtr->SetDescription(descrStr);
269  result = cylinderPtr;
270  }
271 
272  else if (TempCString(listNode->item(1)->getNodeName()) == "meshobject")
273  {
274  // The mesh object can have a material associated and is defined by a file (for example...
275  // a Wavefront file).
276  list<VART::MeshObject*> meshObjectList;
277  list<VART::MeshObject*>::iterator iter;
278  string file_name;
279  string obj_name;
280  string typeName;
281  unsigned int i;
282  unsigned int j;
283  DOMNodeList* childNodes = listNode->item(1)->getChildNodes();
284  DOMNamedNodeMap* attrAux = listNode->item(1)->getAttributes();
285 
286  for (i = 0; i<childNodes->getLength(); i++)
287  {
288  if (TempCString(childNodes->item(i)->getNodeName()) == "material")
289  {
290  }
291  }
292  for (j = 0; j<attrAux->getLength(); j++)
293  {
294  if (TempCString(attrAux->item(j)->getNodeName()) == "filename")
295  file_name = TempCString(attrAux->getNamedItem(XercesString("filename"))->getNodeValue());
296 
297  else if (TempCString(attrAux->item(j)->getNodeName()) == "description")
298  {
299  obj_name = TempCString(attrAux->getNamedItem(XercesString("description"))->getNodeValue());
300  }
301 
302  else if (TempCString(attrAux->item(j)->getNodeName()) == "type")
303  {
304  typeName = TempCString(attrAux->getNamedItem(XercesString("type"))->getNodeValue());
305  }
306  }
307  result = LoadMeshFromFile(basePath+file_name, typeName, obj_name);
308  }
309 
310  else if (TempCString(listNode->item(1)->getNodeName()) == "directionallight")
311  {
312  VART::Light* directionallightPtr = new VART::Light;
313  //~ VART::Color* color;
314  //~ VART::Point4D* location;
315  unsigned int i;
316  float intensity;
317  float ambientIntensity;
318  float xPos;
319  float yPos;
320  float zPos;
321  //~ bool enabled;
322  unsigned char r; // red
323  unsigned char g; // green
324  unsigned char b; // blue
325  unsigned char a; // alpha
326  istringstream stream;
327  DOMNodeList* childNodes = listNode->item(1)->getChildNodes();
328  DOMNamedNodeMap* attrTemp = listNode->item(1)->getAttributes();
329 
330  //~ location = new VART::Point4D;
331  //~ color = new VART::Color;
332  directionallightPtr->autoDelete=true;
333 
334  for (i = 0; i<childNodes->getLength(); i++)
335  {
336  if (TempCString(childNodes->item(i)->getNodeName()) == "intensity")
337  {
338  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
339  TempCString intensityStr(attrPt->getNamedItem(XercesString("value"))->getNodeValue());
340 
341  intensityStr.GetValue(&intensity);
342  directionallightPtr->SetIntensity(intensity);
343  }
344 
345  else if (TempCString(childNodes->item(i)->getNodeName()) == "ambientIntensity")
346  {
347  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
348  TempCString aiStr(attrPt->getNamedItem(XercesString("value"))->getNodeValue());
349 
350  aiStr.GetValue(&ambientIntensity);
351  directionallightPtr->SetAmbientIntensity(ambientIntensity);
352  }
353 
354  else if (TempCString(childNodes->item(i)->getNodeName()) == "color")
355  {
356  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
357  TempCString redStr(attrPt->getNamedItem(XercesString("red"))->getNodeValue());
358  TempCString greenStr(attrPt->getNamedItem(XercesString("green"))->getNodeValue());
359  TempCString blueStr(attrPt->getNamedItem(XercesString("blue"))->getNodeValue());
360  TempCString alphaStr(attrPt->getNamedItem(XercesString("alpha"))->getNodeValue());
361 
362  redStr.GetValue(&rTemp);
363  r = static_cast<unsigned char>(rTemp);
364  greenStr.GetValue(&gTemp);
365  g = static_cast<unsigned char>(gTemp);
366  blueStr.GetValue(&bTemp);
367  b = static_cast<unsigned char>(bTemp);
368  alphaStr.GetValue(&aTemp);
369  a = static_cast<unsigned char>(aTemp);
370 
371  directionallightPtr->SetColor(Color(r,g,b,a));
372  }
373 
374  else if (TempCString(childNodes->item(i)->getNodeName()) == "enabled")
375  {
376  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
377  TempCString boolValue(attrPt->getNamedItem(XercesString("value"))->getNodeValue());
378 
379  if (boolValue == "true")
380  directionallightPtr->Turn(true);
381  else
382  {
383  if (boolValue == "false")
384  directionallightPtr->Turn(false);
385  else
386  {
387  cerr << "XmlScene::LoadSceneNode: Error at 'directionallight' attribute: "
388  << "unknown value for 'enabled': '" << boolValue
389  << "'. Assuming 'true'.\n";
390  directionallightPtr->Turn(true);
391  }
392  }
393  }
394 
395  else if (TempCString(childNodes->item(i)->getNodeName()) == "position")
396  {
397  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
398  TempCString xStr(attrPt->getNamedItem(XercesString("x"))->getNodeValue());
399  TempCString yStr(attrPt->getNamedItem(XercesString("y"))->getNodeValue());
400  TempCString zStr(attrPt->getNamedItem(XercesString("z"))->getNodeValue());
401 
402  xStr.GetValue(&xPos);
403  yStr.GetValue(&yPos);
404  zStr.GetValue(&zPos);
405  directionallightPtr->SetLocation(Point4D(xPos, yPos, zPos, 1.0));
406  }
407  }
408  descrStr = TempCString(attrTemp->getNamedItem(XercesString("description"))->getNodeValue());
409  directionallightPtr->SetDescription(descrStr);
410  result = directionallightPtr;
411  }
412 
413  else if (TempCString(listNode->item(1)->getNodeName()) == "spotlight")
414  {
416  VART::Light* spotlightPtr = new VART::Light;
417  spotlightPtr->autoDelete=true;
418  VART::Color* color;
419  color = new VART::Color;
420  VART::Point4D* location;
421  location = new VART::Point4D;
422  unsigned int i;
423  float intensity;
424  float ambientIntensity;
425  float xPos;
426  float yPos;
427  float zPos;
428  unsigned char r;
429  unsigned char g;
430  unsigned char b;
431  istringstream stream;
432 
433  DOMNodeList* childNodes = listNode->item(1)->getChildNodes();
434  DOMNamedNodeMap* attrTemp = listNode->item(1)->getAttributes();
435 
436  for (i = 0; i<childNodes->getLength(); i++)
437  {
438  if (TempCString(childNodes->item(i)->getNodeName()) == "intensity")
439  {
440  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
441  TempCString intensityStr(attrPt->getNamedItem(XercesString("value"))->getNodeValue());
442 
443  intensityStr.GetValue(&intensity);
444  spotlightPtr->SetIntensity(intensity);
445  }
446  else if (TempCString(childNodes->item(i)->getNodeName()) == "ambientIntensity")
447  {
448  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
449  TempCString aiStr(attrPt->getNamedItem(XercesString("value"))->getNodeValue());
450 
451  aiStr.GetValue(&ambientIntensity);
452  spotlightPtr->SetAmbientIntensity(ambientIntensity);
453  }
454  else if (TempCString(childNodes->item(i)->getNodeName()) == "color")
455  {
456  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
457  TempCString redStr(attrPt->getNamedItem(XercesString("red"))->getNodeValue());
458  TempCString greenStr(attrPt->getNamedItem(XercesString("green"))->getNodeValue());
459  TempCString blueStr(attrPt->getNamedItem(XercesString("blue"))->getNodeValue());
460 
461  redStr.GetValue(&rTemp);
462  r = static_cast<unsigned char>(rTemp);
463  greenStr.GetValue(&gTemp);
464  g = static_cast<unsigned char>(gTemp);
465  blueStr.GetValue(&bTemp);
466  b = static_cast<unsigned char>(bTemp);
467  spotlightPtr->SetColor(Color(r,g,b));
468  }
469  else if (TempCString(childNodes->item(i)->getNodeName()) == "enabled")
470  {
471  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
472  TempCString boolValue(attrPt->getNamedItem(XercesString("value"))->getNodeValue());
473 
474  if (boolValue == "true")
475  spotlightPtr->Turn(true);
476  else
477  {
478  if (boolValue == "false")
479  spotlightPtr->Turn(false);
480  else
481  {
482  cerr << "XmlScene::LoadSceneNode: Error at 'spotlight' attribute: "
483  << "unknown value for 'enabled': '" << boolValue
484  << "'. Assuming 'true'.\n";
485  spotlightPtr->Turn(true);
486  }
487  }
488  }
489  else if (TempCString(childNodes->item(i)->getNodeName()) == "position")
490  {
491  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
492  TempCString xStr(attrPt->getNamedItem(XercesString("x"))->getNodeValue());
493  TempCString yStr(attrPt->getNamedItem(XercesString("y"))->getNodeValue());
494  TempCString zStr(attrPt->getNamedItem(XercesString("z"))->getNodeValue());
495 
496  xStr.GetValue(&xPos);
497  yStr.GetValue(&yPos);
498  zStr.GetValue(&zPos);
499  spotlightPtr->SetLocation(Point4D(xPos, yPos, zPos, 1.0));
500  }
501  }
502  descrStr = TempCString(attrTemp->getNamedItem(XercesString("description"))->getNodeValue());
503  spotlightPtr->SetDescription(descrStr);
504  result = spotlightPtr;
505  }
506 
507  else if (TempCString(listNode->item(1)->getNodeName()) == "pointlight")
508  {
509  VART::Light* pointlightPtr = new VART::Light;
510  pointlightPtr->autoDelete=true;
511  VART::Color* color;
512  color = new VART::Color;
513  VART::Point4D* location;
514  location = new VART::Point4D;
515  unsigned int i;
516  float intensity;
517  float ambientIntensity;
518  float xPos;
519  float yPos;
520  float zPos;
522  unsigned char r;
523  unsigned char g;
524  unsigned char b;
525  istringstream stream;
526 
527  DOMNodeList* childNodes = listNode->item(1)->getChildNodes();
528  DOMNamedNodeMap* attrTemp = listNode->item(1)->getAttributes();
529 
530  for (i = 0; i<childNodes->getLength(); i++)
531  {
532  if (TempCString(childNodes->item(i)->getNodeName()) == "intensity")
533  {
534  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
535  TempCString intensityStr(attrPt->getNamedItem(XercesString("value"))->getNodeValue());
536 
537  intensityStr.GetValue(&intensity);
538  pointlightPtr->SetIntensity(intensity);
539  }
540  else if (TempCString(childNodes->item(i)->getNodeName()) == "ambientIntensity")
541  {
542  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
543  TempCString aiStr(attrPt->getNamedItem(XercesString("value"))->getNodeValue());
544 
545  aiStr.GetValue(&ambientIntensity);
546  pointlightPtr->SetAmbientIntensity(ambientIntensity);
547  }
548  else if (TempCString(childNodes->item(i)->getNodeName()) == "color")
549  {
550  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
551  TempCString redStr(attrPt->getNamedItem(XercesString("red"))->getNodeValue());
552  TempCString greenStr(attrPt->getNamedItem(XercesString("green"))->getNodeValue());
553  TempCString blueStr(attrPt->getNamedItem(XercesString("blue"))->getNodeValue());
554 
555  redStr.GetValue(&rTemp);
556  r = static_cast<unsigned char>(rTemp);
557  greenStr.GetValue(&gTemp);
558  g = static_cast<unsigned char>(gTemp);
559  blueStr.GetValue(&bTemp);
560  b = static_cast<unsigned char>(bTemp);
561  pointlightPtr->SetColor(Color(r,g,b));
562  }
563  else if (TempCString(childNodes->item(i)->getNodeName()) == "enabled")
564  {
565  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
566  TempCString boolValue(attrPt->getNamedItem(XercesString("value"))->getNodeValue());
567 
568  if (boolValue == "true")
569  pointlightPtr->Turn(true);
570  else
571  {
572  if (boolValue == "false")
573  pointlightPtr->Turn(false);
574  else
575  {
576  cerr << "XmlScene::LoadSceneNode: Error at 'pointlight' attribute: "
577  << "unknown value for 'enabled': '" << boolValue
578  << "'. Assuming 'true'.\n";
579  pointlightPtr->Turn(true);
580  }
581  }
582  }
583  else if (TempCString(childNodes->item(i)->getNodeName()) == "position")
584  {
585  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
586  TempCString xStr(attrPt->getNamedItem(XercesString("x"))->getNodeValue());
587  TempCString yStr(attrPt->getNamedItem(XercesString("y"))->getNodeValue());
588  TempCString zStr(attrPt->getNamedItem(XercesString("z"))->getNodeValue());
589 
590  xStr.GetValue(&xPos);
591  yStr.GetValue(&yPos);
592  zStr.GetValue(&zPos);
593  pointlightPtr->SetLocation(Point4D(xPos, yPos, zPos, 1.0));
594  }
595  }
596  descrStr = TempCString(attrTemp->getNamedItem(XercesString("description"))->getNodeValue());
597  pointlightPtr->SetDescription(descrStr);
598  result = pointlightPtr;
599  }
600 
601  else if (TempCString(listNode->item(1)->getNodeName()) == "transform")
602  {
603  //The transform can be defined by a matrix that defines the translation and
604  //rotations of the object, or a scale, or only a rotation, or only a translation.
605 
606  list<VART::Transform>::const_iterator iter;
607  list<VART::Transform> listTrans;
608  istringstream stream;
609  VART::Transform* trans;
610  trans = new VART::Transform();
611  trans->autoDelete=true;
612  unsigned int i;
613  trans->MakeIdentity();
614  float xPos;
615  float yPos;
616  float zPos;
617 
618  DOMNodeList* childNodes = listNode->item(1)->getChildNodes();
619  DOMNamedNodeMap* attrTemp = listNode->item(1)->getAttributes();
620 
621  for (i = 0; i<childNodes->getLength(); i++)
622  {
623 
624  if (TempCString(childNodes->item(i)->getNodeName()) == "matrix")
625  {
626  double data[16];
627  float tempFloat;
628  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
629  TempCString m00Str(attrPt->getNamedItem(XercesString("m00"))->getNodeValue());
630  TempCString m01Str(attrPt->getNamedItem(XercesString("m01"))->getNodeValue());
631  TempCString m02Str(attrPt->getNamedItem(XercesString("m02"))->getNodeValue());
632  TempCString m03Str(attrPt->getNamedItem(XercesString("m03"))->getNodeValue());
633  TempCString m10Str(attrPt->getNamedItem(XercesString("m10"))->getNodeValue());
634  TempCString m11Str(attrPt->getNamedItem(XercesString("m11"))->getNodeValue());
635  TempCString m12Str(attrPt->getNamedItem(XercesString("m12"))->getNodeValue());
636  TempCString m13Str(attrPt->getNamedItem(XercesString("m13"))->getNodeValue());
637  TempCString m20Str(attrPt->getNamedItem(XercesString("m20"))->getNodeValue());
638  TempCString m21Str(attrPt->getNamedItem(XercesString("m21"))->getNodeValue());
639  TempCString m22Str(attrPt->getNamedItem(XercesString("m22"))->getNodeValue());
640  TempCString m23Str(attrPt->getNamedItem(XercesString("m23"))->getNodeValue());
641  TempCString m30Str(attrPt->getNamedItem(XercesString("m30"))->getNodeValue());
642  TempCString m31Str(attrPt->getNamedItem(XercesString("m31"))->getNodeValue());
643  TempCString m32Str(attrPt->getNamedItem(XercesString("m32"))->getNodeValue());
644  TempCString m33Str(attrPt->getNamedItem(XercesString("m33"))->getNodeValue());
645 
646  m00Str.GetValue(&tempFloat);
647  data[0] = tempFloat;
648  m01Str.GetValue(&tempFloat);
649  data[1] = tempFloat;
650  m02Str.GetValue(&tempFloat);
651  data[2] = tempFloat;
652  m03Str.GetValue(&tempFloat);
653  data[3] = tempFloat;
654  m10Str.GetValue(&tempFloat);
655  data[4] = tempFloat;
656  m11Str.GetValue(&tempFloat);
657  data[5] = tempFloat;
658  m12Str.GetValue(&tempFloat);
659  data[6] = tempFloat;
660  m13Str.GetValue(&tempFloat);
661  data[7] = tempFloat;
662  m20Str.GetValue(&tempFloat);
663  data[8] = tempFloat;
664  m21Str.GetValue(&tempFloat);
665  data[9] = tempFloat;
666  m22Str.GetValue(&tempFloat);
667  data[10] = tempFloat;
668  m23Str.GetValue(&tempFloat);
669  data[11] = tempFloat;
670  m30Str.GetValue(&tempFloat);
671  data[12] = tempFloat;
672  m31Str.GetValue(&tempFloat);
673  data[13] = tempFloat;
674  m32Str.GetValue(&tempFloat);
675  data[14] = tempFloat;
676  m33Str.GetValue(&tempFloat);
677  data[15] = tempFloat;
678 
679  trans->SetData(data);
680  descrStr = TempCString(attrTemp->getNamedItem(XercesString("description"))->getNodeValue());
681  trans->SetDescription(descrStr);
682  result = trans;
683  }
684 
685  else if (TempCString(childNodes->item(i)->getNodeName()) == "translation")
686  {
687  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
688  TempCString xStr(attrPt->getNamedItem(XercesString("x"))->getNodeValue());
689  TempCString yStr(attrPt->getNamedItem(XercesString("y"))->getNodeValue());
690  TempCString zStr(attrPt->getNamedItem(XercesString("z"))->getNodeValue());
691 
692  xStr.GetValue(&xPos);
693  yStr.GetValue(&yPos);
694  zStr.GetValue(&zPos);
695  trans-> MakeTranslation(Point4D(xPos, yPos, zPos, 0));
696 
697  descrStr = TempCString(attrTemp->getNamedItem(XercesString("description"))->getNodeValue());
698  trans->SetDescription(descrStr);
699  result = trans;
700  }
701 
702  else if (TempCString(childNodes->item(i)->getNodeName()) == "scale")
703  {
704  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
705  TempCString xStr(attrPt->getNamedItem(XercesString("x"))->getNodeValue());
706  TempCString yStr(attrPt->getNamedItem(XercesString("y"))->getNodeValue());
707  TempCString zStr(attrPt->getNamedItem(XercesString("z"))->getNodeValue());
708 
709  xStr.GetValue(&xPos);
710  yStr.GetValue(&yPos);
711  zStr.GetValue(&zPos);
712  trans-> MakeScale(xPos, yPos, zPos);
713 
714  descrStr = TempCString(attrTemp->getNamedItem(XercesString("description"))->getNodeValue());
715  trans->SetDescription(descrStr);
716  result = trans;
717  }
718 
719  else if (TempCString(childNodes->item(i)->getNodeName()) == "rotation")
720  {
721  DOMNamedNodeMap* attrPt = childNodes->item(i)->getAttributes();
722  float radians;
723  TempCString axisStr(attrPt->getNamedItem(XercesString("axis"))->getNodeValue());
724  TempCString radiansStr(attrPt->getNamedItem(XercesString("radians"))->getNodeValue());
725 
726  radiansStr.GetValue(&radians);
727  if (axisStr == "x")
728  trans->MakeXRotation(radians);
729  else if (axisStr == "y")
730  trans->MakeYRotation(radians);
731  else if (axisStr == "z")
732  trans->MakeZRotation(radians);
733  descrStr = TempCString(attrTemp->getNamedItem(XercesString("description"))->getNodeValue());
734  trans->SetDescription(descrStr);
735  result = trans;
736  }
737  }
738  }
739 
740  else if (TempCString(listNode->item(1)->getNodeName()) == "joint")
741  {
742  list<VART::Dof*> listOfDofs;
743  list<VART::Dof*>::const_iterator iter;
744  DOMNamedNodeMap* attrNode = listNode->item(1)->getAttributes();
745  TempCString name(attrNode->getNamedItem(XercesString("type"))->getNodeValue());
746 
748  if (name == "biaxial")
749  {
750  VART::BiaxialJoint* newJointB;
751  DOMNamedNodeMap* attrAux;
752 
753  newJointB = new VART::BiaxialJoint();
754  newJointB->autoDelete = true;
755  loadDofs (listNode->item(1), &listOfDofs);
756  for (iter = listOfDofs.begin(); iter!= listOfDofs.end(); ++iter)
757  newJointB->AddDof(*iter);
758  attrAux = listNode->item(1)->getAttributes();
759  descrStr = TempCString(attrAux->getNamedItem(XercesString("description"))->getNodeValue());
760  newJointB->SetDescription(descrStr);
761  result = newJointB;
762  }
763 
764  else if (name == "polyaxial")
765  {
766  VART::PolyaxialJoint* newJointB;
767  DOMNamedNodeMap* attrAux;
768 
769  newJointB = new VART::PolyaxialJoint();
770  newJointB->autoDelete = true;
771  loadDofs (listNode->item(1), &listOfDofs);
772  for (iter = listOfDofs.begin(); iter!=listOfDofs.end(); ++iter)
773  newJointB->AddDof(*iter);
774  attrAux = listNode->item(1)->getAttributes();
775  descrStr = TempCString(attrAux->getNamedItem(XercesString("description"))->getNodeValue());
776  newJointB->SetDescription(descrStr);
777  result = newJointB;
778  }
779 
780  else if (name == "uniaxial")
781  {
782  VART::UniaxialJoint* newJointB;
783  DOMNamedNodeMap* attrAux;
784 
785  newJointB = new VART::UniaxialJoint();
786  newJointB->autoDelete = true;
787  attrAux = listNode->item(1)->getAttributes();
788  descrStr = TempCString(attrAux->getNamedItem(XercesString("description"))->getNodeValue());
789  newJointB->SetDescription(descrStr);
790  loadDofs (listNode->item(1), &listOfDofs);
791  for (iter = listOfDofs.begin(); iter!=listOfDofs.end(); ++iter)
792  {
793  newJointB->AddDof(*iter);
794  }
795  result = newJointB;
796  }
797  }
798 
799  // Next XML node
800  currentChild += 2;
801  while (currentChild < numChildren)
802  {
803  // FixMe: Need to treat lights in a proper way.
804  SceneNode* snPtr = LoadSceneNode(listNode->item(currentChild), basePath);
805 
806  result->AddChild(*snPtr);
807  currentChild += 2;
808  }
809  return result;
810 }
811 
812 void VART::XmlScene::loadDofs(DOMNode* node, list<VART::Dof*>* dofs)
813 {
814  list<VART::Dof*>::iterator iter;
815  unsigned int i;
816  unsigned int j;
817  istringstream stream;
818  float xPos;
819  float yPos;
820  float zPos;
821  float minValue, maxValue, resValue;
822  VART::Point4D axis;
823  VART::Point4D position;
824  DOMNodeList* dof = node->getChildNodes();
825  DOMNamedNodeMap* attrAux;
826  string descrStr;
827 
828  for (i = 0; i< dof->getLength(); i++)
829  {
830  if (TempCString(dof->item(i)->getNodeName()) == "dof")
831  {
832  VART::Dof* d = new VART::Dof;
833  DOMNodeList* dofAux = dof->item(i)->getChildNodes();
834 
835  d->autoDelete=true;
836 
837  for (j = 0; j <dofAux->getLength(); j++ )
838  {
839  if (TempCString(dofAux->item(j)->getNodeName()) == "position")
840  {
841  DOMNamedNodeMap* attrPos = dofAux->item(j)->getAttributes();
842  TempCString xStr(attrPos->getNamedItem(XercesString("x"))->getNodeValue());
843  TempCString yStr(attrPos->getNamedItem(XercesString("y"))->getNodeValue());
844  TempCString zStr(attrPos->getNamedItem(XercesString("z"))->getNodeValue());
845 
846  xStr.GetValue(&xPos);
847  yStr.GetValue(&yPos);
848  zStr.GetValue(&zPos);
849  position.SetXYZW(xPos,yPos,zPos,1);
850  }
851 
852  else if (TempCString(dofAux->item(j)->getNodeName()) == "axis")
853  {
854  DOMNamedNodeMap* attrAxis = dofAux->item(j)->getAttributes();
855  TempCString xStr(attrAxis->getNamedItem(XercesString("x"))->getNodeValue());
856  TempCString yStr(attrAxis->getNamedItem(XercesString("y"))->getNodeValue());
857  TempCString zStr(attrAxis->getNamedItem(XercesString("z"))->getNodeValue());
858 
859  xStr.GetValue(&xPos);
860  yStr.GetValue(&yPos);
861  zStr.GetValue(&zPos);
862  axis.SetXYZW(xPos, yPos, zPos, 0);
863  axis.Normalize();
864  }
865  else if (TempCString(dofAux->item(j)->getNodeName()) == "range")
866  {
867  DOMNamedNodeMap *attrRange = dofAux->item(j)->getAttributes();
868  TempCString minStr(attrRange->getNamedItem(XercesString("min"))->getNodeValue());
869  TempCString maxStr(attrRange->getNamedItem(XercesString("max"))->getNodeValue());
870  TempCString resStr(attrRange->getNamedItem(XercesString("rest"))->getNodeValue());
871 
872  minStr.GetValue(&minValue);
873  maxStr.GetValue(&maxValue);
874  resStr.GetValue(&resValue);
875  }
876  }
877  d->Set(axis, position, minValue, maxValue);
878  attrAux = dof->item(i)->getAttributes();
879  descrStr = TempCString(attrAux->getNamedItem(XercesString("description"))->getNodeValue());
880  d->SetDescription(descrStr);
881  d->SetRest(resValue);
882  dofs->push_back(d);
883  }
884  }
885 }
886 
888 {
889 }
A joint with one rotational degrees of freedom.
Definition: uniaxialjoint.h:16
void SetData(double *data)
Set all data in the transform.
Definition: transform.cpp:33
Base class for objects that compose a scene graph.
Definition: scenenode.h:25
Header file for V-ART class "Dof".
void SetColor(const Color &c)
Definition: light.cpp:107
Points and vectors using homogeneous coordinates.
Definition: point4d.h:22
MeshObject * LoadMeshFromFile(std::string filen, std::string type, std::string meshName)
Load MeshObjects from file.
Definition: xmlscene.cpp:95
void SetRadius(float r)
Sets the value of top and bottom radius.
Definition: cylinder.cpp:80
void SetMaterial(const Material &mat)
Material assignment.
Definition: cylinder.h:50
void SetRadius(float r)
Definition: sphere.cpp:44
void Normalize()
Normalizes the point/vector.
Definition: point4d.cpp:88
const std::string & GetDescription() const
Returns a copy of the object's description.
Definition: scenenode.h:50
bool LoadFromFile(const std::string &fileName)
Parses the xml file. If it doesn't have errors, load scene.
Definition: xmlscene.cpp:27
RGBA color representation.
Definition: color.h:24
void SetLocation(const Point4D &newLocation)
Sets the location of the light.
Definition: light.cpp:123
Header file for V-ART class "File".
void SetDescription(const std::string &desc)
Definition: dof.cpp:118
void AddDof(Dof *dof)
Adds a Dof.
void MakeXRotation(double radians)
Turns transform into a rotation around the X axis.
Definition: transform.cpp:64
A sphere.
Definition: sphere.h:16
Geometric transformations.
Definition: transform.h:24
static std::string GetPathFromString(const std::string &fileName)
Definition: file.cpp:9
Header file for V-ART class "UniaxialJoint".
Header file for V-ART class "Sphere".
A cylinder or cone.
Definition: cylinder.h:18
void loadDofs(XERCES_CPP_NAMESPACE::DOMNode *node, std::list< Dof * > *dofs)
Load the dofs of the joint.
Definition: xmlscene.cpp:812
void SetHeight(float h)
Definition: cylinder.cpp:72
Header file for V-ART class "BiaxialJoint".
void SetIntensity(float i)
Definition: light.cpp:91
Header file for V-ART class "PolyaxialJoint".
A joint with two rotational degrees of freedom.
Definition: biaxialjoint.h:16
Class for auto conversion from C strings to Xerces strings.
Definition: xmlbase.h:77
void MakeYRotation(double radians)
Turns transform into a rotation around the Y axis.
Definition: transform.cpp:73
void MakeIdentity()
Turns transform into identity.
Definition: transform.cpp:49
void SetAmbientIntensity(float ai)
Definition: light.cpp:99
void SetDescription(const std::string &desc)
Changes the object's description.
Definition: scenenode.h:53
Material properties for graphical objects.
Definition: material.h:16
void Turn(bool on_off)
Turns a light on or off.
Definition: light.cpp:115
A joint with three rotational degrees of freedom.
SceneNode * LoadSceneNode(XERCES_CPP_NAMESPACE::DOMNode *sceneList, const std::string &basePath)
Load the nodes (transformations, geometry, etc.) of the scene.
Definition: xmlscene.cpp:160
bool GetValue(int *valuePtr) const
Definition: xmlbase.cpp:165
bool LoadScene(const std::string &basePath)
Load the scene based in xml archieve.
Definition: xmlscene.cpp:57
Class for auto conversion from Xerces strings to C strings.
Definition: xmlbase.h:47
void SetMaterial(const Material &mat)
Material assignment.
Definition: sphere.h:33
Header file for V-ART class "Cylinder".
void SetXYZW(double x, double y, double z, double w)
Definition: point4d.cpp:80
Header file for V-ART class "MeshObject".
void SetRest(float rest)
Definition: dof.cpp:321
virtual void AddDof(Dof *dof)
Add a DOF to the joint.
static bool ReadFromOBJ(const std::string &filename, std::list< MeshObject * > *resultPtr)
Read MeshObjects from a Wavefront OBJ file.
Definition: meshobject.cpp:775
Degree Of Freedom - basic component of a Joint.
Definition: dof.h:28
void Set(const Point4D &vec, const Point4D &pos, float min, float max)
Initializes a DOF previouly created with default constructor.
Definition: dof.cpp:102
void AddDof(Dof *dof)
Add a DOF to the joint.
Header file for V-ART class "XmlScene".
void MakeZRotation(double radians)
Turns transform into a rotation around the Z axis.
Definition: transform.cpp:82
Graphical object made of polygon meshes.
Definition: meshobject.h:25
void AddChild(SceneNode &child)
Add a child at the end of child list.
Definition: scenenode.cpp:54
Represents a light source.
Definition: light.h:22