forked from mickp/python-optics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeomopt.py
More file actions
81 lines (62 loc) · 2.05 KB
/
geomopt.py
File metadata and controls
81 lines (62 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from collections import namedtuple
from numpy import tan, pi
Point = namedtuple('Point', 'x, z, tanTheta')
def zImage(rayA, rayB):
a = rayA.points[-1]
b = rayB.points[-1]
if a.z != b.z:
return None
else:
try:
z = a.z + (b.x - a.x) / (a.tanTheta - b.tanTheta)
except ZeroDivisionError:
z = None
except:
raise
return z
class Surface(object):
def __init__(self, dz):
self.dz = dz
class Lens(Surface):
def __init__(self, dz, f):
super(Lens, self).__init__(dz)
self.f = f
class Pupil(Surface):
def __init__(self, dz, r):
super(Pupil, self).__init__(dz)
self.r = r
class Ray(object):
def __init__(self, x0, tan0, col=None):
self.points = [ Point(float(x0), 0., float(tan0))]
self.redraw = [ True ]
if col:
self.colour = col
elif x0 == 0:
self.colour = 'b'
else:
self.colour = 'r'
def propogate(self, surfaces, start=0):
#start = 0
if start >= len(self.points):
start = len(self.points) - 1
else:
self.points = self.points[0:start + 1]
for i, surface in enumerate(surfaces[start:], start):
prevPoint = self.points[i]
z = prevPoint.z + surface.dz
x = prevPoint.x + (surface.dz * prevPoint.tanTheta)
if type(surface) is Lens:
# Lenses bend rays.
tanTheta = prevPoint.tanTheta - x / surface.f
else:
tanTheta = prevPoint.tanTheta
self.points.append(Point(x, z, tanTheta))
if type(surface) is Pupil:
if abs(x) > surface.r:
return
if i+1 == len(surfaces):
if type(surface) is Lens:
zFinal = z + 2. * abs(surface.f)
else:
zFinal = z + 0.5 * surface.dz
self.points.append(Point(x + (zFinal - z) * tanTheta, zFinal, tanTheta))