Skip to content

Commit 79a0c5c

Browse files
committed
add back and clean up straight skeleton
1 parent aceaca6 commit 79a0c5c

File tree

5 files changed

+145
-107
lines changed

5 files changed

+145
-107
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def get_scip_library():
6464
"src/skeletonization.cpp",
6565
"src/reconstruction.cpp",
6666
"src/polygonal_surface_reconstruction.cpp",
67-
# "src/straight_skeleton_2.cpp",
67+
"src/straight_skeleton_2.cpp",
6868
]
6969
),
7070
include_dirs=["./include", get_eigen_include(), get_pybind_include()],

src/compas_cgal.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void init_subdivision(pybind11::module &);
1212
void init_skeletonization(pybind11::module &);
1313
void init_reconstruction(pybind11::module &);
1414
void init_polygonal_surface_reconstruction(pybind11::module &);
15-
// void init_straight_skeleton_2(pybind11::module &);
15+
void init_straight_skeleton_2(pybind11::module &);
1616

1717
// the first parameter here ("_cgal") will be the name of the "so" or "pyd" file that will be produced by PyBind11
1818
// which is the entry point from where all other modules will be accessible.
@@ -31,5 +31,5 @@ PYBIND11_MODULE(_cgal, m)
3131
init_skeletonization(m);
3232
init_reconstruction(m);
3333
init_polygonal_surface_reconstruction(m);
34-
// init_straight_skeleton_2(m);
34+
init_straight_skeleton_2(m);
3535
}

src/straight_skeleton_2.cpp

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ typedef std::vector<PolygonPtr> PolygonPtrVector;
2020
typedef boost::shared_ptr<Polygon_with_holes> PolygonWithHolesPtr;
2121
typedef std::vector<PolygonWithHolesPtr> PolygonWithHolesPtrVector;
2222

23-
std::tuple<compas::RowMatrixXd, std::vector<int>, compas::RowMatrixXi, std::vector<int>> mesh_data_from_skeleton(boost::shared_ptr<Ss> &iss)
23+
std::tuple<compas::RowMatrixXd, std::vector<int>, compas::RowMatrixXi, std::vector<int>>
24+
mesh_data_from_skeleton(boost::shared_ptr<Ss> &iss)
2425
{
2526
std::size_t v = iss->size_of_vertices();
2627
std::size_t e = iss->size_of_halfedges() / 2; // halfedges are stored twice
@@ -71,7 +72,9 @@ std::tuple<compas::RowMatrixXd, std::vector<int>, compas::RowMatrixXi, std::vect
7172
return result;
7273
}
7374

74-
compas::RowMatrixXd polygon_to_data(Polygon_2 const& poly){
75+
compas::RowMatrixXd
76+
polygon_to_data(Polygon_2 const& poly)
77+
{
7578
std::size_t n = poly.size();
7679
compas::RowMatrixXd points(n, 3);
7780
int j = 0;
@@ -84,7 +87,9 @@ compas::RowMatrixXd polygon_to_data(Polygon_2 const& poly){
8487
return points;
8588
}
8689

87-
std::tuple<compas::RowMatrixXd, std::vector<compas::RowMatrixXd>> polygon_with_holes_to_data(Polygon_with_holes const& polywh){
90+
std::tuple<compas::RowMatrixXd, std::vector<compas::RowMatrixXd>>
91+
polygon_with_holes_to_data(Polygon_with_holes const& polywh)
92+
{
8893
std::vector<compas::RowMatrixXd> holes;
8994
compas::RowMatrixXd points = polygon_to_data(polywh.outer_boundary());
9095
for(typename Polygon_with_holes::Hole_const_iterator hi = polywh.holes_begin() ; hi != polywh.holes_end() ; ++ hi){
@@ -95,15 +100,21 @@ std::tuple<compas::RowMatrixXd, std::vector<compas::RowMatrixXd>> polygon_with_h
95100
return result;
96101
}
97102

98-
Polygon_2 data_to_polygon(Eigen::Ref<const compas::RowMatrixXd> &V){
103+
Polygon_2
104+
data_to_polygon(Eigen::Ref<const compas::RowMatrixXd> &V)
105+
{
99106
Polygon_2 poly;
100107
for (int i = 0; i < V.rows(); i++){
101108
poly.push_back(Point(V(i, 0), V(i, 1)));
102109
}
103110
return poly;
104111
}
105112

106-
Polygon_with_holes data_to_polygon_with_holes(Eigen::Ref<const compas::RowMatrixXd> &V, std::vector<Eigen::Ref<const compas::RowMatrixXd>> &holes){
113+
Polygon_with_holes
114+
data_to_polygon_with_holes(
115+
Eigen::Ref<const compas::RowMatrixXd> &V,
116+
std::vector<Eigen::Ref<const compas::RowMatrixXd>> &holes)
117+
{
107118
Polygon_2 outer = data_to_polygon(V);
108119
Polygon_with_holes poly(outer);
109120
for (auto hit : holes){
@@ -118,22 +129,28 @@ Polygon_with_holes data_to_polygon_with_holes(Eigen::Ref<const compas::RowMatrix
118129
return poly;
119130
}
120131

121-
std::tuple<compas::RowMatrixXd, std::vector<int>, compas::RowMatrixXi, std::vector<int>> pmp_create_interior_straight_skeleton(
122-
Eigen::Ref<const compas::RowMatrixXd> &V){
132+
std::tuple<compas::RowMatrixXd, std::vector<int>, compas::RowMatrixXi, std::vector<int>>
133+
pmp_create_interior_straight_skeleton(
134+
Eigen::Ref<const compas::RowMatrixXd> &V)
135+
{
123136
Polygon_2 poly = data_to_polygon(V);
124137
SsPtr iss = CGAL::create_interior_straight_skeleton_2(poly.vertices_begin(), poly.vertices_end());
125138
return mesh_data_from_skeleton(iss);
126139
};
127140

128-
std::tuple<compas::RowMatrixXd, std::vector<int>, compas::RowMatrixXi, std::vector<int>> pmp_create_interior_straight_skeleton_with_holes(
141+
std::tuple<compas::RowMatrixXd, std::vector<int>, compas::RowMatrixXi, std::vector<int>>
142+
pmp_create_interior_straight_skeleton_with_holes(
129143
Eigen::Ref<const compas::RowMatrixXd> &V,
130-
std::vector<Eigen::Ref<const compas::RowMatrixXd>> &holes){
144+
std::vector<Eigen::Ref<const compas::RowMatrixXd>> &holes)
145+
{
131146
Polygon_with_holes poly = data_to_polygon_with_holes(V, holes);
132147
SsPtr iss = CGAL::create_interior_straight_skeleton_2(poly);
133148
return mesh_data_from_skeleton(iss);
134149
}
135150

136-
std::vector<compas::RowMatrixXd> pmp_create_offset_polygons_2_inner(Eigen::Ref<const compas::RowMatrixXd> &V, double &offset){
151+
std::vector<compas::RowMatrixXd>
152+
pmp_create_offset_polygons_2_inner(Eigen::Ref<const compas::RowMatrixXd> &V, double &offset)
153+
{
137154
Polygon_2 poly = data_to_polygon(V);
138155
PolygonPtrVector offset_polygons = CGAL::create_interior_skeleton_and_offset_polygons_2(offset, poly);
139156

@@ -145,7 +162,12 @@ std::vector<compas::RowMatrixXd> pmp_create_offset_polygons_2_inner(Eigen::Ref<c
145162
return result;
146163
}
147164

148-
std::vector<std::tuple<compas::RowMatrixXd, std::vector<compas::RowMatrixXd>>> pmp_create_offset_polygons_2_inner_with_holes(Eigen::Ref<const compas::RowMatrixXd> &V, std::vector<Eigen::Ref<const compas::RowMatrixXd>> &holes, double &offset){
165+
std::vector<std::tuple<compas::RowMatrixXd, std::vector<compas::RowMatrixXd>>>
166+
pmp_create_offset_polygons_2_inner_with_holes(
167+
Eigen::Ref<const compas::RowMatrixXd> &V,
168+
std::vector<Eigen::Ref<const compas::RowMatrixXd>> &holes,
169+
double &offset)
170+
{
149171

150172
Polygon_with_holes poly = data_to_polygon_with_holes(V, holes);
151173
PolygonWithHolesPtrVector offset_polygons = CGAL::create_interior_skeleton_and_offset_polygons_with_holes_2(offset, poly);
@@ -158,7 +180,9 @@ std::vector<std::tuple<compas::RowMatrixXd, std::vector<compas::RowMatrixXd>>> p
158180
return result;
159181
}
160182

161-
std::vector<compas::RowMatrixXd> pmp_create_offset_polygons_2_outer(Eigen::Ref<const compas::RowMatrixXd> &V, double &offset){
183+
std::vector<compas::RowMatrixXd>
184+
pmp_create_offset_polygons_2_outer(Eigen::Ref<const compas::RowMatrixXd> &V, double &offset)
185+
{
162186
Polygon_2 poly = data_to_polygon(V);
163187
PolygonPtrVector offset_polygons = CGAL::create_exterior_skeleton_and_offset_polygons_2(offset, poly);
164188

@@ -170,7 +194,12 @@ std::vector<compas::RowMatrixXd> pmp_create_offset_polygons_2_outer(Eigen::Ref<c
170194
return result;
171195
}
172196

173-
std::vector<std::tuple<compas::RowMatrixXd, std::vector<compas::RowMatrixXd>>> pmp_create_offset_polygons_2_outer_with_holes(Eigen::Ref<const compas::RowMatrixXd> &V, std::vector<Eigen::Ref<const compas::RowMatrixXd>> &holes, double &offset){
197+
std::vector<std::tuple<compas::RowMatrixXd, std::vector<compas::RowMatrixXd>>>
198+
pmp_create_offset_polygons_2_outer_with_holes(
199+
Eigen::Ref<const compas::RowMatrixXd> &V,
200+
std::vector<Eigen::Ref<const compas::RowMatrixXd>> &holes,
201+
double &offset)
202+
{
174203
Polygon_with_holes poly = data_to_polygon_with_holes(V, holes);
175204
PolygonWithHolesPtrVector offset_polygons = CGAL::create_exterior_skeleton_and_offset_polygons_with_holes_2(offset, poly);
176205

@@ -182,7 +211,12 @@ std::vector<std::tuple<compas::RowMatrixXd, std::vector<compas::RowMatrixXd>>> p
182211
return result;
183212
}
184213

185-
std::vector<compas::RowMatrixXd> pmp_create_weighted_offset_polygons_2_inner(Eigen::Ref<const compas::RowMatrixXd> &V, double &offset, Eigen::Ref<const compas::RowMatrixXd> &weights){
214+
std::vector<compas::RowMatrixXd>
215+
pmp_create_weighted_offset_polygons_2_inner(
216+
Eigen::Ref<const compas::RowMatrixXd> &V,
217+
double &offset,
218+
Eigen::Ref<const compas::RowMatrixXd> &weights)
219+
{
186220
Polygon_2 poly = data_to_polygon(V);
187221
std::vector<double> weights_vec;
188222
for (int i = 0; i < weights.rows(); i++)
@@ -200,7 +234,12 @@ std::vector<compas::RowMatrixXd> pmp_create_weighted_offset_polygons_2_inner(Eig
200234
return result;
201235
}
202236

203-
std::vector<compas::RowMatrixXd> pmp_create_weighted_offset_polygons_2_outer(Eigen::Ref<const compas::RowMatrixXd> &V, double &offset, Eigen::Ref<const compas::RowMatrixXd> &weights){
237+
std::vector<compas::RowMatrixXd>
238+
pmp_create_weighted_offset_polygons_2_outer(
239+
Eigen::Ref<const compas::RowMatrixXd> &V,
240+
double &offset,
241+
Eigen::Ref<const compas::RowMatrixXd> &weights)
242+
{
204243
Polygon_2 poly = data_to_polygon(V);
205244
std::vector<double> weights_vec;
206245
for (int i = 0; i < weights.rows(); i++)

src/straight_skeleton_2.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
std::tuple<compas::RowMatrixXd, std::vector<int>, compas::RowMatrixXi, std::vector<int>> pmp_create_interior_straight_skeleton(
88
Eigen::Ref<const compas::RowMatrixXd> &V);
99

10-
1110
std::tuple<compas::RowMatrixXd, std::vector<int>, compas::RowMatrixXi, std::vector<int>> pmp_create_interior_straight_skeleton_with_holes(
1211
Eigen::Ref<const compas::RowMatrixXd> &V,
1312
std::vector<Eigen::Ref<const compas::RowMatrixXd>> &holes);

tests/test_straight_skeleton_2.py

Lines changed: 88 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,96 @@
1-
# from compas_cgal.straight_skeleton_2 import interior_straight_skeleton
2-
# from compas_cgal.straight_skeleton_2 import interior_straight_skeleton_with_holes
3-
# from compas_cgal.straight_skeleton_2 import offset_polygon
4-
# from compas_cgal.straight_skeleton_2 import offset_polygon_with_holes
5-
# from compas_cgal.straight_skeleton_2 import weighted_offset_polygon
1+
from compas_cgal.straight_skeleton_2 import interior_straight_skeleton
2+
from compas_cgal.straight_skeleton_2 import interior_straight_skeleton_with_holes
3+
from compas_cgal.straight_skeleton_2 import offset_polygon
4+
from compas_cgal.straight_skeleton_2 import offset_polygon_with_holes
5+
from compas_cgal.straight_skeleton_2 import weighted_offset_polygon
66

77

8-
# def test_straight_polygon():
9-
# points = [
10-
# (-1, -1, 0),
11-
# (0, -12, 0),
12-
# (1, -1, 0),
13-
# (12, 0, 0),
14-
# (1, 1, 0),
15-
# (0, 12, 0),
16-
# (-1, 1, 0),
17-
# (-12, 0, 0),
18-
# ]
19-
# graph = interior_straight_skeleton(points)
20-
# assert graph.number_of_edges() == 16
8+
def test_straight_polygon():
9+
points = [
10+
(-1, -1, 0),
11+
(0, -12, 0),
12+
(1, -1, 0),
13+
(12, 0, 0),
14+
(1, 1, 0),
15+
(0, 12, 0),
16+
(-1, 1, 0),
17+
(-12, 0, 0),
18+
]
19+
graph = interior_straight_skeleton(points)
20+
assert graph.number_of_edges() == 16
2121

2222

23-
# def test_straight_skeleton_with_holes():
24-
# points = [
25-
# (-1, -1, 0),
26-
# (0, -12, 0),
27-
# (1, -1, 0),
28-
# (12, 0, 0),
29-
# (1, 1, 0),
30-
# (0, 12, 0),
31-
# (-1, 1, 0),
32-
# (-12, 0, 0),
33-
# ]
34-
# hole = [(-1, 0, 0), (0, 1, 0), (1, 0, 0), (0, -1, 0)]
35-
# graph = interior_straight_skeleton_with_holes(points, [hole])
36-
# assert graph.number_of_edges() == 32
23+
def test_straight_skeleton_with_holes():
24+
points = [
25+
(-1, -1, 0),
26+
(0, -12, 0),
27+
(1, -1, 0),
28+
(12, 0, 0),
29+
(1, 1, 0),
30+
(0, 12, 0),
31+
(-1, 1, 0),
32+
(-12, 0, 0),
33+
]
34+
hole = [(-1, 0, 0), (0, 1, 0), (1, 0, 0), (0, -1, 0)]
35+
graph = interior_straight_skeleton_with_holes(points, [hole])
36+
assert graph.number_of_edges() == 32
3737

3838

39-
# def test_offset():
40-
# points = [
41-
# (-1, -1, 0),
42-
# (0, -12, 0),
43-
# (1, -1, 0),
44-
# (12, 0, 0),
45-
# (1, 1, 0),
46-
# (0, 12, 0),
47-
# (-1, 1, 0),
48-
# (-12, 0, 0),
49-
# ]
50-
# offset = 0.5
51-
# polygons = offset_polygon(points, offset)
52-
# assert len(polygons) == 1, len(polygons)
53-
# polygons = offset_polygon(points, -offset)
54-
# assert len(polygons) == 1, len(polygons)
55-
# weights = [0.1, 0.5, 0.3, 0.3, 0.9, 1.0, 0.2, 1.0]
56-
# polygons = weighted_offset_polygon(points, offset, weights)
57-
# assert len(polygons) == 1, len(polygons)
58-
# polygons = weighted_offset_polygon(points, -offset, weights)
59-
# assert len(polygons) == 1, len(polygons)
39+
def test_offset():
40+
points = [
41+
(-1, -1, 0),
42+
(0, -12, 0),
43+
(1, -1, 0),
44+
(12, 0, 0),
45+
(1, 1, 0),
46+
(0, 12, 0),
47+
(-1, 1, 0),
48+
(-12, 0, 0),
49+
]
50+
offset = 0.5
51+
polygons = offset_polygon(points, offset)
52+
assert len(polygons) == 1, len(polygons)
53+
polygons = offset_polygon(points, -offset)
54+
assert len(polygons) == 1, len(polygons)
55+
weights = [0.1, 0.5, 0.3, 0.3, 0.9, 1.0, 0.2, 1.0]
56+
polygons = weighted_offset_polygon(points, offset, weights)
57+
assert len(polygons) == 1, len(polygons)
58+
polygons = weighted_offset_polygon(points, -offset, weights)
59+
assert len(polygons) == 1, len(polygons)
6060

6161

62-
# def test_offset_with_holes():
63-
# points = [
64-
# (65.322, -16.156, 0.0),
65-
# (65.322, -11.157, 0.0),
66-
# (63.022, -11.157, 0.0),
67-
# (63.022, -11.167, 0.0),
68-
# (60.042, -11.167, 0.0),
69-
# (60.042, -16.156, 0.0),
70-
# (61.702, -16.156, 0.0),
71-
# (61.702, -16.416, 0.0),
72-
# (61.712, -16.416, 0.0),
73-
# (61.712, -16.156, 0.0),
74-
# ]
75-
# holes = [
76-
# [
77-
# (63.912, -14.956, 0.0),
78-
# (63.652, -14.956, 0.0),
79-
# (63.652, -14.946, 0.0),
80-
# (61.442, -14.946, 0.0),
81-
# (61.442, -12.367, 0.0),
82-
# (61.702, -12.367, 0.0),
83-
# (61.702, -12.377, 0.0),
84-
# (63.652, -12.377, 0.0),
85-
# (63.652, -12.367, 0.0),
86-
# (63.912, -12.367, 0.0),
87-
# (63.912, -12.834, 0.0),
88-
# ],
89-
# [(61.452, -14.946, 0.0), (61.532, -14.949, 0.0), (61.702, -14.956, 0.0), (61.532, -14.956, 0.0), (61.452, -14.956, 0.0)],
90-
# ]
91-
# result = offset_polygon_with_holes(points, holes, -0.2)
92-
# assert len(result) == 1, len(result)
93-
# polygon, holes = result[0]
94-
# assert len(holes) == 1, len(holes)
95-
# area_net = polygon.area - sum(h.area for h in holes)
96-
# assert abs(area_net - 26.25329) < 1e-3, area_net
62+
def test_offset_with_holes():
63+
points = [
64+
(65.322, -16.156, 0.0),
65+
(65.322, -11.157, 0.0),
66+
(63.022, -11.157, 0.0),
67+
(63.022, -11.167, 0.0),
68+
(60.042, -11.167, 0.0),
69+
(60.042, -16.156, 0.0),
70+
(61.702, -16.156, 0.0),
71+
(61.702, -16.416, 0.0),
72+
(61.712, -16.416, 0.0),
73+
(61.712, -16.156, 0.0),
74+
]
75+
holes = [
76+
[
77+
(63.912, -14.956, 0.0),
78+
(63.652, -14.956, 0.0),
79+
(63.652, -14.946, 0.0),
80+
(61.442, -14.946, 0.0),
81+
(61.442, -12.367, 0.0),
82+
(61.702, -12.367, 0.0),
83+
(61.702, -12.377, 0.0),
84+
(63.652, -12.377, 0.0),
85+
(63.652, -12.367, 0.0),
86+
(63.912, -12.367, 0.0),
87+
(63.912, -12.834, 0.0),
88+
],
89+
[(61.452, -14.946, 0.0), (61.532, -14.949, 0.0), (61.702, -14.956, 0.0), (61.532, -14.956, 0.0), (61.452, -14.956, 0.0)],
90+
]
91+
result = offset_polygon_with_holes(points, holes, -0.2)
92+
assert len(result) == 1, len(result)
93+
polygon, holes = result[0]
94+
assert len(holes) == 1, len(holes)
95+
area_net = polygon.area - sum(h.area for h in holes)
96+
assert abs(area_net - 26.25329) < 1e-3, area_net

0 commit comments

Comments
 (0)