Parker Schuh | 19b93b1 | 2018-03-02 23:26:58 -0800 | [diff] [blame^] | 1 | import os |
| 2 | import basic_window |
| 3 | import gi |
| 4 | import numpy |
| 5 | gi.require_version('Gtk', '3.0') |
| 6 | from gi.repository import Gdk |
| 7 | import cairo |
| 8 | import graph_generate |
| 9 | from graph_generate import XYSegment, AngleSegment, to_theta, to_xy, alpha_blend |
| 10 | from graph_generate import back_to_xy_loop, subdivide_theta, to_theta_loop |
| 11 | from graph_generate import l1, l2, joint_center |
| 12 | |
| 13 | from basic_window import OverrideMatrix, identity, quit_main_loop |
| 14 | |
| 15 | import shapely |
| 16 | from shapely.geometry import Polygon |
| 17 | |
| 18 | def px(cr): |
| 19 | return OverrideMatrix(cr, identity) |
| 20 | |
| 21 | # Draws a cross with fixed dimensions in pixel space. |
| 22 | def draw_px_cross(cr, length_px): |
| 23 | with px(cr): |
| 24 | x,y = cr.get_current_point() |
| 25 | cr.move_to(x, y - length_px) |
| 26 | cr.line_to(x, y + length_px) |
| 27 | cr.stroke() |
| 28 | |
| 29 | cr.move_to(x - length_px, y) |
| 30 | cr.line_to(x + length_px, y) |
| 31 | cr.stroke() |
| 32 | |
| 33 | # Distance between two points in angle space. |
| 34 | def angle_dist_sqr(a1, a2): |
| 35 | return (a1[0] - a2[0]) ** 2 + (a1[1] - a2[1]) ** 2 |
| 36 | |
| 37 | # Find the highest y position that intersects the vertical line defined by x. |
| 38 | def inter_y(x): |
| 39 | return numpy.sqrt((l2 + l1) ** 2 - (x - joint_center[0]) ** 2) + joint_center[1] |
| 40 | |
| 41 | # This is the x position where the inner (hyperextension) circle intersects the horizontal line |
| 42 | derr = numpy.sqrt((l1 - l2) ** 2 - (joint_center[1] - 12.0) ** 2) |
| 43 | |
| 44 | # Define min and max l1 angles based on vertical constraints. |
| 45 | def get_angle(boundary): |
| 46 | h = numpy.sqrt((l1) ** 2 - (boundary - joint_center[0]) ** 2) + joint_center[1] |
| 47 | return numpy.arctan2(h, boundary - joint_center[0]) |
| 48 | |
| 49 | # left hand side lines |
| 50 | lines1 = [ |
| 51 | (-32.525, inter_y(-32.525)), |
| 52 | (-32.525, 5.5), |
| 53 | (-23.025, 5.5), |
| 54 | (-23.025, 12.0), |
| 55 | (joint_center[0] - derr, 12.0), |
| 56 | ] |
| 57 | |
| 58 | # right hand side lines |
| 59 | lines2 = [ |
| 60 | (joint_center[0] + derr, 12.0), |
| 61 | (16.625, 12.0), |
| 62 | (16.625, 5.5), |
| 63 | (32.525, 5.5), |
| 64 | (32.525, inter_y(32.525)) |
| 65 | ] |
| 66 | |
| 67 | t1_min = get_angle(32.525 - 4.0) |
| 68 | t2_min = -7 / 4.0 * numpy.pi |
| 69 | |
| 70 | t1_max = get_angle(-32.525 + 4.0) |
| 71 | t2_max = numpy.pi * 3 / 4.0 |
| 72 | |
| 73 | # Draw lines to cr + stroke. |
| 74 | def draw_lines(cr, lines): |
| 75 | cr.move_to(lines[0][0], lines[0][1]) |
| 76 | for pt in lines[1:]: |
| 77 | cr.line_to(pt[0], pt[1]) |
| 78 | with px(cr): cr.stroke() |
| 79 | |
| 80 | # Rotate a rasterized loop such that it aligns to when the parameters loop |
| 81 | def rotate_to_jump_point(points): |
| 82 | last_pt = points[0] |
| 83 | for pt_i in range(1, len(points)): |
| 84 | pt = points[pt_i] |
| 85 | delta = last_pt[1] - pt[1] |
| 86 | if abs(delta) > numpy.pi: |
| 87 | print(delta) |
| 88 | return points[pt_i:] + points[:pt_i] |
| 89 | last_pt = pt |
| 90 | return points |
| 91 | |
| 92 | # shift points vertically by dy. |
| 93 | def y_shift(points, dy): |
| 94 | return [(x, y + dy) for x, y in points] |
| 95 | |
| 96 | lines1_theta_part = rotate_to_jump_point(to_theta_loop(lines1, 0)) |
| 97 | lines2_theta_part = rotate_to_jump_point(to_theta_loop(lines2)) |
| 98 | |
| 99 | # Some hacks here to make a single polygon by shifting to get an extra copy of the contraints. |
| 100 | lines1_theta = y_shift(lines1_theta_part, -numpy.pi * 2) + lines1_theta_part + \ |
| 101 | y_shift(lines1_theta_part, numpy.pi * 2) |
| 102 | lines2_theta = y_shift(lines2_theta_part, numpy.pi * 2) + lines2_theta_part + \ |
| 103 | y_shift(lines2_theta_part, -numpy.pi * 2) |
| 104 | |
| 105 | lines_theta = lines1_theta + lines2_theta |
| 106 | |
| 107 | p1 = Polygon(lines_theta) |
| 108 | |
| 109 | p2 = Polygon([(t1_min, t2_min), (t1_max, t2_min), |
| 110 | (t1_max, t2_max), (t1_min, t2_max)]) |
| 111 | |
| 112 | # Fully computed theta constrints. |
| 113 | lines_theta = list(p1.intersection(p2).exterior.coords) |
| 114 | |
| 115 | print(", ".join("{%s, %s}" % (a,b) for a, b in lines_theta)) |
| 116 | |
| 117 | lines1_theta_back = back_to_xy_loop(lines1_theta) |
| 118 | lines2_theta_back = back_to_xy_loop(lines2_theta) |
| 119 | |
| 120 | lines_theta_back = back_to_xy_loop(lines_theta) |
| 121 | |
| 122 | # Get the closest point to a line from a test pt. |
| 123 | def get_closest(prev, cur, pt): |
| 124 | dx_ang = (cur[0] - prev[0]) |
| 125 | dy_ang = (cur[1] - prev[1]) |
| 126 | |
| 127 | d = numpy.sqrt(dx_ang ** 2 + dy_ang ** 2) |
| 128 | if (d < 0.000001): |
| 129 | return prev, numpy.sqrt((prev[0] - pt[0]) ** 2 + (prev[1] - pt[1]) ** 2) |
| 130 | |
| 131 | |
| 132 | pdx = -dy_ang / d |
| 133 | pdy = dx_ang / d |
| 134 | |
| 135 | dpx = pt[0] - prev[0] |
| 136 | dpy = pt[1] - prev[1] |
| 137 | |
| 138 | alpha = (dx_ang * dpx + dy_ang * dpy) / d / d |
| 139 | |
| 140 | if (alpha < 0): |
| 141 | return prev, numpy.sqrt((prev[0] - pt[0]) ** 2 + (prev[1] - pt[1]) ** 2) |
| 142 | elif (alpha > 1): |
| 143 | return cur, numpy.sqrt((cur[0] - pt[0]) ** 2 + (cur[1] - pt[1]) ** 2) |
| 144 | else: |
| 145 | return (alpha_blend(prev[0], cur[0], alpha), alpha_blend(prev[1], cur[1], alpha)), \ |
| 146 | abs(dpx * pdx + dpy * pdy) |
| 147 | |
| 148 | # |
| 149 | def closest_segment(lines, pt): |
| 150 | c_pt, c_pt_dist = get_closest(lines[-1], lines[0], pt) |
| 151 | for i in range(1, len(lines)): |
| 152 | prev = lines[i - 1] |
| 153 | cur = lines[i] |
| 154 | c_pt_new, c_pt_new_dist = get_closest(prev, cur, pt) |
| 155 | if c_pt_new_dist < c_pt_dist: |
| 156 | c_pt = c_pt_new |
| 157 | c_pt_dist = c_pt_new_dist |
| 158 | return c_pt, c_pt_dist |
| 159 | |
| 160 | # Create a GTK+ widget on which we will draw using Cairo |
| 161 | class Silly(basic_window.BaseWindow): |
| 162 | def __init__(self): |
| 163 | super().__init__() |
| 164 | |
| 165 | self.theta_version = True |
| 166 | self.reinit_extents() |
| 167 | |
| 168 | self.last_pos = (20, 20) |
| 169 | self.c_i_select = 0 |
| 170 | self.click_bool = False |
| 171 | |
| 172 | |
| 173 | # Extra stuff for drawing lines. |
| 174 | self.segs = [] |
| 175 | self.prev_seg_pt = None |
| 176 | self.now_seg_pt = None |
| 177 | |
| 178 | def reinit_extents(self): |
| 179 | if self.theta_version: |
| 180 | self.extents_x_min = -numpy.pi * 2 |
| 181 | self.extents_x_max = numpy.pi * 2 |
| 182 | self.extents_y_min = -numpy.pi * 2 |
| 183 | self.extents_y_max = numpy.pi * 2 |
| 184 | else: |
| 185 | self.extents_x_min = -40.0 |
| 186 | self.extents_x_max = 40.0 |
| 187 | self.extents_y_min = -4.0 |
| 188 | self.extents_y_max = 110.0 |
| 189 | |
| 190 | self.init_extents((0.5*(self.extents_x_min+self.extents_x_max), 0.5*(self.extents_y_max+self.extents_y_min)), |
| 191 | (1.0*(self.extents_x_max-self.extents_x_min), 1.0*(self.extents_y_max-self.extents_y_min))) |
| 192 | |
| 193 | # Handle the expose-event by drawing |
| 194 | def handle_draw(self, cr): |
| 195 | # use "with px(cr): blah;" to transform to pixel coordinates. |
| 196 | |
| 197 | # Fill the background color of the window with grey |
| 198 | cr.set_source_rgb(0.5, 0.5, 0.5) |
| 199 | cr.paint() |
| 200 | |
| 201 | # Draw a extents rectangle |
| 202 | cr.set_source_rgb(1.0, 1.0, 1.0) |
| 203 | cr.rectangle(self.extents_x_min, self.extents_y_min, |
| 204 | (self.extents_x_max-self.extents_x_min), self.extents_y_max-self.extents_y_min) |
| 205 | cr.fill() |
| 206 | |
| 207 | if not self.theta_version: |
| 208 | |
| 209 | # Draw a filled white rectangle. |
| 210 | cr.set_source_rgb(1.0, 1.0, 1.0) |
| 211 | cr.rectangle(-2.0, -2.0, 4.0, 4.0) |
| 212 | cr.fill() |
| 213 | |
| 214 | cr.set_source_rgb(0.0, 0.0, 1.0) |
| 215 | cr.arc(joint_center[0], joint_center[1], l2 + l1, 0, 2 * numpy.pi) |
| 216 | with px(cr): cr.stroke() |
| 217 | cr.arc(joint_center[0], joint_center[1], l1 - l2, 0, 2 * numpy.pi) |
| 218 | with px(cr): cr.stroke() |
| 219 | |
| 220 | else: |
| 221 | # Draw a filled white rectangle. |
| 222 | cr.set_source_rgb(1.0, 1.0, 1.0) |
| 223 | cr.rectangle(-numpy.pi, -numpy.pi, numpy.pi * 2, numpy.pi * 2) |
| 224 | cr.fill() |
| 225 | |
| 226 | if self.theta_version: |
| 227 | cr.set_source_rgb(0.0, 0.0, 1.0) |
| 228 | for i in range(-6, 6): |
| 229 | cr.move_to(-40, -40 + i * numpy.pi) |
| 230 | cr.line_to(40, 40 + i * numpy.pi) |
| 231 | with px(cr): cr.stroke() |
| 232 | |
| 233 | |
| 234 | if not self.theta_version: |
| 235 | cr.set_source_rgb(0.2, 1.0, 0.2) |
| 236 | draw_lines(cr, lines2) |
| 237 | |
| 238 | if self.theta_version: |
| 239 | cr.set_source_rgb(0.5, 0.5, 1.0) |
| 240 | draw_lines(cr, lines_theta) |
| 241 | |
| 242 | else: |
| 243 | cr.set_source_rgb(0.5, 1.0, 1.0) |
| 244 | draw_lines(cr, lines1) |
| 245 | draw_lines(cr, lines2) |
| 246 | |
| 247 | def set_color(cr, c_i): |
| 248 | if c_i == -2: |
| 249 | cr.set_source_rgb(0.0, 0.25, 1.0) |
| 250 | elif c_i == -1: |
| 251 | cr.set_source_rgb(0.5, 0.0, 1.0) |
| 252 | elif c_i == 0: |
| 253 | cr.set_source_rgb(0.5, 1.0, 1.0) |
| 254 | elif c_i == 1: |
| 255 | cr.set_source_rgb(0.0, 0.5, 1.0) |
| 256 | elif c_i == 2: |
| 257 | cr.set_source_rgb(0.5, 1.0, 0.5) |
| 258 | else: |
| 259 | cr.set_source_rgb(1.0, 0.0, 0.0) |
| 260 | |
| 261 | def get_ci(pt): |
| 262 | t1, t2 = pt |
| 263 | c_i = int(numpy.floor((t2 - t1) / numpy.pi)) |
| 264 | return c_i |
| 265 | |
| 266 | cr.set_source_rgb(0.0, 0.0, 1.0) |
| 267 | lines = subdivide_theta(lines_theta) |
| 268 | o_c_i = c_i = get_ci(lines[0]) |
| 269 | p_xy = to_xy(lines[0][0], lines[0][1]) |
| 270 | if c_i == self.c_i_select: cr.move_to(p_xy[0] + c_i * 0, p_xy[1]) |
| 271 | for pt in lines[1:]: |
| 272 | p_xy = to_xy(pt[0], pt[1]) |
| 273 | c_i = get_ci(pt) |
| 274 | if o_c_i == self.c_i_select: cr.line_to(p_xy[0] + o_c_i * 0, p_xy[1]) |
| 275 | if c_i != o_c_i: |
| 276 | o_c_i = c_i |
| 277 | with px(cr): cr.stroke() |
| 278 | if c_i == self.c_i_select: cr.move_to(p_xy[0] + c_i * 0, p_xy[1]) |
| 279 | |
| 280 | with px(cr): cr.stroke() |
| 281 | |
| 282 | if not self.theta_version: |
| 283 | t1, t2 = to_theta(self.last_pos[0], self.last_pos[1], (self.c_i_select % 2) == 0) |
| 284 | x, y = joint_center[0], joint_center[1] |
| 285 | cr.move_to(x, y) |
| 286 | |
| 287 | x += numpy.cos(t1) * l1 |
| 288 | y += numpy.sin(t1) * l1 |
| 289 | cr.line_to(x, y) |
| 290 | x += numpy.cos(t2) * l2 |
| 291 | y += numpy.sin(t2) * l2 |
| 292 | cr.line_to(x, y) |
| 293 | with px(cr): cr.stroke() |
| 294 | |
| 295 | cr.move_to(self.last_pos[0], self.last_pos[1]) |
| 296 | cr.set_source_rgb(0.0, 1.0, 0.2) |
| 297 | draw_px_cross(cr, 20) |
| 298 | |
| 299 | if self.theta_version: |
| 300 | cr.set_source_rgb(0.0, 1.0, 0.2) |
| 301 | |
| 302 | cr.set_source_rgb(0.0, 1.0, 0.2) |
| 303 | cr.move_to(self.last_pos[0], self.last_pos[1]) |
| 304 | draw_px_cross(cr, 5) |
| 305 | |
| 306 | c_pt, dist = closest_segment(lines_theta, self.last_pos) |
| 307 | print("dist:", dist, c_pt, self.last_pos) |
| 308 | cr.set_source_rgb(0.0, 1.0, 1.0) |
| 309 | cr.move_to(c_pt[0], c_pt[1]) |
| 310 | draw_px_cross(cr, 5) |
| 311 | |
| 312 | cr.set_source_rgb(0.0, 0.5, 1.0) |
| 313 | for seg in self.segs: |
| 314 | seg.DrawTo(cr, self.theta_version) |
| 315 | with px(cr): cr.stroke() |
| 316 | |
| 317 | cr.set_source_rgb(0.0, 1.0, 0.5) |
| 318 | seg = self.current_seg() |
| 319 | print(seg) |
| 320 | if seg: |
| 321 | seg.DrawTo(cr, self.theta_version) |
| 322 | with px(cr): cr.stroke() |
| 323 | |
| 324 | def cur_pt_in_theta(self): |
| 325 | if self.theta_version: return self.last_pos |
| 326 | t1, t2 = to_theta(self.last_pos[0], self.last_pos[1], (self.c_i_select % 2) == 0) |
| 327 | n_ci = int(numpy.floor((t2 - t1) / numpy.pi)) |
| 328 | t2 = t2 + ((self.c_i_select - n_ci)) * numpy.pi |
| 329 | return (t1, t2) |
| 330 | |
| 331 | # Current seg based on which mode the drawing system is in. |
| 332 | def current_seg(self): |
| 333 | if self.prev_seg_pt and self.now_seg_pt: |
| 334 | if self.theta_version: |
| 335 | return AngleSegment(self.prev_seg_pt, self.now_seg_pt) |
| 336 | else: |
| 337 | return XYSegment(self.prev_seg_pt, self.now_seg_pt) |
| 338 | |
| 339 | def do_key_press(self, event): |
| 340 | print("Gdk.KEY_" + Gdk.keyval_name(event.keyval)) |
| 341 | print("Gdk.KEY_" + Gdk.keyval_name(Gdk.keyval_to_lower(event.keyval)) + " is the lower case key for this button press.") |
| 342 | if ( Gdk.keyval_to_lower(event.keyval) == Gdk.KEY_q ): |
| 343 | print("Found q key and exiting.") |
| 344 | quit_main_loop() |
| 345 | elif ( Gdk.keyval_to_lower(event.keyval) == Gdk.KEY_c ): |
| 346 | self.c_i_select += 1 |
| 347 | elif ( Gdk.keyval_to_lower(event.keyval) == Gdk.KEY_v ): |
| 348 | self.c_i_select -= 1 |
| 349 | elif ( Gdk.keyval_to_lower(event.keyval) == Gdk.KEY_f ): |
| 350 | self.click_bool = not self.click_bool |
| 351 | |
| 352 | elif ( Gdk.keyval_to_lower(event.keyval) == Gdk.KEY_w ): |
| 353 | seg = self.current_seg(); |
| 354 | if seg: self.segs.append(seg) |
| 355 | self.prev_seg_pt = self.now_seg_pt |
| 356 | |
| 357 | elif ( Gdk.keyval_to_lower(event.keyval) == Gdk.KEY_r ): |
| 358 | self.prev_seg_pt = self.now_seg_pt |
| 359 | |
| 360 | elif ( Gdk.keyval_to_lower(event.keyval) == Gdk.KEY_p ): |
| 361 | print(repr(self.segs)) |
| 362 | elif ( Gdk.keyval_to_lower(event.keyval) == Gdk.KEY_g ): |
| 363 | if self.segs: |
| 364 | print(repr(self.segs[0].ToThetaPoints())) |
| 365 | elif ( Gdk.keyval_to_lower(event.keyval) == Gdk.KEY_e ): |
| 366 | best_pt = self.now_seg_pt |
| 367 | best_dist = 1e10 |
| 368 | for seg in self.segs: |
| 369 | d = angle_dist_sqr(seg.st, self.now_seg_pt) |
| 370 | if (d < best_dist): |
| 371 | best_pt = seg.st |
| 372 | best_dist = d; |
| 373 | d = angle_dist_sqr(seg.ed, self.now_seg_pt) |
| 374 | if (d < best_dist): |
| 375 | best_pt = seg.ed |
| 376 | best_dist = d |
| 377 | self.now_seg_pt = best_pt |
| 378 | |
| 379 | elif ( Gdk.keyval_to_lower(event.keyval) == Gdk.KEY_t ): |
| 380 | if self.theta_version: |
| 381 | t1, t2 = self.last_pos |
| 382 | data = to_xy(t1, t2) |
| 383 | self.c_i_select = int(numpy.floor((t2 - t1) / numpy.pi)) |
| 384 | self.last_pos = (data[0], data[1]) |
| 385 | else: |
| 386 | self.last_pos = self.cur_pt_in_theta() |
| 387 | |
| 388 | self.theta_version = not self.theta_version |
| 389 | self.reinit_extents() |
| 390 | self.redraw() |
| 391 | |
| 392 | def do_button_press(self, event): |
| 393 | print(event) |
| 394 | print(event.x, event.y, event.button) |
| 395 | self.last_pos = (event.x, event.y) |
| 396 | self.now_seg_pt = self.cur_pt_in_theta(); |
| 397 | |
| 398 | self.redraw() |
| 399 | |
| 400 | silly = Silly() |
| 401 | silly.segs = graph_generate.segs |
| 402 | basic_window.RunApp() |