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