The goal is to optimize the shape of lens in such a way, that parallel rays
falling into the lens will focus in one single point. The shape is optimized by
modifying the lens segments' thickness.
The lens segments are encoded using 3 double values per segment:
the middle x-position of the segment, its top y-position and its thickness*1/2.
// 9 lens pieces: 10 thickness values
double op [] = { 40, 30, 10, // x , y, thickness*1/2
40, 40, 10,
40, 50, 10,
40, 60, 10,
40, 70, 10,
40, 80, 10,
40, 90, 10,
40,100, 10,
40,110, 10,
40,120, 10};
double sp [] = { 0, 0, .1,
0, 0, .1,
0, 0, .1,
0, 0, .1,
0, 0, .1,
0, 0, .1,
0, 0, .1,
0, 0, .1,
0, 0, .1,
0, 0, .1};
As we want to optimize the lens-segments thickness all but the thickness
strategy-parameters are 0;
For each incoming ray 2 refracted rays and 3 points of intersection are calculated:
1.intersection: incoming ray and left lens-border
1.refracted ray: incoming ray at 1.intersection
2.intersection: 1.refracted ray with right lens-border
2.refratced ray: outgoing ray at 2.intersection
3.intersection: 2.refracted ray with focus-plane
For each incoming ray i, let di be the distance of the 3rd intersection-point
and the focus within the focus-plane. Then the quality of a prism lens is calculated
by summing up the squared distances di^2 for each ray.
Calculating the quality for the prism lens is somewhat like doing ray tracing.
We have to calculate refracted rays and points of intersection.
Given a line (the lens border) and an incoming ray I we calculate the refracted
ray T as follows (Ref.: Glassner, Ray Tracing, p.291ff):
T = -p*I+(p*c1-c2)*N
with
N = normal of lens border
p = refraction proportion r2/r1 (lens/medium)
c1 = -I*N = cos(angle between I and N)
c2 = sqrt(1-p^2(1-c1^2)) = cos(angle between T and -N)