<jupyter_start><jupyter_text>Hardcoded DEMO implementation of the RBF kernel for mesh morphing<jupyter_code># import the required libraries
import numpy as np
import matplotlib.pyplot as pl
# x = np.linspace(-5, 5, 11) 
# # numpy.linspace creates an array  
# # of 9 linearly placed elements between 
# # -4 and 4, both inclusive  
# y = np.linspace(-5, 5, 11) 
# z = np.linspace(-5, 5, 11) 
# # The meshgrid function returns 
# # two 2-dimensional arrays  
# x_1, y_1 ,z_1= np.meshgrid(x, y ,z) 
# Define a simple coordinate mesh grid
nx, ny, nz = (10, 10, 10)
mesh = np.zeros((nx * ny * nz, 3))

xv = np.linspace(0, 1, nx)
yv = np.linspace(0, 1, ny)
zv = np.linspace(0, 1, nz)
z_1, y_1, x_1 = np.meshgrid(zv, yv, xv)

# mesh = np.array([x.ravel(), y.ravel(), z.ravel()])
# mesh = mesh.T
# flatten the arrays
num_coor=len(x_1.flatten())
coor=np.zeros((num_coor,3))
coor[:,0]=x_1.flatten()
coor[:,1]=y_1.flatten()
coor[:,2]=z_1.flatten()
# plot in matplotlib
fig = pl.figure(1)
ax = fig.add_subplot(111, projection='3d')
ax.scatter(coor[:, 0], coor[:, 1], coor[:, 2], c='blue', marker='o')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
pl.show()

# inputs
N=8
ctr_pts=np.zeros((N,3))
ctr_pts[0,:]=(-0.1,   0.0,   0.0)
ctr_pts[1,:]= (0.0 ,  0.0 ,  1.0)
ctr_pts[2,:]= (0.0  , 1.0  , 0.0)
ctr_pts[3,:]= (1.0   ,0.0   ,0.0)
ctr_pts[4,:]= (0.0,   1     ,1.0)
ctr_pts[5,:]= (1.0 ,  0.0  , 1.0)
ctr_pts[6,:]= (1.0  , 1.0 ,    0)
ctr_pts[7,:]= (1.0   ,1.0,   1.0)
g=np.zeros((N,3))
g[0,:]= (0.1,  0.2,   0.3)
g[1,:]= (0.0 ,  0.0 ,  1.0)
g[2,:]= (0.0  , 1.0  , 0.0)
g[3,:]= (1.0   ,0.0   ,0.0)
g[4,:]= (0.0,   .8     ,1.0)
g[5,:]= (1.0 ,  0.0  , 1.0)
g[6,:]= (1.0  , 1.0 ,  0)
g[7,:]= (1.2   ,1.2,   1.2)

N=8
# defining  P matrix from the control points 
P=np.zeros((N,4))
for i in range(N):
    for j in range(4):
        if (j==0): P[i,j]=1
        else: P[i,j]=ctr_pts[i,j-1]
# known displacements

P
# define the eucledian distance
def rd(c1, c2):
    return np.sqrt((c1[0]-c2[0])**2+(c1[1]-c2[1])**2+(c1[2]-c2[2])**2)
#rbf as global support spline type
def rbf(r):
    return np.exp(-r**2)
## define the lhs of the rbf kernel
def M(coor,ctr_pts):
    M_m=np.zeros((N,N))
    for i in range(N):
        for j in range(N):
            M_m[i,j]=rbf(rd(ctr_pts[i],ctr_pts[j]))
    return M_m
# formulate the lhs of the system of eqation to be solved 
def lhs_(P,M_m):
    lhs=np.zeros((N+4,N+4))
    P_t=np.transpose(P)
    for i in range(N+4):
        for j in range(N+4):
            if (i<N and j<N): lhs[i,j]=M_m[i,j]
            elif (i<N and j>(N-1)): lhs[i, j]=P[i,j-N]
            elif (i>(N-1) and j<N):lhs[i,j]= P_t[i-N,j]
    return lhs
# solve for the coefficients 
def sol_yb(lhs, g):
    rhs=np.zeros((N+4,3))
    for i in range(N):
        for j in range(3):    
            rhs[i,j]=g[i,j]
    
    inv_lhs=np.linalg.inv(lhs)
    yb= inv_lhs @ rhs
    gmma=np.zeros((N,3))
    beta=np.zeros((4,3))
    for i in range(N+4):
        if (i<N): gmma[i,:]=yb[i,:]
        else : beta[i-N,:]=yb[i,:]
    return (gmma, beta)
                

# get the new points from the old points 
def s_x(ctr_pts, coor, beta, gmma,deg):
    s_x=np.zeros(3)
    for i in range(3):
        for j in range(N):
            s_x[i]=s_x[i]+gmma[j,i]*rbf(rd(coor, ctr_pts[j]),deg)
        s_x[i]=s_x[i]+beta[0,i]*1 + beta[1,i]*coor[0] + coor[1]*beta[2,i] + coor[2]*beta[3,i]
    return s_x<jupyter_output><empty_output><jupyter_text>Calling the required functions and solving in order for individual points<jupyter_code>M_m=M(coor,ctr_pts)
# M_m

lhs=lhs_(P,M_m)
# lhs

gmma, beta=sol_yb(lhs, g)

coor_new=np.zeros((num_coor,3))

cor_t=(1.0,1.0,1.0)
s_x(ctr_pts, cor_t, beta, gmma,deg)

for i in range(num_coor):
    coor_new[i,:]=coor[i,:]+s_x(ctr_pts, coor[i], beta, gmma,deg)
coor_new# new coordinates
# plot in matplotlib
fig = pl.figure(1)
ax = fig.add_subplot(111, projection='3d')
ax.scatter(coor_new[:, 0], coor_new[:, 1], coor_new[:, 2], c='blue', marker='o')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
pl.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21355185909980431, 'fraction_numerical': 0.0547945205479452, 'mean_word_length': 3.308746048472076, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '478320', 'license': 'MIT', 'n_tokens_mistral': 2066, 'n_tokens_neox': 2048, 'n_words': 428, 'path': 'RBF_form-Copy1.ipynb', 'repo_name': 'utkarshmech/rbfmorph', 'size': '4088'}