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