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

Revision 1995, 17.7 KB checked in by bittner, 17 years ago (diff)

mutation tests

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