-
Notifications
You must be signed in to change notification settings - Fork 2
/
harmonograph.py
221 lines (192 loc) · 5.7 KB
/
harmonograph.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
import math
from dataclasses import dataclass
import numpy as np
from curve import Curve
from render import ColorLine, ElegantLine
from parameter import GlobalParameter, Parameter, Parameterized, global_value
from util import abc
freq = GlobalParameter("freq")
@dataclass
class FullWave(Parameterized):
freq: Parameter(
name="frequency",
key="f",
default=2,
adjacent_step=1,
random=lambda rnd: rnd.randrange(*freq.get((1, 6, 1))),
)
amp: Parameter(
name="amplitude",
key="a",
default=0.5,
places=3,
adjacent_step=0.2,
random=lambda rnd: rnd.uniform(0.1, 1.0),
)
tweq: Parameter(
name="frequency tweak",
key="t",
default=0.0,
places=6,
adjacent_step=0.0004,
random=lambda rnd: rnd.gauss(0, 0.005),
)
phase: Parameter(
name="phase",
key="p",
default=0.0,
places=4,
scale=2 * math.pi,
adjacent_step=0.2,
random=lambda rnd: rnd.uniform(0, 2 * math.pi),
)
def __call__(self, t, density=1):
return self.amp * np.sin((self.freq * density + self.tweq) * t + self.phase)
@classmethod
def make_random(cls, name, rnd, limit=None):
freqq = (1, 7, 1)
if limit == "even":
freqq = (2, 7, 2)
elif limit == "odd":
freqq = (1, 7, 2)
with global_value(freq, freqq):
return super().make_random(name, rnd)
@dataclass
class Ramp(Parameterized):
stop: Parameter(
name="stop",
key="z",
default=500,
)
def __call__(self, t):
return t / self.stop
@dataclass
class TimeSpan(Parameterized):
center: Parameter(
name="center",
key="c",
default=900,
adjacent_step=100,
)
width: Parameter(
name="width",
key="w",
default=200,
adjacent_step=50,
)
STYLES = [
ElegantLine(linewidth=3, alpha=1),
ColorLine(lightness=0, linewidth=50, alpha=0.1),
ColorLine(linewidth=10, alpha=0.5),
ColorLine(linewidth=50, alpha=0.1),
]
@dataclass
class Harmonograph(Curve):
ALGORITHM = 1
density: Parameter(
name="density",
key="d",
default=1.0,
places=2,
adjacent=lambda v: [v * 0.8 * 0.8, v * 0.8, v / 0.8, v / 0.8 / 0.8],
)
style: Parameter(
name="style",
key="s",
default=0,
adjacent=lambda _: list(range(len(STYLES))),
)
def __init__(self, density=1.0, style=0):
super().__init__()
self.density = density
self.style = style
self.dimensions = {}
self.set_time_span(TimeSpan("ts", 900, 200))
self.extras = set()
self.render = STYLES[style]
def add_dimension(self, name, waves, extra=False):
self.dimensions[name] = waves
if extra:
self.extras.add(name)
def set_ramp(self, ramp):
self.ramp = ramp
def set_time_span(self, timespan):
self.timespan = timespan
def points(self, dims, scale, dt=0.01):
ts_half = self.timespan.width // 2
t = np.arange(
start=self.timespan.center - ts_half,
stop=self.timespan.center + ts_half,
step=dt / self.density,
)
scale *= 2
scale /= len(self.dimensions["x"]) + 1
pts = []
for dim_name in dims:
waves = self.dimensions[dim_name]
val = 0.0
for wave in waves:
val += wave(t, self.density)
val *= self.ramp(t)
val *= scale
pts.append(val)
for pt in zip(*pts):
yield pt
def param_things(self):
for dim_name, dim in self.dimensions.items():
for wave in dim:
yield wave, (dim_name if dim_name in self.extras else None)
yield self, None
yield self.timespan, None
yield self.ramp, None
@classmethod
def from_params(cls, params, name=""):
assert name == ""
# Deduce the number of pendulums from the parameters
xs = set(k[1] for k in params if k.startswith("x"))
npend = len(xs)
harm = super().from_params(params)
harm.add_dimension(
"x",
[FullWave.from_params(params, f"x{abc(i)}") for i in range(npend)],
)
harm.add_dimension(
"y",
[FullWave.from_params(params, f"y{abc(i)}") for i in range(npend)],
)
harm.add_dimension("j", [FullWave.from_params(params, "j")], extra=True)
harm.add_dimension("k", [FullWave.from_params(params, "k")], extra=True)
harm.set_ramp(Ramp.from_params(params, "r"))
harm.set_time_span(TimeSpan.from_params(params, "ts"))
return harm
@classmethod
def make_random(cls, rnd, npend, syms, rampstop=500):
sym = rnd.choice(syms)
xlimit = ylimit = None
if sym == "X":
xlimit = "odd"
ylimit = "even"
elif sym == "Y":
xlimit = "even"
ylimit = "odd"
elif sym == "R":
xlimit = ylimit = "odd"
harm = cls()
harm.add_dimension(
"x",
[
FullWave.make_random(f"x{abc(i)}", rnd, limit=xlimit)
for i in range(npend)
],
)
harm.add_dimension(
"y",
[
FullWave.make_random(f"y{abc(i)}", rnd, limit=ylimit)
for i in range(npend)
],
)
harm.add_dimension("j", [FullWave.make_random("j", rnd)], extra=True)
harm.add_dimension("k", [FullWave.make_random("k", rnd)], extra=True)
harm.set_ramp(Ramp("r", rampstop))
return harm