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