-
Notifications
You must be signed in to change notification settings - Fork 1
/
skeleton.cpp
208 lines (183 loc) · 7.46 KB
/
skeleton.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
#include <vector>
#include <algorithm>
#include <cpp11/declarations.hpp>
#include <CGAL/create_offset_polygons_from_polygon_with_holes_2.h>
#include <CGAL/create_straight_skeleton_from_polygon_with_holes_2.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Cartesian_converter.h>
#include <euclid.h>
#include <polyclid.h>
#include <boost/shared_ptr.hpp>
typedef CGAL::Exact_predicates_inexact_constructions_kernel I_Kernel;
typedef CGAL::Cartesian_converter<I_Kernel,Kernel> I_to_E;
typedef CGAL::Cartesian_converter<Kernel,I_Kernel> E_to_I;
typedef CGAL::Polygon_with_holes_2<I_Kernel> I_Polygon;
I_Polygon exact_to_inexact_poly(const Polygon& poly) {
static E_to_I converter;
I_Polygon ipoly;
std::vector<I_Kernel::Point_2> ring;
if (!poly.is_unbounded()) {
std::transform(poly.outer_boundary().vertices_begin(), poly.outer_boundary().vertices_end(),
std::back_inserter(ring),
[](const Point_2& p) { return converter(p); });
ipoly = I_Polygon(CGAL::Polygon_2<I_Kernel>(ring.begin(), ring.end()));
}
for (auto iter = poly.holes_begin(); iter != poly.holes_end(); iter++) {
ring.clear();
std::transform(iter->vertices_begin(), iter->vertices_end(),
std::back_inserter(ring),
[](const Point_2& p) { return converter(p); });
ipoly.add_hole(CGAL::Polygon_2<I_Kernel>(ring.begin(), ring.end()));
}
return ipoly;
}
Polygon inexact_to_exact_poly(const I_Polygon& ipoly) {
static I_to_E converter;
Polygon poly;
std::vector<Point_2> ring;
if (!ipoly.is_unbounded()) {
std::transform(ipoly.outer_boundary().vertices_begin(), ipoly.outer_boundary().vertices_end(),
std::back_inserter(ring),
[](const I_Kernel::Point_2& p) { return converter(p); });
poly = Polygon(Segment_trait::Polygon_2(ring.begin(), ring.end()));
}
for (auto iter = ipoly.holes_begin(); iter != ipoly.holes_end(); iter++) {
ring.clear();
std::transform(iter->vertices_begin(), iter->vertices_end(),
std::back_inserter(ring),
[](const I_Kernel::Point_2& p) { return converter(p); });
poly.add_hole(Segment_trait::Polygon_2(ring.begin(), ring.end()));
}
return poly;
}
[[cpp11::register]]
SEXP polygon_skeleton_offset(SEXP polygons, SEXP offset) {
std::vector<Polygon> poly = polyclid::get_polygon_vec(polygons);
std::vector<Exact_number> os = euclid::get_exact_numeric_vec(offset);
size_t max_size = std::max(poly.size(), os.size());
std::vector<Polygon_set> res;
res.reserve(max_size);
E_to_I converter;
for (size_t i = 0; i < max_size; ++i) {
Polygon p = poly[i % poly.size()];
Exact_number of = os[i % os.size()];
if (!(p.get_flag(VALIDITY_CHECKED) && p.get_flag(IS_VALID))) {
cpp11::stop("Input polygons must be valid");
}
if (p.is_na() || of.is_na()) {
res.push_back(Polygon_set::NA_value());
continue;
}
std::vector< boost::shared_ptr<I_Polygon> > offset;
if (of > 0) {
offset = CGAL::create_exterior_skeleton_and_offset_polygons_with_holes_2(converter(of), exact_to_inexact_poly(p));
} else {
offset = CGAL::create_interior_skeleton_and_offset_polygons_with_holes_2(converter(-of), exact_to_inexact_poly(p));
}
Polygon_set offsetted_polygon;
res.push_back(offsetted_polygon);
for (auto iter = offset.begin(); iter != offset.end(); iter++) {
res.back().insert(inexact_to_exact_poly(*iter->get()));
}
}
return polyclid::create_polygon_set_vec(res);
}
[[cpp11::register]]
SEXP polygon_skeleton_polylineset(SEXP polygons, bool keep_boundary, bool only_inner) {
std::vector<Polygon> poly = polyclid::get_polygon_vec(polygons);
std::vector<Polyline_set> res;
res.reserve(poly.size());
I_to_E converter;
for (size_t i = 0; i < poly.size(); ++i) {
Polygon p = poly[i];
if (!(p.get_flag(VALIDITY_CHECKED) && p.get_flag(IS_VALID))) {
cpp11::stop("Input polygons must be valid");
}
if (p.is_na() || p.is_unbounded()) {
res.push_back(Polyline_set::NA_value());
continue;
}
boost::shared_ptr< CGAL::Straight_skeleton_2<I_Kernel> > skeleton = CGAL::create_interior_straight_skeleton_2(exact_to_inexact_poly(p));
Polyline_set skeleton_lines;
res.push_back(skeleton_lines);
for (auto iter = skeleton->halfedges_begin(); iter != skeleton->halfedges_end(); iter++) {
if ((iter->id()%2)==0) {
if (!keep_boundary && !iter->is_bisector()) {
continue;
}
if (only_inner && !iter->is_inner_bisector()) {
continue;
}
res.back().insert_non_overlapping(Segment_2(converter(iter->vertex()->point()), converter(iter->opposite()->vertex()->point())));
}
}
}
return polyclid::create_polyline_set_vec(res);
}
[[cpp11::register]]
SEXP polygon_skeleton_limit(SEXP polygons) {
std::vector<Polygon> poly = polyclid::get_polygon_vec(polygons);
std::vector<Exact_number> res;
res.reserve(poly.size());
for (size_t i = 0; i < poly.size(); ++i) {
Polygon p = poly[i];
if (!(p.get_flag(VALIDITY_CHECKED) && p.get_flag(IS_VALID))) {
cpp11::stop("Input polygons must be valid");
}
if (p.is_na() || p.is_unbounded()) {
res.push_back(Exact_number::NA_value());
continue;
}
boost::shared_ptr< CGAL::Straight_skeleton_2<I_Kernel> > skeleton = CGAL::create_interior_straight_skeleton_2(exact_to_inexact_poly(p));
auto max_time = skeleton->vertices_begin()->time();
for (auto iter = skeleton->vertices_begin(); iter != skeleton->vertices_end(); iter++) {
if (iter->is_skeleton() && iter->time() > max_time) {
max_time = iter->time();
}
}
res.push_back(max_time);
}
return euclid::create_exact_numeric_vec(res);
}
[[cpp11::register]]
SEXP polygon_skeleton_limit_location(SEXP polygons) {
std::vector<Polygon> poly = polyclid::get_polygon_vec(polygons);
std::vector<Polyline_set> res;
res.reserve(poly.size());
I_to_E converter;
for (size_t i = 0; i < poly.size(); ++i) {
Polygon p = poly[i];
if (!(p.get_flag(VALIDITY_CHECKED) && p.get_flag(IS_VALID))) {
cpp11::stop("Input polygons must be valid");
}
if (p.is_na() || p.is_unbounded()) {
res.push_back(Polyline_set::NA_value());
continue;
}
boost::shared_ptr< CGAL::Straight_skeleton_2<I_Kernel> > skeleton = CGAL::create_interior_straight_skeleton_2(exact_to_inexact_poly(p));
auto max_time = skeleton->vertices_begin()->time();
for (auto iter = skeleton->vertices_begin(); iter != skeleton->vertices_end(); iter++) {
if (iter->is_skeleton() && iter->time() > max_time) {
max_time = iter->time();
}
}
res.push_back(Polyline_set());
for (auto iter = skeleton->halfedges_begin(); iter != skeleton->halfedges_end(); iter++) {
if ((iter->id()%2)==0 && iter->is_bisector()) {
bool prim_include = iter->vertex()->time() == max_time;
bool sec_include = iter->opposite()->vertex()->time() == max_time;
if (prim_include && sec_include) {
res.back().insert_non_overlapping(Segment_2(converter(iter->vertex()->point()), converter(iter->opposite()->vertex()->point())));
} else {
if (prim_include) {
CGAL::insert_point(res.back(), converter(iter->vertex()->point()));
}
if (sec_include) {
CGAL::insert_point(res.back(), converter(iter->opposite()->vertex()->point()));
}
}
}
}
}
return polyclid::create_polyline_set_vec(res);
}