Created
September 9, 2018 13:37
-
-
Save frostburn/de08eaa013110d1ff5ffba3db1fcae04 to your computer and use it in GitHub Desktop.
Visualizing a quaternion density cloud
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import quaternion | |
import scipy.misc | |
import multiprocessing | |
t = 0 | |
def project_cloud(seed): | |
np.random.seed(seed) | |
cloud = np.random.randn(1000000, 4) * 0.2 | |
cloud[:, 0] = 0 | |
cloud = quaternion.as_quat_array(cloud) | |
c = np.quaternion(0, 1, 0.5, -t) * 0.05 | |
d = np.quaternion(0, 0.2 + 2 * t, -3, 1) * 0.05 | |
results = [] | |
z = cloud | |
for i in range(16): | |
r = (1 + z) ** 8 | |
z = r*(z + d)/r + c | |
results.append(z) | |
width = 640 | |
height = 360 | |
center_x = 0 | |
center_y = 0 | |
transform = np.quaternion(1, 3, 2, 1) | |
transform /= abs(transform) | |
transform **= 4.7979 * t | |
transform *= 1.2 | |
imgs = [] | |
for z in results: | |
z -= np.quaternion(0, center_y, center_x, 0) | |
z *= transform | |
z = quaternion.as_float_array(z) | |
img = np.histogram2d(z[:, 1], z[:, 2], range=[(-height/width, height/width), (-1, 1)], bins=[height, width])[0] | |
imgs.append(img) | |
return imgs | |
num_frames = 256 | |
for n in range(num_frames): | |
t = n / num_frames | |
p = multiprocessing.Pool(14) # New pool to sync global t | |
imgss = p.map(project_cloud, range(14 * 4)) | |
p.close() | |
r = 0 | |
g = 0 | |
b = 0 | |
for imgs in imgss: | |
r += imgs[-6] | |
g += imgs[-3] | |
b += imgs[-1] | |
weight = (r.mean() + g.mean() + b.mean()) * 9 | |
r /= weight | |
g /= weight | |
b /= weight | |
img = np.array([r, g, b]) | |
filename = 'imgs/density_{0:05d}.png'.format(n) | |
print('Saving', filename) | |
scipy.misc.toimage(img, cmin=0, cmax=1).save(filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment