Volumetric Plot with RGB

This is a comparison to the 3D voxel / volumetric plot with rgb Matplotlib example.

../../_images/vol_plot_rgb.png

S3Dlib does not have a ‘voxel’ object. In this example, a ‘CubicSurface’ object was used to represent a voxel. The method to produce the plot is just to position the set of resized objects to corresponding coordinates. This method was also used to construct the salt unit cell example.

REOM: 1

import numpy as np
from matplotlib import pyplot as plt
import s3dlib.surface as s3d

# 1. Define functions to examine ....................................

def sphereOfCubes(N) :
    inx = np.indices((N,N,N)) - N/2
    radius = int(N/2) - 1
    radialPos = (np.linalg.norm(inx, axis=0) ).astype(int)
    position = []
    for ix in range(N) :
        for iy in range(N) :
            for iz in range(N) :
                if(radialPos[ix,iy,iz]==radius): position.append([ix,iy,iz])
    return  np.array(position,dtype=float)

# 2. Setup and map surfaces .........................................
N=17
cubeCenters = sphereOfCubes(N)
unitCubes = None
for pos in cubeCenters :
    cube = s3d.CubicSurface(0,color='w')
    cube.transform(scale=0.5 , translate=pos-N/2) # unit cubes,  0.0 centered.
    cube.transform(scale=1/(N-1), translate=0.5)  # unit circle, 0.5 centered.
    if unitCubes is None : unitCubes = cube
    else : unitCubes += cube

unitCubes.map_color_from_op(lambda xyz : xyz )   # sphere is in the rgb domain

# 3. Construct figure, add surface, and plot ......................

info = str(unitCubes)+'\nNumber of cube objects in composite: '+str(len(cubeCenters))
fig = plt.figure(figsize=plt.figaspect(1))
fig.text(0.975,0.975,info, ha='right', va='top', fontsize='smaller')
ax = plt.axes(projection='3d', aspect='equal')
ax.set(xlabel='r', ylabel='g', zlabel='b')

s3d.auto_scale(ax,unitCubes)
ax.add_collection3d(unitCubes.shade(direction= (-.5,-1,0.5) ))

plt.show()