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

Revision 318, 15.7 KB checked in by mattausch, 19 years ago (diff)

VisibilityInfo?: query sort operators as templates
X3dExporter: Fixed polygon wire frame, bsp splits, and bsp split planes visualization
ViewCellBsp?: Added rays to split criteria (not finished yet)
OgreOctreeSceneManager?: fixed error (mNumOctants instead of mNumOctrees)

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