Skip to content

Instantly share code, notes, and snippets.

@mtesseracted
Created March 2, 2018 19:14
Show Gist options
  • Save mtesseracted/f03373b1acd81874fe138d395ad82761 to your computer and use it in GitHub Desktop.
Save mtesseracted/f03373b1acd81874fe138d395ad82761 to your computer and use it in GitHub Desktop.
Rotating NumPy logo in voxels
'''
=====================================
Rotating 3D voxel animation of PYTHON
=====================================
Demonstrates using ``ax.voxels`` with uneven coordinates
'''
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as manimation
from math import copysign
def explode(data):
size = np.array(data.shape)*2
data_e = np.zeros(size - 1, dtype=data.dtype)
data_e[::2, ::2, ::2] = data
return data_e
def voxel_face(corns, dm, nf, offst=0.0):
'''
Grab the corner coordinates of one voxel face
Parameters
----------
corns : np.indices array of corners for one voxel
dm : (dimension), values can be 0(x), 1(y), 2(z)
nf : (near/far face), values can be 0(near), 1(far)
offst: how much to offset the face size, also offsets at
1/10th that amount in the perpendicular direction
'''
lc = corns.copy() #local copy so we don't swap original
if dm == 1 : #swap y into x and correct ordering
lc[0], lc[1] = corns[1].transpose(1,0,2), corns[0].transpose(1,0,2)
if dm == 2 : #swap z into x and correct ordering
lc[0], lc[2] = corns[2].transpose(2,1,0), corns[0].transpose(2,1,0)
ret = np.zeros((3,2,2))
xc1 = lc[0,nf,0,0] #hold x dim constant
xc1 += offst * (2*nf - 1) / 10.0 #offset a little
ret[0,:] = np.array([[xc1, xc1], [xc1, xc1]])
yc1, yc2 = lc[1,0,0:2,0]
yc1 += offst #shrink to show voxel edges
yc2 -= offst
ret[1,:] = np.array([[yc1, yc2], [yc1, yc2]])
zc1, zc2 = lc[2,0,0,0:2]
zc1 += offst #shrink to show voxel edges
zc2 -= offst
ret[2,:] = np.array([[zc1, zc1], [zc2, zc2]])
if dm != 0 : #swap x back into desired dimension
ret[0], ret[dm] = ret[dm].copy(), ret[0].copy()
return ret
# build PYTHON letters
n_voxels = np.zeros((5, 4, 5), dtype=bool)
letters = [None]*5
lett_face = np.zeros((6,2),dtype=int)
#N
n_voxels[0, 0, :] = True
n_voxels[-1, 0, :] = True
n_voxels[1, 0, -2] = True
n_voxels[2, 0, -3] = True
n_voxels[3, 0, -4] = True
letters[0] = np.array(np.where(n_voxels)).T
lett_face[0] = [1, 0] #close y face
n_voxels[...] = False
#U
n_voxels[0, 0, :] = True
n_voxels[0, :, 0] = True
n_voxels[0, -1, :] = True
letters[1] = np.array(np.where(n_voxels)).T
lett_face[1] = [0, 0] #close x face
n_voxels[...] = False
#M
n_voxels[0, :, -1] = True
n_voxels[-1, : , -1] = True
n_voxels[1, -2, -1] = True
n_voxels[2, -3, -1] = True
n_voxels[3, -2, -1] = True
letters[2] = np.array(np.where(n_voxels)).T
lett_face[2] = [2, 1] #far z face
n_voxels[...] = False
#P
n_voxels[-1, 0, :] = True #redo of N far bar
n_voxels[-1, :, 2] = True
n_voxels[-1, :, -1] = True
n_voxels[-1, -1, -2] = True
letters[3] = np.array(np.where(n_voxels)).T
lett_face[3] = [0, 1] #far x face
n_voxels[...] = False
#Y
n_voxels[0, :, 0] = True
n_voxels[2, :, 0] = True
n_voxels[3:, 0, 0] = True
n_voxels[:, -1, 0] = True
letters[4] = np.array(np.where(n_voxels)).T
lett_face[4] = [2, 0] #close z face
n_voxels[...] = False
fcol = np.full(n_voxels.shape, '#909cd540')
#739ec040
#7a88cc
ecol = np.full(n_voxels.shape, '#7D84A6')
filled = np.ones(n_voxels.shape)
# upscale the above voxel image, leaving gaps
filled_2 = explode(filled)
fcolors_2 = explode(fcol)
ecolors_2 = explode(ecol)
# Shrink the gaps
corn = np.indices(np.array(filled_2.shape) + 1).astype(float) // 2
ccorn = 0.05 #close corner
fcorn = 1.0 - ccorn
corn[0,0::2, :, :] += ccorn
corn[1,:, 0::2, :] += ccorn
corn[2,:, :, 0::2] += ccorn
corn[0,1::2, :, :] += fcorn
corn[1,:, 1::2, :] += fcorn
corn[2,:, :, 1::2] += fcorn
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.axis("off")
#Plot the voxels
x, y, z = corn
ax.voxels(x, y, z, filled_2, facecolors=fcolors_2, edgecolors=ecolors_2)
#Plot the letter square faces
jj=0
myo = 0.005 #my face offset
for j in [x for x in letters if x is not None]:
locf = np.empty((j.shape[0],3,2,2)) #local face
ji = 0
for i in j:
i = i * 2 #skip empty voxels
loc = corn[:,i[0]:i[0]+2,i[1]:i[1]+2,i[2]:i[2]+2] #local corners
locf[ji] = voxel_face(loc, lett_face[jj,0], lett_face[jj,1], myo)
ax.plot_surface(locf[ji,0],locf[ji,1],locf[ji,2],color='#ffd030d0',
shade=False)
#ffe749
#ffe500
ji += 1
jj += 1
#plt.show()
#'''
ssp = 8 #small step
bsp = 40 #big step
sps = 15 #short pause
#Views: NP, N, U, M, P, Y, NP
view_elev = [ 5, 0, 0, 90, 0, -90, 5]
view_azim = [-45, -90, -180, -90, 0, 0, -45]
view_step = [ssp, bsp, bsp, bsp, bsp, bsp, bsp]
view_paus = [ 40, sps, sps, sps, sps, sps, sps]
FFMpegWriter = manimation.writers['ffmpeg']
metadata = dict(title='Movie Test', artist='Matplotlib',
comment='Movie support!')
writer = FFMpegWriter(fps=25, metadata=metadata)
with writer.saving(fig, "numpyRot1.mp4", 100):
for i in range(1,len(view_elev)):
de = (view_elev[i] - view_elev[i-1])
da = (view_azim[i] - view_azim[i-1])
if abs(da) >= 180 : #unecessary in this config
da -= copysign(360, da)
if abs(de) >= 180 :
de -= copysign(360, de)
steps = view_step[i-1]
da = da / steps
de = de / steps
for j in range(view_paus[i-1]): #Pause on direct view of a letter
ax.view_init(view_elev[i-1], view_azim[i-1])
plt.draw()
writer.grab_frame()
for j in range(steps): #Rotate to next letter
ax.view_init(view_elev[i-1] + j*de,
view_azim[i-1] + j*da)
plt.draw()
writer.grab_frame()
#'''
@mtesseracted
Copy link
Author

mtesseracted commented Mar 2, 2018

Output:
img

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment