optics_main3.py 1.4 KB

1234567891011121314151617181920212223242526272829303132
  1. """The butterfly effect with cylindrical mirrors and a laser"""
  2. # Original idea from "The Action Lab": https://www.youtube.com/watch?v=kBow0kTVn3s
  3. #
  4. from vedo import Plotter, Grid, Cylinder, merge
  5. from optics_base import Ray, Mirror, Detector # see file ./optics_base.py
  6. grid = Grid(res=[3,4]) # pick a few points in space to place cylinders
  7. pts = grid.points.tolist() + grid.cell_centers().points.tolist()
  8. # Create the mirror by merging many (y-scaled) cylinders into a single mesh object
  9. cyls = [Cylinder(p, r=0.065, height=0.2, res=2000).scale([1,1.5,1]) for p in pts]
  10. mirror = Mirror(merge(cyls)).color("silver")
  11. # Create a detector surface as a thin cylinder surrounding the mirror
  12. sd = Cylinder(r=1, height=0.3, cap=False).cut_with_plane([0,-0.95,0], normal='y')
  13. detector = Detector(sd)
  14. def slider(widget, event): ### callback to shift the beam along x
  15. dx = widget.value
  16. ray = Ray([dx,-1.2,-0.1], direction=(0,1,0.02))
  17. ray.maxiterations = 1000 # max nr. of reflections
  18. ray.trace([mirror, detector]) # cumpute trajectory
  19. detector.count().cmap("Reds", on='cells', vmax=10)
  20. line = ray.asLine().linewidth(4).c('green5')
  21. plt.remove("Line").add(line) # remove the old and add the new one
  22. plt = Plotter(axes=1, bg='peachpuff', bg2='blue9')
  23. plt.add_slider(slider, -0.07, 0.07, value=0, pos=5, title="beam shift")
  24. plt.show(mirror, detector, __doc__, elevation=-30)
  25. plt.close()