00001 #ifndef OPENTISSUE_COLLISION_GJK_GJK_SUPPORT_FUNCTORS_CAPSULE_H 00002 #define OPENTISSUE_COLLISION_GJK_GJK_SUPPORT_FUNCTORS_CAPSULE_H 00003 // 00004 // OpenTissue Template Library 00005 // - A generic toolbox for physics-based modeling and simulation. 00006 // Copyright (C) 2008 Department of Computer Science, University of Copenhagen. 00007 // 00008 // OTTL is licensed under zlib: http://opensource.org/licenses/zlib-license.php 00009 // 00010 #include <OpenTissue/configuration.h> 00011 00012 #include <OpenTissue/collision/gjk/support_functors/gjk_sphere.h> 00013 00014 #include <OpenTissue/core/math/math_is_number.h> 00015 00016 #include <cassert> 00017 00018 namespace OpenTissue 00019 { 00020 namespace gjk 00021 { 00025 template<typename math_types> 00026 class Capsule 00027 { 00028 protected: 00029 00030 typedef typename math_types::real_type T; 00031 typedef typename math_types::vector3_type V; 00032 typedef typename math_types::value_traits value_traits; 00033 00034 T m_half_height; 00035 T m_radius; 00036 00037 public: 00038 00042 T const & half_height() const { return this->m_half_height; } 00043 T & half_height() { return this->m_half_height; } 00044 00048 T const & radius() const { return this->m_radius; } 00049 T & radius() { return this->m_radius; } 00050 00051 public: 00052 00053 Capsule() 00054 : m_half_height( value_traits::one() ) 00055 , m_radius( value_traits::one() ) 00056 {} 00057 00058 public: 00059 00060 V operator()(V const & v) const 00061 { 00062 assert( this->m_half_height >= value_traits::zero() || !"Capsule::operator(): Negative half height"); 00063 assert( this->m_radius >= value_traits::zero() || !"Capsule::operator(): Negative radius"); 00064 00065 Sphere<math_types> S; 00066 S.radius() = this->m_radius; 00067 00068 // Get the support point of the sphere 00069 V w = S(v); 00070 00071 // Cut the sphere into two halves and displace them along the z-axis. 00072 if( v(2) > value_traits::zero() ) 00073 w(2) += this->m_half_height; 00074 else if( v(2) < value_traits::zero() ) 00075 w(2) -= this->m_half_height; 00076 00077 assert( is_number( w(0) ) || !"Capsule::operator(): NaN encountered"); 00078 assert( is_number( w(1) ) || !"Capsule::operator(): NaN encountered"); 00079 assert( is_number( w(2) ) || !"Capsule::operator(): NaN encountered"); 00080 00081 return w; 00082 } 00083 00084 }; 00085 00086 } // namespace gjk 00087 00088 } // namespace OpenTissue 00089 00090 // OPENTISSUE_COLLISION_GJK_GJK_SUPPORT_FUNCTORS_CAPSULE_H 00091 #endif