-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmake_triangles.py
More file actions
60 lines (55 loc) · 1.87 KB
/
make_triangles.py
File metadata and controls
60 lines (55 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python
from __future__ import division, print_function
from generalfuncs import ux, uy, are_points_equal
from dataconvert import convert_polyline_to_polytri_version, import_p2t, triangle2threepoints
import sys
import fileinput
def make_triangles(polyline, holes = None):
"""This function takes a dictionary, and an optional holes parameter
that determines the holes of a polyline, and tesselates the polyline
into triangles. This is an intermediate step to calculating the midpoints"""
if holes is None:
holes = []
p2t = import_p2t()
triangles = []
new_polyline = convert_polyline_to_polytri_version(polyline)
if are_points_equal(new_polyline[-1], new_polyline[0]):
del new_polyline[-1]
cdt = p2t.CDT(new_polyline)
for hole in holes:
if are_points_equal(hole[-1], hole[0]):
del hole[-1]
hole = convert_polyline_to_polytri_version(hole)
cdt.add_hole(hole)
triangles.extend(cdt.triangulate())
return list(triangle2threepoints(t) for t in triangles)
def parse_input():
result = []
points = []
for line in fileinput.input():
line = line.strip().lstrip('(').rstrip(')')
if line == "HOLE:":
result.append(points)
points = []
parts = line.split(",", 1)
if len(parts) < 2:
# Skip bad input line
continue
x = float(parts[0])
y = float(parts[1])
points.append((x,y))
if len(points) > 0:
result.append(points)
return result
def main(argv):
line_and_holes = parse_input()
if len(line_and_holes) < 1:
return 1 # No data received!
line = line_and_holes[0]
holes = line_and_holes[1:]
result = make_triangles(line, holes)
for triangle in result:
print(triangle)
if __name__ == '__main__':
retcode = main(sys.argv)
sys.exit(retcode)