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

198 lines
5.8 KiB

  1. /*
  2. * Copyright (C) 2023 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 <engine/ai/navmesh.hpp>
  20. #include <engine/geom/closest-point.hpp>
  21. #include <engine/geom/coordinates.hpp>
  22. #include <engine/math/quaternion.hpp>
  23. #include <iterator>
  24. #include <engine/debug/log.hpp>
  25. namespace ai {
  26. navmesh_traversal traverse_navmesh(const geom::brep_mesh& mesh, geom::brep_face* face, geom::ray<float, 3> ray, float distance)
  27. {
  28. // Get vertex positions and face normals
  29. const auto& vertex_positions = mesh.vertices().attributes().at<math::fvec3>("position");
  30. const auto& face_normals = mesh.faces().attributes().at<math::fvec3>("normal");
  31. // Init traversal result
  32. navmesh_traversal traversal;
  33. traversal.edge = nullptr;
  34. geom::triangle_region region;
  35. auto target_point = ray.extrapolate(distance);
  36. math::fvec3 closest_point;
  37. math::fvec3 traversal_direction = ray.direction;
  38. geom::brep_edge* previous_closest_edge{};
  39. do
  40. {
  41. // Get vertex positions of face
  42. auto loop_it = face->loops().begin();
  43. const auto& a = vertex_positions[loop_it->vertex()->index()];
  44. const auto& b = vertex_positions[(++loop_it)->vertex()->index()];
  45. const auto& c = vertex_positions[(++loop_it)->vertex()->index()];
  46. // Find closest point on face to target point
  47. std::tie(closest_point, region) = geom::closest_point(a, b, c, target_point);
  48. // If point is on the face
  49. if (geom::is_face_region(region))
  50. {
  51. // Traversal complete
  52. break;
  53. }
  54. geom::brep_loop* closest_loop;
  55. // If point is on an edge
  56. if (geom::is_edge_region(region))
  57. {
  58. // Get index of the edge
  59. const auto edge_index = geom::edge_index(region);
  60. // Get pointer to the edge's loop
  61. auto loop_it = face->loops().begin();
  62. std::advance(loop_it, edge_index);
  63. closest_loop = *loop_it;
  64. // If edge is a boundary edge
  65. if (closest_loop->edge()->loops().size() == 1)
  66. {
  67. // Abort traversal
  68. traversal.edge = closest_loop->edge();
  69. break;
  70. }
  71. }
  72. else
  73. {
  74. // Point is on a vertex, get index of vertex on which point lies
  75. const auto vertex_index = geom::vertex_index(region);
  76. // Get pointer to loop originating at the vertex
  77. auto loop_it = face->loops().begin();
  78. std::advance(loop_it, vertex_index);
  79. geom::brep_loop* loop = *loop_it;
  80. // If previous loop edge is a boundary edge
  81. if (loop->previous()->edge()->loops().size() == 1)
  82. {
  83. // If current loop edge is also a boundary edge
  84. if (loop->edge()->loops().size() == 1)
  85. {
  86. // Abort traversal
  87. traversal.edge = loop->edge();
  88. break;
  89. }
  90. // Select current loop
  91. closest_loop = loop;
  92. }
  93. // If current loop edge is a boundary edge
  94. else if (loop->edge()->loops().size() == 1)
  95. {
  96. // Select previous loop
  97. closest_loop = loop->previous();
  98. }
  99. else
  100. // Neither loop edge is a boundary edge
  101. {
  102. // Calculate direction of current loop edge
  103. const auto current_direction = math::normalize
  104. (
  105. vertex_positions[loop->next()->vertex()->index()] -
  106. vertex_positions[loop->vertex()->index()]
  107. );
  108. // Calculate direction of previous loop edge
  109. const auto previous_direction = math::normalize
  110. (
  111. vertex_positions[loop->vertex()->index()] -
  112. vertex_positions[loop->previous()->vertex()->index()]
  113. );
  114. // Select loop with minimal angle between edge and traversal direction
  115. if (std::abs(math::dot(traversal_direction, current_direction)) <
  116. std::abs(math::dot(traversal_direction, previous_direction)))
  117. {
  118. closest_loop = loop;
  119. }
  120. else
  121. {
  122. closest_loop = loop->previous();
  123. }
  124. }
  125. }
  126. // Get edge of closest loop
  127. geom::brep_edge* closest_edge = closest_loop->edge();
  128. // If closest edge is previous closest edge
  129. if (closest_edge == previous_closest_edge)
  130. {
  131. // Abort traversal
  132. traversal.edge = closest_edge;
  133. break;
  134. }
  135. // Remember closest edge to prevent infinite loops
  136. previous_closest_edge = closest_edge;
  137. // Find a loop and face that shares the closest edge
  138. geom::brep_loop* symmetric_loop = closest_edge->loops().front();
  139. if (symmetric_loop == closest_loop)
  140. {
  141. symmetric_loop = closest_edge->loops().back();
  142. }
  143. geom::brep_face* symmetric_face = symmetric_loop->face();
  144. // Find quaternion representing rotation from normal of first face to normal of second face
  145. const auto& n0 = face_normals[face->index()];
  146. const auto& n1 = face_normals[symmetric_face->index()];
  147. const auto rotation = math::rotation(n0, n1);
  148. // Rotate target point
  149. target_point = rotation * (target_point - closest_point) + closest_point;
  150. // Rotate traversal direction
  151. traversal_direction = rotation * traversal_direction;
  152. // Move to next face
  153. face = symmetric_face;
  154. }
  155. while (true);
  156. traversal.face = face;
  157. traversal.target_point = target_point;
  158. traversal.closest_point = closest_point;
  159. auto loop_it = face->loops().begin();
  160. const auto& a = vertex_positions[loop_it->vertex()->index()];
  161. const auto& b = vertex_positions[(++loop_it)->vertex()->index()];
  162. const auto& c = vertex_positions[(++loop_it)->vertex()->index()];
  163. traversal.barycentric = geom::cartesian_to_barycentric(traversal.closest_point, a, b, c);
  164. return traversal;
  165. }
  166. } // namespace ai