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

111 lines
3.3 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 "mesh-functions.hpp"
  20. #include "math/math.hpp"
  21. #include <unordered_map>
  22. struct edge_hasher
  23. {
  24. std::size_t operator()(const std::array<std::size_t, 2>& v) const noexcept
  25. {
  26. std::size_t hash = std::hash<std::size_t>()(v[0]);
  27. return hash ^ (std::hash<std::size_t>()(v[1]) + 0x9e3779b9 + (hash << 6) + (hash >> 2));
  28. }
  29. };
  30. void create_triangle_mesh(mesh& mesh, const std::vector<float3>& vertices, const std::vector<std::array<std::uint_fast32_t, 3>>& triangles)
  31. {
  32. for (const auto& vertex: vertices)
  33. mesh.add_vertex(vertex);
  34. std::unordered_map<std::array<std::size_t, 2>, ::mesh::edge*, edge_hasher> edge_map;
  35. const std::vector<mesh::vertex*>& mesh_vertices = mesh.get_vertices();
  36. std::vector<::mesh::edge*> loop(3);
  37. for (const auto& triangle: triangles)
  38. {
  39. ::mesh::vertex* triangle_vertices[3] =
  40. {
  41. mesh_vertices[triangle[0]],
  42. mesh_vertices[triangle[1]],
  43. mesh_vertices[triangle[2]]
  44. };
  45. for (int j = 0; j < 3; ++j)
  46. {
  47. ::mesh::vertex* start = triangle_vertices[j];
  48. ::mesh::vertex* end = triangle_vertices[(j + 1) % 3];
  49. if (auto it = edge_map.find({start->index, end->index}); it != edge_map.end())
  50. {
  51. loop[j] = it->second;
  52. }
  53. else
  54. {
  55. loop[j] = mesh.add_edge(start, end);
  56. edge_map[{start->index, end->index}] = loop[j];
  57. edge_map[{end->index, start->index}] = loop[j]->symmetric;
  58. }
  59. }
  60. mesh.add_face(loop);
  61. }
  62. }
  63. void calculate_face_normals(float* normals, const mesh& mesh)
  64. {
  65. const std::vector<mesh::face*>& faces = mesh.get_faces();
  66. for (std::size_t i = 0; i < faces.size(); ++i)
  67. {
  68. const mesh::face& face = *(faces[i]);
  69. float3& normal = reinterpret_cast<float3&>(normals[i * 3]);
  70. const float3& a = reinterpret_cast<const float3&>(face.edge->vertex->position);
  71. const float3& b = reinterpret_cast<const float3&>(face.edge->next->vertex->position);
  72. const float3& c = reinterpret_cast<const float3&>(face.edge->previous->vertex->position);
  73. normal = math::normalize(math::cross(b - a, c - a));
  74. }
  75. }
  76. aabb<float> calculate_bounds(const mesh& mesh)
  77. {
  78. float3 bounds_min;
  79. float3 bounds_max;
  80. for (int i = 0; i < 3; ++i)
  81. {
  82. bounds_min[i] = std::numeric_limits<float>::infinity();
  83. bounds_max[i] = -std::numeric_limits<float>::infinity();
  84. }
  85. for (const mesh::vertex* vertex: mesh.get_vertices())
  86. {
  87. const auto& position = vertex->position;
  88. for (int i = 0; i < 3; ++i)
  89. {
  90. bounds_min[i] = std::min<float>(bounds_min[i], position[i]);
  91. bounds_max[i] = std::max<float>(bounds_max[i], position[i]);
  92. }
  93. }
  94. return aabb<float>{bounds_min, bounds_max};
  95. }