intersection.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #pragma once
  24. #include "plane.h"
  25. #include "sphere.h"
  26. #include "frustum.h"
  27. #include "math_utils.h"
  28. #include "types.h"
  29. #include "aabb.h"
  30. namespace crown
  31. {
  32. /// Returns the distance along ray (from, dir) to intersection point with plane @a p.
  33. /// -1.0f if no collision.
  34. inline 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. inline float ray_sphere_intersection(const Vector3& from, const Vector3& dir, const Sphere& s)
  46. {
  47. Vector3 v = s.center() - from;
  48. float b = vector3::dot(v, dir);
  49. float det = (s.radius() * s.radius()) - vector3::dot(v, v) + (b * b);
  50. if (det < 0.0 || b < s.radius())
  51. {
  52. return -1.0f;
  53. }
  54. return b - math::sqrt(det);
  55. }
  56. //-----------------------------------------------------------------------------
  57. inline bool plane_3_intersection(const Plane& p1, const Plane& p2, const Plane& p3, Vector3& ip)
  58. {
  59. const Vector3& n1 = p1.n;
  60. const Vector3& n2 = p2.n;
  61. const Vector3& n3 = p3.n;
  62. float den = -vector3::dot(vector3::cross(n1, n2), n3);
  63. if (math::equals(den, (float)0.0))
  64. {
  65. return false;
  66. }
  67. Vector3 res = p1.d * vector3::cross(n2, n3) + p2.d * vector3::cross(n3, n1) + p3.d * vector3::cross(n1, n2);
  68. ip = res / den;
  69. return true;
  70. }
  71. //-----------------------------------------------------------------------------
  72. inline bool frustum_sphere_intersection(const Frustum& f, const Sphere& s)
  73. {
  74. if (plane::distance_to_point(f.left, s.center()) < -s.radius() ||
  75. plane::distance_to_point(f.right, s.center()) < -s.radius())
  76. {
  77. return false;
  78. }
  79. if (plane::distance_to_point(f.bottom, s.center()) < -s.radius() ||
  80. plane::distance_to_point(f.top, s.center()) < -s.radius())
  81. {
  82. return false;
  83. }
  84. if (plane::distance_to_point(f.near, s.center()) < -s.radius() ||
  85. plane::distance_to_point(f.far, s.center()) < -s.radius())
  86. {
  87. return false;
  88. }
  89. return true;
  90. }
  91. //-----------------------------------------------------------------------------
  92. inline bool frustum_box_intersection(const Frustum& f, const AABB& b)
  93. {
  94. uint8_t out;
  95. out = 0;
  96. if (plane::distance_to_point(f.left, aabb::vertex(b, 0)) < 0.0) out++;
  97. if (plane::distance_to_point(f.left, aabb::vertex(b, 1)) < 0.0) out++;
  98. if (plane::distance_to_point(f.left, aabb::vertex(b, 2)) < 0.0) out++;
  99. if (plane::distance_to_point(f.left, aabb::vertex(b, 3)) < 0.0) out++;
  100. if (plane::distance_to_point(f.left, aabb::vertex(b, 4)) < 0.0) out++;
  101. if (plane::distance_to_point(f.left, aabb::vertex(b, 5)) < 0.0) out++;
  102. if (plane::distance_to_point(f.left, aabb::vertex(b, 6)) < 0.0) out++;
  103. if (plane::distance_to_point(f.left, aabb::vertex(b, 7)) < 0.0) out++;
  104. // If all vertices are outside one face, then the box doesn't intersect the frustum
  105. if (out == 8) return false;
  106. out = 0;
  107. if (plane::distance_to_point(f.right, aabb::vertex(b, 0)) < 0.0) out++;
  108. if (plane::distance_to_point(f.right, aabb::vertex(b, 1)) < 0.0) out++;
  109. if (plane::distance_to_point(f.right, aabb::vertex(b, 2)) < 0.0) out++;
  110. if (plane::distance_to_point(f.right, aabb::vertex(b, 3)) < 0.0) out++;
  111. if (plane::distance_to_point(f.right, aabb::vertex(b, 4)) < 0.0) out++;
  112. if (plane::distance_to_point(f.right, aabb::vertex(b, 5)) < 0.0) out++;
  113. if (plane::distance_to_point(f.right, aabb::vertex(b, 6)) < 0.0) out++;
  114. if (plane::distance_to_point(f.right, aabb::vertex(b, 7)) < 0.0) out++;
  115. if (out == 8) return false;
  116. out = 0;
  117. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 0)) < 0.0) out++;
  118. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 1)) < 0.0) out++;
  119. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 2)) < 0.0) out++;
  120. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 3)) < 0.0) out++;
  121. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 4)) < 0.0) out++;
  122. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 5)) < 0.0) out++;
  123. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 6)) < 0.0) out++;
  124. if (plane::distance_to_point(f.bottom, aabb::vertex(b, 7)) < 0.0) out++;
  125. if (out == 8) return false;
  126. out = 0;
  127. if (plane::distance_to_point(f.top, aabb::vertex(b, 0)) < 0.0) out++;
  128. if (plane::distance_to_point(f.top, aabb::vertex(b, 1)) < 0.0) out++;
  129. if (plane::distance_to_point(f.top, aabb::vertex(b, 2)) < 0.0) out++;
  130. if (plane::distance_to_point(f.top, aabb::vertex(b, 3)) < 0.0) out++;
  131. if (plane::distance_to_point(f.top, aabb::vertex(b, 4)) < 0.0) out++;
  132. if (plane::distance_to_point(f.top, aabb::vertex(b, 5)) < 0.0) out++;
  133. if (plane::distance_to_point(f.top, aabb::vertex(b, 6)) < 0.0) out++;
  134. if (plane::distance_to_point(f.top, aabb::vertex(b, 7)) < 0.0) out++;
  135. if (out == 8) return false;
  136. out = 0;
  137. if (plane::distance_to_point(f.near, aabb::vertex(b, 0)) < 0.0) out++;
  138. if (plane::distance_to_point(f.near, aabb::vertex(b, 1)) < 0.0) out++;
  139. if (plane::distance_to_point(f.near, aabb::vertex(b, 2)) < 0.0) out++;
  140. if (plane::distance_to_point(f.near, aabb::vertex(b, 3)) < 0.0) out++;
  141. if (plane::distance_to_point(f.near, aabb::vertex(b, 4)) < 0.0) out++;
  142. if (plane::distance_to_point(f.near, aabb::vertex(b, 5)) < 0.0) out++;
  143. if (plane::distance_to_point(f.near, aabb::vertex(b, 6)) < 0.0) out++;
  144. if (plane::distance_to_point(f.near, aabb::vertex(b, 7)) < 0.0) out++;
  145. if (out == 8) return false;
  146. out = 0;
  147. if (plane::distance_to_point(f.far, aabb::vertex(b, 0)) < 0.0) out++;
  148. if (plane::distance_to_point(f.far, aabb::vertex(b, 1)) < 0.0) out++;
  149. if (plane::distance_to_point(f.far, aabb::vertex(b, 2)) < 0.0) out++;
  150. if (plane::distance_to_point(f.far, aabb::vertex(b, 3)) < 0.0) out++;
  151. if (plane::distance_to_point(f.far, aabb::vertex(b, 4)) < 0.0) out++;
  152. if (plane::distance_to_point(f.far, aabb::vertex(b, 5)) < 0.0) out++;
  153. if (plane::distance_to_point(f.far, aabb::vertex(b, 6)) < 0.0) out++;
  154. if (plane::distance_to_point(f.far, aabb::vertex(b, 7)) < 0.0) out++;
  155. if (out == 8) return false;
  156. // If we are here, it is because either the box intersects or it is contained in the frustum
  157. return true;
  158. }
  159. } // namespace crown