From 41945cf2d5c83d44424c95b42bdd3eac8396e43b Mon Sep 17 00:00:00 2001 From: ganovelli Date: Tue, 15 Mar 2005 11:22:39 +0000 Subject: [PATCH] added intersection between tow planes (porting from old vcg lib) --- vcg/space/intersection3.h | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/vcg/space/intersection3.h b/vcg/space/intersection3.h index 2b96040b..d676777e 100644 --- a/vcg/space/intersection3.h +++ b/vcg/space/intersection3.h @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.13 2005/01/26 10:03:08 spinelli +aggiunta intersect ray-box + Revision 1.12 2004/10/13 12:45:51 cignoni Better Doxygen documentation @@ -379,6 +382,47 @@ bool Intersection( const Box3 & box, const Line3 & r, Point3 & coord ) return true; // ray hits box } +template +bool Intersection (const Plane3 & plane0, const Plane3 & plane1, + Line3 & line) +{ + // If Cross(N0,N1) is zero, then either planes are parallel and separated + // or the same plane. In both cases, 'false' is returned. Otherwise, + // the intersection line is + // + // L(t) = t*Cross(N0,N1) + c0*N0 + c1*N1 + // + // for some coefficients c0 and c1 and for t any real number (the line + // parameter). Taking dot products with the normals, + // + // d0 = Dot(N0,L) = c0*Dot(N0,N0) + c1*Dot(N0,N1) + // d1 = Dot(N1,L) = c0*Dot(N0,N1) + c1*Dot(N1,N1) + // + // which are two equations in two unknowns. The solution is + // + // c0 = (Dot(N1,N1)*d0 - Dot(N0,N1)*d1)/det + // c1 = (Dot(N0,N0)*d1 - Dot(N0,N1)*d0)/det + // + // where det = Dot(N0,N0)*Dot(N1,N1)-Dot(N0,N1)^2. + + T n00 = plane0.Direction()*plane0.Direction(); + T n01 = plane0.Direction()*plane1.Direction(); + T n11 = plane1.Direction()*plane1.Direction(); + T det = n00*n11-n01*n01; + + const T tolerance = (T)(1e-06f); + if ( math::Abs(det) < tolerance ) + return false; + + T invDet = 1.0f/det; + T c0 = (n11*plane0.Offset() - n01*plane1.Offset())*invDet; + T c1 = (n00*plane1.Offset() - n01*plane0.Offset())*invDet; + + line.SetDirection(plane0.Direction()^plane1.Direction()); + line.SetOrigin(plane0.Direction()*c0+ plane1.Direction()*c1); + + return true; +}