-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathepa_polytope_expand.hpp
284 lines (248 loc) · 8.77 KB
/
epa_polytope_expand.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//
// Created by wei on 22-6-5.
//
#pragma once
#include "epa_polytope_utils.h"
namespace fcl {
namespace cvx_collide {
template <typename T>
bool Polytope<T>::IsPointOutsidePolytopeFace(
const PolytopeFace<T>* face, const Vector3<T>& point,
const T point2plane_should_greater_than) const {
Vector3<T> face_normal;
T area{0};
auto ok = ComputeFaceNormalPointingOutward(face, face_normal, &area);
if (!ok) {
// If area is zero, then also remove it
if (area <= T(0.0))
return true;
else
return false;
}
const Vector3<T>& point_on_face =
face->edges_of_face[0]->vertices_of_edge[0]->vertex_location;
const T dot_value = face_normal.dot(point - point_on_face);
return dot_value >= point2plane_should_greater_than;
}
template <typename T>
typename Polytope<T>::PolytopeExpansionStatus Polytope<T>::ExpandPolytope(
const MinkowskiDiffVertex<T>& next_v, PolytopeFace<T>* start_face) {
// If we are here, we should have a valid start face
assert(start_face != nullptr);
// First mark edge/face visibility as unknown
initVisibilityCacheVariables();
auto ok = computeVisiblePatch(start_face, next_v.vertex);
if (!ok) return PolytopeExpansionStatus::Failed;
// Given visibility of faces/edges, remove the vertices
updateVertexRemoveFlag();
// Now, remove all visible faces and internal edges
removeAccordingToVisibility();
// Add new vertex
PolytopeVertex<T>* new_v = AddNewVertex(next_v);
if (new_v == nullptr) return PolytopeExpansionStatus::MallocFailed;
// Add new faces
bool new_face_ok = true;
auto add_new_edge_face_functor =
[this, &new_v, &new_face_ok](PolytopeElementBase* element) -> void {
auto type = element->type;
if (type != PolytopeElementType::Edge) return;
auto* edge = static_cast<PolytopeEdge<T>*>(element);
if (edge->cached_visibility != EdgeVisibilityStatus::Border) return;
// Two other edges
std::array<PolytopeEdge<T>*, 2> e{nullptr, nullptr};
for (auto i = 0; i < 2; i++) {
PolytopeVertex<T>* v_i = edge->vertices_of_edge[i];
if (v_i->cached_vertex2new_v_edge == nullptr) {
v_i->cached_vertex2new_v_edge = AddNewEdge(new_v, v_i);
}
// Now the edge should be valid
e[i] = static_cast<PolytopeEdge<T>*>(v_i->cached_vertex2new_v_edge);
}
// Make the face
auto* f = AddNewFace(edge, e[0], e[1]);
if (f == nullptr) {
new_face_ok = false;
return;
}
};
visitList(edge_list_, add_new_edge_face_functor);
// Return flag
return new_face_ok ? PolytopeExpansionStatus::OK
: PolytopeExpansionStatus::MallocFailed;
}
template <typename T>
void Polytope<T>::initVisibilityCacheVariables() {
auto visit_element_functor = [](PolytopeElementBase* element) -> void {
// Now it holds a distance
switch (element->type) {
case PolytopeElementType::Vertex: {
auto* vertex = static_cast<PolytopeVertex<T>*>(element);
// mark as true, invalidate later
vertex->cached_vertex_should_remove = true;
vertex->cached_vertex2new_v_edge = nullptr;
return;
}
case PolytopeElementType::Edge: {
auto* edge = static_cast<PolytopeEdge<T>*>(element);
edge->cached_visibility = EdgeVisibilityStatus::Unknown;
return;
}
default: {
assert(element->type == PolytopeElementType::Face);
auto* face = static_cast<PolytopeFace<T>*>(element);
face->cached_visibility = FaceVisibilityStatus::Unknown;
return;
}
}
};
// Go
visitList(vertex_list_, visit_element_functor);
visitList(edge_list_, visit_element_functor);
visitList(face_list_, visit_element_functor);
}
template <typename T>
bool Polytope<T>::computeVisiblePatch(PolytopeFace<T>* start_face,
const Vector3<T>& next_v) {
struct Task {
PolytopeFace<T>* face;
int edge_idx_of_face;
};
std::stack<Task> task_stack;
// Init
start_face->cached_visibility = FaceVisibilityStatus::Visible;
for (auto edge_idx = 0; edge_idx < 3; edge_idx++) {
task_stack.push({start_face, edge_idx});
}
// Start the loop
while (!task_stack.empty()) {
// Get this task
const auto& task = task_stack.top();
PolytopeFace<T>* f = task.face;
int edge_index = task.edge_idx_of_face;
task_stack.pop();
assert(f->cached_visibility == FaceVisibilityStatus::Visible);
PolytopeEdge<T>* edge = f->edges_of_face[edge_index];
if (edge == nullptr) return false;
PolytopeFace<T>* g = (edge->faces_of_edge[0] == f) ? edge->faces_of_edge[1]
: edge->faces_of_edge[0];
if (g == nullptr) return false;
assert(g != nullptr);
bool is_visible = (g->cached_visibility == FaceVisibilityStatus::Visible);
bool is_hidden = (g->cached_visibility == FaceVisibilityStatus::Hidden);
assert(!(is_visible && is_hidden));
if (is_visible) {
// Both f and g are visible
edge->cached_visibility = EdgeVisibilityStatus::Internal;
continue;
}
if (is_hidden) {
edge->cached_visibility = EdgeVisibilityStatus::Border;
continue;
}
// face g is not classified yet
is_visible = IsPointOutsidePolytopeFace(g, next_v);
if (is_visible) {
g->cached_visibility = FaceVisibilityStatus::Visible;
edge->cached_visibility = EdgeVisibilityStatus::Internal;
for (auto i = 0; i < 3; i++) {
task_stack.push({g, i});
}
} else {
g->cached_visibility = FaceVisibilityStatus::Hidden;
edge->cached_visibility = EdgeVisibilityStatus::Border;
}
}
// Expand ok
return true;
}
template <typename T>
void Polytope<T>::updateVertexRemoveFlag() {
auto visit_element_functor = [](PolytopeElementBase* element) -> void {
// Now it holds a distance
switch (element->type) {
case PolytopeElementType::Edge: {
auto* edge = static_cast<PolytopeEdge<T>*>(element);
if (edge->cached_visibility != EdgeVisibilityStatus::Internal) {
// do not remove the edge, thus must keep all vertices
edge->vertices_of_edge[0]->cached_vertex_should_remove = false;
edge->vertices_of_edge[1]->cached_vertex_should_remove = false;
}
return;
}
default: {
return;
}
}
};
// Go
visitList(edge_list_, visit_element_functor);
}
template <typename T>
std::size_t Polytope<T>::removeIf(
PolytopeElementList& list,
const std::function<bool(PolytopeElementBase*)>& remove_if_true,
PolytopeElementMallocList& reclaim_removed_list) {
// Nothing removed
if (isEmptyList(list)) return 0;
// Now, at least one element
std::size_t remove_count = 0;
PolytopeElementBase* prev = &list;
PolytopeElementBase* current = prev->next;
assert(current != nullptr);
while (current != nullptr) {
bool should_remove = remove_if_true(current);
if (should_remove) {
// Update the connection
prev->next = current->next;
// Clear and reclaim this one
current->Clear();
current->malloc_next = reclaim_removed_list.malloc_next;
reclaim_removed_list.malloc_next = current;
// Move to next
remove_count += 1;
current = prev->next;
} else {
prev = current;
current = prev->next;
}
}
return remove_count;
}
template <typename T>
void Polytope<T>::removeAccordingToVisibility() {
// First remove face
auto should_face_removed = [](PolytopeElementBase* element) -> bool {
assert(element->type == PolytopeElementType::Face);
auto* face = static_cast<const PolytopeFace<T>*>(element);
auto should_remove =
(face->cached_visibility == FaceVisibilityStatus::Visible);
// Need to remove the reference in edges
if (should_remove) {
for (auto i = 0; i < 3; i++) {
PolytopeEdge<T>* edge_i = face->edges_of_face[i];
if (edge_i->faces_of_edge[0] == face) {
edge_i->faces_of_edge[0] = edge_i->faces_of_edge[1];
}
edge_i->faces_of_edge[1] = nullptr;
}
}
return should_remove;
};
removeIf(face_list_, should_face_removed, face_malloc_list_);
// Next remove edge
auto should_edge_removed = [](PolytopeElementBase* element) -> bool {
assert(element->type == PolytopeElementType::Edge);
auto* edge = static_cast<const PolytopeEdge<T>*>(element);
return edge->cached_visibility == EdgeVisibilityStatus::Internal;
};
removeIf(edge_list_, should_edge_removed, edge_malloc_list_);
// Finally, remove vertex
auto should_vertex_removed = [](PolytopeElementBase* element) -> bool {
assert(element->type == PolytopeElementType::Vertex);
auto* vertex = static_cast<const PolytopeVertex<T>*>(element);
return vertex->cached_vertex_should_remove;
};
removeIf(vertex_list_, should_vertex_removed, vertex_malloc_list_);
}
} // namespace cvx_collide
} // namespace fcl