vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
WallGeometry.h
Go to the documentation of this file.
1 #ifndef _WALL_GEOMETRY_H
2 #define _WALL_GEOMETRY_H
3 
4 #include <vrecko/ReaderWriter.h>
5 
6 #include <vrecko/Ability.h>
7 #include <vrecko/Scene.h>
9 #include <vrecko/Logger.h>
10 
11 #include <helpers/XercesXMLUtils.h>
12 #include <xercesc/parsers/XercesDOMParser.hpp>
13 #include <xercesc/dom/DOM.hpp>
14 #include <xercesc/sax/HandlerBase.hpp>
15 #include <xercesc/util/XMLString.hpp>
16 #include <xercesc/util/PlatformUtils.hpp>
17 
18 #include <osgDB/ReadFile>
19 
20 #include <osg/Geometry>
21 #include <osg/Geode>
22 #include <osg/StateSet>
23 #include <osg/Material>
24 #include <osg/CullFace>
25 #include <osg/LightModel>
26 #include <vrecko/Scene.h>
27 
28 #include <sstream>
29 #include <vector>
30 #include <set>
31 #include <map>
32 
33 #include "Geometry.h"
34 #include "Tool.h"
35 
36 using namespace vrecko;
37 using namespace vrecko;
38 using namespace WGeometry;
39 
40 namespace APRoomEdit {
41 
42 class WallGeometry: public Ability {
43  private:
44  // pointer to parent enviroment object
45  EnvironmentObject* pEOOwner;
46 
47  // pointer to a geode of the room
48  osg::Geode* pOSGGeode;
49 
50  // pointer to vertex array
51  osg::Vec3Array* pOSGVertexArray;
52 
53  // source filename
54  string filename;
55 
56  // array of walls to be drawn
57  std::vector<osg::ref_ptr<WG_Wall> > walls;
58 
59  // array of points
60  std::vector<osg::ref_ptr<WG_Point> > points;
61 
62  // key is the lowest index of the quad value is wall or point that contains the quad
63  // the index is returned by the LineSegmentIntersection.indexList.at(0)
64  std::map<unsigned int, WG_Wall*> wallMap;
65  std::map<unsigned int, WG_Point*> pointMap;
66 
67  bool dirty;
68 
69  // info for tools
70  WG_Wall* selectedWall;
71  WG_Point* selectedPoint;
72  PointType selectedType;
73 
74  // creates additional quads if there are two walls connected to the point
75  void setCorner(WG_Point* P, WG_Wall* wall, WG_Vertices* vertices, PointType type, PointType opType,
76  osg::Vec3& TCWV, osg::Vec3& TCCWV, osg::Vec3& BCWV, osg::Vec3& BCCWV);
77 
78  public:
79  // constructor
80  WallGeometry();
81 
82  // destructor
83  virtual ~WallGeometry();
84 
85  // called as the first initialization method, before loadXMLParameters
86  virtual void preInitialize();
87 
88  // called as the last initialization method, after loadXMLParameters
89  virtual void postInitialize();
90 
91  // loads XML parameters from given node
92  virtual bool loadXMLParameters(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pParametersNode);
93 
94  inline osg::Geode* getGeode() { return pOSGGeode; }
95 
96  void createDefaultRoom();
97 
98  void setDirty() { dirty = true; }
99 
100  void draw();
101 
102  WG_Wall* getWall(osgUtil::LineSegmentIntersector::Intersection* intersection) {
103  unsigned int index = intersection->indexList.at(0);
104  return (wallMap.find(index) != wallMap.end() ? wallMap[index] : NULL);
105  }
106 
107  WG_Point* getPoint(osgUtil::LineSegmentIntersector::Intersection* intersection) {
108  unsigned int index = intersection->indexList.at(0);
109  WG_Wall* wall = getWall(intersection);
110  WG_Point* point = NULL;
111  osg::Vec2 vectorQ, vectorR;
112  if (wall != NULL) {
113  vectorQ = osg::Vec2(intersection->getLocalIntersectPoint().x(), intersection->getLocalIntersectPoint().z()) - wall->getPoint(WG_Q_POINT)->getPosition();
114  vectorR = osg::Vec2(intersection->getLocalIntersectPoint().x(), intersection->getLocalIntersectPoint().z()) - wall->getPoint(WG_R_POINT)->getPosition();
115  point = (vectorQ.length() < vectorR.length() ? wall->getPoint(WG_Q_POINT) : wall->getPoint(WG_R_POINT));
116  }
117  else
118  point = (pointMap.find(index) != pointMap.end() ? pointMap[index] : NULL);
119 
120  return point;
121  }
122 
123  void activateTool(Tool* tool, osgUtil::LineSegmentIntersector::Intersection* intersection) {
124  if ((tool == NULL) || (tool->getType() == NONE))
125  return;
126 
127  selectedWall = getWall(intersection);
128  selectedPoint = getPoint(intersection);
129 
130  if (selectedWall != NULL) {
131  osg::Vec2 tempVector = osg::Vec2(intersection->getLocalIntersectPoint().x(), intersection->getLocalIntersectPoint().z());
132 
133  WG_Point* tempPoint = selectedWall->getPoint(WG_Q_POINT);
134  tempVector = tempVector - tempPoint->getPosition();
135 
136  float angle = WG_Geometry::getAngle(tempVector);
137  angle = angle - selectedWall->getAngle(WG_Q_POINT);
138  if (angle < 0)
139  angle += 360.0f;
140 
141  selectedType = (angle > 180 ? WG_R_POINT : WG_Q_POINT);
142  }
143 
144  tool->init(intersection, selectedWall, selectedPoint, selectedType, pEOOwner, &walls, &points, pOSGGeode, pOSGVertexArray);
145 
146  if (tool->isDirty()) {
147  setDirty();
148  tool->setDirty(false);
149  }
150  }
151 
152 
153  void deactivateTool(Tool* tool, osgUtil::LineSegmentIntersector::Intersection* intersection) {
154  if ((tool == NULL) || (tool->getType() == NONE))
155  return;
156 
157  tool->terminate(intersection, selectedWall, selectedPoint, selectedType, pEOOwner, &walls, &points, pOSGGeode, pOSGVertexArray);
158 
159  if (tool->isDirty()) {
160  setDirty();
161  tool->setDirty(false);
162  }
163  }
164 
165  void updateTool(Tool* tool, osgUtil::LineSegmentIntersector::Intersection* intersection, osg::Vec3 movingVector) {
166  if ((tool == NULL) || (tool->getType() == NONE))
167  return;
168 
169  tool->update(intersection, selectedWall, selectedPoint, selectedType, pEOOwner, &walls, &points, pOSGGeode, pOSGVertexArray, movingVector);
170 
171  if (tool->isDirty()) {
172  setDirty();
173  tool->setDirty(false);
174  }
175  }
176 };
177 
178 }
179 
180 #endif
181 
182