From 1de3d80e9bd51a9309d43499c4c769c93fbbf339 Mon Sep 17 00:00:00 2001 From: dibenedetto Date: Wed, 28 Sep 2005 19:40:55 +0000 Subject: [PATCH] Added intersection for ray-triangle (with Ray3 type). --- vcg/space/intersection3.h | 53 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/vcg/space/intersection3.h b/vcg/space/intersection3.h index 52f71a9c..7ef24d91 100644 --- a/vcg/space/intersection3.h +++ b/vcg/space/intersection3.h @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.15 2005/06/29 15:28:31 callieri +changed intersection names to more specific to avoid ambiguity + Revision 1.14 2005/03/15 11:22:39 ganovelli added intersection between tow planes (porting from old vcg lib) @@ -79,6 +82,7 @@ Initial Commit #include #include +#include #include #include #include @@ -252,7 +256,7 @@ bool Intersection( const Line3 & ray, const Point3 & vert0, // calculate t, scale parameters, ray intersects triangle dist = edge2 * qvec; if (dist<0) return false; - T inv_det = 1.0 / det; + T inv_det = T(1.0) / det; dist *= inv_det; a *= inv_det; b *= inv_det; @@ -260,6 +264,53 @@ bool Intersection( const Line3 & ray, const Point3 & vert0, return true; } + // ray-triangle, gives barycentric coords of intersection and distance along ray. + // Ray3 type used. +template +bool Intersection( const Ray3 & ray, const Point3 & vert0, + const Point3 & vert1, const Point3 & vert2, + T & a ,T & b, T & dist) +{ + // small (hum) borders around triangle + const T EPSILON2= T(1e-8); + + const T EPSILON = T(1e-8); + + Point3 edge1 = vert1 - vert0; + Point3 edge2 = vert2 - vert0; + + // determinant + Point3 pvec = ray.Direction() ^ edge2; + + T det = edge1*pvec; + + // if determinant is near zero, ray lies in plane of triangle + if (fabs(det) < EPSILON) return false; + + // calculate distance from vert0 to ray origin + Point3 tvec = ray.Origin()- vert0; + + // calculate A parameter and test bounds + a = tvec * pvec; + if (a < -EPSILON2*det || a > det+det*EPSILON2) return false; + + // prepare to test V parameter + Point3 qvec = tvec ^ edge1; + + // calculate B parameter and test bounds + b = ray.Direction() * qvec ; + if (b < -EPSILON2*det || b + a > det+det*EPSILON2) return false; + + // calculate t, scale parameters, ray intersects triangle + dist = edge2 * qvec; + if (dist<0) return false; + T inv_det = T(1.0) / det; + dist *= inv_det; + a *= inv_det; + b *= inv_det; + + return true; +} // ray-triangle, gives intersection 3d point and distance along ray template