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