V-ART
boundingbox.cpp
Go to the documentation of this file.
1 
5 #include "vart/boundingbox.h"
6 #include "vart/transform.h"
7 
8 using namespace std;
9 
11  visible = false;
12  color = VART::Color::RED();
13 }
14 
15 VART::BoundingBox::BoundingBox(double minX, double minY, double minZ,
16  double maxX, double maxY, double maxZ) {
17  visible = false;
18  smallerX = minX;
19  smallerY = minY;
20  smallerZ = minZ;
21  greaterX = maxX;
22  greaterY = maxY;
23  greaterZ = maxZ;
24  color = VART::Color::RED();
25  ProcessCenter();
26 }
27 
29  visible = box.visible;
30  smallerX = box.smallerX;
31  smallerY = box.smallerY;
32  smallerZ = box.smallerZ;
33  greaterX = box.greaterX;
34  greaterY = box.greaterY;
35  greaterZ = box.greaterZ;
36  center = box.center;
37  color = box.color;
38 }
39 
41  visible = box.visible;
42  smallerX = box.smallerX;
43  smallerY = box.smallerY;
44  smallerZ = box.smallerZ;
45  greaterX = box.greaterX;
46  greaterY = box.greaterY;
47  greaterZ = box.greaterZ;
48  center = box.center;
49  color = box.color;
50  return *this;
51 }
52 
54  smallerX = box.smallerX;
55  smallerY = box.smallerY;
56  smallerZ = box.smallerZ;
57  greaterX = box.greaterX;
58  greaterY = box.greaterY;
59  greaterZ = box.greaterZ;
60  center = box.center;
61 }
62 
63 
65  center.SetXYZW((greaterX + smallerX)/2,
66  (greaterY + smallerY)/2,
67  (greaterZ + smallerZ)/2, 1);
68 }
69 
71  VART::BoundingBox box = *this;
72 
73  VART::Point4D ptoBBox = trans * VART::Point4D(box.GetSmallerX(),box.GetSmallerY(),box.GetSmallerZ());
74  this->SetBoundingBox(ptoBBox.GetX(),ptoBBox.GetY(),ptoBBox.GetZ(),ptoBBox.GetX(),ptoBBox.GetY(),ptoBBox.GetZ());
75  this->ConditionalUpdate(trans * VART::Point4D(box.GetSmallerX(), box.GetSmallerY(), box.GetGreaterZ()));
76  this->ConditionalUpdate(trans * VART::Point4D(box.GetSmallerX(), box.GetGreaterY(), box.GetGreaterZ()));
77  this->ConditionalUpdate(trans * VART::Point4D(box.GetSmallerX(), box.GetGreaterY(), box.GetSmallerZ()));
78  this->ConditionalUpdate(trans * VART::Point4D(box.GetGreaterX(), box.GetSmallerY(), box.GetSmallerZ()));
79  this->ConditionalUpdate(trans * VART::Point4D(box.GetGreaterX(), box.GetSmallerY(), box.GetGreaterZ()));
80  this->ConditionalUpdate(trans * VART::Point4D(box.GetGreaterX(), box.GetGreaterY(), box.GetGreaterZ()));
81  this->ConditionalUpdate(trans * VART::Point4D(box.GetGreaterX(), box.GetGreaterY(), box.GetSmallerZ()));
82  ProcessCenter();
83 }
84 
86  return greaterX;
87 }
88 
90  return greaterY;
91 }
92 
94  return greaterZ;
95 }
96 
98  greaterX=v;
99 }
100 
102  greaterY=v;
103 }
104 
106  greaterZ=v;
107 }
108 
110  return smallerX;
111 }
112 
114  return smallerY;
115 }
116 
118  return smallerZ;
119 }
120 
122  smallerX=v;
123 }
124 
126  smallerY=v;
127 }
128 
130  smallerZ=v;
131 }
132 
133 void VART::BoundingBox::SetBoundingBox(double minX, double minY, double minZ,
134  double maxX, double maxY, double maxZ) {
135  smallerX = minX;
136  smallerY = minY;
137  smallerZ = minZ;
138  greaterX = maxX;
139  greaterY = maxY;
140  greaterZ = maxZ;
141  ProcessCenter();
142 }
143 
145  return center;
146 }
147 
148 void VART::BoundingBox::ConditionalUpdate(double x, double y, double z) {
149  if (x < smallerX)
150  smallerX = x;
151  else {
152  if (x > greaterX) greaterX = x;
153  }
154  if (y < smallerY)
155  smallerY = y;
156  else {
157  if (y > greaterY) greaterY = y;
158  }
159  if (z < smallerZ)
160  smallerZ = z;
161  else {
162  if (z > greaterZ) greaterZ = z;
163  }
164 }
165 
167  ConditionalUpdate(point.GetX(), point.GetY(), point.GetZ());
168 }
169 
171  ConditionalUpdate(box.smallerX, box.smallerY, box.smallerZ);
172  ConditionalUpdate(box.greaterX, box.greaterY, box.greaterZ);
173 }
174 
176  double greater = greaterX - smallerX;
177  double tmp = greaterY - smallerY;
178 
179  if (tmp > greater)
180  greater = tmp;
181  tmp = greaterZ - smallerZ;
182  if (tmp > greater)
183  greater = tmp;
184  return greater;
185 }
186 
188  double smaller = greaterX - smallerX;
189  double tmp = greaterY - smallerY;
190 
191  if (tmp < smaller)
192  smaller = tmp;
193  tmp = greaterZ - smallerZ;
194  if (tmp < smaller)
195  smaller = tmp;
196  return smaller;
197 }
198 
200  visible = !visible;
201 }
202 
203 // Should be called if the bounding box is visible
205 #ifdef VART_OGL
206  static float fVec[4];
207 
208  glDisable(GL_LIGHTING); // FixMe: check if lighting is enabled
209  color.Get(fVec);
210  glColor4fv(fVec);
211  glBegin (GL_LINE_LOOP);
212  glVertex3d (smallerX, greaterY, smallerZ);
213  glVertex3d (greaterX, greaterY, smallerZ);
214  glVertex3d (greaterX, smallerY, smallerZ);
215  glVertex3d (smallerX, smallerY, smallerZ);
216  glEnd();
217  glBegin(GL_LINE_LOOP);
218  glVertex3d (smallerX, smallerY, smallerZ);
219  glVertex3d (smallerX, smallerY, greaterZ);
220  glVertex3d (smallerX, greaterY, greaterZ);
221  glVertex3d (smallerX, greaterY, smallerZ);
222  glEnd();
223  glBegin(GL_LINE_LOOP);
224  glVertex3d (greaterX, greaterY, greaterZ);
225  glVertex3d (smallerX, greaterY, greaterZ);
226  glVertex3d (smallerX, smallerY, greaterZ);
227  glVertex3d (greaterX, smallerY, greaterZ);
228  glEnd();
229  glBegin(GL_LINE_LOOP);
230  glVertex3d (greaterX, smallerY, smallerZ);
231  glVertex3d (greaterX, greaterY, smallerZ);
232  glVertex3d (greaterX, greaterY, greaterZ);
233  glVertex3d (greaterX, smallerY, greaterZ);
234  glEnd();
235  glEnable(GL_LIGHTING);
236  return true;
237 #else
238  return false;
239 #endif
240 }
241 
243 {
244  if (b.greaterX < smallerX)
245  return false;
246  if (b.smallerX > greaterX)
247  return false;
248  if (b.greaterZ < smallerZ)
249  return false;
250  if (b.smallerZ > greaterZ)
251  return false;
252  if (b.greaterY < smallerY)
253  return false;
254  if (b.smallerY > greaterY)
255  return false;
256  return true;
257 }
258 
260 {
261  if (p.GetX() < smallerX)
262  return false;
263  if (p.GetX() > greaterX)
264  return false;
265  if (p.GetZ() < smallerZ)
266  return false;
267  if (p.GetZ() > greaterZ)
268  return false;
269  if (p.GetY() < smallerY)
270  return false;
271  if (p.GetY() > greaterY)
272  return false;
273  return true;
274 
275 }
276 
278 {
279  if (box.greaterX < greaterX)
280  greaterX = box.greaterX;
281  if (box.smallerX > smallerX)
282  smallerX = box.smallerX;
283  if (box.greaterZ < greaterZ)
284  greaterZ = box.greaterZ;
285  if (box.smallerZ > smallerZ)
286  smallerZ = box.smallerZ;
287  if (box.greaterY < greaterY)
288  greaterY = box.greaterY;
289  if (box.smallerY > smallerY)
290  smallerY = box.smallerY;
291 }
292 
293 ostream& VART::operator<<(ostream& output, const VART::BoundingBox& box)
294 {
295  output << "(Greaters: " << box.greaterX << "," << box.greaterY << ","
296  << box.greaterZ << " Smallers: " << box.smallerX << ","
297  << box.smallerY << "," << box.smallerZ << ")";
298  return output;
299 }
bool DrawInstanceOGL() const
Draws a bounding box.
BoundingBox()
Creates an uninitialized bounding box.
Definition: boundingbox.cpp:10
static const Color & RED()
Red opaque color.
Definition: color.cpp:75
Points and vectors using homogeneous coordinates.
Definition: point4d.h:22
bool visible
Indicates wether the bounding box is visible.
Definition: boundingbox.h:91
void CutBBox(const BoundingBox &box)
Test against other bounding box and update itself if its outside the given bbox.
void SetBoundingBox(double minX, double minY, double minZ, double maxX, double maxY, double maxZ)
void ConditionalUpdate(double x, double y, double z)
Updates the bounding box if any given coordinate is outside it.
double GetZ() const
Definition: point4d.h:80
void CopyGeometryFrom(const BoundingBox &box)
Copies geometry data from other bounding box.
Definition: boundingbox.cpp:53
std::ostream & operator<<(std::ostream &output, const Joint::DofID &dofId)
void MergeWith(const BoundingBox &box)
Merges a bounding with another, expanding it.
double GetY() const
Definition: point4d.h:79
Axis aligned bounding box.
Definition: boundingbox.h:23
BoundingBox & operator=(const BoundingBox &box)
Definition: boundingbox.cpp:40
double GetGreaterZ() const
Definition: boundingbox.cpp:93
double GetSmallerX() const
bool testPoint(VART::Point4D p)
Test if a point is included in the bbox.
Geometric transformations.
Definition: transform.h:24
double GetSmallerY() const
Header file for V-ART class "BoundingBox".
Header file for V-ART class "Transform".
void SetGreaterY(double v)
double GetGreaterY() const
Definition: boundingbox.cpp:89
void ToggleVisibility()
Toggles the visibility.
double GetSmallerEdge() const
Returns the length of the smaller edge.
void SetSmallerZ(double v)
void SetGreaterX(double v)
Definition: boundingbox.cpp:97
const Point4D & GetCenter() const
void ApplyTransform(const Transform &trans)
Applies transformation to a BoundingBox.
Definition: boundingbox.cpp:70
double GetGreaterEdge() const
Returns the length of the greater edge.
bool testAABBAABB(BoundingBox &b)
Test intersection among AABBs.
void SetSmallerX(double v)
void SetSmallerY(double v)
double GetX() const
Definition: point4d.h:78
double GetGreaterX() const
Definition: boundingbox.cpp:85
void SetGreaterZ(double v)
double GetSmallerZ() const