import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm,colors
import s3dlib.surface as s3d
import s3dlib.pntcloud as ptc
# 1. generate data samples with values ............................
N,seed,val_mult,domain = 50, 6, 25.0, [ [-7,6], [5,30], [0,50] ]
xlim,ylim,zlim = domain
np.random.seed(seed)
xc = np.random.uniform(*xlim, N)
yc = np.random.uniform(*ylim, N)
zc = np.random.uniform(*zlim, N)
dt_coor = np.column_stack((xc,yc,zc))
vals = val_mult*np.random.uniform(size=N)
vmin,vmax,vmid = np.min(vals),np.max(vals), ( np.min(vals) + np.max(vals) )/2
points = np.column_stack((dt_coor,vals))
xmin,xmax,ymin,ymax,zmin,zmax = np.array(domain).flatten()
zmid = 0.5*(zmin+zmax)
verts = [ [xmax,ymin,zmin], [ xmax,ymax,zmid ], [ xmin,ymax,zmax], [ xmin,ymin,zmid ] ]
faces =[ [0,1,2,3]]
# 2. Setup and map point cloud ....................................
drez,trez,cmap = 3, 6, 'jet'
sphere_pts = s3d.samples_to_object(points, domain, cmap, size=0.75)
cloudObj = ptc.Point3DCloud(drez,name='scaled',color='green').domain(domain)
cloudObj.map_vals_from_sampvals(points, vadj='c', cmap=cmap)
surface = s3d.Surface3DCollection(verts,faces).triangulate(trez)
surface.map_color_from_cloud(cloudObj)
# 3. Construct figures, add surfaces, and plot ....................
fig = plt.figure(figsize=(8,4))
# ....................
title = str(cloudObj) +'\n'+ str(surface)
fig.text(0.25,0.9,title, ha='center', fontsize='small')
ax = fig.add_subplot(121, projection='3d', aspect='equal')
ax.view_init(20)
ax.set(xlim=xlim, ylim=ylim, zlim=zlim, xlabel='x',ylabel='y')
ax.add_collection3d( surface+sphere_pts.shade(.5,ax=ax) )
cbar = plt.colorbar(surface.cBar_ScalarMappable, ax=ax, shrink=0.6, pad=0.1 )
cbar.set_label('surface values', rotation=270, labelpad = 15)
# ....................
title = 'sample size : {}\nrange: {:.2f} - {:.2f}'.format(N,vmin,vmax)
fig.text(.75,0.9,title,ha='center', fontsize='small')
ax = fig.add_subplot(122, projection='3d', aspect='equal')
ax.view_init(20)
ax.set(xlim=xlim, ylim=ylim, zlim=zlim, xlabel='x',ylabel='y')
ax.scatter(*dt_coor.T, s=50, c=vals, cmap=cmap ,edgecolor='k')
norm = colors.Normalize(np.min(vals),np.max(vals) )
cbar =fig.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap), ax=ax,shrink=0.6, pad=.1)
cbar.set_label('sample values', rotation=270, labelpad = 15)
# ....................
fig.tight_layout(pad=1)
plt.show()