Skip to content

Commit ed7660f

Browse files
committed
update code
1 parent e531948 commit ed7660f

File tree

3 files changed

+69
-98
lines changed

3 files changed

+69
-98
lines changed

src/compas_triangle/__init__.py

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,4 @@
1-
"""
2-
********************************************************************************
3-
compas_triangle
4-
********************************************************************************
5-
6-
.. currentmodule:: compas_triangle
7-
8-
9-
.. toctree::
10-
:maxdepth: 1
11-
12-
13-
"""
14-
15-
from __future__ import print_function
16-
171
import os
18-
import compas
19-
202

213
__author__ = ["tom van mele"]
224
__copyright__ = "Block Research Group - ETH Zurich"
@@ -32,29 +14,5 @@
3214
DOCS = os.path.abspath(os.path.join(HOME, "docs"))
3315
TEMP = os.path.abspath(os.path.join(HOME, "temp"))
3416

35-
36-
# Check if package is installed from git
37-
# If that's the case, try to append the current head's hash to __version__
38-
try:
39-
git_head_file = compas._os.absjoin(HOME, '.git', 'HEAD')
40-
41-
if os.path.exists(git_head_file):
42-
# git head file contains one line that looks like this:
43-
# ref: refs/heads/master
44-
with open(git_head_file, 'r') as git_head:
45-
_, ref_path = git_head.read().strip().split(' ')
46-
ref_path = ref_path.split('/')
47-
48-
git_head_refs_file = compas._os.absjoin(HOME, '.git', *ref_path)
49-
50-
if os.path.exists(git_head_refs_file):
51-
with open(git_head_refs_file, 'r') as git_head_ref:
52-
git_commit = git_head_ref.read().strip()
53-
__version__ += '-' + git_commit[:8]
54-
except Exception:
55-
pass
56-
57-
58-
__all_plugins__ = ['compas_triangle.delaunay']
59-
6017
__all__ = ["HOME", "DATA", "DOCS", "TEMP"]
18+
__all_plugins__ = ["compas_triangle.delaunay"]

src/compas_triangle/delaunay.py

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
1-
from __future__ import absolute_import
2-
from __future__ import division
3-
from __future__ import print_function
1+
from typing import Annotated
2+
from typing import Optional
43

5-
from triangle import triangulate
6-
from compas.utilities import pairwise
7-
from compas.utilities import geometric_key_xy as geo
84
from compas.geometry import centroid_points_xy
9-
5+
from compas.itertools import pairwise
106
from compas.plugins import plugin
7+
from compas.tolerance import TOL
8+
from triangle import triangulate
9+
10+
geo = TOL.geometric_key_xy
11+
1112

13+
Point = Annotated[list[float], 3]
14+
Polyline = list[Point]
15+
Polygon = list[Point]
16+
Segment = tuple[int, int]
17+
Triangle = tuple[int, int, int]
1218

13-
def _to_vertices_segments_holes(boundary, polylines, polygons):
19+
20+
def _to_vertices_segments_holes(
21+
boundary: list[Point],
22+
polylines: list[Polyline],
23+
polygons: list[Polygon],
24+
) -> tuple[list[Point], list[Segment], list[Point]]:
1425
if geo(boundary[0]) != geo(boundary[-1]):
1526
boundary.append(boundary[0])
1627

@@ -47,8 +58,8 @@ def _to_vertices_segments_holes(boundary, polylines, polygons):
4758
return vertices, segments, holes
4859

4960

50-
@plugin(category='triangulation')
51-
def delaunay_triangulation(points):
61+
@plugin(category="triangulation")
62+
def delaunay_triangulation(points: list[Point]) -> tuple[list[Point], list[Triangle]]:
5263
"""Construct a Delaunay triangulation of set of vertices.
5364
5465
Parameters
@@ -62,24 +73,24 @@ def delaunay_triangulation(points):
6273
* The vertices of the triangulation.
6374
* The faces of the triangulation.
6475
65-
Examples
66-
--------
67-
>>>
68-
6976
References
7077
----------
7178
https://www.cs.cmu.edu/~quake/triangle.delaunay.html
7279
7380
"""
74-
data = {'vertices': [point[0:2] for point in points]}
75-
result = triangulate(data, opts='c')
76-
vertices = [[x, y, 0.0] for x, y in result['vertices']]
77-
faces = result['triangles']
81+
data = {"vertices": [point[0:2] for point in points]}
82+
result = triangulate(data, opts="c")
83+
vertices = [[x, y, 0.0] for x, y in result["vertices"]]
84+
faces = result["triangles"]
7885
return vertices, faces
7986

8087

81-
@plugin(category='triangulation')
82-
def constrained_delaunay_triangulation(boundary, polylines=None, polygons=None):
88+
@plugin(category="triangulation")
89+
def constrained_delaunay_triangulation(
90+
boundary: list[Point],
91+
polylines: Optional[list[Polyline]] = None,
92+
polygons: Optional[list[Polygon]] = None,
93+
) -> tuple[list[Point], list[Triangle]]:
8394
"""Construct a Delaunay triangulation of set of vertices, constrained to the specified segments.
8495
8596
Parameters
@@ -101,31 +112,33 @@ def constrained_delaunay_triangulation(boundary, polylines=None, polygons=None):
101112
-----
102113
No additional points will be inserted in the triangulation.
103114
104-
Examples
105-
--------
106-
>>>
107-
108115
References
109116
----------
110117
https://www.cs.cmu.edu/~quake/triangle.delaunay.html
111118
112119
"""
113120
vertices, segments, holes = _to_vertices_segments_holes(boundary, polylines, polygons)
114121

115-
data = {'vertices': vertices, 'segments': segments}
122+
data = {"vertices": vertices, "segments": segments}
116123

117124
if len(holes) > 0:
118-
data['holes'] = holes
125+
data["holes"] = holes
119126

120-
result = triangulate(data, opts='p')
127+
result = triangulate(data, opts="p")
121128

122-
vertices = [[x, y, 0.0] for x, y in result['vertices']]
123-
faces = result['triangles']
129+
vertices = [[x, y, 0.0] for x, y in result["vertices"]]
130+
faces = result["triangles"]
124131
return vertices, faces
125132

126133

127-
@plugin(category='triangulation')
128-
def conforming_delaunay_triangulation(boundary, polylines=None, polygons=None, angle=None, area=None):
134+
@plugin(category="triangulation")
135+
def conforming_delaunay_triangulation(
136+
boundary: list[Point],
137+
polylines: Optional[list[Polyline]] = None,
138+
polygons: Optional[list[Polygon]] = None,
139+
angle: Optional[float] = None,
140+
area: Optional[float] = None,
141+
) -> tuple[list[Point], list[Triangle]]:
129142
"""Construct a Conforming Delaunay triangulation of set of vertices, constrained to the specified segments.
130143
131144
Parameters
@@ -152,35 +165,31 @@ def conforming_delaunay_triangulation(boundary, polylines=None, polygons=None, a
152165
* The vertices of the triangulation.
153166
* The faces of the triangulation.
154167
155-
Examples
156-
--------
157-
>>>
158-
159168
References
160169
----------
161170
https://www.cs.cmu.edu/~quake/triangle.delaunay.html
162171
163172
"""
164173
vertices, segments, holes = _to_vertices_segments_holes(boundary, polylines, polygons)
165174

166-
data = {'vertices': vertices, 'segments': segments}
175+
data = {"vertices": vertices, "segments": segments}
167176

168177
if len(holes) > 0:
169-
data['holes'] = holes
178+
data["holes"] = holes
170179

171-
opts = 'pq'
180+
opts = "pq"
172181

173182
if angle:
174-
opts = '{}{}'.format(opts, angle)
183+
opts = "{}{}".format(opts, angle)
175184

176185
if area:
177-
opts = '{}a{}'.format(opts, area)
186+
opts = "{}a{}".format(opts, area)
178187

179-
if opts == 'pq':
180-
opts = 'pq0D'
188+
if opts == "pq":
189+
opts = "pq0D"
181190

182191
result = triangulate(data, opts=opts)
183192

184-
vertices = [[x, y, 0.0] for x, y in result['vertices']]
185-
faces = result['triangles']
193+
vertices = [[x, y, 0.0] for x, y in result["vertices"]]
194+
faces = result["triangles"]
186195
return vertices, faces

src/compas_triangle/rhino.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
1-
from __future__ import print_function
2-
from __future__ import absolute_import
3-
from __future__ import division
1+
import compas_rhino.conversions
2+
import compas_rhino.objects
3+
import rhinoscriptsyntax as rs # type: ignore
4+
from compas.geometry import Curve
45

5-
import compas_rhino
6-
from compas_rhino.conversions import RhinoCurve
6+
7+
def guid_to_compas_curve(guid) -> Curve:
8+
return compas_rhino.conversions.curveobject_to_compas(compas_rhino.objects.find_object(guid))
79

810

911
def discretise_boundary(guids, length):
1012
boundary = []
1113
for guid in guids:
12-
compas_rhino.rs.EnableRedraw(False)
13-
segments = compas_rhino.rs.ExplodeCurves(guid)
14+
rs.EnableRedraw(False)
15+
16+
segments = rs.ExplodeCurves(guid)
1417
for segment in segments:
15-
curve = RhinoCurve.from_guid(segment).to_compas()
18+
curve: Curve = guid_to_compas_curve(segment)
1619
N = int(curve.length() / length)
1720
_, points = curve.divide_by_count(N, return_points=True)
1821
boundary.extend(map(list, points))
19-
compas_rhino.rs.DeleteObjects(segments)
20-
compas_rhino.rs.EnableRedraw(True)
22+
23+
rs.DeleteObjects(segments)
24+
rs.EnableRedraw(True)
2125
return boundary
2226

2327

2428
def discretise_constraints(guids, length):
2529
polylines = []
2630
if guids:
2731
for guid in guids:
28-
curve = RhinoCurve.from_guid(guid).to_compas()
32+
curve: Curve = guid_to_compas_curve(guid)
2933
N = int(curve.length() / length)
3034
_, points = curve.divide_by_count(N, return_points=True)
3135
polylines.append(map(list, points))

0 commit comments

Comments
 (0)