Antkeeper  0.0.1
brep-edge.cpp
Go to the documentation of this file.
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 
22 #include <algorithm>
23 
24 namespace geom {
25 
27 {
28  if (empty())
29  {
30  // List empty, initialize
31  m_head = loop;
32  loop->m_edge_next = loop;
33  loop->m_edge_previous = loop;
34  }
35  else
36  {
37  // Append loop
38  loop->m_edge_next = m_head;
39  loop->m_edge_previous = m_head->m_edge_previous;
40  m_head->m_edge_previous->m_edge_next = loop;
41  m_head->m_edge_previous = loop;
42  }
43 
44  ++m_size;
45 }
46 
48 {
49  // Directly link next and previous loops
50  loop->m_edge_next->m_edge_previous = loop->m_edge_previous;
51  loop->m_edge_previous->m_edge_next = loop->m_edge_next;
52 
53  // If loop was the list head, update head
54  if (m_head == loop)
55  {
56  m_head = loop->m_edge_next;
57  }
58 
59  --m_size;
60 }
61 
63 {
64  if (a == b)
65  {
66  return nullptr;
67  }
68 
70  ab->m_index = size() - 1;
71  ab->m_vertices[0] = a;
72  ab->m_vertices[1] = b;
73 
74  // Append edge AB to the edge lists of vertices A and B
75  a->m_edges.push_back(ab);
76  b->m_edges.push_back(ab);
77 
78  return ab;
79 };
80 
82 {
83  // Kill all loops and faces bounded by this edge
84  while (!edge->loops().empty())
85  {
86  m_mesh->faces().erase(edge->loops().back()->face());
87  }
88 
89  // Remove this edge from its vertices' lists of edges
90  edge->vertices().front()->m_edges.remove(edge);
91  edge->vertices().back()->m_edges.remove(edge);
92 
93  // Erase edge
95 }
96 
98 {
99  while (!empty())
100  {
101  erase(back());
102  }
103 }
104 
106 {
107  if (!a->edges().empty() && !b->edges().empty() && a != b)
108  {
109  brep_edge* ea = a->edges().front();
110  brep_edge* eb = b->edges().front();
111  const std::size_t n = std::min<std::size_t>(a->edges().size(), b->edges().size());
112  for (std::size_t i = 0; i < n; ++i)
113  {
114  if (ea->vertices()[1] == b || ea->vertices()[0] == b)
115  {
116  return ea;
117  }
118 
119  if (eb->vertices()[1] == a || eb->vertices()[0] == a)
120  {
121  return eb;
122  }
123 
124  ea = ea->m_vertex_next[ea->vertices()[1] == a];
125  eb = eb->m_vertex_next[eb->vertices()[1] == b];
126  }
127  }
128 
129  return nullptr;
130 }
131 
132 } // namespace geom
void erase(brep_edge *edge) override
Erases an edge and all dependent loops and faces.
Definition: brep-edge.cpp:81
void clear() noexcept
Erases all edges and their dependent loops and faces.
Definition: brep-edge.cpp:97
brep_edge * find(brep_vertex *a, brep_vertex *b) const
Finds an edge bounded by vertices a and b (in any order).
Definition: brep-edge.cpp:105
constexpr bool empty() const noexcept
Returns true if the list is empty, false otherwise.
Definition: brep-edge.hpp:197
void push_back(brep_loop *loop)
Appends a loop to the end of the list.
Definition: brep-edge.cpp:26
void remove(brep_loop *loop)
Removes an loop from the list.
Definition: brep-edge.cpp:47
brep_loop * back() const noexcept
Returns the last loop.
Definition: brep-edge.hpp:127
Curve segment bounded by two vertices.
Definition: brep-edge.hpp:237
constexpr const brep_edge_loop_list & loops() const noexcept
Returns the list of loops that share this edge.
Definition: brep-edge.hpp:262
constexpr const std::array< brep_vertex *, 2 > & vertices() const noexcept
Returns the pair of vertices that bound this edge.
Definition: brep-edge.hpp:256
constexpr std::size_t size() const noexcept
Returns the number of elements in the container.
virtual void erase(element_type *element)
Erases an element from the container.
constexpr bool empty() const noexcept
Returns true if the container is empty, false otherwise.
constexpr element_type * back() const noexcept
Returns the last element.
virtual element_type * emplace_back()
Appends a new element to the end of the container.
void erase(brep_face *face) override
Erases a face and all of its loops.
Definition: brep-face.cpp:117
Connected boundary of a single face.
Definition: brep-loop.hpp:38
constexpr brep_face * face() const noexcept
Returns a pointer to the loop face.
Definition: brep-loop.hpp:70
const brep_face_container & faces() const noexcept
Returns the mesh faces.
Definition: brep-mesh.hpp:105
A point in space.
Geometric algorithms.
@ a
Vertex A region.
@ ab
Edge AB region.
@ b
Vertex B region.