raytri.c 7.9 KB

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