interpolate_scalar5.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """Interpolate a 2D surface through a set of points"""
  2. import numpy as np
  3. from scipy.spatial import distance_matrix
  4. from vedo import Grid, Points, Lines, show
  5. np.random.seed(1)
  6. def harmonic_shepard(pts, vals, radius):
  7. dists = distance_matrix(pts, pts) + radius
  8. rdists = 1.0 / dists
  9. sum_vals = np.sum(rdists * vals, axis=1)
  10. return sum_vals / np.sum(rdists, axis=1)
  11. # Create a grid of points
  12. surf = Grid(res=[25,25])
  13. # Pick n random points on the surface
  14. ids = np.random.randint(0, surf.npoints, 10)
  15. pts = surf.coordinates[ids]
  16. # Create a set of random scalars
  17. scals1 = np.random.randn(10) * 0.1
  18. ptss1 = pts.copy()
  19. ptss1[:,2] = scals1 # assign scalars as z-coords
  20. pts1 = Points(ptss1).color("red5").point_size(15)
  21. # Compute an interpolated (smoothed) set of scalars
  22. scals2 = harmonic_shepard(pts, scals1, radius=0.1)
  23. ptss2 = pts.copy()
  24. ptss2[:,2] = scals2
  25. pts2 = Points(ptss2).color("purple5").point_size(15)
  26. # Warp the surface to match the interpolated points
  27. ptsource, pttarget = [], []
  28. for pt in pts2.coordinates:
  29. pt_surf = surf.closest_point(pt)
  30. ptsource.append(pt_surf)
  31. pttarget.append(pt)
  32. warped = surf.warp(ptsource, pttarget, mode='2d')
  33. warped.color("b4").lc('lightblue').wireframe(False).lw(1)
  34. lines = Lines(pts1, pts2, lw=2)
  35. show(pts1, pts2, lines, warped, __doc__, axes=1, viewup="z").close()