Visualization Library

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

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

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 #include <vlCore/AABB.hpp>
00033 
00034 using namespace vl;
00035 
00036 //-----------------------------------------------------------------------------
00037 // AABB
00038 //-----------------------------------------------------------------------------
00039 AABB::AABB() 
00040 {
00041   setNull();
00042 }
00043 //-----------------------------------------------------------------------------
00044 AABB::AABB( const vec3& center, real radius ) 
00045 {
00046   mMax = center + radius;
00047   mMin = center - radius;
00048 }
00049 //-----------------------------------------------------------------------------
00050 AABB::AABB( const vec3& pt1, const vec3& pt2, real displace) 
00051 {
00052   mMax = mMin = pt1;
00053   if ( mMax.x() < pt2.x() ) mMax.x() = pt2.x();
00054   if ( mMax.y() < pt2.y() ) mMax.y() = pt2.y();
00055   if ( mMax.z() < pt2.z() ) mMax.z() = pt2.z();
00056   if ( mMin.x() > pt2.x() ) mMin.x() = pt2.x();
00057   if ( mMin.y() > pt2.y() ) mMin.y() = pt2.y();
00058   if ( mMin.z() > pt2.z() ) mMin.z() = pt2.z();
00059 
00060   mMax = mMax + displace;
00061   mMin = mMin - displace;
00062 }
00063 //-----------------------------------------------------------------------------
00064 void AABB::enlarge(real displace) {
00065   if ( isNull() )
00066     return;
00067 
00068   mMax = mMax + displace;
00069   mMin = mMin - displace;
00070 }
00071 //-----------------------------------------------------------------------------
00072 bool AABB::intersects(const AABB& bb) const
00073 {
00074   if (isNull() || bb.isNull())
00075     return false;
00076 
00077   if (mMax.x() < bb.mMin.x())
00078     return false;
00079 
00080   if (mMax.z() < bb.mMin.z())
00081     return false;
00082 
00083   if (mMin.x() > bb.mMax.x())
00084     return false;
00085 
00086   if (mMin.z() > bb.mMax.z())
00087     return false;
00088 
00089   return true;
00090 }
00091 //-----------------------------------------------------------------------------
00092 vec3 AABB::clip(const vec3& v, bool clipx, bool clipy, bool clipz) const 
00093 {
00094   if (isNull())
00095     return v;
00096 
00097   vec3 tmp = v;
00098 
00099   if (clipx) {
00100       if (v.x() > mMax.x())
00101         tmp.x() = mMax.x();
00102       if (v.x() < mMin.x())
00103         tmp.x() = mMin.x();
00104   }
00105 
00106   if (clipy) {
00107       if (v.y() > mMax.y())
00108         tmp.y() = mMax.y();
00109       if (v.y() < mMin.y())
00110         tmp.y() = mMin.y();
00111     }
00112 
00113   if (clipz) {
00114       if (v.z() > mMax.z())
00115         tmp.z() = mMax.z();
00116       if (v.z() < mMin.z())
00117         tmp.z() = mMin.z();
00118     }
00119     return tmp;
00120 }
00121 //-----------------------------------------------------------------------------
00122 bool AABB::isInside(const vec3& v, bool clipx, bool clipy, bool clipz) const 
00123 {
00124   vec3 t = v;
00125   return v == clip(t, clipx, clipy, clipz);
00126 }
00127 //-----------------------------------------------------------------------------
00128 bool AABB::isInside(const vec3& v) const 
00129 {
00130   return v.x() >= minCorner().x() && v.x() <= maxCorner().x() &&
00131          v.y() >= minCorner().y() && v.y() <= maxCorner().y() &&
00132          v.z() >= minCorner().z() && v.z() <= maxCorner().z();
00133 }
00134 //-----------------------------------------------------------------------------
00135 void AABB::addPoint(const vec3& v, real radius) 
00136 {
00137   if (isNull())
00138   {
00139     mMax = v;
00140     mMin = v;
00141   }
00142 
00143   if ( mMax.x() < v.x() + radius) mMax.x() = v.x() + radius;
00144   if ( mMax.y() < v.y() + radius) mMax.y() = v.y() + radius;
00145   if ( mMax.z() < v.z() + radius) mMax.z() = v.z() + radius;
00146   if ( mMin.x() > v.x() - radius) mMin.x() = v.x() - radius;
00147   if ( mMin.y() > v.y() - radius) mMin.y() = v.y() - radius;
00148   if ( mMin.z() > v.z() - radius) mMin.z() = v.z() - radius;
00149 }
00150 //-----------------------------------------------------------------------------
00151 real AABB::width() const {
00152   if (isNull())
00153     return 0;
00154   else
00155     return mMax.x() - mMin.x();
00156 }
00157 //-----------------------------------------------------------------------------
00158 real AABB::height() const {
00159   if (isNull())
00160     return 0;
00161   else
00162     return mMax.y() - mMin.y();
00163 }
00164 //-----------------------------------------------------------------------------
00165 real AABB::depth() const {
00166   if (isNull())
00167     return 0;
00168   else
00169     return mMax.z() - mMin.z();
00170 }
00171 //-----------------------------------------------------------------------------
00172 AABB AABB::operator+(const AABB& aabb) const 
00173 {
00174   if(isNull())
00175     return aabb;
00176   if (aabb.isNull())
00177     return *this;
00178   AABB tmp = aabb;
00179   tmp.addPoint( mMin );
00180   tmp.addPoint( mMax );
00181   return tmp;
00182 }
00183 //-----------------------------------------------------------------------------
00184 vec3 AABB::center() const
00185 {
00186   vec3 c = (minCorner() + maxCorner()) / 2.0f;
00187   return c;
00188 }
00189 //-----------------------------------------------------------------------------

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.