From 418b4c2dc4418ead5050803a933ab21da3b17ef7 Mon Sep 17 00:00:00 2001 From: Romana Rust Date: Tue, 1 Oct 2024 13:21:43 +0200 Subject: [PATCH 1/4] create_offset_polygons_with_holes_2 --- CHANGELOG.md | 1 + src/compas_cgal/straight_skeleton_2.py | 57 ++++++++ src/straight_skeleton_2.cpp | 180 ++++++++++++++----------- src/straight_skeleton_2.h | 10 ++ tests/test_straight_skeleton_2.py | 38 ++++++ 5 files changed, 206 insertions(+), 80 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7022e20..784bb41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Added recipe hasher. * Added `scip` to dev install instructions in README.md +* Added `compas_cgal.straight_skeleton_2.create_offset_polygons_with_holes_2`. ### Changed diff --git a/src/compas_cgal/straight_skeleton_2.py b/src/compas_cgal/straight_skeleton_2.py index 3da4649..e004238 100644 --- a/src/compas_cgal/straight_skeleton_2.py +++ b/src/compas_cgal/straight_skeleton_2.py @@ -155,6 +155,63 @@ def create_offset_polygons_2(points, offset) -> list[Polygon]: return [Polygon(points.tolist()) for points in offset_polygons] +def create_offset_polygons_with_holes_2(points, holes, offset) -> list[Tuple[Polygon, list[Polygon]]]: + """Compute the polygon offset with holes. + + Parameters + ---------- + points : list of point coordinates or :class:`compas.geometry.Polygon` + The points of the polygon. + holes : list of list of point coordinates or list of :class:`compas.geometry.Polygon` + The holes of the polygon. + offset : float + The offset distance. If negative, the offset is outside the polygon, otherwise inside. + + Returns + ------- + + Returns + ------- + list of tuple of (:class:`Polygon`, list[:class:`Polygon`]) + The polygons with holes. + + Raises + ------ + ValueError + If the normal of the polygon is not [0, 0, 1]. + If the normal of a hole is not [0, 0, -1]. + """ + points = list(points) + normal = normal_polygon(points, True) + if not TOL.is_allclose(normal, [0, 0, 1]): + raise ValueError("The normal of the polygon should be [0, 0, 1]. The normal of the provided polygon is {}".format(normal)) + V = np.asarray(points, dtype=np.float64) + + H = [] + for i, hole in enumerate(holes): + points = list(hole) + normal_hole = normal_polygon(points, True) + if not TOL.is_allclose(normal_hole, [0, 0, -1]): + raise ValueError("The normal of the hole should be [0, 0, -1]. The normal of the provided {}-th hole is {}".format(i, normal_hole)) + hole = np.asarray(points, dtype=np.float64) + H.append(hole) + + offset = float(offset) + if offset < 0: # outside + offset_polygons = straight_skeleton_2.create_offset_polygons_2_outer_with_holes(V, H, abs(offset)) + else: # inside + offset_polygons = straight_skeleton_2.create_offset_polygons_2_inner_with_holes(V, H, offset) + + result = [] + for points, holes_np in offset_polygons: + polygon = Polygon(points.tolist()) + holes = [] + for hole in holes_np: + holes.append(Polygon(hole.tolist())) + result.append((polygon, holes)) + return result + + def create_weighted_offset_polygons_2(points, offset, weights) -> list[Polygon]: """Compute the polygon offset with weights. diff --git a/src/straight_skeleton_2.cpp b/src/straight_skeleton_2.cpp index 95b04a8..78f09dc 100644 --- a/src/straight_skeleton_2.cpp +++ b/src/straight_skeleton_2.cpp @@ -6,6 +6,7 @@ #include #include #include +#include typedef CGAL::Exact_predicates_inexact_constructions_kernel K; typedef K::Point_2 Point; @@ -17,6 +18,8 @@ typedef CGAL::Straight_skeleton_2::Halfedge_const_handle Halfedge_const_handl typedef CGAL::Straight_skeleton_2::Vertex_const_handle Vertex_const_handle; typedef boost::shared_ptr PolygonPtr; typedef std::vector PolygonPtrVector; +typedef boost::shared_ptr PolygonWithHolesPtr; +typedef std::vector PolygonWithHolesPtrVector; std::tuple, compas::RowMatrixXi, std::vector> mesh_data_from_skeleton(boost::shared_ptr &iss){ @@ -62,97 +65,119 @@ std::tuple, compas::RowMatrixXi, std::vect return result; } -std::tuple, compas::RowMatrixXi, std::vector> pmp_create_interior_straight_skeleton( - Eigen::Ref &V) -{ - Polygon_2 poly; - for (int i = 0; i < V.rows(); i++) - { - poly.push_back(Point(V(i, 0), V(i, 1))); +compas::RowMatrixXd polygon_to_data(Polygon_2 const& poly){ + std::size_t n = poly.size(); + compas::RowMatrixXd points(n, 3); + int j = 0; + for(typename Polygon_2::Vertex_const_iterator vi = poly.vertices_begin() ; vi != poly.vertices_end() ; ++ vi){ + points(j, 0) = (double)(*vi).x(); + points(j, 1) = (double)(*vi).y(); + points(j, 2) = 0; + j++; } - SsPtr iss = CGAL::create_interior_straight_skeleton_2(poly.vertices_begin(), poly.vertices_end()); + return points; +} - return mesh_data_from_skeleton(iss); -}; +std::tuple> polygon_with_holes_to_data(Polygon_with_holes const& polywh){ + std::vector 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){ + compas::RowMatrixXd hole = polygon_to_data(*hi); + holes.push_back(hole); + } + std::tuple> result = std::make_tuple(points, holes); + return result; +} -std::tuple, compas::RowMatrixXi, std::vector> pmp_create_interior_straight_skeleton_with_holes( - Eigen::Ref &V, - std::vector> &holes) -{ - Polygon_2 outer; - for (int i = 0; i < V.rows(); i++) - { - outer.push_back(Point(V(i, 0), V(i, 1))); +Polygon_2 data_to_polygon(Eigen::Ref &V){ + Polygon_2 poly; + for (int i = 0; i < V.rows(); i++){ + poly.push_back(Point(V(i, 0), V(i, 1))); } - Polygon_with_holes poly(outer); + return poly; +} - for (auto hit : holes) - { +Polygon_with_holes data_to_polygon_with_holes(Eigen::Ref &V, std::vector> &holes){ + Polygon_2 outer = data_to_polygon(V); + Polygon_with_holes poly(outer); + for (auto hit : holes){ compas::RowMatrixXd H = hit; + //Polygon_2 hole = data_to_polygon(*H); // why does this not work? Polygon_2 hole; - for (int i = 0; i < H.rows(); i++) - { + for (int i = 0; i < H.rows(); i++){ hole.push_back(Point(H(i, 0), H(i, 1))); } poly.add_hole(hole); - } + return poly; +} +std::tuple, compas::RowMatrixXi, std::vector> pmp_create_interior_straight_skeleton( + Eigen::Ref &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::RowMatrixXi, std::vector> pmp_create_interior_straight_skeleton_with_holes( + Eigen::Ref &V, + std::vector> &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 pmp_create_offset_polygons_2_inner(Eigen::Ref &V, double &offset){ - Polygon_2 poly; - for (int i = 0; i < V.rows(); i++){ - poly.push_back(Point(V(i, 0), V(i, 1))); - } + Polygon_2 poly = data_to_polygon(V); PolygonPtrVector offset_polygons = CGAL::create_interior_skeleton_and_offset_polygons_2(offset, poly); std::vector result; - for(auto pi = offset_polygons.begin(); pi != offset_polygons.end(); ++pi){ - std::size_t n = (*pi)->size(); - compas::RowMatrixXd points(n, 3); - int j = 0; - for (auto vi = (*pi)->vertices_begin(); vi != (*pi)->vertices_end(); ++vi){ - points(j, 0) = (double)(*vi).x(); - points(j, 1) = (double)(*vi).y(); - points(j, 2) = 0; - j++; - } + for(typename PolygonPtrVector::const_iterator pi = offset_polygons.begin() ; pi != offset_polygons.end() ; ++ pi ){ + compas::RowMatrixXd points = polygon_to_data(**pi); result.push_back(points); } return result; } -std::vector pmp_create_offset_polygons_2_outer(Eigen::Ref &V, double &offset){ - Polygon_2 poly; - for (int i = 0; i < V.rows(); i++){ - poly.push_back(Point(V(i, 0), V(i, 1))); +std::vector>> pmp_create_offset_polygons_2_inner_with_holes(Eigen::Ref &V, std::vector> &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); + + std::vector>> result; + for(typename PolygonWithHolesPtrVector::const_iterator pi = offset_polygons.begin() ; pi != offset_polygons.end() ; ++ pi ){ + std::tuple> polywh_data = polygon_with_holes_to_data(**pi); + result.push_back(polywh_data); } + return result; +} + +std::vector pmp_create_offset_polygons_2_outer(Eigen::Ref &V, double &offset){ + Polygon_2 poly = data_to_polygon(V); PolygonPtrVector offset_polygons = CGAL::create_exterior_skeleton_and_offset_polygons_2(offset, poly); std::vector result; - for(auto pi = offset_polygons.begin(); pi != offset_polygons.end(); ++pi){ - std::size_t n = (*pi)->size(); - compas::RowMatrixXd points(n, 3); - int j = 0; - for (auto vi = (*pi)->vertices_begin(); vi != (*pi)->vertices_end(); ++vi){ - points(j, 0) = (double)(*vi).x(); - points(j, 1) = (double)(*vi).y(); - points(j, 2) = 0; - j++; - } + for(typename PolygonPtrVector::const_iterator pi = offset_polygons.begin() ; pi != offset_polygons.end() ; ++ pi ){ + compas::RowMatrixXd points = polygon_to_data(**pi); result.push_back(points); } return result; } -std::vector pmp_create_weighted_offset_polygons_2_inner(Eigen::Ref &V, double &offset, Eigen::Ref &weights){ - Polygon_2 poly; - for (int i = 0; i < V.rows(); i++){ - poly.push_back(Point(V(i, 0), V(i, 1))); +std::vector>> pmp_create_offset_polygons_2_outer_with_holes(Eigen::Ref &V, std::vector> &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); + + std::vector>> result; + for(typename PolygonWithHolesPtrVector::const_iterator pi = offset_polygons.begin() ; pi != offset_polygons.end() ; ++ pi ){ + std::tuple> polywh_data = polygon_with_holes_to_data(**pi); + result.push_back(polywh_data); } + return result; +} + +std::vector pmp_create_weighted_offset_polygons_2_inner(Eigen::Ref &V, double &offset, Eigen::Ref &weights){ + Polygon_2 poly = data_to_polygon(V); std::vector weights_vec; for (int i = 0; i < weights.rows(); i++){ weights_vec.push_back(weights(i, 0)); @@ -161,26 +186,15 @@ std::vector pmp_create_weighted_offset_polygons_2_inner(Eig PolygonPtrVector offset_polygons = CGAL::create_offset_polygons_2(offset, *iss); std::vector result; - for(auto pi = offset_polygons.begin(); pi != offset_polygons.end(); ++pi){ - std::size_t n = (*pi)->size(); - compas::RowMatrixXd points(n, 3); - int j = 0; - for (auto vi = (*pi)->vertices_begin(); vi != (*pi)->vertices_end(); ++vi){ - points(j, 0) = (double)(*vi).x(); - points(j, 1) = (double)(*vi).y(); - points(j, 2) = 0; - j++; - } + for(typename PolygonPtrVector::const_iterator pi = offset_polygons.begin() ; pi != offset_polygons.end() ; ++ pi ){ + compas::RowMatrixXd points = polygon_to_data(**pi); result.push_back(points); } return result; } std::vector pmp_create_weighted_offset_polygons_2_outer(Eigen::Ref &V, double &offset, Eigen::Ref &weights){ - Polygon_2 poly; - for (int i = 0; i < V.rows(); i++){ - poly.push_back(Point(V(i, 0), V(i, 1))); - } + Polygon_2 poly = data_to_polygon(V); std::vector weights_vec; for (int i = 0; i < weights.rows(); i++){ weights_vec.push_back(weights(i, 0)); @@ -189,16 +203,8 @@ std::vector pmp_create_weighted_offset_polygons_2_outer(Eig PolygonPtrVector offset_polygons = CGAL::create_offset_polygons_2(offset, *iss); std::vector result; - for(auto pi = offset_polygons.begin(); pi != offset_polygons.end(); ++pi){ - std::size_t n = (*pi)->size(); - compas::RowMatrixXd points(n, 3); - int j = 0; - for (auto vi = (*pi)->vertices_begin(); vi != (*pi)->vertices_end(); ++vi){ - points(j, 0) = (double)(*vi).x(); - points(j, 1) = (double)(*vi).y(); - points(j, 2) = 0; - j++; - } + for(typename PolygonPtrVector::const_iterator pi = offset_polygons.begin() ; pi != offset_polygons.end() ; ++ pi ){ + compas::RowMatrixXd points = polygon_to_data(**pi); result.push_back(points); } return result; @@ -229,12 +235,26 @@ void init_straight_skeleton_2(pybind11::module &m) pybind11::arg("V").noconvert(), pybind11::arg("offset").noconvert()); + submodule.def( + "create_offset_polygons_2_inner_with_holes", + &pmp_create_offset_polygons_2_inner_with_holes, + pybind11::arg("V").noconvert(), + pybind11::arg("holes").noconvert(), + pybind11::arg("offset").noconvert()); + submodule.def( "create_offset_polygons_2_outer", &pmp_create_offset_polygons_2_outer, pybind11::arg("V").noconvert(), pybind11::arg("offset").noconvert()); + submodule.def( + "create_offset_polygons_2_outer_with_holes", + &pmp_create_offset_polygons_2_outer_with_holes, + pybind11::arg("V").noconvert(), + pybind11::arg("holes").noconvert(), + pybind11::arg("offset").noconvert()); + submodule.def( "create_weighted_offset_polygons_2_inner", &pmp_create_weighted_offset_polygons_2_inner, diff --git a/src/straight_skeleton_2.h b/src/straight_skeleton_2.h index 1d5f250..b5955aa 100644 --- a/src/straight_skeleton_2.h +++ b/src/straight_skeleton_2.h @@ -16,10 +16,20 @@ std::vector pmp_create_offset_polygons_2_inner( Eigen::Ref &V, double &offset); +std::vector>> pmp_create_offset_polygons_2_inner_with_holes( + Eigen::Ref &V, + std::vector> &holes, + double &offset); + std::vector pmp_create_offset_polygons_2_outer( Eigen::Ref &V, double &offset); +std::vector>> pmp_create_offset_polygons_2_outer_with_holes( + Eigen::Ref &V, + std::vector> &holes, + double &offset); + std::vector pmp_create_weighted_offset_polygons_2_inner( Eigen::Ref &V, double &offset, diff --git a/tests/test_straight_skeleton_2.py b/tests/test_straight_skeleton_2.py index 2bc5ba7..83661b4 100644 --- a/tests/test_straight_skeleton_2.py +++ b/tests/test_straight_skeleton_2.py @@ -1,6 +1,7 @@ from compas_cgal.straight_skeleton_2 import create_interior_straight_skeleton from compas_cgal.straight_skeleton_2 import create_interior_straight_skeleton_with_holes from compas_cgal.straight_skeleton_2 import create_offset_polygons_2 +from compas_cgal.straight_skeleton_2 import create_offset_polygons_with_holes_2 from compas_cgal.straight_skeleton_2 import create_weighted_offset_polygons_2 @@ -56,3 +57,40 @@ def test_offset(): assert len(polygons) == 1, len(polygons) polygons = create_weighted_offset_polygons_2(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 = create_offset_polygons_with_holes_2(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 From 8f6dd739c676fa545cce6c68df23a69c75befea6 Mon Sep 17 00:00:00 2001 From: Romana Rust Date: Tue, 1 Oct 2024 18:21:50 +0200 Subject: [PATCH 2/4] Update straight_skeleton_2.py --- src/compas_cgal/straight_skeleton_2.py | 38 ++++++++++++-------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/src/compas_cgal/straight_skeleton_2.py b/src/compas_cgal/straight_skeleton_2.py index e004238..d471481 100644 --- a/src/compas_cgal/straight_skeleton_2.py +++ b/src/compas_cgal/straight_skeleton_2.py @@ -49,7 +49,7 @@ def graph_from_skeleton_data(points: VerticesNumpy, indices: IntNx1, edges: IntN def create_interior_straight_skeleton(points, as_graph=True) -> Union[Graph, Tuple[VerticesNumpy, IntNx1, IntNx2, IntNx1]]: - """Compute the skeleton of a polygon. + """Compute the skeleton of a 2D polygon. Parameters ---------- @@ -66,7 +66,7 @@ def create_interior_straight_skeleton(points, as_graph=True) -> Union[Graph, Tup Raises ------ ValueError - If the normal of the polygon is not [0, 0, 1]. + If the normal of the polygon is not directed vertically upwards like [0, 0, 1]. """ points = list(points) normal = normal_polygon(points, True) @@ -80,12 +80,12 @@ def create_interior_straight_skeleton(points, as_graph=True) -> Union[Graph, Tup def create_interior_straight_skeleton_with_holes(points, holes, as_graph=True) -> Union[Graph, Tuple[VerticesNumpy, IntNx1, IntNx2, IntNx1]]: - """Compute the skeleton of a polygon with holes. + """Compute the skeleton of a 2D polygon with holes. Parameters ---------- points : list of point coordinates or :class:`compas.geometry.Polygon` - The points of the polygon. + The points of the 2D polygon. holes : list of list of point coordinates or list of :class:`compas.geometry.Polygon` The holes of the polygon. as_graph : bool, optional @@ -99,8 +99,8 @@ def create_interior_straight_skeleton_with_holes(points, holes, as_graph=True) - Raises ------ ValueError - If the normal of the polygon is not [0, 0, 1]. - If the normal of a hole is not [0, 0, -1]. + If the normal of the polygon is not directed vertically upwards like [0, 0, 1]. + If the normal of a hole is not directed vertically downwards like [0, 0, -1]. """ points = list(points) normal = normal_polygon(points, True) @@ -123,12 +123,12 @@ def create_interior_straight_skeleton_with_holes(points, holes, as_graph=True) - def create_offset_polygons_2(points, offset) -> list[Polygon]: - """Compute the polygon offset. + """Compute the offset from a 2D polygon. Parameters ---------- points : list of point coordinates or :class:`compas.geometry.Polygon` - The points of the polygon. + The points of the 2D polygon. offset : float The offset distance. If negative, the offset is outside the polygon, otherwise inside. @@ -140,7 +140,7 @@ def create_offset_polygons_2(points, offset) -> list[Polygon]: Raises ------ ValueError - If the normal of the polygon is not [0, 0, 1]. + If the normal of the polygon is not directed vertically upwards like [0, 0, 1]. """ points = list(points) normal = normal_polygon(points, True) @@ -156,20 +156,17 @@ def create_offset_polygons_2(points, offset) -> list[Polygon]: def create_offset_polygons_with_holes_2(points, holes, offset) -> list[Tuple[Polygon, list[Polygon]]]: - """Compute the polygon offset with holes. + """Compute the offset from a 2D polygon with holes. Parameters ---------- points : list of point coordinates or :class:`compas.geometry.Polygon` - The points of the polygon. + The points of the 2D polygon. holes : list of list of point coordinates or list of :class:`compas.geometry.Polygon` The holes of the polygon. offset : float The offset distance. If negative, the offset is outside the polygon, otherwise inside. - Returns - ------- - Returns ------- list of tuple of (:class:`Polygon`, list[:class:`Polygon`]) @@ -178,8 +175,8 @@ def create_offset_polygons_with_holes_2(points, holes, offset) -> list[Tuple[Pol Raises ------ ValueError - If the normal of the polygon is not [0, 0, 1]. - If the normal of a hole is not [0, 0, -1]. + If the normal of the polygon is not directed vertically upwards like [0, 0, 1]. + If the normal of a hole is not directed vertically downwards like [0, 0, -1]. """ points = list(points) normal = normal_polygon(points, True) @@ -189,14 +186,13 @@ def create_offset_polygons_with_holes_2(points, holes, offset) -> list[Tuple[Pol H = [] for i, hole in enumerate(holes): - points = list(hole) + points = hole normal_hole = normal_polygon(points, True) if not TOL.is_allclose(normal_hole, [0, 0, -1]): raise ValueError("The normal of the hole should be [0, 0, -1]. The normal of the provided {}-th hole is {}".format(i, normal_hole)) hole = np.asarray(points, dtype=np.float64) H.append(hole) - offset = float(offset) if offset < 0: # outside offset_polygons = straight_skeleton_2.create_offset_polygons_2_outer_with_holes(V, H, abs(offset)) else: # inside @@ -213,12 +209,12 @@ def create_offset_polygons_with_holes_2(points, holes, offset) -> list[Tuple[Pol def create_weighted_offset_polygons_2(points, offset, weights) -> list[Polygon]: - """Compute the polygon offset with weights. + """Compute the offset from a 2D polygon with weights. Parameters ---------- points : list of point coordinates or :class:`compas.geometry.Polygon` - The points of the polygon. + The points of the 2D polygon. offset : float The offset distance. If negative, the offset is outside the polygon, otherwise inside. weights : list of float @@ -232,7 +228,7 @@ def create_weighted_offset_polygons_2(points, offset, weights) -> list[Polygon]: Raises ------ ValueError - If the normal of the polygon is not [0, 0, 1]. + If the normal of the polygon is not directed vertically upwards like [0, 0, 1]. ValueError If the number of weights does not match the number of points. """ From 161a1277a90f2a8d6627f01a3947f1466fd871e3 Mon Sep 17 00:00:00 2001 From: Romana Rust Date: Thu, 3 Oct 2024 07:52:43 +0200 Subject: [PATCH 3/4] changing names --- CHANGELOG.md | 6 ++++- docs/api/compas_cgal.straight_skeleton_2.rst | 7 ++++-- docs/examples/straight_skeleton_2.py | 4 ++-- docs/examples/straight_skeleton_2_holes.py | 4 ++-- docs/examples/straight_skeleton_2_offset.py | 10 ++++---- .../straight_skeleton_2_offset_weighted.py | 4 ++-- src/compas_cgal/straight_skeleton_2.py | 10 ++++---- tests/test_straight_skeleton_2.py | 24 +++++++++---------- 8 files changed, 38 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 784bb41..b6b2449 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,9 +11,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Added recipe hasher. * Added `scip` to dev install instructions in README.md -* Added `compas_cgal.straight_skeleton_2.create_offset_polygons_with_holes_2`. +* Added `compas_cgal.straight_skeleton_2.offset_polygon_with_holes`. ### Changed +* Changed name of `compas_cgal.straight_skeleton_2.create_interior_straight_skeleton` to `interior_straight_skeleton` +* Changed name of `compas_cgal.straight_skeleton_2.create_interior_straight_skeleton_with_holes` to `interior_straight_skeleton_with_holes` +* Changed name of `compas_cgal.straight_skeleton_2.create_offset_polygons_2` to `offset_polygon` +* Changed name of `compas_cgal.straight_skeleton_2.create_weighted_offset_polygons_2` to `weighted_offset_polygon` ### Removed diff --git a/docs/api/compas_cgal.straight_skeleton_2.rst b/docs/api/compas_cgal.straight_skeleton_2.rst index 3f37685..7daa1bf 100644 --- a/docs/api/compas_cgal.straight_skeleton_2.rst +++ b/docs/api/compas_cgal.straight_skeleton_2.rst @@ -8,5 +8,8 @@ compas_cgal.straight_skeleton_2 :toctree: generated/ :nosignatures: - create_interior_straight_skeleton - create_interior_straight_skeleton_with_holes + interior_straight_skeleton + interior_straight_skeleton_with_holes + offset_polygon + weighted_offset_polygon + offset_polygon_with_holes diff --git a/docs/examples/straight_skeleton_2.py b/docs/examples/straight_skeleton_2.py index 0be3eb3..c3d2180 100644 --- a/docs/examples/straight_skeleton_2.py +++ b/docs/examples/straight_skeleton_2.py @@ -1,4 +1,4 @@ -from compas_cgal.straight_skeleton_2 import create_interior_straight_skeleton +from compas_cgal.straight_skeleton_2 import interior_straight_skeleton from compas_viewer import Viewer points = [ @@ -15,7 +15,7 @@ ] -graph = create_interior_straight_skeleton(points) +graph = interior_straight_skeleton(points) # ============================================================================== # Viz diff --git a/docs/examples/straight_skeleton_2_holes.py b/docs/examples/straight_skeleton_2_holes.py index b1b91bf..0787a1e 100644 --- a/docs/examples/straight_skeleton_2_holes.py +++ b/docs/examples/straight_skeleton_2_holes.py @@ -1,7 +1,7 @@ from compas.geometry import Polygon from compas_viewer import Viewer -from compas_cgal.straight_skeleton_2 import create_interior_straight_skeleton_with_holes +from compas_cgal.straight_skeleton_2 import interior_straight_skeleton_with_holes points = [ (-1.91, 3.59, 0.0), @@ -25,7 +25,7 @@ polygon = Polygon(points) holes = [Polygon(hole) for hole in holes] -graph = create_interior_straight_skeleton_with_holes(polygon, holes) +graph = interior_straight_skeleton_with_holes(polygon, holes) # ============================================================================== # Viz diff --git a/docs/examples/straight_skeleton_2_offset.py b/docs/examples/straight_skeleton_2_offset.py index c2dfd46..f6b3006 100644 --- a/docs/examples/straight_skeleton_2_offset.py +++ b/docs/examples/straight_skeleton_2_offset.py @@ -1,7 +1,7 @@ from compas.geometry import Polygon from compas_viewer import Viewer -from compas_cgal.straight_skeleton_2 import create_offset_polygons_2 +from compas_cgal.straight_skeleton_2 import offset_polygon points = [ (-1.91, 3.59, 0.0), @@ -18,8 +18,8 @@ polygon = Polygon(points) offset = 1.5 -offset_polygons_inner = create_offset_polygons_2(points, offset) -offset_polygons_outer = create_offset_polygons_2(points, -offset) +offset_polygon_inner = offset_polygon(points, offset) +offset_polygon_outer = offset_polygon(points, -offset) # ============================================================================== # Viz @@ -29,9 +29,9 @@ viewer.scene.add(polygon) viewer.config.renderer.show_grid = False -for opolygon in offset_polygons_inner: +for opolygon in offset_polygon_inner: viewer.scene.add(opolygon, linecolor=(1.0, 0.0, 0.0), facecolor=(1.0, 1.0, 1.0, 0.0)) -for opolygon in offset_polygons_outer: +for opolygon in offset_polygon_outer: viewer.scene.add(opolygon, linecolor=(0.0, 0.0, 1.0), facecolor=(1.0, 1.0, 1.0, 0.0)) viewer.show() diff --git a/docs/examples/straight_skeleton_2_offset_weighted.py b/docs/examples/straight_skeleton_2_offset_weighted.py index b7ce2b0..09f9273 100644 --- a/docs/examples/straight_skeleton_2_offset_weighted.py +++ b/docs/examples/straight_skeleton_2_offset_weighted.py @@ -1,7 +1,7 @@ from compas.geometry import Polygon from compas_viewer import Viewer -from compas_cgal.straight_skeleton_2 import create_weighted_offset_polygons_2 +from compas_cgal.straight_skeleton_2 import weighted_offset_polygons points = [ (-1.91, 3.59, 0.0), @@ -21,7 +21,7 @@ distances = [0.1, 0.3, 0.6, 0.1, 0.7, 0.5, 0.2, 0.4, 0.8, 0.2] weights = [1.0 / d for d in distances] offset = 1.0 -offset_polygons_outer = create_weighted_offset_polygons_2(points, -offset, weights) +offset_polygons_outer = weighted_offset_polygons(points, -offset, weights) # ============================================================================== # Viz diff --git a/src/compas_cgal/straight_skeleton_2.py b/src/compas_cgal/straight_skeleton_2.py index d471481..88cf24d 100644 --- a/src/compas_cgal/straight_skeleton_2.py +++ b/src/compas_cgal/straight_skeleton_2.py @@ -48,7 +48,7 @@ def graph_from_skeleton_data(points: VerticesNumpy, indices: IntNx1, edges: IntN return graph -def create_interior_straight_skeleton(points, as_graph=True) -> Union[Graph, Tuple[VerticesNumpy, IntNx1, IntNx2, IntNx1]]: +def interior_straight_skeleton(points, as_graph=True) -> Union[Graph, Tuple[VerticesNumpy, IntNx1, IntNx2, IntNx1]]: """Compute the skeleton of a 2D polygon. Parameters @@ -79,7 +79,7 @@ def create_interior_straight_skeleton(points, as_graph=True) -> Union[Graph, Tup return points, indices, edges, edge_types -def create_interior_straight_skeleton_with_holes(points, holes, as_graph=True) -> Union[Graph, Tuple[VerticesNumpy, IntNx1, IntNx2, IntNx1]]: +def interior_straight_skeleton_with_holes(points, holes, as_graph=True) -> Union[Graph, Tuple[VerticesNumpy, IntNx1, IntNx2, IntNx1]]: """Compute the skeleton of a 2D polygon with holes. Parameters @@ -122,7 +122,7 @@ def create_interior_straight_skeleton_with_holes(points, holes, as_graph=True) - return points, indices, edges, edge_types -def create_offset_polygons_2(points, offset) -> list[Polygon]: +def offset_polygon(points, offset) -> list[Polygon]: """Compute the offset from a 2D polygon. Parameters @@ -155,7 +155,7 @@ def create_offset_polygons_2(points, offset) -> list[Polygon]: return [Polygon(points.tolist()) for points in offset_polygons] -def create_offset_polygons_with_holes_2(points, holes, offset) -> list[Tuple[Polygon, list[Polygon]]]: +def offset_polygon_with_holes(points, holes, offset) -> list[Tuple[Polygon, list[Polygon]]]: """Compute the offset from a 2D polygon with holes. Parameters @@ -208,7 +208,7 @@ def create_offset_polygons_with_holes_2(points, holes, offset) -> list[Tuple[Pol return result -def create_weighted_offset_polygons_2(points, offset, weights) -> list[Polygon]: +def weighted_offset_polygon(points, offset, weights) -> list[Polygon]: """Compute the offset from a 2D polygon with weights. Parameters diff --git a/tests/test_straight_skeleton_2.py b/tests/test_straight_skeleton_2.py index 83661b4..340bae3 100644 --- a/tests/test_straight_skeleton_2.py +++ b/tests/test_straight_skeleton_2.py @@ -1,8 +1,8 @@ -from compas_cgal.straight_skeleton_2 import create_interior_straight_skeleton -from compas_cgal.straight_skeleton_2 import create_interior_straight_skeleton_with_holes -from compas_cgal.straight_skeleton_2 import create_offset_polygons_2 -from compas_cgal.straight_skeleton_2 import create_offset_polygons_with_holes_2 -from compas_cgal.straight_skeleton_2 import create_weighted_offset_polygons_2 +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(): @@ -16,7 +16,7 @@ def test_straight_polygon(): (-1, 1, 0), (-12, 0, 0), ] - graph = create_interior_straight_skeleton(points) + graph = interior_straight_skeleton(points) assert graph.number_of_edges() == 16 @@ -32,7 +32,7 @@ def test_straight_skeleton_with_holes(): (-12, 0, 0), ] hole = [(-1, 0, 0), (0, 1, 0), (1, 0, 0), (0, -1, 0)] - graph = create_interior_straight_skeleton_with_holes(points, [hole]) + graph = interior_straight_skeleton_with_holes(points, [hole]) assert graph.number_of_edges() == 32 @@ -48,14 +48,14 @@ def test_offset(): (-12, 0, 0), ] offset = 0.5 - polygons = create_offset_polygons_2(points, offset) + polygons = offset_polygon(points, offset) assert len(polygons) == 1, len(polygons) - polygons = create_offset_polygons_2(points, -offset) + 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 = create_weighted_offset_polygons_2(points, offset, weights) + polygons = weighted_offset_polygon(points, offset, weights) assert len(polygons) == 1, len(polygons) - polygons = create_weighted_offset_polygons_2(points, -offset, weights) + polygons = weighted_offset_polygon(points, -offset, weights) assert len(polygons) == 1, len(polygons) @@ -88,7 +88,7 @@ def test_offset_with_holes(): ], [(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 = create_offset_polygons_with_holes_2(points, holes, -0.2) + 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) From 33787d52185db170bbe44bd62a29e1d96f4100b2 Mon Sep 17 00:00:00 2001 From: Romana Rust Date: Thu, 3 Oct 2024 08:09:07 +0200 Subject: [PATCH 4/4] Update polygonal_poinset_reconstruction.rst --- docs/examples/polygonal_poinset_reconstruction.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples/polygonal_poinset_reconstruction.rst b/docs/examples/polygonal_poinset_reconstruction.rst index 8ba34fa..466c4e6 100644 --- a/docs/examples/polygonal_poinset_reconstruction.rst +++ b/docs/examples/polygonal_poinset_reconstruction.rst @@ -7,5 +7,5 @@ Polygonal PointSet Reconstruction :class: figure-img img-fluid -.. literalinclude:: polygonal_poinset_reconstruction_ransac.py +.. literalinclude:: polygonal_poinset_reconstruction.py :language: python