-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
334 lines (247 loc) · 9.12 KB
/
gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# Copyright © 2022 Alexander L. Hayes
"""
Controls:
- Left Mouse: Place points or toggle point color
- Right Mouse: Draw lines connecting two points
- `b`: Turn off graph paper / gray background mode (helpful for taking screenshots)
- `c`: Reset
- `s`: Save.
"""
import argparse
from copy import deepcopy
from datetime import datetime
from enum import Enum
import json
import os
os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "hide"
from roofworld.exporter import Network
import matplotlib.pyplot as plt
import pygame
from srlearn.rdn import BoostedRDNClassifier
from srlearn import Database
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
LIGHTGRAY = (250, 250, 250)
LIGHTBLUE = (150, 173, 233)
ORANGE = (255, 165, 0)
WIDTH, HEIGHT = 1000, 600
SPACING = 25
LINE_MODE = None
def myround(x, base=25):
return base * round(x / base)
def draw_graphpaper(screen, k):
for i in range(WIDTH // k):
x = k * i
y = k * i
pygame.draw.line(screen, LIGHTBLUE, (x, 0), (x, HEIGHT), 1)
pygame.draw.line(screen, LIGHTBLUE, (0, y), (WIDTH, y), 1)
class Operations(Enum):
ADD_POINT = 1
ADD_LINE = 2
TOGGLE_POINT = 3
RESET = 4
class FlatWorld:
def __init__(self):
self.operations = []
self.points = {}
self.lines = []
@staticmethod
def from_json(file_path: str):
with open(file_path, "r") as fh:
points, lines = json.loads(fh.read())
# Unmap the points
pnts = {}
for entry in points:
pnts[tuple(entry["point"])] = tuple(entry["color"])
fw = FlatWorld()
fw.points = pnts
fw.lines = lines
return fw
def to_json(self, file_path: str):
# Remap the points to a list, since JSON doesn't recognize tuple dictionaries
pnts_list = []
for k, v in self.points.items():
pnts_list.append({
"point": k,
"color": v,
})
with open(file_path, "w") as fh:
fh.write(json.dumps([pnts_list, self.lines]))
def dump_state(self):
print(self.points)
print(self.lines)
net = Network(self.points, self.lines)
database = net.describe()
for example in database.pos + database.neg:
print(example)
print("\n")
for fact in database.facts:
print(fact)
print("\n")
def predict(self):
net = Network(self.points, self.lines)
database = net.describe()
db = Database()
db.pos = database.pos + database.neg
db.neg = []
db.facts = database.facts
rdn = BoostedRDNClassifier()
rdn.from_json("roofworld/predict_highpoint_rdn.json")
zs = rdn.predict_proba(db)
xs = [x for (x, _) in self.points]
ys = [y for (_, y) in self.points]
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
ax.scatter(xs, ys, zs, cmap="gray")
plt.show()
def export(self, start=0):
net = Network(self.points, self.lines, start=start)
database = net.describe()
with open("pos.txt", "a") as fh:
for ex in database.pos:
fh.write(ex + "\n")
with open("neg.txt", "a") as fh:
for ex in database.neg:
fh.write(ex + "\n")
with open("facts.txt", "a") as fh:
for ex in database.facts:
fh.write(ex + "\n")
def add_point(self, x, y):
self.operations.append((Operations.ADD_POINT, (x, y)))
self.points[(x, y)] = BLACK
def toggle_point(self, x, y):
c = self.points[(x, y)]
self.operations.append((Operations.TOGGLE_POINT, (x, y)))
if c == BLACK:
self.points[(x, y)] = ORANGE
else:
self.points[(x, y)] = BLACK
def add_line(self, x1, y1, x2, y2):
if (((x1, y1), (x2, y2)) in self.lines) or (((x2, y2), (x1, y1)) in self.lines):
# TODO(hayesall): Is there a data structure that has better performance?
pass
else:
self.operations.append((Operations.ADD_LINE, (x1, y1, x2, y2)))
self.lines.append(((x1, y1), (x2, y2)))
def reset(self):
self.operations = [
(
Operations.RESET,
(
deepcopy(self.operations),
deepcopy(self.points),
deepcopy(self.lines),
),
)
]
self.points = {}
self.lines = []
def undo(self):
if not self.operations:
print("Reached furthest point back in history.")
else:
last_op, last_value = self.operations.pop()
match last_op:
case Operations.ADD_POINT:
self.points.pop(last_value, None)
case Operations.ADD_LINE:
self.lines.pop()
case Operations.TOGGLE_POINT:
c = self.points[last_value]
if c == BLACK:
self.points[last_value] = ORANGE
else:
self.points[last_value] = BLACK
case Operations.RESET:
ops, pnts, lns = last_value
self.operations = ops
self.points = pnts
self.lines = lns
case _:
raise RuntimeError("Tried to undo an unknown operation.")
def __repr__(self):
return str(self.operations) + str(self.points) + str(self.lines)
if __name__ == "__main__":
PARSER = argparse.ArgumentParser()
PARSER.add_argument("file", nargs="?", help="Load from a json file.")
ARGS = PARSER.parse_args()
if ARGS.file:
fw = FlatWorld.from_json(ARGS.file)
else:
fw = FlatWorld()
DONE = False
BACKGROUND = True
pygame.init()
pygame.event.set_blocked(pygame.MOUSEMOTION)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Roof World GUI")
CLOCK = pygame.time.Clock()
save_state = 0
while not DONE:
CLOCK.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
DONE = True
if event.type == pygame.KEYDOWN and event.key == pygame.K_d:
fw.dump_state()
if event.type == pygame.KEYDOWN and event.key == pygame.K_p:
fw.predict()
if event.type == pygame.KEYDOWN and event.key == pygame.K_b:
BACKGROUND = not BACKGROUND
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
LINE_MODE = None
if (
event.type == pygame.KEYDOWN
and event.key == pygame.K_z
and pygame.key.get_mods() & pygame.KMOD_LCTRL
):
fw.undo()
if event.type == pygame.KEYDOWN and event.key == pygame.K_c:
fw.reset()
if event.type == pygame.KEYDOWN and event.key == pygame.K_s:
# TODO(hayesall): Save to an output directory.
# Get a datetime + path
save_to = f"saved/building_{int(datetime.now().timestamp())}.json"
fw.to_json(save_to)
fw.export(start=save_state)
save_state += len(fw.points)
print("Saved to network.pickle")
if event.type == pygame.MOUSEBUTTONUP and event.button == 3:
# Draw the lines using right click
if not LINE_MODE:
# Make sure there's a point near the right click location:
x, y = pygame.mouse.get_pos()
x_r, y_r = myround(x), myround(y)
if (x_r, y_r) in fw.points:
LINE_MODE = (x_r, y_r)
else:
x, y = pygame.mouse.get_pos()
x_r, y_r = myround(x), myround(y)
# We want:
# - To draw from point to a point
# - Do NOT want to draw from a point to itself
if (x_r, y_r) in fw.points and (x_r, y_r) != LINE_MODE:
fw.add_line(*LINE_MODE, x_r, y_r)
LINE_MODE = None
if event.type == pygame.MOUSEBUTTONUP and event.button == 1:
# Draw the points using left click
x, y = pygame.mouse.get_pos()
x_r, y_r = myround(x), myround(y)
if fw.points.get((x_r, y_r)):
fw.toggle_point(x_r, y_r)
else:
fw.add_point(x_r, y_r)
# Render the scene
if BACKGROUND:
screen.fill(LIGHTGRAY)
draw_graphpaper(screen, SPACING)
else:
screen.fill(WHITE)
for ((x1, y1), (x2, y2)) in fw.lines:
pygame.draw.line(screen, BLACK, (x1, y1), (x2, y2), 3)
for (x, y) in fw.points:
pygame.draw.circle(screen, fw.points[(x, y)], (x, y), 5)
if LINE_MODE:
pygame.draw.line(screen, BLACK, LINE_MODE, pygame.mouse.get_pos(), 1)
pygame.display.flip()
pygame.quit()