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') |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 10 | from gi.repository import Gdk, Gtk, GLib |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 11 | import cairo |
Ravago Jones | b170ed3 | 2022-06-01 21:16:15 -0700 | [diff] [blame] | 12 | from libspline import Spline, DistanceSpline |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 13 | import enum |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 14 | import json |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 15 | import copy |
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 |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 20 | from multispline import Multispline, ControlPointIndex |
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""" |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 32 | def __init__(self): |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 33 | super(FieldWidget, self).__init__() |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 34 | self.set_field(FIELD) |
Ravago Jones | 8da89c4 | 2022-07-17 19:34:06 -0700 | [diff] [blame] | 35 | self.set_size_request(self.mToPx(self.field.width), |
| 36 | self.mToPx(self.field.length)) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 37 | |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 38 | self.multisplines = [] |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 39 | self.graph = Graph() |
Ravago Jones | 0a1d409 | 2022-06-03 12:47:32 -0700 | [diff] [blame] | 40 | self.graph.cursor_watcher = self |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 41 | self.set_vexpand(True) |
| 42 | self.set_hexpand(True) |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 43 | self.undo_history = [] |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 44 | # init field drawing |
| 45 | # add default spline for testing purposes |
| 46 | # init editing / viewing modes and pointer location |
| 47 | self.mode = Mode.kPlacing |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 48 | self.mousex = 0 |
| 49 | self.mousey = 0 |
| 50 | self.module_path = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 51 | self.path_to_export = os.path.join(self.module_path, |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 52 | 'points_for_pathedit.json') |
| 53 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 54 | # For the editing mode |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 55 | self.control_point_index = None |
| 56 | self.active_multispline_index = -1 |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 57 | |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 58 | self.zoom_transform = cairo.Matrix() |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 59 | |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 60 | self.set_events(Gdk.EventMask.BUTTON_PRESS_MASK |
| 61 | | Gdk.EventMask.BUTTON_PRESS_MASK |
| 62 | | Gdk.EventMask.BUTTON_RELEASE_MASK |
| 63 | | Gdk.EventMask.POINTER_MOTION_MASK |
| 64 | | Gdk.EventMask.SCROLL_MASK) |
| 65 | |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 66 | @property |
| 67 | def active_multispline(self): |
| 68 | """Get the current active multispline or create a new one""" |
| 69 | if not self.multisplines: |
| 70 | self.multisplines.append(Multispline()) |
| 71 | self.active_multispline_index = -1 |
| 72 | |
| 73 | return self.multisplines[self.active_multispline_index] |
| 74 | |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 75 | def set_field(self, field): |
| 76 | self.field = field |
Ravago Jones | c26b916 | 2021-06-30 20:12:48 -0700 | [diff] [blame] | 77 | try: |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 78 | self.field_png = cairo.ImageSurface.create_from_png( |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 79 | "frc971/control_loops/python/field_images/" + |
| 80 | self.field.field_id + ".png") |
Ravago Jones | c26b916 | 2021-06-30 20:12:48 -0700 | [diff] [blame] | 81 | except cairo.Error: |
| 82 | self.field_png = None |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 83 | |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 84 | self.queue_draw() |
Ravago Jones | c26b916 | 2021-06-30 20:12:48 -0700 | [diff] [blame] | 85 | |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 86 | def invert(self, transform): |
| 87 | xx, yx, xy, yy, x0, y0 = transform |
| 88 | matrix = cairo.Matrix(xx, yx, xy, yy, x0, y0) |
| 89 | matrix.invert() |
| 90 | return matrix |
| 91 | |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 92 | # returns the transform from widget space to field space |
| 93 | @property |
| 94 | def input_transform(self): |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 95 | # the transform for input needs to be the opposite of the transform for drawing |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 96 | return self.invert(self.field_transform.multiply(self.zoom_transform)) |
| 97 | |
| 98 | @property |
| 99 | def field_transform(self): |
| 100 | field_transform = cairo.Matrix() |
Ravago Jones | 8da89c4 | 2022-07-17 19:34:06 -0700 | [diff] [blame] | 101 | field_transform.scale(1, -1) # flipped y-axis |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 102 | field_transform.scale(1 / self.pxToM_scale(), 1 / self.pxToM_scale()) |
Ravago Jones | 8da89c4 | 2022-07-17 19:34:06 -0700 | [diff] [blame] | 103 | field_transform.translate(self.field.width / 2, |
| 104 | -1 * self.field.length / 2) |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 105 | return field_transform |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 106 | |
| 107 | # returns the scale from pixels in field space to meters in field space |
| 108 | def pxToM_scale(self): |
| 109 | available_space = self.get_allocation() |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 110 | return np.maximum(self.field.width / available_space.width, |
| 111 | self.field.length / available_space.height) |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 112 | |
| 113 | def pxToM(self, p): |
| 114 | return p * self.pxToM_scale() |
| 115 | |
| 116 | def mToPx(self, m): |
| 117 | return m / self.pxToM_scale() |
| 118 | |
Ravago Jones | b170ed3 | 2022-06-01 21:16:15 -0700 | [diff] [blame] | 119 | def draw_robot_at_point(self, cr, spline, t): |
| 120 | """Draws the robot at a point along a Spline or DistanceSpline""" |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 121 | |
Ravago Jones | b170ed3 | 2022-06-01 21:16:15 -0700 | [diff] [blame] | 122 | # we accept both Spline and DistanceSpline |
| 123 | if type(spline) is Spline: |
| 124 | point = spline.Point(t) |
| 125 | theta = spline.Theta(t) |
| 126 | elif type(spline) is DistanceSpline: |
| 127 | point = spline.XY(t) |
| 128 | theta = spline.Theta(t) |
| 129 | else: |
| 130 | raise TypeError( |
| 131 | f"expected Spline or DistanceSpline (got {type(spline)})") |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 132 | |
Ravago Jones | b170ed3 | 2022-06-01 21:16:15 -0700 | [diff] [blame] | 133 | # Transform so that +y is forward along the spline |
| 134 | transform = cairo.Matrix() |
| 135 | transform.translate(*point) |
| 136 | transform.rotate(theta - np.pi / 2) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 137 | |
Ravago Jones | b170ed3 | 2022-06-01 21:16:15 -0700 | [diff] [blame] | 138 | cr.save() |
| 139 | cr.set_matrix(transform.multiply(cr.get_matrix())) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 140 | |
Ravago Jones | b170ed3 | 2022-06-01 21:16:15 -0700 | [diff] [blame] | 141 | # Draw Robot |
| 142 | set_color(cr, palette["BLACK"]) |
| 143 | cr.rectangle(-self.field.robot.width / 2, -self.field.robot.length / 2, |
| 144 | self.field.robot.width, self.field.robot.length) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 145 | cr.stroke() |
| 146 | |
| 147 | #Draw Ball |
| 148 | set_color(cr, palette["ORANGE"], 0.5) |
Ravago Jones | b170ed3 | 2022-06-01 21:16:15 -0700 | [diff] [blame] | 149 | cr.arc(0, self.field.robot.length / 2 + BALL_RADIUS, BALL_RADIUS, 0, |
| 150 | 2 * np.pi) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 151 | cr.stroke() |
| 152 | |
Ravago Jones | b170ed3 | 2022-06-01 21:16:15 -0700 | [diff] [blame] | 153 | # undo the transform |
| 154 | cr.restore() |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 155 | |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 156 | def do_draw(self, cr): # main |
Ravago Jones | 8da89c4 | 2022-07-17 19:34:06 -0700 | [diff] [blame] | 157 | cr.set_matrix( |
| 158 | self.field_transform.multiply(self.zoom_transform).multiply( |
| 159 | cr.get_matrix())) |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 160 | |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 161 | cr.save() |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 162 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 163 | set_color(cr, palette["BLACK"]) |
Ravago Jones | 5f787df | 2021-01-23 16:26:27 -0800 | [diff] [blame] | 164 | |
Henry Speiser | 51be5c6 | 2022-03-13 23:14:36 -0700 | [diff] [blame] | 165 | cr.set_line_width(self.pxToM(1)) |
Ravago Jones | 8da89c4 | 2022-07-17 19:34:06 -0700 | [diff] [blame] | 166 | cr.rectangle(-0.5 * self.field.width, -0.5 * self.field.length, |
| 167 | self.field.width, self.field.length) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 168 | cr.set_line_join(cairo.LINE_JOIN_ROUND) |
| 169 | cr.stroke() |
Ravago Jones | 5f787df | 2021-01-23 16:26:27 -0800 | [diff] [blame] | 170 | |
Ravago Jones | c26b916 | 2021-06-30 20:12:48 -0700 | [diff] [blame] | 171 | if self.field_png: |
| 172 | cr.save() |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 173 | cr.translate(-0.5 * self.field.width, 0.5 * self.field.length) |
Ravago Jones | c26b916 | 2021-06-30 20:12:48 -0700 | [diff] [blame] | 174 | cr.scale( |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 175 | self.field.width / self.field_png.get_width(), |
| 176 | -self.field.length / self.field_png.get_height(), |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 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 | |
Henry Speiser | 51be5c6 | 2022-03-13 23:14:36 -0700 | [diff] [blame] | 184 | cr.set_line_width(self.pxToM(1)) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 185 | if self.mode == Mode.kPlacing or self.mode == Mode.kViewing: |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 186 | set_color(cr, palette["BLACK"]) |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 187 | for multispline in self.multisplines: |
| 188 | for i, point in enumerate(multispline.staged_points): |
| 189 | draw_px_x(cr, point[0], point[1], self.pxToM(2)) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 190 | set_color(cr, palette["WHITE"]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 191 | elif self.mode == Mode.kEditing: |
| 192 | set_color(cr, palette["BLACK"]) |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 193 | if len(self.multisplines) != 0 and self.multisplines[0].getSplines( |
| 194 | ): |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 195 | self.draw_splines(cr) |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 196 | |
| 197 | for multispline in self.multisplines: |
| 198 | for i, points in enumerate(multispline.getSplines()): |
Ravago Jones | 8da89c4 | 2022-07-17 19:34:06 -0700 | [diff] [blame] | 199 | points = [np.array([x, y]) for (x, y) in points] |
| 200 | draw_control_points(cr, |
| 201 | points, |
| 202 | width=self.pxToM(5), |
| 203 | radius=self.pxToM(2)) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 204 | |
Ravago Jones | 36c92f0 | 2021-07-24 16:35:33 -0700 | [diff] [blame] | 205 | p0, p1, p2, p3, p4, p5 = points |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 206 | first_tangent = p0 + 2.0 * (p1 - p0) |
| 207 | second_tangent = p5 + 2.0 * (p4 - p5) |
| 208 | cr.set_source_rgb(0, 0.5, 0) |
Ravago Jones | b170ed3 | 2022-06-01 21:16:15 -0700 | [diff] [blame] | 209 | cr.move_to(*p0) |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 210 | cr.set_line_width(self.pxToM(1.0)) |
Ravago Jones | b170ed3 | 2022-06-01 21:16:15 -0700 | [diff] [blame] | 211 | cr.line_to(*first_tangent) |
| 212 | cr.move_to(*first_tangent) |
| 213 | cr.line_to(*p2) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 214 | |
Ravago Jones | b170ed3 | 2022-06-01 21:16:15 -0700 | [diff] [blame] | 215 | cr.move_to(*p5) |
| 216 | cr.line_to(*second_tangent) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 217 | |
Ravago Jones | b170ed3 | 2022-06-01 21:16:15 -0700 | [diff] [blame] | 218 | cr.move_to(*second_tangent) |
| 219 | cr.line_to(*p3) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 220 | |
| 221 | cr.stroke() |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 222 | cr.set_line_width(self.pxToM(2)) |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 223 | set_color(cr, palette["WHITE"]) |
| 224 | |
| 225 | cr.paint_with_alpha(0.2) |
| 226 | |
Henry Speiser | 51be5c6 | 2022-03-13 23:14:36 -0700 | [diff] [blame] | 227 | draw_px_cross(cr, self.mousex, self.mousey, self.pxToM(2)) |
James Kuszmaul | 1c933e0 | 2020-03-07 16:17:51 -0800 | [diff] [blame] | 228 | cr.restore() |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 229 | |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 230 | def draw_splines(self, cr): |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 231 | for multispline in self.multisplines: |
| 232 | for i, spline in enumerate(multispline.getLibsplines()): |
| 233 | # draw lots of really small line segments to |
| 234 | # approximate the shape of the spline |
| 235 | for k in np.linspace(0.02, 1, 200): |
| 236 | cr.move_to(*spline.Point(k - 0.008)) |
| 237 | cr.line_to(*spline.Point(k)) |
| 238 | cr.stroke() |
| 239 | |
| 240 | if i == 0: |
| 241 | self.draw_robot_at_point(cr, spline, 0) |
| 242 | self.draw_robot_at_point(cr, spline, 1) |
Ravago Jones | b170ed3 | 2022-06-01 21:16:15 -0700 | [diff] [blame] | 243 | |
| 244 | mouse = np.array((self.mousex, self.mousey)) |
| 245 | |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 246 | multispline, result = Multispline.nearest_distance( |
| 247 | self.multisplines, mouse) |
Ravago Jones | b170ed3 | 2022-06-01 21:16:15 -0700 | [diff] [blame] | 248 | |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 249 | # if the mouse is close enough, draw the robot |
Ravago Jones | 291f559 | 2022-07-07 20:40:37 -0700 | [diff] [blame] | 250 | if result is not None and result.fun < 2: |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 251 | distance_spline = DistanceSpline(multispline.getLibsplines()) |
| 252 | x = result.x[0] |
| 253 | |
| 254 | # draw the robot to show its width |
Ravago Jones | 0a1d409 | 2022-06-03 12:47:32 -0700 | [diff] [blame] | 255 | self.draw_robot_at_point(cr, distance_spline, x) |
| 256 | |
Ravago Jones | 291f559 | 2022-07-07 20:40:37 -0700 | [diff] [blame] | 257 | multispline_index = self.multisplines.index(multispline) |
| 258 | self.graph.place_cursor(multispline_index, distance=result.x[0]) |
| 259 | elif self.graph.cursor is not None: |
| 260 | multispline_index, x = self.graph.find_cursor() |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 261 | distance_spline = DistanceSpline( |
Ravago Jones | 291f559 | 2022-07-07 20:40:37 -0700 | [diff] [blame] | 262 | self.multisplines[multispline_index].getLibsplines()) |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 263 | |
| 264 | self.draw_robot_at_point(cr, distance_spline, x) |
| 265 | |
| 266 | # clear the cursor each draw so it doesn't persist |
Ravago Jones | 0a1d409 | 2022-06-03 12:47:32 -0700 | [diff] [blame] | 267 | # after you move off the spline |
| 268 | self.graph.cursor = None |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 269 | |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 270 | def export_json(self, file_name): |
Ravago Jones | 09f5972 | 2021-03-03 21:11:41 -0800 | [diff] [blame] | 271 | self.path_to_export = os.path.join( |
| 272 | self.module_path, # position of the python |
| 273 | "../../..", # root of the repository |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 274 | get_json_folder(self.field), # path from the root |
Ravago Jones | 09f5972 | 2021-03-03 21:11:41 -0800 | [diff] [blame] | 275 | file_name # selected file |
| 276 | ) |
Ravago Jones | 3b92afa | 2021-02-05 14:27:32 -0800 | [diff] [blame] | 277 | |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 278 | # Will export to json file |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 279 | multisplines_object = [ |
| 280 | multispline.toJsonObject() for multispline in self.multisplines |
| 281 | ] |
| 282 | print(multisplines_object) |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 283 | with open(self.path_to_export, mode='w') as points_file: |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 284 | json.dump(multisplines_object, points_file) |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 285 | |
| 286 | def import_json(self, file_name): |
Ravago Jones | 09f5972 | 2021-03-03 21:11:41 -0800 | [diff] [blame] | 287 | self.path_to_export = os.path.join( |
| 288 | self.module_path, # position of the python |
| 289 | "../../..", # root of the repository |
Ravago Jones | 086a887 | 2021-08-07 15:49:40 -0700 | [diff] [blame] | 290 | get_json_folder(self.field), # path from the root |
Ravago Jones | 09f5972 | 2021-03-03 21:11:41 -0800 | [diff] [blame] | 291 | file_name # selected file |
| 292 | ) |
| 293 | |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 294 | # import from json file |
| 295 | print("LOADING LOAD FROM " + file_name) # Load takes a few seconds |
| 296 | with open(self.path_to_export) as points_file: |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 297 | multisplines_object = json.load(points_file) |
| 298 | |
| 299 | self.attempt_append_multisplines() |
| 300 | |
| 301 | # TODO: Export multisplines in different files |
| 302 | if type(multisplines_object) is dict: |
| 303 | multisplines_object = [multisplines_object] |
| 304 | else: |
| 305 | self.multisplines = [] |
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 |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 310 | for multispline_object in multisplines_object: |
| 311 | print(multispline_object) |
| 312 | try: # try to salvage as many segments of the spline as possible |
| 313 | self.multisplines.append( |
| 314 | Multispline.fromJsonObject(multispline_object)) |
| 315 | except IndexError: |
| 316 | # check if they're both 6+5*(k-1) long |
| 317 | expected_length = 6 + 5 * (multispline_object["spline_count"] - |
| 318 | 1) |
| 319 | x_len = len(multispline_object["spline_x"]) |
| 320 | y_len = len(multispline_object["spline_x"]) |
| 321 | if x_len is not expected_length: |
| 322 | print( |
| 323 | "Error: spline x values were not the expected length; expected {} got {}" |
| 324 | .format(expected_length, x_len)) |
| 325 | elif y_len is not expected_length: |
| 326 | print( |
| 327 | "Error: spline y values were not the expected length; expected {} got {}" |
| 328 | .format(expected_length, y_len)) |
Ravago Jones | 3b92afa | 2021-02-05 14:27:32 -0800 | [diff] [blame] | 329 | |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 330 | print("SPLINES LOADED") |
| 331 | self.mode = Mode.kEditing |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 332 | self.queue_draw() |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 333 | self.graph.schedule_recalculate(self.multisplines) |
John Park | 909c039 | 2020-03-05 23:56:30 -0800 | [diff] [blame] | 334 | |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 335 | def attempt_append_multisplines(self): |
| 336 | if len(self.undo_history |
| 337 | ) == 0 or self.multisplines != self.undo_history[-1]: |
| 338 | self.undo_history.append(copy.deepcopy(self.multisplines)) |
Ryan Yin | d8be388 | 2021-10-13 20:59:41 -0700 | [diff] [blame] | 339 | |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 340 | def clear(self, should_attempt_append=True): |
Ryan Yin | d8be388 | 2021-10-13 20:59:41 -0700 | [diff] [blame] | 341 | if should_attempt_append: |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 342 | self.attempt_append_multisplines() |
| 343 | self.multisplines = [] |
Ryan Yin | 85f861f | 2021-09-16 17:55:11 -0700 | [diff] [blame] | 344 | #recalulate graph using new points |
| 345 | self.graph.axis.clear() |
| 346 | self.graph.queue_draw() |
| 347 | #allow placing again |
| 348 | self.mode = Mode.kPlacing |
| 349 | #redraw entire graph |
| 350 | self.queue_draw() |
Ryan Yin | d8be388 | 2021-10-13 20:59:41 -0700 | [diff] [blame] | 351 | |
Ryan Yin | d8be388 | 2021-10-13 20:59:41 -0700 | [diff] [blame] | 352 | def undo(self): |
| 353 | try: |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 354 | self.undo_history.pop() |
Ryan Yin | d8be388 | 2021-10-13 20:59:41 -0700 | [diff] [blame] | 355 | except IndexError: |
| 356 | return |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 357 | if len(self.undo_history) == 0: |
| 358 | self.clear(should_attempt_append=False) #clear, don't do anything |
Ryan Yin | d8be388 | 2021-10-13 20:59:41 -0700 | [diff] [blame] | 359 | return |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 360 | if len(self.multisplines) > 0 and not any( |
| 361 | multispline.staged_points |
| 362 | for multispline in self.multisplines): |
Ravago Jones | 8da89c4 | 2022-07-17 19:34:06 -0700 | [diff] [blame] | 363 | self.mode = Mode.kEditing |
Ryan Yin | d8be388 | 2021-10-13 20:59:41 -0700 | [diff] [blame] | 364 | else: |
| 365 | self.mode = Mode.kPlacing |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 366 | self.clear(should_attempt_append=False) |
| 367 | self.multisplines = copy.deepcopy(self.undo_history[-1]) |
Ryan Yin | d8be388 | 2021-10-13 20:59:41 -0700 | [diff] [blame] | 368 | self.queue_draw() |
| 369 | |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 370 | def do_key_press_event(self, event): |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 371 | keyval = Gdk.keyval_to_lower(event.keyval) |
Ryan Yin | d8be388 | 2021-10-13 20:59:41 -0700 | [diff] [blame] | 372 | if keyval == Gdk.KEY_z and event.state & Gdk.ModifierType.CONTROL_MASK: |
| 373 | self.undo() |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 374 | # TODO: This should be a button |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 375 | if keyval == Gdk.KEY_p: |
| 376 | self.mode = Mode.kPlacing |
| 377 | # F0 = A1 |
| 378 | # B1 = 2F0 - E0 |
| 379 | # C1= d0 + 4F0 - 4E0 |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 380 | multispline = self.active_multispline |
| 381 | multispline.extrapolate() |
| 382 | self.queue_draw() |
| 383 | elif keyval == Gdk.KEY_m: |
| 384 | self.multisplines.append(Multispline()) |
| 385 | self.active_spline_index = len(self.multisplines) - 1 |
| 386 | self.mode = Mode.kPlacing |
| 387 | |
| 388 | multispline = self.multisplines[-2] |
| 389 | #multispline.extrapolate() |
Ravago Jones | 128fb99 | 2021-07-31 13:56:58 -0700 | [diff] [blame] | 390 | self.queue_draw() |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 391 | |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 392 | def do_button_release_event(self, event): |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 393 | self.attempt_append_multisplines() |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 394 | self.mousex, self.mousey = self.input_transform.transform_point( |
| 395 | event.x, event.y) |
| 396 | if self.mode == Mode.kEditing: |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 397 | if self.control_point_index != None: |
| 398 | multispline = self.multisplines[ |
| 399 | self.control_point_index.multispline_index] |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 400 | |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 401 | multispline.setControlPoint(self.control_point_index, |
| 402 | self.mousex, self.mousey) |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 403 | |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 404 | multispline.splineExtrapolate( |
| 405 | self.control_point_index.spline_index) |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 406 | |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 407 | multispline.update_lib_spline() |
| 408 | self.graph.schedule_recalculate(self.multisplines) |
| 409 | |
| 410 | self.control_point_index = None |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 411 | |
| 412 | def do_button_press_event(self, event): |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 413 | self.mousex, self.mousey = self.input_transform.transform_point( |
| 414 | event.x, event.y) |
Ravago Jones | 6d460fe | 2021-07-03 16:59:55 -0700 | [diff] [blame] | 415 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 416 | if self.mode == Mode.kPlacing: |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 417 | if self.active_multispline.addPoint(self.mousex, self.mousey): |
John Park | 91e6973 | 2019-03-03 13:12:43 -0800 | [diff] [blame] | 418 | self.mode = Mode.kEditing |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 419 | self.graph.schedule_recalculate(self.multisplines) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 420 | elif self.mode == Mode.kEditing: |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 421 | # Now after we have no control point index, |
| 422 | # the user can click for new point |
| 423 | if self.control_point_index == None: |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 424 | # Get clicked point |
| 425 | # Find nearest |
| 426 | # Move nearest to clicked |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 427 | cur_p = [self.mousex, self.mousey] |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 428 | # Get the distance between each for x and y |
| 429 | # Save the index of the point closest |
John Park | 13d3e28 | 2019-01-26 20:16:48 -0800 | [diff] [blame] | 430 | 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] | 431 | index_of_closest = 0 |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 432 | for index_multisplines, multispline in enumerate( |
| 433 | self.multisplines): |
| 434 | for index_splines, points in enumerate( |
| 435 | multispline.getSplines()): |
| 436 | for index_points, val in enumerate(points): |
| 437 | distance = np.sqrt((cur_p[0] - val[0])**2 + |
| 438 | (cur_p[1] - val[1])**2) |
| 439 | if distance < nearest: |
| 440 | nearest = distance |
| 441 | index_of_closest = index_points |
| 442 | self.control_point_index = ControlPointIndex( |
| 443 | index_multisplines, index_splines, |
| 444 | index_points) |
Ravago Jones | 128fb99 | 2021-07-31 13:56:58 -0700 | [diff] [blame] | 445 | self.queue_draw() |
| 446 | |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 447 | def do_motion_notify_event(self, event): |
| 448 | old_x = self.mousex |
| 449 | old_y = self.mousey |
Ravago Jones | 797c49c | 2021-07-31 14:51:59 -0700 | [diff] [blame] | 450 | self.mousex, self.mousey = self.input_transform.transform_point( |
| 451 | event.x, event.y) |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 452 | dif_x = self.mousex - old_x |
| 453 | dif_y = self.mousey - old_y |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 454 | difs = np.array([dif_x, dif_y]) |
Ravago Jones | 128fb99 | 2021-07-31 13:56:58 -0700 | [diff] [blame] | 455 | |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 456 | if self.mode == Mode.kEditing and self.control_point_index != None: |
| 457 | multispline = self.multisplines[ |
| 458 | self.control_point_index.multispline_index] |
| 459 | multispline.updates_for_mouse_move( |
| 460 | self.control_point_index.control_point_index, |
| 461 | self.control_point_index.spline_index, self.mousex, |
| 462 | self.mousey, difs) |
Ravago Jones | 128fb99 | 2021-07-31 13:56:58 -0700 | [diff] [blame] | 463 | |
Ravago Jones | fa8da56 | 2022-07-02 18:10:22 -0700 | [diff] [blame] | 464 | multispline.update_lib_spline() |
| 465 | self.graph.schedule_recalculate(self.multisplines) |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 466 | self.queue_draw() |
Ravago Jones | 128fb99 | 2021-07-31 13:56:58 -0700 | [diff] [blame] | 467 | |
Ravago Jones | 76ecec8 | 2021-08-07 14:37:08 -0700 | [diff] [blame] | 468 | def do_scroll_event(self, event): |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 469 | |
Ravago Jones | 0178120 | 2021-08-01 15:25:11 -0700 | [diff] [blame] | 470 | self.mousex, self.mousey = self.input_transform.transform_point( |
| 471 | event.x, event.y) |
| 472 | |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 473 | step_size = self.pxToM(20) # px |
Ravago Jones | 0178120 | 2021-08-01 15:25:11 -0700 | [diff] [blame] | 474 | |
| 475 | if event.direction == Gdk.ScrollDirection.UP: |
| 476 | # zoom out |
| 477 | scale_by = step_size |
| 478 | elif event.direction == Gdk.ScrollDirection.DOWN: |
| 479 | # zoom in |
| 480 | scale_by = -step_size |
| 481 | else: |
| 482 | return |
| 483 | |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 484 | scale = (self.field.width + scale_by) / self.field.width |
Ravago Jones | 0178120 | 2021-08-01 15:25:11 -0700 | [diff] [blame] | 485 | |
Ribhav Kaul | fa68ba8 | 2021-09-11 16:26:57 -0700 | [diff] [blame] | 486 | # This restricts the amount it can be scaled. |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 487 | if self.zoom_transform.xx <= 0.5: |
Ribhav Kaul | fa68ba8 | 2021-09-11 16:26:57 -0700 | [diff] [blame] | 488 | scale = max(scale, 1) |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 489 | elif self.zoom_transform.xx >= 16: |
Ribhav Kaul | fa68ba8 | 2021-09-11 16:26:57 -0700 | [diff] [blame] | 490 | scale = min(scale, 1) |
| 491 | |
Ravago Jones | de18dfe | 2022-03-16 20:57:19 -0700 | [diff] [blame] | 492 | # undo the scaled translation that the old zoom transform did |
Ravago Jones | 8da89c4 | 2022-07-17 19:34:06 -0700 | [diff] [blame] | 493 | x, y = self.invert(self.zoom_transform).transform_point( |
| 494 | event.x, event.y) |
Ravago Jones | de18dfe | 2022-03-16 20:57:19 -0700 | [diff] [blame] | 495 | |
Ravago Jones | 0178120 | 2021-08-01 15:25:11 -0700 | [diff] [blame] | 496 | # move the origin to point |
Ravago Jones | de18dfe | 2022-03-16 20:57:19 -0700 | [diff] [blame] | 497 | self.zoom_transform.translate(x, y) |
Ravago Jones | 0178120 | 2021-08-01 15:25:11 -0700 | [diff] [blame] | 498 | |
| 499 | # scale from new origin |
Ravago Jones | 54dafeb | 2022-03-02 20:41:47 -0800 | [diff] [blame] | 500 | self.zoom_transform.scale(scale, scale) |
Ravago Jones | 0178120 | 2021-08-01 15:25:11 -0700 | [diff] [blame] | 501 | |
| 502 | # move back |
Ravago Jones | de18dfe | 2022-03-16 20:57:19 -0700 | [diff] [blame] | 503 | self.zoom_transform.translate(-x, -y) |
Ravago Jones | 0178120 | 2021-08-01 15:25:11 -0700 | [diff] [blame] | 504 | |
| 505 | self.queue_draw() |