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

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