Ray-triagle itersectio ria urless October 2006 I this hadout, we explore the steps eeded to compute the itersectio of a ray with a triagle, ad the to compute the barycetric coordiates of that itersectio. First, we cosider the geometry of such a itersectio: P d where a ray with origi P ad directio d itersects a triagle defied by its vertices,,, ad at itersectio poit. The square regio diagrammatically surroudig triagle represets the supportig plae of the triagle, i.e., the plae that the triagle lies o. To perform ray-triagle itersectio, we must perform two steps: 1. Determie the poit of itersectio,. 2. Determie if lies iside of triagle. Fially, we ll compute the barycetric coordiates of with respect to. Determiig Determiig the poit of itersectio i tur requires us to take two more steps: 1. Determie the equatio of the supportig plae for triagle. 2. Itersect the ray with the supportig plae. We ll hadle these steps i reverse order. Ray-plae itersectio It is well kow that the equatio of a plae ca be writte as: ax + by + cz = d The coefficiets a, b, ad c form a vector that is ormal to the plae, = [a b c] T. Thus, we ca re-write the plae equatio as: x = d 1
where x = [x y z] T. Now we cosider the ray determied by P ad d: Rt ( ) = P+ td To solve for the itersectio of ray R(t) with the plae, we simply substitute x = R(t) ito the plae equatio ad solve for t: Rt ( ) = d [ P+ td] = d P+ t d= d d P t = d Note that if d =0, the d is parallel to the plae ad the ray does ot itersect the plae (i.e., the itersectio is at ifiity). e sure to check for this case! The itersectio poit ca thus be determied by pluggig the resultig t ito the ray equatio for R(t). Solvig for the supportig plae To itersect with a triagle, we eed to solve for the equatio of its supportig plae. s oted above, we eed oly solve for the ormal to the plae ad the coefficiet d. The figure below illustrates how we might solve for the ormal usig cross products. - - I particular, we ca costruct the vectors - ad - ad compute their ormalized cross product: = ( ) ( ) ( ) ( ) [Strictly speakig, does ot eed to be ormalized (i.e., of uit legth) i order for ray-plae ad ray-triagle itersectio to work; however, for shadig purposes, a ormalized ormal is ecessary ayway, so we preset the ormalized result.] 2
We should take a momet here to make a few commets. First, if we view the ormal to a triagle as simply beig a uit vector that is perpedicular to the supportig plae, the, i fact, both the computed above ad would satisfy our defiitio. Which oe is right? We ll assume that a triagle is actually oe-sided so that there is a correct ormal directio that poits to the outside of the object that the triagle belogs to. I additio, we ll assume that this directio is implicit i the orderig of the vertices. I particular, if you curl the figers of your right had from to to, the your thumb will poit i the directio of the ormal. This right-had rule is aother way of sayig that we expect the vertices to be arraged i couter-clockwise order with respect to the ormal. Next, we chose to cross - ito -. Did the order matter? Yes! If we had doe it the other way aroud, we would have bee computig. Fially, could we have chose other vectors for computig the ormal? Defiitely! y two vectors that are taget to the plae ca be crossed ito each other (i the correct order!) to yield the ormal vector. For istace, we could have crossed - with -, i that order, to compute the ormal. Gettig back to the sub-problem at had, how do we compute the remaiig coefficiet d i order to establish the supportig plae equatio? Earlier we stated the plae equatio as x = d. y x that satisfies this equatio must lie o the plae. Thus, if we pick a poit that lies o the plae, ad plug it ito the equatio, the the equatio must be satisfied. learly, the vertices of the triagle lie o the supportig plae, so we just eed to pick oe ay oe of them. We ll pick. Now we fid that, simply: d = t this poit, we have ow computed all the coefficiets eeded to describe the supportig plae of the triagle ad ca go ahead ad compute the itersectio. Triagle iside-outside testig Now we ve got the situatio where we have a triagle ad a itersectio poit, but we do t kow if actually lies iside the triagle. To determie this, we eed to perform a iside-outside test. I particular, lies iside of triagle if it lies o the iside of all of the lies determied by edges,, ad. elow, o the left, we see that satisfies this requiremet. O the right, we see that is iside of edges ad, but ot, ad is therefore outside the triagle. We ca ow use cross products to determie which side of each edge is o. osider the edge. If we form the vectors - ad -, the their cross product should poit i the same directio as the ormal. I other words, is o the iside of if: [( ) ( )] 3
The figure below, o the left, illustrates this idea for the two cases where is iside ad outside of the edge. I the secod case, the cross product will poit i the opposite directio of the ormal. - - - - We ca develop similar coditios for the other edges. s a result, is iside of if all of the followig coditios are met simultaeously: [( ) ( )] [( ) ( )] [( ) ( )] If ay oe of these tests fails, the is ot iside of, ad the result is o itersectio. Note that we used the greater tha or equal to compariso here. We do this i order to isure that the vertices,, ad ad the edges,, ad are all cosidered to be part of the triagle. For istace if were equal to, the the first test would retur a value of zero. Or, if were alog the edge, the the secod test would retur a value of zero (parallel vectors have a cross product of zero). I both cases, however, we would wat to declare to have itersected with the triagle; thus, the tests must have the greater tha or equal to comparisos. Note that the triagle-itersectio procedure described i this documet is ot as efficiet as it could be. s oted i class, you ca speed up the procedure by first projectig the triagle oto a axis aliged plae ad the perform the calculatios i 2D. The details of this procedure are beyod the scope of this documet. omputig barycetric coordiates s a fial step, if is iside of, we will compute the barycetric coordiates of with respect to. Recall from lecture that the barycetric coordiates ca be calculated i terms of area ratios: rea( ) rea( ) rea( ) α = β = γ = rea( ) rea( ) rea( ) I fact, these areas are each proportioal to the legths of the correspodig cross products described above. For istace, rea( ) = ( ) ( ) / 2 4
Sice the cross product poits i the directio (give that is iside of ), the we ca actually compute the legth of the cross product as simply: ( ) ( ) = [( ) ( )] Fially, we have the equatios for the barycetric coordiates of : [( ) ( )] [( ) ( )] [( ) ( )] α = β = γ = [( ) ( ) ] [( ) ( ) ] [( ) ( ) ] Note that we already computed the umerators i the previous sectios, as well as the terms eeded for the deomiator, so very little extra work is eeded to establish the barycetric coordiates. Oce these coordiates are calculated it is possible to, e.g., iterpolate ormals stored at vertices. I that case, oe would compute the ormal N at poit to be a weighted sum of the ormals N, N, ad N at vertices,, ad, respectively: N = αn + βn + γn αn + βn + γn The deomiator is required so that N is of uit legth ad ca be used i subsequet shadig, reflectio, ad refractio calculatios. 5