Code_TYMPAN  4.4.0
Industrial site acoustic simulation
BBox.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 #ifndef BBOX_H
17 #define BBOX_H
18 
19 #include <iostream>
20 #include "Geometry/mathlib.h"
21 #include <algorithm>
22 
29 using namespace std;
30 
31 class BBox
32 {
33 public:
34  // public data
38  bool isNull;
39 
40  // public methods
42  BBox() : isNull(true) {}
43 
49  BBox(const vec3& _pMin, const vec3& _pMax) : isNull(false)
50  {
51  pMin = _pMin;
52  pMax = _pMax;
53  centroid = (pMin + pMax) / 2.0f;
54  }
55 
57  BBox(const BBox& other)
58  {
59  pMin = vec3(other.pMin);
60  pMax = vec3(other.pMax);
61  centroid = (pMin + pMax) / 2.0f;
62  isNull = other.isNull;
63  }
64 
67  {
68  return pMin;
69  }
70 
73  {
74  return pMax;
75  }
76 
79  {
80  return centroid;
81  }
82 
84  void setBBMin(vec3& _pMin)
85  {
86  pMin = _pMin;
87  centroid = (pMin + pMax) / 2.0f;
88  }
89 
91  void setBBMax(vec3& _pMax)
92  {
93  pMax = _pMax;
94  centroid = (pMin + pMax) / 2.0f;
95  }
96 
98  double SurfaceArea() const
99  {
100  vec3 d = pMax - pMin;
101  return 2 * (d.x * d.y + d.x * d.z + d.y * d.z);
102  }
103 
110  BBox Union(const BBox& b, const vec3& p)
111  {
112  BBox ret = b;
113  ret.pMin.x = min(b.pMin.x, p.x);
114  ret.pMin.y = min(b.pMin.y, p.y);
115  ret.pMin.z = min(b.pMin.z, p.z);
116  ret.pMax.x = max(b.pMax.x, p.x);
117  ret.pMax.y = max(b.pMax.y, p.y);
118  ret.pMax.z = max(b.pMax.z, p.z);
119  ret.centroid = (ret.pMin + ret.pMax) * 0.5;
120  return ret;
121  }
122 
128  BBox Union(const vec3& p)
129  {
130  BBox ret;
131  ret.isNull = false;
132  ret.pMin.x = min(pMin.x, p.x);
133  ret.pMin.y = min(pMin.y, p.y);
134  ret.pMin.z = min(pMin.z, p.z);
135  ret.pMax.x = max(pMax.x, p.x);
136  ret.pMax.y = max(pMax.y, p.y);
137  ret.pMax.z = max(pMax.z, p.z);
138  ret.centroid = (ret.pMin + ret.pMax) * 0.5;
139  return ret;
140  }
141 
147  BBox Union(const BBox& b2)
148  {
149  BBox ret;
150  if (isNull)
151  {
152  ret = b2;
153  }
154  else if (b2.isNull)
155  {
156  ret = *this;
157  }
158  else
159  {
160  ret.isNull = false;
161  ret.pMin.x = min(pMin.x, b2.pMin.x);
162  ret.pMin.y = min(pMin.y, b2.pMin.y);
163  ret.pMin.z = min(pMin.z, b2.pMin.z);
164  ret.pMax.x = max(pMax.x, b2.pMax.x);
165  ret.pMax.y = max(pMax.y, b2.pMax.y);
166  ret.pMax.z = max(pMax.z, b2.pMax.z);
167  ret.centroid = (ret.pMin + ret.pMax) * 0.5;
168  }
169  return ret;
170  }
171 
178  BBox Union(const BBox& b1, const BBox& b2)
179  {
180  BBox ret = b1;
181  ret.pMin.x = min(b1.pMin.x, b2.pMin.x);
182  ret.pMin.y = min(b1.pMin.y, b2.pMin.y);
183  ret.pMin.z = min(b1.pMin.z, b2.pMin.z);
184  ret.pMax.x = max(b1.pMax.x, b2.pMax.x);
185  ret.pMax.y = max(b1.pMax.y, b2.pMax.y);
186  ret.pMax.z = max(b1.pMax.z, b2.pMax.z);
187  ret.centroid = (ret.pMin + ret.pMax) * 0.5;
188  return ret;
189  }
190 
195  inline bool isInBox(const vec3& p) const
196  {
197  return p.x >= pMin.x && p.y >= pMin.y && p.z >= pMin.z && p.x <= pMax.x && p.y <= pMax.y &&
198  p.z <= pMax.z;
199  }
200 
205  inline bool intersectBox(const BBox& box)
206  {
207  if (isNull || box.isNull)
208  {
209  return false;
210  }
211  bool x = (pMax.x >= box.pMin.x) && (pMin.x <= box.pMax.x);
212  bool y = (pMax.y >= box.pMin.y) && (pMin.y <= box.pMax.y);
213  bool z = (pMax.z >= box.pMin.z) && (pMin.z <= box.pMax.z);
214  return (x && y && z);
215  }
216 
218  int MaximumExtend() const
219  {
220  vec3 diag = pMax - pMin;
221  if (diag.x > diag.y && diag.x > diag.z)
222  {
223  return 0;
224  }
225  else if (diag.y > diag.z)
226  {
227  return 1;
228  }
229  else
230  {
231  return 2;
232  }
233  }
234 
243  bool IntersectP(vec3 rayPos, vec3 rayDir, decimal* hitt0 = NULL, decimal* hitt1 = NULL) const
244  {
245  if (isNull)
246  {
247  return false;
248  }
249 
250  // float t0 = ray->mint; float t1 = ray->maxt;
251  decimal t0 = 0, t1 = 1000000;
252  decimal invRayDir, tNear, tFar;
253 
254  // Composante sur X
255  // TODO : If denominator is Zero
256  invRayDir = 1 / rayDir.x;
257  tNear = (pMin.x - rayPos.x) * invRayDir;
258  tFar = (pMax.x - rayPos.x) * invRayDir;
259 
260  if (tNear > tFar)
261  {
262  swap(tNear, tFar);
263  }
264  t0 = tNear > t0 ? tNear : t0;
265  t1 = tFar < t1 ? tFar : t1;
266  if (t0 > t1)
267  {
268  return false;
269  }
270 
271  // Composante sur Y
272  // TODO : If denominator is Zero
273  invRayDir = 1 / rayDir.y;
274  tNear = (pMin.y - rayPos.y) * invRayDir;
275  tFar = (pMax.y - rayPos.y) * invRayDir;
276 
277  if (tNear > tFar)
278  {
279  swap(tNear, tFar);
280  }
281  t0 = tNear > t0 ? tNear : t0;
282  t1 = tFar < t1 ? tFar : t1;
283  if (t0 > t1)
284  {
285  return false;
286  }
287 
288  // Composante sur Z
289  // TODO : If denominator is Zero
290  invRayDir = 1 / rayDir.z;
291  tNear = (pMin.z - rayPos.z) * invRayDir;
292  tFar = (pMax.z - rayPos.z) * invRayDir;
293 
294  if (tNear > tFar)
295  {
296  swap(tNear, tFar);
297  }
298  t0 = tNear > t0 ? tNear : t0;
299  t1 = tFar < t1 ? tFar : t1;
300  if (t0 > t1)
301  {
302  return false;
303  }
304 
305  if (hitt0)
306  {
307  *hitt0 = t0;
308  }
309  if (hitt1)
310  {
311  *hitt1 = t1;
312  }
313  return true;
314  }
315 
316  /* Not used (and seems not coded)
317  void print()
318  {
319  if (isNull) //std::cout << "Non initialisee." << std::endl;
320  {
321  }
322  else //std::cout << "Initialisee." << std::endl;
323  {
324  }
325  } */
326 
328  vec3& operator[](int i)
329  {
330  if (i == 0)
331  {
332  return pMin;
333  }
334  else
335  {
336  return pMax;
337  }
338  }
340  const vec3& operator[](int i) const
341  {
342  if (i == 0)
343  {
344  return pMin;
345  }
346  else
347  {
348  return pMax;
349  }
350  }
351 
354  {
355  return sqrt((pMax.x - pMin.x) * (pMax.x - pMin.x) + (pMax.y - pMin.y) * (pMax.y - pMin.y) +
356  (pMax.z - pMin.z) * (pMax.z - pMin.z));
357  }
358 
360  bool Inside(const vec3& pt) const
361  {
362  return (pt.x >= pMin.x && pt.x <= pMax.x && pt.y >= pMin.y && pt.y <= pMax.y && pt.z >= pMin.z &&
363  pt.z <= pMax.z);
364  }
365 };
366 
367 #endif // BBOX_H
#define min(a, b)
Definition of a bounding box which is aligned along the axis (BBox AABB).
Definition: BBox.h:32
vec3 pMax
Upper point of the BBox.
Definition: BBox.h:36
bool IntersectP(vec3 rayPos, vec3 rayDir, decimal *hitt0=NULL, decimal *hitt1=NULL) const
Compute the intersection between a Ray and this BBox.
Definition: BBox.h:243
BBox Union(const BBox &b, const vec3 &p)
Union of a point and a BBox. A new BBox is created.
Definition: BBox.h:110
decimal diag()
Diagonal length of the BBox.
Definition: BBox.h:353
void setBBMax(vec3 &_pMax)
Set the upper point (center point is recomputed)
Definition: BBox.h:91
BBox Union(const BBox &b2)
Union of a BBox and the current one. A new BBox is created.
Definition: BBox.h:147
vec3 centroid
Center point of the BBox.
Definition: BBox.h:37
vec3 getCentroid()
Return the center point.
Definition: BBox.h:78
bool Inside(const vec3 &pt) const
Return true if the point pt is inside the BBox.
Definition: BBox.h:360
vec3 pMin
Lower point of the BBox.
Definition: BBox.h:35
BBox(const BBox &other)
Copy constructor.
Definition: BBox.h:57
double SurfaceArea() const
Return the BBox area (sum of the lateral areas). Used for the SAH calculation of the accelerators.
Definition: BBox.h:98
BBox(const vec3 &_pMin, const vec3 &_pMax)
Constructor with initialization of the 3 points of the BBox.
Definition: BBox.h:49
void setBBMin(vec3 &_pMin)
Set the lower point (center point is recomputed)
Definition: BBox.h:84
bool intersectBox(const BBox &box)
Test the intersection of a BBox with this one.
Definition: BBox.h:205
BBox Union(const BBox &b1, const BBox &b2)
Union of two BBox. A new BBox is created.
Definition: BBox.h:178
vec3 getBBMax()
Return the upper point.
Definition: BBox.h:72
int MaximumExtend() const
Return the index of the dominant direction (maximal dimension). Index is 0 for x, 1 for y,...
Definition: BBox.h:218
const vec3 & operator[](int i) const
Return the lower (0) or upper (1) point.
Definition: BBox.h:340
bool isNull
True if the BBox is initialized, false if not.
Definition: BBox.h:38
BBox()
Default constructor.
Definition: BBox.h:42
bool isInBox(const vec3 &p) const
Check if a point is inside the BBox.
Definition: BBox.h:195
vec3 getBBMin()
Return the lower point.
Definition: BBox.h:66
BBox Union(const vec3 &p)
Union of a point and the current (this) BBox. A new BBox is created.
Definition: BBox.h:128
vec3 & operator[](int i)
Return the lower (0) or upper (1) point.
Definition: BBox.h:328
float decimal
Definition: mathlib.h:45
base_vec3< decimal > vec3
Definition: mathlib.h:381