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

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

global lines 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  Vector3 GetRandomSurfacePoint() const;
185        Vector3 GetUniformRandomSurfacePoint() const;
186
187  Vector3 GetPoint(const Vector3 &p) const {
188    return mMin + p*Size();
189  }
190
191  // Returns the smallest axis-aligned box that includes all points
192  // inside the two given boxes.
193  friend inline AxisAlignedBox3 Union(const AxisAlignedBox3 &x,
194                             const AxisAlignedBox3 &y);
195
196  // Returns the intersection of two axis-aligned boxes.
197  friend inline AxisAlignedBox3 Intersect(const AxisAlignedBox3 &x,
198                                 const AxisAlignedBox3 &y);
199
200  // Given 4x4 matrix, transform the current box to new one.
201  friend inline AxisAlignedBox3 Transform(const AxisAlignedBox3 &box,
202                                          const Matrix4x4 &tform);
203
204 
205  // returns true when two boxes are completely equal
206  friend inline int operator== (const AxisAlignedBox3 &A, const AxisAlignedBox3 &B);
207 
208  virtual float SurfaceArea() const;
209  virtual float GetVolume() const {
210    return (mMax.x - mMin.x) * (mMax.y - mMin.y) * (mMax.z - mMin.z);
211  }
212
213  // Six faces are distuinguished by their name.
214  enum EFaces { ID_Back = 0, ID_Left = 1, ID_Bottom = 2, ID_Front = 3,
215                ID_Right = 4, ID_Top = 5};
216 
217  int
218  ComputeMinMaxT(const Vector3 &origin,
219                                 const Vector3 &direction,
220                                 float *tmin,
221                                 float *tmax) const;
222       
223  // Compute tmin and tmax for a ray, whenever required .. need not pierce box
224  int ComputeMinMaxT(const Ray &ray, float *tmin, float *tmax) const;
225
226  // Compute tmin and tmax for a ray, whenever required .. need not pierce box
227  int ComputeMinMaxT(const Ray &ray,
228                                         float *tmin,
229                                         float *tmax,
230                                         EFaces &entryFace,
231                                         EFaces &exitFace) const;
232 
233  // If a ray pierces the box .. returns 1, otherwise 0.
234  // Computes the signed distances for case: tmin < tmax and tmax > 0
235  int GetMinMaxT(const Ray &ray, float *tmin, float *tmax) const;
236  // computes the signed distances for case: tmin < tmax and tmax > 0
237  int GetMinMaxT(const Ray &ray, float *tmin, float *tmax,
238                 EFaces &entryFace, EFaces &exitFace) const;
239 
240  // Writes a brief description of the object, indenting by the given
241  // number of spaces first.
242  virtual void Describe(ostream& app, int ind) const;
243
244  // For edge .. number <0..11> returns two incident vertices
245  void GetEdge(const int edge, Vector3 *a, Vector3 *b) const;
246
247  // Compute the coordinates of one vertex of the box for 0/1 in each axis
248  // 0 .. smaller coordinates, 1 .. large coordinates
249  Vector3 GetVertex(int xAxis, int yAxis, int zAxis) const;
250
251  // Compute the vertex for number N=<0..7>, N = 4*x + 2*y + z, where
252  // x,y,z are either 0 or 1; (0 .. lower coordinate, 1 .. large coordinate)
253  // (xmin,ymin, zmin) .. N = 0, (xmax, ymax, zmax) .. N= 7
254  void GetVertex(const int N, Vector3 &vertex) const;
255
256  Vector3 GetVertex(const int N) const {
257    Vector3 v;
258    GetVertex(N, v);
259    return v;
260  }
261
262  // Returns 1, if the box includes on arbitrary face a given box
263  int IsPiercedByBox(const AxisAlignedBox3 &box, int &axis) const;
264
265
266  int GetFaceVisibilityMask(const Vector3 &position) const;
267  int GetFaceVisibilityMask(const Rectangle3 &rectangle) const;
268
269  Rectangle3 GetFace(const int face) const;
270 
271  /** Extracts plane of bounding box.
272  */
273  Plane3 GetPlane(const int face) const;
274 
275  // For a given point returns the region, where the point is located
276  // there are 27 regions (0..26) .. determined by the planes embedding in the
277  // sides of the bounding box (0 .. lower the position of the box,
278  // 1 .. inside the box, 2 .. greater than box). The region number is given as
279  // R = 9*x + 3*y + z  ; e.g. region .. inside the box is 13.
280  int GetRegionID(const Vector3 &point) const;
281 
282  // Set the corner point of rectangle on the face of bounding box
283  // given by the index number and the rectangle lying on this face
284  //  void GetFaceRectCorner(const CRectLeaf2D *rect, EFaces faceIndx,
285  //                     const int &cornerIndx, Vector3 &cornerPoint);
286
287  // Project the box to a plane given a normal vector of this plane. Computes
288  // the surface area of projected silhouettes for parallel projection.
289  float ProjectToPlaneSA(const Vector3 &normal) const;
290
291  // Computes projected surface area of the box to a given viewing plane
292  // given a viewpoint. This corresponds the probability, the box will
293  // be hit by the ray .. moreover returns .. the region number (0-26).
294  // the function supposes all the points lie of the box lies in the viewing
295  // frustrum !!! The positive halfspace of viewplane has to contain
296  // viewpoint. "projectionType" == 0 .. perspective projection,
297  // == 1 .. parallel projection.
298  float ProjectToPlaneSA(const Plane3 &viewplane,
299                         const Vector3 &viewpoint,
300                         int *tcase,
301                         const float &maxSA,
302                         int projectionType) const;
303
304  // Computes projected surface area of the box to a given viewing plane
305  // and viewpoint. It clipps the area by all the planes given .. they should
306  // define the viewing frustrum. Variable tclip defines, which planes are
307  // used for clipping, parameter 31 is the most general, clip all the plane.
308  // 1 .. clip left, 2 .. clip top, 4 .. clip right, 8 .. clip bottom,
309  // 16 .. clip supporting plane(its normal towards the viewing frustrum).
310  // "typeProjection" == 0 .. perspective projection,
311  // == 1 .. parallel projection
312  float ProjectToPlaneSA(const Plane3 &viewplane,
313                         const Vector3 &viewpoint,
314                         int *tcase, int &tclip,
315                         const Plane3 &leftPlane,
316                         const Plane3 &topPlane,
317                         const Plane3 &rightPlane,
318                         const Plane3 &bottomPlane,
319                         const Plane3 &suppPlane,
320                         const float &maxSA,
321                         int typeProjection) const;
322
323  // Projects the box to a unit sphere enclosing a given viewpoint and
324  // returns the solid angle of the box projected to a unit sphere
325  float ProjectToSphereSA(const Vector3 &viewpoint, int *tcase) const;
326
327  /** Returns vertex indices of edge.
328  */
329  void GetEdge(const int edge, int  &aIdx, int &bIdx) const;
330
331  /** Computes cross section of plane with box (i.e., bounds box).
332          @returns the cross section
333  */
334  Polygon3 *CrossSection(const Plane3 &plane) const;
335
336  /** Computes minimal and maximal t of ray, including the object intersections.
337          @returns true if ray hits the bounding box.
338  */
339  bool GetRaySegment(const Ray &ray, float &minT, float &maxT) const;
340
341  /** If the boxes are intersecting on a common face, this function
342          returns the face intersection, false otherwise.
343   
344          @param neighbour the neighbouring box intersecting with this box.
345  */
346  bool GetIntersectionFace(Rectangle3 &face,
347                                                   const AxisAlignedBox3 &neighbour) const;
348
349  /** Includes the box faces to the mesh description
350  */
351  friend void IncludeBoxInMesh(const AxisAlignedBox3 &box, Mesh &mesh);
352
353  /** Box faces are turned into polygons.
354  */
355  void ExtractPolys(PolygonContainer &polys) const;
356
357  /** Splits the box into two separate boxes with respect to the split plane
358  */
359  void Split(const int axis,
360                         const float value,
361                         AxisAlignedBox3 &left,
362                         AxisAlignedBox3 &right) const;
363
364#define __EXTENT_HACK
365  // get the extent of face
366  float GetExtent(const int &face) const {
367#if defined(__EXTENT_HACK) && defined(__VECTOR_HACK)
368    return mMin[face];
369#else
370    if (face < 3)
371      return mMin[face];
372    else
373      return mMax[face-3];
374#endif
375  }
376
377  // The vertices that form boundaries of the projected bounding box
378  // for all the regions possible, number of regions is 3^3 = 27,
379  // since two parallel sides of bbox forms three disjoint spaces
380  // the vertices are given in anti-clockwise order .. stopped by -1 elem.
381  static const int bvertices[27][9];
382
383  // The list of all faces visible from a given region (except region 13)
384  // the faces are identified by triple: (axis, min-vertex, max-vertex),
385  // that is maximaly three triples are defined. axis = 0 (x-axis),
386  // axis = 1 (y-axis), axis = 2 (z-axis), -1 .. terminator. Is is always
387  // true that: min-vertex < max-vertex for all coordinates excluding axis
388  static const int bfaces[27][10];
389 
390  // The correct corners indexed starting from entry face to exit face
391  // first index determines entry face, second index exit face, and
392  // the two numbers (indx, inc) determines: ind = the index on the exit
393  // face, when starting from the vertex 0 on entry face, 'inc' is
394  // the increment when we go on entry face in order 0,1,2,3 to create
395  // convex shaft with the rectangle on exit face. That is, inc = -1 or 1.
396  static const int pairFaceRects[6][6][2];
397
398  // The vertices that form CLOSEST points with respect to the region
399  // for all the regions possible, number of regions is 3^3 = 27,
400  // since two parallel sides of bbox forms three disjoint spaces.
401  // The vertices are given in anti-clockwise order, stopped by -1 elem,
402  // at most 8 points, at least 1 point.
403  static const int cvertices[27][9];
404  static const int csvertices[27][6];
405
406  // The vertices that form FARTHEST points with respect to the region
407  // for all the regions possible, number of regions is 3^3 = 27,
408  // since two parallel sides of bbox forms three disjoint spaces.
409  // The vertices are given in anti-clockwise order, stopped by -1 elem,
410  // at most 8 points, at least 1 point.
411  static const int fvertices[27][9]; 
412  static const int fsvertices[27][9];
413
414  // input and output operator with stream
415  friend ostream& operator<<(ostream &s, const AxisAlignedBox3 &A);
416  friend istream& operator>>(istream &s, AxisAlignedBox3 &A);
417
418protected:
419  // definition of friend functions
420  friend class Ray;
421};
422
423// --------------------------------------------------------------------------
424// Implementation of inline (member) functions
425 
426inline bool
427Overlap(const AxisAlignedBox3 &x, const AxisAlignedBox3 &y)
428{
429  if (x.mMax.x < y.mMin.x ||
430      x.mMin.x > y.mMax.x ||
431      x.mMax.y < y.mMin.y ||
432      x.mMin.y > y.mMax.y ||
433      x.mMax.z < y.mMin.z ||
434      x.mMin.z > y.mMax.z) {
435    return false;
436  }
437  return true;
438}
439
440inline bool
441OverlapS(const AxisAlignedBox3 &x, const AxisAlignedBox3 &y)
442{
443  if (x.mMax.x <= y.mMin.x ||
444      x.mMin.x >= y.mMax.x ||
445      x.mMax.y <= y.mMin.y ||
446      x.mMin.y >= y.mMax.y ||
447      x.mMax.z <= y.mMin.z ||
448      x.mMin.z >= y.mMax.z) {
449    return false;
450  }
451  return true;
452}
453
454inline bool
455Overlap(const AxisAlignedBox3 &x, const AxisAlignedBox3 &y, float eps)
456{
457  if ( (x.mMax.x - eps) < y.mMin.x ||
458       (x.mMin.x + eps) > y.mMax.x ||
459       (x.mMax.y - eps) < y.mMin.y ||
460       (x.mMin.y + eps) > y.mMax.y ||
461       (x.mMax.z - eps) < y.mMin.z ||
462       (x.mMin.z + eps) > y.mMax.z ) {
463    return false;
464  }
465  return true;
466}
467
468inline AxisAlignedBox3
469Intersect(const AxisAlignedBox3 &x, const AxisAlignedBox3 &y)
470{
471  if (x.Unbounded())
472    return y;
473  else
474    if (y.Unbounded())
475      return x;
476  AxisAlignedBox3 ret = x;
477  if (Overlap(ret, y)) {
478    Maximize(ret.mMin, y.mMin);
479    Minimize(ret.mMax, y.mMax);
480    return ret;
481  }
482  else      // Null intersection.
483    return AxisAlignedBox3(Vector3(0), Vector3(0));
484  // return AxisAlignedBox3(Vector3(0), Vector3(-1));
485}
486
487inline AxisAlignedBox3
488Union(const AxisAlignedBox3 &x, const AxisAlignedBox3 &y)
489{
490  Vector3 min = x.mMin;
491  Vector3 max = x.mMax;
492  Minimize(min, y.mMin);
493  Maximize(max, y.mMax);
494  return AxisAlignedBox3(min, max);
495}
496
497inline AxisAlignedBox3
498Transform(const AxisAlignedBox3 &box, const Matrix4x4 &tform)
499{
500  Vector3 mmin(MAXFLOAT);
501  Vector3 mmax(-MAXFLOAT);
502
503  AxisAlignedBox3 ret(mmin, mmax);
504  ret.Include(tform * Vector3(box.mMin.x, box.mMin.y, box.mMin.z));
505  ret.Include(tform * Vector3(box.mMin.x, box.mMin.y, box.mMax.z));
506  ret.Include(tform * Vector3(box.mMin.x, box.mMax.y, box.mMin.z));
507  ret.Include(tform * Vector3(box.mMin.x, box.mMax.y, box.mMax.z));
508  ret.Include(tform * Vector3(box.mMax.x, box.mMin.y, box.mMin.z));
509  ret.Include(tform * Vector3(box.mMax.x, box.mMin.y, box.mMax.z));
510  ret.Include(tform * Vector3(box.mMax.x, box.mMax.y, box.mMin.z));
511  ret.Include(tform * Vector3(box.mMax.x, box.mMax.y, box.mMax.z));
512  return ret;
513}
514
515inline float RatioOfOverlap(const AxisAlignedBox3 &box1, const AxisAlignedBox3 &box2)
516{
517        // return ratio of intersection to union
518        const AxisAlignedBox3 bisect = Intersect(box1, box2);
519        const AxisAlignedBox3 bunion = Union(box1, box2);
520
521        return bisect.GetVolume() / bunion.GetVolume();
522}
523
524inline int operator==(const AxisAlignedBox3 &A, const AxisAlignedBox3 &B)
525{
526  return (A.mMin == B.mMin) && (A.mMax == B.mMax);
527}
528
529 
530}
531
532
533#endif
Note: See TracBrowser for help on using the repository browser.