Skip to content

Commit

Permalink
add back and clean up straight skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvanmele committed Oct 29, 2024
1 parent aceaca6 commit 79a0c5c
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 107 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def get_scip_library():
"src/skeletonization.cpp",
"src/reconstruction.cpp",
"src/polygonal_surface_reconstruction.cpp",
# "src/straight_skeleton_2.cpp",
"src/straight_skeleton_2.cpp",
]
),
include_dirs=["./include", get_eigen_include(), get_pybind_include()],
Expand Down
4 changes: 2 additions & 2 deletions src/compas_cgal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void init_subdivision(pybind11::module &);
void init_skeletonization(pybind11::module &);
void init_reconstruction(pybind11::module &);
void init_polygonal_surface_reconstruction(pybind11::module &);
// void init_straight_skeleton_2(pybind11::module &);
void init_straight_skeleton_2(pybind11::module &);

// the first parameter here ("_cgal") will be the name of the "so" or "pyd" file that will be produced by PyBind11
// which is the entry point from where all other modules will be accessible.
Expand All @@ -31,5 +31,5 @@ PYBIND11_MODULE(_cgal, m)
init_skeletonization(m);
init_reconstruction(m);
init_polygonal_surface_reconstruction(m);
// init_straight_skeleton_2(m);
init_straight_skeleton_2(m);
}
69 changes: 54 additions & 15 deletions src/straight_skeleton_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ typedef std::vector<PolygonPtr> PolygonPtrVector;
typedef boost::shared_ptr<Polygon_with_holes> PolygonWithHolesPtr;
typedef std::vector<PolygonWithHolesPtr> PolygonWithHolesPtrVector;

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

compas::RowMatrixXd polygon_to_data(Polygon_2 const& poly){
compas::RowMatrixXd
polygon_to_data(Polygon_2 const& poly)
{
std::size_t n = poly.size();
compas::RowMatrixXd points(n, 3);
int j = 0;
Expand All @@ -84,7 +87,9 @@ compas::RowMatrixXd polygon_to_data(Polygon_2 const& poly){
return points;
}

std::tuple<compas::RowMatrixXd, std::vector<compas::RowMatrixXd>> polygon_with_holes_to_data(Polygon_with_holes const& polywh){
std::tuple<compas::RowMatrixXd, std::vector<compas::RowMatrixXd>>
polygon_with_holes_to_data(Polygon_with_holes const& polywh)
{
std::vector<compas::RowMatrixXd> holes;
compas::RowMatrixXd points = polygon_to_data(polywh.outer_boundary());
for(typename Polygon_with_holes::Hole_const_iterator hi = polywh.holes_begin() ; hi != polywh.holes_end() ; ++ hi){
Expand All @@ -95,15 +100,21 @@ std::tuple<compas::RowMatrixXd, std::vector<compas::RowMatrixXd>> polygon_with_h
return result;
}

Polygon_2 data_to_polygon(Eigen::Ref<const compas::RowMatrixXd> &V){
Polygon_2
data_to_polygon(Eigen::Ref<const compas::RowMatrixXd> &V)
{
Polygon_2 poly;
for (int i = 0; i < V.rows(); i++){
poly.push_back(Point(V(i, 0), V(i, 1)));
}
return poly;
}

Polygon_with_holes data_to_polygon_with_holes(Eigen::Ref<const compas::RowMatrixXd> &V, std::vector<Eigen::Ref<const compas::RowMatrixXd>> &holes){
Polygon_with_holes
data_to_polygon_with_holes(
Eigen::Ref<const compas::RowMatrixXd> &V,
std::vector<Eigen::Ref<const compas::RowMatrixXd>> &holes)
{
Polygon_2 outer = data_to_polygon(V);
Polygon_with_holes poly(outer);
for (auto hit : holes){
Expand All @@ -118,22 +129,28 @@ Polygon_with_holes data_to_polygon_with_holes(Eigen::Ref<const compas::RowMatrix
return poly;
}

std::tuple<compas::RowMatrixXd, std::vector<int>, compas::RowMatrixXi, std::vector<int>> pmp_create_interior_straight_skeleton(
Eigen::Ref<const compas::RowMatrixXd> &V){
std::tuple<compas::RowMatrixXd, std::vector<int>, compas::RowMatrixXi, std::vector<int>>
pmp_create_interior_straight_skeleton(
Eigen::Ref<const compas::RowMatrixXd> &V)
{
Polygon_2 poly = data_to_polygon(V);
SsPtr iss = CGAL::create_interior_straight_skeleton_2(poly.vertices_begin(), poly.vertices_end());
return mesh_data_from_skeleton(iss);
};

std::tuple<compas::RowMatrixXd, std::vector<int>, compas::RowMatrixXi, std::vector<int>> pmp_create_interior_straight_skeleton_with_holes(
std::tuple<compas::RowMatrixXd, std::vector<int>, compas::RowMatrixXi, std::vector<int>>
pmp_create_interior_straight_skeleton_with_holes(
Eigen::Ref<const compas::RowMatrixXd> &V,
std::vector<Eigen::Ref<const compas::RowMatrixXd>> &holes){
std::vector<Eigen::Ref<const compas::RowMatrixXd>> &holes)
{
Polygon_with_holes poly = data_to_polygon_with_holes(V, holes);
SsPtr iss = CGAL::create_interior_straight_skeleton_2(poly);
return mesh_data_from_skeleton(iss);
}

std::vector<compas::RowMatrixXd> pmp_create_offset_polygons_2_inner(Eigen::Ref<const compas::RowMatrixXd> &V, double &offset){
std::vector<compas::RowMatrixXd>
pmp_create_offset_polygons_2_inner(Eigen::Ref<const compas::RowMatrixXd> &V, double &offset)
{
Polygon_2 poly = data_to_polygon(V);
PolygonPtrVector offset_polygons = CGAL::create_interior_skeleton_and_offset_polygons_2(offset, poly);

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

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){
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)
{

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

std::vector<compas::RowMatrixXd> pmp_create_offset_polygons_2_outer(Eigen::Ref<const compas::RowMatrixXd> &V, double &offset){
std::vector<compas::RowMatrixXd>
pmp_create_offset_polygons_2_outer(Eigen::Ref<const compas::RowMatrixXd> &V, double &offset)
{
Polygon_2 poly = data_to_polygon(V);
PolygonPtrVector offset_polygons = CGAL::create_exterior_skeleton_and_offset_polygons_2(offset, poly);

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

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){
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)
{
Polygon_with_holes poly = data_to_polygon_with_holes(V, holes);
PolygonWithHolesPtrVector offset_polygons = CGAL::create_exterior_skeleton_and_offset_polygons_with_holes_2(offset, poly);

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

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){
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)
{
Polygon_2 poly = data_to_polygon(V);
std::vector<double> weights_vec;
for (int i = 0; i < weights.rows(); i++)
Expand All @@ -200,7 +234,12 @@ std::vector<compas::RowMatrixXd> pmp_create_weighted_offset_polygons_2_inner(Eig
return result;
}

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){
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)
{
Polygon_2 poly = data_to_polygon(V);
std::vector<double> weights_vec;
for (int i = 0; i < weights.rows(); i++)
Expand Down
1 change: 0 additions & 1 deletion src/straight_skeleton_2.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
std::tuple<compas::RowMatrixXd, std::vector<int>, compas::RowMatrixXi, std::vector<int>> pmp_create_interior_straight_skeleton(
Eigen::Ref<const compas::RowMatrixXd> &V);


std::tuple<compas::RowMatrixXd, std::vector<int>, compas::RowMatrixXi, std::vector<int>> pmp_create_interior_straight_skeleton_with_holes(
Eigen::Ref<const compas::RowMatrixXd> &V,
std::vector<Eigen::Ref<const compas::RowMatrixXd>> &holes);
Expand Down
176 changes: 88 additions & 88 deletions tests/test_straight_skeleton_2.py
Original file line number Diff line number Diff line change
@@ -1,96 +1,96 @@
# from compas_cgal.straight_skeleton_2 import interior_straight_skeleton
# from compas_cgal.straight_skeleton_2 import interior_straight_skeleton_with_holes
# from compas_cgal.straight_skeleton_2 import offset_polygon
# from compas_cgal.straight_skeleton_2 import offset_polygon_with_holes
# from compas_cgal.straight_skeleton_2 import weighted_offset_polygon
from compas_cgal.straight_skeleton_2 import interior_straight_skeleton
from compas_cgal.straight_skeleton_2 import interior_straight_skeleton_with_holes
from compas_cgal.straight_skeleton_2 import offset_polygon
from compas_cgal.straight_skeleton_2 import offset_polygon_with_holes
from compas_cgal.straight_skeleton_2 import weighted_offset_polygon


# def test_straight_polygon():
# points = [
# (-1, -1, 0),
# (0, -12, 0),
# (1, -1, 0),
# (12, 0, 0),
# (1, 1, 0),
# (0, 12, 0),
# (-1, 1, 0),
# (-12, 0, 0),
# ]
# graph = interior_straight_skeleton(points)
# assert graph.number_of_edges() == 16
def test_straight_polygon():
points = [
(-1, -1, 0),
(0, -12, 0),
(1, -1, 0),
(12, 0, 0),
(1, 1, 0),
(0, 12, 0),
(-1, 1, 0),
(-12, 0, 0),
]
graph = interior_straight_skeleton(points)
assert graph.number_of_edges() == 16


# def test_straight_skeleton_with_holes():
# points = [
# (-1, -1, 0),
# (0, -12, 0),
# (1, -1, 0),
# (12, 0, 0),
# (1, 1, 0),
# (0, 12, 0),
# (-1, 1, 0),
# (-12, 0, 0),
# ]
# hole = [(-1, 0, 0), (0, 1, 0), (1, 0, 0), (0, -1, 0)]
# graph = interior_straight_skeleton_with_holes(points, [hole])
# assert graph.number_of_edges() == 32
def test_straight_skeleton_with_holes():
points = [
(-1, -1, 0),
(0, -12, 0),
(1, -1, 0),
(12, 0, 0),
(1, 1, 0),
(0, 12, 0),
(-1, 1, 0),
(-12, 0, 0),
]
hole = [(-1, 0, 0), (0, 1, 0), (1, 0, 0), (0, -1, 0)]
graph = interior_straight_skeleton_with_holes(points, [hole])
assert graph.number_of_edges() == 32


# def test_offset():
# points = [
# (-1, -1, 0),
# (0, -12, 0),
# (1, -1, 0),
# (12, 0, 0),
# (1, 1, 0),
# (0, 12, 0),
# (-1, 1, 0),
# (-12, 0, 0),
# ]
# offset = 0.5
# polygons = offset_polygon(points, offset)
# assert len(polygons) == 1, len(polygons)
# polygons = offset_polygon(points, -offset)
# assert len(polygons) == 1, len(polygons)
# weights = [0.1, 0.5, 0.3, 0.3, 0.9, 1.0, 0.2, 1.0]
# polygons = weighted_offset_polygon(points, offset, weights)
# assert len(polygons) == 1, len(polygons)
# polygons = weighted_offset_polygon(points, -offset, weights)
# assert len(polygons) == 1, len(polygons)
def test_offset():
points = [
(-1, -1, 0),
(0, -12, 0),
(1, -1, 0),
(12, 0, 0),
(1, 1, 0),
(0, 12, 0),
(-1, 1, 0),
(-12, 0, 0),
]
offset = 0.5
polygons = offset_polygon(points, offset)
assert len(polygons) == 1, len(polygons)
polygons = offset_polygon(points, -offset)
assert len(polygons) == 1, len(polygons)
weights = [0.1, 0.5, 0.3, 0.3, 0.9, 1.0, 0.2, 1.0]
polygons = weighted_offset_polygon(points, offset, weights)
assert len(polygons) == 1, len(polygons)
polygons = weighted_offset_polygon(points, -offset, weights)
assert len(polygons) == 1, len(polygons)


# def test_offset_with_holes():
# points = [
# (65.322, -16.156, 0.0),
# (65.322, -11.157, 0.0),
# (63.022, -11.157, 0.0),
# (63.022, -11.167, 0.0),
# (60.042, -11.167, 0.0),
# (60.042, -16.156, 0.0),
# (61.702, -16.156, 0.0),
# (61.702, -16.416, 0.0),
# (61.712, -16.416, 0.0),
# (61.712, -16.156, 0.0),
# ]
# holes = [
# [
# (63.912, -14.956, 0.0),
# (63.652, -14.956, 0.0),
# (63.652, -14.946, 0.0),
# (61.442, -14.946, 0.0),
# (61.442, -12.367, 0.0),
# (61.702, -12.367, 0.0),
# (61.702, -12.377, 0.0),
# (63.652, -12.377, 0.0),
# (63.652, -12.367, 0.0),
# (63.912, -12.367, 0.0),
# (63.912, -12.834, 0.0),
# ],
# [(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)],
# ]
# result = offset_polygon_with_holes(points, holes, -0.2)
# assert len(result) == 1, len(result)
# polygon, holes = result[0]
# assert len(holes) == 1, len(holes)
# area_net = polygon.area - sum(h.area for h in holes)
# assert abs(area_net - 26.25329) < 1e-3, area_net
def test_offset_with_holes():
points = [
(65.322, -16.156, 0.0),
(65.322, -11.157, 0.0),
(63.022, -11.157, 0.0),
(63.022, -11.167, 0.0),
(60.042, -11.167, 0.0),
(60.042, -16.156, 0.0),
(61.702, -16.156, 0.0),
(61.702, -16.416, 0.0),
(61.712, -16.416, 0.0),
(61.712, -16.156, 0.0),
]
holes = [
[
(63.912, -14.956, 0.0),
(63.652, -14.956, 0.0),
(63.652, -14.946, 0.0),
(61.442, -14.946, 0.0),
(61.442, -12.367, 0.0),
(61.702, -12.367, 0.0),
(61.702, -12.377, 0.0),
(63.652, -12.377, 0.0),
(63.652, -12.367, 0.0),
(63.912, -12.367, 0.0),
(63.912, -12.834, 0.0),
],
[(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)],
]
result = offset_polygon_with_holes(points, holes, -0.2)
assert len(result) == 1, len(result)
polygon, holes = result[0]
assert len(holes) == 1, len(holes)
area_net = polygon.area - sum(h.area for h in holes)
assert abs(area_net - 26.25329) < 1e-3, area_net

0 comments on commit 79a0c5c

Please sign in to comment.