moving_least_squares1D.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. """1D Moving Least Squares (MLS)
  2. to project a cloud of unordered points
  3. to become a smooth, ordered line"""
  4. from vedo import *
  5. settings.default_font = "Antares"
  6. N = 3 # nr. of iterations
  7. # build some initial cloud of noisy points along a line
  8. pts = [(sin(6*x), cos(2*x)*x, cos(9*x)) for x in np.arange(0,2, 0.001)]
  9. # pts = [(0, sin(x), cos(x)) for x in np.arange(0,6, .002)]
  10. # pts = [(sqrt(x), sin(x), x/5) for x in np.arange(0, 16, 0.01)]
  11. pts += np.random.randn(len(pts), 3) / 15 # add noise
  12. np.random.shuffle(pts) # make sure points are not ordered
  13. pts = Points(pts, r=5)
  14. plt = Plotter(N=N, axes=1)
  15. plt.at(0).show(pts, __doc__, viewup='z')
  16. for i in range(1, N):
  17. pts = pts.clone().smooth_mls_1d(n=50).color(i)
  18. if i == N-1:
  19. # at the last iteration make sure points
  20. # are separated by tol (in % of the bounding box)
  21. pts.subsample(0.025)
  22. plt.at(i).show(pts, f"Iteration {i}, #points: {pts.npoints}")
  23. line = pts.generate_segments().clean().join()
  24. # printc("lines:", line.lines)
  25. plt += [line, line.labels("id").bc("blue5")]
  26. plt.interactive().close()