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

176 lines
5.8 KiB

  1. /*
  2. * Copyright (C) 2020 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 "geometry/mesh-accelerator.hpp"
  20. #include "geometry/mesh-functions.hpp"
  21. #include "geometry/morton.hpp"
  22. #include <bitset>
  23. mesh_accelerator::mesh_accelerator()
  24. {}
  25. void mesh_accelerator::build(const mesh& mesh)
  26. {
  27. // Clear octree and face map
  28. octree.clear();
  29. face_map.clear();
  30. // Calculate mesh dimensions
  31. aabb<float> bounds = calculate_bounds(mesh);
  32. float3 mesh_dimensions = bounds.max_point - bounds.min_point;
  33. center_offset = mesh_dimensions * 0.5f - (bounds.min_point + bounds.max_point) * 0.5f;
  34. // Calculate node dimensions at each octree depth
  35. for (auto i = 0; i <= octree32::max_depth; ++i)
  36. {
  37. node_dimensions[i] = mesh_dimensions * static_cast<float>((1.0f / std::pow(2, i)));
  38. }
  39. // Add faces to octree
  40. for (mesh::face* face: mesh.get_faces())
  41. {
  42. // Calculate face bounds
  43. float3 min_point = reinterpret_cast<const float3&>(face->edge->vertex->position);
  44. float3 max_point = min_point;
  45. mesh::edge* edge = face->edge;
  46. do
  47. {
  48. const auto& position = edge->vertex->position;
  49. for (int i = 0; i < 3; ++i)
  50. {
  51. min_point[i] = std::min<float>(min_point[i], position[i]);
  52. max_point[i] = std::max<float>(max_point[i], position[i]);
  53. }
  54. edge = edge->next;
  55. }
  56. while (edge != face->edge);
  57. // 1. Find max depth node of aabb min
  58. // 2. Find max depth node of aabb max
  59. // 3. Find common ancestor of the two nodes--that's the containing node.
  60. octree32::node_type min_node = find_node(min_point);
  61. octree32::node_type max_node = find_node(max_point);
  62. octree32::node_type containing_node = octree32::common_ancestor(min_node, max_node);
  63. // Insert containing node into octree
  64. octree.insert(containing_node);
  65. // Add face to face map
  66. face_map[containing_node].push_back(face);
  67. }
  68. }
  69. std::optional<mesh_accelerator::ray_query_result> mesh_accelerator::query_nearest(const ray<float>& ray) const
  70. {
  71. ray_query_result result;
  72. result.t = std::numeric_limits<float>::infinity();
  73. result.face = nullptr;
  74. query_nearest_recursive(result.t, result.face, octree.root, ray);
  75. if (result.face)
  76. return std::optional{result};
  77. return std::nullopt;
  78. }
  79. void mesh_accelerator::query_nearest_recursive(float& nearest_t, ::mesh::face*& nearest_face, octree32::node_type node, const ray<float>& ray) const
  80. {
  81. // Get node bounds
  82. const aabb<float> node_bounds = get_node_bounds(node);
  83. // Test for intersection with node bounds
  84. auto aabb_intersection = ray_aabb_intersection(ray, node_bounds);
  85. // If ray passed through this node
  86. if (std::get<0>(aabb_intersection))
  87. {
  88. // Test all triangles in the node
  89. if (auto it = face_map.find(node); it != face_map.end())
  90. {
  91. const std::list<mesh::face*>& faces = it->second;
  92. for (mesh::face* face: faces)
  93. {
  94. // Get triangle coordinates
  95. const float3& a = reinterpret_cast<const float3&>(face->edge->vertex->position);
  96. const float3& b = reinterpret_cast<const float3&>(face->edge->next->vertex->position);
  97. const float3& c = reinterpret_cast<const float3&>(face->edge->previous->vertex->position);
  98. // Test for intersection with triangle
  99. auto triangle_intersection = ray_triangle_intersection(ray, a, b, c);
  100. if (std::get<0>(triangle_intersection))
  101. {
  102. float t = std::get<1>(triangle_intersection);
  103. if (t < nearest_t)
  104. {
  105. nearest_t = t;
  106. nearest_face = face;
  107. }
  108. }
  109. }
  110. }
  111. // Test all child nodes
  112. if (!octree.is_leaf(node))
  113. {
  114. for (int i = 0; i < 8; ++i)
  115. query_nearest_recursive(nearest_t, nearest_face, octree.child(node, i), ray);
  116. }
  117. }
  118. }
  119. aabb<float> mesh_accelerator::get_node_bounds(octree32::node_type node) const
  120. {
  121. // Decode Morton location of node
  122. auto [x, y, z] = morton::decode_3d(octree32::location(node));
  123. float3 node_location = float3{static_cast<float>(x), static_cast<float>(y), static_cast<float>(z)};
  124. // Get node dimensions at node depth
  125. const float3& dimensions = node_dimensions[octree32::depth(node)];
  126. // Calculate AABB
  127. float3 min_point = (node_location * dimensions) - center_offset;
  128. return aabb<float>{min_point, min_point + dimensions};
  129. }
  130. octree32::node_type mesh_accelerator::find_node(const float3& point) const
  131. {
  132. // Transform point to octree space
  133. float3 transformed_point = (point + center_offset);
  134. // Account for floating-point tolerance
  135. const float epsilon = 0.00001f;
  136. transformed_point.x = std::max<float>(0.0f, std::min<float>(node_dimensions[0].x - epsilon, transformed_point.x));
  137. transformed_point.y = std::max<float>(0.0f, std::min<float>(node_dimensions[0].y - epsilon, transformed_point.y));
  138. transformed_point.z = std::max<float>(0.0f, std::min<float>(node_dimensions[0].z - epsilon, transformed_point.z));
  139. // Transform point to max-depth node space
  140. transformed_point = transformed_point / node_dimensions[octree32::max_depth];
  141. // Encode transformed point as a Morton location code
  142. std::uint32_t location = morton::encode_3d(
  143. static_cast<std::uint32_t>(transformed_point.x),
  144. static_cast<std::uint32_t>(transformed_point.y),
  145. static_cast<std::uint32_t>(transformed_point.z));
  146. /// Return max depth node at the determined location
  147. return octree32::node(octree32::max_depth, location);
  148. }