-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow-fields.py
340 lines (268 loc) · 11.8 KB
/
flow-fields.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
335
336
337
338
339
340
import time
import uuid
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.lines import Line2D
from functools import partial
from perlin2D import generate_fractal_noise_2d, generate_perlin_noise_2d
class Line(Line2D):
"""Custom line class.
The line width is defined in the same units as the plotted data.
E.g. a line of length 1.0 and width 1.0 looks like a square.
In contrast, the width of a line plotted with plt.plot() is defined
in points.
"""
def __init__(self, *args, **kwargs):
_lw_data = kwargs.pop("linewidth", 1)
super().__init__(*args, **kwargs)
self._lw_data = _lw_data
def _get_lw(self):
if self.axes is not None:
ppd = 72./self.axes.figure.dpi
trans = self.axes.transData.transform
return ((trans((1, self._lw_data))-trans((0, 0)))*ppd)[1]
else:
return 1
def _set_lw(self, lw):
self._lw_data = lw
_linewidth = property(_get_lw, _set_lw)
def lerp(a, b, x):
"""Linear interpolation."""
return a + x * (b - a)
def map_range(x, out_min, out_max):
"""Linearly map the range of x from min(x), max(x) to out_min, out_max."""
in_min, in_max = x.min(), x.max()
return (x - in_min) / (in_max - in_min) * (out_max - out_min) + out_min
def round_to_nearest(x, a):
"""Round values of x to nearest multiple of a."""
return np.round(x / a) * a
def curl(field, x, y, eps=0.001):
"""Computes curl of a 2D vector field."""
# grad = np.gradient(field(x, y))
# curl = np.array([grad[0], -grad[1]])
x_comp = (field(x, y + eps) - field(x, y - eps)) / (2 * eps);
y_comp = - (field(x + eps, y) - field(x - eps, y)) / (2 * eps);
curl = np.array([x_comp, - y_comp])
return curl
def get_velocity(point, field, bounds, interpolate):
"""Get velocity of field at query point (x, y).
Linearly interpolates between grid cells if interpolate=True.
Otherwise returns vector of closest grid cell.
"""
x_min, y_min, x_max, y_max = bounds
h, w = field.shape[0:2]
i = (point[0] - x_min) / (x_max - x_min) * (w - 1)
j = (point[1] - y_min) / (y_max - y_min) * (h - 1)
cell_x = int(i)
cell_y = int(j)
frac_x = i % 1
frac_y = j % 1
if interpolate:
vel = lerp(lerp(field[cell_y, cell_x ], field[cell_y , cell_x + 1], frac_x),
lerp(field[cell_y, cell_x + 1], field[cell_y + 1, cell_x + 1], frac_x),
frac_y)
else:
vel = field[cell_y, cell_x]
return vel
def is_collision(point, width, lines, widths, safety_fac=1.5):
"""Check if point collides with points of another line."""
if not lines:
return False
margin = 0.0 # Fixed margin between curves, optional
# Array with line width of every point on every line
widths_all_points = np.repeat(widths, [line.shape[0] for line in lines])
all_points = np.vstack(lines)
# Squared distance between query point and points of all the other lines
dist = np.sum((all_points - point)**2, axis=1)
is_collision = dist < (((widths_all_points + width + margin) / 2)**2) * safety_fac
return np.any(is_collision)
def out_of_bounds(point, width, bounds):
"""Check if point is out of bounds."""
x_min, y_min, x_max, y_max = bounds
r = width / 2
out_of_bounds = ( point[0] - r < x_min
or point[0] + r > x_max
or point[1] - r < y_min
or point[1] + r > y_max)
return out_of_bounds
def generate_width_dist(name, max_width):
"""Generate a function f(x) that returns a line width.
Typically x is proportional to the number of lines already generated.
"""
# width = (0.05 - 0.005) * ((n_max_lines - len(lines)) / n_max_lines)**5 + 0.005
width_dist = None
if name == "uniform":
width_dist = partial(uniform_width, width=max_width)
elif name == "decreasing":
width_dist = partial(decreasing_width, max_width=max_width)
return width_dist
def uniform_width(x, width):
return width
def decreasing_width(x, max_width):
"""Start with thick lines and gradually decrease width."""
width = 2 / (x / 20 + 2) * max_width * (np.random.rand(1) * 0.5 + 0.5)
return width
def trace_line(start_point, width, lines, widths, field, bounds, max_len, interpolate, step_size):
"""Trace single flow line of vector field."""
line = [start_point]
n_points = int(max_len / 2 / step_size)
# Trace forward
for _ in range(n_points):
point = line[-1]
vel = get_velocity(point, field, bounds, interpolate)
point_new = point + vel * step_size
if is_collision(point_new, width, lines, widths) or out_of_bounds(point_new, width, bounds):
break
line.append(point_new)
# Trace backward
line.reverse()
for _ in range(n_points):
point = line[-1]
vel = get_velocity(point, field, bounds, interpolate)
point_new = point - vel * step_size
if is_collision(point_new, width, lines, widths) or out_of_bounds(point_new, width, bounds):
break
line.append(point_new)
return np.array(line)
def trace_field(field, bounds, max_len, width_dist, n_max_lines, interpolate, step_size):
"""Trace flow lines of vector field."""
lines = []
widths = []
max_attempts = 1000 # Max attempts at finding a valid starting point
for i in range(n_max_lines):
width = width_dist(i)
width = max(float(width), step_size / 5)
# Try to generate valid starting point for new line
start_point_valid = False
for _ in range(max_attempts):
start_point = np.random.uniform(bounds[0:2], bounds[2:4])
if not ( is_collision(start_point, width, lines, widths)
or out_of_bounds(start_point, width, bounds)):
start_point_valid = True
break
if start_point_valid:
line = trace_line(start_point, width, lines, widths, field, bounds, max_len, interpolate, step_size)
lines.append(line)
widths.append(width)
else:
break
return lines, widths
def generate_field(n_cells, min_angle, max_angle, round_to=0):
"""Generate vector field with n_cells."""
noise = generate_fractal_noise_2d((n_cells, n_cells),
(1, 1),
octaves=2,
persistence=0.5)
angle_field = map_range(noise, min_angle, max_angle)
if round_to:
angle_field = round_to_nearest(angle_field, round_to)
field = np.stack([np.cos(angle_field), np.sin(angle_field)], axis=2)
return field
def fig_setup(col_bg="#ffffff"):
"""Set up figure for plotting."""
fig = plt.figure(figsize=(8, 8))
ax = plt.Axes(fig, [0, 0, 1, 1], facecolor=col_bg)
fig.add_axes(ax)
ax.set_xlim([-0.1, 1.1])
ax.set_ylim([-0.1, 1.1])
# Remove frame but keep background color
for s in ax.spines.values():
s.set_visible(False)
return fig, ax
def plot(lines, widths, col_lines, col_bg, end_splits, splits_start, step_size, style="lines"):
"""Main plotting function."""
fig, ax = fig_setup(col_bg)
colors = np.random.choice(col_lines, len(lines))
for line, width, color in zip(lines, widths, colors):
# Linearly interpolate curve to generate more interesting end segments
# Repeat every but last point of curve n_int times.
# Add scaled difference between consecutive points to repeated array
# to obtain interpolated curve.
n_int = 10
diff = np.repeat(np.diff(line, axis=0), n_int, axis=0) / n_int
int_step = np.tile(np.arange(n_int), line.shape[0] - 1)[:, np.newaxis] * diff
line_int = np.repeat(line, n_int, axis=0)[:-n_int] + int_step
line = line_int
if style == "lines":
line_len = line.shape[0]
if end_splits:
main_line_len = int(line_len * (np.random.rand(1) * (1 - splits_start - 0.05) + splits_start))
if line_len - main_line_len >= 2:
n_splits = min(line_len - main_line_len, end_splits + 1)
# Randomly select splits between end of main line and end of overall line
ids = np.sort(np.random.choice(range(main_line_len, line_len), n_splits, replace=False))
# Ensure first split starts at the end of main line and last split
# ends at the very end of the overall line
ids[[0, -1]] = [main_line_len, line_len + 1]
# Generate slice for each split segment such that they overlap at
# split point.
slices = [slice(ids[i], ids[i+1] + 1) for i in range(len(ids) - 1)]
for s in reversed(slices):
line_plot = Line(line[s, 0], line[s, 1],
color=np.random.choice(col_lines, 1)[0],
linewidth=width,
solid_capstyle="round")
ax.add_line(line_plot)
else:
line_plot = Line(line[main_line_len:, 0], line[main_line_len:, 1],
color=np.random.choice(col_lines, 1)[0],
linewidth=width,
solid_capstyle="round")
ax.add_line(line_plot)
else:
main_line_len = line_len
line_plot = Line(line[:main_line_len + 1, 0], line[:main_line_len + 1, 1],
linewidth=width,
color=color,
solid_capstyle="round")
ax.add_line(line_plot)
if style == "dots":
step = int( width / step_size) + 1
for point in line[::step]:
circle = plt.Circle(tuple(point), width / 2, color=color)
ax.add_patch(circle)
return fig
def unique_file_name(name, rnd_len=10):
"""Generate unique file name."""
file_name = name + "-" + str(uuid.uuid4().hex)[:rnd_len]
return file_name
def date_file_name(name):
"""Generate file name with date suffix."""
time_str = time.strftime("%Y%m%d-%H%M%S")
return name + "_" + time_str
color_palettes = {
"autumn": [ "#03071e", "#370617", "#6a040f", "#9d0208", "#d00000", "#dc2f02",
"#e85d04", "#f48c06", "#faa307", "#ffba08"],
"violet-red": ["#ea698b","#d55d92","#c05299","#ac46a1","#973aa8","#822faf",
"#6d23b6","#6411ad","#571089","#47126b"],
"blue-orange": ["#8ecae6", "#73bfdc", "#58b4d1", "#219ebc", "#126782", "#023047",
"#ffb703", "#fd9e02", "#fb8500", "#fb9017"],
"blue-berry": ["#b7094c", "#a01a58", "#892b64", "#723c70", "#5c4d7d", "#455e89",
"#2e6f95", "#1780a1", "#0091ad"],
"black-white": ["#000000", "#ffffff"],
"dark": [ "#111111", "#222222"]
}
if __name__ == "__main__":
# Hyper parameters
step_size = 0.005
max_len = 0.3
bounds = [0, 0, 1, 1]
max_width = 0.08
n_max_lines = 500
interpolate = True
n_cells = 200
end_splits = 7
splits_start = 0.4
style = "lines"
col_lines = color_palettes["blue-berry"]
col_bg = "#010101"
field = generate_field(n_cells, -np.pi, np.pi, round_to=0)
width_dist = generate_width_dist("decreasing", max_width)
t_start = time.perf_counter()
lines, widths = trace_field(field, bounds, max_len, width_dist, n_max_lines, interpolate, step_size)
t_end = time.perf_counter()
print(f"Ellapsed time: {t_end - t_start: .2f} s")
fig = plot(lines, widths, col_lines, col_bg, end_splits, splits_start, step_size, style="lines")
plt.show()
file = date_file_name("flow-field") + ".jpg"
fig.savefig(file, dpi=150)