intersection.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "aabb.h"
  6. #include "intersection.h"
  7. #include "plane3.h"
  8. #include "sphere.h"
  9. #include "vector3.h"
  10. namespace crown
  11. {
  12. f32 ray_plane_intersection(const Vector3& from, const Vector3& dir, const Plane3& p)
  13. {
  14. const f32 num = dot(from, p.n);
  15. const f32 den = dot(dir, p.n);
  16. if (fequal(den, 0.0f))
  17. return -1.0f;
  18. return (-p.d - num) / den;
  19. }
  20. f32 ray_disc_intersection(const Vector3& from, const Vector3& dir, const Vector3& center, f32 radius, const Vector3& normal)
  21. {
  22. const Plane3 p = plane3::from_point_and_normal(center, normal);
  23. const f32 t = ray_plane_intersection(from, dir, p);
  24. if (t == -1.0f)
  25. return -1.0f;
  26. const Vector3 intersection_point = from + dir * t;
  27. if (distance_squared(intersection_point, center) < radius*radius)
  28. return t;
  29. return -1.0f;
  30. }
  31. f32 ray_sphere_intersection(const Vector3& from, const Vector3& dir, const Sphere& s)
  32. {
  33. const Vector3 v = s.c - from;
  34. const f32 b = dot(v, dir);
  35. const f32 rr = s.r * s.r;
  36. const f32 bb = b * b;
  37. const f32 det = rr - dot(v, v) + bb;
  38. if (det < 0.0f || b < s.r)
  39. return -1.0f;
  40. return b - sqrtf(det);
  41. }
  42. // http://www.opengl-tutorial.org/miscellaneous/clicking-on-objects/picking-with-custom-ray-obb-function/
  43. f32 ray_obb_intersection(const Vector3& from, const Vector3& dir, const Matrix4x4& tm, const Vector3& half_extents)
  44. {
  45. f32 tmin = 0.0f;
  46. f32 tmax = 999999999.9f;
  47. const Vector3 obb_pos = vector3(tm.t.x, tm.t.y, tm.t.z);
  48. const Vector3 delta = obb_pos - from;
  49. {
  50. const Vector3 xaxis = vector3(tm.x.x, tm.x.y, tm.x.z);
  51. const f32 e = dot(xaxis, delta);
  52. const f32 f = dot(dir, xaxis);
  53. if (fabs(f) > 0.001f)
  54. {
  55. f32 t1 = (e-half_extents.x)/f;
  56. f32 t2 = (e+half_extents.x)/f;
  57. if (t1 > t2) { f32 w=t1;t1=t2;t2=w; }
  58. if (t2 < tmax)
  59. tmax = t2;
  60. if (t1 > tmin)
  61. tmin = t1;
  62. if (tmax < tmin)
  63. return -1.0f;
  64. }
  65. else
  66. {
  67. if (-e-half_extents.x > 0.0f || -e+half_extents.x < 0.0f)
  68. return -1.0f;
  69. }
  70. }
  71. {
  72. const Vector3 yaxis = vector3(tm.y.x, tm.y.y, tm.y.z);
  73. const f32 e = dot(yaxis, delta);
  74. const f32 f = dot(dir, yaxis);
  75. if (fabs(f) > 0.001f)
  76. {
  77. f32 t1 = (e-half_extents.y)/f;
  78. f32 t2 = (e+half_extents.y)/f;
  79. if (t1 > t2) { f32 w=t1;t1=t2;t2=w; }
  80. if (t2 < tmax)
  81. tmax = t2;
  82. if (t1 > tmin)
  83. tmin = t1;
  84. if (tmin > tmax)
  85. return -1.0f;
  86. }
  87. else
  88. {
  89. if (-e-half_extents.y > 0.0f || -e+half_extents.y < 0.0f)
  90. return -1.0f;
  91. }
  92. }
  93. {
  94. const Vector3 zaxis = vector3(tm.z.x, tm.z.y, tm.z.z);
  95. const f32 e = dot(zaxis, delta);
  96. const f32 f = dot(dir, zaxis);
  97. if (fabs(f) > 0.001f)
  98. {
  99. f32 t1 = (e-half_extents.z)/f;
  100. f32 t2 = (e+half_extents.z)/f;
  101. if (t1 > t2) { f32 w=t1;t1=t2;t2=w; }
  102. if (t2 < tmax)
  103. tmax = t2;
  104. if (t1 > tmin)
  105. tmin = t1;
  106. if (tmin > tmax)
  107. return -1.0f;
  108. }
  109. else
  110. {
  111. if (-e-half_extents.z > 0.0f || -e+half_extents.z < 0.0f)
  112. return -1.0f;
  113. }
  114. }
  115. return tmin;
  116. }
  117. f32 ray_triangle_intersection(const Vector3& from, const Vector3& dir, const Vector3& v0, const Vector3& v1, const Vector3& v2)
  118. {
  119. const Vector3 verts[] = { v0, v1, v2 };
  120. const u16 inds[] = { 0, 1, 2 };
  121. return ray_mesh_intersection(from, dir, MATRIX4X4_IDENTITY, verts, sizeof(Vector3), inds, 3);
  122. }
  123. f32 ray_mesh_intersection(const Vector3& from, const Vector3& dir, const Matrix4x4& tm, const void* vertices, u32 stride, const u16* indices, u32 num)
  124. {
  125. bool hit = false;
  126. f32 tmin = 999999999.9f;
  127. for (u32 i = 0; i < num; i += 3)
  128. {
  129. const u32 i0 = indices[i + 0];
  130. const u32 i1 = indices[i + 1];
  131. const u32 i2 = indices[i + 2];
  132. const Vector3& v0 = *(const Vector3*)((const char*)vertices + i0*stride) * tm;
  133. const Vector3& v1 = *(const Vector3*)((const char*)vertices + i1*stride) * tm;
  134. const Vector3& v2 = *(const Vector3*)((const char*)vertices + i2*stride) * tm;
  135. // https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
  136. // Find vectors for two edges sharing v0
  137. const Vector3 e1 = v1 - v0;
  138. const Vector3 e2 = v2 - v0;
  139. // Begin calculating determinant - also used to calculate u parameter
  140. const Vector3 P = cross(dir, e2);
  141. // If determinant is near zero, ray lies in plane of triangle
  142. const f32 det = dot(e1, P);
  143. if (fequal(det, 0.0f))
  144. continue;
  145. const f32 inv_det = 1.0f / det;
  146. // Distance from v0 to ray origin
  147. const Vector3 T = from - v0;
  148. // u parameter and test bound
  149. const f32 u = dot(T, P) * inv_det;
  150. // The intersection lies outside of the triangle
  151. if (u < 0.0f || u > 1.0f)
  152. continue;
  153. // Prepare to test v parameter
  154. const Vector3 Q = cross(T, e1);
  155. // v parameter and test bound
  156. const f32 v = dot(dir, Q) * inv_det;
  157. // The intersection lies outside of the triangle
  158. if (v < 0.0f || u + v > 1.0f)
  159. continue;
  160. const f32 t = dot(e2, Q) * inv_det;
  161. // Ray intersection
  162. if (t > FLOAT_EPSILON)
  163. {
  164. hit = true;
  165. tmin = fmin(t, tmin);
  166. }
  167. }
  168. return hit ? tmin : -1.0f;
  169. }
  170. bool plane_3_intersection(const Plane3& a, const Plane3& b, const Plane3& c, Vector3& ip)
  171. {
  172. const Vector3 na = a.n;
  173. const Vector3 nb = b.n;
  174. const Vector3 nc = c.n;
  175. const f32 den = -dot(cross(na, nb), nc);
  176. if (fequal(den, 0.0f))
  177. return false;
  178. const f32 inv_den = 1.0f / den;
  179. const Vector3 nbnc = a.d * cross(nb, nc);
  180. const Vector3 ncna = b.d * cross(nc, na);
  181. const Vector3 nanb = c.d * cross(na, nb);
  182. ip = (nbnc + ncna + nanb) * inv_den;
  183. return true;
  184. }
  185. bool frustum_sphere_intersection(const Frustum& f, const Sphere& s)
  186. {
  187. if (plane3::distance_to_point(f.left, s.c) < -s.r ||
  188. plane3::distance_to_point(f.right, s.c) < -s.r)
  189. {
  190. return false;
  191. }
  192. if (plane3::distance_to_point(f.bottom, s.c) < -s.r ||
  193. plane3::distance_to_point(f.top, s.c) < -s.r)
  194. {
  195. return false;
  196. }
  197. if (plane3::distance_to_point(f.near, s.c) < -s.r ||
  198. plane3::distance_to_point(f.far, s.c) < -s.r)
  199. {
  200. return false;
  201. }
  202. return true;
  203. }
  204. bool frustum_box_intersection(const Frustum& f, const AABB& b)
  205. {
  206. const Vector3 v0 = aabb::vertex(b, 0);
  207. const Vector3 v1 = aabb::vertex(b, 1);
  208. const Vector3 v2 = aabb::vertex(b, 2);
  209. const Vector3 v3 = aabb::vertex(b, 3);
  210. const Vector3 v4 = aabb::vertex(b, 4);
  211. const Vector3 v5 = aabb::vertex(b, 5);
  212. const Vector3 v6 = aabb::vertex(b, 6);
  213. const Vector3 v7 = aabb::vertex(b, 7);
  214. u8 out = 0;
  215. out += (plane3::distance_to_point(f.left, v0) < 0.0f) ? 1 : 0;
  216. out += (plane3::distance_to_point(f.left, v1) < 0.0f) ? 1 : 0;
  217. out += (plane3::distance_to_point(f.left, v2) < 0.0f) ? 1 : 0;
  218. out += (plane3::distance_to_point(f.left, v3) < 0.0f) ? 1 : 0;
  219. out += (plane3::distance_to_point(f.left, v4) < 0.0f) ? 1 : 0;
  220. out += (plane3::distance_to_point(f.left, v5) < 0.0f) ? 1 : 0;
  221. out += (plane3::distance_to_point(f.left, v6) < 0.0f) ? 1 : 0;
  222. out += (plane3::distance_to_point(f.left, v7) < 0.0f) ? 1 : 0;
  223. if (out == 8) return false;
  224. out = 0;
  225. out += (plane3::distance_to_point(f.right, v0) < 0.0f) ? 1 : 0;
  226. out += (plane3::distance_to_point(f.right, v1) < 0.0f) ? 1 : 0;
  227. out += (plane3::distance_to_point(f.right, v2) < 0.0f) ? 1 : 0;
  228. out += (plane3::distance_to_point(f.right, v3) < 0.0f) ? 1 : 0;
  229. out += (plane3::distance_to_point(f.right, v4) < 0.0f) ? 1 : 0;
  230. out += (plane3::distance_to_point(f.right, v5) < 0.0f) ? 1 : 0;
  231. out += (plane3::distance_to_point(f.right, v6) < 0.0f) ? 1 : 0;
  232. out += (plane3::distance_to_point(f.right, v7) < 0.0f) ? 1 : 0;
  233. if (out == 8) return false;
  234. out = 0;
  235. out += (plane3::distance_to_point(f.bottom, v0) < 0.0f) ? 1 : 0;
  236. out += (plane3::distance_to_point(f.bottom, v1) < 0.0f) ? 1 : 0;
  237. out += (plane3::distance_to_point(f.bottom, v2) < 0.0f) ? 1 : 0;
  238. out += (plane3::distance_to_point(f.bottom, v3) < 0.0f) ? 1 : 0;
  239. out += (plane3::distance_to_point(f.bottom, v4) < 0.0f) ? 1 : 0;
  240. out += (plane3::distance_to_point(f.bottom, v5) < 0.0f) ? 1 : 0;
  241. out += (plane3::distance_to_point(f.bottom, v6) < 0.0f) ? 1 : 0;
  242. out += (plane3::distance_to_point(f.bottom, v7) < 0.0f) ? 1 : 0;
  243. if (out == 8) return false;
  244. out = 0;
  245. out += (plane3::distance_to_point(f.top, v0) < 0.0f) ? 1 : 0;
  246. out += (plane3::distance_to_point(f.top, v1) < 0.0f) ? 1 : 0;
  247. out += (plane3::distance_to_point(f.top, v2) < 0.0f) ? 1 : 0;
  248. out += (plane3::distance_to_point(f.top, v3) < 0.0f) ? 1 : 0;
  249. out += (plane3::distance_to_point(f.top, v4) < 0.0f) ? 1 : 0;
  250. out += (plane3::distance_to_point(f.top, v5) < 0.0f) ? 1 : 0;
  251. out += (plane3::distance_to_point(f.top, v6) < 0.0f) ? 1 : 0;
  252. out += (plane3::distance_to_point(f.top, v7) < 0.0f) ? 1 : 0;
  253. if (out == 8) return false;
  254. out = 0;
  255. out += (plane3::distance_to_point(f.near, v0) < 0.0f) ? 1 : 0;
  256. out += (plane3::distance_to_point(f.near, v1) < 0.0f) ? 1 : 0;
  257. out += (plane3::distance_to_point(f.near, v2) < 0.0f) ? 1 : 0;
  258. out += (plane3::distance_to_point(f.near, v3) < 0.0f) ? 1 : 0;
  259. out += (plane3::distance_to_point(f.near, v4) < 0.0f) ? 1 : 0;
  260. out += (plane3::distance_to_point(f.near, v5) < 0.0f) ? 1 : 0;
  261. out += (plane3::distance_to_point(f.near, v6) < 0.0f) ? 1 : 0;
  262. out += (plane3::distance_to_point(f.near, v7) < 0.0f) ? 1 : 0;
  263. if (out == 8) return false;
  264. out = 0;
  265. out += (plane3::distance_to_point(f.far, v0) < 0.0f) ? 1 : 0;
  266. out += (plane3::distance_to_point(f.far, v1) < 0.0f) ? 1 : 0;
  267. out += (plane3::distance_to_point(f.far, v2) < 0.0f) ? 1 : 0;
  268. out += (plane3::distance_to_point(f.far, v3) < 0.0f) ? 1 : 0;
  269. out += (plane3::distance_to_point(f.far, v4) < 0.0f) ? 1 : 0;
  270. out += (plane3::distance_to_point(f.far, v5) < 0.0f) ? 1 : 0;
  271. out += (plane3::distance_to_point(f.far, v6) < 0.0f) ? 1 : 0;
  272. out += (plane3::distance_to_point(f.far, v7) < 0.0f) ? 1 : 0;
  273. if (out == 8) return false;
  274. // If we are here, it is because either the box intersects or it is contained in the frustum
  275. return true;
  276. }
  277. } // namespace crown