source: GTP/trunk/Lib/Vis/Preprocessing/src/AxisAlignedBox3.h @ 859

Revision 859, 17.5 KB checked in by bittner, 18 years ago (diff)

apply filter routine for working modules

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