Skip to content

Commit

Permalink
add examples/advanced/interpolate_scalar5.py
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomusy committed Sep 15, 2023
1 parent e16577f commit b42b55c
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions examples/advanced/interpolate_scalar5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""Interpolate a 2D surface through a set of points"""
import numpy as np
from scipy.spatial import distance_matrix
from vedo import Grid, Points, Lines, show
np.random.seed(1)

def harmonic_shepard(pts, vals, radius):
dists = distance_matrix(pts, pts) + radius
rdists = 1.0 / dists
sum_vals = np.sum(rdists * vals, axis=1)
return sum_vals / np.sum(rdists, axis=1)

# Create a grid of points
surf = Grid(res=[25,25])

# Pick n random points on the surface
ids = np.random.randint(0, surf.npoints, 10)
pts = surf.points(ids)

# Create a set of random scalars
scals1 = np.random.randn(10) * 0.1

ptss1 = pts.copy()
ptss1[:,2] = scals1 # assign scalars as z-coords
pts1 = Points(ptss1, r=15, c="red5")

# Compute an interpolated (smoothed) set of scalars
scals2 = harmonic_shepard(pts, scals1, radius=0.1)
ptss2 = pts.copy()
ptss2[:,2] = scals2
pts2 = Points(ptss2, r=15, c="purple5")

# Warp the surface to match the interpolated points
ptsource, pttarget = [], []
for pt in pts2.points():
pt_surf = surf.closest_point(pt)
ptsource.append(pt_surf)
pttarget.append(pt)
warped = surf.warp(ptsource, pttarget, mode='2d')
warped.color("b4").lc('lightblue').wireframe(False).lw(1)

lines = Lines(pts1, pts2, lw=2)

show(pts1, pts2, lines, warped, __doc__, axes=1, viewup="z").close()

0 comments on commit b42b55c

Please sign in to comment.