spline_tool.py 991 B

12345678910111213141516171819202122232425262728293031323334
  1. """Modify a spline interactively.
  2. - Drag points with mouse
  3. - Add points by clicking on the line
  4. - Remove them by selecting&pressing DEL
  5. --- PRESS q TO PROCEED ---"""
  6. from vedo import Circle, show
  7. # Create a set of points in space
  8. pts = Circle(res=8).extrude(zshift=0.5).ps(4)
  9. # Visualize the points
  10. plt = show(pts, __doc__, interactive=False, axes=1)
  11. # Add the spline tool using the same points and interact with it
  12. sptool = plt.add_spline_tool(pts, closed=True)
  13. # Add a callback to print the center of mass of the spline
  14. sptool.add_observer(
  15. "end of interaction",
  16. lambda o, e: (
  17. print(f"Spline changed! CM = {sptool.spline().center_of_mass()}"),
  18. print(f"\tNumber of points: {sptool.spline().points.size}"),
  19. )
  20. )
  21. # Stay in the loop until the user presses q
  22. plt.interactive()
  23. # Switch off the tool
  24. sptool.off()
  25. # Extract and visualize the resulting spline
  26. sp = sptool.spline().lw(4)
  27. show(sp, "My spline is ready!", interactive=True, resetcam=False).close()