-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add examples/advanced/interpolate_scalar5.py
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|