blob: fa9c9e59ef878e956ca72b08bf80d7d339ebff19 [file] [log] [blame]
Brian Silvermanb0ebf1d2018-10-17 23:36:40 -07001#!/usr/bin/python3
2
Parker Schuhdc682952018-03-03 18:24:01 -08003from __future__ import print_function
Parker Schuh19b93b12018-03-02 23:26:58 -08004import os
Andrew Runke6842bf92019-01-26 15:38:25 -08005from frc971.control_loops.python import basic_window
6from frc971.control_loops.python.color import Color, palette
Parker Schuhdc682952018-03-03 18:24:01 -08007import random
Parker Schuh19b93b12018-03-02 23:26:58 -08008import gi
9import numpy
10gi.require_version('Gtk', '3.0')
Andrew Runke6842bf92019-01-26 15:38:25 -080011from gi.repository import Gdk, Gtk
Parker Schuh19b93b12018-03-02 23:26:58 -080012import cairo
13import graph_generate
14from graph_generate import XYSegment, AngleSegment, to_theta, to_xy, alpha_blend
15from graph_generate import back_to_xy_loop, subdivide_theta, to_theta_loop
16from graph_generate import l1, l2, joint_center
17
Andrew Runke6842bf92019-01-26 15:38:25 -080018from frc971.control_loops.python.basic_window import OverrideMatrix, identity, quit_main_loop, set_color
Parker Schuh19b93b12018-03-02 23:26:58 -080019
20import shapely
21from shapely.geometry import Polygon
22
Parker Schuhdc682952018-03-03 18:24:01 -080023
Parker Schuh19b93b12018-03-02 23:26:58 -080024def px(cr):
Parker Schuhdc682952018-03-03 18:24:01 -080025 return OverrideMatrix(cr, identity)
Parker Schuh19b93b12018-03-02 23:26:58 -080026
Parker Schuhdc682952018-03-03 18:24:01 -080027
Parker Schuh19b93b12018-03-02 23:26:58 -080028def draw_px_cross(cr, length_px):
Parker Schuhdc682952018-03-03 18:24:01 -080029 """Draws a cross with fixed dimensions in pixel space."""
30 with px(cr):
31 x, y = cr.get_current_point()
32 cr.move_to(x, y - length_px)
33 cr.line_to(x, y + length_px)
34 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -080035
Parker Schuhdc682952018-03-03 18:24:01 -080036 cr.move_to(x - length_px, y)
37 cr.line_to(x + length_px, y)
38 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -080039
Parker Schuhdc682952018-03-03 18:24:01 -080040
Parker Schuh19b93b12018-03-02 23:26:58 -080041def angle_dist_sqr(a1, a2):
Parker Schuhdc682952018-03-03 18:24:01 -080042 """Distance between two points in angle space."""
43 return (a1[0] - a2[0])**2 + (a1[1] - a2[1])**2
44
Parker Schuh19b93b12018-03-02 23:26:58 -080045
46# Find the highest y position that intersects the vertical line defined by x.
47def inter_y(x):
Parker Schuhdc682952018-03-03 18:24:01 -080048 return numpy.sqrt((l2 + l1)**2 -
49 (x - joint_center[0])**2) + joint_center[1]
50
Parker Schuh19b93b12018-03-02 23:26:58 -080051
52# This is the x position where the inner (hyperextension) circle intersects the horizontal line
Parker Schuhdc682952018-03-03 18:24:01 -080053derr = numpy.sqrt((l1 - l2)**2 - (joint_center[1] - 0.3048)**2)
54
Parker Schuh19b93b12018-03-02 23:26:58 -080055
56# Define min and max l1 angles based on vertical constraints.
57def get_angle(boundary):
Parker Schuhdc682952018-03-03 18:24:01 -080058 h = numpy.sqrt((l1)**2 - (boundary - joint_center[0])**2) + joint_center[1]
59 return numpy.arctan2(h, boundary - joint_center[0])
60
Parker Schuh19b93b12018-03-02 23:26:58 -080061
62# left hand side lines
63lines1 = [
Parker Schuhdc682952018-03-03 18:24:01 -080064 (-0.826135, inter_y(-0.826135)),
65 (-0.826135, 0.1397),
66 (-23.025 * 0.0254, 0.1397),
67 (-23.025 * 0.0254, 0.3048),
68 (joint_center[0] - derr, 0.3048),
Parker Schuh19b93b12018-03-02 23:26:58 -080069]
70
71# right hand side lines
Parker Schuhdc682952018-03-03 18:24:01 -080072lines2 = [(joint_center[0] + derr, 0.3048), (0.422275, 0.3048),
73 (0.422275, 0.1397), (0.826135, 0.1397), (0.826135,
74 inter_y(0.826135))]
Parker Schuh19b93b12018-03-02 23:26:58 -080075
Parker Schuhdc682952018-03-03 18:24:01 -080076t1_min = get_angle((32.525 - 4.0) * 0.0254)
77t2_min = -7.0 / 4.0 * numpy.pi
Parker Schuh19b93b12018-03-02 23:26:58 -080078
Parker Schuhdc682952018-03-03 18:24:01 -080079t1_max = get_angle((-32.525 + 4.0) * 0.0254)
80t2_max = numpy.pi * 3.0 / 4.0
81
Parker Schuh19b93b12018-03-02 23:26:58 -080082
83# Draw lines to cr + stroke.
84def draw_lines(cr, lines):
Parker Schuhdc682952018-03-03 18:24:01 -080085 cr.move_to(lines[0][0], lines[0][1])
86 for pt in lines[1:]:
87 cr.line_to(pt[0], pt[1])
88 with px(cr):
89 cr.stroke()
90
Parker Schuh19b93b12018-03-02 23:26:58 -080091
92# Rotate a rasterized loop such that it aligns to when the parameters loop
93def rotate_to_jump_point(points):
Parker Schuhdc682952018-03-03 18:24:01 -080094 last_pt = points[0]
95 for pt_i in range(1, len(points)):
96 pt = points[pt_i]
97 delta = last_pt[1] - pt[1]
98 if abs(delta) > numpy.pi:
Parker Schuhdc682952018-03-03 18:24:01 -080099 return points[pt_i:] + points[:pt_i]
100 last_pt = pt
101 return points
102
Parker Schuh19b93b12018-03-02 23:26:58 -0800103
104# shift points vertically by dy.
105def y_shift(points, dy):
Parker Schuhdc682952018-03-03 18:24:01 -0800106 return [(x, y + dy) for x, y in points]
107
Parker Schuh19b93b12018-03-02 23:26:58 -0800108
109lines1_theta_part = rotate_to_jump_point(to_theta_loop(lines1, 0))
110lines2_theta_part = rotate_to_jump_point(to_theta_loop(lines2))
111
112# Some hacks here to make a single polygon by shifting to get an extra copy of the contraints.
113lines1_theta = y_shift(lines1_theta_part, -numpy.pi * 2) + lines1_theta_part + \
114 y_shift(lines1_theta_part, numpy.pi * 2)
115lines2_theta = y_shift(lines2_theta_part, numpy.pi * 2) + lines2_theta_part + \
116 y_shift(lines2_theta_part, -numpy.pi * 2)
117
118lines_theta = lines1_theta + lines2_theta
119
120p1 = Polygon(lines_theta)
121
Parker Schuhdc682952018-03-03 18:24:01 -0800122p2 = Polygon([(t1_min, t2_min), (t1_max, t2_min), (t1_max, t2_max), (t1_min,
123 t2_max)])
Parker Schuh19b93b12018-03-02 23:26:58 -0800124
125# Fully computed theta constrints.
126lines_theta = list(p1.intersection(p2).exterior.coords)
127
Parker Schuh19b93b12018-03-02 23:26:58 -0800128lines1_theta_back = back_to_xy_loop(lines1_theta)
129lines2_theta_back = back_to_xy_loop(lines2_theta)
130
131lines_theta_back = back_to_xy_loop(lines_theta)
132
Parker Schuhdc682952018-03-03 18:24:01 -0800133
Parker Schuh19b93b12018-03-02 23:26:58 -0800134# Get the closest point to a line from a test pt.
135def get_closest(prev, cur, pt):
Parker Schuhdc682952018-03-03 18:24:01 -0800136 dx_ang = (cur[0] - prev[0])
137 dy_ang = (cur[1] - prev[1])
Parker Schuh19b93b12018-03-02 23:26:58 -0800138
Parker Schuhdc682952018-03-03 18:24:01 -0800139 d = numpy.sqrt(dx_ang**2 + dy_ang**2)
140 if (d < 0.000001):
141 return prev, numpy.sqrt((prev[0] - pt[0])**2 + (prev[1] - pt[1])**2)
142
143 pdx = -dy_ang / d
144 pdy = dx_ang / d
145
146 dpx = pt[0] - prev[0]
147 dpy = pt[1] - prev[1]
148
149 alpha = (dx_ang * dpx + dy_ang * dpy) / d / d
150
151 if (alpha < 0):
152 return prev, numpy.sqrt((prev[0] - pt[0])**2 + (prev[1] - pt[1])**2)
153 elif (alpha > 1):
154 return cur, numpy.sqrt((cur[0] - pt[0])**2 + (cur[1] - pt[1])**2)
155 else:
156 return (alpha_blend(prev[0], cur[0], alpha), alpha_blend(prev[1], cur[1], alpha)), \
157 abs(dpx * pdx + dpy * pdy)
Parker Schuh19b93b12018-03-02 23:26:58 -0800158
159
Andrew Runke6842bf92019-01-26 15:38:25 -0800160
Parker Schuh19b93b12018-03-02 23:26:58 -0800161def closest_segment(lines, pt):
Parker Schuhdc682952018-03-03 18:24:01 -0800162 c_pt, c_pt_dist = get_closest(lines[-1], lines[0], pt)
163 for i in range(1, len(lines)):
164 prev = lines[i - 1]
165 cur = lines[i]
166 c_pt_new, c_pt_new_dist = get_closest(prev, cur, pt)
167 if c_pt_new_dist < c_pt_dist:
168 c_pt = c_pt_new
169 c_pt_dist = c_pt_new_dist
170 return c_pt, c_pt_dist
171
Parker Schuh19b93b12018-03-02 23:26:58 -0800172
173# Create a GTK+ widget on which we will draw using Cairo
174class Silly(basic_window.BaseWindow):
Parker Schuhdc682952018-03-03 18:24:01 -0800175 def __init__(self):
176 super(Silly, self).__init__()
Parker Schuh19b93b12018-03-02 23:26:58 -0800177
Andrew Runke6842bf92019-01-26 15:38:25 -0800178 self.window = Gtk.Window()
179 self.window.set_title("DrawingArea")
180
181 self.window.set_events(Gdk.EventMask.BUTTON_PRESS_MASK
182 | Gdk.EventMask.BUTTON_RELEASE_MASK
183 | Gdk.EventMask.POINTER_MOTION_MASK
184 | Gdk.EventMask.SCROLL_MASK
185 | Gdk.EventMask.KEY_PRESS_MASK)
186 self.method_connect("key-press-event", self.do_key_press)
187 self.method_connect("button-press-event",
188 self._do_button_press_internal)
189 self.method_connect("configure-event", self._do_configure)
190 self.window.add(self)
191 self.window.show_all()
192
Austin Schuhed018082018-07-08 16:01:01 -0700193 self.theta_version = False
Parker Schuhdc682952018-03-03 18:24:01 -0800194 self.reinit_extents()
Parker Schuh19b93b12018-03-02 23:26:58 -0800195
Parker Schuhdc682952018-03-03 18:24:01 -0800196 self.last_pos = (numpy.pi / 2.0, 1.0)
197 self.circular_index_select = -1
Parker Schuh19b93b12018-03-02 23:26:58 -0800198
Parker Schuhdc682952018-03-03 18:24:01 -0800199 # Extra stuff for drawing lines.
200 self.segments = []
201 self.prev_segment_pt = None
202 self.now_segment_pt = None
Austin Schuhee249892018-07-08 16:20:09 -0700203 self.spline_edit = 0
204 self.edit_control1 = True
Parker Schuh19b93b12018-03-02 23:26:58 -0800205
Andrew Runke6842bf92019-01-26 15:38:25 -0800206 def do_key_press(self, event):
207 pass
208
209 def _do_button_press_internal(self, event):
210 o_x = event.x
211 o_y = event.y
212 x = event.x - self.window_shape[0] / 2
213 y = self.window_shape[1] / 2 - event.y
214 scale = self.get_current_scale()
215 event.x = x / scale + self.center[0]
216 event.y = y / scale + self.center[1]
217 self.do_button_press(event)
218 event.x = o_x
219 event.y = o_y
220
221 def do_button_press(self, event):
222 pass
223
224 def _do_configure(self, event):
225 self.window_shape = (event.width, event.height)
226
227 def redraw(self):
228 if not self.needs_redraw:
229 self.needs_redraw = True
230 self.window.queue_draw()
231
232 def method_connect(self, event, cb):
233 def handler(obj, *args):
234 cb(*args)
235
236 self.window.connect(event, handler)
237
Parker Schuhdc682952018-03-03 18:24:01 -0800238 def reinit_extents(self):
239 if self.theta_version:
240 self.extents_x_min = -numpy.pi * 2
241 self.extents_x_max = numpy.pi * 2
242 self.extents_y_min = -numpy.pi * 2
243 self.extents_y_max = numpy.pi * 2
Parker Schuh19b93b12018-03-02 23:26:58 -0800244 else:
Parker Schuhdc682952018-03-03 18:24:01 -0800245 self.extents_x_min = -40.0 * 0.0254
246 self.extents_x_max = 40.0 * 0.0254
247 self.extents_y_min = -4.0 * 0.0254
248 self.extents_y_max = 110.0 * 0.0254
Parker Schuh19b93b12018-03-02 23:26:58 -0800249
Parker Schuhdc682952018-03-03 18:24:01 -0800250 self.init_extents(
251 (0.5 * (self.extents_x_min + self.extents_x_max), 0.5 *
252 (self.extents_y_max + self.extents_y_min)),
253 (1.0 * (self.extents_x_max - self.extents_x_min), 1.0 *
254 (self.extents_y_max - self.extents_y_min)))
Parker Schuh19b93b12018-03-02 23:26:58 -0800255
Parker Schuhdc682952018-03-03 18:24:01 -0800256 # Handle the expose-event by drawing
257 def handle_draw(self, cr):
258 # use "with px(cr): blah;" to transform to pixel coordinates.
Parker Schuh19b93b12018-03-02 23:26:58 -0800259
Parker Schuhdc682952018-03-03 18:24:01 -0800260 # Fill the background color of the window with grey
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800261 set_color(cr, palette["GREY"])
Parker Schuhdc682952018-03-03 18:24:01 -0800262 cr.paint()
Parker Schuh19b93b12018-03-02 23:26:58 -0800263
Parker Schuhdc682952018-03-03 18:24:01 -0800264 # Draw a extents rectangle
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800265 set_color(cr, palette["WHITE"])
Parker Schuhdc682952018-03-03 18:24:01 -0800266 cr.rectangle(self.extents_x_min, self.extents_y_min,
267 (self.extents_x_max - self.extents_x_min),
268 self.extents_y_max - self.extents_y_min)
269 cr.fill()
Parker Schuh19b93b12018-03-02 23:26:58 -0800270
Parker Schuhdc682952018-03-03 18:24:01 -0800271 if not self.theta_version:
272 # Draw a filled white rectangle.
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800273 set_color(cr, palette["WHITE"])
Parker Schuhdc682952018-03-03 18:24:01 -0800274 cr.rectangle(-2.0, -2.0, 4.0, 4.0)
275 cr.fill()
Parker Schuh19b93b12018-03-02 23:26:58 -0800276
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800277 set_color(cr, palette["BLUE"])
Parker Schuhdc682952018-03-03 18:24:01 -0800278 cr.arc(joint_center[0], joint_center[1], l2 + l1, 0,
279 2.0 * numpy.pi)
280 with px(cr):
281 cr.stroke()
282 cr.arc(joint_center[0], joint_center[1], l1 - l2, 0,
283 2.0 * numpy.pi)
284 with px(cr):
285 cr.stroke()
286 else:
287 # Draw a filled white rectangle.
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800288 set_color(cr, palette["WHITE"])
Parker Schuhdc682952018-03-03 18:24:01 -0800289 cr.rectangle(-numpy.pi, -numpy.pi, numpy.pi * 2.0, numpy.pi * 2.0)
290 cr.fill()
Parker Schuh19b93b12018-03-02 23:26:58 -0800291
Parker Schuhdc682952018-03-03 18:24:01 -0800292 if self.theta_version:
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800293 set_color(cr, palette["BLUE"])
Parker Schuhdc682952018-03-03 18:24:01 -0800294 for i in range(-6, 6):
295 cr.move_to(-40, -40 + i * numpy.pi)
296 cr.line_to(40, 40 + i * numpy.pi)
297 with px(cr):
298 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800299
Parker Schuhdc682952018-03-03 18:24:01 -0800300 if self.theta_version:
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800301 set_color(cr, Color(0.5, 0.5, 1.0))
Parker Schuhdc682952018-03-03 18:24:01 -0800302 draw_lines(cr, lines_theta)
303 else:
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800304 set_color(cr, Color(0.5, 1.0, 1.0))
Parker Schuhdc682952018-03-03 18:24:01 -0800305 draw_lines(cr, lines1)
306 draw_lines(cr, lines2)
Parker Schuh19b93b12018-03-02 23:26:58 -0800307
Parker Schuhdc682952018-03-03 18:24:01 -0800308 def get_circular_index(pt):
309 theta1, theta2 = pt
310 circular_index = int(numpy.floor((theta2 - theta1) / numpy.pi))
311 return circular_index
Parker Schuh19b93b12018-03-02 23:26:58 -0800312
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800313 set_color(cr, palette["BLUE"])
Parker Schuhdc682952018-03-03 18:24:01 -0800314 lines = subdivide_theta(lines_theta)
315 o_circular_index = circular_index = get_circular_index(lines[0])
316 p_xy = to_xy(lines[0][0], lines[0][1])
317 if circular_index == self.circular_index_select:
318 cr.move_to(p_xy[0] + circular_index * 0, p_xy[1])
319 for pt in lines[1:]:
320 p_xy = to_xy(pt[0], pt[1])
321 circular_index = get_circular_index(pt)
322 if o_circular_index == self.circular_index_select:
323 cr.line_to(p_xy[0] + o_circular_index * 0, p_xy[1])
324 if circular_index != o_circular_index:
325 o_circular_index = circular_index
326 with px(cr):
327 cr.stroke()
328 if circular_index == self.circular_index_select:
329 cr.move_to(p_xy[0] + circular_index * 0, p_xy[1])
Parker Schuh19b93b12018-03-02 23:26:58 -0800330
Parker Schuhdc682952018-03-03 18:24:01 -0800331 with px(cr):
332 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800333
Parker Schuhdc682952018-03-03 18:24:01 -0800334 if not self.theta_version:
335 theta1, theta2 = to_theta(self.last_pos, self.circular_index_select)
336 x, y = joint_center[0], joint_center[1]
337 cr.move_to(x, y)
Parker Schuh19b93b12018-03-02 23:26:58 -0800338
Parker Schuhdc682952018-03-03 18:24:01 -0800339 x += numpy.cos(theta1) * l1
340 y += numpy.sin(theta1) * l1
341 cr.line_to(x, y)
342 x += numpy.cos(theta2) * l2
343 y += numpy.sin(theta2) * l2
344 cr.line_to(x, y)
345 with px(cr):
346 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800347
Parker Schuhdc682952018-03-03 18:24:01 -0800348 cr.move_to(self.last_pos[0], self.last_pos[1])
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800349 set_color(cr, Color(0.0, 1.0, 0.2))
Parker Schuhdc682952018-03-03 18:24:01 -0800350 draw_px_cross(cr, 20)
Parker Schuh19b93b12018-03-02 23:26:58 -0800351
Parker Schuhdc682952018-03-03 18:24:01 -0800352 if self.theta_version:
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800353 set_color(cr, Color(0.0, 1.0, 0.2))
Parker Schuhdc682952018-03-03 18:24:01 -0800354 cr.move_to(self.last_pos[0], self.last_pos[1])
355 draw_px_cross(cr, 5)
Parker Schuh19b93b12018-03-02 23:26:58 -0800356
Parker Schuhdc682952018-03-03 18:24:01 -0800357 c_pt, dist = closest_segment(lines_theta, self.last_pos)
358 print("dist:", dist, c_pt, self.last_pos)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800359 set_color(cr, palette["CYAN"])
Parker Schuhdc682952018-03-03 18:24:01 -0800360 cr.move_to(c_pt[0], c_pt[1])
361 draw_px_cross(cr, 5)
Parker Schuh19b93b12018-03-02 23:26:58 -0800362
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800363 set_color(cr, Color(0.0, 0.5, 1.0))
Parker Schuhdc682952018-03-03 18:24:01 -0800364 for segment in self.segments:
365 color = [0, random.random(), 1]
366 random.shuffle(color)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800367 set_color(cr, Color(color[0], color[1], color[2]))
Parker Schuhdc682952018-03-03 18:24:01 -0800368 segment.DrawTo(cr, self.theta_version)
369 with px(cr):
370 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800371
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800372 set_color(cr, Color(0.0, 1.0, 0.5))
Parker Schuhdc682952018-03-03 18:24:01 -0800373 segment = self.current_seg()
374 if segment:
375 print(segment)
376 segment.DrawTo(cr, self.theta_version)
377 with px(cr):
378 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800379
Parker Schuhdc682952018-03-03 18:24:01 -0800380 def cur_pt_in_theta(self):
381 if self.theta_version: return self.last_pos
382 return to_theta(self.last_pos, self.circular_index_select)
383
384 # Current segment based on which mode the drawing system is in.
385 def current_seg(self):
386 if self.prev_segment_pt and self.now_segment_pt:
387 if self.theta_version:
388 return AngleSegment(self.prev_segment_pt, self.now_segment_pt)
389 else:
390 return XYSegment(self.prev_segment_pt, self.now_segment_pt)
391
392 def do_key_press(self, event):
393 keyval = Gdk.keyval_to_lower(event.keyval)
394 print("Gdk.KEY_" + Gdk.keyval_name(keyval))
395 if keyval == Gdk.KEY_q:
396 print("Found q key and exiting.")
397 quit_main_loop()
398 elif keyval == Gdk.KEY_c:
399 # Increment which arm solution we render
400 self.circular_index_select += 1
401 print(self.circular_index_select)
402 elif keyval == Gdk.KEY_v:
403 # Decrement which arm solution we render
404 self.circular_index_select -= 1
405 print(self.circular_index_select)
406 elif keyval == Gdk.KEY_w:
407 # Add this segment to the segment list.
408 segment = self.current_seg()
409 if segment: self.segments.append(segment)
410 self.prev_segment_pt = self.now_segment_pt
411
412 elif keyval == Gdk.KEY_r:
413 self.prev_segment_pt = self.now_segment_pt
414
415 elif keyval == Gdk.KEY_p:
416 # Print out the segments.
417 print(repr(self.segments))
418 elif keyval == Gdk.KEY_g:
419 # Generate theta points.
420 if self.segments:
421 print(repr(self.segments[0].ToThetaPoints()))
422 elif keyval == Gdk.KEY_e:
423 best_pt = self.now_segment_pt
424 best_dist = 1e10
425 for segment in self.segments:
426 d = angle_dist_sqr(segment.start, self.now_segment_pt)
427 if (d < best_dist):
428 best_pt = segment.start
429 best_dist = d
430 d = angle_dist_sqr(segment.end, self.now_segment_pt)
431 if (d < best_dist):
432 best_pt = segment.end
433 best_dist = d
434 self.now_segment_pt = best_pt
435
436 elif keyval == Gdk.KEY_t:
437 # Toggle between theta and xy renderings
438 if self.theta_version:
439 theta1, theta2 = self.last_pos
440 data = to_xy(theta1, theta2)
441 self.circular_index_select = int(
442 numpy.floor((theta2 - theta1) / numpy.pi))
443 self.last_pos = (data[0], data[1])
444 else:
445 self.last_pos = self.cur_pt_in_theta()
446
447 self.theta_version = not self.theta_version
448 self.reinit_extents()
Austin Schuhee249892018-07-08 16:20:09 -0700449
450 elif keyval == Gdk.KEY_z:
451 self.edit_control1 = not self.edit_control1
452 if self.edit_control1:
453 self.now_segment_pt = self.segments[0].control1
454 else:
455 self.now_segment_pt = self.segments[0].control2
456 if not self.theta_version:
457 data = to_xy(self.now_segment_pt[0], self.now_segment_pt[1])
458 self.last_pos = (data[0], data[1])
459 else:
460 self.last_pos = self.now_segment_pt
461
462 print("self.last_pos: ", self.last_pos, " ci: ",
463 self.circular_index_select)
464
Parker Schuhdc682952018-03-03 18:24:01 -0800465 self.redraw()
466
467 def do_button_press(self, event):
468 self.last_pos = (event.x, event.y)
469 self.now_segment_pt = self.cur_pt_in_theta()
Austin Schuhee249892018-07-08 16:20:09 -0700470
471 if self.edit_control1:
472 self.segments[0].control1 = self.now_segment_pt
473 else:
474 self.segments[0].control2 = self.now_segment_pt
475
Austin Schuh17e484e2018-03-11 01:11:36 -0800476 print('Clicked at theta: %s' % (repr(self.now_segment_pt,)))
Parker Schuhdc682952018-03-03 18:24:01 -0800477 if not self.theta_version:
478 print('Clicked at xy, circular index: (%f, %f, %f)' %
479 (self.last_pos[0], self.last_pos[1],
480 self.circular_index_select))
481
Austin Schuhed018082018-07-08 16:01:01 -0700482 print('c1: numpy.array([%f, %f])' % (self.segments[0].control1[0],
483 self.segments[0].control1[1]))
484 print('c2: numpy.array([%f, %f])' % (self.segments[0].control2[0],
485 self.segments[0].control2[1]))
486
Parker Schuhdc682952018-03-03 18:24:01 -0800487 self.redraw()
488
Parker Schuh19b93b12018-03-02 23:26:58 -0800489
490silly = Silly()
Parker Schuhdc682952018-03-03 18:24:01 -0800491silly.segments = graph_generate.segments
Parker Schuh19b93b12018-03-02 23:26:58 -0800492basic_window.RunApp()