blob: 7b6179cf90e7d31a85e1137a362b192ba4f6e866 [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
Ravago Jones5127ccc2022-07-31 16:32:45 -070010
Parker Schuh19b93b12018-03-02 23:26:58 -080011gi.require_version('Gtk', '3.0')
Andrew Runke6842bf92019-01-26 15:38:25 -080012from gi.repository import Gdk, Gtk
Parker Schuh19b93b12018-03-02 23:26:58 -080013import cairo
14import graph_generate
15from graph_generate import XYSegment, AngleSegment, to_theta, to_xy, alpha_blend
16from graph_generate import back_to_xy_loop, subdivide_theta, to_theta_loop
17from graph_generate import l1, l2, joint_center
18
Andrew Runke6842bf92019-01-26 15:38:25 -080019from frc971.control_loops.python.basic_window import OverrideMatrix, identity, quit_main_loop, set_color
Parker Schuh19b93b12018-03-02 23:26:58 -080020
21import shapely
22from shapely.geometry import Polygon
23
Parker Schuhdc682952018-03-03 18:24:01 -080024
Parker Schuh19b93b12018-03-02 23:26:58 -080025def px(cr):
Parker Schuhdc682952018-03-03 18:24:01 -080026 return OverrideMatrix(cr, identity)
Parker Schuh19b93b12018-03-02 23:26:58 -080027
Parker Schuhdc682952018-03-03 18:24:01 -080028
Parker Schuh19b93b12018-03-02 23:26:58 -080029def draw_px_cross(cr, length_px):
Parker Schuhdc682952018-03-03 18:24:01 -080030 """Draws a cross with fixed dimensions in pixel space."""
31 with px(cr):
32 x, y = cr.get_current_point()
33 cr.move_to(x, y - length_px)
34 cr.line_to(x, y + length_px)
35 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -080036
Parker Schuhdc682952018-03-03 18:24:01 -080037 cr.move_to(x - length_px, y)
38 cr.line_to(x + length_px, y)
39 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -080040
Parker Schuhdc682952018-03-03 18:24:01 -080041
Parker Schuh19b93b12018-03-02 23:26:58 -080042def angle_dist_sqr(a1, a2):
Parker Schuhdc682952018-03-03 18:24:01 -080043 """Distance between two points in angle space."""
44 return (a1[0] - a2[0])**2 + (a1[1] - a2[1])**2
45
Parker Schuh19b93b12018-03-02 23:26:58 -080046
47# Find the highest y position that intersects the vertical line defined by x.
48def inter_y(x):
Parker Schuhdc682952018-03-03 18:24:01 -080049 return numpy.sqrt((l2 + l1)**2 -
50 (x - joint_center[0])**2) + joint_center[1]
51
Parker Schuh19b93b12018-03-02 23:26:58 -080052
53# This is the x position where the inner (hyperextension) circle intersects the horizontal line
Parker Schuhdc682952018-03-03 18:24:01 -080054derr = numpy.sqrt((l1 - l2)**2 - (joint_center[1] - 0.3048)**2)
55
Parker Schuh19b93b12018-03-02 23:26:58 -080056
57# Define min and max l1 angles based on vertical constraints.
58def get_angle(boundary):
Parker Schuhdc682952018-03-03 18:24:01 -080059 h = numpy.sqrt((l1)**2 - (boundary - joint_center[0])**2) + joint_center[1]
60 return numpy.arctan2(h, boundary - joint_center[0])
61
Parker Schuh19b93b12018-03-02 23:26:58 -080062
63# left hand side lines
64lines1 = [
Parker Schuhdc682952018-03-03 18:24:01 -080065 (-0.826135, inter_y(-0.826135)),
66 (-0.826135, 0.1397),
67 (-23.025 * 0.0254, 0.1397),
68 (-23.025 * 0.0254, 0.3048),
69 (joint_center[0] - derr, 0.3048),
Parker Schuh19b93b12018-03-02 23:26:58 -080070]
71
72# right hand side lines
Parker Schuhdc682952018-03-03 18:24:01 -080073lines2 = [(joint_center[0] + derr, 0.3048), (0.422275, 0.3048),
Ravago Jones5127ccc2022-07-31 16:32:45 -070074 (0.422275, 0.1397), (0.826135, 0.1397),
75 (0.826135, inter_y(0.826135))]
Parker Schuh19b93b12018-03-02 23:26:58 -080076
Parker Schuhdc682952018-03-03 18:24:01 -080077t1_min = get_angle((32.525 - 4.0) * 0.0254)
78t2_min = -7.0 / 4.0 * numpy.pi
Parker Schuh19b93b12018-03-02 23:26:58 -080079
Parker Schuhdc682952018-03-03 18:24:01 -080080t1_max = get_angle((-32.525 + 4.0) * 0.0254)
81t2_max = numpy.pi * 3.0 / 4.0
82
Parker Schuh19b93b12018-03-02 23:26:58 -080083
84# Draw lines to cr + stroke.
85def draw_lines(cr, lines):
Parker Schuhdc682952018-03-03 18:24:01 -080086 cr.move_to(lines[0][0], lines[0][1])
87 for pt in lines[1:]:
88 cr.line_to(pt[0], pt[1])
89 with px(cr):
90 cr.stroke()
91
Parker Schuh19b93b12018-03-02 23:26:58 -080092
93# Rotate a rasterized loop such that it aligns to when the parameters loop
94def rotate_to_jump_point(points):
Parker Schuhdc682952018-03-03 18:24:01 -080095 last_pt = points[0]
96 for pt_i in range(1, len(points)):
97 pt = points[pt_i]
98 delta = last_pt[1] - pt[1]
99 if abs(delta) > numpy.pi:
Parker Schuhdc682952018-03-03 18:24:01 -0800100 return points[pt_i:] + points[:pt_i]
101 last_pt = pt
102 return points
103
Parker Schuh19b93b12018-03-02 23:26:58 -0800104
105# shift points vertically by dy.
106def y_shift(points, dy):
Parker Schuhdc682952018-03-03 18:24:01 -0800107 return [(x, y + dy) for x, y in points]
108
Parker Schuh19b93b12018-03-02 23:26:58 -0800109
110lines1_theta_part = rotate_to_jump_point(to_theta_loop(lines1, 0))
111lines2_theta_part = rotate_to_jump_point(to_theta_loop(lines2))
112
113# Some hacks here to make a single polygon by shifting to get an extra copy of the contraints.
114lines1_theta = y_shift(lines1_theta_part, -numpy.pi * 2) + lines1_theta_part + \
115 y_shift(lines1_theta_part, numpy.pi * 2)
116lines2_theta = y_shift(lines2_theta_part, numpy.pi * 2) + lines2_theta_part + \
117 y_shift(lines2_theta_part, -numpy.pi * 2)
118
119lines_theta = lines1_theta + lines2_theta
120
121p1 = Polygon(lines_theta)
122
Ravago Jones5127ccc2022-07-31 16:32:45 -0700123p2 = Polygon([(t1_min, t2_min), (t1_max, t2_min), (t1_max, t2_max),
124 (t1_min, t2_max)])
Parker Schuh19b93b12018-03-02 23:26:58 -0800125
126# Fully computed theta constrints.
127lines_theta = list(p1.intersection(p2).exterior.coords)
128
Parker Schuh19b93b12018-03-02 23:26:58 -0800129lines1_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 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):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700175
Parker Schuhdc682952018-03-03 18:24:01 -0800176 def __init__(self):
177 super(Silly, self).__init__()
Parker Schuh19b93b12018-03-02 23:26:58 -0800178
Andrew Runke6842bf92019-01-26 15:38:25 -0800179 self.window = Gtk.Window()
180 self.window.set_title("DrawingArea")
181
182 self.window.set_events(Gdk.EventMask.BUTTON_PRESS_MASK
183 | Gdk.EventMask.BUTTON_RELEASE_MASK
184 | Gdk.EventMask.POINTER_MOTION_MASK
185 | Gdk.EventMask.SCROLL_MASK
186 | Gdk.EventMask.KEY_PRESS_MASK)
187 self.method_connect("key-press-event", self.do_key_press)
188 self.method_connect("button-press-event",
189 self._do_button_press_internal)
190 self.method_connect("configure-event", self._do_configure)
191 self.window.add(self)
192 self.window.show_all()
193
Austin Schuhed018082018-07-08 16:01:01 -0700194 self.theta_version = False
Parker Schuhdc682952018-03-03 18:24:01 -0800195 self.reinit_extents()
Parker Schuh19b93b12018-03-02 23:26:58 -0800196
Parker Schuhdc682952018-03-03 18:24:01 -0800197 self.last_pos = (numpy.pi / 2.0, 1.0)
198 self.circular_index_select = -1
Parker Schuh19b93b12018-03-02 23:26:58 -0800199
Parker Schuhdc682952018-03-03 18:24:01 -0800200 # Extra stuff for drawing lines.
201 self.segments = []
202 self.prev_segment_pt = None
203 self.now_segment_pt = None
Austin Schuhee249892018-07-08 16:20:09 -0700204 self.spline_edit = 0
205 self.edit_control1 = True
Parker Schuh19b93b12018-03-02 23:26:58 -0800206
Andrew Runke6842bf92019-01-26 15:38:25 -0800207 def do_key_press(self, event):
208 pass
209
210 def _do_button_press_internal(self, event):
211 o_x = event.x
212 o_y = event.y
213 x = event.x - self.window_shape[0] / 2
214 y = self.window_shape[1] / 2 - event.y
215 scale = self.get_current_scale()
216 event.x = x / scale + self.center[0]
217 event.y = y / scale + self.center[1]
218 self.do_button_press(event)
219 event.x = o_x
220 event.y = o_y
221
222 def do_button_press(self, event):
223 pass
224
225 def _do_configure(self, event):
226 self.window_shape = (event.width, event.height)
227
228 def redraw(self):
229 if not self.needs_redraw:
230 self.needs_redraw = True
231 self.window.queue_draw()
232
233 def method_connect(self, event, cb):
Ravago Jones5127ccc2022-07-31 16:32:45 -0700234
Andrew Runke6842bf92019-01-26 15:38:25 -0800235 def handler(obj, *args):
236 cb(*args)
237
238 self.window.connect(event, handler)
239
Parker Schuhdc682952018-03-03 18:24:01 -0800240 def reinit_extents(self):
241 if self.theta_version:
242 self.extents_x_min = -numpy.pi * 2
243 self.extents_x_max = numpy.pi * 2
244 self.extents_y_min = -numpy.pi * 2
245 self.extents_y_max = numpy.pi * 2
Parker Schuh19b93b12018-03-02 23:26:58 -0800246 else:
Parker Schuhdc682952018-03-03 18:24:01 -0800247 self.extents_x_min = -40.0 * 0.0254
248 self.extents_x_max = 40.0 * 0.0254
249 self.extents_y_min = -4.0 * 0.0254
250 self.extents_y_max = 110.0 * 0.0254
Parker Schuh19b93b12018-03-02 23:26:58 -0800251
Parker Schuhdc682952018-03-03 18:24:01 -0800252 self.init_extents(
253 (0.5 * (self.extents_x_min + self.extents_x_max), 0.5 *
254 (self.extents_y_max + self.extents_y_min)),
255 (1.0 * (self.extents_x_max - self.extents_x_min), 1.0 *
256 (self.extents_y_max - self.extents_y_min)))
Parker Schuh19b93b12018-03-02 23:26:58 -0800257
Parker Schuhdc682952018-03-03 18:24:01 -0800258 # Handle the expose-event by drawing
259 def handle_draw(self, cr):
260 # use "with px(cr): blah;" to transform to pixel coordinates.
Parker Schuh19b93b12018-03-02 23:26:58 -0800261
Parker Schuhdc682952018-03-03 18:24:01 -0800262 # Fill the background color of the window with grey
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800263 set_color(cr, palette["GREY"])
Parker Schuhdc682952018-03-03 18:24:01 -0800264 cr.paint()
Parker Schuh19b93b12018-03-02 23:26:58 -0800265
Parker Schuhdc682952018-03-03 18:24:01 -0800266 # Draw a extents rectangle
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800267 set_color(cr, palette["WHITE"])
Parker Schuhdc682952018-03-03 18:24:01 -0800268 cr.rectangle(self.extents_x_min, self.extents_y_min,
269 (self.extents_x_max - self.extents_x_min),
270 self.extents_y_max - self.extents_y_min)
271 cr.fill()
Parker Schuh19b93b12018-03-02 23:26:58 -0800272
Parker Schuhdc682952018-03-03 18:24:01 -0800273 if not self.theta_version:
274 # Draw a filled white rectangle.
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800275 set_color(cr, palette["WHITE"])
Parker Schuhdc682952018-03-03 18:24:01 -0800276 cr.rectangle(-2.0, -2.0, 4.0, 4.0)
277 cr.fill()
Parker Schuh19b93b12018-03-02 23:26:58 -0800278
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800279 set_color(cr, palette["BLUE"])
Parker Schuhdc682952018-03-03 18:24:01 -0800280 cr.arc(joint_center[0], joint_center[1], l2 + l1, 0,
281 2.0 * numpy.pi)
282 with px(cr):
283 cr.stroke()
284 cr.arc(joint_center[0], joint_center[1], l1 - l2, 0,
285 2.0 * numpy.pi)
286 with px(cr):
287 cr.stroke()
288 else:
289 # Draw a filled white rectangle.
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800290 set_color(cr, palette["WHITE"])
Parker Schuhdc682952018-03-03 18:24:01 -0800291 cr.rectangle(-numpy.pi, -numpy.pi, numpy.pi * 2.0, numpy.pi * 2.0)
292 cr.fill()
Parker Schuh19b93b12018-03-02 23:26:58 -0800293
Parker Schuhdc682952018-03-03 18:24:01 -0800294 if self.theta_version:
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800295 set_color(cr, palette["BLUE"])
Parker Schuhdc682952018-03-03 18:24:01 -0800296 for i in range(-6, 6):
297 cr.move_to(-40, -40 + i * numpy.pi)
298 cr.line_to(40, 40 + i * numpy.pi)
299 with px(cr):
300 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800301
Parker Schuhdc682952018-03-03 18:24:01 -0800302 if self.theta_version:
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800303 set_color(cr, Color(0.5, 0.5, 1.0))
Parker Schuhdc682952018-03-03 18:24:01 -0800304 draw_lines(cr, lines_theta)
305 else:
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800306 set_color(cr, Color(0.5, 1.0, 1.0))
Parker Schuhdc682952018-03-03 18:24:01 -0800307 draw_lines(cr, lines1)
308 draw_lines(cr, lines2)
Parker Schuh19b93b12018-03-02 23:26:58 -0800309
Parker Schuhdc682952018-03-03 18:24:01 -0800310 def get_circular_index(pt):
311 theta1, theta2 = pt
312 circular_index = int(numpy.floor((theta2 - theta1) / numpy.pi))
313 return circular_index
Parker Schuh19b93b12018-03-02 23:26:58 -0800314
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800315 set_color(cr, palette["BLUE"])
Parker Schuhdc682952018-03-03 18:24:01 -0800316 lines = subdivide_theta(lines_theta)
317 o_circular_index = circular_index = get_circular_index(lines[0])
318 p_xy = to_xy(lines[0][0], lines[0][1])
319 if circular_index == self.circular_index_select:
320 cr.move_to(p_xy[0] + circular_index * 0, p_xy[1])
321 for pt in lines[1:]:
322 p_xy = to_xy(pt[0], pt[1])
323 circular_index = get_circular_index(pt)
324 if o_circular_index == self.circular_index_select:
325 cr.line_to(p_xy[0] + o_circular_index * 0, p_xy[1])
326 if circular_index != o_circular_index:
327 o_circular_index = circular_index
328 with px(cr):
329 cr.stroke()
330 if circular_index == self.circular_index_select:
331 cr.move_to(p_xy[0] + circular_index * 0, p_xy[1])
Parker Schuh19b93b12018-03-02 23:26:58 -0800332
Parker Schuhdc682952018-03-03 18:24:01 -0800333 with px(cr):
334 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800335
Parker Schuhdc682952018-03-03 18:24:01 -0800336 if not self.theta_version:
Ravago Jones5127ccc2022-07-31 16:32:45 -0700337 theta1, theta2 = to_theta(self.last_pos,
338 self.circular_index_select)
Parker Schuhdc682952018-03-03 18:24:01 -0800339 x, y = joint_center[0], joint_center[1]
340 cr.move_to(x, y)
Parker Schuh19b93b12018-03-02 23:26:58 -0800341
Parker Schuhdc682952018-03-03 18:24:01 -0800342 x += numpy.cos(theta1) * l1
343 y += numpy.sin(theta1) * l1
344 cr.line_to(x, y)
345 x += numpy.cos(theta2) * l2
346 y += numpy.sin(theta2) * l2
347 cr.line_to(x, y)
348 with px(cr):
349 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800350
Parker Schuhdc682952018-03-03 18:24:01 -0800351 cr.move_to(self.last_pos[0], self.last_pos[1])
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800352 set_color(cr, Color(0.0, 1.0, 0.2))
Parker Schuhdc682952018-03-03 18:24:01 -0800353 draw_px_cross(cr, 20)
Parker Schuh19b93b12018-03-02 23:26:58 -0800354
Parker Schuhdc682952018-03-03 18:24:01 -0800355 if self.theta_version:
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800356 set_color(cr, Color(0.0, 1.0, 0.2))
Parker Schuhdc682952018-03-03 18:24:01 -0800357 cr.move_to(self.last_pos[0], self.last_pos[1])
358 draw_px_cross(cr, 5)
Parker Schuh19b93b12018-03-02 23:26:58 -0800359
Parker Schuhdc682952018-03-03 18:24:01 -0800360 c_pt, dist = closest_segment(lines_theta, self.last_pos)
361 print("dist:", dist, c_pt, self.last_pos)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800362 set_color(cr, palette["CYAN"])
Parker Schuhdc682952018-03-03 18:24:01 -0800363 cr.move_to(c_pt[0], c_pt[1])
364 draw_px_cross(cr, 5)
Parker Schuh19b93b12018-03-02 23:26:58 -0800365
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800366 set_color(cr, Color(0.0, 0.5, 1.0))
Parker Schuhdc682952018-03-03 18:24:01 -0800367 for segment in self.segments:
368 color = [0, random.random(), 1]
369 random.shuffle(color)
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800370 set_color(cr, Color(color[0], color[1], color[2]))
Parker Schuhdc682952018-03-03 18:24:01 -0800371 segment.DrawTo(cr, self.theta_version)
372 with px(cr):
373 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800374
Tabitha Jarvis1007a132018-12-12 21:47:54 -0800375 set_color(cr, Color(0.0, 1.0, 0.5))
Parker Schuhdc682952018-03-03 18:24:01 -0800376 segment = self.current_seg()
377 if segment:
378 print(segment)
379 segment.DrawTo(cr, self.theta_version)
380 with px(cr):
381 cr.stroke()
Parker Schuh19b93b12018-03-02 23:26:58 -0800382
Parker Schuhdc682952018-03-03 18:24:01 -0800383 def cur_pt_in_theta(self):
384 if self.theta_version: return self.last_pos
385 return to_theta(self.last_pos, self.circular_index_select)
386
387 # Current segment based on which mode the drawing system is in.
388 def current_seg(self):
389 if self.prev_segment_pt and self.now_segment_pt:
390 if self.theta_version:
391 return AngleSegment(self.prev_segment_pt, self.now_segment_pt)
392 else:
393 return XYSegment(self.prev_segment_pt, self.now_segment_pt)
394
395 def do_key_press(self, event):
396 keyval = Gdk.keyval_to_lower(event.keyval)
397 print("Gdk.KEY_" + Gdk.keyval_name(keyval))
398 if keyval == Gdk.KEY_q:
399 print("Found q key and exiting.")
400 quit_main_loop()
401 elif keyval == Gdk.KEY_c:
402 # Increment which arm solution we render
403 self.circular_index_select += 1
404 print(self.circular_index_select)
405 elif keyval == Gdk.KEY_v:
406 # Decrement which arm solution we render
407 self.circular_index_select -= 1
408 print(self.circular_index_select)
409 elif keyval == Gdk.KEY_w:
410 # Add this segment to the segment list.
411 segment = self.current_seg()
412 if segment: self.segments.append(segment)
413 self.prev_segment_pt = self.now_segment_pt
414
415 elif keyval == Gdk.KEY_r:
416 self.prev_segment_pt = self.now_segment_pt
417
418 elif keyval == Gdk.KEY_p:
419 # Print out the segments.
420 print(repr(self.segments))
421 elif keyval == Gdk.KEY_g:
422 # Generate theta points.
423 if self.segments:
424 print(repr(self.segments[0].ToThetaPoints()))
425 elif keyval == Gdk.KEY_e:
426 best_pt = self.now_segment_pt
427 best_dist = 1e10
428 for segment in self.segments:
429 d = angle_dist_sqr(segment.start, self.now_segment_pt)
430 if (d < best_dist):
431 best_pt = segment.start
432 best_dist = d
433 d = angle_dist_sqr(segment.end, self.now_segment_pt)
434 if (d < best_dist):
435 best_pt = segment.end
436 best_dist = d
437 self.now_segment_pt = best_pt
438
439 elif keyval == Gdk.KEY_t:
440 # Toggle between theta and xy renderings
441 if self.theta_version:
442 theta1, theta2 = self.last_pos
443 data = to_xy(theta1, theta2)
444 self.circular_index_select = int(
445 numpy.floor((theta2 - theta1) / numpy.pi))
446 self.last_pos = (data[0], data[1])
447 else:
448 self.last_pos = self.cur_pt_in_theta()
449
450 self.theta_version = not self.theta_version
451 self.reinit_extents()
Austin Schuhee249892018-07-08 16:20:09 -0700452
453 elif keyval == Gdk.KEY_z:
454 self.edit_control1 = not self.edit_control1
455 if self.edit_control1:
456 self.now_segment_pt = self.segments[0].control1
457 else:
458 self.now_segment_pt = self.segments[0].control2
459 if not self.theta_version:
460 data = to_xy(self.now_segment_pt[0], self.now_segment_pt[1])
461 self.last_pos = (data[0], data[1])
462 else:
463 self.last_pos = self.now_segment_pt
464
465 print("self.last_pos: ", self.last_pos, " ci: ",
466 self.circular_index_select)
467
Parker Schuhdc682952018-03-03 18:24:01 -0800468 self.redraw()
469
470 def do_button_press(self, event):
471 self.last_pos = (event.x, event.y)
472 self.now_segment_pt = self.cur_pt_in_theta()
Austin Schuhee249892018-07-08 16:20:09 -0700473
474 if self.edit_control1:
475 self.segments[0].control1 = self.now_segment_pt
476 else:
477 self.segments[0].control2 = self.now_segment_pt
478
Ravago Jones5127ccc2022-07-31 16:32:45 -0700479 print('Clicked at theta: %s' % (repr(self.now_segment_pt, )))
Parker Schuhdc682952018-03-03 18:24:01 -0800480 if not self.theta_version:
481 print('Clicked at xy, circular index: (%f, %f, %f)' %
482 (self.last_pos[0], self.last_pos[1],
483 self.circular_index_select))
484
Ravago Jones5127ccc2022-07-31 16:32:45 -0700485 print('c1: numpy.array([%f, %f])' %
486 (self.segments[0].control1[0], self.segments[0].control1[1]))
487 print('c2: numpy.array([%f, %f])' %
488 (self.segments[0].control2[0], self.segments[0].control2[1]))
Austin Schuhed018082018-07-08 16:01:01 -0700489
Parker Schuhdc682952018-03-03 18:24:01 -0800490 self.redraw()
491
Parker Schuh19b93b12018-03-02 23:26:58 -0800492
493silly = Silly()
Parker Schuhdc682952018-03-03 18:24:01 -0800494silly.segments = graph_generate.segments
Parker Schuh19b93b12018-03-02 23:26:58 -0800495basic_window.RunApp()