Go to the documentation of this file.00001 #ifndef OPENTISSUE_COLLISION_INTERSECT_INTERSECT_LINE_TRIANGLE_H
00002 #define OPENTISSUE_COLLISION_INTERSECT_INTERSECT_LINE_TRIANGLE_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/math/math_precision.h>
00013 #include <OpenTissue/core/geometry/geometry_barycentric.h>
00014 #include <OpenTissue/core/geometry/geometry_plane.h>
00015
00016 namespace OpenTissue
00017 {
00018
00019 namespace intersect
00020 {
00021
00035 template<typename vector3_type,typename triangle_type>
00036 bool line_triangle(vector3_type const & p0,vector3_type const & p1,triangle_type const & triangle,vector3_type & p)
00037 {
00038 typedef typename triangle_type::math_types math_types;
00039 typedef typename math_types::real_type real_type;
00040
00041 static real_type const threshold = math::working_precision<real_type>();
00042 static real_type const lower = -threshold;
00043 static real_type const upper = 1.+threshold;
00044
00045 OpenTissue::geometry::Plane<math_types> plane(triangle.p0(),triangle.p1(),triangle.p2());
00046
00047 real_type d0 = plane.signed_distance(p0);
00048 real_type d1 = plane.signed_distance(p1);
00049 if(d0>=0 && d1>=0)
00050 return false;
00051 if(d0<0 && d1<0)
00052 return false;
00053 real_type t = d0/(d0-d1);
00054 p = (p1-p0)*t + p0;
00055 real_type w1,w2,w3;
00056 OpenTissue::geometry::barycentric_algebraic(triangle.p0(),triangle.p1(),triangle.p2(),p,w1,w2,w3);
00057 if(
00058 (w1>lower)&&(w1<upper)
00059 &&
00060 (w2>lower)&&(w2<upper)
00061 &&
00062 (w3>lower)&&(w3<upper)
00063 )
00064 {
00065 return true;
00066 }
00067 return false;
00068 }
00069
00070 }
00071
00072 }
00073
00074
00075 #endif