From 0251d89f11d71ce848bf54b09fde7ec0b9cf0c72 Mon Sep 17 00:00:00 2001 From: cnr-isti-vclab Date: Wed, 26 Jan 2005 10:03:08 +0000 Subject: [PATCH] aggiunta intersect ray-box --- vcg/space/intersection3.h | 82 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/vcg/space/intersection3.h b/vcg/space/intersection3.h index 7765ead2..2b96040b 100644 --- a/vcg/space/intersection3.h +++ b/vcg/space/intersection3.h @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.12 2004/10/13 12:45:51 cignoni +Better Doxygen documentation + Revision 1.11 2004/09/09 14:41:32 ponchio forgotten typename SEGMENTTYPE::... @@ -300,6 +303,85 @@ bool Intersection( const Line3 & ray, const Point3 & vert0, inte = vert0 + edge1*a + edge2*b; return true; } + + +// ray-box +template +bool Intersection( const Box3 & box, const Line3 & r, Point3 & coord ) +{ + const int NUMDIM = 3; + const int RIGHT = 0; + const int LEFT = 1; + const int MIDDLE = 2; + + int inside = 1; + char quadrant[NUMDIM]; + int i; + int whichPlane; + Point3 maxT,candidatePlane; + + // Find candidate planes; this loop can be avoided if + // rays cast all from the eye(assume perpsective view) + for (i=0; i box.max[i]) + { + quadrant[i] = RIGHT; + candidatePlane[i] = box.max[i]; + inside = 0; + } + else + { + quadrant[i] = MIDDLE; + } + } + + // Ray origin inside bounding box + if(inside){ + coord = r.Origin(); + return true; + } + + // Calculate T distances to candidate planes + for (i = 0; i < NUMDIM; i++) + { + if (quadrant[i] != MIDDLE && r.Direction()[i] !=0.) + maxT[i] = (candidatePlane[i]-r.Origin()[i]) / r.Direction()[i]; + else + maxT[i] = -1.; + } + + // Get largest of the maxT's for final choice of intersection + whichPlane = 0; + for (i = 1; i < NUMDIM; i++) + if (maxT[whichPlane] < maxT[i]) + whichPlane = i; + + // Check final candidate actually inside box + if (maxT[whichPlane] < 0.) return false; + for (i = 0; i < NUMDIM; i++) + if (whichPlane != i) + { + coord[i] = r.Origin()[i] + maxT[whichPlane] *r.Direction()[i]; + if (coord[i] < box.min[i] || coord[i] > box.max[i]) + return false; + } + else + { + coord[i] = candidatePlane[i]; + } + return true; // ray hits box +} + + + + /*@}*/ } // end namespace