-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslplat_utils.py
304 lines (217 loc) · 7.57 KB
/
slplat_utils.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import math
def nb(val): # check for not boolean.
return not isinstance(val,bool)
def get_xy(mb):
def set_mode(key):
nonlocal mode
if key in ['dir','delta','radius','length','cw','ccw','t','tangent','right','left']:
mode = key
return True
return False
set_mode("dir")
angle = 0.0
degree_fact = 1.0
dir = ""
tangent_dir = distance = radius = arc_length = delta_angle = cw = tangent_curve = curve_dir = False
rx = []
ry = []
for m in mb.split():
if set_mode(m):
if mode == 'delta':
angle = 0.0
degree_fact = 1.0
if mode == 'ccw':
cw = -1
if mode == 'cw':
cw = 1
if mode == 't' or mode == 'tangent':
tangent_curve = 1
if mode == 'r' or mode == 'right':
curve_dir = 1
if mode == 'l' or mode == 'left':
curve_dir = -1
continue
try:
val = float(m)
except:
if m.lower() in 'nswe':
dir += (m.lower())
if len(dir) == 2:
mode = 'distance'
continue
else:
if mode == 'dir':
angle += val*degree_fact
degree_fact /= 60.0
tangent_dir = angle
if mode == 'distance':
distance = val
if mode == 'delta':
angle += val*degree_fact
degree_fact /= 60.0
delta_angle = angle
if mode == 'radius':
radius = val
if mode == 'length':
arc_length = val
if nb(tangent_dir):
if dir == 'ne':
pass
else:
if dir == 'nw':
tangent_dir = 360 - tangent_dir
else:
if dir == 'se':
tangent_dir = 180 - tangent_dir
else:
if dir == 'sw':
tangent_dir = 180 + tangent_dir
else:
raise ValueError(mb)
tangent_dir %= 360.0
if nb(distance):
x = math.sin(math.radians(tangent_dir)) * distance
y = math.cos(math.radians(tangent_dir)) * distance
rx.append(x)
ry.append(y)
#storyofmathematics.com/circle-chord
if nb(tangent_dir) and nb(tangent_curve) and nb(curve_dir):
if curve_dir == 1:
delta_angle = 90
cw = 1
else:
delta_angle = 270
cw = -1
#print()
#print()
#print("tangent_curve:",tangent_curve)
#print("curve_dir:",curve_dir)
#print("tangent_dir",tangent_dir)
#print("delta_angle",delta_angle)
#print("radius:",radius)
#print("arc_length:",arc_length)
#print("cw:",cw)
if nb(tangent_dir) and nb(delta_angle) and nb(radius) and nb(arc_length) and nb(cw):
#print("found arc dir_angle({}) delta({}) radius({}) length({})".format(tangent_dir,delta_angle,radius,arc_length))
arc_measure = arc_length/radius # radians
radial_dir = math.radians( (tangent_dir + delta_angle) % 360) # dir to center of circle
inside_triangle_angle = math.pi/2 - arc_measure/2
chord_direction = radial_dir + inside_triangle_angle
chord_length = 2 * radius * math.sin(arc_measure/2)
#print("arc_length",arc_length)
#print("radius:",radius)
#print("arc_measure",math.degrees(arc_length/radius))
#print("tangent_dir:",tangent_dir)
#print("delta_angle:",delta_angle)
#print("radial_dir:",math.degrees(radial_dir))
#print("inside_triangle_angle:",math.degrees(inside_triangle_angle))
#print("chord_direction:",math.degrees(chord_direction))
#print("chord_length:",chord_length)
degrees_per_step = 15.0
arc_step_count = round(math.degrees(arc_measure)/degrees_per_step) + 1 # so you get at least 1
arc_step = arc_measure/arc_step_count
#print('arc_step:',arc_step,type(arc_step))
ref_x = radius * math.sin(radial_dir)
ref_y = radius * math.cos(radial_dir)
for i in range(arc_step_count):
radial_dir += arc_step * cw
x = radius * math.sin(radial_dir)
y = radius * math.cos(radial_dir)
rx.append(ref_x - x)
# ry.append(-(ref_y - y))
ry.append(ref_y - y)
ref_x = x
ref_y = y
return rx,ry
def get_path_points(origin,path):
x = [origin[0]]
y = [origin[1]]
branches = []
scale = 1.0 #pixels per foot
property_sets = []
segment_sets = []
property_set = {'ls': 'solid','color':'black','alpha': .5}
for mb in path:
if type(mb) is list:
branch_segments,branch_property_sets = get_path_points([x[-1],y[-1]],mb)
segment_sets.extend(branch_segments)
property_sets.extend(branch_property_sets)
if type(mb) is dict:
if len(x) > 1:
segment_sets.append({'x':x,'y':y})
property_sets.append(property_set)
x = [x[-1]]
y = [y[-1]]
property_set = property_set.copy()
for key in mb.keys():
property_set[key] = mb[key]
if type(mb) is str:
if mb == "pob":
x = [x[-1]]
y = [y[-1]]
continue
px,py = get_xy(mb)
for zx,zy in zip(px,py):
x.append(x[-1] + zx * scale)
y.append(y[-1] + zy * scale)
segment_sets.append({'x':x,'y':y})
property_sets.append(property_set)
return segment_sets,property_sets
def get_as_mb(segments):
"""Convert points back to metes and bounds
"""
ref_x = 0.0
ref_y = 0.0
mbs = []
for segment in segments:
xa = segment['x']
ya = segment['y']
for x,y in zip(xa,ya):
if x==0 and y==0:
continue
dx = x - ref_x
dy = y - ref_y
mb = ""
if dx == 0:
if dy >= 0:
mb = 'n 0 e {}'.format(dy)
else:
mb = 's 0 w {}'.format(-dy)
else:
if dy == 0:
if dx >= 0:
mb = 'n 90 e {}'.format(dx)
else:
mb = 'n 90 w {}'.format(-dx)
else:
ang = math.degrees(math.atan(dx/dy))
ang = round(ang,10)
if dy >= 0:
mb = "n {}".format(abs(ang))
if dx >= 0:
mb += " e"
else:
mb += " w"
else:
mb = "s {}".format(abs(ang))
if dx >= 0:
mb += " e"
else:
mb += " w"
length = math.sqrt(dx**2 + dy**2)
length = round(length,10)
mb += " {}".format(length)
ref_x = x
ref_y = y
mbs.append(mb)
return mbs
def last_point(segments):
seg = segments[-1]
return [seg['x'][-1],seg['y'][-1]]
class plot_info():
def __init__(self,text,origin,path,aux_info = None):
self.text = text
self.origin = origin
self.path = path
self.selected = False
self.aux_info = aux_info