Go to the documentation of this file.00001 #ifndef OPENTISSUE_COLLISION_COLLISION_RAY_TRIANGLE_H
00002 #define OPENTISSUE_COLLISION_COLLISION_RAY_TRIANGLE_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 namespace OpenTissue
00013 {
00014 namespace collision
00015 {
00016
00030 template<typename vector3_type,typename real_type>
00031 bool ray_triangle(
00032 vector3_type const & p
00033 , vector3_type const & r
00034 , vector3_type const & p0
00035 , vector3_type const & p1
00036 , vector3_type const & p2
00037 , real_type & t
00038 )
00039 {
00040 assert(r(0)!=0 || r(1)!=0 || r(2)!=0 || !"ray_triangle(): ray vector was zero!");
00041
00042 vector3_type e0 = p1 - p0;
00043 vector3_type e1 = p2 - p0;
00044 vector3_type n = e0 % e1;
00045
00046 real_type nr = n*r;
00047
00048 if(nr>=0)
00049 return false;
00050
00051 t = (n*(p0-p))/nr;
00052 if(t<0)
00053 return false;
00054
00055 vector3_type pp = p + (r*t);
00056
00057
00058 real_type w1,w2,w3;
00059 OpenTissue::geometry::barycentric_algebraic(p0,p1,p2,pp,w1,w2,w3);
00060
00061 real_type delta = 10e-5;
00062 real_type lower = -delta;
00063 real_type upper = 1.+delta;
00064 if(
00065 (w1>lower)&&(w1<upper)
00066 &&
00067 (w2>lower)&&(w2<upper)
00068 &&
00069 (w3>lower)&&(w3<upper)
00070 )
00071 return true;
00072
00073
00074
00075
00076
00077
00078
00079
00080
00082
00083
00089
00090
00091 return false;
00092 }
00093
00094 template<typename vector3_type, typename triangle_type>
00095 bool ray_triangle(vector3_type const & p,vector3_type const & r, triangle_type const & triangle)
00096
00097 {
00098 return ray_triangle(p,r,triangle.p0(),triangle.p1(),triangle.p2());
00099 }
00100
00101 template<typename ray_type, typename triangle_type>
00102 bool ray_triangle(ray_type const & ray, triangle_type const & triangle)
00103
00104 {
00105 return ray_triangle(ray.p(),ray.r(),triangle.p0(),triangle.p1(),triangle.p2());
00106 }
00107
00108 }
00109 }
00110
00111
00112 #endif