moving_least_squares2D.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. """Use a variant of the Moving Least Squares (MLS)
  2. algorithm to project a cloud of points to become a smooth surface.
  3. In the second window we show the error estimated for
  4. each point in color scale (left) or in size scale (right)."""
  5. from vedo import *
  6. printc(__doc__, invert=1)
  7. plt1 = Plotter(N=3, axes=1)
  8. mesh = Mesh(dataurl+"bunny.obj").normalize().subdivide()
  9. pts = mesh.coordinates
  10. pts += np.random.randn(len(pts), 3)/20 # add noise, will not mess up the original points
  11. #################################### smooth cloud with MLS
  12. # build the mesh points
  13. s0 = Points(pts, r=3).color("blue")
  14. plt1.at(0).show(s0, "original point cloud + noise")
  15. # project s1 points into a smooth surface of points
  16. # The parameter f controls the size of the local regression.
  17. mls1 = s0.clone().smooth_mls_2d(f=0.5)
  18. plt1.at(1).show(mls1, "MLS first pass, f=0.5")
  19. # mls1 is an Assembly so unpack it to get the first object it contains
  20. mls2 = mls1.clone().smooth_mls_2d(radius=0.1)
  21. plt1.at(2).show(mls2, "MLS second pass, radius=0.1")
  22. #################################### draw errors
  23. plt2 = Plotter(pos=(300, 400), N=2, axes=1)
  24. variances = mls2.pointdata["MLSVariance"]
  25. vmin, vmax = np.min(variances), np.max(variances)
  26. print("min and max of variances:", vmin, vmax)
  27. vcols = [color_map(v, "jet", vmin, vmax) for v in variances] # scalars->colors
  28. sp0 = Spheres(mls2.coordinates, c=vcols, r=0.02) # error as color
  29. sp1 = Spheres(mls2.coordinates, c="red5", r=variances/4) # error as point size
  30. mesh.color("k").alpha(0.05).wireframe()
  31. plt2.at(0).show(sp0, "Use color to represent variance")
  32. plt2.at(1).show(sp1, "point size to represent variance", zoom=1.3, interactive=True)
  33. plt2.close()
  34. plt1.close()