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 |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 5 | import copy |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 6 | from color import Color, palette |
| 7 | import random |
| 8 | import gi |
| 9 | import numpy as np |
| 10 | import scipy.spatial.distance |
| 11 | gi.require_version('Gtk', '3.0') |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 12 | gi.require_version('Gdk', '3.0') |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 13 | from gi.repository import Gdk, Gtk, GLib |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 14 | import cairo |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 15 | from libspline import Spline, DistanceSpline, Trajectory |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 16 | import enum |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 17 | import json |
| 18 | from basic_window import * |
| 19 | from constants import * |
| 20 | from drawing_constants import * |
| 21 | from points import Points |
| 22 | from graph import Graph |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 23 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 24 | |
| 25 | class Mode(enum.Enum): |
| 26 | kViewing = 0 |
| 27 | kPlacing = 1 |
| 28 | kEditing = 2 |
| 29 | kExporting = 3 |
| 30 | kImporting = 4 |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 31 | |
| 32 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 33 | class GTK_Widget(BaseWindow): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 34 | """Create a GTK+ widget on which we will draw using Cairo""" |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 35 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 36 | def __init__(self): |
| 37 | super(GTK_Widget, self).__init__() |
| 38 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 39 | self.points = Points() |
| 40 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 41 | # init field drawing |
| 42 | # add default spline for testing purposes |
| 43 | # init editing / viewing modes and pointer location |
| 44 | self.mode = Mode.kPlacing |
| 45 | self.x = 0 |
| 46 | self.y = 0 |
Andrew Runke | 0f945fd | 2019-01-27 21:10:37 -0800 | [diff] [blame] | 47 | module_path = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 48 | self.path_to_export = os.path.join(module_path, |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 49 | 'points_for_pathedit.json') |
| 50 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 51 | # update list of control points |
| 52 | self.point_selected = False |
| 53 | # self.adding_spline = False |
| 54 | self.index_of_selected = -1 |
| 55 | self.new_point = [] |
| 56 | |
| 57 | # For the editing mode |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 58 | 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] | 59 | self.held_x = 0 |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 60 | self.spline_edit = -1 |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 61 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 62 | self.curves = [] |
| 63 | |
| 64 | self.colors = [] |
| 65 | |
| 66 | for c in palette: |
| 67 | self.colors.append(palette[c]) |
| 68 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 69 | self.reinit_extents() |
| 70 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 71 | self.inStart = None |
| 72 | self.inEnd = None |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 73 | self.inValue = None |
| 74 | self.startSet = False |
| 75 | |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 76 | self.module_path = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 77 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 78 | """set extents on images""" |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 79 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 80 | def reinit_extents(self): |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 81 | self.extents_x_min = -1.0 * SCREEN_SIZE |
| 82 | self.extents_x_max = SCREEN_SIZE |
| 83 | self.extents_y_min = -1.0 * SCREEN_SIZE |
| 84 | self.extents_y_max = SCREEN_SIZE |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 85 | |
| 86 | # this needs to be rewritten with numpy, i dont think this ought to have |
| 87 | # SciPy as a dependecy |
| 88 | def get_index_of_nearest_point(self): |
| 89 | cur_p = [[self.x, self.y]] |
| 90 | distances = scipy.spatial.distance.cdist(cur_p, self.all_controls) |
| 91 | |
| 92 | return np.argmin(distances) |
| 93 | |
| 94 | # return the closest point to the loc of the click event |
| 95 | def get_nearest_point(self): |
| 96 | return self.all_controls[self.get_index_of_nearest_point()] |
| 97 | |
John Park | a30a778 | 2019-02-01 18:47:26 -0800 | [diff] [blame] | 98 | def draw_field_elements(self, cr): |
John Park | cf54516 | 2020-02-23 20:07:25 -0800 | [diff] [blame] | 99 | if FIELD == 2019: |
| 100 | draw_HAB(cr) |
| 101 | draw_rockets(cr) |
| 102 | draw_cargo_ship(cr) |
| 103 | elif FIELD == 2020: |
| 104 | set_color(cr, palette["BLACK"]) |
| 105 | markers(cr) |
| 106 | draw_shield_generator(cr) |
| 107 | draw_trench_run(cr) |
| 108 | draw_init_lines(cr) |
| 109 | draw_control_panel(cr) |
John Park | a30a778 | 2019-02-01 18:47:26 -0800 | [diff] [blame] | 110 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 111 | def draw_robot_at_point(self, cr, i, p, spline): |
| 112 | p1 = [mToPx(spline.Point(i)[0]), mToPx(spline.Point(i)[1])] |
| 113 | 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] | 114 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 115 | #Calculate Robot |
| 116 | distance = np.sqrt((p2[1] - p1[1])**2 + (p2[0] - p1[0])**2) |
| 117 | x_difference_o = p2[0] - p1[0] |
| 118 | y_difference_o = p2[1] - p1[1] |
| 119 | x_difference = x_difference_o * mToPx(LENGTH_OF_ROBOT / 2) / distance |
| 120 | y_difference = y_difference_o * mToPx(LENGTH_OF_ROBOT / 2) / distance |
| 121 | |
| 122 | front_middle = [] |
| 123 | front_middle.append(p1[0] + x_difference) |
| 124 | front_middle.append(p1[1] + y_difference) |
| 125 | |
| 126 | back_middle = [] |
| 127 | back_middle.append(p1[0] - x_difference) |
| 128 | back_middle.append(p1[1] - y_difference) |
| 129 | |
| 130 | slope = [-(1 / x_difference_o) / (1 / y_difference_o)] |
| 131 | angle = np.arctan(slope) |
| 132 | |
| 133 | x_difference = np.sin(angle[0]) * mToPx(WIDTH_OF_ROBOT / 2) |
| 134 | y_difference = np.cos(angle[0]) * mToPx(WIDTH_OF_ROBOT / 2) |
| 135 | |
| 136 | front_1 = [] |
| 137 | front_1.append(front_middle[0] - x_difference) |
| 138 | front_1.append(front_middle[1] - y_difference) |
| 139 | |
| 140 | front_2 = [] |
| 141 | front_2.append(front_middle[0] + x_difference) |
| 142 | front_2.append(front_middle[1] + y_difference) |
| 143 | |
| 144 | back_1 = [] |
| 145 | back_1.append(back_middle[0] - x_difference) |
| 146 | back_1.append(back_middle[1] - y_difference) |
| 147 | |
| 148 | back_2 = [] |
| 149 | back_2.append(back_middle[0] + x_difference) |
| 150 | back_2.append(back_middle[1] + y_difference) |
| 151 | |
| 152 | x_difference = x_difference_o * mToPx( |
| 153 | LENGTH_OF_ROBOT / 2 + ROBOT_SIDE_TO_BALL_CENTER) / distance |
| 154 | y_difference = y_difference_o * mToPx( |
| 155 | LENGTH_OF_ROBOT / 2 + ROBOT_SIDE_TO_BALL_CENTER) / distance |
| 156 | |
| 157 | #Calculate Ball |
| 158 | ball_center = [] |
| 159 | ball_center.append(p1[0] + x_difference) |
| 160 | ball_center.append(p1[1] + y_difference) |
| 161 | |
| 162 | x_difference = x_difference_o * mToPx( |
| 163 | LENGTH_OF_ROBOT / 2 + ROBOT_SIDE_TO_HATCH_PANEL) / distance |
| 164 | y_difference = y_difference_o * mToPx( |
| 165 | LENGTH_OF_ROBOT / 2 + ROBOT_SIDE_TO_HATCH_PANEL) / distance |
| 166 | |
| 167 | #Calculate Panel |
| 168 | panel_center = [] |
| 169 | panel_center.append(p1[0] + x_difference) |
| 170 | panel_center.append(p1[1] + y_difference) |
| 171 | |
| 172 | x_difference = np.sin(angle[0]) * mToPx(HATCH_PANEL_WIDTH / 2) |
| 173 | y_difference = np.cos(angle[0]) * mToPx(HATCH_PANEL_WIDTH / 2) |
| 174 | |
| 175 | panel_1 = [] |
| 176 | panel_1.append(panel_center[0] + x_difference) |
| 177 | panel_1.append(panel_center[1] + y_difference) |
| 178 | |
| 179 | panel_2 = [] |
| 180 | panel_2.append(panel_center[0] - x_difference) |
| 181 | panel_2.append(panel_center[1] - y_difference) |
| 182 | |
| 183 | #Draw Robot |
| 184 | cr.move_to(front_1[0], front_1[1]) |
| 185 | cr.line_to(back_1[0], back_1[1]) |
| 186 | cr.line_to(back_2[0], back_2[1]) |
| 187 | cr.line_to(front_2[0], front_2[1]) |
| 188 | cr.line_to(front_1[0], front_1[1]) |
| 189 | |
| 190 | cr.stroke() |
| 191 | |
| 192 | #Draw Ball |
| 193 | set_color(cr, palette["ORANGE"], 0.5) |
| 194 | cr.move_to(back_middle[0], back_middle[1]) |
| 195 | cr.line_to(ball_center[0], ball_center[1]) |
| 196 | cr.arc(ball_center[0], ball_center[1], mToPx(BALL_RADIUS), 0, |
| 197 | 2 * np.pi) |
| 198 | cr.stroke() |
| 199 | |
| 200 | #Draw Panel |
| 201 | set_color(cr, palette["YELLOW"], 0.5) |
| 202 | cr.move_to(panel_1[0], panel_1[1]) |
| 203 | cr.line_to(panel_2[0], panel_2[1]) |
| 204 | |
| 205 | cr.stroke() |
| 206 | cr.set_source_rgba(0, 0, 0, 1) |
| 207 | |
| 208 | def handle_draw(self, cr): # main |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 209 | # Fill the background color of the window with grey |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 210 | set_color(cr, palette["WHITE"]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 211 | cr.paint() |
| 212 | |
| 213 | # Draw a extents rectangle |
| 214 | set_color(cr, palette["WHITE"]) |
| 215 | cr.rectangle(self.extents_x_min, self.extents_y_min, |
| 216 | (self.extents_x_max - self.extents_x_min), |
| 217 | self.extents_y_max - self.extents_y_min) |
| 218 | cr.fill() |
| 219 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 220 | cr.move_to(0, 50) |
| 221 | cr.show_text('Press "e" to export') |
| 222 | cr.show_text('Press "i" to import') |
| 223 | |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 224 | cr.save() |
| 225 | cr.translate(mToPx(WIDTH_OF_FIELD_IN_METERS) / 2.0, 0.0) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 226 | set_color(cr, palette["BLACK"]) |
John Park | cf54516 | 2020-02-23 20:07:25 -0800 | [diff] [blame] | 227 | if FIELD == 2020: |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 228 | cr.rectangle(-mToPx(WIDTH_OF_FIELD_IN_METERS) / 2.0, |
| 229 | -mToPx(LENGTH_OF_FIELD_IN_METERS) / 2.0, |
| 230 | mToPx(WIDTH_OF_FIELD_IN_METERS), |
| 231 | mToPx(LENGTH_OF_FIELD_IN_METERS)) |
John Park | cf54516 | 2020-02-23 20:07:25 -0800 | [diff] [blame] | 232 | else: |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 233 | cr.rectangle(0, -SCREEN_SIZE / 2, SCREEN_SIZE, SCREEN_SIZE) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 234 | cr.set_line_join(cairo.LINE_JOIN_ROUND) |
| 235 | cr.stroke() |
John Park | a30a778 | 2019-02-01 18:47:26 -0800 | [diff] [blame] | 236 | self.draw_field_elements(cr) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 237 | y = 0 |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 238 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 239 | # update everything |
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 | if self.mode == Mode.kPlacing or self.mode == Mode.kViewing: |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 242 | set_color(cr, palette["BLACK"]) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 243 | cr.move_to(-SCREEN_SIZE, 170) |
| 244 | plotPoints = self.points.getPoints() |
| 245 | if plotPoints: |
| 246 | for i, point in enumerate(plotPoints): |
John Park | 13d3e28 | 2019-01-26 20:16:48 -0800 | [diff] [blame] | 247 | draw_px_x(cr, mToPx(point[0]), mToPx(point[1]), 10) |
| 248 | cr.move_to(mToPx(point[0]), mToPx(point[1]) - 15) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 249 | display_text(cr, str(i), 0.5, 0.5, 2, 2) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 250 | set_color(cr, palette["WHITE"]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 251 | |
| 252 | elif self.mode == Mode.kEditing: |
| 253 | set_color(cr, palette["BLACK"]) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 254 | cr.move_to(-SCREEN_SIZE, 170) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 255 | display_text(cr, "EDITING", 1, 1, 1, 1) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 256 | if self.points.getSplines(): |
| 257 | self.draw_splines(cr) |
| 258 | for i, points in enumerate(self.points.getSplines()): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 259 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 260 | p0 = np.array([mToPx(points[0][0]), mToPx(points[0][1])]) |
| 261 | p1 = np.array([mToPx(points[1][0]), mToPx(points[1][1])]) |
| 262 | p2 = np.array([mToPx(points[2][0]), mToPx(points[2][1])]) |
| 263 | p3 = np.array([mToPx(points[3][0]), mToPx(points[3][1])]) |
| 264 | p4 = np.array([mToPx(points[4][0]), mToPx(points[4][1])]) |
| 265 | p5 = np.array([mToPx(points[5][0]), mToPx(points[5][1])]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 266 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 267 | draw_control_points(cr, [p0, p1, p2, p3, p4, p5]) |
| 268 | first_tangent = p0 + 2.0 * (p1 - p0) |
| 269 | second_tangent = p5 + 2.0 * (p4 - p5) |
| 270 | cr.set_source_rgb(0, 0.5, 0) |
| 271 | cr.move_to(p0[0], p0[1]) |
| 272 | cr.set_line_width(1.0) |
| 273 | cr.line_to(first_tangent[0], first_tangent[1]) |
| 274 | cr.move_to(first_tangent[0], first_tangent[1]) |
| 275 | cr.line_to(p2[0], p2[1]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 276 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 277 | cr.move_to(p5[0], p5[1]) |
| 278 | cr.line_to(second_tangent[0], second_tangent[1]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 279 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 280 | cr.move_to(second_tangent[0], second_tangent[1]) |
| 281 | cr.line_to(p3[0], p3[1]) |
| 282 | |
| 283 | cr.stroke() |
| 284 | cr.set_line_width(2.0) |
| 285 | self.points.update_lib_spline() |
| 286 | set_color(cr, palette["WHITE"]) |
| 287 | |
| 288 | cr.paint_with_alpha(0.2) |
| 289 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 290 | draw_px_cross(cr, self.x, self.y, 10) |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 291 | cr.restore() |
| 292 | mygraph = Graph(cr, self.points) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 293 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 294 | def draw_splines(self, cr): |
| 295 | holder_spline = [] |
| 296 | for i, points in enumerate(self.points.getSplines()): |
| 297 | array = np.zeros(shape=(6, 2), dtype=float) |
| 298 | for j, point in enumerate(points): |
| 299 | array[j, 0] = point[0] |
| 300 | array[j, 1] = point[1] |
| 301 | spline = Spline(np.ascontiguousarray(np.transpose(array))) |
| 302 | for k in np.linspace(0.01, 1, 100): |
Ravago Jones | 26f7ad0 | 2021-02-05 15:45:59 -0800 | [diff] [blame] | 303 | cr.move_to( |
| 304 | mToPx(spline.Point(k - 0.01)[0]), |
| 305 | mToPx(spline.Point(k - 0.01)[1])) |
| 306 | cr.line_to( |
| 307 | mToPx(spline.Point(k)[0]), mToPx(spline.Point(k)[1])) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 308 | cr.stroke() |
| 309 | holding = [ |
| 310 | spline.Point(k - 0.01)[0], |
| 311 | spline.Point(k - 0.01)[1] |
| 312 | ] |
| 313 | holder_spline.append(holding) |
| 314 | if i == 0: |
| 315 | self.draw_robot_at_point(cr, 0.00, 0.01, spline) |
| 316 | self.draw_robot_at_point(cr, 1, 0.01, spline) |
| 317 | self.curves.append(holder_spline) |
| 318 | |
| 319 | def mouse_move(self, event): |
| 320 | old_x = self.x |
| 321 | old_y = self.y |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 322 | self.x = event.x - mToPx(WIDTH_OF_FIELD_IN_METERS / 2.0) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 323 | self.y = event.y |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 324 | dif_x = self.x - old_x |
| 325 | dif_y = self.y - old_y |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 326 | difs = np.array([pxToM(dif_x), pxToM(dif_y)]) |
| 327 | |
| 328 | if self.mode == Mode.kEditing: |
| 329 | self.spline_edit = self.points.updates_for_mouse_move( |
| 330 | self.index_of_edit, self.spline_edit, self.x, self.y, difs) |
| 331 | |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 332 | def export_json(self, file_name): |
| 333 | self.path_to_export = os.path.join(self.module_path, |
| 334 | "spline_jsons/" + file_name) |
| 335 | if file_name[-5:] != ".json": |
| 336 | print("Error: Filename doesn't end in .json") |
| 337 | else: |
| 338 | # Will export to json file |
| 339 | self.mode = Mode.kEditing |
| 340 | exportList = [l.tolist() for l in self.points.getSplines()] |
| 341 | with open(self.path_to_export, mode='w') as points_file: |
| 342 | json.dump(exportList, points_file) |
| 343 | |
| 344 | def import_json(self, file_name): |
| 345 | self.path_to_export = os.path.join(self.module_path, |
| 346 | "spline_jsons/" + file_name) |
| 347 | if file_name[-5:] != ".json": |
| 348 | print("Error: Filename doesn't end in .json") |
| 349 | else: |
| 350 | # import from json file |
| 351 | self.mode = Mode.kEditing |
| 352 | self.points.resetPoints() |
| 353 | self.points.resetSplines() |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 354 | print("LOADING LOAD FROM " + file_name) # Load takes a few seconds |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 355 | with open(self.path_to_export) as points_file: |
| 356 | self.points.setUpSplines(json.load(points_file)) |
| 357 | |
| 358 | self.points.update_lib_spline() |
| 359 | print("SPLINES LOADED") |
| 360 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 361 | def do_key_press(self, event, file_name): |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 362 | keyval = Gdk.keyval_to_lower(event.keyval) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 363 | if keyval == Gdk.KEY_q: |
| 364 | print("Found q key and exiting.") |
| 365 | quit_main_loop() |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 366 | if keyval == Gdk.KEY_e: |
| 367 | export_json(file_name) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 368 | |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 369 | if keyval == Gdk.KEY_i: |
| 370 | import_json(file_name) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 371 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 372 | if keyval == Gdk.KEY_p: |
| 373 | self.mode = Mode.kPlacing |
| 374 | # F0 = A1 |
| 375 | # B1 = 2F0 - E0 |
| 376 | # C1= d0 + 4F0 - 4E0 |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 377 | spline_index = len(self.points.getSplines()) - 1 |
| 378 | self.points.resetPoints() |
| 379 | self.points.extrapolate( |
| 380 | self.points.getSplines()[len(self.points.getSplines()) - 1][5], |
| 381 | self.points.getSplines()[len(self.points.getSplines()) - 1][4], |
| 382 | self.points.getSplines()[len(self.points.getSplines()) - 1][3]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 383 | |
| 384 | def button_press_action(self): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 385 | if self.mode == Mode.kPlacing: |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 386 | if self.points.add_point(self.x, self.y): |
| 387 | self.mode = Mode.kEditing |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 388 | elif self.mode == Mode.kEditing: |
| 389 | # Now after index_of_edit is not -1, the point is selected, so |
| 390 | # user can click for new point |
| 391 | if self.index_of_edit > -1 and self.held_x != self.x: |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 392 | self.points.setSplines(self.spline_edit, self.index_of_edit, |
| 393 | pxToM(self.x), pxToM(self.y)) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 394 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 395 | self.spline_edit = self.points.splineExtrapolate( |
| 396 | self.spline_edit) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 397 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 398 | self.index_of_edit = -1 |
| 399 | self.spline_edit = -1 |
| 400 | else: |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 401 | # Get clicked point |
| 402 | # Find nearest |
| 403 | # Move nearest to clicked |
John Park | 13d3e28 | 2019-01-26 20:16:48 -0800 | [diff] [blame] | 404 | cur_p = [pxToM(self.x), pxToM(self.y)] |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 405 | # Get the distance between each for x and y |
| 406 | # Save the index of the point closest |
John Park | 13d3e28 | 2019-01-26 20:16:48 -0800 | [diff] [blame] | 407 | 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] | 408 | index_of_closest = 0 |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 409 | for index_splines, points in enumerate( |
| 410 | self.points.getSplines()): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 411 | for index_points, val in enumerate(points): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 412 | distance = np.sqrt((cur_p[0] - val[0])**2 + |
| 413 | (cur_p[1] - val[1])**2) |
| 414 | if distance < nearest: |
| 415 | nearest = distance |
| 416 | index_of_closest = index_points |
| 417 | print("Nearest: " + str(nearest)) |
| 418 | print("Index: " + str(index_of_closest)) |
| 419 | self.index_of_edit = index_of_closest |
| 420 | self.spline_edit = index_splines |
| 421 | self.held_x = self.x |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 422 | |
| 423 | def do_button_press(self, event): |
John Park | 13d3e28 | 2019-01-26 20:16:48 -0800 | [diff] [blame] | 424 | # Be consistent with the scaling in the drawing_area |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 425 | self.x = event.x * 2 - mToPx(WIDTH_OF_FIELD_IN_METERS / 2.0) |
Andrew Runke | a9c8de5 | 2019-01-26 19:54:29 -0800 | [diff] [blame] | 426 | self.y = event.y * 2 |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 427 | self.button_press_action() |