Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlCore/AABB.hpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #ifndef AABB_INCLUDE_ONCE
00033 #define AABB_INCLUDE_ONCE
00034 
00035 #include <vlCore/Vector3.hpp>
00036 #include <vlCore/Matrix4.hpp>
00037 
00038 namespace vl
00039 {
00040   //-----------------------------------------------------------------------------
00041   // AABB
00042   //-----------------------------------------------------------------------------
00044   class VLCORE_EXPORT AABB 
00045   {
00046   public:
00048     AABB();
00049 
00051     AABB( const vec3& center, real radius );
00052 
00054     AABB( const vec3& pt1, const vec3& pt2, real displace=0);
00055 
00057     void setNull() { mMin = 1; mMax = -1; }
00058 
00060     bool isNull()  const { return mMin.x() > mMax.x() || mMin.y() > mMax.y() || mMin.z() > mMax.z(); }
00061 
00063     bool isPoint() const { return mMin == mMax; }
00064 
00067     void enlarge(real displace);
00068 
00070     bool intersects(const AABB & bb) const;
00071 
00073     vec3 clip(const vec3& p, bool clipx=true, bool clipy=true, bool clipz=true) const;
00074 
00077     bool isInside(const vec3& p, bool clipx, bool clipy, bool clipz) const;
00078 
00080     bool isInside(const vec3& p) const;
00081 
00083     real width() const;
00084 
00086     real height() const;
00087 
00089     real depth() const;
00090 
00092     bool operator==(const AABB& aabb) const
00093     {
00094       return mMin == aabb.mMin && mMax == aabb.mMax;
00095     }
00096 
00098     bool operator!=(const AABB& aabb) const
00099     {
00100       return !operator==(aabb);
00101     }
00102 
00104     AABB operator+(const AABB& aabb) const;
00105 
00107     AABB& operator+=(const AABB& other)
00108     {
00109       *this = *this + other;
00110       return *this;
00111     }
00112 
00114     AABB operator+(const vec3& p)
00115     {
00116       AABB aabb = *this;
00117       aabb += p;
00118       return aabb;
00119     }
00120 
00122     const AABB& operator+=(const vec3& p)
00123     {
00124       addPoint(p);
00125       return *this;
00126     }
00127 
00129     vec3 center() const;
00130 
00132     real longestSideLength() const
00133     {
00134       real side = width();
00135       if (height() > side)
00136         side = height();
00137       if (depth() > side)
00138         side = depth();
00139       return side;
00140     }
00141 
00144     void addPoint(const vec3& p, real radius);
00145 
00147     void addPoint(const vec3& p) 
00148     {
00149       if (isNull())
00150       {
00151         mMax = p;
00152         mMin = p;
00153         return;
00154       }
00155 
00156       if ( mMax.x() < p.x() ) mMax.x() = p.x();
00157       if ( mMax.y() < p.y() ) mMax.y() = p.y();
00158       if ( mMax.z() < p.z() ) mMax.z() = p.z();
00159       if ( mMin.x() > p.x() ) mMin.x() = p.x();
00160       if ( mMin.y() > p.y() ) mMin.y() = p.y();
00161       if ( mMin.z() > p.z() ) mMin.z() = p.z();
00162     }
00163 
00165     void transformed(AABB& out, const mat4& mat) const 
00166     {
00167       out.setNull();
00168       if ( !isNull() )
00169       {
00170         out.addPoint( mat * vec3(minCorner().x(), minCorner().y(), minCorner().z()) );
00171         out.addPoint( mat * vec3(minCorner().x(), maxCorner().y(), minCorner().z()) );
00172         out.addPoint( mat * vec3(maxCorner().x(), maxCorner().y(), minCorner().z()) );
00173         out.addPoint( mat * vec3(maxCorner().x(), minCorner().y(), minCorner().z()) );
00174         out.addPoint( mat * vec3(minCorner().x(), minCorner().y(), maxCorner().z()) );
00175         out.addPoint( mat * vec3(minCorner().x(), maxCorner().y(), maxCorner().z()) );
00176         out.addPoint( mat * vec3(maxCorner().x(), maxCorner().y(), maxCorner().z()) );
00177         out.addPoint( mat * vec3(maxCorner().x(), minCorner().y(), maxCorner().z()) );
00178       }
00179     }
00180 
00182     AABB transformed(const mat4& mat) const 
00183     {
00184       AABB aabb;
00185       transformed(aabb, mat);
00186       return aabb;
00187     }
00188     
00190     const vec3& minCorner() const { return mMin; }
00191 
00193     const vec3& maxCorner() const { return mMax; }
00194 
00196     void setMinCorner(real x, real y, real z) { mMin.x() = x; mMin.y() = y; mMin.z() = z; }
00197 
00199     void setMinCorner(const vec3& v) { mMin = v; }
00200 
00202     void setMaxCorner(real x, real y, real z) { mMax.x() = x; mMax.y() = y; mMax.z() = z; }
00203 
00205     void setMaxCorner(const vec3& v) { mMax = v; }
00206 
00208     real volume() const { return width() * height() * depth(); }
00209 
00210   protected:
00211     vec3 mMin;
00212     vec3 mMax;
00213   };
00214 }
00215 
00216 #endif

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:28.
Permission is granted to use this page to write and publish articles regarding Visualization Library.