1
+ from typing import Tuple
2
+ from typing import Union
3
+
1
4
import numpy as np
5
+ from compas .datastructures import Graph
2
6
from compas .geometry import Polygon
3
7
from compas .geometry import normal_polygon
4
8
from compas .tolerance import TOL
5
9
6
10
from compas_cgal ._cgal import straight_skeleton_2
7
11
8
- from .types import PolylinesNumpy
12
+ from .types import IntNx1
13
+ from .types import IntNx2
14
+ from .types import VerticesNumpy
15
+
9
16
17
+ def graph_from_skeleton_data (points : VerticesNumpy , indices : IntNx1 , edges : IntNx2 , edge_types : IntNx1 ) -> Graph :
18
+ """Create a graph from the skeleton data.
19
+
20
+ Parameters
21
+ ----------
22
+ points : :class:`numpy.ndarray`
23
+ The vertices of the skeleton, each vertex defined by 3 spatial coordinates.
24
+ indices : :class:`numpy.ndarray`
25
+ The vertex indices of the skeleton, corresponding to the points.
26
+ edges : :class:`numpy.ndarray`
27
+ The edges of the skeleton, each edge defined by 2 vertex indices.
28
+ edge_types : :class:`numpy.ndarray`
29
+ The type per edge, `0` for inner bisector, `1` for bisector, and `2` for boundary.
10
30
11
- def create_interior_straight_skeleton (points ) -> PolylinesNumpy :
31
+ Returns
32
+ -------
33
+ :class:`compas.datastructures.Graph`
34
+ The skeleton as a graph.
35
+ """
36
+ graph = Graph ()
37
+ for pt , i in zip (points , indices ):
38
+ graph .add_node (key = i , x = pt [0 ], y = pt [1 ], z = pt [2 ])
39
+
40
+ for edge , etype in zip (edges , edge_types ):
41
+ edge = graph .add_edge (* edge )
42
+ if etype == 0 :
43
+ graph .edge_attribute (edge , "inner_bisector" , True )
44
+ elif etype == 1 :
45
+ graph .edge_attribute (edge , "bisector" , True )
46
+ else :
47
+ graph .edge_attribute (edge , "boundary" , True )
48
+ return graph
49
+
50
+
51
+ def create_interior_straight_skeleton (points , as_graph = True ) -> Union [Graph , Tuple [VerticesNumpy , IntNx1 , IntNx2 , IntNx1 ]]:
12
52
"""Compute the skeleton of a polygon.
13
53
14
54
Parameters
15
55
----------
16
56
points : list of point coordinates or :class:`compas.geometry.Polygon`
17
57
The points of the polygon.
58
+ as_graph : bool, optional
59
+ Whether the skeleton should be returned as a graph, defaults to `True`.
18
60
19
61
Returns
20
62
-------
21
- :attr:`compas_cgal.types.PolylinesNumpy`
63
+ :attr:`compas.datastructures.Graph` or tuple of (vertices, indices, edges, edge_types)
22
64
The skeleton of the polygon.
23
65
24
66
Raises
@@ -31,10 +73,13 @@ def create_interior_straight_skeleton(points) -> PolylinesNumpy:
31
73
if not TOL .is_allclose (normal , [0 , 0 , 1 ]):
32
74
raise ValueError ("The normal of the polygon should be [0, 0, 1]. The normal of the provided polygon is {}" .format (normal ))
33
75
V = np .asarray (points , dtype = np .float64 )
34
- return straight_skeleton_2 .create_interior_straight_skeleton (V )
76
+ points , indices , edges , edge_types = straight_skeleton_2 .create_interior_straight_skeleton (V )
77
+ if as_graph :
78
+ return graph_from_skeleton_data (points , indices , edges , edge_types )
79
+ return points , indices , edges , edge_types
35
80
36
81
37
- def create_interior_straight_skeleton_with_holes (points , holes ) -> PolylinesNumpy :
82
+ def create_interior_straight_skeleton_with_holes (points , holes , as_graph = True ) -> Union [ Graph , Tuple [ VerticesNumpy , IntNx1 , IntNx2 , IntNx1 ]] :
38
83
"""Compute the skeleton of a polygon with holes.
39
84
40
85
Parameters
@@ -43,10 +88,12 @@ def create_interior_straight_skeleton_with_holes(points, holes) -> PolylinesNump
43
88
The points of the polygon.
44
89
holes : list of list of point coordinates or list of :class:`compas.geometry.Polygon`
45
90
The holes of the polygon.
91
+ as_graph : bool, optional
92
+ Whether the skeleton should be returned as a graph, defaults to `True`.
46
93
47
94
Returns
48
95
-------
49
- :attr:`compas_cgal.types.PolylinesNumpy`
96
+ :attr:`compas.datastructures.Graph` or tuple of (vertices, indices, edges, edge_types)
50
97
The skeleton of the polygon.
51
98
52
99
Raises
@@ -69,7 +116,10 @@ def create_interior_straight_skeleton_with_holes(points, holes) -> PolylinesNump
69
116
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
117
hole = np .asarray (points , dtype = np .float64 )
71
118
H .append (hole )
72
- return straight_skeleton_2 .create_interior_straight_skeleton_with_holes (V , H )
119
+ points , indices , edges , edge_types = straight_skeleton_2 .create_interior_straight_skeleton_with_holes (V , H )
120
+ if as_graph :
121
+ return graph_from_skeleton_data (points , indices , edges , edge_types )
122
+ return points , indices , edges , edge_types
73
123
74
124
75
125
def create_offset_polygons_2 (points , offset ) -> list [Polygon ]:
0 commit comments