Package MeatEngine :: Package RayTrace :: Module triangle
[hide private]
[frames] | no frames]

Source Code for Module MeatEngine.RayTrace.triangle

 1  import MeatEngine.Math.vector 
 2  import intersect 
 3   
 4  EPSILON=0.000001 
 5   
 6  """ 
 7  from "Fast, Minimum Storage Ray/Triangle Intersection" by Moller and 
 8  Trumbore 
 9  """ 
10   
11   
12 -def intersectRayTriangle(orig, dir, 13 v0, v1, v2, 14 cullBackface=False):
15 """ 16 intersectTriangle takes 5 points (origin, direction, and three 17 vertices) and returns a distance and a u,v 18 """ 19 20 edge1=v1.sub(v0) 21 edge2=v2.sub(v0) 22 23 pVec = dir.cross(edge2) 24 det = edge1.dot(pVec) 25 26 if cullBackface: 27 if det<EPSILON: 28 # backfacing, return None 29 return None 30 31 tVec=orig.sub(v0) 32 u=tVec.dot(pVec) 33 if (u < 0 or u > det): 34 return None 35 36 qVec=tVec.cross(edge1) 37 38 v=dir.dot(qVec) 39 if (v < 0 or v > det): 40 return None 41 42 t=edge2.dot(qVec) 43 invDet=1.0/det 44 45 t *= invDet 46 u *= invDet 47 v *= invDet 48 else: 49 #not culling 50 51 if (det > -EPSILON and det < EPSILON): 52 # edge-on 53 return None 54 55 invDet= 1.0/det 56 tVec=orig.sub(v0) 57 58 u=tVec.dot(pVec)*invDet 59 if (u<0.0 or u>1.0): 60 return None 61 62 qVec=tVec.cross(edge1) 63 64 v=dir.dot(qVec)*invDet 65 66 if (v<0.0 or v>1.0): 67 return None 68 69 t=edge2.dot(qVec)*invDet 70 71 if u+v>1: 72 return None 73 74 pos=v0.add(edge1.mul(u).add(edge2.mul(v))) 75 76 return intersect.Intersection(t, 77 pos, 78 (u,v), 79 edge1.cross(edge2))
80