blob: 0694ae0d8bf21fc4d1395e20d67a9c9837245a2d [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:
Parker Schuhdc682952018-03-03 18:24:01 -080096 return points[pt_i:] + points[:pt_i]
97 last_pt = pt
98 return points
99
Parker Schuh19b93b12018-03-02 23:26:58 -0800100
101# shift points vertically by dy.
102def y_shift(points, dy):
Parker Schuhdc682952018-03-03 18:24:01 -0800103 return [(x, y + dy) for x, y in points]
104
Parker Schuh19b93b12018-03-02 23:26:58 -0800105
106lines1_theta_part = rotate_to_jump_point(to_theta_loop(lines1, 0))
107lines2_theta_part = rotate_to_jump_point(to_theta_loop(lines2))
108
109# Some hacks here to make a single polygon by shifting to get an extra copy of the contraints.
110lines1_theta = y_shift(lines1_theta_part, -numpy.pi * 2) + lines1_theta_part + \
111 y_shift(lines1_theta_part, numpy.pi * 2)
112lines2_theta = y_shift(lines2_theta_part, numpy.pi * 2) + lines2_theta_part + \
113 y_shift(lines2_theta_part, -numpy.pi * 2)
114
115lines_theta = lines1_theta + lines2_theta
116
117p1 = Polygon(lines_theta)
118
Parker Schuhdc682952018-03-03 18:24:01 -0800119p2 = Polygon([(t1_min, t2_min), (t1_max, t2_min), (t1_max, t2_max), (t1_min,
120 t2_max)])
Parker Schuh19b93b12018-03-02 23:26:58 -0800121
122# Fully computed theta constrints.
123lines_theta = list(p1.intersection(p2).exterior.coords)
124
Parker Schuh19b93b12018-03-02 23:26:58 -0800125lines1_theta_back = back_to_xy_loop(lines1_theta)
126lines2_theta_back = back_to_xy_loop(lines2_theta)
127
128lines_theta_back = back_to_xy_loop(lines_theta)
129
Parker Schuhdc682952018-03-03 18:24:01 -0800130
Parker Schuh19b93b12018-03-02 23:26:58 -0800131# Get the closest point to a line from a test pt.
132def get_closest(prev, cur, pt):
Parker Schuhdc682952018-03-03 18:24:01 -0800133 dx_ang = (cur[0] - prev[0])
134 dy_ang = (cur[1] - prev[1])
Parker Schuh19b93b12018-03-02 23:26:58 -0800135
Parker Schuhdc682952018-03-03 18:24:01 -0800136 d = numpy.sqrt(dx_ang**2 + dy_ang**2)
137 if (d < 0.000001):
138 return prev, numpy.sqrt((prev[0] - pt[0])**2 + (prev[1] - pt[1])**2)
139
140 pdx = -dy_ang / d
141 pdy = dx_ang / d
142
143 dpx = pt[0] - prev[0]
144 dpy = pt[1] - prev[1]
145
146 alpha = (dx_ang * dpx + dy_ang * dpy) / d / d
147
148 if (alpha < 0):
149 return prev, numpy.sqrt((prev[0] - pt[0])**2 + (prev[1] - pt[1])**2)
150 elif (alpha > 1):
151 return cur, numpy.sqrt((cur[0] - pt[0])**2 + (cur[1] - pt[1])**2)
152 else:
153 return (alpha_blend(prev[0], cur[0], alpha), alpha_blend(prev[1], cur[1], alpha)), \
154 abs(dpx * pdx + dpy * pdy)
Parker Schuh19b93b12018-03-02 23:26:58 -0800155
156
Parker Schuhdc682952018-03-03 18:24:01 -0800157#
Parker Schuh19b93b12018-03-02 23:26:58 -0800158def closest_segment(lines, pt):
Parker Schuhdc682952018-03-03 18:24:01 -0800159 c_pt, c_pt_dist = get_closest(lines[-1], lines[0], pt)
160 for i in range(1, len(lines)):
161 prev = lines[i - 1]
162 cur = lines[i]
163 c_pt_new, c_pt_new_dist = get_closest(prev, cur, pt)
164 if c_pt_new_dist < c_pt_dist:
165 c_pt = c_pt_new
166 c_pt_dist = c_pt_new_dist
167 return c_pt, c_pt_dist
168
Parker Schuh19b93b12018-03-02 23:26:58 -0800169
170# Create a GTK+ widget on which we will draw using Cairo
171class Silly(basic_window.BaseWindow):
Parker Schuhdc682952018-03-03 18:24:01 -0800172 def __init__(self):
173 super(Silly, self).__init__()
Parker Schuh19b93b12018-03-02 23:26:58 -0800174
Austin Schuhed018082018-07-08 16:01:01 -0700175 self.theta_version = False
Parker Schuhdc682952018-03-03 18:24:01 -0800176 self.reinit_extents()
Parker Schuh19b93b12018-03-02 23:26:58 -0800177
Parker Schuhdc682952018-03-03 18:24:01 -0800178 self.last_pos = (numpy.pi / 2.0, 1.0)
179 self.circular_index_select = -1
Parker Schuh19b93b12018-03-02 23:26:58 -0800180
Parker Schuhdc682952018-03-03 18:24:01 -0800181 # Extra stuff for drawing lines.
182 self.segments = []
183 self.prev_segment_pt = None
184 self.now_segment_pt = None
Parker Schuh19b93b12018-03-02 23:26:58 -0800185
Parker Schuhdc682952018-03-03 18:24:01 -0800186 def reinit_extents(self):
187 if self.theta_version:
188 self.extents_x_min = -numpy.pi * 2
189 self.extents_x_max = numpy.pi * 2
190 self.extents_y_min = -numpy.pi * 2
191 self.extents_y_max = numpy.pi * 2
Parker Schuh19b93b12018-03-02 23:26:58 -0800192 else:
Parker Schuhdc682952018-03-03 18:24:01 -0800193 self.extents_x_min = -40.0 * 0.0254
194 self.extents_x_max = 40.0 * 0.0254
195 self.extents_y_min = -4.0 * 0.0254
196 self.extents_y_max = 110.0 * 0.0254
Parker Schuh19b93b12018-03-02 23:26:58 -0800197
Parker Schuhdc682952018-03-03 18:24:01 -0800198 self.init_extents(
199 (0.5 * (self.extents_x_min + self.extents_x_max), 0.5 *
200 (self.extents_y_max + self.extents_y_min)),
201 (1.0 * (self.extents_x_max - self.extents_x_min), 1.0 *
202 (self.extents_y_max - self.extents_y_min)))
Parker Schuh19b93b12018-03-02 23:26:58 -0800203
Parker Schuhdc682952018-03-03 18:24:01 -0800204 # Handle the expose-event by drawing
205 def handle_draw(self, cr):
206 # use "with px(cr): blah;" to transform to pixel coordinates.
Parker Schuh19b93b12018-03-02 23:26:58 -0800207
Parker Schuhdc682952018-03-03 18:24:01 -0800208 # Fill the background color of the window with grey
209 cr.set_source_rgb(0.5, 0.5, 0.5)
210 cr.paint()
Parker Schuh19b93b12018-03-02 23:26:58 -0800211
Parker Schuhdc682952018-03-03 18:24:01 -0800212 # Draw a extents rectangle
213 cr.set_source_rgb(1.0, 1.0, 1.0)
214 cr.rectangle(self.extents_x_min, self.extents_y_min,
215 (self.extents_x_max - self.extents_x_min),
216 self.extents_y_max - self.extents_y_min)
217 cr.fill()
Parker Schuh19b93b12018-03-02 23:26:58 -0800218
Parker Schuhdc682952018-03-03 18:24:01 -0800219 if not self.theta_version:
220 # Draw a filled white rectangle.
221 cr.set_source_rgb(1.0, 1.0, 1.0)
222 cr.rectangle(-2.0, -2.0, 4.0, 4.0)
223 cr.fill()
Parker Schuh19b93b12018-03-02 23:26:58 -0800224
Parker Schuhdc682952018-03-03 18:24:01 -0800225 cr.set_source_rgb(0.0, 0.0, 1.0)
226 cr.arc(joint_center[0], joint_center[1], l2 + l1, 0,
227 2.0 * numpy.pi)
228 with px(cr):
229 cr.stroke()
230 cr.arc(joint_center[0], joint_center[1], l1 - l2, 0,
231 2.0 * numpy.pi)
232 with px(cr):
233 cr.stroke()
234 else:
235 # Draw a filled white rectangle.
236 cr.set_source_rgb(1.0, 1.0, 1.0)
237 cr.rectangle(-numpy.pi, -numpy.pi, numpy.pi * 2.0, numpy.pi * 2.0)
238 cr.fill()
Parker Schuh19b93b12018-03-02 23:26:58 -0800239
Parker Schuhdc682952018-03-03 18:24:01 -0800240 if self.theta_version:
241 cr.set_source_rgb(0.0, 0.0, 1.0)
242 for i in range(-6, 6):
243 cr.move_to(-40, -40 + i * numpy.pi)
244 cr.line_to(40, 40 + i * numpy.pi)
245 with px(cr):
246 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800247
Parker Schuhdc682952018-03-03 18:24:01 -0800248 if self.theta_version:
249 cr.set_source_rgb(0.5, 0.5, 1.0)
250 draw_lines(cr, lines_theta)
251 else:
252 cr.set_source_rgb(0.5, 1.0, 1.0)
253 draw_lines(cr, lines1)
254 draw_lines(cr, lines2)
Parker Schuh19b93b12018-03-02 23:26:58 -0800255
Parker Schuhdc682952018-03-03 18:24:01 -0800256 def set_color(cr, circular_index):
257 if circular_index == -2:
258 cr.set_source_rgb(0.0, 0.25, 1.0)
259 elif circular_index == -1:
260 cr.set_source_rgb(0.5, 0.0, 1.0)
261 elif circular_index == 0:
262 cr.set_source_rgb(0.5, 1.0, 1.0)
263 elif circular_index == 1:
264 cr.set_source_rgb(0.0, 0.5, 1.0)
265 elif circular_index == 2:
266 cr.set_source_rgb(0.5, 1.0, 0.5)
267 else:
268 cr.set_source_rgb(1.0, 0.0, 0.0)
Parker Schuh19b93b12018-03-02 23:26:58 -0800269
Parker Schuhdc682952018-03-03 18:24:01 -0800270 def get_circular_index(pt):
271 theta1, theta2 = pt
272 circular_index = int(numpy.floor((theta2 - theta1) / numpy.pi))
273 return circular_index
Parker Schuh19b93b12018-03-02 23:26:58 -0800274
Parker Schuhdc682952018-03-03 18:24:01 -0800275 cr.set_source_rgb(0.0, 0.0, 1.0)
276 lines = subdivide_theta(lines_theta)
277 o_circular_index = circular_index = get_circular_index(lines[0])
278 p_xy = to_xy(lines[0][0], lines[0][1])
279 if circular_index == self.circular_index_select:
280 cr.move_to(p_xy[0] + circular_index * 0, p_xy[1])
281 for pt in lines[1:]:
282 p_xy = to_xy(pt[0], pt[1])
283 circular_index = get_circular_index(pt)
284 if o_circular_index == self.circular_index_select:
285 cr.line_to(p_xy[0] + o_circular_index * 0, p_xy[1])
286 if circular_index != o_circular_index:
287 o_circular_index = circular_index
288 with px(cr):
289 cr.stroke()
290 if circular_index == self.circular_index_select:
291 cr.move_to(p_xy[0] + circular_index * 0, p_xy[1])
Parker Schuh19b93b12018-03-02 23:26:58 -0800292
Parker Schuhdc682952018-03-03 18:24:01 -0800293 with px(cr):
294 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800295
Parker Schuhdc682952018-03-03 18:24:01 -0800296 if not self.theta_version:
297 theta1, theta2 = to_theta(self.last_pos, self.circular_index_select)
298 x, y = joint_center[0], joint_center[1]
299 cr.move_to(x, y)
Parker Schuh19b93b12018-03-02 23:26:58 -0800300
Parker Schuhdc682952018-03-03 18:24:01 -0800301 x += numpy.cos(theta1) * l1
302 y += numpy.sin(theta1) * l1
303 cr.line_to(x, y)
304 x += numpy.cos(theta2) * l2
305 y += numpy.sin(theta2) * l2
306 cr.line_to(x, y)
307 with px(cr):
308 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800309
Parker Schuhdc682952018-03-03 18:24:01 -0800310 cr.move_to(self.last_pos[0], self.last_pos[1])
311 cr.set_source_rgb(0.0, 1.0, 0.2)
312 draw_px_cross(cr, 20)
Parker Schuh19b93b12018-03-02 23:26:58 -0800313
Parker Schuhdc682952018-03-03 18:24:01 -0800314 if self.theta_version:
315 cr.set_source_rgb(0.0, 1.0, 0.2)
Parker Schuh19b93b12018-03-02 23:26:58 -0800316
Parker Schuhdc682952018-03-03 18:24:01 -0800317 cr.set_source_rgb(0.0, 1.0, 0.2)
318 cr.move_to(self.last_pos[0], self.last_pos[1])
319 draw_px_cross(cr, 5)
Parker Schuh19b93b12018-03-02 23:26:58 -0800320
Parker Schuhdc682952018-03-03 18:24:01 -0800321 c_pt, dist = closest_segment(lines_theta, self.last_pos)
322 print("dist:", dist, c_pt, self.last_pos)
323 cr.set_source_rgb(0.0, 1.0, 1.0)
324 cr.move_to(c_pt[0], c_pt[1])
325 draw_px_cross(cr, 5)
Parker Schuh19b93b12018-03-02 23:26:58 -0800326
Parker Schuhdc682952018-03-03 18:24:01 -0800327 cr.set_source_rgb(0.0, 0.5, 1.0)
328 for segment in self.segments:
329 color = [0, random.random(), 1]
330 random.shuffle(color)
331 cr.set_source_rgb(*color)
332 segment.DrawTo(cr, self.theta_version)
333 with px(cr):
334 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800335
Parker Schuhdc682952018-03-03 18:24:01 -0800336 cr.set_source_rgb(0.0, 1.0, 0.5)
337 segment = self.current_seg()
338 if segment:
339 print(segment)
340 segment.DrawTo(cr, self.theta_version)
341 with px(cr):
342 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800343
Parker Schuhdc682952018-03-03 18:24:01 -0800344 def cur_pt_in_theta(self):
345 if self.theta_version: return self.last_pos
346 return to_theta(self.last_pos, self.circular_index_select)
347
348 # Current segment based on which mode the drawing system is in.
349 def current_seg(self):
350 if self.prev_segment_pt and self.now_segment_pt:
351 if self.theta_version:
352 return AngleSegment(self.prev_segment_pt, self.now_segment_pt)
353 else:
354 return XYSegment(self.prev_segment_pt, self.now_segment_pt)
355
356 def do_key_press(self, event):
357 keyval = Gdk.keyval_to_lower(event.keyval)
358 print("Gdk.KEY_" + Gdk.keyval_name(keyval))
359 if keyval == Gdk.KEY_q:
360 print("Found q key and exiting.")
361 quit_main_loop()
362 elif keyval == Gdk.KEY_c:
363 # Increment which arm solution we render
364 self.circular_index_select += 1
365 print(self.circular_index_select)
366 elif keyval == Gdk.KEY_v:
367 # Decrement which arm solution we render
368 self.circular_index_select -= 1
369 print(self.circular_index_select)
370 elif keyval == Gdk.KEY_w:
371 # Add this segment to the segment list.
372 segment = self.current_seg()
373 if segment: self.segments.append(segment)
374 self.prev_segment_pt = self.now_segment_pt
375
376 elif keyval == Gdk.KEY_r:
377 self.prev_segment_pt = self.now_segment_pt
378
379 elif keyval == Gdk.KEY_p:
380 # Print out the segments.
381 print(repr(self.segments))
382 elif keyval == Gdk.KEY_g:
383 # Generate theta points.
384 if self.segments:
385 print(repr(self.segments[0].ToThetaPoints()))
386 elif keyval == Gdk.KEY_e:
387 best_pt = self.now_segment_pt
388 best_dist = 1e10
389 for segment in self.segments:
390 d = angle_dist_sqr(segment.start, self.now_segment_pt)
391 if (d < best_dist):
392 best_pt = segment.start
393 best_dist = d
394 d = angle_dist_sqr(segment.end, self.now_segment_pt)
395 if (d < best_dist):
396 best_pt = segment.end
397 best_dist = d
398 self.now_segment_pt = best_pt
399
400 elif keyval == Gdk.KEY_t:
401 # Toggle between theta and xy renderings
402 if self.theta_version:
403 theta1, theta2 = self.last_pos
404 data = to_xy(theta1, theta2)
405 self.circular_index_select = int(
406 numpy.floor((theta2 - theta1) / numpy.pi))
407 self.last_pos = (data[0], data[1])
408 else:
409 self.last_pos = self.cur_pt_in_theta()
410
411 self.theta_version = not self.theta_version
412 self.reinit_extents()
413 self.redraw()
414
415 def do_button_press(self, event):
416 self.last_pos = (event.x, event.y)
417 self.now_segment_pt = self.cur_pt_in_theta()
Austin Schuh17e484e2018-03-11 01:11:36 -0800418 print('Clicked at theta: %s' % (repr(self.now_segment_pt,)))
Parker Schuhdc682952018-03-03 18:24:01 -0800419 if not self.theta_version:
420 print('Clicked at xy, circular index: (%f, %f, %f)' %
421 (self.last_pos[0], self.last_pos[1],
422 self.circular_index_select))
423
Austin Schuhed018082018-07-08 16:01:01 -0700424 print('c1: numpy.array([%f, %f])' % (self.segments[0].control1[0],
425 self.segments[0].control1[1]))
426 print('c2: numpy.array([%f, %f])' % (self.segments[0].control2[0],
427 self.segments[0].control2[1]))
428
Parker Schuhdc682952018-03-03 18:24:01 -0800429 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()