| 90 | } |
| 91 | |
| 92 | int intersect_triangle(double orig[3], double dir[3], double vert0[3], double vert1[3], double vert2[3], double *t, |
| 93 | double *u, double *v) { |
| 94 | double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3]; |
| 95 | double det, inv_det; |
| 96 | |
| 97 | |
| 98 | #define CROSS(dest, v1, v2) \ |
| 99 | dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \ |
| 100 | dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \ |
| 101 | dest[2]=v1[0]*v2[1]-v1[1]*v2[0]; |
| 102 | #define DOT(v1, v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2]) |
| 103 | |
| 104 | #define SUB(dest, v1, v2) \ |
| 105 | dest[0]=v1[0]-v2[0]; \ |
| 106 | dest[1]=v1[1]-v2[1]; \ |
| 107 | dest[2]=v1[2]-v2[2]; |
| 108 | |
| 109 | |
| 110 | /* find vectors for two edges sharing vert0 */ |
| 111 | SUB(edge1, vert1, vert0); |
| 112 | SUB(edge2, vert2, vert0); |
| 113 | |
| 114 | /* begin calculating determinant - also used to calculate U parameter */ |
| 115 | CROSS(pvec, dir, edge2); |
| 116 | |
| 117 | /* if determinant is near zero, ray lies in plane of triangle */ |
| 118 | det = DOT(edge1, pvec); |
| 119 | |
| 120 | /* calculate distance from vert0 to ray origin */ |
| 121 | SUB(tvec, orig, vert0); |
| 122 | inv_det = 1.0 / det; |
| 123 | |
| 124 | CROSS(qvec, tvec, edge1); |
| 125 | |
| 126 | if (det > EPSILON) { |
| 127 | *u = DOT(tvec, pvec); |
| 128 | if (*u < 0.0 || *u > det) |
| 129 | return 0; |
| 130 | |
| 131 | /* calculate V parameter and test bounds */ |
| 132 | *v = DOT(dir, qvec); |
| 133 | if (*v < 0.0 || *u + *v > det) |
| 134 | return 0; |
| 135 | |
| 136 | } |
| 137 | /*else if(det < -EPSILON) |
| 138 | { |
| 139 | // calculate U parameter and test bounds |
| 140 | *u = DOT(tvec, pvec); |
| 141 | if (*u > 0.0 || *u < det) |
| 142 | return 0; |
| 143 | |
| 144 | // calculate V parameter and test bounds |
| 145 | *v = DOT(dir, qvec) ; |
| 146 | if (*v > 0.0 || *u + *v < det) |
| 147 | return 0; |
| 148 | }*/ |
| 149 | else return 0; /* ray is parallell to the plane of the triangle */ |
no outgoing calls
no test coverage detected