-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added straight skeleton 2 #29
Merged
+220
−8
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f83a8b7
Update README.md
romanarust c0b949f
add straight skeleton 2
romanarust 1c8d264
Create test_straight_skeleton_2.py
romanarust 5dc909a
add doc
romanarust d39792b
Update CHANGELOG.md
romanarust 032fae0
Update AUTHORS.md
romanarust 6c623b6
Merge branch 'main' into feature/straight_skeleton_2
romanarust 30ccf2d
Update straight_skeleton_2.py
romanarust ec8996a
remove assert
romanarust 9b2f245
adding another test
romanarust e93d240
change to TOL
romanarust 391a8a9
edge and edges to compas namespace
romanarust bb3d711
Delete PLACEHOLDER
romanarust c229d1a
Update setup.py
romanarust File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
******************************************************************************** | ||
compas_cgal.straight_skeleton_2 | ||
******************************************************************************** | ||
|
||
.. currentmodule:: compas_cgal.straight_skeleton_2 | ||
|
||
.. autosummary:: | ||
:toctree: generated/ | ||
:nosignatures: | ||
|
||
create_interior_straight_skeleton |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from compas.datastructures import Graph | ||
from compas.geometry import Polygon | ||
from compas_viewer import Viewer | ||
|
||
from compas_cgal.straight_skeleton_2 import create_interior_straight_skeleton | ||
|
||
points = [ | ||
(-1.91, 3.59, 0.0), | ||
(-5.53, -5.22, 0.0), | ||
(-0.39, -1.98, 0.0), | ||
(2.98, -5.51, 0.0), | ||
(4.83, -2.02, 0.0), | ||
(9.70, -3.63, 0.0), | ||
(12.23, 1.25, 0.0), | ||
(3.42, 0.66, 0.0), | ||
(2.92, 4.03, 0.0), | ||
(-1.91, 3.59, 0.0), | ||
] | ||
polygon = Polygon(points) | ||
lines = create_interior_straight_skeleton(points) | ||
graph = Graph.from_lines(lines) | ||
|
||
# ============================================================================== | ||
# Viz | ||
# ============================================================================== | ||
|
||
viewer = Viewer(width=1600, height=900) | ||
viewer.renderer_config.show_grid = False | ||
viewer.scene.add(graph, edgecolor=(1.0, 0.0, 0.0)) | ||
viewer.scene.add(polygon) | ||
viewer.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
******************************************************************************** | ||
2D Straight Skeleton | ||
******************************************************************************** | ||
|
||
.. figure:: /_images/cgal_straight_skeleton_2.png | ||
:figclass: figure | ||
:class: figure-img img-fluid | ||
|
||
|
||
.. literalinclude:: straight_skeleton_2.py | ||
:language: python |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import numpy as np | ||
from compas.geometry import normal_polygon | ||
from compas.tolerance import TOL | ||
|
||
from compas_cgal._cgal import straight_skeleton_2 | ||
|
||
from .types import PolylinesNumpy | ||
|
||
|
||
def create_interior_straight_skeleton(points) -> PolylinesNumpy: | ||
"""Compute the skeleton of a polygon. | ||
|
||
Parameters | ||
---------- | ||
points : list of point coordinates or :class:`compas.geometry.Polygon` | ||
The points of the polygon. | ||
|
||
Returns | ||
------- | ||
:attr:`compas_cgal.types.PolylinesNumpy` | ||
The skeleton of the polygon. | ||
|
||
Raises | ||
------ | ||
ValueError | ||
If the normal of the polygon is not [0, 0, 1]. | ||
""" | ||
points = list(points) | ||
if not TOL.is_allclose(normal_polygon(points, True), [0, 0, 1]): | ||
raise ValueError("Please pass a polygon with a normal vector of [0, 0, 1].") | ||
V = np.asarray(points, dtype=np.float64) | ||
return straight_skeleton_2.create_interior_straight_skeleton(V) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
#include "straight_skeleton_2.h" | ||
#include <CGAL/Polygon_2.h> | ||
#include <CGAL/create_straight_skeleton_2.h> | ||
|
||
typedef CGAL::Exact_predicates_inexact_constructions_kernel K; | ||
typedef K::Point_2 Point; | ||
typedef CGAL::Polygon_2<K> Polygon_2; | ||
typedef CGAL::Straight_skeleton_2<K> Ss; | ||
typedef boost::shared_ptr<Ss> SsPtr; | ||
typedef CGAL::Straight_skeleton_2<K>::Halfedge_const_handle Halfedge_const_handle; | ||
typedef CGAL::Straight_skeleton_2<K>::Vertex_const_handle Vertex_const_handle; | ||
|
||
compas::Edges pmp_create_interior_straight_skeleton( | ||
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))); | ||
} | ||
SsPtr iss = CGAL::create_interior_straight_skeleton_2(poly.vertices_begin(), poly.vertices_end()); | ||
compas::Edges edgelist; | ||
for(auto hit = iss->halfedges_begin(); hit != iss->halfedges_end(); ++hit){ | ||
const Halfedge_const_handle h = hit; | ||
if(!h->is_bisector()){ | ||
continue; | ||
} | ||
const Vertex_const_handle& v1 = h->vertex(); | ||
const Vertex_const_handle& v2 = h->opposite()->vertex(); | ||
if(&*v1 < &*v2){ | ||
std::vector<double> s_vec = {v1->point().x(), v1->point().y(), 0}; | ||
std::vector<double> t_vec = {v2->point().x(), v2->point().y(), 0}; | ||
compas::Edge edge = std::make_tuple(s_vec, t_vec); | ||
edgelist.push_back(edge); | ||
} | ||
|
||
} | ||
return edgelist; | ||
}; | ||
|
||
|
||
// =========================================================================== | ||
// PyBind11 | ||
// =========================================================================== | ||
|
||
void init_straight_skeleton_2(pybind11::module &m) | ||
{ | ||
pybind11::module submodule = m.def_submodule("straight_skeleton_2"); | ||
|
||
submodule.def( | ||
"create_interior_straight_skeleton", | ||
&pmp_create_interior_straight_skeleton, | ||
pybind11::arg("V").noconvert()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#ifndef COMPAS_STRAIGHT_SKELETON_2_H | ||
#define COMPAS_STRAIGHT_SKELETON_2_H | ||
|
||
#include <compas.h> | ||
|
||
|
||
compas::Edges pmp_create_interior_straight_skeleton( | ||
Eigen::Ref<const compas::RowMatrixXd> &V); | ||
|
||
#endif /* COMPAS_STRAIGHT_SKELETON_2_H */ |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from compas_cgal.straight_skeleton_2 import create_interior_straight_skeleton | ||
from compas.tolerance import TOL | ||
|
||
|
||
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), | ||
] | ||
lines = create_interior_straight_skeleton(points) | ||
assert len(lines) == 8 | ||
|
||
|
||
def test_straight_polygon_2_compare(): | ||
points = [ | ||
(-1.91, 3.59, 0.0), | ||
(-5.53, -5.22, 0.0), | ||
(-0.39, -1.98, 0.0), | ||
(2.98, -5.51, 0.0), | ||
(4.83, -2.02, 0.0), | ||
(9.70, -3.63, 0.0), | ||
(12.23, 1.25, 0.0), | ||
(3.42, 0.66, 0.0), | ||
(2.92, 4.03, 0.0), | ||
(-1.91, 3.59, 0.0), | ||
] | ||
lines = create_interior_straight_skeleton(points) | ||
|
||
expected = [ | ||
[[-1.91, 3.59, 0.0], [-0.139446292, 1.191439787, 0.0]], | ||
[[-5.53, -5.22, 0.0], [-0.139446292, 1.191439787, 0.0]], | ||
[[-0.39, -1.98, 0.0], [0.008499564, 1.241560466, 0.0]], | ||
[[2.98, -5.51, 0.0], [2.44972507, -1.674799065, 0.0]], | ||
[[4.83, -2.02, 0.0], [4.228131167, -0.522007766, 0.0]], | ||
[[8.663865218, -1.084821998, 0.0], [9.7, -3.63, 0.0]], | ||
[[12.23, 1.25, 0.0], [8.663865218, -1.084821998, 0.0]], | ||
[[3.42, 0.66, 0.0], [1.755862468, -1.404991433, 0.0]], | ||
[[2.92, 4.03, 0.0], [0.563706846, 1.033296141, 0.0]], | ||
[[4.228131167, -0.522007766, 0.0], [2.44972507, -1.674799065, 0.0]], | ||
[[4.228131167, -0.522007766, 0.0], [8.663865218, -1.084821998, 0.0]], | ||
[[1.755862468, -1.404991433, 0.0], [2.44972507, -1.674799065, 0.0]], | ||
[[0.563706846, 1.033296141, 0.0], [1.755862468, -1.404991433, 0.0]], | ||
[[-0.139446292, 1.191439787, 0.0], [0.008499564, 1.241560466, 0.0]], | ||
[[0.563706846, 1.033296141, 0.0], [0.008499564, 1.241560466, 0.0]], | ||
] | ||
for act, exp in zip(lines, expected): | ||
sa, ea = act | ||
se, ee = exp | ||
# the line direction sometimes changes ... | ||
assert TOL.is_allclose(sa, se) or TOL.is_allclose(sa, ee) | ||
assert TOL.is_allclose(ea, ee) or TOL.is_allclose(ea, se) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps it would be worth adding a few tests of "known"/"predictable" skeletons?