colorlines.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. """Color lines by a scalar
  2. Click the lines to get their lengths"""
  3. from vedo import sin, cos, Line, Lines, mag, Plotter
  4. # Define the points for the first line
  5. pts1 = [(sin(x/8), cos(x/8), x/5) for x in range(25)]
  6. # Create the first line and color it black
  7. l1 = Line(pts1).c('black')
  8. # Create the second line by cloning the first and rotating it
  9. l2 = l1.clone().rotate_z(180).shift(1,0,0)
  10. # Calculate a scalar value for each line segment as
  11. # the distance between the corresponding points on the two lines
  12. dist = mag(l1.vertices - l2.vertices)
  13. # Color the lines based on the scalar value using the 'Accent' colormap,
  14. # and add a scalar bar to the plot
  15. lines = Lines(l1, l2, lw=8)
  16. lines.celldata["distance"] = dist
  17. lines.cmap('Accent').add_scalarbar('length')
  18. # Define a callback function to print the length of the clicked line segment
  19. def clickfunc(evt):
  20. if evt.object:
  21. # Get the ID of the closest point on the clicked line segment
  22. idl = evt.object.closest_point(evt.picked3d, return_cell_id=True)
  23. # Print the length of the line segment with 3 decimal places
  24. print('clicked line', idl, 'length =', dist[idl])
  25. # Create a plotter with the mouse click callback function and show the lines
  26. plt = Plotter(axes=1, bg2='lightblue')
  27. plt.add_callback('mouse click', clickfunc)
  28. plt.show(l1,l2, lines, __doc__, viewup='z')
  29. plt.close()