Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

bounds.h

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*-
00002 
00003    this file is part of rcssserver3D
00004    Fri May 9 2003
00005    Copyright (C) 2002,2003 Koblenz University
00006    Copyright (C) 2003 RoboCup Soccer Server 3D Maintenance Group
00007    $Id: bounds.h,v 1.9 2004/06/05 11:20:51 fruit Exp $
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; version 2 of the License.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00021 */
00022 #ifndef SALT_BOUNDS_H
00023 #define SALT_BOUNDS_H
00024 
00025 // #ifdef HAVE_CONFIG_H
00026 // #include <config.h>
00027 // #endif
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     // constructors
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     // inline functions
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(); }      // get distance from middle of bounding box to one of the corners
00134       // (i.e. radius of bounding sphere through Middle()).
00135 
00136     /* multiplies the box with the given matrix */
00137     void    TransformBy(Matrix& matrix);
00138 
00139     // attributes
00140 
00142     Vector3f                minVec;
00143 
00145     Vector3f                maxVec;
00146 };
00147 
00149 class AABB2
00150 {
00151 public:
00152     // constructors
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     // inline functions
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     // get width of bounding box
00206     { return gAbs(minVec.x()-maxVec.x()); }
00207 
00209     f_inline float          GetHeight() const
00210     // get height of bounding box
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     // attributes
00224 
00226     Vector2f                minVec;
00227 
00229     Vector2f                maxVec;
00230 };
00231 
00232 
00234 class BoundingSphere
00235 {
00236 public:
00237     // constructors
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     // inline functions
00256 
00260     f_inline void EncapsulateFast(const Vector3f &v)
00261     { Vector3f diff=(center - v);   float dist=diff.Dot(diff);
00262       // not accurate
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     // non-inline functions
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     // attributes
00295 
00297     Vector3f        center;
00298 
00300     float           radius;
00301 
00305     float           radiusSq;
00306 };
00307 
00308 } // namespace salt
00309 
00310 #endif // SALT_BOUNDS_H

Generated on Thu Apr 6 15:25:37 2006 for rcssserver3d by  doxygen 1.4.4