vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CreateHole.h
Go to the documentation of this file.
1 #ifndef _CREATE_HOLE_H
2 #define _CREATE_HOLE_H
3 
4 #include "Tool.h"
5 
6 namespace APRoomEdit {
7 
8  // tool to create new hole in the curWall, either door or window
9  // (given by the xml parameter)
10  class CreateHole : public Tool {
11  public:
13  ~CreateHole(void) {}
14 
15  virtual bool loadXMLParameters(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pParametersNode) {
16  std::string mode = "";
17  if (findXMLNode(pParametersNode, "Mode")) {
18  ReaderWriter::getStringValue(mode, pParametersNode, "Mode");
19  name = mode;
20  }
21 
22  if (findXMLNode(pParametersNode, "YValue")) {
23  y = ReaderWriter::getFloatValue(pParametersNode, "YValue");
24  }
25  else {
26  y = RE_DEFAULT_HEIGHT / 2;
27  }
28 
29  if (findXMLNode(pParametersNode, "Height")) {
30  height = ReaderWriter::getFloatValue(pParametersNode, "Height");
31  }
32  else {
33  height = RE_DEFAULT_HEIGHT / 3;
34  }
35 
36  if (findXMLNode(pParametersNode, "Width")) {
37  width = ReaderWriter::getFloatValue(pParametersNode, "Width");
38  }
39  else {
40  width = RE_DEFAULT_HEIGHT / 3;
41  }
42 
43  htype = (mode == "door" ? ht_door : ht_window);
44  return true;
45  }
46 
47  virtual void init(osgUtil::LineSegmentIntersector::Intersection* intersection,
48  WG_Wall* curWall,
49  WG_Point* curPoint,
51  vrecko::EnvironmentObject* pEOOwner,
52  std::vector<osg::ref_ptr<WG_Wall> >* walls,
53  std::vector<osg::ref_ptr<WG_Point> >* points,
54  osg::Geode* pOSGGeode,
55  osg::Vec3Array* pOSGVertexArray) {
56 
57  if (curWall != NULL) {
58  osg::Vec2 iPoint = osg::Vec2(intersection->getLocalIntersectPoint().x(), intersection->getLocalIntersectPoint().z());
59  osg::Vec2 iNormal = curWall->getVector(WG_Q_POINT);
60 
61  osg::Vec2 curNormal(-curWall->getVector(WG_Q_POINT).y(),curWall->getVector(WG_Q_POINT).x());
62 
63  osg::Vec2 crossPoint;
64  WG_Geometry::getCrossingPoint(crossPoint, curWall->getPoint(WG_Q_POINT)->getPosition(), curNormal, iPoint, iNormal, false);
65 
66  osg::Vec2 Q_cross = curWall->getPoint(WG_Q_POINT)->getPosition() - crossPoint;
67  float candPosition = Q_cross.length();
68 
69  bool canAdd = true;
70 
71  // test closeness to the edges
72 #define MIN_DISTANCE_FROM_EDGE 3.0
73  if ((candPosition - width/2 < MIN_DISTANCE_FROM_EDGE) ||
74  (candPosition + width/2 > ((curWall->getPoint(WG_Q_POINT)->getPosition() - curWall->getPoint(WG_R_POINT)->getPosition()).length() - MIN_DISTANCE_FROM_EDGE)))
75  canAdd = false;
76 
77 
78  // test if there is already another hole too close to the point where the user
79  // wants to place new one
80  for (unsigned int i=0; i<curWall->getHoles()->size();i++) {
81  if (candPosition < curWall->getHoles()->at(i).getPosition()) {
82  if ((candPosition + width/2) > (curWall->getHoles()->at(i).getPosition() - curWall->getHoles()->at(i).getWidth()/2 - 1.0)) {
83  canAdd = false;
84  break;
85  }
86  }
87  else {
88  if ((candPosition - width/2) < (curWall->getHoles()->at(i).getPosition() + curWall->getHoles()->at(i).getWidth()/2 + 1.0)) {
89  canAdd = false;
90  break;
91  }
92  }
93  }
94 
95  if (!canAdd)
96  logger.warningLog("Too close to another hole or edge");
97  else {
98  WG_Hole hole(candPosition, y, height, width, htype);
99  int i = 0;
100  while(i < (int)curWall->getHoles()->size()) {
101  if ((*curWall->getHoles())[i].getPosition() > candPosition)
102  break;
103  ++i;
104  }
105  curWall->getHoles()->insert(curWall->getHoles()->begin() + i, hole);
106  curWall->computeAttrVertices();
107  }
108  }
109  setDirty(true);
110  }
111 
112  private:
113  HoleType htype;
114  float y;
115  float height;
116  float width;
117 
118  };
119 
120 }
121 
122 #endif
123