Fast, Minimum Storage Ray/Triangle Intersection
Total Page:16
File Type:pdf, Size:1020Kb
Fast Minimum Storage RayTriangle Intersection Ben Trumbore Tomas Moller Program of Computer Graphics Prosolvia Clarus AB Cornell University Chalmers UniversityofTechnology Email wbtgraphicscornelledu Email tompaclarusse Abstract Wepresent a clean algorithm for determining whether a rayintersects a triangle The algorithm translates the origin of the ray and then changes T the base of that vector which yields a vector t u v where t is the distance to the plane in which the triangle lies and u v represents the co ordinates inside the triangle One advantage of this metho d is that the plane equation need not b e computed on the y nor b e stored which can amount to signicant mem ory savings for triangle meshes As we found our metho d to b e compara ble in sp eed to previous metho ds we b elieve it is the fastest raytriangle intersection routine for triangles whichdo not have precomputed plane equations Keywords ray tracing intersection raytriangleintersection base trans formation Intro duction Aray R t with origin O and normalized direction D is dened as R tO tD and a triangle is dened by three vertices V V and V In the raytriangle intersection problem we want to determine if the ray intersects the triangle Previous algorithms havesolved this by rst computing the intersection b etween the ray and the plane in which the triangle lies and then testing if the intersection point is inside the edges Our algorithm uses minimal storage ie only the vertices of the triangle need to be stored and do es not need any prepro cessing For triangle meshes the memory savings are signicant ranging from ab out to dep ending on the amountofvertex sharing In our algorithm a transformation is constructed and applied to the origin of the ray The transformation yields a vector containing the distance t to the intersection and the co ordinates u v of the intersection In this waythe rayplane intersection of previous algorithms is avoided It should b e noted that this metho d has b een known b efore by for example and Intersection Algorithm Apoint T u v on a triangle is given by T u v u v V uV vV where u v are the barycentric co ordinates which must fulll u v and u v Note that u v can be used for texture mapping normal interp olation color interp olation etc Computing the intersection b etween the ray R t and the triangle T u v is equivalenttoR tT u v which yields O tD u v V uV vV Rearranging the terms gives t D V V V V O V u v This means the barycentric co ordinates u v and the distance t from the ray origin to the intersection point can be found by solving the linear system of equations ab ove The ab ove can b e thought of geometrically as translating the triangle to the origin and transforming it to a unit triangle in y z with the ray direction aligned with x as illustrated in gure where M D V V V V is the matrix in equation O M−1[O−V ] D O−V0 0 V2 v V −V −1 translation 2 0 M V1 1 V1−V0 V0 u 1 Figure Translation and change of base of the rayorigin Arenberg in describ es a similar algorithm to the one ab ove He also constructs a matrix but uses the normal of the triangle instead of the ray direction D This metho d requires storing the normal for each triangle or computing them on the y Denoting E V V E V V and T O V the solution to equation is obtained by using Cramers rule t j T E E j u jD T E j jD E E j v jD E T j From linear algebra weknow that jA B C j A C B C B A Equation could therefore b e rewritten as t T E E Q E u D E T P T D E E P E v T E D Q D where P D E and Q T E In our implementation we reuse these factors to sp eed up the computations Implementation The following C implementation available online has b een tailored for optimum p erformance There are two branches in the co de one which eciently culls all back facing triangles ifdef TEST CULL and the other which performs the intersection test on twosided triangles else All computations are delayed until it is known that they are required For example the value for v is not computed until the value of u is found to b e within the allowable range The onesided intersection routine eliminates all triangles where the value of the determinant det is negative This allows the routines only division op eration to b e delayed until an intersection has b een conrmed For shadow test rays this division is not needed at all since all we need is whether the triangle is intersected The twosided intersection routine is forced to p erform that division op er ation in order to evaluate the values of u and v Alternatively this function could b e rewritten to conditionally compare u and v to based on the sign of det Some asp ects of this co de deserve sp ecial attention The calculation of edge vectors can be done as a prepro cess with edge and edge b eing stored in place of vert and vert This sp eedup is only p ossible when the actual spatial lo cations of vert and vert are not needed for other calculations and when the vertex lo cation data is not shared b etween triangles To ensure numerical stability the test which eliminates parallel rays must compare the determinant to a small interval around zero With a prop erly adjusted EPSILON value this algorithm is extremely stable If only front facing triangles are to b e tested the determinant can b e compared to EPSILON rather than a negative determinant indicates a back facing triangle The value of u is compared to an edge of the triangle u and also to a line parallel to that edge but passing through the opp osite p ointofthetriangle u Though not actually testing an edge of the triangle this second test eciently rules out manyintersection p oints without further calculation define EPSILON define CROSSdestvv destvvvv destvvvv destvvvv define DOTvv vvvvv v define SUBdestvv destvv destvv destvv int intersecttriangledouble orig double dir double vert double vert double vert double t double u double v double edge edge tvec pvec qvec double detinvdet find vectors for two edges sharing vert SUBedge vert vert SUBedge vert vert begin calculating determinant also used to calculate U parameter CROSSpvec dir edge if determinant is near zero ray lies in plane of triangle det DOTedge pvec ifdef TESTCULL define TESTCULL if culling is desired if det EPSILON return calculate distance from vert to ray origin SUBtvec orig vert calculate U parameter and test bounds u DOTtvec pvec if u u det return prepare to test V parameter CROSSqvec tvec edge calculate V parameter and test bounds v DOTdir qvec if v u v det return calculate t scale parameters ray intersects triangle t DOTedge qvec invdet det t invdet u invdet v invdet else the nonculling branch if det EPSILON det EPSILON return invdet det calculate distance from vert to ray origin SUBtvec orig vert calculate U parameter and test bounds u DOTtvec pvec invdet if u u return prepare to test V parameter CROSSqvec tvec edge calculate V parameter and test bounds v DOTdir qvec invdet if v u v return calculate t ray intersects triangle t DOTedge qvec invdet endif return Results In a raytriangle intersection routine that also computes the barycentric co ordinates was presented We compared that metho d to ours The two non culling metho ds were implemented in an ecientray tracer Figure presents ray tracing runtimes from a HewlettPackard workstation for the three mo dels shown in gures In this particular implementation the p erfor mance of the two metho ds is roughly comparable detailed statistics is available online Mo del Ob jects Polygons Lights Our metho d sec Badouel sec Car Mandala Fallingwater Figure Contents and runtimes for data sets in gures Conclusions We present an algorithm for raytriangle intersection which we show to be comparable in sp eed to previous metho ds while signicantly reducing memory storage costs byavoiding storing triangle plane equations Acknowledgements Thanks to Peter Shirley Eric Lafortune and the anonymous reviewer whose suggestions greatly improved this pap er This work was supp orted by the NSFARPA Science and Technology Center for Computer Graphics and Scientic Visualization ASC and bythe HewlettPackard Corp oration and by Prosolvia Clarus AB References Arenb erg Je Arenb erg Re RayTriangle Intersection with Barycentric Coordinates in Ray Tracing News edited by Eric Haines Vol No November httpwwwacmorgtogresourcesRTNews Badouel Didier Badouel An Ecient RayPolygon Intersection in Graphics Gems edited by Andrew S Glassner Academic Press Inc pp Haines Eric Haines Point in Polygon Strategies in Graphics Gems IV edited byPaul S Heckb ert AP Professional pp Patel Edward Patel p ersonal communication Shirley Peter Shirley p ersonal communication Web information Source co de statistical analysis and images are available online at httpwwwacmorgjgtpap ersMollerTrumb ore Figure Falling Water Figure Mandala Figure Car mo del is courtesy of Nya Persp ektiv Design AB .