00001 #ifndef OPENTISSUE_COLLISION_GJK_GJK_SUPPORT_FUNCTORS_SPHERE_H 00002 #define OPENTISSUE_COLLISION_GJK_GJK_SUPPORT_FUNCTORS_SPHERE_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/core/math/math_is_number.h> 00013 00014 #include <cmath> 00015 #include <cassert> 00016 00017 namespace OpenTissue 00018 { 00019 namespace gjk 00020 { 00021 00025 template<typename math_types> 00026 class Sphere 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_radius; 00035 00036 public: 00037 00043 T const & radius() const { return this->m_radius; } 00044 T & radius() { return this->m_radius; } 00045 00046 public: 00047 00048 Sphere() 00049 : m_radius( value_traits::one() ) 00050 {} 00051 00052 public: 00053 00054 V operator()(V const & v) const 00055 { 00056 using std::sqrt; 00057 00058 assert( this->m_radius > value_traits::zero() || !"Sphere::operator(): Radius was non-positive"); 00059 00060 T const vv = dot(v,v); 00061 assert( is_number(vv) || !"Sphere::operator(): NaN encountered"); 00062 00063 if (vv > value_traits::zero() ) 00064 { 00065 T const tmp = this->m_radius / sqrt(vv); 00066 assert( is_number(tmp) || !"Sphere::operator(): NaN encountered"); 00067 00068 return v*tmp; 00069 } 00070 return V( this->m_radius, value_traits::zero(), value_traits::zero()); 00071 } 00072 00073 }; 00074 00075 00076 } // namespace gjk 00077 00078 } // namespace OpenTissue 00079 00080 // OPENTISSUE_COLLISION_GJK_GJK_SUPPORT_FUNCTORS_SPHERE_H 00081 #endif