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

Revision 170, 14.9 KB checked in by bittner, 19 years ago (diff)

mesh kd tree added

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