raytri.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* Ray-Triangle Intersection Test Routines */
  2. /* Different optimizations of my and Ben Trumbore's */
  3. /* code from journals of graphics tools (JGT) */
  4. /* http://www.acm.org/jgt/ */
  5. /* by Tomas Moller, May 2000 */
  6. // Alec: I've added an include guard, made all functions inline and added
  7. // IGL_RAY_TRI_ to #define macros
  8. #ifndef IGL_RAY_TRI_C
  9. #define IGL_RAY_TRI_C
  10. #include <math.h>
  11. #define IGL_RAY_TRI_EPSILON 0.000001
  12. #define IGL_RAY_TRI_CROSS(dest,v1,v2) \
  13. dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
  14. dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \
  15. dest[2]=v1[0]*v2[1]-v1[1]*v2[0];
  16. #define IGL_RAY_TRI_DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
  17. #define IGL_RAY_TRI_SUB(dest,v1,v2) \
  18. dest[0]=v1[0]-v2[0]; \
  19. dest[1]=v1[1]-v2[1]; \
  20. dest[2]=v1[2]-v2[2];
  21. /* the original jgt code */
  22. inline int intersect_triangle(double orig[3], double dir[3],
  23. double vert0[3], double vert1[3], double vert2[3],
  24. double *t, double *u, double *v)
  25. {
  26. double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
  27. double det,inv_det;
  28. /* find vectors for two edges sharing vert0 */
  29. IGL_RAY_TRI_SUB(edge1, vert1, vert0);
  30. IGL_RAY_TRI_SUB(edge2, vert2, vert0);
  31. /* begin calculating determinant - also used to calculate U parameter */
  32. IGL_RAY_TRI_CROSS(pvec, dir, edge2);
  33. /* if determinant is near zero, ray lies in plane of triangle */
  34. det = IGL_RAY_TRI_DOT(edge1, pvec);
  35. if (det > -IGL_RAY_TRI_EPSILON && det < IGL_RAY_TRI_EPSILON)
  36. return 0;
  37. inv_det = 1.0 / det;
  38. /* calculate distance from vert0 to ray origin */
  39. IGL_RAY_TRI_SUB(tvec, orig, vert0);
  40. /* calculate U parameter and test bounds */
  41. *u = IGL_RAY_TRI_DOT(tvec, pvec) * inv_det;
  42. if (*u < 0.0 || *u > 1.0)
  43. return 0;
  44. /* prepare to test V parameter */
  45. IGL_RAY_TRI_CROSS(qvec, tvec, edge1);
  46. /* calculate V parameter and test bounds */
  47. *v = IGL_RAY_TRI_DOT(dir, qvec) * inv_det;
  48. if (*v < 0.0 || *u + *v > 1.0)
  49. return 0;
  50. /* calculate t, ray intersects triangle */
  51. *t = IGL_RAY_TRI_DOT(edge2, qvec) * inv_det;
  52. return 1;
  53. }
  54. /* code rewritten to do tests on the sign of the determinant */
  55. /* the division is at the end in the code */
  56. inline int intersect_triangle1(double orig[3], double dir[3],
  57. double vert0[3], double vert1[3], double vert2[3],
  58. double *t, double *u, double *v)
  59. {
  60. double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
  61. double det,inv_det;
  62. /* find vectors for two edges sharing vert0 */
  63. IGL_RAY_TRI_SUB(edge1, vert1, vert0);
  64. IGL_RAY_TRI_SUB(edge2, vert2, vert0);
  65. /* begin calculating determinant - also used to calculate U parameter */
  66. IGL_RAY_TRI_CROSS(pvec, dir, edge2);
  67. /* if determinant is near zero, ray lies in plane of triangle */
  68. det = IGL_RAY_TRI_DOT(edge1, pvec);
  69. if (det > IGL_RAY_TRI_EPSILON)
  70. {
  71. /* calculate distance from vert0 to ray origin */
  72. IGL_RAY_TRI_SUB(tvec, orig, vert0);
  73. /* calculate U parameter and test bounds */
  74. *u = IGL_RAY_TRI_DOT(tvec, pvec);
  75. if (*u < 0.0 || *u > det)
  76. return 0;
  77. /* prepare to test V parameter */
  78. IGL_RAY_TRI_CROSS(qvec, tvec, edge1);
  79. /* calculate V parameter and test bounds */
  80. *v = IGL_RAY_TRI_DOT(dir, qvec);
  81. if (*v < 0.0 || *u + *v > det)
  82. return 0;
  83. }
  84. else if(det < -IGL_RAY_TRI_EPSILON)
  85. {
  86. /* calculate distance from vert0 to ray origin */
  87. IGL_RAY_TRI_SUB(tvec, orig, vert0);
  88. /* calculate U parameter and test bounds */
  89. *u = IGL_RAY_TRI_DOT(tvec, pvec);
  90. /* printf("*u=%f\n",(float)*u); */
  91. /* printf("det=%f\n",det); */
  92. if (*u > 0.0 || *u < det)
  93. return 0;
  94. /* prepare to test V parameter */
  95. IGL_RAY_TRI_CROSS(qvec, tvec, edge1);
  96. /* calculate V parameter and test bounds */
  97. *v = IGL_RAY_TRI_DOT(dir, qvec) ;
  98. if (*v > 0.0 || *u + *v < det)
  99. return 0;
  100. }
  101. else return 0; /* ray is parallell to the plane of the triangle */
  102. inv_det = 1.0 / det;
  103. /* calculate t, ray intersects triangle */
  104. *t = IGL_RAY_TRI_DOT(edge2, qvec) * inv_det;
  105. (*u) *= inv_det;
  106. (*v) *= inv_det;
  107. return 1;
  108. }
  109. /* code rewritten to do tests on the sign of the determinant */
  110. /* the division is before the test of the sign of the det */
  111. inline int intersect_triangle2(double orig[3], double dir[3],
  112. double vert0[3], double vert1[3], double vert2[3],
  113. double *t, double *u, double *v)
  114. {
  115. double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
  116. double det,inv_det;
  117. /* find vectors for two edges sharing vert0 */
  118. IGL_RAY_TRI_SUB(edge1, vert1, vert0);
  119. IGL_RAY_TRI_SUB(edge2, vert2, vert0);
  120. /* begin calculating determinant - also used to calculate U parameter */
  121. IGL_RAY_TRI_CROSS(pvec, dir, edge2);
  122. /* if determinant is near zero, ray lies in plane of triangle */
  123. det = IGL_RAY_TRI_DOT(edge1, pvec);
  124. /* calculate distance from vert0 to ray origin */
  125. IGL_RAY_TRI_SUB(tvec, orig, vert0);
  126. inv_det = 1.0 / det;
  127. if (det > IGL_RAY_TRI_EPSILON)
  128. {
  129. /* calculate U parameter and test bounds */
  130. *u = IGL_RAY_TRI_DOT(tvec, pvec);
  131. if (*u < 0.0 || *u > det)
  132. return 0;
  133. /* prepare to test V parameter */
  134. IGL_RAY_TRI_CROSS(qvec, tvec, edge1);
  135. /* calculate V parameter and test bounds */
  136. *v = IGL_RAY_TRI_DOT(dir, qvec);
  137. if (*v < 0.0 || *u + *v > det)
  138. return 0;
  139. }
  140. else if(det < -IGL_RAY_TRI_EPSILON)
  141. {
  142. /* calculate U parameter and test bounds */
  143. *u = IGL_RAY_TRI_DOT(tvec, pvec);
  144. if (*u > 0.0 || *u < det)
  145. return 0;
  146. /* prepare to test V parameter */
  147. IGL_RAY_TRI_CROSS(qvec, tvec, edge1);
  148. /* calculate V parameter and test bounds */
  149. *v = IGL_RAY_TRI_DOT(dir, qvec) ;
  150. if (*v > 0.0 || *u + *v < det)
  151. return 0;
  152. }
  153. else return 0; /* ray is parallell to the plane of the triangle */
  154. /* calculate t, ray intersects triangle */
  155. *t = IGL_RAY_TRI_DOT(edge2, qvec) * inv_det;
  156. (*u) *= inv_det;
  157. (*v) *= inv_det;
  158. return 1;
  159. }
  160. /* code rewritten to do tests on the sign of the determinant */
  161. /* the division is before the test of the sign of the det */
  162. /* and one IGL_RAY_TRI_CROSS has been moved out from the if-else if-else */
  163. inline int intersect_triangle3(double orig[3], double dir[3],
  164. double vert0[3], double vert1[3], double vert2[3],
  165. double *t, double *u, double *v)
  166. {
  167. double edge1[3], edge2[3], tvec[3], pvec[3], qvec[3];
  168. double det,inv_det;
  169. /* find vectors for two edges sharing vert0 */
  170. IGL_RAY_TRI_SUB(edge1, vert1, vert0);
  171. IGL_RAY_TRI_SUB(edge2, vert2, vert0);
  172. /* begin calculating determinant - also used to calculate U parameter */
  173. IGL_RAY_TRI_CROSS(pvec, dir, edge2);
  174. /* if determinant is near zero, ray lies in plane of triangle */
  175. det = IGL_RAY_TRI_DOT(edge1, pvec);
  176. /* calculate distance from vert0 to ray origin */
  177. IGL_RAY_TRI_SUB(tvec, orig, vert0);
  178. inv_det = 1.0 / det;
  179. IGL_RAY_TRI_CROSS(qvec, tvec, edge1);
  180. if (det > IGL_RAY_TRI_EPSILON)
  181. {
  182. *u = IGL_RAY_TRI_DOT(tvec, pvec);
  183. if (*u < 0.0 || *u > det)
  184. return 0;
  185. /* calculate V parameter and test bounds */
  186. *v = IGL_RAY_TRI_DOT(dir, qvec);
  187. if (*v < 0.0 || *u + *v > det)
  188. return 0;
  189. }
  190. else if(det < -IGL_RAY_TRI_EPSILON)
  191. {
  192. /* calculate U parameter and test bounds */
  193. *u = IGL_RAY_TRI_DOT(tvec, pvec);
  194. if (*u > 0.0 || *u < det)
  195. return 0;
  196. /* calculate V parameter and test bounds */
  197. *v = IGL_RAY_TRI_DOT(dir, qvec) ;
  198. if (*v > 0.0 || *u + *v < det)
  199. return 0;
  200. }
  201. else return 0; /* ray is parallell to the plane of the triangle */
  202. *t = IGL_RAY_TRI_DOT(edge2, qvec) * inv_det;
  203. (*u) *= inv_det;
  204. (*v) *= inv_det;
  205. return 1;
  206. }
  207. #endif