5.2. Tracing Procedure¶
5.2.1. Tracing Process¶
The tracing process primarily involves three key steps: detecting surface hits, calculating refraction indices and directions, and managing rays that do not make contact with a surface. For ideal lenses, filters, and apertures, only one instance of surface hit detection is required, whereas a standard lens requires two.
Utilizing multiple threads for the majority of tracing tasks significantly enhances computational speed. Each thread is assigned a specific set of rays, ensuring that all processing, including ray creation and system-wide propagation, occurs exclusively within that thread.
Although not explicitly detailed here, the refraction calculation function also computes new ray polarization and weights.
Fig. 5.6 Tracing process flowchart.¶
5.2.2. Refraction¶
The law of refraction is typically expressed in terms of input and output angles. However, for the tracing process, a vector-based formulation is more convenient as it avoids the need to calculate any angles directly.
The following figure illustrates the refraction of a ray on a curved surface.
Fig. 5.7 Refraction on a curved interface.¶
The refractive indices of the media are denoted as \(n_1\) and \(n_2\), while \(s\) and \(s'\) represent the input and output propagation vectors, respectively. It is essential to normalize these vectors, along with the normal vector \(n\), to ensure accurate calculations. Additionally, \(s\) and \(n\) must point in the same half-space direction, which is mathematically ensured by the condition \(s \cdot n \geq 0\).
An equation for this vector-based formulation of the refraction law can be found in [1] and [2]:
In the event of total internal reflection (TIR), the root argument becomes negative. In optrace, TIR rays are absorbed since reflections are not modeled within the system. When this occurs, the user receives a message to the standard output.
5.2.3. Polarization¶
The following calculations are similar to those described in [3].
The polarization vector \(E\) can be decomposed into two components: \(E_\text{p}\), which lies in the plane formed by the surface normal and the incidence vector, and \(E_\text{s}\), which is perpendicular to this plane. When refraction occurs at an interface, the \(E_\text{s}\) component remains unchanged for both the incident and refracted ray vectors, \(s\) and \(s'\). However, the \(E_\text{p}\) component is rotated around \(E_\text{s}\) towards \(s'\), resulting in a new component \(E\text{p}'\).
It’s important to note that, in our calculations, all vectors are unity vectors. The magnitudes of the polarization components are encoded in the scaling factors \(A_\text{tp}\) and \(A_\text{ts}\).
Fig. 5.8 Ray polarization components before and after refraction.¶
Case 1:
For \(s \parallel s'\) the new polarization vector is equal to the initial polarization.
Case 2
For \(s \nparallel s'\) the new polarization vector differs. In optics, the polarization vector and its components are orthogonal to the propagation direction. Moreover, the two polarization components are perpendicular to each other. Given that all vectors mentioned are unity vectors, we can perform the following calculations:
Since \(||E_\text{p}|| = ||E_\text{s}|| = ||E|| = 1\), the amplitude components are then:
For the new polarization unity vector, which also composed of two components, we finally get:
5.2.4. Transmission¶
The new ray powers are determined by calculating the transmission, which can be derived from the properties already established during the refraction calculations. According to the Fresnel equations, the transmission of light depends on its polarization direction. The following equations illustrate this behavior, as detailed in [4].
The polarization components \(A_\text{ts}\) and \(A_\text{tp}\) are obtained from equations (5.25). The cosine terms involved are calculated using the direction and normal vectors as follows: \(\cos \varepsilon = n \cdot s\) and \(\cos \varepsilon' = n \cdot s'\).
For light that hits the surface perpendicularly, this results in an expression that is independent of polarization, as noted in [5]:
5.2.5. Refraction at an Ideal Lens¶
A ray with an unnormalized direction vector \(s_0\) intersects the lens at the point \(P = (x_0, y_0, 0)\) on a lens with a focal length \(f\). The corresponding point on the focal plane is \(P_f = (x_f, y_f, f)\).
According to optics, ideal parallel rays converge at the same position on the focal plane. Therefore, a ray with the same direction, but intersecting the lens at the optical axis, can be used to determine the position \(P_f\). This is illustrated in Fig. 5.9.
Fig. 5.9 Geometry for refraction on an ideal lens.¶
Cartesian Representation
Calculating positions \(x_f\) and \(y_f\) is straightforward by evaluating the linear ray equations \(x(z)\) and \(y(z)\) at \(z=f\).
For \(x_f\), we have:
Similarly, for \(y_f\):
It is important to note that \(s_{0z} = 0\) is prohibited, as all rays are required to have a positive z-direction component.
With the point \(P_f\) known, the outgoing propagation vector \(s_0'\) can be calculated as follows:
Normalizing this vector gives us:
Angular Representation
Consider the x-component of the propagation vector:
Dividing it by \(f\) yields:
From Fig. 5.9, it follows that \(\tan \varepsilon_x' = \frac{s_{0x}'}{f}\) and \(\tan \varepsilon_x = \frac{s_{0x}}{s_{0z}}\). Therefore, we have:
Analogously, in the y-direction, we obtain:
This angular representation is a formulation also found in [6].
5.2.6. Filtering¶
When passing through a filter, a ray with power \(P_i\) and wavelength \(\lambda_i\) is attenuated according to the filter’s transmission function \(T_\text{F}(\lambda)\):
Additionally, ray powers are set to zero if the transmission falls below a specific threshold \(T_\text{th}\). This approach avoids ghost rays that continue to be propagated during raytracing but carry very little power. As their contribution to image formation is negligible, absorbing them as soon as possible helps speed up the tracing process.
As a side note, apertures are also implemented as filters, but with \(T_\text{F}(\lambda) = 0\) for all wavelengths.
5.2.7. Geometry Checks¶
Geometry checks before tracing include:
All tracing-relevant elements must be within the outline.
There must be no object collisions.
Elements must follow a defined, sequential order.
Ray sources must be available.
All ray sources must precede all other types of elements.
Collision checks are performed by first sorting the elements and then comparing positions on adjacent surfaces. This is implemented by randomly sampling many lateral surface positions and checking the correct order of the axial coordinates. While this method doesn’t guarantee the absence of collisions, the sequentiality is verified for each ray during raytracing, and warnings are emitted if issues are detected.
5.2.8. Intersection Calculation¶
5.2.8.1. Surface Extension¶
To simplify the handling of non-intersecting rays and ensure each ray has the same number of sections, the surface is extended so that all rays intersect a surface. Gaps on the surface are filled, and the surface edge is extended radially towards infinity.
An intersection is calculated for the extended surface. The ray is marked as hitting or non-hitting based on a surface mask afterwards.
Fig. 5.10 Surface Extension¶
5.2.8.2. Intersection of a Ray with a Plane¶
The intersection of all planar surface types (CircularSurface, RectangularSurface, RingSurface) as well as the TiltedSurface can be computed analytically. The following definitions hold:
The surface point normal equation is:
The ray equation in dependence of ray parameter \(t\) is as follows:
Inserting these equations into each other results in:
Rearranging provides the ray parameter for the hit position \(t_\text{h}\) for the case where \(\vec{s} \cdot \vec{n} \neq 0\):
This result can be inserted into the ray equation to determine the intersection position. If \(\vec{s} \cdot \vec{n} = 0\), the surface normal and ray direction vector are perpendicular. If the ray lies in the plane, there are infinite solutions. If not, there is none.
5.2.8.3. Intersection of a Ray with a Conic Surface¶
Intersections with a ConicSurface can also be calculated analytically.
Equating the ray equation and the conic section leads to:
The squared radius is:
Some work in rearranging leads to the following equation, valid for \(\rho \neq 0\) (conic section is not a plane):
With the new parameters:
The solutions for \(t\) are (\(\rho \neq 0\)):
The first case produces two intersections, while in the second, the ray touches the surface tangentially. In the fourth case, there is no intersection. In cases one and two, it must be verified whether the intersection lies within the valid radial range of the surface, as the conic equation describes the surface for all possible \(r\). If there are still two valid intersections in the first case, the one with the lower axial position is used. The third case is only relevant for rays lying along a linear section of a surface, such as a plane or a cone surface. This is further explained below.
Equation (5.50) yields the same results as described in [7], although in his derivation, the constants \(A, B, C\) include an additional curvature scaling and the factor of 2 is incorporated inside \(B\).
In the case of \(\rho = 0\) (plane) equation (5.46) simplifies to \(p_z + s_z t = z_0\), resulting in:
The first case produces exactly one intersection and corresponds to the special case of equation (5.44) for \(\vec{n} = (0, 0, 1)\). In the second case, the ray lies within the plane, resulting in infinite intersection positions. In the third case, the ray is parallel but offset from the plane, producing no intersections. We enforce \(s_z > 0\) in our simulator, making only the first case relevant.
For \(\rho \rightarrow \infty\) and \(r \in \mathbb{R}\), the conic section becomes a cone for \(k < -1\), a line from \(z = z_0\) to \(z \rightarrow \infty\) for \(k = -1\), or a point at \((x_0, y_0, z_0)\) for \(k > -1\). In the first two cases, the conic sections consist of long linear segments. Depending on the cases of equation (5.50), a ray can intersect once, twice, not hit the surface, or lie within it. For a point (\(k > -1\)), there can be no intersection or a single one. In the latter case, \(B^2 - CA = 0\) holds, producing a single intersection in the first case of equation (5.50).
5.2.8.4. Numerical Hit Search¶
In cases where the asphere equation or arbitrary analytical functions are involved, an analytical solution for ray intersection does not exist. Therefore, the solution must be approximated numerically and iteratively.
The ray line equation, depending on the ray parameter \(t\), is defined as follows:
The cost function \(G\) in relation to the surface function \(f\) is expressed by:
The parameters \(x_t, y_t, z_t\) are derived from equation (5.53). To determine the position of the intersection, the root of the scalar function \(G\) must be identified. Suitable for this task are typical optimization algorithms. However, these algorithms often lack guaranteed convergence.
To address this issue, the raytracer employs the Regula-Falsi method. This method is a straightforward iterative approach that guarantees superlinear convergence with only a slight modification. This procedure requires the knowledge of an interval containing a root. As the minimum and maximum extent of the surface in the z-direction are predetermined in the raytracer, this requirement is inherently satisfied: A hit can only occur within this range. The method functions by iteratively reducing the interval containing the function’s root. A detailed explanation can be found in [8].
In certain scenarios, the interval size may reduce negligibly across iterations, leading to slow convergence. To mitigate this, the process is extended to the Illinois algorithm, which is detailed in [9].
The implementation in optrace varies by incorporating parallelization and iterating subsequent steps only with rays that have not yet converged to a solution.
5.2.9. Normal Calculation¶
5.2.9.1. General¶
Surface normals are required to calculate the refraction, polarization, and transmittance of a ray. The equation for an analytical, unnormalized normal vector is presented below, as detailed in [10].
To use this vector for calculations, it must be normalized:
5.2.9.2. Planar Surfaces¶
For the surface types Rectangle, Circle, and Ring, the normal vector \(n\) is simply \(n = (0, 0, 1)\). For the surface type TiltedSurface, a user-specified value for \(n\) is provided, which is normalized internally.
5.2.9.3. Asphere¶
The radial derivative of an asphere equation is given by:
This radial component must be rotated around the vector \((0, 0, 1)\) by the angle \(\phi\). According to the general normal vector calculation, the x- and y-components of the unnormalized vector are computed as follows:
These components can be applied to the equation described above.
5.2.9.4. Conic Surface¶
The derivative of the conic function represents a simplified case of the asphere scenario, where polynomial terms are absent:
Here, \(m_r\) is equivalent to \(\tan \alpha\), the tangent of the angle between the surface normal and the vector \((0, 0, 1)\). Using geometrical relations, one can derive \(\sin \alpha\), which leads to the radial component \(n_r\) of the normalized normal vector:
The normalized vector \(n\) for the ConicSurface is expressed as follows:
For a SphericalSurface, this expression simplifies further to:
5.2.9.5. FunctionSurface¶
For the surface types FunctionSurface1D and FunctionSurface2D, the derivatives required for constructing the normal vector are typically calculated numerically. An exception to this arises when a derivative function is directly provided to the object.
The central first derivative has the form:
For partial surface derivatives, the expressions are approximately given by:
For a FunctionSurface1D, both derivatives are the same due to symmetry, so only a single value needs to be computed. The step width \(\varepsilon\) is selected as the maximum of the optimal step width \(\varepsilon_\text{o}\) and the minimal positional step width \(\varepsilon_\text{p}\):
The optimal step width, as derived from [11], is given by:
Here, \(\varepsilon_\text{f}\) represents the machine precision for the floating-point type being utilized. Under the assumption of predominantly spherical surfaces, the primary function component is \(x^2\). Higher polynomial orders are less significant, a reasonable assumption is \(\left| \frac{f(x)}{f^{(3)}(x)} \right| = 50\).
Although this may vary for different functions, due to the influence of the cube root, a quotient 1000 times larger modifies \(\varepsilon_\text{o}\) by only about a factor of \(10\). With a 64-bit floating-point number, where \(\varepsilon_\text{f} \approx 2.22 \times 10^{-16}\), the result is \(\varepsilon_\text{o} \approx 3.22 \times 10^{-5}\). Since optrace units are measured in millimeters, this corresponds to \(32.2\,\) nm.
In order to ensure that numerical differences in \(f\) are not only representable, but also that \(x + \varepsilon\) is distinct from \(x\), it is essential to satisfy the condition \(x + \varepsilon > x (1 + \varepsilon_\text{f})\) for every coordinate \(x\) on the surface.
Given \(r_\text{max}\) as the maximum absolute distance on the surface, the minimal bound can be expressed as:
It is advisable to center the surface at \(r = 0\) prior to differentiation to minimize \(r_\text{max}\).
5.2.9.6. DataSurface¶
A DataSurface object utilizes the scipy.interpolate.InterpolatedUnivariateSpline
(FunctionSurface1D)
or the scipy.interpolate.RectBivariateSpline
(FunctionSurface2D) to interpolate surface values.
To ensure smooth curvature transitions, an interpolation order of k=4
is applied in all lateral dimensions.
By invoking the spline objects with scipy.interpolate.InterpolatedUnivariateSpline.__call__()
or scipy.interpolate.RectBivariateSpline.__call__()
, it is possible to specify a derivative parameter,
enabling the output of surface derivatives.
These derivatives can then be used to compute surface normals, as described in the methods discussed earlier.
It is important to note that due to the nature of interpolation, the minimum and maximum surface values may exceed the specified data range. A warning informs the user about the occurence of large deviations.
References