warp1.py 999 B

12345678910111213141516171819202122232425262728
  1. """Fit a surface to a set of points"""
  2. # Thin Plate Spline transformations describe a nonlinear warp
  3. # transform defined by a set of source and target landmarks.
  4. # Any point on the mesh close to a source landmark will
  5. # be moved to a place close to the corresponding target landmark.
  6. # The points in between are interpolated using Bookstein's algorithm.
  7. from vedo import Grid, Points, Arrows, show
  8. import numpy as np
  9. np.random.seed(1)
  10. surf = Grid([0,0,0], res=[25,25])
  11. ids = np.random.randint(0, surf.npoints, 10) # pick 10 indices
  12. pts = surf.points[ids]
  13. ptsource, pttarget = [], []
  14. for pt in pts:
  15. pt1 = pt + [0, 0, np.random.randn() * 0.1]
  16. pt2 = surf.closest_point(pt1)
  17. ptsource.append(pt2)
  18. pttarget.append(pt1)
  19. warped = surf.warp(ptsource, pttarget, mode='2d')
  20. warped.color("b4").lc('lightblue').lw(1).wireframe(False)
  21. apts = Points(pttarget).point_size(15).c("red5")
  22. arrs = Arrows(ptsource, pttarget).c("black")
  23. show(warped, apts, arrs, __doc__, axes=1, viewup="z").close()