Go to the documentation of this file.00001 #ifndef OPENTISSUE_COLLISION_COLLISION_RAY_AABB_H
00002 #define OPENTISSUE_COLLISION_COLLISION_RAY_AABB_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012
00013 namespace OpenTissue
00014 {
00015 namespace collision
00016 {
00017
00028 template<typename vector3_type>
00029 inline bool ray_aabb(vector3_type const & p,vector3_type const & r, vector3_type const & min_coord,vector3_type const & max_coord)
00030 {
00031 using std::fabs;
00032
00033 typedef typename vector3_type::value_traits value_traits;
00034
00035 assert(min_coord(0) <= max_coord(0) || !"ray_aabb(): aabb was incorrect");
00036 assert(min_coord(1) <= max_coord(1) || !"ray_aabb(): aabb was incorrect");
00037 assert(min_coord(2) <= max_coord(2) || !"ray_aabb(): aabb was incorrect");
00038 assert(r(0)!=0 || r(1)!=0 || r(2)!=0 || !"ray_aabb(): ray vector was zero!");
00039
00040 vector3_type e = (max_coord - min_coord)*value_traits::half();
00041 vector3_type d = p - (e + min_coord);
00042
00043
00044 if (fabs(d(0)) > e(0) && d(0)*r(0) >= 0) return false;
00045 if (fabs(d(1)) > e(1) && d(1)*r(1) >= 0) return false;
00046 if (fabs(d(2)) > e(2) && d(2)*r(2) >= 0) return false;
00047
00048
00049 vector3_type rXd = cross(r , d);
00050
00051 if ( fabs(rXd(0)) > ( e(1)*fabs(r(2)) + e(2)*fabs(r(1)) ) ) return false;
00052 if ( fabs(rXd(1)) > ( e(0)*fabs(r(2)) + e(2)*fabs(r(0)) ) ) return false;
00053 if ( fabs(rXd(2)) > ( e(0)*fabs(r(1)) + e(1)*fabs(r(0)) ) ) return false;
00054
00055
00056 return true;
00057 }
00058
00059 template<typename vector3_type, typename aabb_type>
00060 inline bool ray_aabb(vector3_type const & p,vector3_type const & r, aabb_type const & aabb)
00061 {
00062 return ray_aabb(p,r,aabb.min_coord(),aabb.max_coord());
00063 }
00064
00065 template<typename ray_type, typename aabb_type>
00066 inline bool ray_aabb(ray_type const & ray, aabb_type const & aabb)
00067 {
00068 return ray_aabb(ray.p(),ray.r(),aabb.min_coord(),aabb.max_coord());
00069 }
00070
00071 }
00072 }
00073
00074
00075 #endif