Example #1
0
The points in between are interpolated using Bookstein's algorithm.
'''
from vtkplotter import Plotter, thinPlateSpline, Points, Text
import numpy as np
np.random.seed(1)

vp = Plotter(verbose=0)

act = vp.load('data/shuttle.obj')

# pick 4 random points
indxs = np.random.randint(0, act.N(), 4)

# and move them randomly by a little
ptsource, pttarget = [], []
for i in indxs:
    ptold = act.getPoint(i)
    ptnew = ptold + np.random.rand(3) * 0.2
    act.setPoint(i, ptnew)
    ptsource.append(ptold)
    pttarget.append(ptnew)
    # print(ptold,'->',ptnew)

warped = thinPlateSpline(act, ptsource, pttarget)
warped.alpha(0.4).color('b')
# print(warped.info['transform']) # saved here.

apts = Points(ptsource, r=15, c='r')

vp.show([act, warped, apts, Text(__doc__)], viewup='z')
apos = Points(positions, r=2)

#for p in apos.coordinates(): ####### Uncomment to fix some points.
#    if abs(p[2]-5) > 4.999:  # differences btw RBF and thinplate
#        sources.append(p)    # will become much smaller.
#        deltas.append(np.zeros(3))
sources = np.array(sources)
deltas = np.array(deltas)

src = Points(sources, c='r', r=12)
trs = Points(sources + deltas, c='v', r=12)
arr = Arrows(sources, sources + deltas)

################################################# Thin Plate Splines
warped = thinPlateSpline(apos, sources, sources + deltas)
warped.alpha(0.4).color('lg').pointSize(10)
allarr = Arrows(apos.coordinates(), warped.coordinates())

set1 = [apos, warped, src, trs, arr, Text(__doc__, s=1.2)]
vp = show([set1, allarr], N=2, verbose=0)  # returns the Plotter class

################################################# RBF
from scipy.interpolate import Rbf

x, y, z = sources[:, 0], sources[:, 1], sources[:, 2]
dx, dy, dz = deltas[:, 0], deltas[:, 1], deltas[:, 2]

itrx = Rbf(x, y, z, dx)  # Radial Basis Function interpolator:
itry = Rbf(x, y, z, dy)  #  interoplate the deltas in each separate
itrz = Rbf(x, y, z, dz)  #  cartesian dimension