-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcdt_bindings.cpp
376 lines (358 loc) · 14.2 KB
/
cdt_bindings.cpp
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <CDT.h>
#include <VerifyTopology.h>
#include <pybind11/operators.h>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <algorithm>
#include <string>
#include <sstream>
#include <utility>
namespace py = pybind11;
using coord_t = double;
using NearPointLocator_t = CDT::LocatorKDTree<coord_t>;
using V2d = CDT::V2d<coord_t>;
using Triangulation = CDT::Triangulation<coord_t, NearPointLocator_t>;
namespace
{
std::string TriInd2str(CDT::TriInd it)
{
return it != CDT::noNeighbor ? std::to_string(it) : "-";
}
} // namespace
PYBIND11_MODULE(PythonCDT, m)
{
// clang-format off
m.doc() = R"pbdoc(
PythonCDT module: python bindings for CDT:
Constrained Delaunay Triangulation
-----------------------
.. currentmodule:: PythonCDT
.. autosummary::
:toctree: _generate
)pbdoc";
// clang-format on
#ifdef VERSION_INFO
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
#else
m.attr("__version__") = "dev";
#endif
m.attr("NO_NEIGHBOR") = py::int_(CDT::noNeighbor);
m.attr("NO_VERTEX") = py::int_(CDT::noVertex);
py::enum_<CDT::VertexInsertionOrder::Enum>(m, "VertexInsertionOrder")
.value("AUTO", CDT::VertexInsertionOrder::Auto)
.value("AS_PROVIDED", CDT::VertexInsertionOrder::AsProvided);
py::enum_<CDT::IntersectingConstraintEdges::Enum>(
m, "IntersectingConstraintEdges")
.value("NOT_ALLOWED", CDT::IntersectingConstraintEdges::NotAllowed)
.value("TRY_RESOLVE", CDT::IntersectingConstraintEdges::TryResolve)
.value("DONT_CHECK", CDT::IntersectingConstraintEdges::DontCheck);
py::class_<V2d>(m, "V2d", py::buffer_protocol())
.def(py::init<coord_t, coord_t>(), py::arg("x"), py::arg("y"))
.def(py::init([](py::buffer b) {
// Request a buffer descriptor from Python
py::buffer_info info = b.request();
// Some sanity checks ...
if (info.format != py::format_descriptor<coord_t>::format())
throw std::runtime_error(
"Incompatible format: expected a double array!");
if (info.ndim != 1)
throw std::runtime_error("Incompatible buffer dimension!");
// create from buffer
const coord_t* const ptr = static_cast<coord_t*>(info.ptr);
return V2d{ptr[0], ptr[1]};
}))
.def_readwrite("x", &V2d::x)
.def_readwrite("y", &V2d::y)
.def(
"__eq__",
[](const V2d& lhs, const V2d& rhs) {
return lhs.x == rhs.x && lhs.y == rhs.y;
})
.def(
"__repr__",
[](const V2d& v) {
std::ostringstream oss;
oss << "V2d(" << v.x << ", " << v.y << ")";
return oss.str();
})
.def_buffer([](V2d& v) -> py::buffer_info {
return py::buffer_info(
&v,
sizeof(coord_t),
py::format_descriptor<coord_t>::format(),
1,
{2},
{sizeof(coord_t) * 2});
});
PYBIND11_NUMPY_DTYPE(V2d, x, y);
PYBIND11_NUMPY_DTYPE(CDT::Triangle, vertices, neighbors);
py::class_<CDT::Triangle>(m, "Triangle")
.def_readwrite("vertices", &CDT::Triangle::vertices)
.def_readwrite("neighbors", &CDT::Triangle::neighbors)
.def(
"__eq__",
[](const CDT::Triangle& lhs, const CDT::Triangle& rhs) {
return std::equal(
std::begin(lhs.vertices),
std::end(lhs.vertices),
std::begin(rhs.vertices)) &&
std::equal(
std::begin(lhs.neighbors),
std::end(lhs.neighbors),
std::begin(rhs.neighbors));
})
.def("__repr__", [](const CDT::Triangle& tri) {
std::ostringstream oss;
const CDT::VerticesArr3 vv = tri.vertices;
const CDT::VerticesArr3 nn = tri.neighbors;
oss << "Triangle(vertices(" << vv[0] << ", " << vv[1] << ", "
<< vv[2] << "), neighbors(" << TriInd2str(nn[0]) << ", "
<< TriInd2str(nn[1]) << ", " << TriInd2str(nn[2]) << "))";
return oss.str();
});
py::class_<CDT::Edge>(m, "Edge", py::buffer_protocol())
.def(
py::init<CDT::VertInd, CDT::VertInd>(),
py::arg("index_vert_a"),
py::arg("index_vert_b"))
.def(py::init([](py::buffer b) {
// Request a buffer descriptor from Python
py::buffer_info info = b.request();
// Some sanity checks ...
if (info.format != py::format_descriptor<CDT::VertInd>::format())
throw std::runtime_error(
"Incompatible format: expected a CDT::VertInd array!");
if (info.ndim != 1)
throw std::runtime_error("Incompatible buffer dimension!");
// create from buffer
const CDT::VertInd* const ptr =
static_cast<CDT::VertInd*>(info.ptr);
return CDT::Edge(ptr[0], ptr[1]);
}))
.def_buffer([](CDT::Edge& e) -> py::buffer_info {
return py::buffer_info(
&e,
sizeof(CDT::VertInd),
py::format_descriptor<coord_t>::format(),
1,
{2},
{sizeof(CDT::VertInd) * 2});
})
.def_property_readonly("v1", &CDT::Edge::v1)
.def_property_readonly("v2", &CDT::Edge::v2)
.def(py::self == py::self)
.def(py::self != py::self)
.def(py::hash(py::self))
.def("__repr__", [](const CDT::Edge& e) {
std::ostringstream oss;
oss << "Edge(" << e.v1() << ", " << e.v2() << ")";
return oss.str();
});
py::class_<Triangulation>(m, "Triangulation")
.def(
py::init<
CDT::VertexInsertionOrder::Enum,
CDT::IntersectingConstraintEdges::Enum,
coord_t>(),
py::arg("vertex_insertion_order"),
py::arg("intersecting_edges_strategy"),
py::arg("min_dist_to_constraint_edge"))
// vertices
.def_readonly("vertices", &Triangulation::vertices)
.def(
"vertices_count",
[](const Triangulation& t) { return t.vertices.size(); })
.def(
"vertices_iter",
[](const Triangulation& t) -> py::iterator {
return py::make_iterator(t.vertices.begin(), t.vertices.end());
},
py::keep_alive<0, 1>())
// triangles
.def_readonly("triangles", &Triangulation::triangles)
.def(
"triangles_count",
[](const Triangulation& t) { return t.triangles.size(); })
.def(
"triangles_iter",
[](const Triangulation& t) -> py::iterator {
return py::make_iterator(
t.triangles.begin(), t.triangles.end());
},
py::keep_alive<0, 1>())
// fixed edges
.def_readonly("fixed_edges", &Triangulation::fixedEdges)
.def(
"fixed_edges_count",
[](const Triangulation& t) { return t.fixedEdges.size(); })
.def(
"fixed_edges_iter",
[](const Triangulation& t) -> py::iterator {
return py::make_iterator(
t.fixedEdges.begin(), t.fixedEdges.end());
},
py::keep_alive<0, 1>())
// overlaps
.def_readonly("overlap_count", &Triangulation::overlapCount)
.def(
"overlap_count_count",
[](const Triangulation& t) { return t.overlapCount.size(); })
.def(
"overlap_count_iter",
[](const Triangulation& t) -> py::iterator {
return py::make_iterator(
t.overlapCount.begin(), t.overlapCount.end());
},
py::keep_alive<0, 1>())
// piece to originals mapping for edges
.def_readonly("piece_to_originals", &Triangulation::pieceToOriginals)
.def(
"piece_to_originals_count",
[](const Triangulation& t) { return t.pieceToOriginals.size(); })
.def(
"piece_to_originals_iter",
[](const Triangulation& t) -> py::iterator {
return py::make_iterator(
t.pieceToOriginals.begin(), t.pieceToOriginals.end());
},
py::keep_alive<0, 1>())
// methods
.def(
"insert_vertices",
static_cast<void (Triangulation::*)(const std::vector<V2d>&)>(
&Triangulation::insertVertices),
py::arg("vertices"))
.def(
"insert_vertices",
[](Triangulation& t, py::buffer b) {
const py::buffer_info info = b.request();
// sanity checks
if (info.format != py::format_descriptor<coord_t>::format())
{
throw std::runtime_error(
"Incompatible format: expected a double array!");
}
if (info.ndim != 1 && info.ndim != 2)
{
throw std::runtime_error("Incompatible buffer dimension!");
}
if (info.size % 2 != 0)
{
throw std::runtime_error("Buffer must hold even number of "
"coordinates (2 per vertex)!");
}
// create from buffer
struct XY
{
coord_t xy[2];
};
const std::size_t n_vert = info.size / 2;
const XY* const ptr = static_cast<XY*>(info.ptr);
t.insertVertices(
ptr,
ptr + n_vert,
[](const XY& v) { return v.xy[0]; },
[](const XY& v) { return v.xy[1]; });
},
py::arg("vertex_buffer"))
.def(
"insert_edges",
static_cast<void (Triangulation::*)(const std::vector<CDT::Edge>&)>(
&Triangulation::insertEdges),
py::arg("edges"))
.def(
"insert_edges",
[](Triangulation& t, py::buffer b) {
const py::buffer_info info = b.request();
// sanity checks
if (info.format !=
py::format_descriptor<CDT::VertInd>::format())
{
throw std::runtime_error(
"Incompatible format: expected a CDT::VertInd array!");
}
if (info.ndim != 1 && info.ndim != 2)
{
throw std::runtime_error("Incompatible buffer dimension!");
}
if (info.size % 2 != 0)
{
throw std::runtime_error("Buffer must hold even number of "
"CDT::VertInd (2 per edge)!");
}
// create from buffer
struct EdgeData
{
CDT::VertInd vv[2];
};
const std::size_t n_vert = info.size / 2;
const EdgeData* const ptr = static_cast<EdgeData*>(info.ptr);
t.insertEdges(
ptr,
ptr + n_vert,
[](const EdgeData& e) { return e.vv[0]; },
[](const EdgeData& e) { return e.vv[1]; });
},
py::arg("edge_buffer"))
.def(
"conform_to_edges",
static_cast<void (Triangulation::*)(const std::vector<CDT::Edge>&)>(
&Triangulation::conformToEdges),
py::arg("edges"))
.def(
"conform_to_edges",
[](Triangulation& t, py::buffer b) {
const py::buffer_info info = b.request();
// sanity checks
if (info.format !=
py::format_descriptor<CDT::VertInd>::format())
{
throw std::runtime_error(
"Incompatible format: expected a CDT::VertInd array!");
}
if (info.ndim != 1 && info.ndim != 2)
{
throw std::runtime_error("Incompatible buffer dimension!");
}
if (info.size % 2 != 0)
{
throw std::runtime_error("Buffer must hold even number of "
"CDT::VertInd (2 per edge)!");
}
// create from buffer
struct EdgeData
{
CDT::VertInd vv[2];
};
const std::size_t n_vert = info.size / 2;
const EdgeData* const ptr = static_cast<EdgeData*>(info.ptr);
t.conformToEdges(
ptr,
ptr + n_vert,
[](const EdgeData& e) { return e.vv[0]; },
[](const EdgeData& e) { return e.vv[1]; });
},
py::arg("edge_buffer"))
.def("erase_super_triangle", &Triangulation::eraseSuperTriangle)
.def("erase_outer_triangles", &Triangulation::eraseOuterTriangles)
.def(
"erase_outer_triangles_and_holes",
&Triangulation::eraseOuterTrianglesAndHoles)
.def("is_finalized", &Triangulation::isFinalized)
.def(
"calculate_triangle_depths",
&Triangulation::calculateTriangleDepths)
.def(
"remove_triangles",
static_cast<void (Triangulation::*)(const CDT::TriIndUSet&)>(
&Triangulation::removeTriangles),
py::arg("triangle_indices"));
m.def(
"verify_topology",
&CDT::verifyTopology<coord_t, NearPointLocator_t>,
py::arg("triangulation"));
}