-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathbroad_phase.cpp
More file actions
284 lines (260 loc) · 10.3 KB
/
broad_phase.cpp
File metadata and controls
284 lines (260 loc) · 10.3 KB
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
#include <common.hpp>
#include <ipc/broad_phase/broad_phase.hpp>
#include <ipc/candidates/candidates.hpp>
namespace py = pybind11;
using namespace ipc;
class PyBroadPhase : public BroadPhase {
public:
using BroadPhase::BroadPhase; // Inherit constructors
std::string name() const override
{
PYBIND11_OVERRIDE_PURE(std::string, BroadPhase, name);
}
void build(
Eigen::ConstRef<Eigen::MatrixXd> vertices,
Eigen::ConstRef<Eigen::MatrixXi> edges,
Eigen::ConstRef<Eigen::MatrixXi> faces,
const double inflation_radius = 0) override
{
PYBIND11_OVERRIDE(
void, BroadPhase, build, vertices, edges, faces, inflation_radius);
}
void build(
Eigen::ConstRef<Eigen::MatrixXd> vertices_t0,
Eigen::ConstRef<Eigen::MatrixXd> vertices_t1,
Eigen::ConstRef<Eigen::MatrixXi> edges,
Eigen::ConstRef<Eigen::MatrixXi> faces,
const double inflation_radius = 0) override
{
PYBIND11_OVERRIDE(
void, BroadPhase, build, vertices_t0, vertices_t1, edges, faces,
inflation_radius);
}
void clear() override { PYBIND11_OVERRIDE(void, BroadPhase, clear); }
void detect_vertex_vertex_candidates(
std::vector<VertexVertexCandidate>& candidates) const override
{
py::gil_scoped_acquire gil; // Acquire GIL before calling Python code
py::function override =
py::get_override(this, "detect_vertex_vertex_candidates");
if (override) {
candidates = override().cast<std::vector<VertexVertexCandidate>>();
return;
}
throw std::runtime_error(
"Tried to call pure virtual function \"BroadPhase::detect_vertex_vertex_candidates\"");
}
void detect_edge_vertex_candidates(
std::vector<EdgeVertexCandidate>& candidates) const override
{
py::gil_scoped_acquire gil; // Acquire GIL before calling Python code
py::function override =
py::get_override(this, "detect_edge_vertex_candidates");
if (override) {
candidates = override().cast<std::vector<EdgeVertexCandidate>>();
return;
}
throw std::runtime_error(
"Tried to call pure virtual function \"BroadPhase::detect_edge_vertex_candidates\"");
}
void detect_edge_edge_candidates(
std::vector<EdgeEdgeCandidate>& candidates) const override
{
py::gil_scoped_acquire gil; // Acquire GIL before calling Python code
py::function override =
py::get_override(this, "detect_edge_edge_candidates");
if (override) {
candidates = override().cast<std::vector<EdgeEdgeCandidate>>();
return;
}
throw std::runtime_error(
"Tried to call pure virtual function \"BroadPhase::detect_edge_edge_candidates\"");
}
void detect_face_vertex_candidates(
std::vector<FaceVertexCandidate>& candidates) const override
{
py::gil_scoped_acquire gil; // Acquire GIL before calling Python code
py::function override =
py::get_override(this, "detect_face_vertex_candidates");
if (override) {
candidates = override().cast<std::vector<FaceVertexCandidate>>();
return;
}
throw std::runtime_error(
"Tried to call pure virtual function \"BroadPhase::detect_face_vertex_candidates\"");
}
void detect_edge_face_candidates(
std::vector<EdgeFaceCandidate>& candidates) const override
{
py::gil_scoped_acquire gil; // Acquire GIL before calling Python code
py::function override =
py::get_override(this, "detect_edge_face_candidates");
if (override) {
candidates = override().cast<std::vector<EdgeFaceCandidate>>();
return;
}
throw std::runtime_error(
"Tried to call pure virtual function \"BroadPhase::detect_edge_face_candidates\"");
}
void detect_face_face_candidates(
std::vector<FaceFaceCandidate>& candidates) const override
{
py::gil_scoped_acquire gil; // Acquire GIL before calling Python code
py::function override =
py::get_override(this, "detect_face_face_candidates");
if (override) {
candidates = override().cast<std::vector<FaceFaceCandidate>>();
return;
}
throw std::runtime_error(
"Tried to call pure virtual function \"BroadPhase::detect_face_face_candidates\"");
}
void
detect_collision_candidates(int dim, Candidates& candidates) const override
{
{
py::gil_scoped_acquire
gil; // Acquire GIL before calling Python code
py::function override =
py::get_override(this, "detect_collision_candidates");
if (override) {
candidates = override(dim).cast<Candidates>();
return;
}
}
BroadPhase::detect_collision_candidates(dim, candidates);
}
};
void define_broad_phase(py::module_& m)
{
py::class_<BroadPhase, PyBroadPhase, std::shared_ptr<BroadPhase>>(
m, "BroadPhase")
.def(py::init<>())
.def("name", &BroadPhase::name, "Get the name of the broad phase.")
.def(
"build",
py::overload_cast<
Eigen::ConstRef<Eigen::MatrixXd>,
Eigen::ConstRef<Eigen::MatrixXi>,
Eigen::ConstRef<Eigen::MatrixXi>, const double>(
&BroadPhase::build),
R"ipc_Qu8mg5v7(
Build the broad phase for static collision detection.
Parameters:
vertices: Vertex positions
edges: Collision mesh edges
faces: Collision mesh faces
inflation_radius: Radius of inflation around all elements.
)ipc_Qu8mg5v7",
py::arg("vertices"), py::arg("edges"), py::arg("faces"),
py::arg("inflation_radius") = 0)
.def(
"build",
py::overload_cast<
Eigen::ConstRef<Eigen::MatrixXd>,
Eigen::ConstRef<Eigen::MatrixXd>,
Eigen::ConstRef<Eigen::MatrixXi>,
Eigen::ConstRef<Eigen::MatrixXi>, const double>(
&BroadPhase::build),
R"ipc_Qu8mg5v7(
Build the broad phase for continuous collision detection.
Parameters:
vertices_t0: Starting vertices of the vertices.
vertices_t1: Ending vertices of the vertices.
edges: Collision mesh edges
faces: Collision mesh faces
inflation_radius: Radius of inflation around all elements.
)ipc_Qu8mg5v7",
py::arg("vertices_t0"), py::arg("vertices_t1"), py::arg("edges"),
py::arg("faces"), py::arg("inflation_radius") = 0)
.def("clear", &BroadPhase::clear, "Clear any built data.")
.def(
"detect_vertex_vertex_candidates",
[](const BroadPhase& self) {
std::vector<VertexVertexCandidate> candidates;
self.detect_vertex_vertex_candidates(candidates);
return candidates;
},
R"ipc_Qu8mg5v7(
Find the candidate vertex-vertex collisions.
Returns:
The candidate vertex-vertex collisions.
)ipc_Qu8mg5v7")
.def(
"detect_edge_vertex_candidates",
[](const BroadPhase& self) {
std::vector<EdgeVertexCandidate> candidates;
self.detect_edge_vertex_candidates(candidates);
return candidates;
},
R"ipc_Qu8mg5v7(
Find the candidate edge-vertex collisions.
Returns:
The candidate edge-vertex collisions.
)ipc_Qu8mg5v7")
.def(
"detect_edge_edge_candidates",
[](const BroadPhase& self) {
std::vector<EdgeEdgeCandidate> candidates;
self.detect_edge_edge_candidates(candidates);
return candidates;
},
R"ipc_Qu8mg5v7(
Find the candidate edge-edge collisions.
Returns:
The candidate edge-edge collisions.
)ipc_Qu8mg5v7")
.def(
"detect_face_vertex_candidates",
[](const BroadPhase& self) {
std::vector<FaceVertexCandidate> candidates;
self.detect_face_vertex_candidates(candidates);
return candidates;
},
R"ipc_Qu8mg5v7(
Find the candidate face-vertex collisions.
Returns:
The candidate face-vertex collisions.
)ipc_Qu8mg5v7")
.def(
"detect_edge_face_candidates",
[](const BroadPhase& self) {
std::vector<EdgeFaceCandidate> candidates;
self.detect_edge_face_candidates(candidates);
return candidates;
},
R"ipc_Qu8mg5v7(
Find the candidate edge-face intersections.
Returns:
The candidate edge-face intersections.
)ipc_Qu8mg5v7")
.def(
"detect_face_face_candidates",
[](const BroadPhase& self) {
std::vector<FaceFaceCandidate> candidates;
self.detect_face_face_candidates(candidates);
return candidates;
},
R"ipc_Qu8mg5v7(
Find the candidate face-face collisions.
Returns:
The candidate face-face collisions.
)ipc_Qu8mg5v7")
.def(
"detect_collision_candidates",
[](const BroadPhase& self, int dim) {
Candidates candidates;
self.detect_collision_candidates(dim, candidates);
return candidates;
},
R"ipc_Qu8mg5v7(
Detect all collision candidates needed for a given dimensional simulation.
Parameters:
dim: The dimension of the simulation (i.e., 2 or 3).
candidates: The detected collision candidates.
)ipc_Qu8mg5v7",
py::arg("dim"))
.def_readwrite(
"can_vertices_collide", &BroadPhase::can_vertices_collide,
"Function for determining if two vertices can collide.");
}