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

433 lines
8.2 KiB

  1. /*
  2. * Copyright (C) 2021 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.hpp"
  20. #include <stdexcept>
  21. namespace geom {
  22. mesh::mesh(const mesh& other)
  23. {
  24. *this = other;
  25. }
  26. mesh::~mesh()
  27. {
  28. clear();
  29. }
  30. mesh& mesh::operator=(const mesh& other)
  31. {
  32. // Clear the mesh
  33. clear();
  34. // Resize vertices, edges, and faces
  35. vertices.resize(other.vertices.size());
  36. edges.resize(other.edges.size());
  37. faces.resize(other.faces.size());
  38. // Allocate vertices
  39. for (std::size_t i = 0; i < vertices.size(); ++i)
  40. vertices[i] = new vertex();
  41. // Allocate edges
  42. for (std::size_t i = 0; i < edges.size(); ++i)
  43. {
  44. edges[i] = new edge();
  45. edges[i]->symmetric = new edge();
  46. edges[i]->symmetric->symmetric = edges[i];
  47. }
  48. // Allocate faces
  49. for (std::size_t i = 0; i < faces.size(); ++i)
  50. faces[i] = new face();
  51. // Copy vertices
  52. for (std::size_t i = 0; i < vertices.size(); ++i)
  53. {
  54. vertex* va = vertices[i];
  55. const vertex* vb = other.vertices[i];
  56. va->index = vb->index;
  57. va->position = vb->position;
  58. va->edge = nullptr;
  59. if (vb->edge)
  60. {
  61. va->edge = edges[vb->edge->index];
  62. if (vb->edge != other.edges[vb->edge->index])
  63. va->edge = va->edge->symmetric;
  64. }
  65. }
  66. // Copy edges
  67. for (std::size_t i = 0; i < edges.size(); ++i)
  68. {
  69. edge* ea = edges[i];
  70. const edge* eb = other.edges[i];
  71. for (std::size_t j = 0; j < 2; ++j)
  72. {
  73. ea->index = eb->index;
  74. ea->vertex = vertices[eb->vertex->index];
  75. ea->face = nullptr;
  76. if (eb->face)
  77. ea->face = faces[eb->face->index];
  78. ea->previous = edges[eb->previous->index];
  79. if (eb->previous != other.edges[eb->previous->index])
  80. ea->previous = ea->previous->symmetric;
  81. ea->next = edges[eb->next->index];
  82. if (eb->next != other.edges[eb->next->index])
  83. ea->next = ea->next->symmetric;
  84. ea = ea->symmetric;
  85. eb = eb->symmetric;
  86. }
  87. }
  88. // Copy faces
  89. for (std::size_t i = 0; i < faces.size(); ++i)
  90. {
  91. face* fa = faces[i];
  92. const face* fb = other.faces[i];
  93. fa->index = fb->index;
  94. fa->edge = edges[fb->edge->index];
  95. if (fb->edge != other.edges[fb->edge->index])
  96. fa->edge = fa->edge->symmetric;
  97. }
  98. return *this;
  99. }
  100. void mesh::clear()
  101. {
  102. // Deallocate vertices
  103. for (mesh::vertex* vertex: vertices)
  104. delete vertex;
  105. // Deallocate edges
  106. for (mesh::edge* edge: edges)
  107. {
  108. delete edge->symmetric;
  109. delete edge;
  110. }
  111. // Deallocate faces
  112. for (mesh::face* face: faces)
  113. delete face;
  114. vertices.clear();
  115. edges.clear();
  116. faces.clear();
  117. }
  118. mesh::vertex* mesh::add_vertex(const float3& position)
  119. {
  120. mesh::vertex* vertex = new mesh::vertex();
  121. vertex->edge = nullptr;
  122. vertex->position = position;
  123. vertex->index = vertices.size();
  124. vertices.push_back(vertex);
  125. return vertex;
  126. }
  127. mesh::edge* mesh::add_edge(mesh::vertex* a, mesh::vertex* b)
  128. {
  129. mesh::edge* ab = new mesh::edge();
  130. mesh::edge* ba = new mesh::edge();
  131. ab->vertex = a;
  132. ab->face = nullptr;
  133. ab->previous = ba;
  134. ab->next = ba;
  135. ab->symmetric = ba;
  136. ab->index = edges.size();
  137. ba->vertex = b;
  138. ba->face = nullptr;
  139. ba->previous = ab;
  140. ba->next = ab;
  141. ba->symmetric = ab;
  142. ba->index = edges.size();
  143. if (!a->edge)
  144. {
  145. a->edge = ab;
  146. }
  147. else
  148. {
  149. mesh::edge* a_in = find_free_incident(a);
  150. mesh::edge* a_out = a_in->next;
  151. a_in->next = ab;
  152. ab->previous = a_in;
  153. ba->next = a_out;
  154. a_out->previous = ba;
  155. }
  156. if (!b->edge)
  157. {
  158. b->edge = ba;
  159. }
  160. else
  161. {
  162. mesh::edge* b_in = find_free_incident(b);
  163. mesh::edge* b_out = b_in->next;
  164. b_in->next = ba;
  165. ba->previous = b_in;
  166. ab->next = b_out;
  167. b_out->previous = ab;
  168. }
  169. // Add edge
  170. edges.push_back(ab);
  171. return ab;
  172. }
  173. mesh::face* mesh::add_face(const loop& loop)
  174. {
  175. if (loop.empty())
  176. {
  177. throw std::runtime_error("Empty edge loop");
  178. }
  179. // Validate edge loop
  180. for (std::size_t i = 0; i < loop.size(); ++i)
  181. {
  182. mesh::edge* current = loop[i];
  183. mesh::edge* next = loop[(i + 1) % loop.size()];
  184. if (current->symmetric->vertex != next->vertex)
  185. {
  186. // Disconnected edge loop
  187. throw std::runtime_error("Disconnected edge loop");
  188. }
  189. if (current->face)
  190. {
  191. // This edge already has a face
  192. throw std::runtime_error("Non-manifold mesh 1");
  193. }
  194. }
  195. // Make edges adjacent
  196. for (std::size_t i = 0; i < loop.size(); ++i)
  197. {
  198. if (!make_adjacent(loop[i], loop[(i + 1) % loop.size()]))
  199. {
  200. throw std::runtime_error("Non-manifold mesh 2");
  201. }
  202. }
  203. // Create face
  204. mesh::face* face = new mesh::face();
  205. face->edge = loop[0];
  206. face->index = faces.size();
  207. // Add face
  208. faces.push_back(face);
  209. // Connect edges to the face
  210. for (mesh::edge* edge: loop)
  211. {
  212. edge->face = face;
  213. }
  214. return face;
  215. }
  216. void mesh::remove_face(mesh::face* face)
  217. {
  218. // Nullify pointers to this face
  219. mesh::edge* edge = face->edge;
  220. do
  221. {
  222. edge->face = nullptr;
  223. edge = edge->next;
  224. }
  225. while (edge != face->edge);
  226. // Adjust indices of faces after this face
  227. for (std::size_t i = face->index + 1; i < faces.size(); ++i)
  228. {
  229. --faces[i]->index;
  230. }
  231. // Remove face from the faces vector
  232. faces.erase(faces.begin() + face->index);
  233. // Deallocate face
  234. delete face;
  235. }
  236. void mesh::remove_edge(mesh::edge* edge)
  237. {
  238. mesh::edge* ab = edge;
  239. mesh::edge* ba = edge->symmetric;
  240. mesh::vertex* a = ab->vertex;
  241. mesh::edge* a_in = ab->previous;
  242. mesh::edge* a_out = ba->next;
  243. mesh::vertex* b = ba->vertex;
  244. mesh::edge* b_in = ba->previous;
  245. mesh::edge* b_out = ab->next;
  246. // Remove dependent faces
  247. if (ab->face)
  248. remove_face(ab->face);
  249. if (ba->face)
  250. remove_face(ba->face);
  251. // Re-link edges
  252. if (a->edge == ab)
  253. a->edge = (a_out == ab) ? nullptr : a_out;
  254. if (b->edge == ba)
  255. b->edge = (b_out == ba) ? nullptr : b_out;
  256. a_in->next = a_out;
  257. a_out->previous = a_in;
  258. b_in->next = b_out;
  259. b_out->previous = b_in;
  260. // Adjust indices of edges after this edge
  261. for (std::size_t i = edge->index + 1; i < edges.size(); ++i)
  262. {
  263. --edges[i]->index;
  264. --edges[i]->symmetric->index;
  265. }
  266. // Remove edge from the edges vector
  267. edges.erase(edges.begin() + edge->index);
  268. // Deallocate edge
  269. delete edge->symmetric;
  270. delete edge;
  271. }
  272. void mesh::remove_vertex(mesh::vertex* vertex)
  273. {
  274. // Remove connected edges
  275. if (vertex->edge)
  276. {
  277. mesh::edge* current = nullptr;
  278. mesh::edge* next = vertex->edge;
  279. do
  280. {
  281. current = next;
  282. next = next->symmetric->next;
  283. if (next == current)
  284. {
  285. next = next->symmetric->next;
  286. }
  287. remove_edge(current);
  288. }
  289. while (current != next);
  290. }
  291. // Adjust indices of vertices after this vertex
  292. for (std::size_t i = vertex->index + 1; i < vertices.size(); ++i)
  293. {
  294. --vertices[i]->index;
  295. }
  296. // Remove vertex from the vertices vector
  297. vertices.erase(vertices.begin() + vertex->index);
  298. // Deallocate vertex
  299. delete vertex;
  300. }
  301. mesh::edge* mesh::find_free_incident(mesh::vertex* vertex) const
  302. {
  303. mesh::edge* begin = vertex->edge->symmetric;
  304. mesh::edge* current = begin;
  305. do
  306. {
  307. if (!current->face)
  308. {
  309. return current;
  310. }
  311. current = current->next->symmetric;
  312. }
  313. while (current != begin);
  314. return nullptr;
  315. }
  316. mesh::edge* mesh::find_free_incident(mesh::edge* start_edge, mesh::edge* end_edge) const
  317. {
  318. if (start_edge == end_edge)
  319. {
  320. return nullptr;
  321. }
  322. mesh::edge* current = start_edge;
  323. do
  324. {
  325. if (!current->face)
  326. {
  327. return current;
  328. }
  329. current = current->next->symmetric;
  330. }
  331. while (current != end_edge);
  332. return nullptr;
  333. }
  334. bool mesh::make_adjacent(mesh::edge* in, mesh::edge* out)
  335. {
  336. if (in->next == out)
  337. {
  338. return true;
  339. }
  340. mesh::edge* b = in->next;
  341. mesh::edge* d = out->previous;
  342. mesh::edge* g = find_free_incident(out->symmetric, in);
  343. if (!g)
  344. {
  345. return false;
  346. }
  347. mesh::edge* h = g->next;
  348. in->next = out;
  349. out->previous = in;
  350. g->next = b;
  351. b->previous = g;
  352. d->next = h;
  353. h->previous = d;
  354. return true;
  355. }
  356. } // namespace geom