💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.1 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "intersection.hpp"
  20. #include <limits>
  21. namespace geom {
  22. std::tuple<bool, float> ray_plane_intersection(const ray<float>& ray, const plane<float>& plane)
  23. {
  24. float denom = math::dot(ray.direction, plane.normal);
  25. if (denom != 0.0f)
  26. {
  27. float t = -(math::dot(ray.origin, plane.normal) + plane.distance) / denom;
  28. if (t >= 0.0f)
  29. {
  30. return std::make_tuple(true, t);
  31. }
  32. }
  33. return std::make_tuple(false, std::numeric_limits<float>::infinity());
  34. }
  35. std::tuple<bool, float, float, float> ray_triangle_intersection(const ray<float>& ray, const float3& a, const float3& b, const float3& c)
  36. {
  37. // Find edges
  38. float3 edge10 = b - a;
  39. float3 edge20 = c - a;
  40. // Calculate determinant
  41. float3 pv = math::cross(ray.direction, edge20);
  42. float det = math::dot(edge10, pv);
  43. if (!det)
  44. {
  45. return std::make_tuple(false, std::numeric_limits<float>::infinity(), 0.0f, 0.0f);
  46. }
  47. float inverse_det = 1.0f / det;
  48. // Calculate u
  49. float3 tv = ray.origin - a;
  50. float u = math::dot(tv, pv) * inverse_det;
  51. if (u < 0.0f || u > 1.0f)
  52. {
  53. return std::make_tuple(false, std::numeric_limits<float>::infinity(), 0.0f, 0.0f);
  54. }
  55. // Calculate v
  56. float3 qv = math::cross(tv, edge10);
  57. float v = math::dot(ray.direction, qv) * inverse_det;
  58. if (v < 0.0f || u + v > 1.0f)
  59. {
  60. return std::make_tuple(false, std::numeric_limits<float>::infinity(), 0.0f, 0.0f);
  61. }
  62. // Calculate t
  63. float t = math::dot(edge20, qv) * inverse_det;
  64. if (t > 0.0f)
  65. {
  66. return std::make_tuple(true, t, u, v);
  67. }
  68. return std::make_tuple(false, std::numeric_limits<float>::infinity(), 0.0f, 0.0f);
  69. }
  70. std::tuple<bool, float, float> ray_aabb_intersection(const ray<float>& ray, const aabb<float>& aabb)
  71. {
  72. float t0 = -std::numeric_limits<float>::infinity();
  73. float t1 = std::numeric_limits<float>::infinity();
  74. for (std::size_t i = 0; i < 3; ++i)
  75. {
  76. if (ray.direction[i] == 0.0f)
  77. {
  78. if (ray.origin[i] < aabb.min_point[i] || ray.origin[i] > aabb.max_point[i])
  79. {
  80. return std::make_tuple(false, std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());
  81. }
  82. }
  83. else
  84. {
  85. float tmin = (aabb.min_point[i] - ray.origin[i]) / ray.direction[i];
  86. float tmax = (aabb.max_point[i] - ray.origin[i]) / ray.direction[i];
  87. t0 = std::max(t0, std::min(tmin, tmax));
  88. t1 = std::min(t1, std::max(tmin, tmax));
  89. }
  90. }
  91. if (t0 > t1 || t1 < 0.0f)
  92. {
  93. return std::make_tuple(false, std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());
  94. }
  95. return std::make_tuple(true, t0, t1);
  96. }
  97. std::tuple<bool, float, float, std::size_t, std::size_t> ray_mesh_intersection(const ray<float>& ray, const mesh& mesh)
  98. {
  99. const std::vector<mesh::face*>& triangles = mesh.get_faces();
  100. bool intersection = false;
  101. float t0 = std::numeric_limits<float>::infinity();
  102. float t1 = -std::numeric_limits<float>::infinity();
  103. std::size_t index0 = triangles.size();
  104. std::size_t index1 = triangles.size();
  105. for (std::size_t i = 0; i < triangles.size(); ++i)
  106. {
  107. const mesh::face* triangle = triangles[i];
  108. const float3& a = reinterpret_cast<const float3&>(triangle->edge->vertex->position);
  109. const float3& b = reinterpret_cast<const float3&>(triangle->edge->next->vertex->position);
  110. const float3& c = reinterpret_cast<const float3&>(triangle->edge->previous->vertex->position);
  111. auto result = ray_triangle_intersection(ray, a, b, c);
  112. if (std::get<0>(result))
  113. {
  114. intersection = true;
  115. float t = std::get<1>(result);
  116. if (t < t0)
  117. {
  118. t0 = t;
  119. index0 = i;
  120. }
  121. if (t > t1)
  122. {
  123. t1 = t;
  124. index1 = i;
  125. }
  126. }
  127. }
  128. return std::make_tuple(intersection, t0, t1, index0, index1);
  129. }
  130. bool aabb_aabb_intersection(const aabb<float>& a, const aabb<float>& b)
  131. {
  132. if (a.max_point.x < b.min_point.x || a.min_point.x > b.max_point.x)
  133. return false;
  134. if (a.max_point.y < b.min_point.y || a.min_point.y > b.max_point.y)
  135. return false;
  136. if (a.max_point.z < b.min_point.z || a.min_point.z > b.max_point.z)
  137. return false;
  138. return true;
  139. }
  140. bool aabb_sphere_intersection(const aabb<float>& aabb, const float3& center, float radius)
  141. {
  142. float distance_squared = 0.0f;
  143. for (int i = 0; i < 3; ++i)
  144. {
  145. float v = center[i];
  146. if (v < aabb.min_point[i])
  147. distance_squared += (aabb.min_point[i] - v) * (aabb.min_point[i] - v);
  148. if (v > aabb.max_point[i])
  149. distance_squared += (v - aabb.max_point[i]) * (v - aabb.max_point[i]);
  150. }
  151. return (distance_squared <= (radius * radius));
  152. }
  153. } // namespace geom