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

132 lines
3.1 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/geom/brep/brep-edge.hpp>
  20. #include <engine/geom/brep/brep-mesh.hpp>
  21. #include <algorithm>
  22. namespace geom {
  23. void brep_edge_loop_list::push_back(brep_loop* loop)
  24. {
  25. if (empty())
  26. {
  27. // List empty, initialize
  28. m_head = loop;
  29. loop->m_edge_next = loop;
  30. loop->m_edge_previous = loop;
  31. }
  32. else
  33. {
  34. // Append loop
  35. loop->m_edge_next = m_head;
  36. loop->m_edge_previous = m_head->m_edge_previous;
  37. m_head->m_edge_previous->m_edge_next = loop;
  38. m_head->m_edge_previous = loop;
  39. }
  40. ++m_size;
  41. }
  42. void brep_edge_loop_list::remove(brep_loop* loop)
  43. {
  44. // Directly link next and previous loops
  45. loop->m_edge_next->m_edge_previous = loop->m_edge_previous;
  46. loop->m_edge_previous->m_edge_next = loop->m_edge_next;
  47. // If loop was the list head, update head
  48. if (m_head == loop)
  49. {
  50. m_head = loop->m_edge_next;
  51. }
  52. --m_size;
  53. }
  54. brep_edge* brep_edge_container::emplace_back(brep_vertex* a, brep_vertex* b)
  55. {
  56. if (a == b)
  57. {
  58. return nullptr;
  59. }
  60. brep_edge* ab = brep_element_container<brep_edge>::emplace_back();
  61. ab->m_index = size() - 1;
  62. ab->m_vertices[0] = a;
  63. ab->m_vertices[1] = b;
  64. // Append edge AB to the edge lists of vertices A and B
  65. a->m_edges.push_back(ab);
  66. b->m_edges.push_back(ab);
  67. return ab;
  68. };
  69. void brep_edge_container::erase(brep_edge* edge)
  70. {
  71. // Kill all loops and faces bounded by this edge
  72. while (!edge->loops().empty())
  73. {
  74. m_mesh->faces().erase(edge->loops().back()->face());
  75. }
  76. // Remove this edge from its vertices' lists of edges
  77. edge->vertices().front()->m_edges.remove(edge);
  78. edge->vertices().back()->m_edges.remove(edge);
  79. // Erase edge
  80. brep_element_container<brep_edge>::erase(edge);
  81. }
  82. void brep_edge_container::clear() noexcept
  83. {
  84. while (!empty())
  85. {
  86. erase(back());
  87. }
  88. }
  89. brep_edge* brep_edge_container::find(brep_vertex* a, brep_vertex* b) const
  90. {
  91. if (!a->edges().empty() && !b->edges().empty() && a != b)
  92. {
  93. brep_edge* ea = a->edges().front();
  94. brep_edge* eb = b->edges().front();
  95. const std::size_t n = std::min<std::size_t>(a->edges().size(), b->edges().size());
  96. for (std::size_t i = 0; i < n; ++i)
  97. {
  98. if (ea->vertices()[1] == b || ea->vertices()[0] == b)
  99. {
  100. return ea;
  101. }
  102. if (eb->vertices()[1] == a || eb->vertices()[0] == a)
  103. {
  104. return eb;
  105. }
  106. ea = ea->m_vertex_next[ea->vertices()[1] == a];
  107. eb = eb->m_vertex_next[eb->vertices()[1] == b];
  108. }
  109. }
  110. return nullptr;
  111. }
  112. } // namespace geom