vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
WallMovement.h
Go to the documentation of this file.
1 #ifndef _WALL_MOVEMENT_H
2 #define _WALL_MOVEMENT_H
3 
4 #include "Tool.h"
5 
6 namespace APRoomEdit {
7 
8  class WallMovement : public Tool {
9  public:
11  virtual ~WallMovement(void) {}
12 
13  virtual bool loadXMLParameters(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pParametersNode) {
14  if (findXMLNode(pParametersNode, "Name")) {
15  ReaderWriter::getStringValue(name, pParametersNode, "Name");
16  } else {
17  name = "WallMovement";
18  }
19  return true;
20  }
21 
22 
23  virtual void update(osgUtil::LineSegmentIntersector::Intersection* intersection, WG_Wall* curWall, WG_Point* curPoint, PointType type, vrecko::EnvironmentObject* pEOOwner, std::vector<osg::ref_ptr<WG_Wall> >* walls, std::vector<osg::ref_ptr<WG_Point> >* points, osg::Geode* pOSGGeode, osg::Vec3Array* pOSGVertexArray, osg::Vec3& movingVector) {
24 
25  if (curWall != NULL) {
26  osg::Vec2 wallNormal = osg::Vec2(curWall->getFaceNormal(type).x(), curWall->getFaceNormal(type).z());
27  float scalar = wallNormal.x() * movingVector.x() + wallNormal.y() * movingVector.z();
28  wallNormal.normalize();
29  wallNormal *= scalar;
30 
31  WG_Point* Q = curWall->getPoint(WG_Q_POINT);
32  WG_Point* R = curWall->getPoint(WG_R_POINT);
33 
34  osg::Vec2 posQ = Q->getPosition() + wallNormal;
35  osg::Vec2 posR = R->getPosition() + wallNormal;
36 
37 
38  for (unsigned int i=0; i<walls->size(); i++) {
39  WG_Wall* wallI = walls->at(i).get();
40 
41  bool skipQ = false;
42  bool skipR = false;
43  for (unsigned int j=0; j<Q->getWalls()->size(); j++) {
44  skipQ = (wallI == Q->getWalls()->at(j).first.get());
45  if (skipQ)
46  break;
47  }
48  for (unsigned int j=0; j<R->getWalls()->size(); j++) {
49  skipR = (wallI == R->getWalls()->at(j).first.get());
50  if (skipR)
51  break;
52  }
53  WG_Vertices* qV = wallI->getVertices(WG_Q_POINT);
54  WG_Vertices* rV = wallI->getVertices(WG_R_POINT);
55  if (!skipQ) {
56  bool b1 = WG_Geometry::barycentricTest(posQ,
57  osg::Vec2(qV->getBottomCCWVertex().x(), qV->getBottomCCWVertex().z()),
58  osg::Vec2(qV->getBottomCWVertex().x(), qV->getBottomCWVertex().z()),
59  osg::Vec2(rV->getBottomCCWVertex().x(), rV->getBottomCCWVertex().z()));
60  bool b2 = WG_Geometry::barycentricTest(posQ,
61  osg::Vec2(qV->getBottomCCWVertex().x(), qV->getBottomCCWVertex().z()),
62  osg::Vec2(qV->getBottomCWVertex().x(), qV->getBottomCWVertex().z()),
63  osg::Vec2(rV->getBottomCWVertex().x(), rV->getBottomCWVertex().z()));
64  if (b1 || b2)
65  return;
66  }
67  if (!skipR) {
68  bool b3 = WG_Geometry::barycentricTest(posR,
69  osg::Vec2(qV->getBottomCCWVertex().x(), qV->getBottomCCWVertex().z()),
70  osg::Vec2(qV->getBottomCWVertex().x(), qV->getBottomCWVertex().z()),
71  osg::Vec2(rV->getBottomCCWVertex().x(), rV->getBottomCCWVertex().z()));
72  bool b4 = WG_Geometry::barycentricTest(posR,
73  osg::Vec2(qV->getBottomCCWVertex().x(), qV->getBottomCCWVertex().z()),
74  osg::Vec2(qV->getBottomCWVertex().x(), qV->getBottomCWVertex().z()),
75  osg::Vec2(rV->getBottomCWVertex().x(), rV->getBottomCWVertex().z()));
76  if (b3 || b4)
77  return;
78  }
79  }
80 
81 
82 
83  for (unsigned int k=0; k<Q->getWalls()->size(); k++) {
84  WG_Wall* wallK = Q->getWalls()->at(k).first.get();
85  PointType ptype = Q->getWalls()->at(k).second;
86  PointType opptype = (ptype == WG_Q_POINT ? WG_R_POINT : WG_Q_POINT);
87 
88  osg::Vec2 length = wallK->getPoint(opptype)->getPosition() - posQ;
89  if (length.length() < RE_MIN_LENGTH)
90  return;
91 
92  float angleCW, angleCCW, angle, angleCompare, angleMin;
93  osg::Vec2 v;
94  std::pair<osg::ref_ptr<WG_Wall>, PointType> wallCW;
95  std::pair<osg::ref_ptr<WG_Wall>, PointType> wallCCW;
96  if (wallK->getPoint(ptype)->getWallNeighbours(wallK, wallCW, wallCCW)) {
97  angleCW = wallCW.first->getAngle(wallCW.second);
98  angleCCW = wallCCW.first->getAngle(wallCCW.second);
99  v = wallK->getPoint(opptype)->getPosition() - posQ;
100  angle = WG_Geometry::getAngle(v);
101  angleCompare = angleCCW - angle;
102  angleMin = 5+RE_MIN_ANGLE(wallK->getVector(WG_Q_POINT).length(),
103  wallCCW.first->getVector(WG_Q_POINT).length());
104  if ((angleCompare < angleMin) && (angleCompare > - angleMin))
105  return;
106  angleCompare = angle - angleCW;
107  angleMin = 5+RE_MIN_ANGLE(wallK->getVector(WG_Q_POINT).length(),
108  wallCW.first->getVector(WG_Q_POINT).length());
109  if ((angleCompare < angleMin) && (angleCompare > - angleMin))
110  return;
111  }
112 
113  if (wallK->getPoint(opptype)->getWallNeighbours(wallK, wallCW, wallCCW)) {
114  angleCW = wallCW.first->getAngle(wallCW.second);
115  angleCCW = wallCCW.first->getAngle(wallCCW.second);
116  v = posQ - wallK->getPoint(opptype)->getPosition();
117  angle = WG_Geometry::getAngle(v);
118  angleCompare = angleCCW - angle;
119  angleMin = 5+RE_MIN_ANGLE(wallK->getVector(WG_Q_POINT).length(),
120  wallCCW.first->getVector(WG_Q_POINT).length());
121  if ((angleCompare < angleMin) && (angleCompare > - angleMin))
122  return;
123  angleCompare = angle - angleCW;
124  angleMin = 5+RE_MIN_ANGLE(wallK->getVector(WG_Q_POINT).length(),
125  wallCW.first->getVector(WG_Q_POINT).length());
126  if ((angleCompare < angleMin) && (angleCompare > - angleMin))
127  return;
128  }
129  }
130 
131 
132  for (unsigned int k=0; k<R->getWalls()->size(); k++) {
133  WG_Wall* wallK = R->getWalls()->at(k).first.get();
134  PointType ptype = R->getWalls()->at(k).second;
135  PointType opptype = (ptype == WG_Q_POINT ? WG_R_POINT : WG_Q_POINT);
136 
137  osg::Vec2 length = wallK->getPoint(opptype)->getPosition() - posR;
138  if (length.length() < RE_MIN_LENGTH)
139  return;
140 
141  float angleCW, angleCCW, angle, angleCompare, angleMin;
142  osg::Vec2 v;
143  std::pair<osg::ref_ptr<WG_Wall>, PointType> wallCW;
144  std::pair<osg::ref_ptr<WG_Wall>, PointType> wallCCW;
145  if (wallK->getPoint(ptype)->getWallNeighbours(wallK, wallCW, wallCCW)) {
146  angleCW = wallCW.first->getAngle(wallCW.second);
147  angleCCW = wallCCW.first->getAngle(wallCCW.second);
148  v = wallK->getPoint(opptype)->getPosition() - posR;
149  angle = WG_Geometry::getAngle(v);
150  angleCompare = angleCCW - angle;
151  angleMin = 5+RE_MIN_ANGLE(wallK->getVector(WG_Q_POINT).length(),
152  wallCCW.first->getVector(WG_Q_POINT).length());
153  if ((angleCompare < angleMin) && (angleCompare > - angleMin))
154  return;
155  angleCompare = angle - angleCW;
156  angleMin = 5+RE_MIN_ANGLE(wallK->getVector(WG_Q_POINT).length(),
157  wallCW.first->getVector(WG_Q_POINT).length());
158  if ((angleCompare < angleMin) && (angleCompare > - angleMin))
159  return;
160  }
161 
162  if (wallK->getPoint(opptype)->getWallNeighbours(wallK, wallCW, wallCCW)) {
163  angleCW = wallCW.first->getAngle(wallCW.second);
164  angleCCW = wallCCW.first->getAngle(wallCCW.second);
165  v = posR - wallK->getPoint(opptype)->getPosition();
166  angle = WG_Geometry::getAngle(v);
167  angleCompare = angleCCW - angle;
168  angleMin = 5+RE_MIN_ANGLE(wallK->getVector(WG_Q_POINT).length(),
169  wallCCW.first->getVector(WG_Q_POINT).length());
170  if ((angleCompare < angleMin) && (angleCompare > - angleMin))
171  return;
172  angleCompare = angle - angleCW;
173  angleMin = 5+RE_MIN_ANGLE(wallK->getVector(WG_Q_POINT).length(),
174  wallCW.first->getVector(WG_Q_POINT).length());
175  if ((angleCompare < angleMin) && (angleCompare > - angleMin))
176  return;
177  }
178  }
179 
180 
181  Q->setPosition(posQ);
182  R->setPosition(posR);
183  for (unsigned int l=0; l<Q->getWalls()->size(); l++) {
184  PointType typeL = (Q->getWalls()->at(l).second == WG_Q_POINT ? WG_R_POINT : WG_Q_POINT);
185  WG_Wall* wallL = Q->getWalls()->at(l).first.get();
187  wallL->computeAttrVertices();
188  for (unsigned int m=0; m<wallL->getPoint(typeL)->getWalls()->size(); m++)
189  wallL->getPoint(typeL)->getWalls()->at(m).first->computeAttrVertices();
190  }
191  for (unsigned int l=0; l<R->getWalls()->size(); l++) {
192  PointType typeL = (R->getWalls()->at(l).second == WG_Q_POINT ? WG_R_POINT : WG_Q_POINT);
193  WG_Wall* wallL = R->getWalls()->at(l).first.get();
195  wallL->computeAttrVertices();
196  for (unsigned int m=0; m<wallL->getPoint(typeL)->getWalls()->size(); m++)
197  wallL->getPoint(typeL)->getWalls()->at(m).first->computeAttrVertices();
198  }
199  curWall->computeAttrVertices();
200  setDirty(true);
201  }
202  }
203 
204  };
205 
206 }
207 
208 #endif
209