blob: 183f7bfa1b1751546852046cda189992af4a3bf7 [file] [log] [blame]
Parker Schuhdc682952018-03-03 18:24:01 -08001from __future__ import print_function
Parker Schuh19b93b12018-03-02 23:26:58 -08002import os
3import basic_window
Parker Schuhdc682952018-03-03 18:24:01 -08004import random
Parker Schuh19b93b12018-03-02 23:26:58 -08005import gi
6import numpy
7gi.require_version('Gtk', '3.0')
8from gi.repository import Gdk
9import cairo
10import graph_generate
11from graph_generate import XYSegment, AngleSegment, to_theta, to_xy, alpha_blend
12from graph_generate import back_to_xy_loop, subdivide_theta, to_theta_loop
13from graph_generate import l1, l2, joint_center
14
15from basic_window import OverrideMatrix, identity, quit_main_loop
16
17import shapely
18from shapely.geometry import Polygon
19
Parker Schuhdc682952018-03-03 18:24:01 -080020
Parker Schuh19b93b12018-03-02 23:26:58 -080021def px(cr):
Parker Schuhdc682952018-03-03 18:24:01 -080022 return OverrideMatrix(cr, identity)
Parker Schuh19b93b12018-03-02 23:26:58 -080023
Parker Schuhdc682952018-03-03 18:24:01 -080024
Parker Schuh19b93b12018-03-02 23:26:58 -080025def draw_px_cross(cr, length_px):
Parker Schuhdc682952018-03-03 18:24:01 -080026 """Draws a cross with fixed dimensions in pixel space."""
27 with px(cr):
28 x, y = cr.get_current_point()
29 cr.move_to(x, y - length_px)
30 cr.line_to(x, y + length_px)
31 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -080032
Parker Schuhdc682952018-03-03 18:24:01 -080033 cr.move_to(x - length_px, y)
34 cr.line_to(x + length_px, y)
35 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -080036
Parker Schuhdc682952018-03-03 18:24:01 -080037
Parker Schuh19b93b12018-03-02 23:26:58 -080038def angle_dist_sqr(a1, a2):
Parker Schuhdc682952018-03-03 18:24:01 -080039 """Distance between two points in angle space."""
40 return (a1[0] - a2[0])**2 + (a1[1] - a2[1])**2
41
Parker Schuh19b93b12018-03-02 23:26:58 -080042
43# Find the highest y position that intersects the vertical line defined by x.
44def inter_y(x):
Parker Schuhdc682952018-03-03 18:24:01 -080045 return numpy.sqrt((l2 + l1)**2 -
46 (x - joint_center[0])**2) + joint_center[1]
47
Parker Schuh19b93b12018-03-02 23:26:58 -080048
49# This is the x position where the inner (hyperextension) circle intersects the horizontal line
Parker Schuhdc682952018-03-03 18:24:01 -080050derr = numpy.sqrt((l1 - l2)**2 - (joint_center[1] - 0.3048)**2)
51
Parker Schuh19b93b12018-03-02 23:26:58 -080052
53# Define min and max l1 angles based on vertical constraints.
54def get_angle(boundary):
Parker Schuhdc682952018-03-03 18:24:01 -080055 h = numpy.sqrt((l1)**2 - (boundary - joint_center[0])**2) + joint_center[1]
56 return numpy.arctan2(h, boundary - joint_center[0])
57
Parker Schuh19b93b12018-03-02 23:26:58 -080058
59# left hand side lines
60lines1 = [
Parker Schuhdc682952018-03-03 18:24:01 -080061 (-0.826135, inter_y(-0.826135)),
62 (-0.826135, 0.1397),
63 (-23.025 * 0.0254, 0.1397),
64 (-23.025 * 0.0254, 0.3048),
65 (joint_center[0] - derr, 0.3048),
Parker Schuh19b93b12018-03-02 23:26:58 -080066]
67
68# right hand side lines
Parker Schuhdc682952018-03-03 18:24:01 -080069lines2 = [(joint_center[0] + derr, 0.3048), (0.422275, 0.3048),
70 (0.422275, 0.1397), (0.826135, 0.1397), (0.826135,
71 inter_y(0.826135))]
Parker Schuh19b93b12018-03-02 23:26:58 -080072
Parker Schuhdc682952018-03-03 18:24:01 -080073t1_min = get_angle((32.525 - 4.0) * 0.0254)
74t2_min = -7.0 / 4.0 * numpy.pi
Parker Schuh19b93b12018-03-02 23:26:58 -080075
Parker Schuhdc682952018-03-03 18:24:01 -080076t1_max = get_angle((-32.525 + 4.0) * 0.0254)
77t2_max = numpy.pi * 3.0 / 4.0
78
Parker Schuh19b93b12018-03-02 23:26:58 -080079
80# Draw lines to cr + stroke.
81def draw_lines(cr, lines):
Parker Schuhdc682952018-03-03 18:24:01 -080082 cr.move_to(lines[0][0], lines[0][1])
83 for pt in lines[1:]:
84 cr.line_to(pt[0], pt[1])
85 with px(cr):
86 cr.stroke()
87
Parker Schuh19b93b12018-03-02 23:26:58 -080088
89# Rotate a rasterized loop such that it aligns to when the parameters loop
90def rotate_to_jump_point(points):
Parker Schuhdc682952018-03-03 18:24:01 -080091 last_pt = points[0]
92 for pt_i in range(1, len(points)):
93 pt = points[pt_i]
94 delta = last_pt[1] - pt[1]
95 if abs(delta) > numpy.pi:
96 print(delta)
97 return points[pt_i:] + points[:pt_i]
98 last_pt = pt
99 return points
100
Parker Schuh19b93b12018-03-02 23:26:58 -0800101
102# shift points vertically by dy.
103def y_shift(points, dy):
Parker Schuhdc682952018-03-03 18:24:01 -0800104 return [(x, y + dy) for x, y in points]
105
Parker Schuh19b93b12018-03-02 23:26:58 -0800106
107lines1_theta_part = rotate_to_jump_point(to_theta_loop(lines1, 0))
108lines2_theta_part = rotate_to_jump_point(to_theta_loop(lines2))
109
110# Some hacks here to make a single polygon by shifting to get an extra copy of the contraints.
111lines1_theta = y_shift(lines1_theta_part, -numpy.pi * 2) + lines1_theta_part + \
112 y_shift(lines1_theta_part, numpy.pi * 2)
113lines2_theta = y_shift(lines2_theta_part, numpy.pi * 2) + lines2_theta_part + \
114 y_shift(lines2_theta_part, -numpy.pi * 2)
115
116lines_theta = lines1_theta + lines2_theta
117
118p1 = Polygon(lines_theta)
119
Parker Schuhdc682952018-03-03 18:24:01 -0800120p2 = Polygon([(t1_min, t2_min), (t1_max, t2_min), (t1_max, t2_max), (t1_min,
121 t2_max)])
Parker Schuh19b93b12018-03-02 23:26:58 -0800122
123# Fully computed theta constrints.
124lines_theta = list(p1.intersection(p2).exterior.coords)
125
Parker Schuhdc682952018-03-03 18:24:01 -0800126print("Theta constraint.")
127print(", ".join("{%s, %s}" % (a, b) for a, b in lines_theta))
Parker Schuh19b93b12018-03-02 23:26:58 -0800128
129lines1_theta_back = back_to_xy_loop(lines1_theta)
130lines2_theta_back = back_to_xy_loop(lines2_theta)
131
132lines_theta_back = back_to_xy_loop(lines_theta)
133
Parker Schuhdc682952018-03-03 18:24:01 -0800134
Parker Schuh19b93b12018-03-02 23:26:58 -0800135# Get the closest point to a line from a test pt.
136def get_closest(prev, cur, pt):
Parker Schuhdc682952018-03-03 18:24:01 -0800137 dx_ang = (cur[0] - prev[0])
138 dy_ang = (cur[1] - prev[1])
Parker Schuh19b93b12018-03-02 23:26:58 -0800139
Parker Schuhdc682952018-03-03 18:24:01 -0800140 d = numpy.sqrt(dx_ang**2 + dy_ang**2)
141 if (d < 0.000001):
142 return prev, numpy.sqrt((prev[0] - pt[0])**2 + (prev[1] - pt[1])**2)
143
144 pdx = -dy_ang / d
145 pdy = dx_ang / d
146
147 dpx = pt[0] - prev[0]
148 dpy = pt[1] - prev[1]
149
150 alpha = (dx_ang * dpx + dy_ang * dpy) / d / d
151
152 if (alpha < 0):
153 return prev, numpy.sqrt((prev[0] - pt[0])**2 + (prev[1] - pt[1])**2)
154 elif (alpha > 1):
155 return cur, numpy.sqrt((cur[0] - pt[0])**2 + (cur[1] - pt[1])**2)
156 else:
157 return (alpha_blend(prev[0], cur[0], alpha), alpha_blend(prev[1], cur[1], alpha)), \
158 abs(dpx * pdx + dpy * pdy)
Parker Schuh19b93b12018-03-02 23:26:58 -0800159
160
Parker Schuhdc682952018-03-03 18:24:01 -0800161#
Parker Schuh19b93b12018-03-02 23:26:58 -0800162def closest_segment(lines, pt):
Parker Schuhdc682952018-03-03 18:24:01 -0800163 c_pt, c_pt_dist = get_closest(lines[-1], lines[0], pt)
164 for i in range(1, len(lines)):
165 prev = lines[i - 1]
166 cur = lines[i]
167 c_pt_new, c_pt_new_dist = get_closest(prev, cur, pt)
168 if c_pt_new_dist < c_pt_dist:
169 c_pt = c_pt_new
170 c_pt_dist = c_pt_new_dist
171 return c_pt, c_pt_dist
172
Parker Schuh19b93b12018-03-02 23:26:58 -0800173
174# Create a GTK+ widget on which we will draw using Cairo
175class Silly(basic_window.BaseWindow):
Parker Schuhdc682952018-03-03 18:24:01 -0800176 def __init__(self):
177 super(Silly, self).__init__()
Parker Schuh19b93b12018-03-02 23:26:58 -0800178
Parker Schuhdc682952018-03-03 18:24:01 -0800179 self.theta_version = True
180 self.reinit_extents()
Parker Schuh19b93b12018-03-02 23:26:58 -0800181
Parker Schuhdc682952018-03-03 18:24:01 -0800182 self.last_pos = (numpy.pi / 2.0, 1.0)
183 self.circular_index_select = -1
Parker Schuh19b93b12018-03-02 23:26:58 -0800184
Parker Schuhdc682952018-03-03 18:24:01 -0800185 # Extra stuff for drawing lines.
186 self.segments = []
187 self.prev_segment_pt = None
188 self.now_segment_pt = None
Parker Schuh19b93b12018-03-02 23:26:58 -0800189
Parker Schuhdc682952018-03-03 18:24:01 -0800190 def reinit_extents(self):
191 if self.theta_version:
192 self.extents_x_min = -numpy.pi * 2
193 self.extents_x_max = numpy.pi * 2
194 self.extents_y_min = -numpy.pi * 2
195 self.extents_y_max = numpy.pi * 2
Parker Schuh19b93b12018-03-02 23:26:58 -0800196 else:
Parker Schuhdc682952018-03-03 18:24:01 -0800197 self.extents_x_min = -40.0 * 0.0254
198 self.extents_x_max = 40.0 * 0.0254
199 self.extents_y_min = -4.0 * 0.0254
200 self.extents_y_max = 110.0 * 0.0254
Parker Schuh19b93b12018-03-02 23:26:58 -0800201
Parker Schuhdc682952018-03-03 18:24:01 -0800202 self.init_extents(
203 (0.5 * (self.extents_x_min + self.extents_x_max), 0.5 *
204 (self.extents_y_max + self.extents_y_min)),
205 (1.0 * (self.extents_x_max - self.extents_x_min), 1.0 *
206 (self.extents_y_max - self.extents_y_min)))
Parker Schuh19b93b12018-03-02 23:26:58 -0800207
Parker Schuhdc682952018-03-03 18:24:01 -0800208 # Handle the expose-event by drawing
209 def handle_draw(self, cr):
210 # use "with px(cr): blah;" to transform to pixel coordinates.
Parker Schuh19b93b12018-03-02 23:26:58 -0800211
Parker Schuhdc682952018-03-03 18:24:01 -0800212 # Fill the background color of the window with grey
213 cr.set_source_rgb(0.5, 0.5, 0.5)
214 cr.paint()
Parker Schuh19b93b12018-03-02 23:26:58 -0800215
Parker Schuhdc682952018-03-03 18:24:01 -0800216 # Draw a extents rectangle
217 cr.set_source_rgb(1.0, 1.0, 1.0)
218 cr.rectangle(self.extents_x_min, self.extents_y_min,
219 (self.extents_x_max - self.extents_x_min),
220 self.extents_y_max - self.extents_y_min)
221 cr.fill()
Parker Schuh19b93b12018-03-02 23:26:58 -0800222
Parker Schuhdc682952018-03-03 18:24:01 -0800223 if not self.theta_version:
224 # Draw a filled white rectangle.
225 cr.set_source_rgb(1.0, 1.0, 1.0)
226 cr.rectangle(-2.0, -2.0, 4.0, 4.0)
227 cr.fill()
Parker Schuh19b93b12018-03-02 23:26:58 -0800228
Parker Schuhdc682952018-03-03 18:24:01 -0800229 cr.set_source_rgb(0.0, 0.0, 1.0)
230 cr.arc(joint_center[0], joint_center[1], l2 + l1, 0,
231 2.0 * numpy.pi)
232 with px(cr):
233 cr.stroke()
234 cr.arc(joint_center[0], joint_center[1], l1 - l2, 0,
235 2.0 * numpy.pi)
236 with px(cr):
237 cr.stroke()
238 else:
239 # Draw a filled white rectangle.
240 cr.set_source_rgb(1.0, 1.0, 1.0)
241 cr.rectangle(-numpy.pi, -numpy.pi, numpy.pi * 2.0, numpy.pi * 2.0)
242 cr.fill()
Parker Schuh19b93b12018-03-02 23:26:58 -0800243
Parker Schuhdc682952018-03-03 18:24:01 -0800244 if self.theta_version:
245 cr.set_source_rgb(0.0, 0.0, 1.0)
246 for i in range(-6, 6):
247 cr.move_to(-40, -40 + i * numpy.pi)
248 cr.line_to(40, 40 + i * numpy.pi)
249 with px(cr):
250 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800251
Parker Schuhdc682952018-03-03 18:24:01 -0800252 if self.theta_version:
253 cr.set_source_rgb(0.5, 0.5, 1.0)
254 draw_lines(cr, lines_theta)
255 else:
256 cr.set_source_rgb(0.5, 1.0, 1.0)
257 draw_lines(cr, lines1)
258 draw_lines(cr, lines2)
Parker Schuh19b93b12018-03-02 23:26:58 -0800259
Parker Schuhdc682952018-03-03 18:24:01 -0800260 def set_color(cr, circular_index):
261 if circular_index == -2:
262 cr.set_source_rgb(0.0, 0.25, 1.0)
263 elif circular_index == -1:
264 cr.set_source_rgb(0.5, 0.0, 1.0)
265 elif circular_index == 0:
266 cr.set_source_rgb(0.5, 1.0, 1.0)
267 elif circular_index == 1:
268 cr.set_source_rgb(0.0, 0.5, 1.0)
269 elif circular_index == 2:
270 cr.set_source_rgb(0.5, 1.0, 0.5)
271 else:
272 cr.set_source_rgb(1.0, 0.0, 0.0)
Parker Schuh19b93b12018-03-02 23:26:58 -0800273
Parker Schuhdc682952018-03-03 18:24:01 -0800274 def get_circular_index(pt):
275 theta1, theta2 = pt
276 circular_index = int(numpy.floor((theta2 - theta1) / numpy.pi))
277 return circular_index
Parker Schuh19b93b12018-03-02 23:26:58 -0800278
Parker Schuhdc682952018-03-03 18:24:01 -0800279 cr.set_source_rgb(0.0, 0.0, 1.0)
280 lines = subdivide_theta(lines_theta)
281 o_circular_index = circular_index = get_circular_index(lines[0])
282 p_xy = to_xy(lines[0][0], lines[0][1])
283 if circular_index == self.circular_index_select:
284 cr.move_to(p_xy[0] + circular_index * 0, p_xy[1])
285 for pt in lines[1:]:
286 p_xy = to_xy(pt[0], pt[1])
287 circular_index = get_circular_index(pt)
288 if o_circular_index == self.circular_index_select:
289 cr.line_to(p_xy[0] + o_circular_index * 0, p_xy[1])
290 if circular_index != o_circular_index:
291 o_circular_index = circular_index
292 with px(cr):
293 cr.stroke()
294 if circular_index == self.circular_index_select:
295 cr.move_to(p_xy[0] + circular_index * 0, p_xy[1])
Parker Schuh19b93b12018-03-02 23:26:58 -0800296
Parker Schuhdc682952018-03-03 18:24:01 -0800297 with px(cr):
298 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800299
Parker Schuhdc682952018-03-03 18:24:01 -0800300 if not self.theta_version:
301 theta1, theta2 = to_theta(self.last_pos, self.circular_index_select)
302 x, y = joint_center[0], joint_center[1]
303 cr.move_to(x, y)
Parker Schuh19b93b12018-03-02 23:26:58 -0800304
Parker Schuhdc682952018-03-03 18:24:01 -0800305 x += numpy.cos(theta1) * l1
306 y += numpy.sin(theta1) * l1
307 cr.line_to(x, y)
308 x += numpy.cos(theta2) * l2
309 y += numpy.sin(theta2) * l2
310 cr.line_to(x, y)
311 with px(cr):
312 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800313
Parker Schuhdc682952018-03-03 18:24:01 -0800314 cr.move_to(self.last_pos[0], self.last_pos[1])
315 cr.set_source_rgb(0.0, 1.0, 0.2)
316 draw_px_cross(cr, 20)
Parker Schuh19b93b12018-03-02 23:26:58 -0800317
Parker Schuhdc682952018-03-03 18:24:01 -0800318 if self.theta_version:
319 cr.set_source_rgb(0.0, 1.0, 0.2)
Parker Schuh19b93b12018-03-02 23:26:58 -0800320
Parker Schuhdc682952018-03-03 18:24:01 -0800321 cr.set_source_rgb(0.0, 1.0, 0.2)
322 cr.move_to(self.last_pos[0], self.last_pos[1])
323 draw_px_cross(cr, 5)
Parker Schuh19b93b12018-03-02 23:26:58 -0800324
Parker Schuhdc682952018-03-03 18:24:01 -0800325 c_pt, dist = closest_segment(lines_theta, self.last_pos)
326 print("dist:", dist, c_pt, self.last_pos)
327 cr.set_source_rgb(0.0, 1.0, 1.0)
328 cr.move_to(c_pt[0], c_pt[1])
329 draw_px_cross(cr, 5)
Parker Schuh19b93b12018-03-02 23:26:58 -0800330
Parker Schuhdc682952018-03-03 18:24:01 -0800331 cr.set_source_rgb(0.0, 0.5, 1.0)
332 for segment in self.segments:
333 color = [0, random.random(), 1]
334 random.shuffle(color)
335 cr.set_source_rgb(*color)
336 segment.DrawTo(cr, self.theta_version)
337 with px(cr):
338 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800339
Parker Schuhdc682952018-03-03 18:24:01 -0800340 cr.set_source_rgb(0.0, 1.0, 0.5)
341 segment = self.current_seg()
342 if segment:
343 print(segment)
344 segment.DrawTo(cr, self.theta_version)
345 with px(cr):
346 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800347
Parker Schuhdc682952018-03-03 18:24:01 -0800348 def cur_pt_in_theta(self):
349 if self.theta_version: return self.last_pos
350 return to_theta(self.last_pos, self.circular_index_select)
351
352 # Current segment based on which mode the drawing system is in.
353 def current_seg(self):
354 if self.prev_segment_pt and self.now_segment_pt:
355 if self.theta_version:
356 return AngleSegment(self.prev_segment_pt, self.now_segment_pt)
357 else:
358 return XYSegment(self.prev_segment_pt, self.now_segment_pt)
359
360 def do_key_press(self, event):
361 keyval = Gdk.keyval_to_lower(event.keyval)
362 print("Gdk.KEY_" + Gdk.keyval_name(keyval))
363 if keyval == Gdk.KEY_q:
364 print("Found q key and exiting.")
365 quit_main_loop()
366 elif keyval == Gdk.KEY_c:
367 # Increment which arm solution we render
368 self.circular_index_select += 1
369 print(self.circular_index_select)
370 elif keyval == Gdk.KEY_v:
371 # Decrement which arm solution we render
372 self.circular_index_select -= 1
373 print(self.circular_index_select)
374 elif keyval == Gdk.KEY_w:
375 # Add this segment to the segment list.
376 segment = self.current_seg()
377 if segment: self.segments.append(segment)
378 self.prev_segment_pt = self.now_segment_pt
379
380 elif keyval == Gdk.KEY_r:
381 self.prev_segment_pt = self.now_segment_pt
382
383 elif keyval == Gdk.KEY_p:
384 # Print out the segments.
385 print(repr(self.segments))
386 elif keyval == Gdk.KEY_g:
387 # Generate theta points.
388 if self.segments:
389 print(repr(self.segments[0].ToThetaPoints()))
390 elif keyval == Gdk.KEY_e:
391 best_pt = self.now_segment_pt
392 best_dist = 1e10
393 for segment in self.segments:
394 d = angle_dist_sqr(segment.start, self.now_segment_pt)
395 if (d < best_dist):
396 best_pt = segment.start
397 best_dist = d
398 d = angle_dist_sqr(segment.end, self.now_segment_pt)
399 if (d < best_dist):
400 best_pt = segment.end
401 best_dist = d
402 self.now_segment_pt = best_pt
403
404 elif keyval == Gdk.KEY_t:
405 # Toggle between theta and xy renderings
406 if self.theta_version:
407 theta1, theta2 = self.last_pos
408 data = to_xy(theta1, theta2)
409 self.circular_index_select = int(
410 numpy.floor((theta2 - theta1) / numpy.pi))
411 self.last_pos = (data[0], data[1])
412 else:
413 self.last_pos = self.cur_pt_in_theta()
414
415 self.theta_version = not self.theta_version
416 self.reinit_extents()
417 self.redraw()
418
419 def do_button_press(self, event):
420 self.last_pos = (event.x, event.y)
421 self.now_segment_pt = self.cur_pt_in_theta()
422 print('Clicked at theta: (%f, %f)' % (self.now_segment_pt[0],
423 self.now_segment_pt[1]))
424 if not self.theta_version:
425 print('Clicked at xy, circular index: (%f, %f, %f)' %
426 (self.last_pos[0], self.last_pos[1],
427 self.circular_index_select))
428
429 self.redraw()
430
Parker Schuh19b93b12018-03-02 23:26:58 -0800431
432silly = Silly()
Parker Schuhdc682952018-03-03 18:24:01 -0800433silly.segments = graph_generate.segments
Parker Schuh19b93b12018-03-02 23:26:58 -0800434basic_window.RunApp()