intersection.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "intersection.h"
  24. #include "aabb.h"
  25. #include "plane.h"
  26. #include "sphere.h"
  27. #include "vector3.h"
  28. namespace crown
  29. {
  30. namespace math
  31. {
  32. /// Returns the distance along ray (from, dir) to intersection point with plane @a p.
  33. /// -1.0f if no collision.
  34. float ray_plane_intersection(const Vector3& from, const Vector3& dir, const Plane& p)
  35. {
  36. float nd = vector3::dot(dir, p.n);
  37. float orpn = vector3::dot(from, p.n);
  38. float dist = -1.0f;
  39. if (nd < 0.0f)
  40. dist = (-p.d - orpn) / nd;
  41. return dist > 0.0f ? dist : -1.0f;
  42. }
  43. /// Returns the distance along ray (from, dir) to intersection point with sphere @a s.
  44. /// -1.0f if no collision.
  45. float ray_sphere_intersection(const Vector3& from, const Vector3& dir, const Sphere& s)
  46. {
  47. Vector3 v = s.c - from;
  48. float b = vector3::dot(v, dir);
  49. float det = (s.r * s.r) - vector3::dot(v, v) + (b * b);
  50. if (det < 0.0 || b < s.r)
  51. {
  52. return -1.0f;
  53. }
  54. return b - sqrt(det);
  55. }
  56. // http://www.opengl-tutorial.org/miscellaneous/clicking-on-objects/picking-with-custom-ray-obb-function/
  57. float ray_oobb_intersection(const Vector3& from, const Vector3& dir, const OBB& obb)
  58. {
  59. using namespace vector3;
  60. float tmin = 0.0f;
  61. float tmax = 100000.0f;
  62. Vector3 obb_pos(obb.tm.t.x, obb.tm.t.y, obb.tm.t.z);
  63. Vector3 delta = obb_pos - from;
  64. {
  65. const Vector3 xaxis(obb.tm.x.x, obb.tm.x.y, obb.tm.x.z);
  66. float e = dot(xaxis, delta);
  67. float f = dot(dir, xaxis);
  68. if (fabs(f) > 0.001f)
  69. {
  70. float t1 = (e+obb.aabb.min.x)/f;
  71. float t2 = (e+obb.aabb.max.x)/f;
  72. if (t1>t2){
  73. float w=t1;t1=t2;t2=w;
  74. }
  75. if (t2 < tmax)
  76. tmax = t2;
  77. if (t1 > tmin)
  78. tmin = t1;
  79. if (tmax < tmin)
  80. return -1.0f;
  81. }
  82. else
  83. {
  84. if(-e+obb.aabb.min.x > 0.0f || -e+obb.aabb.max.x < 0.0f)
  85. return -1.0f;
  86. }
  87. }
  88. {
  89. const Vector3 yaxis(obb.tm.y.x, obb.tm.y.y, obb.tm.y.z);
  90. float e = dot(yaxis, delta);
  91. float f = dot(dir, yaxis);
  92. if (fabs(f) > 0.001f){
  93. float t1 = (e+obb.aabb.min.y)/f;
  94. float t2 = (e+obb.aabb.max.y)/f;
  95. if (t1>t2){float w=t1;t1=t2;t2=w;}
  96. if (t2 < tmax)
  97. tmax = t2;
  98. if (t1 > tmin)
  99. tmin = t1;
  100. if (tmin > tmax)
  101. return -1.0f;
  102. }
  103. else
  104. {
  105. if(-e+obb.aabb.min.y > 0.0f || -e+obb.aabb.max.y < 0.0f)
  106. return -1.0f;
  107. }
  108. }
  109. {
  110. const Vector3 zaxis(obb.tm.z.x, obb.tm.z.y, obb.tm.z.z);
  111. float e = dot(zaxis, delta);
  112. float f = dot(dir, zaxis);
  113. if (fabs(f) > 0.001f){
  114. float t1 = (e+obb.aabb.min.z)/f;
  115. float t2 = (e+obb.aabb.max.z)/f;
  116. if (t1>t2){float w=t1;t1=t2;t2=w;}
  117. if (t2 < tmax)
  118. tmax = t2;
  119. if (t1 > tmin)
  120. tmin = t1;
  121. if (tmin > tmax)
  122. return -1.0f;
  123. }
  124. else
  125. {
  126. if(-e+obb.aabb.min.z > 0.0f || -e+obb.aabb.max.z < 0.0f)
  127. return -1.0f;
  128. }
  129. }
  130. return tmin;
  131. }
  132. bool plane_3_intersection(const Plane& p1, const Plane& p2, const Plane& p3, Vector3& ip)
  133. {
  134. const Vector3& n1 = p1.n;
  135. const Vector3& n2 = p2.n;
  136. const Vector3& n3 = p3.n;
  137. float den = -vector3::dot(vector3::cross(n1, n2), n3);
  138. if (equals(den, (float)0.0))
  139. {
  140. return false;
  141. }
  142. Vector3 res = p1.d * vector3::cross(n2, n3) + p2.d * vector3::cross(n3, n1) + p3.d * vector3::cross(n1, n2);
  143. ip = res / den;
  144. return true;
  145. }
  146. bool frustum_sphere_intersection(const Frustum& f, const Sphere& s)
  147. {
  148. if (plane::distance_to_point(f.left, s.c) < -s.r ||
  149. plane::distance_to_point(f.right, s.c) < -s.r)
  150. {
  151. return false;
  152. }
  153. if (plane::distance_to_point(f.bottom, s.c) < -s.r ||
  154. plane::distance_to_point(f.top, s.c) < -s.r)
  155. {
  156. return false;
  157. }
  158. if (plane::distance_to_point(f.near, s.c) < -s.r ||
  159. plane::distance_to_point(f.far, s.c) < -s.r)
  160. {
  161. return false;
  162. }
  163. return true;
  164. }
  165. bool frustum_box_intersection(const Frustum& f, const AABB& b)
  166. {
  167. uint8_t out;
  168. out = 0;
  169. if (plane::distance_to_point(f.left, aabb::vertex(b, 0)) < 0.0) out++;
  170. if (plane::distance_to_point(f.left, aabb::vertex(b, 1)) < 0.0) out++;
  171. if (plane::distance_to_point(f.left, aabb::vertex(b, 2)) < 0.0) out++;
  172. if (plane::distance_to_point(f.left, aabb::vertex(b, 3)) < 0.0) out++;
  173. if (plane::distance_to_point(f.left, aabb::vertex(b, 4)) < 0.0) out++;
  174. if (plane::distance_to_point(f.left, aabb::vertex(b, 5)) < 0.0) out++;
  175. if (plane::distance_to_point(f.left, aabb::vertex(b, 6)) < 0.0) out++;
  176. if (plane::distance_to_point(f.left, aabb::vertex(b, 7)) < 0.0) out++;
  177. // If all vertices are outside one face, then the box doesn't intersect the frustum
  178. if (out == 8) return false;
  179. out = 0;
  180. if (plane::distance_to_point(f.right, aabb::vertex(b, 0)) < 0.0) out++;
  181. if (plane::distance_to_point(f.right, aabb::vertex(b, 1)) < 0.0) out++;
  182. if (plane::distance_to_point(f.right, aabb::vertex(b, 2)) < 0.0) out++;
  183. if (plane::distance_to_point(f.right, aabb::vertex(b, 3)) < 0.0) out++;
  184. if (plane::distance_to_point(f.right, aabb::vertex(b, 4)) < 0.0) out++;
  185. if (plane::distance_to_point(f.right, aabb::vertex(b, 5)) < 0.0) out++;
  186. if (plane::distance_to_point(f.right, aabb::vertex(b, 6)) < 0.0) out++;
  187. if (plane::distance_to_point(f.right, aabb::vertex(b, 7)) < 0.0) out++;
  188. if (out == 8) return false;
  189. out = 0;
  190. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 0)) < 0.0) out++;
  191. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 1)) < 0.0) out++;
  192. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 2)) < 0.0) out++;
  193. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 3)) < 0.0) out++;
  194. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 4)) < 0.0) out++;
  195. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 5)) < 0.0) out++;
  196. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 6)) < 0.0) out++;
  197. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 7)) < 0.0) out++;
  198. if (out == 8) return false;
  199. out = 0;
  200. if (plane::distance_to_point(f.top, aabb::vertex(b, 0)) < 0.0) out++;
  201. if (plane::distance_to_point(f.top, aabb::vertex(b, 1)) < 0.0) out++;
  202. if (plane::distance_to_point(f.top, aabb::vertex(b, 2)) < 0.0) out++;
  203. if (plane::distance_to_point(f.top, aabb::vertex(b, 3)) < 0.0) out++;
  204. if (plane::distance_to_point(f.top, aabb::vertex(b, 4)) < 0.0) out++;
  205. if (plane::distance_to_point(f.top, aabb::vertex(b, 5)) < 0.0) out++;
  206. if (plane::distance_to_point(f.top, aabb::vertex(b, 6)) < 0.0) out++;
  207. if (plane::distance_to_point(f.top, aabb::vertex(b, 7)) < 0.0) out++;
  208. if (out == 8) return false;
  209. out = 0;
  210. if (plane::distance_to_point(f.near, aabb::vertex(b, 0)) < 0.0) out++;
  211. if (plane::distance_to_point(f.near, aabb::vertex(b, 1)) < 0.0) out++;
  212. if (plane::distance_to_point(f.near, aabb::vertex(b, 2)) < 0.0) out++;
  213. if (plane::distance_to_point(f.near, aabb::vertex(b, 3)) < 0.0) out++;
  214. if (plane::distance_to_point(f.near, aabb::vertex(b, 4)) < 0.0) out++;
  215. if (plane::distance_to_point(f.near, aabb::vertex(b, 5)) < 0.0) out++;
  216. if (plane::distance_to_point(f.near, aabb::vertex(b, 6)) < 0.0) out++;
  217. if (plane::distance_to_point(f.near, aabb::vertex(b, 7)) < 0.0) out++;
  218. if (out == 8) return false;
  219. out = 0;
  220. if (plane::distance_to_point(f.far, aabb::vertex(b, 0)) < 0.0) out++;
  221. if (plane::distance_to_point(f.far, aabb::vertex(b, 1)) < 0.0) out++;
  222. if (plane::distance_to_point(f.far, aabb::vertex(b, 2)) < 0.0) out++;
  223. if (plane::distance_to_point(f.far, aabb::vertex(b, 3)) < 0.0) out++;
  224. if (plane::distance_to_point(f.far, aabb::vertex(b, 4)) < 0.0) out++;
  225. if (plane::distance_to_point(f.far, aabb::vertex(b, 5)) < 0.0) out++;
  226. if (plane::distance_to_point(f.far, aabb::vertex(b, 6)) < 0.0) out++;
  227. if (plane::distance_to_point(f.far, aabb::vertex(b, 7)) < 0.0) out++;
  228. if (out == 8) return false;
  229. // If we are here, it is because either the box intersects or it is contained in the frustum
  230. return true;
  231. }
  232. } // namespace math
  233. } // namespace crown