All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends

AABB.h

00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2011, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00037 #ifndef FCL_AABB_H
00038 #define FCL_AABB_H
00039 
00040 
00041 #include "fcl/math/vec_3f.h"
00042 
00043 namespace fcl
00044 {
00045 
00047 class AABB
00048 {
00049 public:
00051   Vec3f min_;
00053   Vec3f max_;
00054 
00056   AABB();
00057 
00059   AABB(const Vec3f& v) : min_(v), max_(v)
00060   {
00061   }
00062 
00064   AABB(const Vec3f& a, const Vec3f&b) : min_(min(a, b)),
00065                                         max_(max(a, b))
00066   {
00067   }
00068 
00070   AABB(const AABB& core, const Vec3f& delta) : min_(core.min_ - delta),
00071                                                max_(core.max_ + delta)
00072   {
00073   }
00074 
00076   AABB(const Vec3f& a, const Vec3f& b, const Vec3f& c) : min_(min(min(a, b), c)),
00077                                                          max_(max(max(a, b), c))
00078   {
00079   }
00080 
00082   inline bool overlap(const AABB& other) const
00083   {
00084     if(min_[0] > other.max_[0]) return false;
00085     if(min_[1] > other.max_[1]) return false;
00086     if(min_[2] > other.max_[2]) return false;
00087 
00088     if(max_[0] < other.min_[0]) return false;
00089     if(max_[1] < other.min_[1]) return false;
00090     if(max_[2] < other.min_[2]) return false;
00091 
00092     return true;
00093   }    
00094 
00096   inline bool contain(const AABB& other) const
00097   {
00098     return (other.min_[0] >= min_[0]) && (other.max_[0] <= max_[0]) && (other.min_[1] >= min_[1]) && (other.max_[1] <= max_[1]) && (other.min_[2] >= min_[2]) && (other.max_[2] <= max_[2]);
00099   }
00100 
00101 
00103   inline bool axisOverlap(const AABB& other, int axis_id) const
00104   {
00105     if(min_[axis_id] > other.max_[axis_id]) return false;
00106 
00107     if(max_[axis_id] < other.min_[axis_id]) return false;
00108 
00109     return true;
00110   }
00111 
00113   inline bool overlap(const AABB& other, AABB& overlap_part) const
00114   {
00115     if(!overlap(other))
00116     {
00117       return false;
00118     }
00119     
00120     overlap_part.min_ = max(min_, other.min_);
00121     overlap_part.max_ = min(max_, other.max_);
00122     return true;
00123   }
00124 
00125 
00127   inline bool contain(const Vec3f& p) const
00128   {
00129     if(p[0] < min_[0] || p[0] > max_[0]) return false;
00130     if(p[1] < min_[1] || p[1] > max_[1]) return false;
00131     if(p[2] < min_[2] || p[2] > max_[2]) return false;
00132 
00133     return true;
00134   }
00135 
00137   inline AABB& operator += (const Vec3f& p)
00138   {
00139     min_.ubound(p);
00140     max_.lbound(p);
00141     return *this;
00142   }
00143 
00145   inline AABB& operator += (const AABB& other)
00146   {
00147     min_.ubound(other.min_);
00148     max_.lbound(other.max_);
00149     return *this;
00150   }
00151 
00153   inline AABB operator + (const AABB& other) const
00154   {
00155     AABB res(*this);
00156     return res += other;
00157   }
00158 
00160   inline FCL_REAL width() const
00161   {
00162     return max_[0] - min_[0];
00163   }
00164 
00166   inline FCL_REAL height() const
00167   {
00168     return max_[1] - min_[1];
00169   }
00170 
00172   inline FCL_REAL depth() const
00173   {
00174     return max_[2] - min_[2];
00175   }
00176 
00178   inline FCL_REAL volume() const
00179   {
00180     return width() * height() * depth();
00181   }  
00182 
00184   inline FCL_REAL size() const
00185   {
00186     return (max_ - min_).sqrLength();
00187   }
00188 
00190   inline  Vec3f center() const
00191   {
00192     return (min_ + max_) * 0.5;
00193   }
00194 
00196   FCL_REAL distance(const AABB& other, Vec3f* P, Vec3f* Q) const;
00197 
00199   FCL_REAL distance(const AABB& other) const;
00200 
00202   inline bool equal(const AABB& other) const
00203   {
00204     return min_.equal(other.min_) && max_.equal(other.max_);
00205   }
00206 
00208   inline AABB& expand(const Vec3f& delta)
00209   {
00210     min_ -= delta;
00211     max_ += delta;
00212     return *this;
00213   }
00214 
00216   inline AABB& expand(const AABB& core, FCL_REAL ratio)
00217   {
00218     min_ = min_ * ratio - core.min_;
00219     max_ = max_ * ratio - core.max_;
00220     return *this;
00221   }  
00222 };
00223 
00225 static inline AABB translate(const AABB& aabb, const Vec3f& t)
00226 {
00227   AABB res(aabb);
00228   res.min_ += t;
00229   res.max_ += t;
00230   return res;
00231 }
00232 
00233 }
00234 
00235 #endif