Skip to content

Commit a307f44

Browse files
committed
2 parents e87aebd + bbc802c commit a307f44

15 files changed

+518
-5
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
* Added `compas_cgal.straight_skeleton_2.create_interior_straight_skeleton`.
13+
* Added `compas_cgal.straight_skeleton_2.create_interior_straight_skeleton_with_holes`.
14+
* Added `compas_cgal.straight_skeleton_2.create_offset_polygons_2_inner`.
15+
* Added `compas_cgal.straight_skeleton_2.create_offset_polygons_2_outer`.
16+
* Added `compas_cgal.straight_skeleton_2.create_weighted_offset_polygons_2_inner`.
17+
* Added `compas_cgal.straight_skeleton_2.create_weighted_offset_polygons_2_outer`.
1318

1419
### Changed
1520

docs/PLACEHOLDER

-1
This file was deleted.

docs/_images/PLACEHOLDER

-1
This file was deleted.
71.4 KB
Loading
Loading
Loading

docs/api/compas_cgal.straight_skeleton_2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ compas_cgal.straight_skeleton_2
99
:nosignatures:
1010

1111
create_interior_straight_skeleton
12+
create_interior_straight_skeleton_with_holes

docs/examples/straight_skeleton_2.rst

+27
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,30 @@
99

1010
.. literalinclude:: straight_skeleton_2.py
1111
:language: python
12+
13+
14+
.. figure:: /_images/cgal_straight_skeleton_2_holes.png
15+
:figclass: figure
16+
:class: figure-img img-fluid
17+
18+
19+
.. literalinclude:: straight_skeleton_2_holes.py
20+
:language: python
21+
22+
23+
.. figure:: /_images/cgal_straight_skeleton_2_offset.png
24+
:figclass: figure
25+
:class: figure-img img-fluid
26+
27+
28+
.. literalinclude:: straight_skeleton_2_offset.py
29+
:language: python
30+
31+
32+
.. figure:: /_images/cgal_straight_skeleton_2_offset_weighted.png
33+
:figclass: figure
34+
:class: figure-img img-fluid
35+
36+
37+
.. literalinclude:: straight_skeleton_2_offset_weighted.py
38+
:language: python
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from compas.datastructures import Graph
2+
from compas.geometry import Polygon
3+
from compas_viewer import Viewer
4+
5+
from compas_cgal.straight_skeleton_2 import create_interior_straight_skeleton_with_holes
6+
7+
points = [
8+
(-1.91, 3.59, 0.0),
9+
(-5.53, -5.22, 0.0),
10+
(-0.39, -1.98, 0.0),
11+
(2.98, -5.51, 0.0),
12+
(4.83, -2.02, 0.0),
13+
(9.70, -3.63, 0.0),
14+
(12.23, 1.25, 0.0),
15+
(3.42, 0.66, 0.0),
16+
(2.92, 4.03, 0.0),
17+
(-1.91, 3.59, 0.0),
18+
]
19+
20+
holes = [
21+
[(0.42, 0.88, 0.0), (1.1, -1.0, 0.0), (-1.97, -0.93, 0.0), (-1.25, 1.82, 0.0)],
22+
[(4.25, -0.64, 0.0), (2.9, -3.03, 0.0), (2.12, -2.16, 0.0), (2.89, -0.36, 0.0)],
23+
[(10.6, 0.29, 0.0), (9.48, -1.54, 0.0), (5.48, -1.26, 0.0), (5.98, -0.04, 0.0)],
24+
]
25+
26+
27+
polygon = Polygon(points)
28+
holes = [Polygon(hole) for hole in holes]
29+
lines = create_interior_straight_skeleton_with_holes(polygon, holes)
30+
graph = Graph.from_lines(lines)
31+
32+
# ==============================================================================
33+
# Viz
34+
# ==============================================================================
35+
36+
viewer = Viewer(width=1600, height=900)
37+
viewer.renderer_config.show_grid = False
38+
viewer.scene.add(graph, edgecolor=(1.0, 0.0, 0.0))
39+
viewer.scene.add(polygon)
40+
for hole in holes:
41+
viewer.scene.add(hole)
42+
viewer.show()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from compas.geometry import Polygon
2+
from compas_viewer import Viewer
3+
4+
from compas_cgal.straight_skeleton_2 import create_offset_polygons_2
5+
6+
points = [
7+
(-1.91, 3.59, 0.0),
8+
(-5.53, -5.22, 0.0),
9+
(-0.39, -1.98, 0.0),
10+
(2.98, -5.51, 0.0),
11+
(4.83, -2.02, 0.0),
12+
(9.70, -3.63, 0.0),
13+
(12.23, 1.25, 0.0),
14+
(3.42, 0.66, 0.0),
15+
(2.92, 4.03, 0.0),
16+
(-1.91, 3.59, 0.0),
17+
]
18+
polygon = Polygon(points)
19+
offset = 1.5
20+
21+
offset_polygons_inner = create_offset_polygons_2(points, offset)
22+
offset_polygons_outer = create_offset_polygons_2(points, -offset)
23+
24+
# ==============================================================================
25+
# Viz
26+
# ==============================================================================
27+
28+
viewer = Viewer(width=1600, height=900)
29+
viewer.scene.add(polygon)
30+
viewer.config.renderer.show_grid = False
31+
32+
for opolygon in offset_polygons_inner:
33+
viewer.scene.add(opolygon, linecolor=(1.0, 0.0, 0.0), facecolor=(1.0, 1.0, 1.0, 0.0))
34+
for opolygon in offset_polygons_outer:
35+
viewer.scene.add(opolygon, linecolor=(0.0, 0.0, 1.0), facecolor=(1.0, 1.0, 1.0, 0.0))
36+
37+
viewer.show()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from compas.geometry import Polygon
2+
from compas_viewer import Viewer
3+
4+
from compas_cgal.straight_skeleton_2 import create_weighted_offset_polygons_2
5+
6+
points = [
7+
(-1.91, 3.59, 0.0),
8+
(-5.53, -5.22, 0.0),
9+
(-0.39, -1.98, 0.0),
10+
(2.98, -5.51, 0.0),
11+
(4.83, -2.02, 0.0),
12+
(9.70, -3.63, 0.0),
13+
(12.23, 1.25, 0.0),
14+
(3.42, 0.66, 0.0),
15+
(2.92, 4.03, 0.0),
16+
(-1.91, 3.59, 0.0),
17+
]
18+
polygon = Polygon(points)
19+
20+
21+
distances = [0.1, 0.3, 0.6, 0.1, 0.7, 0.5, 0.2, 0.4, 0.8, 0.2]
22+
weights = [1.0 / d for d in distances]
23+
offset = 1.0
24+
offset_polygons_outer = create_weighted_offset_polygons_2(points, -offset, weights)
25+
26+
# ==============================================================================
27+
# Viz
28+
# ==============================================================================
29+
30+
viewer = Viewer(width=1600, height=900)
31+
viewer.scene.add(polygon)
32+
viewer.config.renderer.show_grid = False
33+
34+
for opolygon in offset_polygons_outer:
35+
viewer.scene.add(opolygon, linecolor=(0.0, 0.0, 1.0), facecolor=(1.0, 1.0, 1.0, 0.0))
36+
37+
viewer.show()

src/compas_cgal/straight_skeleton_2.py

+116-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
from compas.geometry import Polygon
23
from compas.geometry import normal_polygon
34
from compas.tolerance import TOL
45

@@ -26,7 +27,120 @@ def create_interior_straight_skeleton(points) -> PolylinesNumpy:
2627
If the normal of the polygon is not [0, 0, 1].
2728
"""
2829
points = list(points)
29-
if not TOL.is_allclose(normal_polygon(points, True), [0, 0, 1]):
30-
raise ValueError("Please pass a polygon with a normal vector of [0, 0, 1].")
30+
normal = normal_polygon(points, True)
31+
if not TOL.is_allclose(normal, [0, 0, 1]):
32+
raise ValueError("The normal of the polygon should be [0, 0, 1]. The normal of the provided polygon is {}".format(normal))
3133
V = np.asarray(points, dtype=np.float64)
3234
return straight_skeleton_2.create_interior_straight_skeleton(V)
35+
36+
37+
def create_interior_straight_skeleton_with_holes(points, holes) -> PolylinesNumpy:
38+
"""Compute the skeleton of a polygon with holes.
39+
40+
Parameters
41+
----------
42+
points : list of point coordinates or :class:`compas.geometry.Polygon`
43+
The points of the polygon.
44+
holes : list of list of point coordinates or list of :class:`compas.geometry.Polygon`
45+
The holes of the polygon.
46+
47+
Returns
48+
-------
49+
:attr:`compas_cgal.types.PolylinesNumpy`
50+
The skeleton of the polygon.
51+
52+
Raises
53+
------
54+
ValueError
55+
If the normal of the polygon is not [0, 0, 1].
56+
If the normal of a hole is not [0, 0, -1].
57+
"""
58+
points = list(points)
59+
normal = normal_polygon(points, True)
60+
if not TOL.is_allclose(normal, [0, 0, 1]):
61+
raise ValueError("The normal of the polygon should be [0, 0, 1]. The normal of the provided polygon is {}".format(normal))
62+
V = np.asarray(points, dtype=np.float64)
63+
64+
H = []
65+
for i, hole in enumerate(holes):
66+
points = list(hole)
67+
normal_hole = normal_polygon(points, True)
68+
if not TOL.is_allclose(normal_hole, [0, 0, -1]):
69+
raise ValueError("The normal of the hole should be [0, 0, -1]. The normal of the provided {}-th hole is {}".format(i, normal_hole))
70+
hole = np.asarray(points, dtype=np.float64)
71+
H.append(hole)
72+
return straight_skeleton_2.create_interior_straight_skeleton_with_holes(V, H)
73+
74+
75+
def create_offset_polygons_2(points, offset) -> list[Polygon]:
76+
"""Compute the polygon offset.
77+
78+
Parameters
79+
----------
80+
points : list of point coordinates or :class:`compas.geometry.Polygon`
81+
The points of the polygon.
82+
offset : float
83+
The offset distance. If negative, the offset is outside the polygon, otherwise inside.
84+
85+
Returns
86+
-------
87+
list[:class:`Polygon`]
88+
The offset polygon(s).
89+
90+
Raises
91+
------
92+
ValueError
93+
If the normal of the polygon is not [0, 0, 1].
94+
"""
95+
points = list(points)
96+
normal = normal_polygon(points, True)
97+
if not TOL.is_allclose(normal, [0, 0, 1]):
98+
raise ValueError("The normal of the polygon should be [0, 0, 1]. The normal of the provided polygon is {}".format(normal))
99+
V = np.asarray(points, dtype=np.float64)
100+
offset = float(offset)
101+
if offset < 0: # outside
102+
offset_polygons = straight_skeleton_2.create_offset_polygons_2_outer(V, abs(offset))[1:] # first one is box
103+
else: # inside
104+
offset_polygons = straight_skeleton_2.create_offset_polygons_2_inner(V, offset)
105+
return [Polygon(points.tolist()) for points in offset_polygons]
106+
107+
108+
def create_weighted_offset_polygons_2(points, offset, weights) -> list[Polygon]:
109+
"""Compute the polygon offset with weights.
110+
111+
Parameters
112+
----------
113+
points : list of point coordinates or :class:`compas.geometry.Polygon`
114+
The points of the polygon.
115+
offset : float
116+
The offset distance. If negative, the offset is outside the polygon, otherwise inside.
117+
weights : list of float
118+
The weights for each edge, starting with the edge between the last and the first point.
119+
120+
Returns
121+
-------
122+
list[:class:`Polygon`]
123+
The offset polygon(s).
124+
125+
Raises
126+
------
127+
ValueError
128+
If the normal of the polygon is not [0, 0, 1].
129+
ValueError
130+
If the number of weights does not match the number of points.
131+
"""
132+
points = list(points)
133+
normal = normal_polygon(points, True)
134+
if not TOL.is_allclose(normal, [0, 0, 1]):
135+
raise ValueError("The normal of the polygon should be [0, 0, 1]. The normal of the provided polygon is {}".format(normal))
136+
137+
V = np.asarray(points, dtype=np.float64)
138+
offset = float(offset)
139+
W = np.asarray(weights, dtype=np.float64)
140+
if W.shape[0] != V.shape[0]:
141+
raise ValueError("The number of weights should be equal to the number of points %d != %d." % (W.shape[0], V.shape[0]))
142+
if offset < 0:
143+
offset_polygons = straight_skeleton_2.create_weighted_offset_polygons_2_outer(V, abs(offset), W)[1:]
144+
else:
145+
offset_polygons = straight_skeleton_2.create_weighted_offset_polygons_2_inner(V, offset, W)
146+
return [Polygon(points.tolist()) for points in offset_polygons]

0 commit comments

Comments
 (0)