Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 2 | from __future__ import print_function |
| 3 | import os |
Andrew Runke | 0f945fd | 2019-01-27 21:10:37 -0800 | [diff] [blame] | 4 | import sys |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 5 | from color import palette |
| 6 | from graph import Graph |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 7 | import gi |
| 8 | import numpy as np |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 9 | gi.require_version('Gtk', '3.0') |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 10 | gi.require_version('Gdk', '3.0') |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 11 | from gi.repository import Gdk, Gtk, GLib |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 12 | import cairo |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 13 | from libspline import Spline |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 14 | import enum |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 15 | import json |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 16 | from constants import FIELD |
| 17 | from constants import get_json_folder |
| 18 | from constants import ROBOT_SIDE_TO_BALL_CENTER, ROBOT_SIDE_TO_HATCH_PANEL, HATCH_PANEL_WIDTH, BALL_RADIUS |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 19 | from drawing_constants import set_color, draw_px_cross, draw_px_x, display_text, draw_control_points |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 20 | from points import Points |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 21 | import time |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 22 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 23 | |
| 24 | class Mode(enum.Enum): |
| 25 | kViewing = 0 |
| 26 | kPlacing = 1 |
| 27 | kEditing = 2 |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 28 | |
| 29 | |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 30 | class FieldWidget(Gtk.DrawingArea): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 31 | """Create a GTK+ widget on which we will draw using Cairo""" |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 32 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 33 | def __init__(self): |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 34 | super(FieldWidget, self).__init__() |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 35 | self.set_field(FIELD) |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 36 | self.set_size_request( |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 37 | self.mToPx(self.field.width), self.mToPx(self.field.length)) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 38 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 39 | self.points = Points() |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 40 | self.graph = Graph() |
| 41 | self.set_vexpand(True) |
| 42 | self.set_hexpand(True) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 43 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 44 | # init field drawing |
| 45 | # add default spline for testing purposes |
| 46 | # init editing / viewing modes and pointer location |
| 47 | self.mode = Mode.kPlacing |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 48 | self.mousex = 0 |
| 49 | self.mousey = 0 |
| 50 | self.module_path = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 51 | self.path_to_export = os.path.join(self.module_path, |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 52 | 'points_for_pathedit.json') |
| 53 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 54 | # For the editing mode |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 55 | self.index_of_edit = -1 # Can't be zero beause array starts at 0 |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 56 | self.held_x = 0 |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 57 | self.spline_edit = -1 |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 58 | |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 59 | self.transform = cairo.Matrix() |
| 60 | |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 61 | self.set_events(Gdk.EventMask.BUTTON_PRESS_MASK |
| 62 | | Gdk.EventMask.BUTTON_PRESS_MASK |
| 63 | | Gdk.EventMask.BUTTON_RELEASE_MASK |
| 64 | | Gdk.EventMask.POINTER_MOTION_MASK |
| 65 | | Gdk.EventMask.SCROLL_MASK) |
| 66 | |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 67 | def set_field(self, field): |
| 68 | self.field = field |
Ravago Jones | c26b916 | 2021-06-30 20:12:48 -0700 | [diff] [blame] | 69 | try: |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 70 | self.field_png = cairo.ImageSurface.create_from_png( |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 71 | "frc971/control_loops/python/field_images/" + |
| 72 | self.field.field_id + ".png") |
Ravago Jones | c26b916 | 2021-06-30 20:12:48 -0700 | [diff] [blame] | 73 | except cairo.Error: |
| 74 | self.field_png = None |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 75 | self.queue_draw() |
Ravago Jones | c26b916 | 2021-06-30 20:12:48 -0700 | [diff] [blame] | 76 | |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 77 | # returns the transform from widget space to field space |
| 78 | @property |
| 79 | def input_transform(self): |
| 80 | xx, yx, xy, yy, x0, y0 = self.transform |
| 81 | matrix = cairo.Matrix(xx, yx, xy, yy, x0, y0) |
| 82 | # the transform for input needs to be the opposite of the transform for drawing |
| 83 | matrix.invert() |
| 84 | return matrix |
| 85 | |
| 86 | # returns the scale from pixels in field space to meters in field space |
| 87 | def pxToM_scale(self): |
| 88 | available_space = self.get_allocation() |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 89 | return np.maximum(self.field.width / available_space.width, |
| 90 | self.field.length / available_space.height) |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 91 | |
| 92 | def pxToM(self, p): |
| 93 | return p * self.pxToM_scale() |
| 94 | |
| 95 | def mToPx(self, m): |
| 96 | return m / self.pxToM_scale() |
| 97 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 98 | def draw_robot_at_point(self, cr, i, p, spline): |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 99 | p1 = [self.mToPx(spline.Point(i)[0]), self.mToPx(spline.Point(i)[1])] |
| 100 | p2 = [ |
| 101 | self.mToPx(spline.Point(i + p)[0]), |
| 102 | self.mToPx(spline.Point(i + p)[1]) |
| 103 | ] |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 104 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 105 | #Calculate Robot |
| 106 | distance = np.sqrt((p2[1] - p1[1])**2 + (p2[0] - p1[0])**2) |
| 107 | x_difference_o = p2[0] - p1[0] |
| 108 | y_difference_o = p2[1] - p1[1] |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 109 | x_difference = x_difference_o * self.mToPx( |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 110 | self.field.robot.length / 2) / distance |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 111 | y_difference = y_difference_o * self.mToPx( |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 112 | self.field.robot.length / 2) / distance |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 113 | |
| 114 | front_middle = [] |
| 115 | front_middle.append(p1[0] + x_difference) |
| 116 | front_middle.append(p1[1] + y_difference) |
| 117 | |
| 118 | back_middle = [] |
| 119 | back_middle.append(p1[0] - x_difference) |
| 120 | back_middle.append(p1[1] - y_difference) |
| 121 | |
| 122 | slope = [-(1 / x_difference_o) / (1 / y_difference_o)] |
| 123 | angle = np.arctan(slope) |
| 124 | |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 125 | x_difference = np.sin(angle[0]) * self.mToPx( |
| 126 | self.field.robot.width / 2) |
| 127 | y_difference = np.cos(angle[0]) * self.mToPx( |
| 128 | self.field.robot.width / 2) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 129 | |
| 130 | front_1 = [] |
| 131 | front_1.append(front_middle[0] - x_difference) |
| 132 | front_1.append(front_middle[1] - y_difference) |
| 133 | |
| 134 | front_2 = [] |
| 135 | front_2.append(front_middle[0] + x_difference) |
| 136 | front_2.append(front_middle[1] + y_difference) |
| 137 | |
| 138 | back_1 = [] |
| 139 | back_1.append(back_middle[0] - x_difference) |
| 140 | back_1.append(back_middle[1] - y_difference) |
| 141 | |
| 142 | back_2 = [] |
| 143 | back_2.append(back_middle[0] + x_difference) |
| 144 | back_2.append(back_middle[1] + y_difference) |
| 145 | |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 146 | x_difference = x_difference_o * self.mToPx( |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 147 | self.field.robot.length / 2 + ROBOT_SIDE_TO_BALL_CENTER) / distance |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 148 | y_difference = y_difference_o * self.mToPx( |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 149 | self.field.robot.length / 2 + ROBOT_SIDE_TO_BALL_CENTER) / distance |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 150 | |
| 151 | #Calculate Ball |
| 152 | ball_center = [] |
| 153 | ball_center.append(p1[0] + x_difference) |
| 154 | ball_center.append(p1[1] + y_difference) |
| 155 | |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 156 | x_difference = x_difference_o * self.mToPx( |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 157 | self.field.robot.length / 2 + ROBOT_SIDE_TO_HATCH_PANEL) / distance |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 158 | y_difference = y_difference_o * self.mToPx( |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 159 | self.field.robot.length / 2 + ROBOT_SIDE_TO_HATCH_PANEL) / distance |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 160 | |
| 161 | #Calculate Panel |
| 162 | panel_center = [] |
| 163 | panel_center.append(p1[0] + x_difference) |
| 164 | panel_center.append(p1[1] + y_difference) |
| 165 | |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 166 | x_difference = np.sin(angle[0]) * self.mToPx(HATCH_PANEL_WIDTH / 2) |
| 167 | y_difference = np.cos(angle[0]) * self.mToPx(HATCH_PANEL_WIDTH / 2) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 168 | |
| 169 | panel_1 = [] |
| 170 | panel_1.append(panel_center[0] + x_difference) |
| 171 | panel_1.append(panel_center[1] + y_difference) |
| 172 | |
| 173 | panel_2 = [] |
| 174 | panel_2.append(panel_center[0] - x_difference) |
| 175 | panel_2.append(panel_center[1] - y_difference) |
| 176 | |
| 177 | #Draw Robot |
| 178 | cr.move_to(front_1[0], front_1[1]) |
| 179 | cr.line_to(back_1[0], back_1[1]) |
| 180 | cr.line_to(back_2[0], back_2[1]) |
| 181 | cr.line_to(front_2[0], front_2[1]) |
| 182 | cr.line_to(front_1[0], front_1[1]) |
| 183 | |
| 184 | cr.stroke() |
| 185 | |
| 186 | #Draw Ball |
| 187 | set_color(cr, palette["ORANGE"], 0.5) |
| 188 | cr.move_to(back_middle[0], back_middle[1]) |
| 189 | cr.line_to(ball_center[0], ball_center[1]) |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 190 | cr.arc(ball_center[0], ball_center[1], self.mToPx(BALL_RADIUS), 0, |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 191 | 2 * np.pi) |
| 192 | cr.stroke() |
| 193 | |
| 194 | #Draw Panel |
| 195 | set_color(cr, palette["YELLOW"], 0.5) |
| 196 | cr.move_to(panel_1[0], panel_1[1]) |
| 197 | cr.line_to(panel_2[0], panel_2[1]) |
| 198 | |
| 199 | cr.stroke() |
| 200 | cr.set_source_rgba(0, 0, 0, 1) |
| 201 | |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 202 | def do_draw(self, cr): # main |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 203 | cr.set_matrix(self.transform.multiply(cr.get_matrix())) |
| 204 | |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 205 | cr.save() |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 206 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 207 | set_color(cr, palette["BLACK"]) |
Ravago Jones | 5f787df | 2021-01-23 16:26:27 -0800 | [diff] [blame] | 208 | |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 209 | cr.set_line_width(1.0) |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 210 | cr.rectangle(0, 0, self.mToPx(self.field.width), |
| 211 | self.mToPx(self.field.length)) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 212 | cr.set_line_join(cairo.LINE_JOIN_ROUND) |
| 213 | cr.stroke() |
Ravago Jones | 5f787df | 2021-01-23 16:26:27 -0800 | [diff] [blame] | 214 | |
Ravago Jones | c26b916 | 2021-06-30 20:12:48 -0700 | [diff] [blame] | 215 | if self.field_png: |
| 216 | cr.save() |
Ravago Jones | c26b916 | 2021-06-30 20:12:48 -0700 | [diff] [blame] | 217 | cr.scale( |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 218 | self.mToPx(self.field.width) / self.field_png.get_width(), |
| 219 | self.mToPx(self.field.length) / self.field_png.get_height(), |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 220 | ) |
Ravago Jones | c26b916 | 2021-06-30 20:12:48 -0700 | [diff] [blame] | 221 | cr.set_source_surface(self.field_png) |
| 222 | cr.paint() |
| 223 | cr.restore() |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 224 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 225 | # update everything |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 226 | |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 227 | cr.set_line_width(2.0) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 228 | if self.mode == Mode.kPlacing or self.mode == Mode.kViewing: |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 229 | set_color(cr, palette["BLACK"]) |
Ravago Jones | 36c92f0 | 2021-07-24 16:35:33 -0700 | [diff] [blame] | 230 | for i, point in enumerate(self.points.getPoints()): |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 231 | draw_px_x(cr, self.mToPx(point[0]), self.mToPx(point[1]), 10) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 232 | set_color(cr, palette["WHITE"]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 233 | elif self.mode == Mode.kEditing: |
| 234 | set_color(cr, palette["BLACK"]) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 235 | if self.points.getSplines(): |
| 236 | self.draw_splines(cr) |
| 237 | for i, points in enumerate(self.points.getSplines()): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 238 | |
Ravago Jones | 36c92f0 | 2021-07-24 16:35:33 -0700 | [diff] [blame] | 239 | points = [ |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 240 | np.array([self.mToPx(x), self.mToPx(y)]) |
Ravago Jones | 36c92f0 | 2021-07-24 16:35:33 -0700 | [diff] [blame] | 241 | for (x, y) in points |
| 242 | ] |
| 243 | draw_control_points(cr, points) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 244 | |
Ravago Jones | 36c92f0 | 2021-07-24 16:35:33 -0700 | [diff] [blame] | 245 | p0, p1, p2, p3, p4, p5 = points |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 246 | first_tangent = p0 + 2.0 * (p1 - p0) |
| 247 | second_tangent = p5 + 2.0 * (p4 - p5) |
| 248 | cr.set_source_rgb(0, 0.5, 0) |
| 249 | cr.move_to(p0[0], p0[1]) |
| 250 | cr.set_line_width(1.0) |
| 251 | cr.line_to(first_tangent[0], first_tangent[1]) |
| 252 | cr.move_to(first_tangent[0], first_tangent[1]) |
| 253 | cr.line_to(p2[0], p2[1]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 254 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 255 | cr.move_to(p5[0], p5[1]) |
| 256 | cr.line_to(second_tangent[0], second_tangent[1]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 257 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 258 | cr.move_to(second_tangent[0], second_tangent[1]) |
| 259 | cr.line_to(p3[0], p3[1]) |
| 260 | |
| 261 | cr.stroke() |
| 262 | cr.set_line_width(2.0) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 263 | set_color(cr, palette["WHITE"]) |
| 264 | |
| 265 | cr.paint_with_alpha(0.2) |
| 266 | |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 267 | draw_px_cross(cr, self.mousex, self.mousey, 10) |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 268 | cr.restore() |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 269 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 270 | def draw_splines(self, cr): |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 271 | for i, spline in enumerate(self.points.getLibsplines()): |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 272 | for k in np.linspace(0.01, 1, 100): |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 273 | cr.move_to( |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 274 | self.mToPx(spline.Point(k - 0.01)[0]), |
| 275 | self.mToPx(spline.Point(k - 0.01)[1])) |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 276 | cr.line_to( |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 277 | self.mToPx(spline.Point(k)[0]), |
| 278 | self.mToPx(spline.Point(k)[1])) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 279 | cr.stroke() |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 280 | if i == 0: |
| 281 | self.draw_robot_at_point(cr, 0.00, 0.01, spline) |
| 282 | self.draw_robot_at_point(cr, 1, 0.01, spline) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 283 | |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 284 | def export_json(self, file_name): |
Ravago Jones | 09f5972 | 2021-03-03 21:11:41 -0800 | [diff] [blame] | 285 | self.path_to_export = os.path.join( |
| 286 | self.module_path, # position of the python |
| 287 | "../../..", # root of the repository |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 288 | get_json_folder(self.field), # path from the root |
Ravago Jones | 09f5972 | 2021-03-03 21:11:41 -0800 | [diff] [blame] | 289 | file_name # selected file |
| 290 | ) |
Ravago Jones | 3b92afa | 2021-02-05 14:27:32 -0800 | [diff] [blame] | 291 | |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 292 | # Will export to json file |
| 293 | multi_spline = self.points.toMultiSpline() |
| 294 | print(multi_spline) |
| 295 | with open(self.path_to_export, mode='w') as points_file: |
| 296 | json.dump(multi_spline, points_file) |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 297 | |
| 298 | def import_json(self, file_name): |
Ravago Jones | 09f5972 | 2021-03-03 21:11:41 -0800 | [diff] [blame] | 299 | self.path_to_export = os.path.join( |
| 300 | self.module_path, # position of the python |
| 301 | "../../..", # root of the repository |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 302 | get_json_folder(self.field), # path from the root |
Ravago Jones | 09f5972 | 2021-03-03 21:11:41 -0800 | [diff] [blame] | 303 | file_name # selected file |
| 304 | ) |
| 305 | |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 306 | # import from json file |
| 307 | print("LOADING LOAD FROM " + file_name) # Load takes a few seconds |
| 308 | with open(self.path_to_export) as points_file: |
| 309 | multi_spline = json.load(points_file) |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 310 | |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 311 | # if people messed with the spline json, |
| 312 | # it might not be the right length |
| 313 | # so give them a nice error message |
| 314 | try: # try to salvage as many segments of the spline as possible |
| 315 | self.points.fromMultiSpline(multi_spline) |
| 316 | except IndexError: |
| 317 | # check if they're both 6+5*(k-1) long |
| 318 | expected_length = 6 + 5 * (multi_spline["spline_count"] - 1) |
| 319 | x_len = len(multi_spline["spline_x"]) |
| 320 | y_len = len(multi_spline["spline_x"]) |
| 321 | if x_len is not expected_length: |
| 322 | print( |
| 323 | "Error: spline x values were not the expected length; expected {} got {}" |
| 324 | .format(expected_length, x_len)) |
| 325 | elif y_len is not expected_length: |
| 326 | print( |
| 327 | "Error: spline y values were not the expected length; expected {} got {}" |
| 328 | .format(expected_length, y_len)) |
Ravago Jones | 3b92afa | 2021-02-05 14:27:32 -0800 | [diff] [blame] | 329 | |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 330 | print("SPLINES LOADED") |
| 331 | self.mode = Mode.kEditing |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 332 | self.queue_draw() |
| 333 | self.graph.schedule_recalculate(self.points) |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 334 | |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 335 | def do_key_press_event(self, event): |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 336 | keyval = Gdk.keyval_to_lower(event.keyval) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 337 | |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 338 | # TODO: This should be a button |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 339 | if keyval == Gdk.KEY_p: |
| 340 | self.mode = Mode.kPlacing |
| 341 | # F0 = A1 |
| 342 | # B1 = 2F0 - E0 |
| 343 | # C1= d0 + 4F0 - 4E0 |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 344 | spline_index = len(self.points.getSplines()) - 1 |
| 345 | self.points.resetPoints() |
| 346 | self.points.extrapolate( |
| 347 | self.points.getSplines()[len(self.points.getSplines()) - 1][5], |
| 348 | self.points.getSplines()[len(self.points.getSplines()) - 1][4], |
| 349 | self.points.getSplines()[len(self.points.getSplines()) - 1][3]) |
Ravago Jones | 128fb99 | 2021-07-31 13:56:58 -0700 | [diff] [blame] | 350 | self.queue_draw() |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 351 | |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 352 | def do_button_release_event(self, event): |
| 353 | self.mousex, self.mousey = self.input_transform.transform_point( |
| 354 | event.x, event.y) |
| 355 | if self.mode == Mode.kEditing: |
| 356 | if self.index_of_edit > -1 and self.held_x != self.mousex: |
| 357 | |
| 358 | self.points.setSplines(self.spline_edit, self.index_of_edit, |
| 359 | self.pxToM(self.mousex), |
| 360 | self.pxToM(self.mousey)) |
| 361 | |
| 362 | self.points.splineExtrapolate(self.spline_edit) |
| 363 | |
| 364 | self.points.update_lib_spline() |
| 365 | self.graph.schedule_recalculate(self.points) |
| 366 | |
| 367 | self.index_of_edit = -1 |
| 368 | self.spline_edit = -1 |
| 369 | |
| 370 | def do_button_press_event(self, event): |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 371 | self.mousex, self.mousey = self.input_transform.transform_point( |
| 372 | event.x, event.y) |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 373 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 374 | if self.mode == Mode.kPlacing: |
Ravago Jones | 36c92f0 | 2021-07-24 16:35:33 -0700 | [diff] [blame] | 375 | if self.points.add_point( |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 376 | self.pxToM(self.mousex), self.pxToM(self.mousey)): |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 377 | self.mode = Mode.kEditing |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 378 | self.graph.schedule_recalculate(self.points) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 379 | elif self.mode == Mode.kEditing: |
| 380 | # Now after index_of_edit is not -1, the point is selected, so |
| 381 | # user can click for new point |
Ravago Jones | 128fb99 | 2021-07-31 13:56:58 -0700 | [diff] [blame] | 382 | if self.index_of_edit == -1: |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 383 | # Get clicked point |
| 384 | # Find nearest |
| 385 | # Move nearest to clicked |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 386 | cur_p = [self.pxToM(self.mousex), self.pxToM(self.mousey)] |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 387 | # Get the distance between each for x and y |
| 388 | # Save the index of the point closest |
John Park | 13d3e28 | 2019-01-26 20:16:48 -0800 | [diff] [blame] | 389 | nearest = 1 # Max distance away a the selected point can be in meters |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 390 | index_of_closest = 0 |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 391 | for index_splines, points in enumerate( |
| 392 | self.points.getSplines()): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 393 | for index_points, val in enumerate(points): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 394 | distance = np.sqrt((cur_p[0] - val[0])**2 + |
| 395 | (cur_p[1] - val[1])**2) |
| 396 | if distance < nearest: |
| 397 | nearest = distance |
| 398 | index_of_closest = index_points |
| 399 | print("Nearest: " + str(nearest)) |
| 400 | print("Index: " + str(index_of_closest)) |
| 401 | self.index_of_edit = index_of_closest |
| 402 | self.spline_edit = index_splines |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 403 | self.held_x = self.mousex |
Ravago Jones | 128fb99 | 2021-07-31 13:56:58 -0700 | [diff] [blame] | 404 | self.queue_draw() |
| 405 | |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 406 | def do_motion_notify_event(self, event): |
| 407 | old_x = self.mousex |
| 408 | old_y = self.mousey |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 409 | self.mousex, self.mousey = self.input_transform.transform_point( |
| 410 | event.x, event.y) |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 411 | dif_x = self.mousex - old_x |
| 412 | dif_y = self.mousey - old_y |
| 413 | difs = np.array([self.pxToM(dif_x), self.pxToM(dif_y)]) |
Ravago Jones | 128fb99 | 2021-07-31 13:56:58 -0700 | [diff] [blame] | 414 | |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 415 | if self.mode == Mode.kEditing and self.spline_edit != -1: |
| 416 | self.points.updates_for_mouse_move(self.index_of_edit, |
| 417 | self.spline_edit, |
| 418 | self.pxToM(self.mousex), |
| 419 | self.pxToM(self.mousey), difs) |
Ravago Jones | 128fb99 | 2021-07-31 13:56:58 -0700 | [diff] [blame] | 420 | |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 421 | self.points.update_lib_spline() |
| 422 | self.graph.schedule_recalculate(self.points) |
| 423 | self.queue_draw() |
Ravago Jones | 128fb99 | 2021-07-31 13:56:58 -0700 | [diff] [blame] | 424 | |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 425 | def do_scroll_event(self, event): |
Ravago Jones | 0178120 | 2021-08-01 15:25:11 -0700 | [diff] [blame] | 426 | self.mousex, self.mousey = self.input_transform.transform_point( |
| 427 | event.x, event.y) |
| 428 | |
| 429 | step_size = 20 # px |
| 430 | |
| 431 | if event.direction == Gdk.ScrollDirection.UP: |
| 432 | # zoom out |
| 433 | scale_by = step_size |
| 434 | elif event.direction == Gdk.ScrollDirection.DOWN: |
| 435 | # zoom in |
| 436 | scale_by = -step_size |
| 437 | else: |
| 438 | return |
| 439 | |
| 440 | apparent_width, apparent_height = self.transform.transform_distance( |
| 441 | self.mToPx(FIELD.width), self.mToPx(FIELD.length)) |
| 442 | scale = (apparent_width + scale_by) / apparent_width |
| 443 | |
| 444 | # scale from point in field coordinates |
| 445 | point = self.mousex, self.mousey |
| 446 | |
| 447 | # move the origin to point |
| 448 | self.transform.translate(point[0], point[1]) |
| 449 | |
| 450 | # scale from new origin |
| 451 | self.transform.scale(scale, scale) |
| 452 | |
| 453 | # move back |
| 454 | self.transform.translate(-point[0], -point[1]) |
| 455 | |
| 456 | # snap to the edge when near 1x scaling |
| 457 | if 0.99 < self.transform.xx < 1.01 and -50 < self.transform.x0 < 50: |
| 458 | self.transform.x0 = 0 |
| 459 | self.transform.y0 = 0 |
| 460 | print("snap") |
| 461 | |
| 462 | self.queue_draw() |