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