Python BlocksΒΆ
Cube coordinates are taken from the surface face coordinates constructed using a minor modification of the Composite of Copies example script. The algorithm ( block_surface function ) to produce the cubed visualization is also used for the Bunny Blocks example.
The inclusion of surface geometric image mapping and shading provided the illusion of height on the flat block planes (high-lighted).
REOM: 2
import copy
import numpy as np
import matplotlib.pyplot as plt
import s3dlib.surface as s3d
#.. Python Blocks
# 1. Define function to examine ....................................
def block_surface(surf,N=10) :
bnds = surf.bounds
ofst = np.array([ bnds['xlim'][0], bnds['ylim'][0],bnds['zlim'][0] ])
rang = np.array([
bnds['xlim'][1]- bnds['xlim'][0],
bnds['ylim'][1]- bnds['ylim'][0],
bnds['zlim'][1]- bnds['zlim'][0]
])
scale = N/ np.amax(rang)
fc = np.array(surf.facecenters).T
fc = (fc-ofst)*scale
coor = fc.astype(int)
coor,cInx = np.unique(coor, return_index=True, axis=0)
return coor.astype(float), cInx
# 2. Setup and map surface .........................................
gs= 27
top = s3d.PlanarSurface.grid(gs,gs)
top.map_color_from_image('data/python.png')
top.map_geom_from_image('data/python_elevation.png',0.07)
top.clip_alpha(0.1)
front = copy.copy(top)
side = copy.copy(top)
bottom = copy.copy(top)
backside = copy.copy(top)
top.transform(translate=[0,0,1])
backfront = copy.copy(top)
bottom.transform(rotate=s3d.eulerRot(0,180), translate=[0,0,-1])
front.transform(rotate=s3d.eulerRot(0,90), translate=[0,-1,0])
backfront.transform(rotate=s3d.eulerRot(180,90), translate=[0,0,0])
side.transform(rotate=s3d.eulerRot(90,90), translate=[1,0,0])
backside.transform(rotate=s3d.eulerRot(-90,90), translate=[-1,0,0])
cube = (top+front+side+bottom+backfront+backside)
cube.shade(0,direction= (0,-1,0.5))
# construct composite of cubes ..
cubeColors = cube.facecolors
cubeCenters,colorInx = block_surface(cube,gs)
unitCubes = None
for i,pos in enumerate( cubeCenters ) :
color = cubeColors[ colorInx[i] ]
cube = s3d.CubicSurface(0,color=color)
cube.transform(scale=0.5,translate=pos)
if unitCubes is None : unitCubes = cube
else : unitCubes += cube
info = 'Number of cube objects in composite: '+str(len(cubeCenters))+'\n'+str(unitCubes)
unitCubes.clip_normals()
unitCubes.shade(0.5,direction= (0,-1,0.5) )
unitCubes.set_edgecolor('k')
unitCubes.set_linewidth(0.1)
# 3. Construct figure, add surface plot ............................
fig = plt.figure(figsize=plt.figaspect(1), facecolor='black')
fig.text(0.975,0.975,info, ha='right', va='top',
fontsize='smaller', multialignment='right', color='white')
ax = plt.axes(projection='3d',aspect='equal')
ax.set_facecolor('black')
ax.set_axis_off()
s3d.auto_scale(ax,unitCubes,uscale=0.9)
ax.add_collection3d(unitCubes)
fig.tight_layout(pad=0)
plt.show()