triRayCheck.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //-----------------------------------------------------------------------------
  23. // Ray to triangle intersection test code originally by Tomas Akenine-Möller
  24. // and Ben Trumbore.
  25. // http://www.cs.lth.se/home/Tomas_Akenine_Moller/code/
  26. // Ported to TGE by DAW, 2005-7-15
  27. //-----------------------------------------------------------------------------
  28. #include "util/triRayCheck.h"
  29. #include "math/mPlane.h"
  30. #define EPSILON 0.000001
  31. #define CROSS(dest,v1,v2) \
  32. dest[0]=v1[1]*v2[2]-v1[2]*v2[1]; \
  33. dest[1]=v1[2]*v2[0]-v1[0]*v2[2]; \
  34. dest[2]=v1[0]*v2[1]-v1[1]*v2[0];
  35. #define DOT(v1,v2) (v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2])
  36. #define SUB(dest,v1,v2) \
  37. dest[0]=v1[0]-v2[0]; \
  38. dest[1]=v1[1]-v2[1]; \
  39. dest[2]=v1[2]-v2[2];
  40. bool intersect_triangle(Point3F orig, Point3F dir,
  41. Point3F vert0, Point3F vert1, Point3F vert2,
  42. F32& t, F32& u, F32& v)
  43. {
  44. Point3F edge1, edge2, tvec, pvec, qvec;
  45. F32 det,inv_det;
  46. /* find vectors for two edges sharing vert0 */
  47. edge1.x = vert1.x - vert0.x;
  48. edge1.y = vert1.y - vert0.y;
  49. edge1.z = vert1.z - vert0.z;
  50. edge2.x = vert2.x - vert0.x;
  51. edge2.y = vert2.y - vert0.y;
  52. edge2.z = vert2.z - vert0.z;
  53. /* begin calculating determinant - also used to calculate U parameter */
  54. //CROSS(pvec, dir, edge2);
  55. mCross(dir, edge2, &pvec);
  56. /* if determinant is near zero, ray lies in plane of triangle */
  57. //det = DOT(edge1, pvec);
  58. det = mDot(edge1, pvec);
  59. #ifdef TEST_CULL /* define TEST_CULL if culling is desired */
  60. if (det < EPSILON)
  61. return 0;
  62. /* calculate distance from vert0 to ray origin */
  63. SUB(tvec, orig, vert0);
  64. /* calculate U parameter and test bounds */
  65. *u = DOT(tvec, pvec);
  66. if (*u < 0.0 || *u > det)
  67. return 0;
  68. /* prepare to test V parameter */
  69. CROSS(qvec, tvec, edge1);
  70. /* calculate V parameter and test bounds */
  71. *v = DOT(dir, qvec);
  72. if (*v < 0.0 || *u + *v > det)
  73. return 0;
  74. /* calculate t, scale parameters, ray intersects triangle */
  75. *t = DOT(edge2, qvec);
  76. inv_det = 1.0 / det;
  77. *t *= inv_det;
  78. *u *= inv_det;
  79. *v *= inv_det;
  80. #else /* the non-culling branch */
  81. if (det > -EPSILON && det < EPSILON)
  82. return false;
  83. inv_det = 1.0 / det;
  84. /* calculate distance from vert0 to ray origin */
  85. //SUB(tvec, orig, vert0);
  86. tvec.x = orig.x - vert0.x;
  87. tvec.y = orig.y - vert0.y;
  88. tvec.z = orig.z - vert0.z;
  89. /* calculate U parameter and test bounds */
  90. // *u = DOT(tvec, pvec) * inv_det;
  91. u = mDot(tvec, pvec) * inv_det;
  92. if (u < 0.0 || u > 1.0)
  93. return false;
  94. /* prepare to test V parameter */
  95. //CROSS(qvec, tvec, edge1);
  96. mCross(tvec, edge1, &qvec);
  97. /* calculate V parameter and test bounds */
  98. // *v = DOT(dir, qvec) * inv_det;
  99. v = mDot(dir, qvec) * inv_det;
  100. if (v < 0.0 || u + v > 1.0)
  101. return false;
  102. /* calculate t, ray intersects triangle */
  103. // *t = DOT(edge2, qvec) * inv_det;
  104. t = mDot(edge2, qvec) * inv_det;
  105. #endif
  106. return true;
  107. }
  108. //*** Taken from TSE, and based on the above
  109. bool castRayTriangle(const Point3F& orig, const Point3F& dir,
  110. const Point3F& vert0, const Point3F& vert1, const Point3F& vert2,
  111. F32 &t, Point2F &bary)
  112. {
  113. Point3F tvec, qvec;
  114. // Find vectors for two edges sharing vert0
  115. const Point3F edge1 = vert1 - vert0;
  116. const Point3F edge2 = vert2 - vert0;
  117. // Begin calculating determinant - also used to calculate U parameter.
  118. const Point3F pvec = mCross(dir, edge2);
  119. // If determinant is near zero, ray lies in plane of triangle.
  120. const F32 det = mDot(edge1, pvec);
  121. if (det > 0.00001)
  122. {
  123. // calculate distance from vert0 to ray origin
  124. tvec = orig - vert0;
  125. // calculate U parameter and test bounds
  126. bary.x = mDot(tvec, pvec); // bary.x is really bary.u...
  127. if (bary.x < 0.0 || bary.x > det)
  128. return false;
  129. // prepare to test V parameter
  130. qvec = mCross(tvec, edge1);
  131. // calculate V parameter and test bounds
  132. bary.y = mDot(dir, qvec); // bary.y is really bary.v
  133. if (bary.y < 0.0 || (bary.x + bary.y) > det)
  134. return false;
  135. }
  136. else if(det < -0.00001)
  137. {
  138. // calculate distance from vert0 to ray origin
  139. tvec = orig - vert0;
  140. // calculate U parameter and test bounds
  141. bary.x = mDot(tvec, pvec);
  142. if (bary.x > 0.0 || bary.x < det)
  143. return false;
  144. // prepare to test V parameter
  145. qvec = mCross(tvec, edge1);
  146. // calculate V parameter and test bounds
  147. bary.y = mDot(dir, qvec);
  148. if (bary.y > 0.0 || (bary.x + bary.y) < det)
  149. return false;
  150. }
  151. else
  152. return false; // ray is parallel to the plane of the triangle.
  153. const F32 inv_det = 1.0 / det;
  154. // calculate t, ray intersects triangle
  155. t = mDot(edge2, qvec) * inv_det;
  156. bary *= inv_det;
  157. //AssertFatal((t >= 0.f && t <=1.f), "AtlasGeomTracer::castRayTriangle - invalid t!");
  158. // Hack, check the math here!
  159. return (t >= 0.f && t <=1.f);
  160. }
  161. bool castRayTriangle(const Point3D &orig, const Point3D &dir,
  162. const Point3D &vert0, const Point3D &vert1, const Point3D &vert2)
  163. {
  164. F64 t;
  165. Point2D bary;
  166. Point3D tvec, qvec;
  167. // Find vectors for two edges sharing vert0
  168. const Point3D edge1 = vert1 - vert0;
  169. const Point3D edge2 = vert2 - vert0;
  170. // Begin calculating determinant - also used to calculate U parameter.
  171. Point3D pvec;
  172. mCross(dir, edge2, &pvec);
  173. // If determinant is near zero, ray lies in plane of triangle.
  174. const F64 det = mDot(edge1, pvec);
  175. if (det > 0.00001)
  176. {
  177. // calculate distance from vert0 to ray origin
  178. tvec = orig - vert0;
  179. // calculate U parameter and test bounds
  180. bary.x = mDot(tvec, pvec); // bary.x is really bary.u...
  181. if (bary.x < 0.0 || bary.x > det)
  182. return false;
  183. // prepare to test V parameter
  184. mCross(tvec, edge1, &qvec);
  185. // calculate V parameter and test bounds
  186. bary.y = mDot(dir, qvec); // bary.y is really bary.v
  187. if (bary.y < 0.0 || (bary.x + bary.y) > det)
  188. return false;
  189. }
  190. else if(det < -0.00001)
  191. {
  192. // calculate distance from vert0 to ray origin
  193. tvec = orig - vert0;
  194. // calculate U parameter and test bounds
  195. bary.x = mDot(tvec, pvec);
  196. if (bary.x > 0.0 || bary.x < det)
  197. return false;
  198. // prepare to test V parameter
  199. mCross(tvec, edge1, &qvec);
  200. // calculate V parameter and test bounds
  201. bary.y = mDot(dir, qvec);
  202. if (bary.y > 0.0 || (bary.x + bary.y) < det)
  203. return false;
  204. }
  205. else
  206. return false; // ray is parallel to the plane of the triangle.
  207. const F32 inv_det = 1.0 / det;
  208. // calculate t, ray intersects triangle
  209. t = mDot(edge2, qvec) * inv_det;
  210. bary *= inv_det;
  211. //AssertFatal((t >= 0.f && t <=1.f), "AtlasGeomTracer::castRayTriangle - invalid t!");
  212. // Hack, check the math here!
  213. return (t >= 0.f && t <=1.f);
  214. }