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 | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 4 | import copy |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 5 | import basic_window |
| 6 | from color import Color, palette |
| 7 | import random |
| 8 | import gi |
| 9 | import numpy as np |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 10 | from libspline import Spline |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 11 | import scipy.spatial.distance |
| 12 | gi.require_version('Gtk', '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 |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 15 | import enum |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 16 | import csv # For writing to csv files |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 17 | |
| 18 | from basic_window import OverrideMatrix, identity, quit_main_loop, set_color |
| 19 | |
| 20 | LENGTH_OF_FIELD = 323.65 |
| 21 | PIXELS_ON_SCREEN = 300 |
| 22 | |
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 | def pxToIn(p): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 25 | return p * LENGTH_OF_FIELD / PIXELS_ON_SCREEN |
| 26 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 27 | |
| 28 | def inToPx(i): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 29 | return (i * PIXELS_ON_SCREEN / LENGTH_OF_FIELD) |
| 30 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 31 | |
| 32 | def px(cr): |
| 33 | return OverrideMatrix(cr, identity) |
| 34 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 35 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 36 | def draw_px_cross(cr, x, y, length_px, color=palette["RED"]): |
| 37 | """Draws a cross with fixed dimensions in pixel space.""" |
| 38 | set_color(cr, color) |
| 39 | cr.move_to(x, y - length_px) |
| 40 | cr.line_to(x, y + length_px) |
| 41 | cr.stroke() |
| 42 | |
| 43 | cr.move_to(x - length_px, y) |
| 44 | cr.line_to(x + length_px, y) |
| 45 | cr.stroke() |
| 46 | set_color(cr, palette["LIGHT_GREY"]) |
| 47 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 48 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 49 | def draw_px_x(cr, x, y, length_px1, color=palette["BLACK"]): |
| 50 | """Draws a x with fixed dimensions in pixel space.""" |
| 51 | length_px = length_px1 / np.sqrt(2) |
| 52 | set_color(cr, color) |
| 53 | cr.move_to(x - length_px, y - length_px) |
| 54 | cr.line_to(x + length_px, y + length_px) |
| 55 | cr.stroke() |
| 56 | |
| 57 | cr.move_to(x - length_px, y + length_px) |
| 58 | cr.line_to(x + length_px, y - length_px) |
| 59 | cr.stroke() |
| 60 | set_color(cr, palette["LIGHT_GREY"]) |
| 61 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 62 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 63 | def draw_points(cr, p, size): |
| 64 | for i in range(0, len(p)): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 65 | draw_px_cross(cr, p[i][0], p[i][1], size, Color( |
| 66 | 0, np.sqrt(0.2 * i), 0)) |
| 67 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 68 | |
| 69 | class Mode(enum.Enum): |
| 70 | kViewing = 0 |
| 71 | kPlacing = 1 |
| 72 | kEditing = 2 |
| 73 | kExporting = 3 |
| 74 | kImporting = 4 |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 75 | kConstraint = 5 |
| 76 | |
| 77 | |
| 78 | class ConstraintType(enum.Enum): |
| 79 | kMaxSpeed = 0 |
| 80 | kMaxAcceleration = 1 |
| 81 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 82 | |
| 83 | def display_text(cr, text, widtha, heighta, widthb, heightb): |
| 84 | cr.scale(widtha, -heighta) |
| 85 | cr.show_text(text) |
| 86 | cr.scale(widthb, -heightb) |
| 87 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 88 | |
| 89 | def redraw(needs_redraw, window): |
| 90 | print("Redrew") |
| 91 | if not needs_redraw: |
| 92 | window.queue_draw() |
| 93 | |
| 94 | |
| 95 | class Constraint(): |
| 96 | def __init__(self, start, end, constraint, value): |
| 97 | self.start = start #Array with index and distance from start of spline |
| 98 | self.end = end #Array with index and distance from start of spline |
| 99 | self.constraint = constraint #INT |
| 100 | self.value = value #INT |
| 101 | if self.constraint == 0: |
| 102 | self.conName = "kMaxSpeed" |
| 103 | else: |
| 104 | self.conName = "kMaxAcceleration" |
| 105 | |
| 106 | def toString(self): |
| 107 | |
| 108 | return "START: " + str(self.start[0]) + ", " + str( |
| 109 | self.start[1]) + " | END: " + str(self.end[0]) + ", " + str( |
| 110 | self.end[1]) + " | " + str(self.conName) + ": " + str( |
| 111 | self.value) |
| 112 | |
| 113 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 114 | class GTK_Widget(basic_window.BaseWindow): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 115 | """Create a GTK+ widget on which we will draw using Cairo""" |
| 116 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 117 | def __init__(self): |
| 118 | super(GTK_Widget, self).__init__() |
| 119 | |
| 120 | # init field drawing |
| 121 | # add default spline for testing purposes |
| 122 | # init editing / viewing modes and pointer location |
| 123 | self.mode = Mode.kPlacing |
| 124 | self.x = 0 |
| 125 | self.y = 0 |
| 126 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 127 | # update list of control points |
| 128 | self.point_selected = False |
| 129 | # self.adding_spline = False |
| 130 | self.index_of_selected = -1 |
| 131 | self.new_point = [] |
| 132 | |
| 133 | # For the editing mode |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 134 | 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] | 135 | self.held_x = 0 |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 136 | self.spline_edit = -1 |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 137 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 138 | self.curves = [] |
| 139 | |
| 140 | self.colors = [] |
| 141 | |
| 142 | for c in palette: |
| 143 | self.colors.append(palette[c]) |
| 144 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 145 | self.selected_points = [] |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 146 | self.splines = [] |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 147 | self.reinit_extents() |
| 148 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 149 | self.inStart = None |
| 150 | self.inEnd = None |
| 151 | self.inConstraint = None |
| 152 | self.inValue = None |
| 153 | self.startSet = False |
| 154 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 155 | #John also wrote this |
| 156 | def add_point(self, x, y): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 157 | if (len(self.selected_points) < 6): |
| 158 | self.selected_points.append([x, y]) |
| 159 | if (len(self.selected_points) == 6): |
| 160 | self.mode = Mode.kEditing |
| 161 | self.splines.append(np.array(self.selected_points)) |
| 162 | self.selected_points = [] |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 163 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 164 | """set extents on images""" |
| 165 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 166 | def reinit_extents(self): |
| 167 | self.extents_x_min = -800 |
| 168 | self.extents_x_max = 800 |
| 169 | self.extents_y_min = -800 |
| 170 | self.extents_y_max = 800 |
| 171 | |
| 172 | # this needs to be rewritten with numpy, i dont think this ought to have |
| 173 | # SciPy as a dependecy |
| 174 | def get_index_of_nearest_point(self): |
| 175 | cur_p = [[self.x, self.y]] |
| 176 | distances = scipy.spatial.distance.cdist(cur_p, self.all_controls) |
| 177 | |
| 178 | return np.argmin(distances) |
| 179 | |
| 180 | # return the closest point to the loc of the click event |
| 181 | def get_nearest_point(self): |
| 182 | return self.all_controls[self.get_index_of_nearest_point()] |
| 183 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 184 | def set_index_to_nearest_spline_point(self): |
| 185 | nearest = 50 |
| 186 | index_of_closest = 0 |
| 187 | self.spline_edit = 0 |
| 188 | cur_p = [self.x, self.y] |
| 189 | |
| 190 | for index_splines, points in enumerate(self.spline): |
| 191 | for index_points, i in enumerate(points.curve): |
| 192 | # pythagorean |
| 193 | distance = np.sqrt((cur_p[0] - i[0])**2 + (cur_p[1] - i[1])**2) |
| 194 | if distance < nearest: |
| 195 | nearest = distance |
| 196 | print("DISTANCE: ", distance, " | INDEX: ", index_points) |
| 197 | index_of_closest = index_points |
| 198 | self.index_of_edit = index_of_closest |
| 199 | self.spline_edit = index_splines |
| 200 | self.held_x = self.x |
| 201 | if self.startSet == False: |
| 202 | self.inStart = [self.index_of_edit, self.findDistance()] |
| 203 | self.startSet = True |
| 204 | else: |
| 205 | self.inEnd = [self.index_of_edit, self.findDistance()] |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 206 | self.startSet = False |
| 207 | self.mode = Mode.kEditing |
| 208 | self.spline_edit = -1 |
| 209 | self.index_of_edit = -1 |
| 210 | |
| 211 | print("Nearest: " + str(nearest)) |
| 212 | print("Spline: " + str(self.spline_edit)) |
| 213 | print("Index: " + str(index_of_closest)) |
| 214 | |
| 215 | def findDistance(self): |
| 216 | """ findDistance goes through each point on the spline finding distance to that point from the point before. |
| 217 | It does this to find the the length of the spline to the point that is currently selected. |
| 218 | """ |
| 219 | distance = 0 |
| 220 | points = self.curves[self.spline_edit] |
| 221 | for index, point in enumerate(points): |
| 222 | if index > 0 and index <= self.index_of_edit: |
| 223 | distance += np.sqrt((points[index - 1][0] - point[0])**2 + |
| 224 | (points[index - 1][1] - point[1])**2) |
| 225 | return pxToIn(distance) |
| 226 | |
| 227 | # Handle the expose-event by updating the Window and drawing |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 228 | def handle_draw(self, cr): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 229 | # print(self.new_point) |
| 230 | # print("SELF.POINT_SELECTED: " + str(self.point_selected)) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 231 | |
| 232 | # begin drawing |
| 233 | # Fill the background color of the window with grey |
| 234 | set_color(cr, palette["GREY"]) |
| 235 | cr.paint() |
Andrew Runke | a9c8de5 | 2019-01-26 19:54:29 -0800 | [diff] [blame] | 236 | #Scale the field to fit within drawing area |
Andrew Runke | 6696bb8 | 2019-02-02 14:33:59 -0800 | [diff] [blame^] | 237 | cr.scale(0.5, 0.5) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 238 | |
| 239 | # Draw a extents rectangle |
| 240 | set_color(cr, palette["WHITE"]) |
| 241 | cr.rectangle(self.extents_x_min, self.extents_y_min, |
| 242 | (self.extents_x_max - self.extents_x_min), |
| 243 | self.extents_y_max - self.extents_y_min) |
| 244 | cr.fill() |
| 245 | |
| 246 | #Drawing the switch and scale in the field |
| 247 | cr.move_to(0, 50) |
| 248 | cr.show_text('Press "e" to export') |
| 249 | cr.show_text('Press "i" to import') |
| 250 | |
| 251 | set_color(cr, Color(0.3, 0.3, 0.3)) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 252 | cr.rectangle(-450, -150, 300, 300) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 253 | cr.fill() |
| 254 | set_color(cr, palette["BLACK"]) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 255 | cr.rectangle(-450, -150, 300, 300) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 256 | cr.set_line_join(cairo.LINE_JOIN_ROUND) |
| 257 | cr.stroke() |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 258 | cr.rectangle((inToPx(140 - 161.825) - 300), inToPx(76.575), inToPx(56), |
| 259 | inToPx(-153.15)) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 260 | cr.set_line_join(cairo.LINE_JOIN_ROUND) |
| 261 | cr.stroke() |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 262 | cr.rectangle((inToPx(161.825 - 24) - 300), inToPx(90), inToPx(24), |
| 263 | inToPx(-180)) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 264 | cr.set_line_join(cairo.LINE_JOIN_ROUND) |
| 265 | cr.stroke() |
| 266 | |
| 267 | set_color(cr, Color(0.2, 0.2, 0.2)) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 268 | cr.rectangle( |
| 269 | inToPx(140 - 161.825) - 300, inToPx(76.575), inToPx(56), |
| 270 | inToPx(-153.15)) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 271 | cr.fill() |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 272 | cr.rectangle( |
| 273 | inToPx(161.825 - 24) - 300, inToPx(90), inToPx(24), inToPx(-180)) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 274 | cr.fill() |
| 275 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 276 | y = 0 |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 277 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 278 | # update all the things |
| 279 | |
| 280 | if self.mode == Mode.kViewing: |
| 281 | set_color(cr, palette["BLACK"]) |
| 282 | cr.move_to(-300, 170) |
| 283 | cr.show_text("VIEWING") |
| 284 | set_color(cr, palette["GREY"]) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 285 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 286 | if len(self.selected_points) > 0: |
| 287 | print("SELECTED_POINTS: " + str(len(self.selected_points))) |
| 288 | print("ITEMS:") |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 289 | # for item in self.selected_points: |
| 290 | # print(str(item)) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 291 | for i, point in enumerate(self.selected_points): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 292 | # print("I: " + str(i)) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 293 | draw_px_x(cr, point[0], point[1], 10) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 294 | cr.move_to(point[0], point[1] - 15) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 295 | display_text(cr, str(i), 0.5, 0.5, 2, 2) |
| 296 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 297 | elif self.mode == Mode.kPlacing: |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 298 | set_color(cr, palette["BLACK"]) |
| 299 | cr.move_to(-300, 170) |
| 300 | display_text(cr, "ADD", 1, 1, 1, 1) |
| 301 | set_color(cr, palette["GREY"]) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 302 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 303 | if len(self.selected_points) > 0: |
| 304 | print("SELECTED_POINTS: " + str(len(self.selected_points))) |
| 305 | print("ITEMS:") |
| 306 | for item in self.selected_points: |
| 307 | print(str(item)) |
| 308 | for i, point in enumerate(self.selected_points): |
| 309 | print("I: " + str(i)) |
| 310 | draw_px_x(cr, point[0], point[1], 10) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 311 | cr.move_to(point[0], point[1] - 15) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 312 | display_text(cr, str(i), 0.5, 0.5, 2, 2) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 313 | |
| 314 | elif self.mode == Mode.kEditing: |
| 315 | set_color(cr, palette["BLACK"]) |
| 316 | cr.move_to(-300, 170) |
| 317 | display_text(cr, "EDITING", 1, 1, 1, 1) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 318 | if len(self.splines) > 0: |
| 319 | # print("Splines: " + str(len(self.splines))) |
| 320 | # print("ITEMS:") |
| 321 | holder_spline = [] |
| 322 | for i, points in enumerate(self.splines): |
| 323 | array = np.zeros(shape=(6, 2), dtype=float) |
| 324 | for j, point in enumerate(points): |
| 325 | array[j, 0] = point[0] |
| 326 | array[j, 1] = point[1] |
| 327 | spline = Spline(np.ascontiguousarray(np.transpose(array))) |
| 328 | for k in np.linspace(0.01, 1, 100): |
| 329 | |
| 330 | cr.move_to( |
| 331 | spline.Point(k - 0.01)[0], |
| 332 | spline.Point(k - 0.01)[1]) |
| 333 | cr.line_to(spline.Point(k)[0], spline.Point(k)[1]) |
| 334 | cr.stroke() |
| 335 | holding = [ |
| 336 | spline.Point(k - 0.01)[0], |
| 337 | spline.Point(k - 0.01)[1] |
| 338 | ] |
| 339 | |
| 340 | holder_spline.append(holding) |
| 341 | self.curves.append(holder_spline) |
| 342 | |
| 343 | for spline, points in enumerate(self.splines): |
| 344 | # for item in points: |
| 345 | # print(str(item)) |
| 346 | for i, point in enumerate(points): |
| 347 | # print("I: " + str(i)) |
| 348 | if spline == self.spline_edit and i == self.index_of_edit: |
| 349 | draw_px_x(cr, point[0], point[1], 15, |
| 350 | self.colors[spline]) |
| 351 | elif (spline == 0 and not i == 5) or (not i == 0 |
| 352 | and not i == 5): |
| 353 | draw_px_x(cr, point[0], point[1], 10, |
| 354 | self.colors[spline]) |
| 355 | cr.move_to(point[0], point[1] - 15) |
| 356 | display_text(cr, str(i), 0.5, 0.5, 2, 2) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 357 | |
| 358 | elif self.mode == Mode.kExporting: |
| 359 | set_color(cr, palette["BLACK"]) |
| 360 | cr.move_to(-300, 170) |
| 361 | display_text(cr, "VIEWING", 1, 1, 1, 1) |
| 362 | set_color(cr, palette["GREY"]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 363 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 364 | if len(self.selected_points) > 0: |
| 365 | print("SELECTED_POINTS: " + str(len(self.selected_points))) |
| 366 | print("ITEMS:") |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 367 | # for item in self.selected_points: |
| 368 | # print(str(item)) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 369 | for i, point in enumerate(self.selected_points): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 370 | # print("I: " + str(i)) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 371 | draw_px_x(cr, point[0], point[1], 10) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 372 | cr.move_to(point[0], point[1] - 15) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 373 | display_text(cr, str(i), 0.5, 0.5, 2, 2) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 374 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 375 | elif self.mode == Mode.kImporting: |
| 376 | set_color(cr, palette["BLACK"]) |
| 377 | cr.move_to(-300, 170) |
| 378 | display_text(cr, "VIEWING", 1, 1, 1, 1) |
| 379 | set_color(cr, palette["GREY"]) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 380 | |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 381 | if len(self.selected_points) > 0: |
| 382 | print("SELECTED_POINTS: " + str(len(self.selected_points))) |
| 383 | print("ITEMS:") |
| 384 | for item in self.selected_points: |
| 385 | print(str(item)) |
| 386 | for i, point in enumerate(self.selected_points): |
| 387 | print("I: " + str(i)) |
| 388 | draw_px_x(cr, point[0], point[1], 10) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 389 | cr.move_to(point[0], point[1] - 15) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 390 | display_text(cr, str(i), 0.5, 0.5, 2, 2) |
| 391 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 392 | elif self.mode == Mode.kConstraint: |
| 393 | print("Drawn") |
| 394 | set_color(cr, palette["BLACK"]) |
| 395 | cr.move_to(-300, 170) |
| 396 | display_text(cr, "Adding Constraint", 1, 1, 1, 1) |
| 397 | if len(self.splines) > 0: |
| 398 | # print("Splines: " + str(len(self.splines))) |
| 399 | # print("ITEMS:") |
| 400 | for s, points in enumerate(self.splines): |
| 401 | # for item in points: |
| 402 | # print(str(item)) |
| 403 | for i, point in enumerate(points): |
| 404 | # print("I: " + str(i)) |
| 405 | draw_px_x(cr, point[0], point[1], 10, self.colors[s]) |
| 406 | cr.move_to(point[0], point[1] - 15) |
| 407 | display_text(cr, str(i), 0.5, 0.5, 2, 2) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 408 | |
| 409 | cr.paint_with_alpha(.65) |
| 410 | |
| 411 | draw_px_cross(cr, self.x, self.y, 10) |
| 412 | |
| 413 | def do_key_press(self, event): |
| 414 | keyval = Gdk.keyval_to_lower(event.keyval) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 415 | # print("Gdk.KEY_" + Gdk.keyval_name(keyval)) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 416 | if keyval == Gdk.KEY_q: |
| 417 | print("Found q key and exiting.") |
| 418 | quit_main_loop() |
| 419 | if keyval == Gdk.KEY_e: |
| 420 | self.mode = Mode.kExporting |
| 421 | # Will export to csv file |
| 422 | with open('points_for_pathedit.csv', mode='w') as points_file: |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 423 | writer = csv.writer( |
| 424 | points_file, |
| 425 | delimiter=',', |
| 426 | quotechar='"', |
| 427 | quoting=csv.QUOTE_MINIMAL) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 428 | for item in self.selected_points: |
| 429 | writer.writerow([str(item[0]), str(item[1])]) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 430 | print("Wrote: " + str(item[0]) + " " + str(item[1])) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 431 | if keyval == Gdk.KEY_i: |
| 432 | self.mode = Mode.kImporting |
| 433 | # import from csv file |
| 434 | self.selected_points = [] |
| 435 | with open('points_for_pathedit.csv') as points_file: |
| 436 | reader = csv.reader(points_file, delimiter=',') |
| 437 | for row in reader: |
| 438 | self.add_point(float(row[0]), float(row[1])) |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 439 | print("Added: " + row[0] + " " + row[1]) |
| 440 | if keyval == Gdk.KEY_p: |
| 441 | self.mode = Mode.kPlacing |
| 442 | # F0 = A1 |
| 443 | # B1 = 2F0 - E0 |
| 444 | # C1= d0 + 4F0 - 4E0 |
| 445 | spline_index = len(self.splines) - 1 |
| 446 | self.selected_points = [] |
| 447 | f = self.splines[spline_index][5] |
| 448 | e = self.splines[spline_index][4] |
| 449 | d = self.splines[spline_index][3] |
| 450 | self.selected_points.append(f) |
| 451 | self.selected_points.append(f * 2 + e * -1) |
| 452 | self.selected_points.append(d + f * 4 + e * -4) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 453 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 454 | if keyval == Gdk.KEY_c: |
| 455 | self.mode = Mode.kConstraint |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 456 | |
| 457 | def button_press_action(self): |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 458 | if self.mode == Mode.kPlacing: |
| 459 | #Check that the point clicked is on the field |
| 460 | if (self.x < -150 and self.x > -450 and self.y < 150 |
| 461 | and self.y > -150): |
| 462 | self.add_point(self.x, self.y) |
| 463 | elif self.mode == Mode.kEditing: |
| 464 | # Now after index_of_edit is not -1, the point is selected, so |
| 465 | # user can click for new point |
| 466 | if self.index_of_edit > -1 and self.held_x != self.x: |
| 467 | print("INDEX OF EDIT: " + str(self.index_of_edit)) |
| 468 | self.splines[self.spline_edit][self.index_of_edit] = [ |
| 469 | self.x, self.y |
| 470 | ] |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 471 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 472 | if not self.spline_edit == len(self.splines) - 1: |
| 473 | spline_edit = self.spline_edit + 1 |
| 474 | f = self.splines[self.spline_edit][5] |
| 475 | e = self.splines[self.spline_edit][4] |
| 476 | d = self.splines[self.spline_edit][3] |
| 477 | self.splines[spline_edit][0] = f |
| 478 | self.splines[spline_edit][1] = f * 2 + e * -1 |
| 479 | self.splines[spline_edit][2] = d + f * 4 + e * -4 |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 480 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 481 | if not self.spline_edit == 0: |
| 482 | spline_edit = self.spline_edit - 1 |
| 483 | a = self.splines[self.spline_edit][0] |
| 484 | b = self.splines[self.spline_edit][1] |
| 485 | c = self.splines[self.spline_edit][2] |
| 486 | self.splines[spline_edit][5] = a |
| 487 | self.splines[spline_edit][4] = a * 2 + b * -1 |
| 488 | self.splines[spline_edit][3] = c + a * 4 + b * -4 |
| 489 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 490 | self.index_of_edit = -1 |
| 491 | self.spline_edit = -1 |
| 492 | else: |
| 493 | print("mode == 2") |
| 494 | # Get clicked point |
| 495 | # Find nearest |
| 496 | # Move nearest to clicked |
| 497 | cur_p = [self.x, self.y] |
| 498 | print("CUR_P: " + str(self.x) + " " + str(self.y)) |
| 499 | # Get the distance between each for x and y |
| 500 | # Save the index of the point closest |
| 501 | nearest = 50 |
| 502 | index_of_closest = 0 |
| 503 | for index_splines, points in enumerate(self.splines): |
| 504 | for index_points, val in enumerate(points): |
| 505 | # pythagorean |
| 506 | distance = np.sqrt((cur_p[0] - val[0])**2 + |
| 507 | (cur_p[1] - val[1])**2) |
| 508 | if distance < nearest: |
| 509 | nearest = distance |
| 510 | index_of_closest = index_points |
| 511 | print("Nearest: " + str(nearest)) |
| 512 | print("Index: " + str(index_of_closest)) |
| 513 | self.index_of_edit = index_of_closest |
| 514 | self.spline_edit = index_splines |
| 515 | self.held_x = self.x |
| 516 | elif self.mode == Mode.kConstraint: |
| 517 | print("RAN") |
| 518 | self.set_index_to_nearest_spline_point() |
| 519 | print("FINISHED") |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 520 | |
| 521 | def do_button_press(self, event): |
| 522 | print("button press activated") |
Andrew Runke | a9c8de5 | 2019-01-26 19:54:29 -0800 | [diff] [blame] | 523 | #Be consistent with the scaling in the drawing_area |
| 524 | self.x = event.x * 2 |
| 525 | self.y = event.y * 2 |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 526 | self.button_press_action() |
| 527 | |
| 528 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 529 | class GridWindow(Gtk.Window): |
| 530 | def method_connect(self, event, cb): |
| 531 | def handler(obj, *args): |
| 532 | cb(*args) |
Tabitha Jarvis | 1007a13 | 2018-12-12 21:47:54 -0800 | [diff] [blame] | 533 | |
Andrew Runke | 6842bf9 | 2019-01-26 15:38:25 -0800 | [diff] [blame] | 534 | print("Method_connect ran") |
| 535 | self.connect(event, handler) |
| 536 | |
| 537 | def button_press(self, event): |
| 538 | print("button press activated") |
| 539 | o_x = event.x |
| 540 | o_y = event.y |
| 541 | x = event.x - self.drawing_area.window_shape[0] / 2 |
| 542 | y = self.drawing_area.window_shape[1] / 2 - event.y |
| 543 | scale = self.drawing_area.get_current_scale() |
| 544 | event.x = x / scale + self.drawing_area.center[0] |
| 545 | event.y = y / scale + self.drawing_area.center[1] |
| 546 | self.drawing_area.do_button_press(event) |
| 547 | event.x = o_x |
| 548 | event.y = o_y |
| 549 | |
| 550 | def key_press(self, event): |
| 551 | print("key press activated") |
| 552 | self.drawing_area.do_key_press(event) |
| 553 | self.queue_draw() |
| 554 | |
| 555 | def configure(self, event): |
| 556 | print("configure activated") |
| 557 | self.drawing_area.window_shape = (event.width, event.height) |
| 558 | |
| 559 | def on_submit_click(self, widget): |
| 560 | self.drawing_area.inConstraint = int(self.constraint_box.get_text()) |
| 561 | self.drawing_area.inValue = int(self.value_box.get_text()) |
| 562 | |
| 563 | def __init__(self): |
| 564 | Gtk.Window.__init__(self) |
| 565 | |
| 566 | self.set_default_size(1366, 738) |
| 567 | |
| 568 | flowBox = Gtk.FlowBox() |
| 569 | flowBox.set_valign(Gtk.Align.START) |
| 570 | flowBox.set_selection_mode(Gtk.SelectionMode.NONE) |
| 571 | |
| 572 | flowBox.set_valign(Gtk.Align.START) |
| 573 | |
| 574 | self.add(flowBox) |
| 575 | |
| 576 | container = Gtk.Fixed() |
| 577 | flowBox.add(container) |
| 578 | |
| 579 | self.eventBox = Gtk.EventBox() |
| 580 | container.add(self.eventBox) |
| 581 | |
| 582 | self.eventBox.set_events(Gdk.EventMask.BUTTON_PRESS_MASK |
| 583 | | Gdk.EventMask.BUTTON_RELEASE_MASK |
| 584 | | Gdk.EventMask.POINTER_MOTION_MASK |
| 585 | | Gdk.EventMask.SCROLL_MASK |
| 586 | | Gdk.EventMask.KEY_PRESS_MASK) |
| 587 | |
| 588 | self.drawing_area = GTK_Widget() |
| 589 | self.eventBox.add(self.drawing_area) |
| 590 | |
| 591 | self.method_connect("key-release-event", self.key_press) |
| 592 | self.method_connect("button-release-event", self.button_press) |
| 593 | self.method_connect("configure-event", self.configure) |
| 594 | |
| 595 | # Constraint Boxes |
| 596 | |
| 597 | self.start_box = Gtk.Entry() |
| 598 | self.start_box.set_size_request(100, 20) |
| 599 | |
| 600 | self.constraint_box = Gtk.Entry() |
| 601 | self.constraint_box.set_size_request(100, 20) |
| 602 | |
| 603 | self.constraint_box.set_text("Constraint") |
| 604 | self.constraint_box.set_editable(True) |
| 605 | |
| 606 | container.put(self.constraint_box, 700, 0) |
| 607 | |
| 608 | self.value_box = Gtk.Entry() |
| 609 | self.value_box.set_size_request(100, 20) |
| 610 | |
| 611 | self.value_box.set_text("Value") |
| 612 | self.value_box.set_editable(True) |
| 613 | |
| 614 | container.put(self.value_box, 700, 40) |
| 615 | |
| 616 | self.submit_button = Gtk.Button("Submit") |
| 617 | self.submit_button.connect('clicked', self.on_submit_click) |
| 618 | |
| 619 | container.put(self.submit_button, 880, 0) |
| 620 | |
| 621 | self.show_all() |
| 622 | |
| 623 | |
| 624 | window = GridWindow() |
Andrew Runke | 6696bb8 | 2019-02-02 14:33:59 -0800 | [diff] [blame^] | 625 | basic_window.RunApp() |