Brian Silverman | b0ebf1d | 2018-10-17 23:36:40 -0700 | [diff] [blame] | 1 | #!/usr/bin/python3 |
| 2 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 3 | from __future__ import print_function |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 4 | import os |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame^] | 5 | from frc971.control_loops.python import basic_window |
| 6 | from frc971.control_loops.python.color import Color, palette |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 7 | import random |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 8 | import gi |
| 9 | import numpy |
| 10 | gi.require_version('Gtk', '3.0') |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame^] | 11 | from gi.repository import Gdk, Gtk |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 12 | import cairo |
| 13 | import graph_generate |
| 14 | from graph_generate import XYSegment, AngleSegment, to_theta, to_xy, alpha_blend |
| 15 | from graph_generate import back_to_xy_loop, subdivide_theta, to_theta_loop |
| 16 | from graph_generate import l1, l2, joint_center |
| 17 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame^] | 18 | from frc971.control_loops.python.basic_window import OverrideMatrix, identity, quit_main_loop, set_color |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 19 | |
| 20 | import shapely |
| 21 | from shapely.geometry import Polygon |
| 22 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 23 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 24 | def px(cr): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 25 | return OverrideMatrix(cr, identity) |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 26 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 27 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 28 | def draw_px_cross(cr, length_px): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 29 | """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 Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 35 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 36 | cr.move_to(x - length_px, y) |
| 37 | cr.line_to(x + length_px, y) |
| 38 | cr.stroke() |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 39 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 40 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 41 | def angle_dist_sqr(a1, a2): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 42 | """Distance between two points in angle space.""" |
| 43 | return (a1[0] - a2[0])**2 + (a1[1] - a2[1])**2 |
| 44 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 45 | |
| 46 | # Find the highest y position that intersects the vertical line defined by x. |
| 47 | def inter_y(x): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 48 | return numpy.sqrt((l2 + l1)**2 - |
| 49 | (x - joint_center[0])**2) + joint_center[1] |
| 50 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 51 | |
| 52 | # This is the x position where the inner (hyperextension) circle intersects the horizontal line |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 53 | derr = numpy.sqrt((l1 - l2)**2 - (joint_center[1] - 0.3048)**2) |
| 54 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 55 | |
| 56 | # Define min and max l1 angles based on vertical constraints. |
| 57 | def get_angle(boundary): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 58 | h = numpy.sqrt((l1)**2 - (boundary - joint_center[0])**2) + joint_center[1] |
| 59 | return numpy.arctan2(h, boundary - joint_center[0]) |
| 60 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 61 | |
| 62 | # left hand side lines |
| 63 | lines1 = [ |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 64 | (-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 Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 69 | ] |
| 70 | |
| 71 | # right hand side lines |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 72 | lines2 = [(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 Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 75 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 76 | t1_min = get_angle((32.525 - 4.0) * 0.0254) |
| 77 | t2_min = -7.0 / 4.0 * numpy.pi |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 78 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 79 | t1_max = get_angle((-32.525 + 4.0) * 0.0254) |
| 80 | t2_max = numpy.pi * 3.0 / 4.0 |
| 81 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 82 | |
| 83 | # Draw lines to cr + stroke. |
| 84 | def draw_lines(cr, lines): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 85 | 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 Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 91 | |
| 92 | # Rotate a rasterized loop such that it aligns to when the parameters loop |
| 93 | def rotate_to_jump_point(points): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 94 | 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 Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 99 | return points[pt_i:] + points[:pt_i] |
| 100 | last_pt = pt |
| 101 | return points |
| 102 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 103 | |
| 104 | # shift points vertically by dy. |
| 105 | def y_shift(points, dy): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 106 | return [(x, y + dy) for x, y in points] |
| 107 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 108 | |
| 109 | lines1_theta_part = rotate_to_jump_point(to_theta_loop(lines1, 0)) |
| 110 | lines2_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. |
| 113 | lines1_theta = y_shift(lines1_theta_part, -numpy.pi * 2) + lines1_theta_part + \ |
| 114 | y_shift(lines1_theta_part, numpy.pi * 2) |
| 115 | lines2_theta = y_shift(lines2_theta_part, numpy.pi * 2) + lines2_theta_part + \ |
| 116 | y_shift(lines2_theta_part, -numpy.pi * 2) |
| 117 | |
| 118 | lines_theta = lines1_theta + lines2_theta |
| 119 | |
| 120 | p1 = Polygon(lines_theta) |
| 121 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 122 | p2 = Polygon([(t1_min, t2_min), (t1_max, t2_min), (t1_max, t2_max), (t1_min, |
| 123 | t2_max)]) |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 124 | |
| 125 | # Fully computed theta constrints. |
| 126 | lines_theta = list(p1.intersection(p2).exterior.coords) |
| 127 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 128 | lines1_theta_back = back_to_xy_loop(lines1_theta) |
| 129 | lines2_theta_back = back_to_xy_loop(lines2_theta) |
| 130 | |
| 131 | lines_theta_back = back_to_xy_loop(lines_theta) |
| 132 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 133 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 134 | # Get the closest point to a line from a test pt. |
| 135 | def get_closest(prev, cur, pt): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 136 | dx_ang = (cur[0] - prev[0]) |
| 137 | dy_ang = (cur[1] - prev[1]) |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 138 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 139 | 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 Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 158 | |
| 159 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame^] | 160 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 161 | def closest_segment(lines, pt): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 162 | 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 Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 172 | |
| 173 | # Create a GTK+ widget on which we will draw using Cairo |
| 174 | class Silly(basic_window.BaseWindow): |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 175 | def __init__(self): |
| 176 | super(Silly, self).__init__() |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 177 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame^] | 178 | 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 Schuh | ed01808 | 2018-07-08 16:01:01 -0700 | [diff] [blame] | 193 | self.theta_version = False |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 194 | self.reinit_extents() |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 195 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 196 | self.last_pos = (numpy.pi / 2.0, 1.0) |
| 197 | self.circular_index_select = -1 |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 198 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 199 | # Extra stuff for drawing lines. |
| 200 | self.segments = [] |
| 201 | self.prev_segment_pt = None |
| 202 | self.now_segment_pt = None |
Austin Schuh | ee24989 | 2018-07-08 16:20:09 -0700 | [diff] [blame] | 203 | self.spline_edit = 0 |
| 204 | self.edit_control1 = True |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 205 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame^] | 206 | 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 Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 238 | 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 Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 244 | else: |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 245 | 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 Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 249 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 250 | 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 Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 255 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 256 | # Handle the expose-event by drawing |
| 257 | def handle_draw(self, cr): |
| 258 | # use "with px(cr): blah;" to transform to pixel coordinates. |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 259 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 260 | # Fill the background color of the window with grey |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 261 | set_color(cr, palette["GREY"]) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 262 | cr.paint() |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 263 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 264 | # Draw a extents rectangle |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 265 | set_color(cr, palette["WHITE"]) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 266 | 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 Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 270 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 271 | if not self.theta_version: |
| 272 | # Draw a filled white rectangle. |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 273 | set_color(cr, palette["WHITE"]) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 274 | cr.rectangle(-2.0, -2.0, 4.0, 4.0) |
| 275 | cr.fill() |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 276 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 277 | set_color(cr, palette["BLUE"]) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 278 | 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 Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 288 | set_color(cr, palette["WHITE"]) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 289 | cr.rectangle(-numpy.pi, -numpy.pi, numpy.pi * 2.0, numpy.pi * 2.0) |
| 290 | cr.fill() |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 291 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 292 | if self.theta_version: |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 293 | set_color(cr, palette["BLUE"]) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 294 | 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 Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 299 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 300 | if self.theta_version: |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 301 | set_color(cr, Color(0.5, 0.5, 1.0)) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 302 | draw_lines(cr, lines_theta) |
| 303 | else: |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 304 | set_color(cr, Color(0.5, 1.0, 1.0)) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 305 | draw_lines(cr, lines1) |
| 306 | draw_lines(cr, lines2) |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 307 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 308 | def get_circular_index(pt): |
| 309 | theta1, theta2 = pt |
| 310 | circular_index = int(numpy.floor((theta2 - theta1) / numpy.pi)) |
| 311 | return circular_index |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 312 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 313 | set_color(cr, palette["BLUE"]) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 314 | 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 Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 330 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 331 | with px(cr): |
| 332 | cr.stroke() |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 333 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 334 | 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 Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 338 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 339 | 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 Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 347 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 348 | cr.move_to(self.last_pos[0], self.last_pos[1]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 349 | set_color(cr, Color(0.0, 1.0, 0.2)) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 350 | draw_px_cross(cr, 20) |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 351 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 352 | if self.theta_version: |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 353 | set_color(cr, Color(0.0, 1.0, 0.2)) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 354 | cr.move_to(self.last_pos[0], self.last_pos[1]) |
| 355 | draw_px_cross(cr, 5) |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 356 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 357 | c_pt, dist = closest_segment(lines_theta, self.last_pos) |
| 358 | print("dist:", dist, c_pt, self.last_pos) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 359 | set_color(cr, palette["CYAN"]) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 360 | cr.move_to(c_pt[0], c_pt[1]) |
| 361 | draw_px_cross(cr, 5) |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 362 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 363 | set_color(cr, Color(0.0, 0.5, 1.0)) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 364 | for segment in self.segments: |
| 365 | color = [0, random.random(), 1] |
| 366 | random.shuffle(color) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 367 | set_color(cr, Color(color[0], color[1], color[2])) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 368 | segment.DrawTo(cr, self.theta_version) |
| 369 | with px(cr): |
| 370 | cr.stroke() |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 371 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 372 | set_color(cr, Color(0.0, 1.0, 0.5)) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 373 | 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 Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 379 | |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 380 | 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 Schuh | ee24989 | 2018-07-08 16:20:09 -0700 | [diff] [blame] | 449 | |
| 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 Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 465 | 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 Schuh | ee24989 | 2018-07-08 16:20:09 -0700 | [diff] [blame] | 470 | |
| 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 Schuh | 17e484e | 2018-03-11 01:11:36 -0800 | [diff] [blame] | 476 | print('Clicked at theta: %s' % (repr(self.now_segment_pt,))) |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 477 | 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 Schuh | ed01808 | 2018-07-08 16:01:01 -0700 | [diff] [blame] | 482 | 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 Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 487 | self.redraw() |
| 488 | |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 489 | |
| 490 | silly = Silly() |
Parker Schuh | dc68295 | 2018-03-03 18:24:01 -0800 | [diff] [blame] | 491 | silly.segments = graph_generate.segments |
Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame] | 492 | basic_window.RunApp() |