💿🐜 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.

184 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. std::tuple<bool, float> ray_plane_intersection(const ray<float>& ray, const plane<float>& plane)
  22. {
  23. float denom = math::dot(ray.direction, plane.normal);
  24. if (denom != 0.0f)
  25. {
  26. float t = -(math::dot(ray.origin, plane.normal) + plane.distance) / denom;
  27. if (t >= 0.0f)
  28. {
  29. return std::make_tuple(true, t);
  30. }
  31. }
  32. return std::make_tuple(false, std::numeric_limits<float>::infinity());
  33. }
  34. std::tuple<bool, float, float, float> ray_triangle_intersection(const ray<float>& ray, const float3& a, const float3& b, const float3& c)
  35. {
  36. // Find edges
  37. float3 edge10 = b - a;
  38. float3 edge20 = c - a;
  39. // Calculate determinant
  40. float3 pv = math::cross(ray.direction, edge20);
  41. float det = math::dot(edge10, pv);
  42. if (!det)
  43. {
  44. return std::make_tuple(false, std::numeric_limits<float>::infinity(), 0.0f, 0.0f);
  45. }
  46. float inverse_det = 1.0f / det;
  47. // Calculate u
  48. float3 tv = ray.origin - a;
  49. float u = math::dot(tv, pv) * inverse_det;
  50. if (u < 0.0f || u > 1.0f)
  51. {
  52. return std::make_tuple(false, std::numeric_limits<float>::infinity(), 0.0f, 0.0f);
  53. }
  54. // Calculate v
  55. float3 qv = math::cross(tv, edge10);
  56. float v = math::dot(ray.direction, qv) * inverse_det;
  57. if (v < 0.0f || u + v > 1.0f)
  58. {
  59. return std::make_tuple(false, std::numeric_limits<float>::infinity(), 0.0f, 0.0f);
  60. }
  61. // Calculate t
  62. float t = math::dot(edge20, qv) * inverse_det;
  63. if (t > 0.0f)
  64. {
  65. return std::make_tuple(true, t, u, v);
  66. }
  67. return std::make_tuple(false, std::numeric_limits<float>::infinity(), 0.0f, 0.0f);
  68. }
  69. std::tuple<bool, float, float> ray_aabb_intersection(const ray<float>& ray, const aabb<float>& aabb)
  70. {
  71. float t0 = -std::numeric_limits<float>::infinity();
  72. float t1 = std::numeric_limits<float>::infinity();
  73. for (std::size_t i = 0; i < 3; ++i)
  74. {
  75. if (ray.direction[i] == 0.0f)
  76. {
  77. if (ray.origin[i] < aabb.min_point[i] || ray.origin[i] > aabb.max_point[i])
  78. {
  79. return std::make_tuple(false, std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());
  80. }
  81. }
  82. else
  83. {
  84. float tmin = (aabb.min_point[i] - ray.origin[i]) / ray.direction[i];
  85. float tmax = (aabb.max_point[i] - ray.origin[i]) / ray.direction[i];
  86. t0 = std::max(t0, std::min(tmin, tmax));
  87. t1 = std::min(t1, std::max(tmin, tmax));
  88. }
  89. }
  90. if (t0 > t1 || t1 < 0.0f)
  91. {
  92. return std::make_tuple(false, std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity());
  93. }
  94. return std::make_tuple(true, t0, t1);
  95. }
  96. std::tuple<bool, float, float, std::size_t, std::size_t> ray_mesh_intersection(const ray<float>& ray, const mesh& mesh)
  97. {
  98. const std::vector<mesh::face*>& triangles = mesh.get_faces();
  99. bool intersection = false;
  100. float t0 = std::numeric_limits<float>::infinity();
  101. float t1 = -std::numeric_limits<float>::infinity();
  102. std::size_t index0 = triangles.size();
  103. std::size_t index1 = triangles.size();
  104. for (std::size_t i = 0; i < triangles.size(); ++i)
  105. {
  106. const mesh::face* triangle = triangles[i];
  107. const float3& a = reinterpret_cast<const float3&>(triangle->edge->vertex->position);
  108. const float3& b = reinterpret_cast<const float3&>(triangle->edge->next->vertex->position);
  109. const float3& c = reinterpret_cast<const float3&>(triangle->edge->previous->vertex->position);
  110. auto result = ray_triangle_intersection(ray, a, b, c);
  111. if (std::get<0>(result))
  112. {
  113. intersection = true;
  114. float t = std::get<1>(result);
  115. if (t < t0)
  116. {
  117. t0 = t;
  118. index0 = i;
  119. }
  120. if (t > t1)
  121. {
  122. t1 = t;
  123. index1 = i;
  124. }
  125. }
  126. }
  127. return std::make_tuple(intersection, t0, t1, index0, index1);
  128. }
  129. bool aabb_aabb_intersection(const aabb<float>& a, const aabb<float>& b)
  130. {
  131. if (a.max_point.x < b.min_point.x || a.min_point.x > b.max_point.x)
  132. return false;
  133. if (a.max_point.y < b.min_point.y || a.min_point.y > b.max_point.y)
  134. return false;
  135. if (a.max_point.z < b.min_point.z || a.min_point.z > b.max_point.z)
  136. return false;
  137. return true;
  138. }
  139. bool aabb_sphere_intersection(const aabb<float>& aabb, const float3& center, float radius)
  140. {
  141. float distance_squared = 0.0f;
  142. for (int i = 0; i < 3; ++i)
  143. {
  144. float v = center[i];
  145. if (v < aabb.min_point[i])
  146. distance_squared += (aabb.min_point[i] - v) * (aabb.min_point[i] - v);
  147. if (v > aabb.max_point[i])
  148. distance_squared += (v - aabb.max_point[i]) * (v - aabb.max_point[i]);
  149. }
  150. return (distance_squared <= (radius * radius));
  151. }