-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulate.py
93 lines (67 loc) · 1.95 KB
/
simulate.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
import csv
import importlib
import os
import sys
import time
import matplotlib.pyplot as plot
import numpy as np
animations = {}
def files_in(path):
for name in os.listdir(path):
if os.path.isfile(os.path.join(path, name)):
yield name
def load_animation(name: str) -> None:
full_name = f"animations.{name}"
resolved_name = importlib.util.resolve_name(full_name, None)
spec = importlib.util.find_spec(resolved_name)
lib = importlib.util.module_from_spec(spec)
sys.modules[resolved_name] = lib
spec.loader.exec_module(lib)
instantiate = getattr(lib, 'instantiate')
return instantiate()
# Load all animations in ./animations
for file_name in files_in("animations"):
if not file_name.endswith(".py"):
continue
name = file_name.replace(".py", "")
animations[name] = load_animation(name)
# Load lamp data
data_file_name = "data.csv"
lamps = []
# Read coordinates
with open(data_file_name, "r", newline="") as file_handle:
reader = csv.reader(file_handle)
for row in reader:
lamps.append(np.array(np.double(row)))
lamps = np.array(lamps)
colors = np.full([lamps.shape[0], 3], np.array([1.0, 1.0, 1.0]), dtype=np.ndarray)
# Plot
xs = lamps[:, 0]
ys = lamps[:, 1]
zs = lamps[:, 2]
figure = plot.figure()
figure.set_facecolor((0, 0, 0))
axes = figure.add_subplot(projection="3d")
axes.set_box_aspect((np.ptp(xs), np.ptp(ys), np.ptp(zs)))
axes.set_facecolor((0, 0, 0))
axes.xaxis.pane.fill = False
axes.yaxis.pane.fill = False
axes.grid(False)
figure.tight_layout(pad=0.0)
points = axes.scatter(xs, ys, zs, s=50, c=colors)
plot.show(block=False)
# Load animation
anim = animations["wanderingSphere"]
# Animate
t_start = time.time()
t = 0.0
t_prev = 0.0
while True:
t_prev = t
t = time.time() - t_start
dt = t - t_prev
anim.update(t, dt, lamps, colors)
points.set_facecolors(colors)
plot.pause(1.0 / 60.0)
if not plot.fignum_exists(figure.number):
break