optics_main1.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import numpy as np
  2. import vedo
  3. from optics_base import Lens, Ray, Mirror, Detector, Screen # see file ./optics_base.py
  4. ###################################################################### thin lenses
  5. s = vedo.Sphere(r=2, res=50) # construct a thin lens:
  6. shape = s.boolean("intersect", s.clone().z(3.5)).z(1.4)
  7. lens = Lens(shape, ref_index=1.52).color("orange9")
  8. screen= Screen(3,3).z(5)
  9. elements = [lens, screen]
  10. source = vedo.Disc(r1=0, r2=0.7, res=4).points # numpy 3d points
  11. lines = [Ray(pt).trace(elements).asLine() for pt in source] # list of vedo.Line
  12. vedo.show("Test of 1/f = (n-1) :dot (1/R1-1/R2) :approx 1/2",
  13. elements, lines, lens.boundaries().lw(2),
  14. azimuth=-90, zoom=1.2, size=(1100,700), axes=dict(zxgrid=True)).close()
  15. ####################################################################### dispersion
  16. s = vedo.Cone(res=4).scale([1,1,0.4]).rotate_y(-90).rotate_x(45).pos(-0.5,0,1.5)
  17. prism = Lens(s, ref_index="glass").lw(1)
  18. screen= Screen(2,2).z(6)
  19. lines = []
  20. for wl in np.arange(450,750, 10)*1e-09:
  21. ray = Ray(direction=(-0.5,0,1), wave_length=wl)
  22. line = ray.trace([prism,screen]).asLine()
  23. lines.append(line)
  24. vedo.show("Test of chromatic dispersion", prism, screen, lines,
  25. zoom=1.5, size=(1100,700), axes=1).close()
  26. ################################################################ spherical mirrors
  27. s1 = vedo.Sphere(r=7, res=50).cut_with_plane([0,0,6],'z').cut_with_cylinder(invert=True)
  28. s2 = vedo.Sphere(r=5, res=50).cut_with_plane([0,0,-2],'-z').cut_with_cylinder().z(10)
  29. m1 = Mirror(s1)
  30. m2 = Mirror(s2)
  31. screen = Screen(5,5).z(9)
  32. elements = [m2, m1, m2, m1, screen] ## NOTE ordering!
  33. source= vedo.Disc(r1=1, r2=3, res=[20,60]).cut_with_plane().cut_with_plane(normal='y').z(1)
  34. lines = [Ray(pt).trace(elements).asLine(2) for pt in source.points]
  35. vedo.show("Reflection from spherical mirrors", elements, lines, axes=1).close()
  36. ################################################################# parabolic mirror
  37. s = vedo.Paraboloid(res=200).cut_with_plane([0,0,-0.4], 'z').scale([1,1,0.1]).z(1)
  38. elements = [Mirror(s), Screen(0.2,0.2).z(0.35)]
  39. source= vedo.Disc(r1=.1, r2=.3, res=[10,30]).cut_with_plane().cut_with_plane(normal='y')
  40. lines = [Ray(pt).trace(elements).asLine() for pt in source.points]
  41. vedo.show("Reflection from a parabolic mirror", elements, lines, axes=2, azimuth=-90).close()
  42. ################################################################# mesh mirror
  43. # Create the mirror from a vedo.Mesh object
  44. shape = vedo.Mesh(vedo.dataurl+"bunny.obj").fill_holes().subdivide().smooth()
  45. shape.scale(7).pos(0.1,-0.6,0).rotate_x(90)
  46. mirror = Mirror(shape).color("silver")
  47. # Create a detector surface as a quad-sphere surrounding the shape
  48. sd = vedo.Sphere(quads=1, res=12).cut_with_plane([0,-0.8,0], normal='y')
  49. detector = Detector(sd).color("white").alpha(1).lw(1)
  50. source = vedo.Grid(res=[30,30]).rotate_x(90).y(-1)
  51. lines=[]
  52. for pt in source.points:
  53. ray = Ray(pt, direction=(0,1,0)).trace([mirror, detector])
  54. line = ray.asLine(min_hits=2, max_hits=4)
  55. lines.append(line)
  56. detector.count().cmap("Reds", on='cells', vmax=10).add_scalarbar("Counts")
  57. vedo.show(mirror, detector, lines, "A Mesh mirror and a spherical detector",
  58. elevation=-90, axes=1, bg='bb', bg2='blue9').close()