forked from aewallin/linuxcnc-scripts
/
ttt2medial.py
143 lines (124 loc) · 4.9 KB
/
ttt2medial.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import openvoronoi as ovd # https://github.com/aewallin/openvoronoi
import ttt # https://github.com/aewallin/truetype-tracer
import time
import ngc_writer # https://github.com/aewallin/linuxcnc-scripts
ngc_writer.clearance_height=10
ngc_writer.plunge_height = 2
ngc_writer.feed = 200
ngc_writer.plunge_feed = 100
scale = 7
def printMedial(vd):
maw = ovd.MedialAxisWalk( vd.getGraph() )
toolpath = maw.walk()
for chain in toolpath:
n = 0
for move in chain:
for point in move:
if n==0: # don't draw anything on the first iteration
p = point[0]
z = point[1]
ngc_writer.pen_up();
ngc_writer.xy_rapid_to( scale*p.x, scale*p.y );
ngc_writer.pen_down()
ngc_writer.plunge( -z ) # now we are at the correct height, at the startpoint of the first move
else:
p = point[0]
z = point[1]
ngc_writer.line_to( scale*p.x, scale*p.y, scale*(-z) )
n=n+1
return
# this function inserts point-sites into vd
def insert_polygon_points(vd, polygon):
pts=[]
for p in polygon:
pts.append( ovd.Point( p[0], p[1] ) )
id_list = []
#print "inserting ",len(pts)," point-sites:"
m=0
for p in pts:
id_list.append( vd.addVertexSite( p ) )
#print " ",m," added vertex ", id_list[ len(id_list) -1 ]
m=m+1
return id_list
# this function inserts line-segments into vd
def insert_polygon_segments(vd,id_list):
j=0
#print "inserting ",len(id_list)," line-segments:"
for n in range(len(id_list)):
n_nxt = n+1
if n==(len(id_list)-1):
n_nxt=0
#print " ",j,"inserting segement ",id_list[n]," - ",id_list[n_nxt]
vd.addLineSite( id_list[n], id_list[n_nxt])
j=j+1
# this function takes all segments from ttt and inserts them into vd
def insert_many_polygons(vd,segs):
polygon_ids =[]
t_before = time.time()
for poly in segs:
poly_id = insert_polygon_points(vd,poly)
polygon_ids.append(poly_id)
t_after = time.time()
pt_time = t_after-t_before
t_before = time.time()
for ids in polygon_ids:
insert_polygon_segments(vd,ids)
t_after = time.time()
seg_time = t_after-t_before
return [pt_time, seg_time]
# this translates segments from ttt
def translate(segs,x,y):
out = []
for seg in segs:
seg2 = []
for p in seg:
p2 = []
p2.append(p[0] + x)
p2.append(p[1] + y)
seg2.append(p2)
out.append(seg2)
return out
# modify by deleting last point (since it is identical to the first point)
def modify_segments(segs):
segs_mod =[]
for seg in segs:
first = seg[0]
last = seg[ len(seg)-1 ]
assert( first[0]==last[0] and first[1]==last[1] )
seg.pop()
seg.reverse() # to get interior or exterior offsets. Try commenting out this and see what happens.
segs_mod.append(seg)
return segs_mod
# get segments from ttt
def ttt_segments(text,scale):
wr = ttt.SEG_Writer()
wr.arc = False # approximate arcs with lines
wr.conic = False # approximate conic with arc/line
wr.cubic = False # approximate cubic with arc/line
wr.conic_biarc_subdivision = 10 # this has no effect?
wr.conic_line_subdivision = 50 # =10 increasesn nr of points to 366, = 5 gives 729 pts
wr.cubic_biarc_subdivision = 10 # no effect?
wr.cubic_line_subdivision = 10 # no effect?
wr.scale = float(1)/float(scale)
ttt.ttt(text,wr)
segs = wr.get_segments()
return segs
if __name__ == "__main__":
vd = ovd.VoronoiDiagram(1,120) # parameters: (r,bins)
# float r = radius within which all geometry is located. it is best to use 1 (unit-circle) for now.
# int bins = number of bins for grid-search (affects performance, should not affect correctness)
# get segments from ttt. NOTE: must set scale so all geometry fits within unit-circle!
segs = ttt_segments( "EMC2", 15000) # (text, scale) all coordinates are divided by scale
segs = translate(segs, -0.06, 0.05)
segs = modify_segments(segs)
times = insert_many_polygons(vd,segs) # insert segments into vd
print "( ttt2medial.py - experimental v-carving script )"
print "( TTT++",ttt.version()," )"
print "( OpenVoronoi",vd.version()," )"
print "( VD built in %02.3f seconds )" % ( sum(times))
print "( VD check: ", vd.check(), " )"
ovd.PolygonInterior( vd.getGraph() , True ) # filter so that only polygon interior remains
ovd.MedialAxis( vd.getGraph() ) # filter so that only medial axis remains
ngc_writer.preamble()
printMedial( vd )
ngc_writer.postamble()