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