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

Revision 1715, 17.4 KB checked in by bittner, 18 years ago (diff)

new visibility filter support

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