From d11959f146fe93afe44df468c01a36c9577a20ec Mon Sep 17 00:00:00 2001 From: uitt_hu Date: Fri, 16 Oct 2026 15:11:03 +0200 Subject: [PATCH 1/6] Detector hits as values, moving a detector empties it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A hit kept a reference to the ray or beamlet that made it, so solving the same beam again changed the hits of earlier solves (#42). All four hit types now store only the values their evaluation needs and are isbits: - RayHit, PolarizedRayHit: hit point, direction, opl, λ, n, projection factor (and E) - AstigmaticGaussianBeamletHit: its cache plus polarization, segment length and λ - GaussianBeamletHit: the complex beam parameter q0 = s0 + i z_R at the segment start; the field within the segment follows in closed form from q(l) = q0 + l, instead of evaluating the waist and divergence rays at every field point. s0, w0 and the divergence come from the rays (_local_beam, split off gauss_parameters), so the result equals the ray construction for rays in one plane with the chief ray. The kinematic primitives of AbstractDetector call empty! first: the stored hits belong to the pose of the detector during the solve. Docs and agent skill state that a detector keeps the hits of consecutive solves side by side, and that the field functions superpose all of them coherently. Closes #42 Co-Authored-By: Claude Opus 5.5 --- docs/src/basics/components/detectors.md | 24 ++ ext/BeamletOpticsOpticsBaseExt.jl | 5 +- skills/beamletoptics/CHECKLIST.md | 2 +- skills/beamletoptics/SKILL.md | 2 +- skills/beamletoptics/WORKFLOW.md | 3 +- skills/beamletoptics/components/detectors.md | 6 +- src/Gaussian.jl | 28 ++- src/OpticalComponents/Detectors/Detector.jl | 232 +++++++++++------- .../Detectors/DetectorUtils.jl | 2 +- .../Detectors/FieldCalculation.jl | 28 ++- test/Components/TestDetector.jl | 123 ++++++++++ test/TestOpticsBaseExt.jl | 12 +- 12 files changed, 354 insertions(+), 113 deletions(-) diff --git a/docs/src/basics/components/detectors.md b/docs/src/basics/components/detectors.md index 7e796a313..87c77aaad 100644 --- a/docs/src/basics/components/detectors.md +++ b/docs/src/basics/components/detectors.md @@ -25,6 +25,30 @@ Detector After solving a system containing a `Detector`, the methods listed below can be used in order to analyze the stored data. If no data is obtained during the tracing procedure, an error message will be stored. +## Reading out a detector + +A detector stores *hits*. The following rules define what a readout computes. + +1. A hit holds the values of the ray or beamlet at the moment of the solve. Solving again, or moving a source or any other component, does not change hits that are already stored. +2. Any kinematic call on the detector (e.g. [`translate3d!`](@ref) or [`rotate3d!`](@ref)) empties it, also when it is moved as part of an [`ObjectGroup`](@ref), since its hits would no longer lie on its surface. +3. A detector can hold the hits of solves that follow each other in time side by side. It is up to the user when to evaluate them. [`spot_diagram`](@ref) shows all hits, which is harmless. [`electric_field`](@ref), [`intensity`](@ref) and [`optical_power`](@ref) superpose all hits **coherently**. This is right for beams that exist at the same time, e.g. a beam group or an interferometer. For solves that follow each other in time, the user must know what this computes, or evaluate after each solve and reset the detector (`initialize = true`, [`initialize!`](@ref) or [`empty!`](@ref)). + +```julia +# Reset and evaluate after every solve +for y in ys + translate_to3d!(mirror, [0, y, 0]) + solve_system!(system, source; initialize = true) + push!(powers, optical_power(pd)) +end + +# Solve several times, evaluate once: the spots of all solves +for _ in 1:10 + zrotate3d!(source, deg2rad(1)) + solve_system!(system, source) +end +spots = spot_diagram(pd) +``` + ## Spot diagrams The `spot_diagram` method provides a straight forward way to generate spot diagrams, which are commonly used to perform initial assessments of the optical performance of an imaging setup. diff --git a/ext/BeamletOpticsOpticsBaseExt.jl b/ext/BeamletOpticsOpticsBaseExt.jl index 3c2aa77ce..dd7959e95 100644 --- a/ext/BeamletOpticsOpticsBaseExt.jl +++ b/ext/BeamletOpticsOpticsBaseExt.jl @@ -40,8 +40,9 @@ const _PREFIX = "BeamletOptics/OpticsBase extension" origin = BeamletOptics.position(detector), axes = ) Samples the coherent field of the beamlets hitting a BeamletOptics (BMO) `Detector` into -an [`OpticsBase.PlaneField`](https://StackEnjoyer.github.io/OpticsBase.jl/dev/). Call it after `solve_system!` and before moving any object, solving again or -emptying the detector. Requires `using OpticsBase` (package extension). +an [`OpticsBase.PlaneField`](https://StackEnjoyer.github.io/OpticsBase.jl/dev/). Call it after `solve_system!`. The hits keep the state of +their solve: solving again or moving other objects does not change them, moving the detector +empties it. Requires `using OpticsBase` (package extension). # Arguments diff --git a/skills/beamletoptics/CHECKLIST.md b/skills/beamletoptics/CHECKLIST.md index 336cbfa4b..bb4618acc 100644 --- a/skills/beamletoptics/CHECKLIST.md +++ b/skills/beamletoptics/CHECKLIST.md @@ -8,7 +8,7 @@ - [ ] Lens radius signs follow ISO 10110 (biconvex: `r1 > 0`, `r2 < 0`). - [ ] Geometry assumes +y as the optical axis; rotations are cumulative and relative. - [ ] Sources start **outside** of all elements and point at them. -- [ ] Detectors emptied before every re-solve in a loop (`initialize = true`, `initialize!(system)` or `empty!(detector)`); results read out before the detector is moved. +- [ ] Detectors emptied before every re-solve in a loop (`initialize = true`, `initialize!(system)` or `empty!(detector)`), unless the hits of all solves are wanted together (e.g. the spots of a scan); `electric_field`, `intensity` and `optical_power` superpose all stored hits coherently; moving the detector empties it. - [ ] Beam model fits the question (see below). - [ ] Script was **run**, and the printed numbers were compared with a paraxial or analytic estimate. - [ ] For non-trivial geometry, a rendered image was checked. diff --git a/skills/beamletoptics/SKILL.md b/skills/beamletoptics/SKILL.md index 1cb8c604e..49e0cbd63 100644 --- a/skills/beamletoptics/SKILL.md +++ b/skills/beamletoptics/SKILL.md @@ -28,7 +28,7 @@ When this Skill is active: `λ -> 1.5`, `DiscreteRefractiveIndex(...)` or `SellmeierEquation(...)`. - **Detectors accumulate.** Solve with `solve_system!(system, beam; initialize = true)`, or call `initialize!(system)` (all detectors) or `empty!(detector)` (one) before every `solve_system!` that reuses the detectors; never reset - between sources that should superpose. + between sources that should superpose. Field functions superpose all stored hits coherently; moving the detector empties it. - **All kinematics are relative** (`translate3d!`, `xrotate3d!`, …), angles in radians (`deg2rad`). Use `translate_to3d!` for absolute positioning. - Run the script after writing it. A simulation that "looks right" but was never executed is not done. diff --git a/skills/beamletoptics/WORKFLOW.md b/skills/beamletoptics/WORKFLOW.md index 53acc58a4..e69b36c3b 100644 --- a/skills/beamletoptics/WORKFLOW.md +++ b/skills/beamletoptics/WORKFLOW.md @@ -43,7 +43,8 @@ for y in ys end ``` -- Read everything you need from a detector **before** moving anything or solving again (hits reference beam state). +- Hits are values of the solve: solving again or moving anything but the detector does not change them. Moving the detector (also via a group) empties it. +- A detector holds the hits of consecutive solves side by side; you decide when to evaluate. `spot_diagram` shows all hits; `electric_field`, `intensity`, `optical_power` superpose all hits **coherently**. For solves that follow each other in time, know what this computes, or evaluate after each solve and reset (`initialize = true`). - See `templates/01_singlet_spot_diagram.jl` (focus scan) and `templates/04_michelson_scan.jl` (fringes). ## Interferometers and coherent sums diff --git a/skills/beamletoptics/components/detectors.md b/skills/beamletoptics/components/detectors.md index 70eb3aafd..aef8c4dc4 100644 --- a/skills/beamletoptics/components/detectors.md +++ b/skills/beamletoptics/components/detectors.md @@ -9,8 +9,10 @@ Detector(edge_length, stop = true) it catches beams travelling along −x. - With `stop = false`, beams continue behind the detector. - Stores *hits* of exactly **one** type (Ray, PolarizedRay, GaussianBeamlet or AstigmaticGaussianBeamlet). -- **Accumulates across solves:** solve with `initialize = true`, or call `initialize!(system)` (all detectors of a system, nested groups included) or `empty!(det)` (one detector) before each `solve_system!` that reuses it. By default the solver does not reset detectors: all beams of a beam group, and several sources solved one after another, superpose on purpose. -- Read out all results before moving the detector again. +- **Hits are values** of the beam state at the solve. Solving again, or moving a source, mirror or lens, never changes stored hits. +- **Moving the detector empties it:** every kinematic call on it (also a move of a group containing it) clears its hits. +- **Accumulates across solves:** by default the solver does not reset detectors, so hits of consecutive solves sit side by side and all beams of a beam group, and several sources solved one after another, superpose on purpose. The user decides when to evaluate. To start empty, solve with `initialize = true`, or call `initialize!(system)` (all detectors of a system, nested groups included) or `empty!(det)` (one detector). +- `spot_diagram` shows all stored hits. `electric_field`, `intensity` and `optical_power` superpose **all** stored hits **coherently**, as for beams that exist at the same time. For solves that follow each other in time, know what this computes, or evaluate after each solve and reset (`initialize = true`). ## Readout diff --git a/src/Gaussian.jl b/src/Gaussian.jl index f152e6b71..fd6c2e6f9 100644 --- a/src/Gaussian.jl +++ b/src/Gaussian.jl @@ -301,6 +301,26 @@ function gauss_parameters( hint = point_on_beam(gauss, z) ) p0, index = hint + w, R, z, w0, _ = _local_beam(gauss, p0, index) + ψ = -atan(1, √(1 / (R * z) - 1)) + # Catch NaNs and correct Gouy phase sign based on curvature sign + isnan(R) && (R = zero(R)) + isnan(ψ) && (ψ = zero(ψ)) + isnan(w0) && (w0 = w) + R < 0 && (ψ = -ψ) + return (w, R, ψ + ψ0, w0) +end + +""" + _local_beam(gauss::GaussianBeamlet, p0, index) + +Parameters of the segment `index` of `gauss` on the plane through `p0` normal to its chief ray, +from the heights and slopes of the waist and divergence rays (Arnaud 1985, DeJager 1992): beam +radius `w`, curvature `R` (1/radius of curvature), signed distance `z` from the local waist, waist +radius `w0` and far-field divergence `F` (`w² = w0² + F² z²` within the segment). Values can be NaN, +e.g. at a plane where the divergence ray crosses the chief ray. +""" +function _local_beam(gauss::GaussianBeamlet, p0, index) chief = gauss.chief.rays[index] div = gauss.divergence.rays[index] waist = gauss.waist.rays[index] @@ -342,14 +362,8 @@ function gauss_parameters( w = sqrt(y_d^2 + y_w^2) R = E_kt / w^2 z = E_kt / F_kt^2 - ψ = -atan(1, √(1 / (R * z) - 1)) w0 = H / (n * F_kt) - # Catch NaNs and correct Gouy phase sign based on curvature sign - isnan(R) && (R = zero(R)) - isnan(ψ) && (ψ = zero(ψ)) - isnan(w0) && (w0 = w) - R < 0 && (ψ = -ψ) - return (w, R, ψ + ψ0, w0) + return (w, R, z, w0, F_kt) end """ diff --git a/src/OpticalComponents/Detectors/Detector.jl b/src/OpticalComponents/Detectors/Detector.jl index bb5a215b3..03d86054d 100644 --- a/src/OpticalComponents/Detectors/Detector.jl +++ b/src/OpticalComponents/Detectors/Detector.jl @@ -31,6 +31,9 @@ is placed on the user. This function should be called `empty!`. A single detecto detectors of a [`System`](@ref) or [`ObjectGroup`](@ref) with [`initialize!`](@ref), which calls `empty!` on every `AbstractDetector`. The solver does not reset detectors unless asked to (keyword `initialize` of [`solve_system!`](@ref)), since the beams of a beam group and several sources solved one after another are meant to superpose. + +Every kinematic function empties a detector, since the stored data belong to the pose of the detector during the solve, see +[`translate3d!`](@ref)`(::Movable, ::AbstractDetector, offset)`. A subtype gets this from its `empty!` method without further code. """ abstract type AbstractDetector{T} <: AbstractObject{T} end @@ -46,6 +49,32 @@ end initialize!(d::AbstractDetector) = (empty!(d); nothing) +""" + translate3d!(::Movable, detector::AbstractDetector, offset) + +Empties the `detector` (see `empty!`) and moves it by `offset`. Its hits lie on the surface +where it was during the solve, hence they are discarded when it moves. All kinematic functions +are derived from this one and [`rotate3d!`](@ref)`(::Movable, ::AbstractDetector, R)`, so each of +them empties the detector, also when it moves as a member of an [`ObjectGroup`](@ref). +""" +function translate3d!(::Movable, d::AbstractDetector, offset) + empty!(d) + translate3d!(shape_trait_of(d), d, offset) + return nothing +end + +""" + rotate3d!(::Movable, detector::AbstractDetector, R::AbstractMatrix) + +Empties the `detector` (see `empty!`) and rotates it by `R` about its `position`, see +[`translate3d!`](@ref)`(::Movable, ::AbstractDetector, offset)`. +""" +function rotate3d!(::Movable, d::AbstractDetector, R::AbstractMatrix) + empty!(d) + rotate3d!(shape_trait_of(d), d, R) + return nothing +end + """ AbstractDetectorHit @@ -72,62 +101,85 @@ abstract type AbstractDetectorHit end AbstractRayHit{T} <: AbstractDetectorHit Abstract supertype for detector hits produced by [`AbstractRay`](@ref)s. -Provides a common interface for extracting positional and optical path -information from rays stored in a detector hit. Currently the following concrete types are implemented: - [`RayHit`](@ref) - [`PolarizedRayHit`](@ref) -# Implementation reqs. - -Subtypes of `AbstractRayHit` must implement the following: +A hit stores the values of the ray at the moment it hit the detector, not the ray itself: a +new solve of the beam or a move of any object does not change it. -## Fields - -- `ray`: stores the `AbstractRay` that has intersected the detector -- `opl`: stores the [`optical_path_length`](@ref) of the parent beam (incl. the ray) - -## Functions +# Implementation reqs. -The interface provides the following functions for the fields above: +Subtypes of `AbstractRayHit` must implement the following functions: -- `position`: returns the `ray` position -- `direction`: returns the `ray` direction -- `length`: returns the `ray` length -- `optical_path_length`: returns the `opl` -- `wavenumber`: returns the `ray` wavenumber -- `hit_point`: returns the R³ point of intersection -- `projection_factor`: returns the scalar projection between the surface normal and ray dir. +- `hit_point`: the R³ point of intersection +- `direction`: the direction of the ray +- `optical_path_length`: the optical path length of the beam up to the hit point (incl. its parents) +- `wavelength`: the vacuum wavelength of the ray +- `wavenumber`: the vacuum wavenumber `2π / λ` +- `refractive_index`: the refractive index in front of the detector +- `projection_factor`: `|d · n|` of the ray direction `d` and the surface normal `n`; `1` if the + ray ended without an intersection """ abstract type AbstractRayHit{T} <: AbstractDetectorHit end -position(hit::AbstractRayHit) = position(hit.ray) -direction(hit::AbstractRayHit) = direction(hit.ray) - -length(hit::AbstractRayHit) = length(hit.ray) +hit_point(hit::AbstractRayHit) = hit.p +direction(hit::AbstractRayHit) = hit.d optical_path_length(hit::AbstractRayHit) = hit.opl -wavenumber(hit::AbstractRayHit) = wavenumber(hit.ray) - -hit_point(hit::AbstractRayHit) = position(hit) + length(hit.ray) * direction(hit) +wavelength(hit::AbstractRayHit) = hit.λ +wavenumber(hit::AbstractRayHit) = wavenumber(wavelength(hit)) +refractive_index(hit::AbstractRayHit) = hit.n +projection_factor(hit::AbstractRayHit) = hit.proj -function projection_factor(hit::AbstractRayHit) - abs(dot(direction(hit), normal3d(intersection(hit.ray)))) -end +""" + RayHit(ray::Ray, opl) -"Stores a [`Ray`](@ref) hit" +Stores the hit of a [`Ray`](@ref) as values: hit point `p`, direction `d`, optical path length `opl` +of the beam up to the hit point, wavelength `λ`, refractive index `n` and projection factor `proj`. +""" struct RayHit{T} <: AbstractRayHit{T} - ray::Ray{T} + p::Point3{T} + d::Point3{T} opl::T + λ::T + n::T + proj::T end -"Stores a [`PolarizedRay`](@ref) hit" +RayHit(ray::Ray{T}, opl::Real) where {T} = + RayHit{T}(_ray_end(ray), direction(ray), opl, wavelength(ray), refractive_index(ray), _projection(ray)) + +""" + PolarizedRayHit(ray::PolarizedRay, opl) + +Stores the hit of a [`PolarizedRay`](@ref) as values: those of a [`RayHit`](@ref) and the field +vector `E` of the ray in global coordinates. +""" struct PolarizedRayHit{T} <: AbstractRayHit{T} - ray::PolarizedRay{T} + p::Point3{T} + d::Point3{T} opl::T + λ::T + n::T + proj::T + E::Point3{Complex{T}} end -polarization(hit::PolarizedRayHit) = polarization(hit.ray) +PolarizedRayHit(ray::PolarizedRay{T}, opl::Real) where {T} = + PolarizedRayHit{T}(_ray_end(ray), direction(ray), opl, wavelength(ray), refractive_index(ray), + _projection(ray), polarization(ray)) + +polarization(hit::PolarizedRayHit) = hit.E + +# End point of a ray that hit a surface +_ray_end(ray::AbstractRay) = position(ray) + length(ray) * direction(ray) + +# |d·n| of a ray ending on a surface with normal n; 1 without an intersection +function _projection(ray::AbstractRay{T}) where {T} + isnothing(intersection(ray)) && return one(T) + return abs(dot(direction(ray), normal3d(intersection(ray)))) +end """ AbstractBeamletHit @@ -141,19 +193,26 @@ abstract type AbstractBeamletHit{T} <: AbstractDetectorHit end """ GaussianBeamletHit{T} <: AbstractBeamletHit{T} -Stores a [`GaussianBeamlet`], where `l0` represents the length of the parent beam -up until the current beam section, identified by the `id` index, `Δl0` the optical -minus the geometric path and `ψ0` the Gouy phase that the beam has collected up to there. +Stores the segment of a [`GaussianBeamlet`](@ref) that hit the detector as values, without a +reference to the beamlet: `p0` and `d0` are start and direction of the segment, `l_seg` its +length, `l0` the length of the beam (including its parents) up to `p0`, `Δl0` the optical minus +the geometric path and `ψ0` the Gouy phase that the beam has collected up to there. Within the +segment, of refractive index `n`, the beam follows the complex beam parameter `q0 = s0 + i z_R` +at `p0`: `s0` is the signed distance from the local waist of radius `w0`. `E_amp` is the field +amplitude times the beam radius, `E0 w0` of the source. """ struct GaussianBeamletHit{T} <: AbstractBeamletHit{T} - gauss::GaussianBeamlet{T} l0::T - id::Int Δl0::T ψ0::T - # Cache p0::Point3{T} d0::Point3{T} + l_seg::T + q0::Complex{T} + w0::T + n::T + λ::T + E_amp::Complex{T} sqrt_proj::T w_max::T end @@ -161,13 +220,13 @@ end """ AstigmaticGaussianBeamletHit{T} <: AbstractBeamletHit{T} -Stores an [`AstigmaticGaussianBeamlet`], where `l0` represents the length of the parent beam -up until the current beam section, identified by the `id` index. +Stores the segment of an [`AstigmaticGaussianBeamlet`](@ref) that hit the detector as values, +without a reference to the beamlet: `p0` and `d0` are start and direction of the segment, +`l_seg` its length, `l0` the length of the beam (including its parents) up to `p0`, `λ` the +wavelength and `E_pol` the polarization of the chief ray of the segment. """ struct AstigmaticGaussianBeamletHit{T} <: AbstractBeamletHit{T} - agb::AstigmaticGaussianBeamlet{T} l0::T - id::Int # Cache p0::Point3{T} d0::Point3{T} @@ -188,31 +247,20 @@ struct AstigmaticGaussianBeamletHit{T} <: AbstractBeamletHit{T} E_ref_amp::Complex{T} sqrt_proj::T w_max::T + l_seg::T + λ::T + E_pol::Point3{Complex{T}} end -position(hit::GaussianBeamletHit) = position(hit.gauss.chief.rays[hit.id]) -direction(hit::GaussianBeamletHit) = direction(hit.gauss.chief.rays[hit.id]) -wavelength(hit::GaussianBeamletHit) = wavelength(hit.gauss.chief.rays[hit.id]) -refractive_index(hit::GaussianBeamletHit) = refractive_index(hit.gauss.chief.rays[hit.id]) +position(hit::AbstractBeamletHit) = hit.p0 +direction(hit::AbstractBeamletHit) = hit.d0 +wavelength(hit::AbstractBeamletHit) = hit.λ +refractive_index(hit::GaussianBeamletHit) = hit.n +refractive_index(hit::AstigmaticGaussianBeamletHit) = hit.n_eff -position(hit::AstigmaticGaussianBeamletHit) = position(hit.agb.c.rays[hit.id]) -direction(hit::AstigmaticGaussianBeamletHit) = direction(hit.agb.c.rays[hit.id]) -wavelength(hit::AstigmaticGaussianBeamletHit) = wavelength(hit.agb.c.rays[hit.id]) -refractive_index(hit::AstigmaticGaussianBeamletHit) = refractive_index(hit.agb.c.rays[hit.id]) +hit_point(hit::AbstractBeamletHit) = position(hit) + hit.l_seg * direction(hit) -function hit_point(hit::GaussianBeamletHit) - position(hit) + length(hit.gauss.chief.rays[hit.id]) * direction(hit) -end -function hit_point(hit::AstigmaticGaussianBeamletHit) - position(hit) + length(hit.agb.c.rays[hit.id]) * direction(hit) -end - -function projection_factor(hit::GaussianBeamletHit) - abs(dot(direction(hit), normal3d(intersection(hit.gauss.chief.rays[hit.id])))) -end -function projection_factor(hit::AstigmaticGaussianBeamletHit) - abs(dot(direction(hit), normal3d(intersection(hit.agb.c.rays[hit.id])))) -end +projection_factor(hit::AbstractBeamletHit) = hit.sqrt_proj^2 """ Detector <: AbstractDetector @@ -257,10 +305,12 @@ of a **left-handed** (x, z) surface coordinate system, where incoming beams inte deterministic. !!! warning "Reading hits" - Read the hits of the detector before moving any object or solving again. - The hits refer to the rays and beams of the current solve, which a new call of - [`solve_system!`](@ref) replaces (child beams and all rays after the first are new objects), - and moving objects afterwards leaves the stored hits inconsistent with the system. + A hit stores the values of the ray or beamlet at the moment of the solve, in world coordinates. + Solving again or moving a source or another object does not change the stored hits. Moving the + detector itself empties it, since its hits would no longer lie on its surface. The hits of + consecutive solves are kept side by side, and it is up to the user when to evaluate them: + [`spot_diagram`](@ref) shows all of them, whereas [`electric_field`](@ref), [`intensity`](@ref) and + [`optical_power`](@ref) superpose all of them coherently, as for beams that exist at the same time. # Fields @@ -473,18 +523,38 @@ needs to evaluate the field of that segment at any point near it. [`Detector`](@ one per beamlet that hits them; it can also be built for any traced segment, e.g. to sample a beamlet on a plane of one's own. `l0` is the length of the beam (including its parents) up to the start of the segment. The projection factor is `1` if the segment ends without an -intersection. +intersection. The hit keeps no reference to `g`: a new solve of `g` does not change it. """ function GaussianBeamletHit(g::GaussianBeamlet{R}, id::Integer) where {R} ray = g.chief.rays[id] l0 = _length_before(g.chief, id) p0 = position(ray) d0 = direction(ray) - sqrt_proj = _projection_factor(ray) + λ = wavelength(g) + n = refractive_index(ray) + # Complex beam parameter q0 = s0 + i z_R at the segment start, along which the beam evolves + # within the segment as q(l) = q0 + l: s0 is the signed distance from the local waist w0 and + # z_R = w0 / F, both from the waist and divergence rays, as in `gauss_parameters` + w, _, s0, w0, F = _local_beam(g, p0, id) + q0, w0 = if isfinite(s0) && isfinite(w0) && w0 > 0 && F > 0 + Complex{R}(s0, w0 / F), R(w0) + else + # no divergence measurable at p0 (e.g. the divergence ray crosses the chief ray there): + # a waist at p0, as the NaN fallback of `gauss_parameters` + Complex{R}(0, π * n * w^2 / λ), R(w) + end + l_seg = length(ray) # Beam radius at the end of the segment, for the auto-limits of the field plots - l_end = isfinite(length(ray)) ? l0 + length(ray) : l0 - w_max, _, _, _ = gauss_parameters(g, l_end) - return GaussianBeamletHit(g, l0, Int(id), _path_excess_before(g, id), _gouy_before(g, id), p0, d0, sqrt_proj, w_max) + w_max = _beam_radius(q0, w0, isfinite(l_seg) ? l_seg : zero(R)) + return GaussianBeamletHit{R}(l0, _path_excess_before(g, id), _gouy_before(g, id), p0, d0, l_seg, + q0, w0, n, λ, electric_field(g) * beam_waist(g), _projection_factor(ray), w_max) +end + +# Radius of a Gaussian beam with the beam parameter `q0` = s0 + i z_R and the waist radius `w0` +# at the distance `l` behind the point of `q0` +function _beam_radius(q0::Complex, w0::Real, l::Real) + s0, z_R = reim(q0) + return w0 * sqrt(1 + ((s0 + l) / z_R)^2) end """ @@ -498,7 +568,8 @@ area within the segments since the start of the root beam; the Gouy phase is fol there as well, i.e. a child beamlet continues its parent. [`Detector`](@ref)s store one per beamlet that hits them; it can also be built for any traced segment, e.g. to sample a beamlet on a plane of one's own. The projection factor is -`1` if the segment ends without an intersection. +`1` if the segment ends without an intersection. The hit keeps no reference to `agb`: a new +solve of `agb` does not change it. """ function AstigmaticGaussianBeamletHit(agb::AstigmaticGaussianBeamlet{R}, id::Integer) where {R} # Pre-calculate cache @@ -546,9 +617,9 @@ function AstigmaticGaussianBeamletHit(agb::AstigmaticGaussianBeamlet{R}, id::Int w_max = max(norm(H1), norm(H2)) n_eff = refractive_index(agb, id) - return AstigmaticGaussianBeamletHit( - agb, R(l0), Int(id), p0, d0, h1, u1, h2, u2, gouy0, ρ1, ρ2, k0, R(Δl), - n_eff, E_ref_amp, sqrt_proj, w_max) + return AstigmaticGaussianBeamletHit{R}( + R(l0), p0, d0, h1, u1, h2, u2, gouy0, ρ1, ρ2, k0, R(Δl), + n_eff, E_ref_amp, sqrt_proj, w_max, length(chief), wavelength(chief), E_vec) end # Geometric length of `beam` (including its parents) up to the start of ray `id` @@ -562,10 +633,7 @@ function _length_before(beam::Beam{T}, id::Integer) where {T} end # √|d·n| of a ray ending on a surface with normal n; 1 without an intersection -function _projection_factor(ray::AbstractRay{T}) where {T} - isnothing(intersection(ray)) && return one(T) - return sqrt(abs(dot(direction(ray), normal3d(intersection(ray))))) -end +_projection_factor(ray::AbstractRay) = sqrt(_projection(ray)) function interact3d(::AbstractSystem, d::Detector, g::GaussianBeamlet{R}, id::Int) where {R} push!(d, GaussianBeamletHit(g, id)) diff --git a/src/OpticalComponents/Detectors/DetectorUtils.jl b/src/OpticalComponents/Detectors/DetectorUtils.jl index 6e3b03e29..72c2e231c 100644 --- a/src/OpticalComponents/Detectors/DetectorUtils.jl +++ b/src/OpticalComponents/Detectors/DetectorUtils.jl @@ -171,7 +171,7 @@ function calc_local_pos( # Use cached parabasal parameters at segment start # Propagate to detector (end of segment) # chief segment length - l_seg = length(rays(hit.agb.c)[hit.id]) + l_seg = hit.l_seg h1 = (hit.h1 + l_seg * hit.u1) * crop_factor h2 = (hit.h2 + l_seg * hit.u2) * crop_factor diff --git a/src/OpticalComponents/Detectors/FieldCalculation.jl b/src/OpticalComponents/Detectors/FieldCalculation.jl index 755bac505..2bcc9f42e 100644 --- a/src/OpticalComponents/Detectors/FieldCalculation.jl +++ b/src/OpticalComponents/Detectors/FieldCalculation.jl @@ -92,13 +92,21 @@ function beamlet_hit_field(hit::GaussianBeamletHit{G}, p::AbstractArray) where { l1 = _pseudo_dot(v, hit.d0) # Transverse distance r - r_vec = v - l1 * hit.d0 - r = norm(r_vec) - - # Distance along beam - z = hit.l0 + l1 - - return _segment_field(hit.gauss, r, z, hit.p0 + l1 * hit.d0, hit.id, hit.l0, hit.Δl0, hit.ψ0) + r = norm(v - l1 * hit.d0) + + # Beam radius, curvature and Gouy phase from q(l1) = q0 + l1 = s + i z_R + s0, z_R = reim(hit.q0) + s = s0 + l1 + w = _beam_radius(hit.q0, hit.w0, l1) + curv = s / (s^2 + z_R^2) + ψ = hit.ψ0 - atan(s / z_R) + + # Optical path up to the point, l0 + l1 along the beam. The curvature term is a geometric + # length like l1, hence it enters with the wavenumber n k0 of the medium as well. + k0 = wavenumber(hit.λ) + n = hit.n + opl = hit.l0 + l1 + hit.Δl0 + (n - 1) * l1 + return hit.E_amp / w * exp(-r^2 / w^2) * cis(k0 * opl + ψ + n * k0 * r^2 * curv / 2) end function beamlet_hit_field(hit::AstigmaticGaussianBeamletHit{G}, p::AbstractArray) where {G} @@ -151,7 +159,7 @@ function beamlet_hit_polarization(hit::AstigmaticGaussianBeamletHit) # `beamlet_hit_field` already carries the complex amplitude `E_ref_amp` (magnitude of the # polarization of the segment and phase of its largest component), so dividing it out # keeps amplitude and phase from being counted twice. - E = polarization(rays(hit.agb.c)[hit.id]) + E = hit.E_pol iszero(hit.E_ref_amp) && return zero(E) return E / hit.E_ref_amp end @@ -308,7 +316,7 @@ function electric_field( hit_data = map(hits) do hit dir = direction(hit) - p_hit = position(hit) + length(hit) * dir + p_hit = hit_point(hit) proj = projection_factor(hit) k = wavenumber(hit) opl = optical_path_length(hit) @@ -375,7 +383,7 @@ function electric_field( hit_data = map(hits) do hit dir = direction(hit) - p_hit = position(hit) + length(hit) * dir + p_hit = hit_point(hit) E0 = polarization(hit) k = wavenumber(hit) opl = optical_path_length(hit) diff --git a/test/Components/TestDetector.jl b/test/Components/TestDetector.jl index 56019df9b..f6789167b 100644 --- a/test/Components/TestDetector.jl +++ b/test/Components/TestDetector.jl @@ -357,6 +357,129 @@ end end end +@testset "Hits are values" begin + # https://github.com/JuliaPhysics/BeamletOptics.jl/issues/42: a hit holds no reference to the + # beam that made it, hence a vector of hits is one block of memory + for H in (BMO.RayHit, BMO.PolarizedRayHit, BMO.GaussianBeamletHit, BMO.AstigmaticGaussianBeamletHit) + @test isbitstype(H{Float64}) + end +end + +@testset "A new solve does not change the stored hits" begin + # https://github.com/JuliaPhysics/BeamletOptics.jl/issues/42 + @testset "Beam rotated between solves" begin + pd = Detector(50mm) + translate3d!(pd, [0, 100mm, 0]) + system = System([pd]) + beam = Beam([0.0, 0, 0], [0.0, 1, 0]) + for _ in 1:5 + solve_system!(system, beam) + zrotate3d!(beam, deg2rad(1)) + end + # the hit of the i-th solve lies 100 mm · tan((i − 1)°) to the side + xs = sort(abs.(first.(spot_diagram(pd)))) + @test xs ≈ 100mm .* tan.(deg2rad.(0:4)) atol = 1e-12 + end + E0 = [1.0, 0, 0] + beams = ( + "Beam" => () -> Beam([0.0, 0, 0], [0.0, 1, 0], 1e-6), + "Beam of PolarizedRays" => () -> Beam([0.0, 0, 0], [0.0, 1, 0], 1e-6, E0), + "GaussianBeamlet" => () -> GaussianBeamlet([0.0, 0, 0], [0.0, 1, 0], 1e-6, 1mm), + "AstigmaticGaussianBeamlet" => () -> AstigmaticGaussianBeamlet([0.0, 0, 0], [0.0, 1, 0], 1e-6, 1mm)) + # the hit is on the first ray, on the second ray behind a mirror, or on a child beam + for (label, beam) in beams, path in (:direct, :mirror, :splitter) + @testset "$label, $path" begin + pd = Detector(20mm) + system = System([pd]) + if path === :mirror + m = RoundPlanoMirror(30mm, 5mm) + zrotate3d!(m, deg2rad(45)) + translate3d!(m, [0, 50mm, 0]) + zrotate3d!(pd, deg2rad(90)) + translate3d!(pd, [100mm, 50mm, 0]) + push!(system, m) + else + translate3d!(pd, [0, 150mm, 0]) + end + if path === :splitter + bs = ThinBeamsplitter(30mm) + zrotate3d!(bs, deg2rad(45)) + translate3d!(bs, [0, 50mm, 0]) + push!(system, bs) + end + b = beam() + solve_system!(system, b) + before = only(BMO.hits(pd)) + # move the source and the mirror or splitter, the beam still hits the detector + translate3d!(b, [0, 0, 0.5mm]) + path === :mirror && translate3d!(m, [5mm, 0, 0]) + path === :splitter && translate3d!(bs, [0, 10mm, 0]) + solve_system!(system, b) + @test BMO.hit_count(pd) == 2 + @test first(BMO.hits(pd)) === before + @test BMO.hit_point(last(BMO.hits(pd)))[3] ≈ 0.5mm + end + end + @testset "Field of a stored GaussianBeamletHit" begin + # free space from the waist: E0 w0 / w(L) · exp(i (k L − atan(L / z_R))) on the axis + λ, w0, L = 1e-6, 0.5mm, 300mm + pd = Detector(20mm) + translate3d!(pd, [0, L, 0]) + system = System([pd]) + gb = GaussianBeamlet([0.0, 0, 0], [0.0, 1, 0], λ, w0) + solve_system!(system, gb) + hit = only(BMO.hits(pd)) + translate3d!(gb, [0, 0, 1mm]) + solve_system!(system, gb) + z_R = π * w0^2 / λ + w = w0 * sqrt(1 + (L / z_R)^2) + E = electric_field(gb) * w0 / w * cis(2π / λ * L - atan(L / z_R)) + @test BMO.beamlet_hit_field(first(BMO.hits(pd)), BMO.Point3(0.0, L, 0.0)) ≈ E + @test first(BMO.hits(pd)) === hit + end +end + +@testset "Moving a detector empties it" begin + pivot = [1mm, 2mm, 3mm] + R = BMO.rotate3d([0.0, 0, 1], 0.1) + moves = ( + "translate3d!" => pd -> translate3d!(pd, [0, 1mm, 0]), + "translate_to3d!" => pd -> translate_to3d!(pd, [0, 120mm, 0]), + "reset_translation3d!" => pd -> reset_translation3d!(pd), + "rotate3d!(R)" => pd -> rotate3d!(pd, R), + "rotate3d!(R, pivot)" => pd -> rotate3d!(pd, R, pivot), + "rotate3d!(axis, θ)" => pd -> rotate3d!(pd, [0.0, 0, 1], 0.1), + "rotate3d!(axis, θ, pivot)" => pd -> rotate3d!(pd, [0.0, 0, 1], 0.1, pivot), + "xrotate3d!" => pd -> xrotate3d!(pd, 0.1), + "yrotate3d!" => pd -> yrotate3d!(pd, 0.1), + "zrotate3d!" => pd -> zrotate3d!(pd, 0.1), + "align3d!" => pd -> align3d!(pd, [0.1, 1, 0]), + "reset_rotation3d!" => pd -> reset_rotation3d!(pd), + "translate3d! of a group" => pd -> translate3d!(ObjectGroup([pd]), [0, 1mm, 0]), + "zrotate3d! of a group" => pd -> zrotate3d!(ObjectGroup([pd]), 0.1)) + for (label, move!) in moves + @testset "$label" begin + pd = Detector(50mm) + translate3d!(pd, [0, 100mm, 0]) + solve_system!(System([pd]), Beam([0.0, 0, 0], [0.0, 1, 0])) + @test BMO.hit_count(pd) == 1 + move!(pd) + @test isnothing(BMO.hits(pd)) + end + end + @testset "Moving other objects keeps the hits" begin + pd = Detector(50mm) + translate3d!(pd, [0, 100mm, 0]) + lens = ThinLens(80mm, 80mm, 4mm, 1.5) + translate3d!(lens, [0, 50mm, 0]) + beam = Beam([0.0, 0, 0], [0.0, 1, 0]) + solve_system!(System([lens, pd]), beam) + translate3d!(lens, [1mm, 0, 0]) + translate3d!(beam, [0, 0, 1mm]) + @test BMO.hit_count(pd) == 1 + end +end + end # TESTSET end # MODULE \ No newline at end of file diff --git a/test/TestOpticsBaseExt.jl b/test/TestOpticsBaseExt.jl index 90e2d9ec6..8f930ffc6 100644 --- a/test/TestOpticsBaseExt.jl +++ b/test/TestOpticsBaseExt.jl @@ -454,18 +454,18 @@ end end @testset "PlaneField of one traced beamlet segment" begin - det = trace!(detector_at(zdet), agb_at()) - hit = only(BMO.hits(det)) + agb = agb_at() + det = trace!(detector_at(zdet), agb) fd = PlaneField(det; size = (48, 48), spacing = (w0 / 6, w0 / 6), progress = false) - fb = PlaneField(hit.agb, hit.id; size = (48, 48), spacing = (w0 / 6, w0 / 6), + fb = PlaneField(agb, length(BMO.rays(agb.c)); size = (48, 48), spacing = (w0 / 6, w0 / 6), origin = fd.origin, axes = fd.axes) @test fb.E == fd.E && fb.H == fd.H @test fb.origin == fd.origin && fb.axes == fd.axes && fb.n == fd.n # stigmatic beamlets too - det = trace!(detector_at(zdet), BMO.GaussianBeamlet([0.0, 0, 0], [0.0, 1, 0], λ, w0; P0)) - hit = only(BMO.hits(det)) + gb = BMO.GaussianBeamlet([0.0, 0, 0], [0.0, 1, 0], λ, w0; P0) + det = trace!(detector_at(zdet), gb) fd = PlaneField(det; size = (32, 32), spacing = (w0 / 4, w0 / 4), progress = false) - fb = PlaneField(hit.gauss, hit.id; size = (32, 32), spacing = (w0 / 4, w0 / 4), + fb = PlaneField(gb, length(BMO.rays(gb.chief)); size = (32, 32), spacing = (w0 / 4, w0 / 4), origin = fd.origin, axes = fd.axes) @test fb.E == fd.E end From 7f641ececf49bb33f6e96e33afe013e1aea15c53 Mon Sep 17 00:00:00 2001 From: uitt_hu Date: Fri, 16 Oct 2026 16:14:58 +0200 Subject: [PATCH 2/6] GaussianBeamletHit: q0 from gauss_parameters, no new internal function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The beam parameter q0 = s0 + i z_R at the segment start follows from the curvature c and the local Gouy phase ψ that gauss_parameters already returns: q0 = −i sin(ψ) exp(iψ) / c. This keeps the invariant of the waist and divergence rays, as _local_beam did, so gauss_parameters is unchanged against master. Co-Authored-By: Claude Opus 5.5 --- src/Gaussian.jl | 28 ++++++--------------- src/OpticalComponents/Detectors/Detector.jl | 15 ++++++----- 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/src/Gaussian.jl b/src/Gaussian.jl index fd6c2e6f9..f152e6b71 100644 --- a/src/Gaussian.jl +++ b/src/Gaussian.jl @@ -301,26 +301,6 @@ function gauss_parameters( hint = point_on_beam(gauss, z) ) p0, index = hint - w, R, z, w0, _ = _local_beam(gauss, p0, index) - ψ = -atan(1, √(1 / (R * z) - 1)) - # Catch NaNs and correct Gouy phase sign based on curvature sign - isnan(R) && (R = zero(R)) - isnan(ψ) && (ψ = zero(ψ)) - isnan(w0) && (w0 = w) - R < 0 && (ψ = -ψ) - return (w, R, ψ + ψ0, w0) -end - -""" - _local_beam(gauss::GaussianBeamlet, p0, index) - -Parameters of the segment `index` of `gauss` on the plane through `p0` normal to its chief ray, -from the heights and slopes of the waist and divergence rays (Arnaud 1985, DeJager 1992): beam -radius `w`, curvature `R` (1/radius of curvature), signed distance `z` from the local waist, waist -radius `w0` and far-field divergence `F` (`w² = w0² + F² z²` within the segment). Values can be NaN, -e.g. at a plane where the divergence ray crosses the chief ray. -""" -function _local_beam(gauss::GaussianBeamlet, p0, index) chief = gauss.chief.rays[index] div = gauss.divergence.rays[index] waist = gauss.waist.rays[index] @@ -362,8 +342,14 @@ function _local_beam(gauss::GaussianBeamlet, p0, index) w = sqrt(y_d^2 + y_w^2) R = E_kt / w^2 z = E_kt / F_kt^2 + ψ = -atan(1, √(1 / (R * z) - 1)) w0 = H / (n * F_kt) - return (w, R, z, w0, F_kt) + # Catch NaNs and correct Gouy phase sign based on curvature sign + isnan(R) && (R = zero(R)) + isnan(ψ) && (ψ = zero(ψ)) + isnan(w0) && (w0 = w) + R < 0 && (ψ = -ψ) + return (w, R, ψ + ψ0, w0) end """ diff --git a/src/OpticalComponents/Detectors/Detector.jl b/src/OpticalComponents/Detectors/Detector.jl index 03d86054d..1edb1811e 100644 --- a/src/OpticalComponents/Detectors/Detector.jl +++ b/src/OpticalComponents/Detectors/Detector.jl @@ -533,15 +533,14 @@ function GaussianBeamletHit(g::GaussianBeamlet{R}, id::Integer) where {R} λ = wavelength(g) n = refractive_index(ray) # Complex beam parameter q0 = s0 + i z_R at the segment start, along which the beam evolves - # within the segment as q(l) = q0 + l: s0 is the signed distance from the local waist w0 and - # z_R = w0 / F, both from the waist and divergence rays, as in `gauss_parameters` - w, _, s0, w0, F = _local_beam(g, p0, id) - q0, w0 = if isfinite(s0) && isfinite(w0) && w0 > 0 && F > 0 - Complex{R}(s0, w0 / F), R(w0) + # within the segment as q(l) = q0 + l. From the local Gouy phase ψ = −atan(s0 / z_R) and the + # curvature c = s0 / (s0² + z_R²) of `gauss_parameters`: q0 = −i sin(ψ) exp(iψ) / c + _, curv, ψ, w0 = gauss_parameters(g, l0, zero(R); hint = (p0, id)) + q0 = if iszero(curv) + # waist at p0 (curvature and Gouy phase 0) + Complex{R}(0, π * n * w0^2 / λ) else - # no divergence measurable at p0 (e.g. the divergence ray crosses the chief ray there): - # a waist at p0, as the NaN fallback of `gauss_parameters` - Complex{R}(0, π * n * w^2 / λ), R(w) + Complex{R}(-im * sin(ψ) * cis(ψ) / curv) end l_seg = length(ray) # Beam radius at the end of the segment, for the auto-limits of the field plots From 01ff78aa983a2474e1ffcec6f15d6da7db928168 Mon Sep 17 00:00:00 2001 From: uitt_hu Date: Sat, 17 Oct 2026 17:37:32 +0200 Subject: [PATCH 3/6] Review of #164: validity of the stigmatic hit, no position of beamlet hits MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Docstring of GaussianBeamletHit and the stigmatic beam page: the detector field agrees with the waist and divergence rays to about 1e-6 where the stigmatic model applies; near the focus of a beam made astigmatic by a tilted or decentered lens the radius can differ by tens of percent - position(::AbstractBeamletHit) removed, as for ray hits: it returned the segment start, not the hit point; hit_point reads the fields - _projection_factor renamed _sqrt_projection, it returns √|d·n| Co-Authored-By: Claude Opus 5.5 --- docs/src/basics/beams/stigmatic_beam.md | 3 +++ src/OpticalComponents/Detectors/Detector.jl | 14 +++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/docs/src/basics/beams/stigmatic_beam.md b/docs/src/basics/beams/stigmatic_beam.md index 646f5933f..d37d42ba3 100644 --- a/docs/src/basics/beams/stigmatic_beam.md +++ b/docs/src/basics/beams/stigmatic_beam.md @@ -60,4 +60,7 @@ Once a `GaussianBeamlet` has been traced through an optical system, several para The user can obtain parameters such as the beam waist radius, the radius of curvature and more using the [`gauss_parameters`](@ref) and/or [`waist_parameters`](@ref) functions. Below the local waist radius and curvature ``R = r^{-1}`` have been calculated for the example above. +!!! warning "Outside the stigmatic model" + A [`Detector`](@ref) evaluates a beamlet segment from its complex beam parameter at the segment start, whereas [`gauss_parameters`](@ref) evaluates the waist and divergence rays at every plane. Where the stigmatic model applies, both agree to about ``10^{-6}``. Behind a tilted or decentered lens the beam becomes astigmatic and the auxiliary rays no longer cross: near the focus of such a beam the beam radius of the two can differ by tens of percent, and neither is right. Use an [`AstigmaticGaussianBeamlet`](@ref) there. + ![Gauss beam parameters](gauss_parameters.png) \ No newline at end of file diff --git a/src/OpticalComponents/Detectors/Detector.jl b/src/OpticalComponents/Detectors/Detector.jl index 1edb1811e..f50f80483 100644 --- a/src/OpticalComponents/Detectors/Detector.jl +++ b/src/OpticalComponents/Detectors/Detector.jl @@ -200,6 +200,11 @@ the geometric path and `ψ0` the Gouy phase that the beam has collected up to th segment, of refractive index `n`, the beam follows the complex beam parameter `q0 = s0 + i z_R` at `p0`: `s0` is the signed distance from the local waist of radius `w0`. `E_amp` is the field amplitude times the beam radius, `E0 w0` of the source. + +Where the stigmatic model applies, the field agrees with the waist and divergence rays of the +beamlet (see [`gauss_parameters`](@ref)) to about `1e-6`. Near the focus of a beam made +astigmatic by a tilted or decentered lens the beam radius can differ by tens of percent; use +an [`AstigmaticGaussianBeamlet`](@ref) there. """ struct GaussianBeamletHit{T} <: AbstractBeamletHit{T} l0::T @@ -252,13 +257,12 @@ struct AstigmaticGaussianBeamletHit{T} <: AbstractBeamletHit{T} E_pol::Point3{Complex{T}} end -position(hit::AbstractBeamletHit) = hit.p0 direction(hit::AbstractBeamletHit) = hit.d0 wavelength(hit::AbstractBeamletHit) = hit.λ refractive_index(hit::GaussianBeamletHit) = hit.n refractive_index(hit::AstigmaticGaussianBeamletHit) = hit.n_eff -hit_point(hit::AbstractBeamletHit) = position(hit) + hit.l_seg * direction(hit) +hit_point(hit::AbstractBeamletHit) = hit.p0 + hit.l_seg * hit.d0 projection_factor(hit::AbstractBeamletHit) = hit.sqrt_proj^2 @@ -546,7 +550,7 @@ function GaussianBeamletHit(g::GaussianBeamlet{R}, id::Integer) where {R} # Beam radius at the end of the segment, for the auto-limits of the field plots w_max = _beam_radius(q0, w0, isfinite(l_seg) ? l_seg : zero(R)) return GaussianBeamletHit{R}(l0, _path_excess_before(g, id), _gouy_before(g, id), p0, d0, l_seg, - q0, w0, n, λ, electric_field(g) * beam_waist(g), _projection_factor(ray), w_max) + q0, w0, n, λ, electric_field(g) * beam_waist(g), _sqrt_projection(ray), w_max) end # Radius of a Gaussian beam with the beam parameter `q0` = s0 + i z_R and the waist radius `w0` @@ -576,7 +580,7 @@ function AstigmaticGaussianBeamletHit(agb::AstigmaticGaussianBeamlet{R}, id::Int p0 = position(chief) d0 = direction(chief) k0 = 2π / wavelength(chief) - sqrt_proj = _projection_factor(chief) + sqrt_proj = _sqrt_projection(chief) # Parabasal parameters at segment start (p0) h1, u1, h2, u2, _ = parabasal_ray_parameters(agb, p0, id) @@ -632,7 +636,7 @@ function _length_before(beam::Beam{T}, id::Integer) where {T} end # √|d·n| of a ray ending on a surface with normal n; 1 without an intersection -_projection_factor(ray::AbstractRay) = sqrt(_projection(ray)) +_sqrt_projection(ray::AbstractRay) = sqrt(_projection(ray)) function interact3d(::AbstractSystem, d::Detector, g::GaussianBeamlet{R}, id::Int) where {R} push!(d, GaussianBeamletHit(g, id)) From bc9a7010340c28dbc9896ec6ad846561b7ebf782 Mon Sep 17 00:00:00 2001 From: uitt_hu Date: Sat, 17 Oct 2026 18:34:19 +0200 Subject: [PATCH 4/6] GaussianBeamletHit: waist and divergence rays as values instead of q0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Against an AstigmaticGaussianBeamlet, the closed form from q0 at the segment start focused a beam behind a tilted, decentered lens to the diffraction limit (9.4 µm at 2° tilt, AGB 33 µm, master 28 µm): it drops that the waist and divergence rays no longer cross. The hit stores the two rays instead, as offsets y0 + l v from the chief ray on the planes normal to it (GaussianSegment, isbits), and gauss_parameters(segment, l) evaluates them with the formula of Arnaud and DeJager. gauss_parameters(gauss, z, ψ0; hint) uses the same method, so a hit and its beamlet give the same field; w, R and ψ equal master to 1e-9. The slopes are cot θ = (ŷ·a)/|ŷ × a| instead of tan(π/2 − acos(ŷ·a)), and the plane crossings are linear in l, so no line-plane intersection per evaluation. The validity notes of the previous commit are removed, the hit and gauss_parameters no longer differ. Co-Authored-By: Claude Opus 5.5 --- docs/src/basics/beams/stigmatic_beam.md | 3 - src/Gaussian.jl | 96 +++++++++++++------ src/OpticalComponents/Detectors/Detector.jl | 47 +++------ .../Detectors/FieldCalculation.jl | 13 +-- test/Components/TestDetector.jl | 3 +- 5 files changed, 86 insertions(+), 76 deletions(-) diff --git a/docs/src/basics/beams/stigmatic_beam.md b/docs/src/basics/beams/stigmatic_beam.md index d37d42ba3..646f5933f 100644 --- a/docs/src/basics/beams/stigmatic_beam.md +++ b/docs/src/basics/beams/stigmatic_beam.md @@ -60,7 +60,4 @@ Once a `GaussianBeamlet` has been traced through an optical system, several para The user can obtain parameters such as the beam waist radius, the radius of curvature and more using the [`gauss_parameters`](@ref) and/or [`waist_parameters`](@ref) functions. Below the local waist radius and curvature ``R = r^{-1}`` have been calculated for the example above. -!!! warning "Outside the stigmatic model" - A [`Detector`](@ref) evaluates a beamlet segment from its complex beam parameter at the segment start, whereas [`gauss_parameters`](@ref) evaluates the waist and divergence rays at every plane. Where the stigmatic model applies, both agree to about ``10^{-6}``. Behind a tilted or decentered lens the beam becomes astigmatic and the auxiliary rays no longer cross: near the focus of such a beam the beam radius of the two can differ by tens of percent, and neither is right. Use an [`AstigmaticGaussianBeamlet`](@ref) there. - ![Gauss beam parameters](gauss_parameters.png) \ No newline at end of file diff --git a/src/Gaussian.jl b/src/Gaussian.jl index f152e6b71..c355f50f1 100644 --- a/src/Gaussian.jl +++ b/src/Gaussian.jl @@ -302,40 +302,80 @@ function gauss_parameters( ) p0, index = hint chief = gauss.chief.rays[index] - div = gauss.divergence.rays[index] - waist = gauss.waist.rays[index] + w, R, ψ, w0 = gauss_parameters(GaussianSegment(gauss, index), dot(p0 - position(chief), direction(chief))) + return (w, R, ψ + ψ0, w0) +end + +""" + GaussianSegment{T} + +The waist and divergence rays of one segment of a [`GaussianBeamlet`](@ref), as values relative +to its chief ray: on the plane normal to the chief ray at the distance `l` behind its start, an +auxiliary ray with direction `a` crosses at the transverse offset `y0 + l * v` from the chief ray. +Both rays are straight within the segment, hence this holds exactly. `n` is the refractive index +of the segment and `λ` the vacuum wavelength. [`gauss_parameters`](@ref)`(segment, l)` evaluates it, +a [`GaussianBeamletHit`](@ref) stores it. +""" +struct GaussianSegment{T} + y0_w::Point3{T} + v_w::Point3{T} + a_w::Point3{T} + y0_d::Point3{T} + v_d::Point3{T} + a_d::Point3{T} + n::T + λ::T +end + +function GaussianSegment(gauss::GaussianBeamlet{T}, index::Integer) where {T} + chief = gauss.chief.rays[index] + p, d = position(chief), direction(chief) + y0_w, v_w, a_w = _crossing(p, d, gauss.waist.rays[index]) + y0_d, v_d, a_d = _crossing(p, d, gauss.divergence.rays[index]) + return GaussianSegment{T}(y0_w, v_w, a_w, y0_d, v_d, a_d, refractive_index(chief), wavelength(gauss)) +end + +# Offset `y0 + l v` from the chief ray (start `p`, direction `d`) at which `ray` crosses the plane +# normal to `d` at the distance `l` behind `p`, and the direction of `ray` +function _crossing(p, d, ray) + a = direction(ray) + c = dot(d, a) + return position(ray) + dot(p - position(ray), d) / c * a - p, a / c - d, a +end + +# Height and slope of an auxiliary ray with the offset `y0 + l v` and the direction `a`: the slope +# is tan(π/2 − θ) = cot θ of the angle θ between the offset and `a` +function _height_slope(y0, v, a, l) + y = y0 + l * v + h = norm(y) + ŷ = y / h + return h, dot(ŷ, a) / norm(cross(ŷ, a)) +end + +""" + gauss_parameters(segment::GaussianSegment, l) + +Returns `(w, R, ψ, w0)` of the [`GaussianSegment`](@ref) at the distance `l` behind the start of +its chief ray, as [`gauss_parameters`](@ref)`(gauss, z)` does, but with the Gouy phase `ψ` relative +to the local waist of the segment. +""" +function gauss_parameters(s::GaussianSegment, l::Real) #= - Divergence ray height and slope (same for waist ray) - - find divergence ray "height" and "slope" at intersection point y0 with target plane at p0 of chief ray - - ray height "y_d" is length between p0 and y0 - - ray slope "m_d" is angle between vector p0 -> y0 and divergence ray direction -> gives unambiguous angle for signed ray slope calculation - - fails if y_d is zero -> catch R=Inf, ψ=0 and H=λ/π + Height y and slope m of the divergence and waist rays on the plane at l + - the height is the distance between the chief ray and the crossing point of the ray + - the slope is the cotangent of the angle between the offset and the ray direction, which + gives the signed slope + - fails if a height is zero -> catch R=Inf, ψ=0 and H=λ/π =# - intersection_len = line_plane_distance3d(p0, - direction(chief), - position(div), - direction(div)) - y0 = position(div) + intersection_len * direction(div) - p0 - y_d = norm(y0) - y0 /= y_d - m_d = tan(π / 2 - angle3d(y0, direction(div))) - # Waist ray height and slope - intersection_len = line_plane_distance3d(p0, - direction(chief), - position(waist), - direction(waist)) - y0 = position(waist) + intersection_len * direction(waist) - p0 - y_w = norm(y0) - y0 /= y_w - m_w = tan(π / 2 - angle3d(y0, direction(waist))) + y_d, m_d = _height_slope(s.y0_d, s.v_d, s.a_d, l) + y_w, m_w = _height_slope(s.y0_w, s.v_w, s.a_w, l) # Beam parameters as per Arnaud (1985) and DeJager (1992) - n = refractive_index(chief) + n = s.n H = abs(n * (y_w * m_d - y_d * m_w)) # Test optical invariant - λ = wavelength(gauss) + λ = s.λ if !isapprox(H, λ / π, atol = Config.get_invariant_threshold()) H = λ / π - # println("H not fulfilled at z=$z") end E_kt = y_d * m_d + y_w * m_w F_kt = sqrt(m_d^2 + m_w^2) @@ -349,7 +389,7 @@ function gauss_parameters( isnan(ψ) && (ψ = zero(ψ)) isnan(w0) && (w0 = w) R < 0 && (ψ = -ψ) - return (w, R, ψ + ψ0, w0) + return (w, R, ψ, w0) end """ diff --git a/src/OpticalComponents/Detectors/Detector.jl b/src/OpticalComponents/Detectors/Detector.jl index f50f80483..8d863f65f 100644 --- a/src/OpticalComponents/Detectors/Detector.jl +++ b/src/OpticalComponents/Detectors/Detector.jl @@ -196,15 +196,10 @@ abstract type AbstractBeamletHit{T} <: AbstractDetectorHit end Stores the segment of a [`GaussianBeamlet`](@ref) that hit the detector as values, without a reference to the beamlet: `p0` and `d0` are start and direction of the segment, `l_seg` its length, `l0` the length of the beam (including its parents) up to `p0`, `Δl0` the optical minus -the geometric path and `ψ0` the Gouy phase that the beam has collected up to there. Within the -segment, of refractive index `n`, the beam follows the complex beam parameter `q0 = s0 + i z_R` -at `p0`: `s0` is the signed distance from the local waist of radius `w0`. `E_amp` is the field -amplitude times the beam radius, `E0 w0` of the source. - -Where the stigmatic model applies, the field agrees with the waist and divergence rays of the -beamlet (see [`gauss_parameters`](@ref)) to about `1e-6`. Near the focus of a beam made -astigmatic by a tilted or decentered lens the beam radius can differ by tens of percent; use -an [`AstigmaticGaussianBeamlet`](@ref) there. +the geometric path and `ψ0` the Gouy phase that the beam has collected up to there. `segment` +holds the waist and divergence rays of the segment (see [`BeamletOptics.GaussianSegment`](@ref)), +from which the field follows as from the beamlet itself, see [`gauss_parameters`](@ref). +`E_amp` is the field amplitude times the beam radius, `E0 w0` of the source. """ struct GaussianBeamletHit{T} <: AbstractBeamletHit{T} l0::T @@ -213,10 +208,7 @@ struct GaussianBeamletHit{T} <: AbstractBeamletHit{T} p0::Point3{T} d0::Point3{T} l_seg::T - q0::Complex{T} - w0::T - n::T - λ::T + segment::GaussianSegment{T} E_amp::Complex{T} sqrt_proj::T w_max::T @@ -258,8 +250,9 @@ struct AstigmaticGaussianBeamletHit{T} <: AbstractBeamletHit{T} end direction(hit::AbstractBeamletHit) = hit.d0 -wavelength(hit::AbstractBeamletHit) = hit.λ -refractive_index(hit::GaussianBeamletHit) = hit.n +wavelength(hit::GaussianBeamletHit) = hit.segment.λ +wavelength(hit::AstigmaticGaussianBeamletHit) = hit.λ +refractive_index(hit::GaussianBeamletHit) = hit.segment.n refractive_index(hit::AstigmaticGaussianBeamletHit) = hit.n_eff hit_point(hit::AbstractBeamletHit) = hit.p0 + hit.l_seg * hit.d0 @@ -534,30 +527,12 @@ function GaussianBeamletHit(g::GaussianBeamlet{R}, id::Integer) where {R} l0 = _length_before(g.chief, id) p0 = position(ray) d0 = direction(ray) - λ = wavelength(g) - n = refractive_index(ray) - # Complex beam parameter q0 = s0 + i z_R at the segment start, along which the beam evolves - # within the segment as q(l) = q0 + l. From the local Gouy phase ψ = −atan(s0 / z_R) and the - # curvature c = s0 / (s0² + z_R²) of `gauss_parameters`: q0 = −i sin(ψ) exp(iψ) / c - _, curv, ψ, w0 = gauss_parameters(g, l0, zero(R); hint = (p0, id)) - q0 = if iszero(curv) - # waist at p0 (curvature and Gouy phase 0) - Complex{R}(0, π * n * w0^2 / λ) - else - Complex{R}(-im * sin(ψ) * cis(ψ) / curv) - end + segment = GaussianSegment(g, id) l_seg = length(ray) # Beam radius at the end of the segment, for the auto-limits of the field plots - w_max = _beam_radius(q0, w0, isfinite(l_seg) ? l_seg : zero(R)) + w_max = first(gauss_parameters(segment, isfinite(l_seg) ? l_seg : zero(R))) return GaussianBeamletHit{R}(l0, _path_excess_before(g, id), _gouy_before(g, id), p0, d0, l_seg, - q0, w0, n, λ, electric_field(g) * beam_waist(g), _sqrt_projection(ray), w_max) -end - -# Radius of a Gaussian beam with the beam parameter `q0` = s0 + i z_R and the waist radius `w0` -# at the distance `l` behind the point of `q0` -function _beam_radius(q0::Complex, w0::Real, l::Real) - s0, z_R = reim(q0) - return w0 * sqrt(1 + ((s0 + l) / z_R)^2) + segment, electric_field(g) * beam_waist(g), _sqrt_projection(ray), w_max) end """ diff --git a/src/OpticalComponents/Detectors/FieldCalculation.jl b/src/OpticalComponents/Detectors/FieldCalculation.jl index 2bcc9f42e..f2a1d0db0 100644 --- a/src/OpticalComponents/Detectors/FieldCalculation.jl +++ b/src/OpticalComponents/Detectors/FieldCalculation.jl @@ -94,17 +94,14 @@ function beamlet_hit_field(hit::GaussianBeamletHit{G}, p::AbstractArray) where { # Transverse distance r r = norm(v - l1 * hit.d0) - # Beam radius, curvature and Gouy phase from q(l1) = q0 + l1 = s + i z_R - s0, z_R = reim(hit.q0) - s = s0 + l1 - w = _beam_radius(hit.q0, hit.w0, l1) - curv = s / (s^2 + z_R^2) - ψ = hit.ψ0 - atan(s / z_R) + # Beam radius, curvature and Gouy phase from the waist and divergence rays of the segment + w, curv, ψ, _ = gauss_parameters(hit.segment, l1) + ψ += hit.ψ0 # Optical path up to the point, l0 + l1 along the beam. The curvature term is a geometric # length like l1, hence it enters with the wavenumber n k0 of the medium as well. - k0 = wavenumber(hit.λ) - n = hit.n + k0 = wavenumber(hit.segment.λ) + n = hit.segment.n opl = hit.l0 + l1 + hit.Δl0 + (n - 1) * l1 return hit.E_amp / w * exp(-r^2 / w^2) * cis(k0 * opl + ψ + n * k0 * r^2 * curv / 2) end diff --git a/test/Components/TestDetector.jl b/test/Components/TestDetector.jl index f6789167b..9ef431b70 100644 --- a/test/Components/TestDetector.jl +++ b/test/Components/TestDetector.jl @@ -434,7 +434,8 @@ end z_R = π * w0^2 / λ w = w0 * sqrt(1 + (L / z_R)^2) E = electric_field(gb) * w0 / w * cis(2π / λ * L - atan(L / z_R)) - @test BMO.beamlet_hit_field(first(BMO.hits(pd)), BMO.Point3(0.0, L, 0.0)) ≈ E + # the waist and divergence rays give the Gouy phase to about 1e-7 rad + @test BMO.beamlet_hit_field(first(BMO.hits(pd)), BMO.Point3(0.0, L, 0.0)) ≈ E rtol = 1e-6 @test first(BMO.hits(pd)) === hit end end From 67c058aff075fe262b3c6033fc5c98b10e205395 Mon Sep 17 00:00:00 2001 From: uitt_hu Date: Sun, 18 Oct 2026 22:41:52 +0200 Subject: [PATCH 5/6] hit_point and projection_factor of a ray instead of internal helpers _ray_end and _projection computed what hit_point and projection_factor already name for hits; they are now methods of these verbs for an AbstractRay. _sqrt_projection is gone, the two beamlet hits take sqrt(projection_factor(ray)). Co-Authored-By: Claude Opus 5.5 --- src/OpticalComponents/Detectors/Detector.jl | 26 +++++++++++---------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/OpticalComponents/Detectors/Detector.jl b/src/OpticalComponents/Detectors/Detector.jl index 8d863f65f..784335dcc 100644 --- a/src/OpticalComponents/Detectors/Detector.jl +++ b/src/OpticalComponents/Detectors/Detector.jl @@ -148,7 +148,7 @@ struct RayHit{T} <: AbstractRayHit{T} end RayHit(ray::Ray{T}, opl::Real) where {T} = - RayHit{T}(_ray_end(ray), direction(ray), opl, wavelength(ray), refractive_index(ray), _projection(ray)) + RayHit{T}(hit_point(ray), direction(ray), opl, wavelength(ray), refractive_index(ray), projection_factor(ray)) """ PolarizedRayHit(ray::PolarizedRay, opl) @@ -167,16 +167,21 @@ struct PolarizedRayHit{T} <: AbstractRayHit{T} end PolarizedRayHit(ray::PolarizedRay{T}, opl::Real) where {T} = - PolarizedRayHit{T}(_ray_end(ray), direction(ray), opl, wavelength(ray), refractive_index(ray), - _projection(ray), polarization(ray)) + PolarizedRayHit{T}(hit_point(ray), direction(ray), opl, wavelength(ray), refractive_index(ray), + projection_factor(ray), polarization(ray)) polarization(hit::PolarizedRayHit) = hit.E -# End point of a ray that hit a surface -_ray_end(ray::AbstractRay) = position(ray) + length(ray) * direction(ray) +"Returns the point where the `ray` ends at its [`Intersection`](@ref) with a surface." +hit_point(ray::AbstractRay) = position(ray) + length(ray) * direction(ray) -# |d·n| of a ray ending on a surface with normal n; 1 without an intersection -function _projection(ray::AbstractRay{T}) where {T} +""" + projection_factor(ray::AbstractRay) + +Returns `|d · n|` of the direction `d` of the `ray` and the normal `n` of the surface it ends on; +`1` if the ray ends without an intersection. +""" +function projection_factor(ray::AbstractRay{T}) where {T} isnothing(intersection(ray)) && return one(T) return abs(dot(direction(ray), normal3d(intersection(ray)))) end @@ -532,7 +537,7 @@ function GaussianBeamletHit(g::GaussianBeamlet{R}, id::Integer) where {R} # Beam radius at the end of the segment, for the auto-limits of the field plots w_max = first(gauss_parameters(segment, isfinite(l_seg) ? l_seg : zero(R))) return GaussianBeamletHit{R}(l0, _path_excess_before(g, id), _gouy_before(g, id), p0, d0, l_seg, - segment, electric_field(g) * beam_waist(g), _sqrt_projection(ray), w_max) + segment, electric_field(g) * beam_waist(g), sqrt(projection_factor(ray)), w_max) end """ @@ -555,7 +560,7 @@ function AstigmaticGaussianBeamletHit(agb::AstigmaticGaussianBeamlet{R}, id::Int p0 = position(chief) d0 = direction(chief) k0 = 2π / wavelength(chief) - sqrt_proj = _sqrt_projection(chief) + sqrt_proj = sqrt(projection_factor(chief)) # Parabasal parameters at segment start (p0) h1, u1, h2, u2, _ = parabasal_ray_parameters(agb, p0, id) @@ -610,9 +615,6 @@ function _length_before(beam::Beam{T}, id::Integer) where {T} return l end -# √|d·n| of a ray ending on a surface with normal n; 1 without an intersection -_sqrt_projection(ray::AbstractRay) = sqrt(_projection(ray)) - function interact3d(::AbstractSystem, d::Detector, g::GaussianBeamlet{R}, id::Int) where {R} push!(d, GaussianBeamletHit(g, id)) if stop(d) From 66437e98ebccde9b33ccb5d4165209e4607dc753 Mon Sep 17 00:00:00 2001 From: uitt_hu Date: Sun, 18 Oct 2026 22:55:47 +0200 Subject: [PATCH 6/6] hit_point(ray) for the end point of a ray throughout The end point position(ray) + length(ray) * direction(ray) was written out in 15 places (beamsplitters, lenses, mirrors, polarizers, the Gaussian beamlet, Base.show of a beam). hit_point and projection_factor of an AbstractRay move to the ray interface in AbstractRay.jl, and these places call hit_point(ray). Co-Authored-By: Claude Opus 5.5 --- src/AbstractTypes/AbstractRay.jl | 18 ++++++++++++++++++ src/Beam.jl | 2 +- src/Gaussian.jl | 2 +- .../Beamsplitters/ThinBeamsplitter.jl | 8 ++++---- src/OpticalComponents/Detectors/Detector.jl | 14 -------------- src/OpticalComponents/Lenses.jl | 4 ++-- src/OpticalComponents/Mirrors/Mirrors.jl | 4 ++-- .../Polarizers/LinearPolarizer.jl | 6 +++--- .../Polarizers/PolarizationFilter.jl | 4 ++-- 9 files changed, 33 insertions(+), 29 deletions(-) diff --git a/src/AbstractTypes/AbstractRay.jl b/src/AbstractTypes/AbstractRay.jl index 70de6c51c..0118e446d 100644 --- a/src/AbstractTypes/AbstractRay.jl +++ b/src/AbstractTypes/AbstractRay.jl @@ -129,6 +129,24 @@ end Base.empty!(ray::AbstractRay) = intersection!(ray, nothing) +""" + hit_point(ray::AbstractRay) + +Returns the point where the `ray` ends, i.e. where it hits the surface of its [`Intersection`](@ref). +""" +hit_point(ray::AbstractRay) = position(ray) + length(ray) * direction(ray) + +""" + projection_factor(ray::AbstractRay) + +Returns `|d · n|` of the direction `d` of the `ray` and the normal `n` of the surface it ends on; +`1` if the ray ends without an intersection. +""" +function projection_factor(ray::AbstractRay{T}) where {T} + isnothing(intersection(ray)) && return one(T) + return abs(dot(direction(ray), normal3d(intersection(ray)))) +end + """ translate3d!(::Movable, ray::AbstractRay, offset) diff --git a/src/Beam.jl b/src/Beam.jl index 0bdf06fd6..8b5e47aa9 100644 --- a/src/Beam.jl +++ b/src/Beam.jl @@ -247,7 +247,7 @@ function Base.show(io::IO, ::MIME"text/plain", beam::Beam) println(io, " Intersects with $(typeof(object(intersection(ray))))") println(io, " Pos.: $(position(ray))") println(io, " Dir.: $(direction(ray))") - println(io, " End.: $(position(ray) .+ length(ray) .* direction(ray))") + println(io, " End.: $(hit_point(ray))") end end return nothing diff --git a/src/Gaussian.jl b/src/Gaussian.jl index c355f50f1..6ce768214 100644 --- a/src/Gaussian.jl +++ b/src/Gaussian.jl @@ -477,7 +477,7 @@ _local_gouy_start(gauss::GaussianBeamlet{T}, index::Integer) where {T} = function _local_gouy_end(gauss::GaussianBeamlet{T}, index::Integer) where {T} ray = rays(gauss.chief)[index] return gauss_parameters(gauss, zero(T), zero(T); - hint = (position(ray) + length(ray) * direction(ray), index))[3] + hint = (hit_point(ray), index))[3] end """ diff --git a/src/OpticalComponents/Beamsplitters/ThinBeamsplitter.jl b/src/OpticalComponents/Beamsplitters/ThinBeamsplitter.jl index 11d99d6ab..d386471cb 100644 --- a/src/OpticalComponents/Beamsplitters/ThinBeamsplitter.jl +++ b/src/OpticalComponents/Beamsplitters/ThinBeamsplitter.jl @@ -74,7 +74,7 @@ Base.isvalid(bs::AbstractBeamsplitter) = reflectance(bs)^2 + transmittance(bs)^2 @inline function _beamsplitter_transmitted_beam( ::AbstractBeamsplitter, ::Beam{T, R}, ray::R) where {T <: Real, R <: Ray{T}} - pos = position(ray) + length(ray) * direction(ray) + pos = hit_point(ray) dir = direction(ray) return Beam(Ray(pos, dir, wavelength(ray))) end @@ -82,7 +82,7 @@ end @inline function _beamsplitter_reflected_beam( ::AbstractBeamsplitter, ::Beam{T, R}, ray::R) where {T <: Real, R <: Ray{T}} normal = normal3d(intersection(ray)) - pos = position(ray) + length(ray) * direction(ray) + pos = hit_point(ray) dir = reflection3d(direction(ray), normal) return Beam(Ray(pos, dir, wavelength(ray))) end @@ -90,7 +90,7 @@ end @inline function _beamsplitter_transmitted_beam(bs::AbstractBeamsplitter, ::Beam{T, R}, ray::R) where {T <: Real, R <: PolarizedRay{T}} J = SPBasis(transmittance(bs), 0, 0, transmittance(bs)) - pos = position(ray) + length(ray) * direction(ray) + pos = hit_point(ray) dir = direction(ray) E0 = _calculate_global_E0(bs, ray, dir, J) return Beam(PolarizedRay(pos, dir, wavelength(ray), E0)) @@ -100,7 +100,7 @@ end ray::R) where {T <: Real, R <: PolarizedRay{T}} J = SPBasis(-reflectance(bs), 0, 0, reflectance(bs)) normal = normal3d(intersection(ray)) - pos = position(ray) + length(ray) * direction(ray) + pos = hit_point(ray) in_dir = direction(ray) out_dir = reflection3d(in_dir, normal) E0 = _calculate_global_E0(bs, ray, out_dir, J) diff --git a/src/OpticalComponents/Detectors/Detector.jl b/src/OpticalComponents/Detectors/Detector.jl index 784335dcc..160ff4728 100644 --- a/src/OpticalComponents/Detectors/Detector.jl +++ b/src/OpticalComponents/Detectors/Detector.jl @@ -172,20 +172,6 @@ PolarizedRayHit(ray::PolarizedRay{T}, opl::Real) where {T} = polarization(hit::PolarizedRayHit) = hit.E -"Returns the point where the `ray` ends at its [`Intersection`](@ref) with a surface." -hit_point(ray::AbstractRay) = position(ray) + length(ray) * direction(ray) - -""" - projection_factor(ray::AbstractRay) - -Returns `|d · n|` of the direction `d` of the `ray` and the normal `n` of the surface it ends on; -`1` if the ray ends without an intersection. -""" -function projection_factor(ray::AbstractRay{T}) where {T} - isnothing(intersection(ray)) && return one(T) - return abs(dot(direction(ray), normal3d(intersection(ray)))) -end - """ AbstractBeamletHit diff --git a/src/OpticalComponents/Lenses.jl b/src/OpticalComponents/Lenses.jl index 802d1dd9c..dd75902d5 100644 --- a/src/OpticalComponents/Lenses.jl +++ b/src/OpticalComponents/Lenses.jl @@ -66,7 +66,7 @@ function interact3d(system::AbstractSystem, end # Calculate new dir. and pos. ndir, TIR = refraction3d(direction(ray), normal, n1, n2) - npos = position(ray) + length(ray) * direction(ray) + npos = hit_point(ray) # In case of TIR, update hint and n2 if TIR hint = Hint(optic) @@ -87,7 +87,7 @@ function interact3d(system::AbstractSystem, optic::AbstractRefractiveOptic, ::Beam{T, R}, ray::R) where {T <: Real, R <: PolarizedRay{T}} lambda = wavelength(ray) normal = normal3d(intersection(ray)) - raypos = position(ray) + length(ray) * direction(ray) + raypos = hit_point(ray) if isentering(ray) # Entering optic n1 = refractive_index(ray) diff --git a/src/OpticalComponents/Mirrors/Mirrors.jl b/src/OpticalComponents/Mirrors/Mirrors.jl index 6f56afa65..c9cb01c4e 100644 --- a/src/OpticalComponents/Mirrors/Mirrors.jl +++ b/src/OpticalComponents/Mirrors/Mirrors.jl @@ -39,7 +39,7 @@ function interact3d(::AbstractSystem, ::Beam{T, R}, ray::R) where {T <: Real, R <: Ray{T}} normal = normal3d(intersection(ray)) - npos = position(ray) + length(ray) * direction(ray) + npos = hit_point(ray) ndir = reflection3d(direction(ray), normal) return BeamInteraction{T, R}(nothing, Ray{T}(npos, ndir, nothing, wavelength(ray), refractive_index(ray))) @@ -56,7 +56,7 @@ function interact3d(::AbstractSystem, ::Beam{T, R}, ray::R) where {T <: Real, R <: PolarizedRay{T}} normal = normal3d(intersection(ray)) - npos = position(ray) + length(ray) * direction(ray) + npos = hit_point(ray) ndir = reflection3d(direction(ray), normal) # Jones reflection matrix J = SPBasis(-1, 0, 0, 1) diff --git a/src/OpticalComponents/Polarizers/LinearPolarizer.jl b/src/OpticalComponents/Polarizers/LinearPolarizer.jl index e65787a71..e2d55bc64 100644 --- a/src/OpticalComponents/Polarizers/LinearPolarizer.jl +++ b/src/OpticalComponents/Polarizers/LinearPolarizer.jl @@ -96,7 +96,7 @@ function _is_film_interface(lp::LinearPolarizer, from, ray::AbstractRay) nrm = normal3d(intersection(ray)) abs(dot(nrm, y)) ≥ 0.5 || return false c = position(lp) - p = position(ray) + length(ray) * direction(ray) + p = hit_point(ray) if from === lp.front return dot(p - c, y) > -thickness(lp.front) / 2 else @@ -117,7 +117,7 @@ function _film_interface(system::AbstractSystem, lp::LinearPolarizer, from, to, n2 = refractive_index(to, λ) normal = -normal3d(intersection(ray)) ndir, TIR = refraction3d(direction(ray), normal, n1, n2) - npos = position(ray) + length(ray) * direction(ray) + npos = hit_point(ray) if TIR hint = Hint(lp, shape(from)) n = n1 @@ -141,7 +141,7 @@ function _film_interface(system::AbstractSystem, lp::LinearPolarizer, from, to, n1 = refractive_index(from, λ) n2 = refractive_index(to, λ) normal = -normal3d(intersection(ray)) - npos = position(ray) + length(ray) * direction(ray) + npos = hit_point(ray) in_dir = direction(ray) θi = angle3d(in_dir, -normal) rs, rp, ts, tp = fresnel_coefficients(θi, n2 / n1) diff --git a/src/OpticalComponents/Polarizers/PolarizationFilter.jl b/src/OpticalComponents/Polarizers/PolarizationFilter.jl index 2512a8e04..e10ce4196 100644 --- a/src/OpticalComponents/Polarizers/PolarizationFilter.jl +++ b/src/OpticalComponents/Polarizers/PolarizationFilter.jl @@ -62,7 +62,7 @@ function interact3d(::AbstractSystem, polfilter::PolarizationFilter, ::Beam{T, R}, ray::R) where {T <: Real, R <: PolarizedRay{T}} - npos = position(ray) + length(ray) * direction(ray) + npos = hit_point(ray) ndir = direction(ray) E0 = _calculate_global_E0(polfilter, ray, ndir, polfilter.JMat) @@ -83,7 +83,7 @@ function interact3d( ray::R ) where {T <: Real, R <: Ray{T}} # unpolarized rays (e.g. beamlet auxiliary rays) pass through the ideal filter unchanged - npos = position(ray) + length(ray) * direction(ray) + npos = hit_point(ray) ndir = direction(ray) return BeamInteraction{T, R}(nothing, Ray{T}(npos, ndir, nothing, wavelength(ray), refractive_index(ray)))