source: trunk/VUT/GtpVisibilityPreprocessor/src/AxisAlignedBox3.h @ 162

Revision 162, 14.8 KB checked in by bittner, 19 years ago (diff)

functional raycasting version

Line 
1#ifndef _AxisAlignedBox3_H__
2#define _AxisAlignedBox3_H__
3
4
5#include "Matrix4x4.h"
6#include "Vector3.h"
7#include "Plane3.h"
8
9class Plane3;
10class Ray;
11
12
13// --------------------------------------------------------
14// CAABox class.
15//  This is a box in 3-space, defined by min and max
16//  corner vectors.  Many useful operations are defined
17//  on this
18// --------------------------------------------------------
19class AxisAlignedBox3
20{
21protected:
22  Vector3 mMin, mMax;
23public:
24  // Constructors.
25  AxisAlignedBox3() { }
26  AxisAlignedBox3(const Vector3 &nMin, const Vector3 &nMax)
27  {
28    mMin = nMin; mMax = nMax;
29  }
30
31  //  AxisAlignedBox3(const Vector3 &center, const float radius):min(center - Vector3(radius)),
32  //                                                  max(center + Vector3(radius)) {}
33
34  // initialization to the non existing bounding box
35  void Initialize() {
36    mMin = Vector3(MAXFLOAT);
37    mMax = Vector3(-MAXFLOAT);
38  }
39
40  // The center of the box
41  Vector3 Center() const { return 0.5 * (mMin + mMax); }
42 
43  // The diagonal of the box
44  Vector3 Diagonal() const { return (mMax -mMin); }
45
46  float Center(const int axis) const {
47    return  0.5 * (mMin[axis] + mMax[axis]);
48  }
49
50  float Min(const int axis) const {
51    return mMin[axis];
52  }
53
54  float Max(const int axis) const {
55    return  mMax[axis];
56  }
57 
58  // Read-only const access tomMin and max vectors using references
59  const Vector3& Min() const { return mMin;}
60  const Vector3& Max() const { return mMax;}
61
62  void Enlarge (const Vector3 &v) {
63    mMax += v;
64    mMin -= v;
65  }
66
67  void SetMin(int axis, const float value) {
68   mMin[axis] = value;
69  }
70
71  void SetMax(int axis, const float value) {
72    mMax[axis] = value;
73  }
74
75  // Decrease box by given splitting plane
76  void Reduce(int axis, int right, float value) {
77    if ( (value >=mMin[axis]) && (value <= mMax[axis]) )
78      if (right)
79       mMin[axis] = value;
80      else
81        mMax[axis] = value;
82  }
83
84  // the size of the box along all the axes
85  Vector3 Size() const { return mMax -mMin; }
86
87  // Return whether the box is unbounded.  Unbounded boxes appear
88  // when unbounded objects such as quadric surfaces are included.
89  bool Unbounded() const;
90
91  // Expand the axis-aligned box to include the given object.
92  void Include(const Vector3 &newpt);
93  void Include(const AxisAlignedBox3 &bbox);
94  // Expand the axis-aligned box to include given values in particular axis
95  void Include(const int &axis, const float &newBound);
96
97 
98
99  // Overlap returns 1 if the two axis-aligned boxes overlap .. even weakly
100  friend inline bool Overlap(const AxisAlignedBox3 &, const AxisAlignedBox3 &);
101
102  // Overlap returns 1 if the two axis-aligned boxes overlap .. only strongly
103  friend inline bool OverlapS(const AxisAlignedBox3 &,const AxisAlignedBox3 &);
104
105  // Overlap returns 1 if the two axis-aligned boxes overlap for a given
106  // epsilon. If eps > 0.0, then the boxes has to have the real intersection
107  // box, if eps < 0.0, then the boxes need not intersect really, they
108  // can be at eps distance in the projection
109  friend inline bool Overlap(const AxisAlignedBox3 &,
110                             const AxisAlignedBox3 &,
111                             float eps);
112
113  // Includes returns true if a includes b (completely
114  bool Includes(const AxisAlignedBox3 &b) const;
115
116  virtual int IsInside(const Vector3 &v) const;
117
118  // Test if the box is really sensefull
119  virtual bool IsCorrect();
120
121  // To answer true requires the box of real volume of non-zero value
122  bool IsSingularOrIncorrect() const;
123
124  // When the box is not of non-zero or negative surface area
125  bool IsCorrectAndNotPoint() const;
126
127  // Returns true when the box degenerates to a point
128  bool IsPoint() const;
129
130  void
131  GetSqrDistances(const Vector3 &point,
132                  float &minDistance,
133                  float &maxDistance
134                  ) const;
135
136  // returns true, when the sphere specified by the origin and radius
137  // fully contains the box
138  bool IsFullyContainedInSphere(const Vector3 &center, float radius) const;
139
140  // returns true, when the volume of the sphere and volume of the
141  // axis aligned box has no intersection
142  bool HasNoIntersectionWithSphere(const Vector3 &center,
143                                   float radius) const;
144
145
146  // Given a sphere described by the center and radius,
147  // the fullowing function returns:
148  //   -1 ... the sphere and the box are completely separate
149  //    0 ... the sphere and the box only partially overlap
150  //    1 ... the sphere contains fully the box
151  //  Note: the case when box fully contains the sphere is not reported
152  //        since it was not required.
153  int MutualPositionWithSphere(const Vector3 &center, float radius) const;
154
155  // Given a cube described by the center and half-size (radius),
156  // the following function returns:
157  //   -1 ... the cube and the box are completely separate
158  //    0 ... the cube and the box only partially overlap
159  //    1 ... the cube contains fully the box
160  int MutualPositionWithCube(const Vector3 &center, float halfSize) const;
161 
162  // Returns the smallest axis-aligned box that includes all points
163  // inside the two given boxes.
164  friend inline AxisAlignedBox3 Union(const AxisAlignedBox3 &x,
165                             const AxisAlignedBox3 &y);
166
167  // Returns the intersection of two axis-aligned boxes.
168  friend inline AxisAlignedBox3 Intersect(const AxisAlignedBox3 &x,
169                                 const AxisAlignedBox3 &y);
170
171  // Given 4x4 matrix, transform the current box to new one.
172  friend inline AxisAlignedBox3 Transform(const AxisAlignedBox3 &box,
173                                          const Matrix4x4 &tform);
174
175  // returns true when two boxes are completely equal
176  friend inline int operator== (const AxisAlignedBox3 &A, const AxisAlignedBox3 &B);
177 
178  virtual float SurfaceArea() const;
179  virtual float GetVolume() const {
180    return (mMax.x -mMin.x) * (mMax.y -mMin.y) * (mMax.z - mMin.z);
181  }
182
183  // Six faces are distuinguished by their name.
184  enum EFaces { ID_Back = 0, ID_Left = 1, ID_Bottom = 2, ID_Front = 3,
185                ID_Right = 4, ID_Top = 5};
186 
187  // Compute tmin and tmax for a ray, whenever required .. need not pierce box
188  int ComputeMinMaxT(const Ray &ray, float *tmin, float *tmax) const;
189
190  // Compute tmin and tmax for a ray, whenever required .. need not pierce box
191  int ComputeMinMaxT(const Ray &ray, float *tmin, float *tmax,
192                     EFaces &entryFace, EFaces &exitFace) const;
193 
194  // If a ray pierces the box .. returns 1, otherwise 0.
195  // Computes the signed distances for case: tmin < tmax and tmax > 0
196  int GetMinMaxT(const Ray &ray, float *tmin, float *tmax) const;
197  // computes the signed distances for case: tmin < tmax and tmax > 0
198  int GetMinMaxT(const Ray &ray, float *tmin, float *tmax,
199                 EFaces &entryFace, EFaces &exitFace) const;
200 
201  // Writes a brief description of the object, indenting by the given
202  // number of spaces first.
203  virtual void Describe(ostream& app, int ind) const;
204
205  // For edge .. number <0..11> returns two incident vertices
206  void GetEdge(const int edge, Vector3 *a, Vector3 *b) const;
207
208  // Compute the coordinates of one vertex of the box for 0/1 in each axis
209  // 0 .. smaller coordinates, 1 .. large coordinates
210  Vector3 GetVertex(int xAxis, int yAxis, int zAxis) const;
211
212  // Compute the vertex for number N=<0..7>, N = 4*x + 2*y + z, where
213  // x,y,z are either 0 or 1; (0 .. lower coordinate, 1 .. large coordinate)
214  // (xmin,ymin, zmin) .. N = 0, (xmax, ymax, zmax) .. N= 7
215  void GetVertex(int N, Vector3 &vertex) const;
216 
217  // Returns 1, if the box includes on arbitrary face a given box
218  int IsPiercedByBox(const AxisAlignedBox3 &box, int &axis) const;
219
220  // For a given point returns the region, where the point is located
221  // there are 27 regions (0..26) .. determined by the planes embedding in the
222  // sides of the bounding box (0 .. lower the position of the box,
223  // 1 .. inside the box, 2 .. greater than box). The region number is given as
224  // R = 9*x + 3*y + z  ; e.g. region .. inside the box is 13.
225  int GetRegionID(const Vector3 &point) const;
226
227  // Set the corner point of rectangle on the face of bounding box
228  // given by the index number and the rectangle lying on this face
229  //  void GetFaceRectCorner(const CRectLeaf2D *rect, EFaces faceIndx,
230  //                     const int &cornerIndx, Vector3 &cornerPoint);
231
232  // Project the box to a plane given a normal vector of this plane. Computes
233  // the surface area of projected silhouettes for parallel projection.
234  float ProjectToPlaneSA(const Vector3 &normal) const;
235
236  // Computes projected surface area of the box to a given viewing plane
237  // given a viewpoint. This corresponds the probability, the box will
238  // be hit by the ray .. moreover returns .. the region number (0-26).
239  // the function supposes all the points lie of the box lies in the viewing
240  // frustrum !!! The positive halfspace of viewplane has to contain
241  // viewpoint. "projectionType" == 0 .. perspective projection,
242  // == 1 .. parallel projection.
243  float ProjectToPlaneSA(const Plane3 &viewplane,
244                         const Vector3 &viewpoint,
245                         int *tcase,
246                         const float &maxSA,
247                         int projectionType) const;
248
249  // Computes projected surface area of the box to a given viewing plane
250  // and viewpoint. It clipps the area by all the planes given .. they should
251  // define the viewing frustrum. Variable tclip defines, which planes are
252  // used for clipping, parameter 31 is the most general, clip all the plane.
253  // 1 .. clip left, 2 .. clip top, 4 .. clip right, 8 .. clip bottom,
254  // 16 .. clip supporting plane(its normal towards the viewing frustrum).
255  // "typeProjection" == 0 .. perspective projection,
256  // == 1 .. parallel projection
257  float ProjectToPlaneSA(const Plane3 &viewplane,
258                         const Vector3 &viewpoint,
259                         int *tcase, int &tclip,
260                         const Plane3 &leftPlane,
261                         const Plane3 &topPlane,
262                         const Plane3 &rightPlane,
263                         const Plane3 &bottomPlane,
264                         const Plane3 &suppPlane,
265                         const float &maxSA,
266                         int typeProjection) const;
267
268  // Projects the box to a unit sphere enclosing a given viewpoint and
269  // returns the solid angle of the box projected to a unit sphere
270  float ProjectToSphereSA(const Vector3 &viewpoint, int *tcase) const;
271
272
273#define __EXTENT_HACK
274  // get the extent of face
275  float GetExtent(const int &face) const {
276#if defined(__EXTENT_HACK) && defined(__VECTOR_HACK)
277    return mMin[face];
278#else
279    if (face < 3)
280      return mMin[face];
281    else
282      return mMax[face-3];
283#endif
284  }
285
286  // The vertices that form boundaries of the projected bounding box
287  // for all the regions possible, number of regions is 3^3 = 27,
288  // since two parallel sides of bbox forms three disjoint spaces
289  // the vertices are given in anti-clockwise order .. stopped by -1 elem.
290  static const int bvertices[27][9];
291
292  // The list of all faces visible from a given region (except region 13)
293  // the faces are identified by triple: (axis, min-vertex, max-vertex),
294  // that is maximaly three triples are defined. axis = 0 (x-axis),
295  // axis = 1 (y-axis), axis = 2 (z-axis), -1 .. terminator. Is is always
296  // true that: min-vertex < max-vertex for all coordinates excluding axis
297  static const int bfaces[27][10];
298 
299  // The correct corners indexed starting from entry face to exit face
300  // first index determines entry face, second index exit face, and
301  // the two numbers (indx, inc) determines: ind = the index on the exit
302  // face, when starting from the vertex 0 on entry face, 'inc' is
303  // the increment when we go on entry face in order 0,1,2,3 to create
304  // convex shaft with the rectangle on exit face. That is, inc = -1 or 1.
305  static const int pairFaceRects[6][6][2];
306
307  // The vertices that form CLOSEST points with respect to the region
308  // for all the regions possible, number of regions is 3^3 = 27,
309  // since two parallel sides of bbox forms three disjoint spaces.
310  // The vertices are given in anti-clockwise order, stopped by -1 elem,
311  // at most 8 points, at least 1 point.
312  static const int cvertices[27][9];
313  static const int csvertices[27][6];
314
315  // The vertices that form FARTHEST points with respect to the region
316  // for all the regions possible, number of regions is 3^3 = 27,
317  // since two parallel sides of bbox forms three disjoint spaces.
318  // The vertices are given in anti-clockwise order, stopped by -1 elem,
319  // at most 8 points, at least 1 point.
320  static const int fvertices[27][9]; 
321  static const int fsvertices[27][9];
322
323  // input and output operator with stream
324  friend ostream& operator<<(ostream &s, const AxisAlignedBox3 &A);
325  friend istream& operator>>(istream &s, AxisAlignedBox3 &A);
326
327protected:
328  // definition of friend functions
329  friend class Ray;
330};
331
332// --------------------------------------------------------------------------
333// Implementation of inline (member) functions
334 
335inline bool
336Overlap(const AxisAlignedBox3 &x, const AxisAlignedBox3 &y)
337{
338  if (x.mMax.x < y.mMin.x ||
339      x.mMin.x > y.mMax.x ||
340      x.mMax.y < y.mMin.y ||
341      x.mMin.y > y.mMax.y ||
342      x.mMax.z < y.mMin.z ||
343      x.mMin.z > y.mMax.z) {
344    return false;
345  }
346  return true;
347}
348
349inline bool
350OverlapS(const AxisAlignedBox3 &x, const AxisAlignedBox3 &y)
351{
352  if (x.mMax.x <= y.mMin.x ||
353      x.mMin.x >= y.mMax.x ||
354      x.mMax.y <= y.mMin.y ||
355      x.mMin.y >= y.mMax.y ||
356      x.mMax.z <= y.mMin.z ||
357      x.mMin.z >= y.mMax.z) {
358    return false;
359  }
360  return true;
361}
362
363inline bool
364Overlap(const AxisAlignedBox3 &x, const AxisAlignedBox3 &y, float eps)
365{
366  if ( (x.mMax.x - eps) < y.mMin.x ||
367       (x.mMin.x + eps) > y.mMax.x ||
368       (x.mMax.y - eps) < y.mMin.y ||
369       (x.mMin.y + eps) > y.mMax.y ||
370       (x.mMax.z - eps) < y.mMin.z ||
371       (x.mMin.z + eps) > y.mMax.z ) {
372    return false;
373  }
374  return true;
375}
376
377inline AxisAlignedBox3
378Intersect(const AxisAlignedBox3 &x, const AxisAlignedBox3 &y)
379{
380  if (x.Unbounded())
381    return y;
382  else
383    if (y.Unbounded())
384      return x;
385  AxisAlignedBox3 ret = x;
386  if (Overlap(ret, y)) {
387    Maximize(ret.mMin, y.mMin);
388    Minimize(ret.mMax, y.mMax);
389    return ret;
390  }
391  else      // Null intersection.
392    return AxisAlignedBox3(Vector3(0), Vector3(0));
393  // return AxisAlignedBox3(Vector3(0), Vector3(-1));
394}
395
396inline AxisAlignedBox3
397Union(const AxisAlignedBox3 &x, const AxisAlignedBox3 &y)
398{
399  Vector3 min = x.mMin;
400  Vector3 max = x.mMax;
401  Minimize(min, y.mMin);
402  Maximize(max, y.mMax);
403  return AxisAlignedBox3(min, max);
404}
405
406inline AxisAlignedBox3
407Transform(const AxisAlignedBox3 &box, const Matrix4x4 &tform)
408{
409  Vector3 mmin(MAXFLOAT);
410  Vector3 mmax(-MAXFLOAT);
411
412  AxisAlignedBox3 ret(mmin, mmax);
413  ret.Include(tform * Vector3(box.mMin.x, box.mMin.y, box.mMin.z));
414  ret.Include(tform * Vector3(box.mMin.x, box.mMin.y, box.mMax.z));
415  ret.Include(tform * Vector3(box.mMin.x, box.mMax.y, box.mMin.z));
416  ret.Include(tform * Vector3(box.mMin.x, box.mMax.y, box.mMax.z));
417  ret.Include(tform * Vector3(box.mMax.x, box.mMin.y, box.mMin.z));
418  ret.Include(tform * Vector3(box.mMax.x, box.mMin.y, box.mMax.z));
419  ret.Include(tform * Vector3(box.mMax.x, box.mMax.y, box.mMin.z));
420  ret.Include(tform * Vector3(box.mMax.x, box.mMax.y, box.mMax.z));
421  return ret;
422}
423
424
425inline int operator==(const AxisAlignedBox3 &A, const AxisAlignedBox3 &B)
426{
427  return (A.mMin == B.mMin) && (A.mMax == B.mMax);
428}
429
430 
431
432
433
434#endif
Note: See TracBrowser for help on using the repository browser.