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

Revision 459, 16.4 KB checked in by bittner, 19 years ago (diff)

merge

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