vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RoomLoader.h
Go to the documentation of this file.
1 #ifndef _ROOM_LOADER_H
2 #define _ROOM_LOADER_H
3 
4 #include "Tool.h"
5 #include "Geometry.h"
6 
7 namespace APRoomEdit {
8 
9  // tool to load room from xml
10  class RoomLoader : public Tool {
11  public:
12  RoomLoader(std::string f) {filename = f; type=LOADER; useEG=true;}
13  virtual ~RoomLoader() {}
14 
15  virtual bool loadXMLParameters(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pParametersNode) {
16  if (findXMLNode(pParametersNode, "FileName")) {
17  ReaderWriter::getStringValue(filename, pParametersNode, "FileName");
18  name = filename;
19  if (findXMLNode(pParametersNode, "UseEmbeddedGeometry")) {
20  useEG = ReaderWriter::getBoolValue(pParametersNode, "UseEmbeddedGeometry");
21  }
22  }
23  else
24  return false;
25  return true;
26  }
27 
28  virtual void init(osgUtil::LineSegmentIntersector::Intersection* intersection,
29  WG_Wall* curWall,
30  WG_Point* curPoint,
32  vrecko::EnvironmentObject* pEOOwner,
33  std::vector<osg::ref_ptr<WG_Wall> >* walls,
34  std::vector<osg::ref_ptr<WG_Point> >* points,
35  osg::Geode* pOSGGeode,
36  osg::Vec3Array* pOSGVertexArray) {
37 
38  //verify if given file exists
39  std::ifstream file(filename.c_str());
40  if(!file) {
41  logger.errorLog("RoomEdit Plugin: ERROR in loading scene - file: '%s' doesn't exists", filename.c_str());
42  return;
43  }
44 
45  file.close();
46 
47  // Notify user
48  vrecko::logger.noticeLog("RoomEdit Plugin: Loading model from %s", filename.c_str());
49  std::vector<osg::ref_ptr<WG_Point> > tempPoints;
50 
51  // Create XML parser and parse the input file
52  XercesDOMParser* Parser = new XercesDOMParser();
53  const char* F;
54  F = (filename.c_str());
55  Parser->parse(F);
56  XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* Document = Parser->getDocument();
57 
58  // Get root
59  XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* RootNode = Document->getDocumentElement();
60 
61  //XMLCh tempStr[100];
62 
63 
64  XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* wallGeometryNode = findXMLNode(RootNode, "WallGeometry");
65 
66 
67  int id;
68  osg::Vec2 position;
69  float height = RE_DEFAULT_HEIGHT;
70  WG_Point* tempPoint;
71  XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* pointsNodes = findXMLNode(RootNode, "Points");
72  int pointSize = ReaderWriter::getIntValue(pointsNodes, "PointsCount");
73  tempPoints.resize(pointSize);
74 
75  // Go through all Points
76  for (int i = 1; i < pointSize+1; i++) {
77  XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* pointNode = pointsNodes->getChildNodes()->item(i);
78  id = ReaderWriter::getIntValue(pointNode, "Id");
79  height = ReaderWriter::getFloatValue(pointNode, "Height");
80  ReaderWriter::getVec2Value(position, pointNode, "Position");
81 
82  std::cout << id << ": " << height << ", " << position.x() << ", " << position.y() << std::endl;
83 
84  tempPoint = new WG_Point(position, height);
85  tempPoint->setId(id);
86  tempPoints[id] = tempPoint;
87  }
88 
89 
90  XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* wallsNodes = findXMLNode(RootNode, "Walls");
91  int wallSize = ReaderWriter::getIntValue(wallsNodes, "WallsCount");
92 
93  if (wallSize) {
94  points->clear();
95  for (unsigned int a=0; a<tempPoints.size(); a++)
96  points->push_back(tempPoints.at(a));
97  tempPoints.clear();
98  }
99  else {
100  vrecko::logger.warningLog("No walls included. Model will not be changed.");
101  tempPoints.clear();
102  return;
103  }
104 
105  walls->clear();
106 
107  int qId, rId;
108  WG_Wall* tempWall;
109  osg::Vec3 tempVec3;
110  osg::Vec2 tempVec2;
111  // Go through all Points
112  for (int j = 1; j < wallSize+1; j++) {
113  XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* wallNode = wallsNodes->getChildNodes()->item(j);
114  XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* qNode = findXMLNode(wallNode, "QPoint");
115  XERCES_CPP_NAMESPACE_QUALIFIER DOMNode* rNode = findXMLNode(wallNode, "RPoint");
116  qId = ReaderWriter::getIntValue(qNode, "Id");
117  rId = ReaderWriter::getIntValue(rNode, "Id");
118 
119  cout<< "loaded ids "<< j <<": " << qId << ", " << rId << endl;
120 
121  tempWall = new WG_Wall(points->at(qId).get(), points->at(rId).get(), false);
122 
123  ReaderWriter::getVec3Value(tempVec3, qNode, "Color");
124  tempWall->setColor(tempVec3, WG_Q_POINT);
125 
126  ReaderWriter::getVec3Value(tempVec3, rNode, "Color");
127  tempWall->setColor(tempVec3, WG_R_POINT);
128 
129  if (useEG) {
130  ReaderWriter::getVec3Value(tempVec3, qNode, "tCWV");
131  tempWall->getVertices(WG_Q_POINT)->setTopCWVertex(tempVec3);
132 
133  ReaderWriter::getVec3Value(tempVec3, qNode, "tCCWV");
134  tempWall->getVertices(WG_Q_POINT)->setTopCCWVertex(tempVec3);
135 
136  ReaderWriter::getVec3Value(tempVec3, qNode, "bCWV");
137  tempWall->getVertices(WG_Q_POINT)->setBottomCWVertex(tempVec3);
138 
139  ReaderWriter::getVec3Value(tempVec3, qNode, "bCCWV");
140  tempWall->getVertices(WG_Q_POINT)->setBottomCCWVertex(tempVec3);
141 
142  ReaderWriter::getVec2Value(tempVec2, qNode, "nCWV");
143  tempWall->getVertices(WG_Q_POINT)->setNormalCWVertex(tempVec2);
144 
145  ReaderWriter::getVec2Value(tempVec2, qNode, "nCCWV");
146  tempWall->getVertices(WG_Q_POINT)->setNormalCCWVertex(tempVec2);
147 
148  ReaderWriter::getVec3Value(tempVec3, rNode, "tCWV");
149  tempWall->getVertices(WG_R_POINT)->setTopCWVertex(tempVec3);
150 
151  ReaderWriter::getVec3Value(tempVec3, rNode, "tCCWV");
152  tempWall->getVertices(WG_R_POINT)->setTopCCWVertex(tempVec3);
153 
154  ReaderWriter::getVec3Value(tempVec3, rNode, "bCWV");
155  tempWall->getVertices(WG_R_POINT)->setBottomCWVertex(tempVec3);
156 
157  ReaderWriter::getVec3Value(tempVec3, rNode, "bCCWV");
158  tempWall->getVertices(WG_R_POINT)->setBottomCCWVertex(tempVec3);
159 
160  ReaderWriter::getVec2Value(tempVec2, rNode, "nCWV");
161  tempWall->getVertices(WG_R_POINT)->setNormalCWVertex(tempVec2);
162 
163  ReaderWriter::getVec2Value(tempVec2, rNode, "nCCWV");
164  tempWall->getVertices(WG_R_POINT)->setNormalCCWVertex(tempVec2);
165 
166  ReaderWriter::getVec3Value(tempVec3, wallNode, "topNormal");
167  tempWall->setTopNormal(tempVec3);
168 
169  ReaderWriter::getVec3Value(tempVec3, wallNode, "bottomNormal");
170  tempWall->setBottomNormal(tempVec3);
171 
172  ReaderWriter::getVec3Value(tempVec3, wallNode, "qrNormal");
173  tempWall->setFaceNormal(tempVec3, WG_Q_POINT);
174 
175  ReaderWriter::getVec3Value(tempVec3, wallNode, "rqNormal");
176  tempWall->setFaceNormal(tempVec3, WG_R_POINT);
177  }
178 
179 
180  walls->push_back(tempWall);
181  }
182 
183  if (useEG)
184  for (unsigned int k=0; k<walls->size(); k++) {
185  walls->at(k)->setFlag(RE_WALL_DRAWN);
186  }
187  else
188  for (unsigned int k=0; k<walls->size(); k++) {
189  if (!(walls->at(k)->getFlag() & RE_WALL_DRAWN)) {
190  walls->at(k)->computeAttrVertices();
191  }
192  }
193 
194  delete Parser;
195 
196  setDirty(true);
197  }
198 
199  private:
200  std::string filename;
201  bool useEG;
202  };
203 
204 }
205 
206 #endif
207