IceAABB.h
Go to the documentation of this file.
1 
8 
11 // Include Guard
12 #ifndef __ICEAABB_H__
13 #define __ICEAABB_H__
14 
15  // Forward declarations
16  class Sphere;
17 
19 #define AABB_COMMON_METHODS \
20  AABB& Add(const AABB& aabb); \
21  float MakeCube(AABB& cube) const; \
22  void MakeSphere(Sphere& sphere) const; \
23  const sbyte* ComputeOutline(const Point& local_eye, sdword& num) const; \
24  float ComputeBoxArea(const Point& eye, const Matrix4x4& mat, float width, float height, sdword& num) const; \
25  bool IsInside(const AABB& box) const; \
26  bool ComputePlanes(Plane* planes) const; \
27  bool ComputePoints(Point* pts) const; \
28  const Point* GetVertexNormals() const; \
29  const udword* GetEdges() const; \
30  const Point* GetEdgeNormals() const; \
31  inline_ BOOL ContainsPoint(const Point& p) const \
32  { \
33  if(p.x > GetMax(0) || p.x < GetMin(0)) return FALSE; \
34  if(p.y > GetMax(1) || p.y < GetMin(1)) return FALSE; \
35  if(p.z > GetMax(2) || p.z < GetMin(2)) return FALSE; \
36  return TRUE; \
37  }
38 
39  enum AABBType
40  {
43 
44  AABB_FORCE_DWORD = 0x7fffffff,
45  };
46 
47 #ifdef USE_MINMAX
48 
49  struct ICEMATHS_API ShadowAABB
50  {
51  Point mMin;
52  Point mMax;
53  };
54 
55  class ICEMATHS_API AABB
56  {
57  public:
59  inline_ AABB() {}
61  inline_ ~AABB() {}
62 
65 
67 
72  void SetMinMax(const Point& min, const Point& max) { mMin = min; mMax = max; }
74 
76 
81  void SetCenterExtents(const Point& c, const Point& e) { mMin = c - e; mMax = c + e; }
83 
85 
88  void SetEmpty() { Point p(MIN_FLOAT, MIN_FLOAT, MIN_FLOAT); mMin = -p; mMax = p;}
90 
92 
95  void SetPoint(const Point& pt) { mMin = mMax = pt; }
97 
99 
103  float GetSize() const { Point e; GetExtents(e); return e.Max(); }
105 
107 
111  void Extend(const Point& p)
113  {
114  if(p.x > mMax.x) mMax.x = p.x;
115  if(p.x < mMin.x) mMin.x = p.x;
116 
117  if(p.y > mMax.y) mMax.y = p.y;
118  if(p.y < mMin.y) mMin.y = p.y;
119 
120  if(p.z > mMax.z) mMax.z = p.z;
121  if(p.z < mMin.z) mMin.z = p.z;
122  }
123  // Data access
124 
126  inline_ void GetMin(Point& min) const { min = mMin; }
128  inline_ void GetMax(Point& max) const { max = mMax; }
129 
131  inline_ float GetMin(udword axis) const { return mMin[axis]; }
133  inline_ float GetMax(udword axis) const { return mMax[axis]; }
134 
136  inline_ void GetCenter(Point& center) const { center = (mMax + mMin)*0.5f; }
138  inline_ void GetExtents(Point& extents) const { extents = (mMax - mMin)*0.5f; }
139 
141  inline_ float GetCenter(udword axis) const { return (mMax[axis] + mMin[axis])*0.5f; }
143  inline_ float GetExtents(udword axis) const { return (mMax[axis] - mMin[axis])*0.5f; }
144 
146  inline_ void GetDiagonal(Point& diagonal) const { diagonal = mMax - mMin; }
147  inline_ float GetWidth() const { return mMax.x - mMin.x; }
148  inline_ float GetHeight() const { return mMax.y - mMin.y; }
149  inline_ float GetDepth() const { return mMax.z - mMin.z; }
150 
152  inline_ float GetVolume() const { return GetWidth() * GetHeight() * GetDepth(); }
153 
155 
160  inline_ BOOL Intersect(const AABB& a) const
162  {
163  if(mMax.x < a.mMin.x
164  || a.mMax.x < mMin.x
165  || mMax.y < a.mMin.y
166  || a.mMax.y < mMin.y
167  || mMax.z < a.mMin.z
168  || a.mMax.z < mMin.z) return FALSE;
169 
170  return TRUE;
171  }
172 
174 
180  inline_ BOOL Intersect(const AABB& a, udword axis) const
182  {
183  if(mMax[axis] < a.mMin[axis] || a.mMax[axis] < mMin[axis]) return FALSE;
184  return TRUE;
185  }
186 
188 
194  inline_ void Rotate(const Matrix4x4& mtx, AABB& aabb) const
196  {
197  // The three edges transformed: you can efficiently transform an X-only vector
198  // by just getting the "X" column of the matrix
199  Point vx,vy,vz;
200  mtx.GetRow(0, vx); vx *= (mMax.x - mMin.x);
201  mtx.GetRow(1, vy); vy *= (mMax.y - mMin.y);
202  mtx.GetRow(2, vz); vz *= (mMax.z - mMin.z);
203 
204  // Transform the min point
205  aabb.mMin = aabb.mMax = mMin * mtx;
206 
207  // Take the transformed min & axes and find new extents
208  // Using CPU code in the right place is faster...
209  if(IS_NEGATIVE_FLOAT(vx.x)) aabb.mMin.x += vx.x; else aabb.mMax.x += vx.x;
210  if(IS_NEGATIVE_FLOAT(vx.y)) aabb.mMin.y += vx.y; else aabb.mMax.y += vx.y;
211  if(IS_NEGATIVE_FLOAT(vx.z)) aabb.mMin.z += vx.z; else aabb.mMax.z += vx.z;
212  if(IS_NEGATIVE_FLOAT(vy.x)) aabb.mMin.x += vy.x; else aabb.mMax.x += vy.x;
213  if(IS_NEGATIVE_FLOAT(vy.y)) aabb.mMin.y += vy.y; else aabb.mMax.y += vy.y;
214  if(IS_NEGATIVE_FLOAT(vy.z)) aabb.mMin.z += vy.z; else aabb.mMax.z += vy.z;
215  if(IS_NEGATIVE_FLOAT(vz.x)) aabb.mMin.x += vz.x; else aabb.mMax.x += vz.x;
216  if(IS_NEGATIVE_FLOAT(vz.y)) aabb.mMin.y += vz.y; else aabb.mMax.y += vz.y;
217  if(IS_NEGATIVE_FLOAT(vz.z)) aabb.mMin.z += vz.z; else aabb.mMax.z += vz.z;
218  }
219 
221 
225  inline_ BOOL IsValid() const
227  {
228  // Consistency condition for (Min, Max) boxes: min < max
229  if(mMin.x > mMax.x) return FALSE;
230  if(mMin.y > mMax.y) return FALSE;
231  if(mMin.z > mMax.z) return FALSE;
232  return TRUE;
233  }
234 
236  inline_ AABB& operator*=(float s)
237  {
238  Point Center; GetCenter(Center);
239  Point Extents; GetExtents(Extents);
240  SetCenterExtents(Center, Extents * s);
241  return *this;
242  }
243 
245  inline_ AABB& operator/=(float s)
246  {
247  Point Center; GetCenter(Center);
248  Point Extents; GetExtents(Extents);
249  SetCenterExtents(Center, Extents / s);
250  return *this;
251  }
252 
254  inline_ AABB& operator+=(const Point& trans)
255  {
256  mMin+=trans;
257  mMax+=trans;
258  return *this;
259  }
260  private:
261  Point mMin;
262  Point mMax;
263  };
264 
265 #else
266 
267  class ICEMATHS_API AABB
268  {
269  public:
274 
277 
279 
284  void SetMinMax(const Point& min, const Point& max) { mCenter = (max + min)*0.5f; mExtents = (max - min)*0.5f; }
286 
288 
293  void SetCenterExtents(const Point& c, const Point& e) { mCenter = c; mExtents = e; }
295 
297 
300  void SetEmpty() { mCenter.Zero(); mExtents.Set(MIN_FLOAT, MIN_FLOAT, MIN_FLOAT);}
302 
304 
307  void SetPoint(const Point& pt) { mCenter = pt; mExtents.Zero(); }
309 
311 
315  float GetSize() const { return mExtents.Max(); }
317 
319 
323  void Extend(const Point& p)
325  {
326  Point Max = mCenter + mExtents;
327  Point Min = mCenter - mExtents;
328 
329  if(p.x > Max.x) Max.x = p.x;
330  if(p.x < Min.x) Min.x = p.x;
331 
332  if(p.y > Max.y) Max.y = p.y;
333  if(p.y < Min.y) Min.y = p.y;
334 
335  if(p.z > Max.z) Max.z = p.z;
336  if(p.z < Min.z) Min.z = p.z;
337 
338  SetMinMax(Min, Max);
339  }
340  // Data access
341 
343  inline_ void GetMin(Point& min) const { min = mCenter - mExtents; }
345  inline_ void GetMax(Point& max) const { max = mCenter + mExtents; }
346 
348  inline_ float GetMin(udword axis) const { return mCenter[axis] - mExtents[axis]; }
350  inline_ float GetMax(udword axis) const { return mCenter[axis] + mExtents[axis]; }
351 
353  inline_ void GetCenter(Point& center) const { center = mCenter; }
355  inline_ void GetExtents(Point& extents) const { extents = mExtents; }
356 
358  inline_ float GetCenter(udword axis) const { return mCenter[axis]; }
360  inline_ float GetExtents(udword axis) const { return mExtents[axis]; }
361 
363  inline_ void GetDiagonal(Point& diagonal) const { diagonal = mExtents * 2.0f; }
364  inline_ float GetWidth() const { return mExtents.x * 2.0f; }
365  inline_ float GetHeight() const { return mExtents.y * 2.0f; }
366  inline_ float GetDepth() const { return mExtents.z * 2.0f; }
367 
369  inline_ float GetVolume() const { return mExtents.x * mExtents.y * mExtents.z * 8.0f; }
370 
372 
377  inline_ BOOL Intersect(const AABB& a) const
379  {
380  float tx = mCenter.x - a.mCenter.x; float ex = a.mExtents.x + mExtents.x; if(AIR(tx) > IR(ex)) return FALSE;
381  float ty = mCenter.y - a.mCenter.y; float ey = a.mExtents.y + mExtents.y; if(AIR(ty) > IR(ey)) return FALSE;
382  float tz = mCenter.z - a.mCenter.z; float ez = a.mExtents.z + mExtents.z; if(AIR(tz) > IR(ez)) return FALSE;
383  return TRUE;
384  }
385 
387 
392  inline_ bool GomezIntersect(const AABB& a)
394  {
395  Point T = mCenter - a.mCenter; // Vector from A to B
396  return ((fabsf(T.x) <= (a.mExtents.x + mExtents.x))
397  && (fabsf(T.y) <= (a.mExtents.y + mExtents.y))
398  && (fabsf(T.z) <= (a.mExtents.z + mExtents.z)));
399  }
400 
402 
408  inline_ BOOL Intersect(const AABB& a, udword axis) const
410  {
411  float t = mCenter[axis] - a.mCenter[axis];
412  float e = a.mExtents[axis] + mExtents[axis];
413  if(AIR(t) > IR(e)) return FALSE;
414  return TRUE;
415  }
416 
418 
423  inline_ void Rotate(const Matrix4x4& mtx, AABB& aabb) const
425  {
426  // Compute new center
427  aabb.mCenter = mCenter * mtx;
428 
429  // Compute new extents. FPU code & CPU code have been interleaved for improved performance.
430  Point Ex(mtx.m[0][0] * mExtents.x, mtx.m[0][1] * mExtents.x, mtx.m[0][2] * mExtents.x);
431  IR(Ex.x)&=0x7fffffff; IR(Ex.y)&=0x7fffffff; IR(Ex.z)&=0x7fffffff;
432 
433  Point Ey(mtx.m[1][0] * mExtents.y, mtx.m[1][1] * mExtents.y, mtx.m[1][2] * mExtents.y);
434  IR(Ey.x)&=0x7fffffff; IR(Ey.y)&=0x7fffffff; IR(Ey.z)&=0x7fffffff;
435 
436  Point Ez(mtx.m[2][0] * mExtents.z, mtx.m[2][1] * mExtents.z, mtx.m[2][2] * mExtents.z);
437  IR(Ez.x)&=0x7fffffff; IR(Ez.y)&=0x7fffffff; IR(Ez.z)&=0x7fffffff;
438 
439  aabb.mExtents.x = Ex.x + Ey.x + Ez.x;
440  aabb.mExtents.y = Ex.y + Ey.y + Ez.y;
441  aabb.mExtents.z = Ex.z + Ey.z + Ez.z;
442  }
443 
445 
449  inline_ BOOL IsValid() const
451  {
452  // Consistency condition for (Center, Extents) boxes: Extents >= 0
453  if(IS_NEGATIVE_FLOAT(mExtents.x)) return FALSE;
454  if(IS_NEGATIVE_FLOAT(mExtents.y)) return FALSE;
455  if(IS_NEGATIVE_FLOAT(mExtents.z)) return FALSE;
456  return TRUE;
457  }
458 
460  inline_ AABB& operator*=(float s) { mExtents*=s; return *this; }
461 
463  inline_ AABB& operator/=(float s) { mExtents/=s; return *this; }
464 
467  {
468  mCenter+=trans;
469  return *this;
470  }
471  private:
474  };
475 
476 #endif
477 
479  {
480  if(p.x > max.x) max.x = p.x;
481  if(p.x < min.x) min.x = p.x;
482 
483  if(p.y > max.y) max.y = p.y;
484  if(p.y < min.y) min.y = p.y;
485 
486  if(p.z > max.z) max.z = p.z;
487  if(p.z < min.z) min.z = p.z;
488  }
489 
490  inline_ void ComputeAABB(AABB& aabb, const Point* list, udword nb_pts)
491  {
492  if(list)
493  {
496  while(nb_pts--)
497  {
498 // _prefetch(list+1); // off by one ?
499  ComputeMinMax(*list++, Mini, Maxi);
500  }
501  aabb.SetMinMax(Mini, Maxi);
502  }
503  }
504 
505 #endif // __ICEAABB_H__
Point mCenter
AABB Center.
Definition: IceAABB.h:472
inline_ ~AABB()
Destructor.
Definition: IceAABB.h:273
#define IR(x)
Integer representation of a floating-point value.
Definition: IceFPU.h:18
int c
Definition: autoplay.py:16
inline_ float GetVolume() const
Volume.
Definition: IceAABB.h:369
#define FALSE
Definition: OPC_IceHook.h:9
#define IS_NEGATIVE_FLOAT(x)
Definition: IceFPU.h:32
inline_ void GetExtents(Point &extents) const
Get box extents.
Definition: IceAABB.h:355
void SetMinMax(const Point &min, const Point &max)
Definition: IceAABB.h:285
inline_ AABB()
Constructor.
Definition: IceAABB.h:271
static int min(int a, int b)
inline_ void ComputeMinMax(const Point &p, Point &min, Point &max)
Definition: IceAABB.h:478
inline_ void GetMin(Point &min) const
Get min point of the box.
Definition: IceAABB.h:343
inline_ float GetHeight() const
Definition: IceAABB.h:365
inline_ AABB & operator+=(const Point &trans)
Operator for AABB += Point. Translates the box.
Definition: IceAABB.h:466
#define TRUE
Definition: OPC_IceHook.h:13
#define inline_
AABB used for dynamic updates. Not visible == not updated.
Definition: IceAABB.h:42
float z
Definition: IcePoint.h:524
Definition: IcePoint.h:25
#define ICEMATHS_API
Definition: OPC_IceHook.h:51
#define AABB_COMMON_METHODS
Declarations of type-independent methods (most of them implemented in the .cpp)
Definition: OPC_IceHook.h:20
png_infop png_bytep * trans
Definition: png.h:2435
int BOOL
Another boolean type.
Definition: IceTypes.h:102
float x
Definition: IcePoint.h:524
unsigned int udword
sizeof(udword) must be 4
Definition: IceTypes.h:65
AABB_COMMON_METHODS
Type-independent methods.
Definition: IceAABB.h:276
inline_ AABB & operator/=(float s)
Operator for AABB /= float. Scales the extents, keeps same center.
Definition: IceAABB.h:463
inline_ AABB & operator*=(float s)
Operator for AABB *= float. Scales the extents, keeps same center.
Definition: IceAABB.h:460
inline_ float GetMax(udword axis) const
Get component of the box&#39;s max point along a given axis.
Definition: IceAABB.h:350
#define MAX_FLOAT
max possible float value
Definition: IceTypes.h:130
Point mExtents
x, y and z extents
Definition: IceAABB.h:473
string a
inline_ void GetMax(Point &max) const
Get max point of the box.
Definition: IceAABB.h:345
t
inline_ float GetWidth() const
Definition: IceAABB.h:364
AABBType
Definition: IceAABB.h:39
inline_ float GetExtents(udword axis) const
Get component of the box&#39;s extents along a given axis.
Definition: IceAABB.h:360
inline_ Point & Set(float _x, float _y, float _z)
Assignment from values.
Definition: IcePoint.h:57
inline_ void ComputeAABB(AABB &aabb, const Point *list, udword nb_pts)
Definition: IceAABB.h:490
inline_ float Max() const
Returns MAX(x, y, z);.
Definition: IcePoint.h:201
inline_ void GetDiagonal(Point &diagonal) const
Get box diagonal.
Definition: IceAABB.h:363
inline_ void GetRow(const udword r, HPoint &p) const
Returns a row.
Definition: IceMatrix4x4.h:73
inline_ Point & Zero()
Clears the vector.
Definition: IcePoint.h:44
float y
Definition: IcePoint.h:524
inline_ float GetCenter(udword axis) const
Get component of the box&#39;s center along a given axis.
Definition: IceAABB.h:358
#define AIR(x)
Absolute integer representation of a floating-point value.
Definition: IceFPU.h:24
inline_ BOOL Intersect(const AABB &a, const SAP_Box &b)
inline_ float GetMin(udword axis) const
Get component of the box&#39;s min point along a given axis.
Definition: IceAABB.h:348
inline_ void GetCenter(Point &center) const
Get box center.
Definition: IceAABB.h:353
inline_ float GetDepth() const
Definition: IceAABB.h:366
#define MIN_FLOAT
min possible loat value
Definition: IceTypes.h:131
static int max(int a, int b)
AABB used for rendering. Not visible == not rendered.
Definition: IceAABB.h:41


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Sep 8 2022 02:24:03