Surface Face Distribution DualCmapΒΆ

../../_images/swirl_dualcmap.png
import numpy as np
from matplotlib import pyplot as plt
import s3dlib.surface as s3d
import s3dlib.cmap_utilities as cmu

#.. Dualcmap for polygon face attributes
# 1. Define function to examine .....................................

kr = cmu.hsv_cmap_gradient( [0.00,1,0], [0.00,1,1],name='black_red' )
kg = cmu.hsv_cmap_gradient( [0.33,1,0], [0.33,1,1],name='black_green' )
cmap2d = cmu.DualCmap(kr,kg)
# ............................................................
def swirl(rtp) :
    r,t,p = rtp
    u,v = t,p
    R = 2 + np.sin(7*u + 5*v)
    return R,t,p

# 2. Setup and map surfaces .........................................
mlt = 20

surface = s3d.SphericalSurface.grid(5*mlt,7*mlt)
surface.map_geom_from_op(lambda c: [2+np.sin(7*c[1]+5*c[2]),c[1],c[2]] )
data = np.array(surface.area_h2b)     #     NOTE: number of faces = N ------
dualmap = lambda c : cmap2d(*data)    # <-- rtp.shape=(3,N) & data.shape=(2,N)
surface.map_color_from_op( dualmap )  #     so 'works' since same N for both,
                                      #     thus results in colors.shape=(4,N)
N = 200
x=np.linspace(0,1,N)
y=np.linspace(0,1,N)
z = cmap2d(*np.meshgrid(x, y)).T

# 3. Construct figures, add surface, plot ...........................
info = str(surface) +'\n' + str(cmap2d)
minmax = (-0.5*N,1.5*N)
fig = plt.figure(figsize=(8,4))
fig.text(0.975,0.975,info, ha='right', va='top', fontsize='smaller', multialignment='right')

ax1 = fig.add_subplot(121, projection='3d')
fig.text(0.45,0.85,'Triangular Face Attributes', ha='left', 
    va='top', fontsize='xx-large', multialignment='right')
ax1.set_axis_off()
s3d.auto_scale(ax1,surface,uscale=0.6)
ax1.add_collection3d(surface.shade())

ax2 = fig.add_subplot(122)
fig.text(0.75,0.23,'Relative Area', ha='center', va='center', fontsize='x-large')
fig.text(0.61,0.51,'Shape', ha='center', va='center', fontsize='x-large', rotation=90)
ax2.set(xlim=minmax, ylim=minmax )
ax2.set_axis_off()
ax2.imshow(z)
fig.tight_layout()
plt.show()