00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef SALT_BOUNDS_H
00023 #define SALT_BOUNDS_H
00024
00025
00026
00027
00028
00029 #include "defines.h"
00030 #include "matrix.h"
00031 #include "vector.h"
00032
00033 #include <cfloat>
00034
00035 namespace salt
00036 {
00037
00039 class AABB3
00040 {
00041 public:
00042
00043
00045 f_inline AABB3() { Init(); }
00046
00048 f_inline AABB3(const Vector3f &mn, const Vector3f &mx)
00049 { Init(); Encapsulate(mn); Encapsulate(mx); }
00050
00051
00052
00054 f_inline void Init()
00055 { minVec.Set(FLT_MAX, FLT_MAX, FLT_MAX);
00056 maxVec.Set(-FLT_MAX, -FLT_MAX, -FLT_MAX);
00057 }
00058
00060 f_inline void Encapsulate(const Vector3f &v)
00061 { minVec.x() = gMin(minVec.x(), v.x());
00062 minVec.y() = gMin(minVec.y(), v.y());
00063 minVec.z() = gMin(minVec.z(), v.z());
00064 maxVec.x() = gMax(maxVec.x(), v.x());
00065 maxVec.y() = gMax(maxVec.y(), v.y());
00066 maxVec.z() = gMax(maxVec.z(), v.z()); }
00067
00069 f_inline void Encapsulate(const float x, const float y, const float z)
00070 { minVec.x() = gMin(minVec.x(), x);
00071 minVec.y() = gMin(minVec.y(), y);
00072 minVec.z() = gMin(minVec.z(), z);
00073 maxVec.x() = gMax(maxVec.x(), x);
00074 maxVec.y() = gMax(maxVec.y(), y);
00075 maxVec.z() = gMax(maxVec.z(), z); }
00076
00078 f_inline void Encapsulate(const AABB3 &box)
00079 { Encapsulate(box.minVec); Encapsulate(box.maxVec);}
00080
00082 f_inline void Widen(float delta)
00083 { minVec.x()-=delta;
00084 minVec.y()-=delta;
00085 minVec.z()-=delta;
00086 maxVec.x()+=delta;
00087 maxVec.y()+=delta;
00088 maxVec.z()+=delta;
00089 }
00090
00092 f_inline void Translate(const Vector3f &v)
00093 { minVec+=v; maxVec+=v; }
00094
00096 f_inline bool Contains(const Vector3f &v) const
00097 { return (gInRange(v.x(), minVec.x(), maxVec.x()) &&
00098 gInRange(v.z(), minVec.z(), maxVec.z()) &&
00099 gInRange(v.y(), minVec.y(), maxVec.y())); }
00100
00102 f_inline bool Contains(const AABB3 &b) const
00103 { return (Contains(b.minVec) && Contains(b.maxVec)); }
00104
00106 f_inline bool Intersects(const AABB3 &b) const
00107 { return !(minVec.x() > b.maxVec.x() || maxVec.x() < b.minVec.x() ||
00108 minVec.y() > b.maxVec.y() || maxVec.y() < b.minVec.y() ||
00109 minVec.z() > b.maxVec.z() || maxVec.z() < b.minVec.z()); }
00110
00112 f_inline float GetWidth() const
00113 { return gAbs(minVec.x()-maxVec.x()); }
00114
00116 f_inline float GetHeight() const
00117 { return gAbs(minVec.y()-maxVec.y()); }
00118
00120 f_inline float GetDepth() const
00121 { return gAbs(minVec.z()-maxVec.z()); }
00122
00124 f_inline Vector3f GetMiddle() const
00125 { return Vector3f((minVec.x()+maxVec.x())*0.5f,
00126 (minVec.y()+maxVec.y())*0.5f,
00127 (minVec.z()+maxVec.z())*0.5f); }
00128
00132 f_inline float GetRadius() const
00133 { return ((maxVec-minVec)*0.5).Length(); }
00134
00135
00136
00137 void TransformBy(Matrix& matrix);
00138
00139
00140
00142 Vector3f minVec;
00143
00145 Vector3f maxVec;
00146 };
00147
00149 class AABB2
00150 {
00151 public:
00152
00153
00155 f_inline AABB2()
00156 { Init(); }
00157
00159 f_inline AABB2(const Vector2f &mn, const Vector2f &mx)
00160 { Init(); Encapsulate(mn); Encapsulate(mx); }
00161
00162
00163
00165 f_inline void Init()
00166 { minVec.Set(FLT_MAX, FLT_MAX); maxVec.Set(-FLT_MAX, -FLT_MAX); }
00167
00169 f_inline void Encapsulate(const Vector2f &v)
00170 { minVec.x() = gMin(minVec.x(), v.x());
00171 minVec.y() = gMin(minVec.y(), v.y());
00172 maxVec.x() = gMax(maxVec.x(), v.x());
00173 maxVec.y() = gMax(maxVec.y(), v.y()); }
00174
00176 f_inline void Encapsulate(const AABB2 &box)
00177 { Encapsulate(box.minVec); Encapsulate(box.maxVec);}
00178
00180 f_inline void Widen(float delta)
00181 { minVec.x()-=delta; minVec.y()-=delta; maxVec.x()+=delta; maxVec.y()+=delta; }
00182
00184 f_inline void Translate(const Vector2f &v)
00185 { minVec+=v; maxVec+=v; }
00186
00188 f_inline bool Contains(const Vector2f &v) const
00189 { return (gInRange(v.x(), minVec.x(), maxVec.x()) && gInRange(v.y(), minVec.y(), maxVec.y())); }
00190
00192 f_inline bool Contains(const AABB2 &b) const
00193 { return (Contains(b.minVec) && Contains(b.maxVec)); }
00194
00196 f_inline bool Intersects(const AABB2 &b) const
00197 { return !(minVec.x() > b.maxVec.x() ||
00198 maxVec.x() < b.minVec.x() ||
00199 minVec.y() > b.maxVec.y() ||
00200 maxVec.y() < b.minVec.y());
00201 }
00202
00204 f_inline float GetWidth() const
00205
00206 { return gAbs(minVec.x()-maxVec.x()); }
00207
00209 f_inline float GetHeight() const
00210
00211 { return gAbs(minVec.y()-maxVec.y()); }
00212
00214 f_inline Vector2f GetMiddle() const
00215 { return Vector2f((minVec.x()+maxVec.x())*0.5f, (minVec.y()+maxVec.y())*0.5f); }
00216
00220 f_inline float GetRadius() const
00221 { return ((maxVec-minVec)*0.5).Length(); }
00222
00223
00224
00226 Vector2f minVec;
00227
00229 Vector2f maxVec;
00230 };
00231
00232
00234 class BoundingSphere
00235 {
00236 public:
00237
00238
00240 f_inline BoundingSphere()
00241 : center(Vector3f(0,0,0)), radius(0.0f), radiusSq(0.0f) {}
00242
00244 f_inline BoundingSphere(const Vector3f &pos, float rad)
00245 : center(pos), radius(rad), radiusSq(rad*rad) {}
00246
00252 f_inline BoundingSphere(const Vector3f &pos, float rad, float radSq)
00253 : center(pos), radius(rad), radiusSq(radSq) {}
00254
00255
00256
00260 f_inline void EncapsulateFast(const Vector3f &v)
00261 { Vector3f diff=(center - v); float dist=diff.Dot(diff);
00262
00263 if (dist>radiusSq) { radiusSq=dist; radius=gSqrt(dist); }}
00264
00266 f_inline bool Contains(const Vector3f &v)
00267 { return ((center - v).SquareLength() < radiusSq); }
00268
00270 f_inline bool Contains(const BoundingSphere &s) const
00271 { return (radius >= s.radius) &&
00272 ((center - s.center).SquareLength() <
00273 (radius - s.radius) * (radius - s.radius));
00274 }
00275
00277 f_inline bool Intersects(const BoundingSphere &s) const
00278 { return ((center - s.center).SquareLength() <
00279 (radius + s.radius) * (radius + s.radius)); }
00280
00281
00282
00286 void Encapsulate(const Vector3f &v);
00287
00289 bool Contains(const AABB3 &b) const;
00290
00292 bool Intersects(const AABB3 &b) const;
00293
00294
00295
00297 Vector3f center;
00298
00300 float radius;
00301
00305 float radiusSq;
00306 };
00307
00308 }
00309
00310 #endif // SALT_BOUNDS_H