Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/src/api/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ Since an optical element can consist of multiple joint shapes, the return type m

The closest hit of a ray is found on two levels: the solver searches the system for the objects that the ray can reach, and each object it reaches computes its own hit.

The search, shown below, goes through the top-level objects and groups of the system one at a time and keeps the closest hit found so far. Once there is a hit, every part whose bounding sphere the ray enters only beyond that hit (plus `get_coincidence_tolerance()`, so that touching surfaces are still found) is skipped, see [Bounding sphere pretest](@ref). A group is entered only if the ray reaches its sphere; its objects are then searched in the same way. `trace_all` starts without a hit. `trace_one` starts with the hit of the hinted object, so it can skip more parts from the start, see [Hints](@ref). A [`StaticSystem`](@ref) has no groups while tracing, since it flattens them when it is built.
The search, shown below, goes through `children(system)`, the top-level objects and groups of the system, one at a time and keeps the closest hit found so far. Once there is a hit, every part whose bounding sphere the ray enters only beyond that hit (plus `get_coincidence_tolerance()`, so that touching surfaces are still found) is skipped, see [Bounding sphere pretest](@ref). A group is entered only if the ray reaches its sphere; its objects are then searched in the same way. `trace_all` starts without a hit. `trace_one` starts with the hit of the hinted object, so it can skip more parts from the start, see [Hints](@ref). A [`StaticSystem`](@ref) has no groups while tracing, since it flattens them when it is built.

```mermaid
flowchart TB
EA(["<b>trace_all</b><br/>best hit so far: none"])
EO(["<b>trace_one</b><br/>best hit so far: hinted object"])
N{"another top-level<br/>object or group?"}
N{"another object or group<br/>of children(system)?"}
H{"is it the<br/>hinted object?"}
BH{"best hit<br/>so far?"}
BY{"<b>isbeyond</b><br>does its sphere start<br/>behind the best hit?<br/><i></i>"}
Expand Down
6 changes: 6 additions & 0 deletions docs/src/basics/systems.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ System
StaticSystem
```

A system is a tree in the sense of AbstractTrees: `children(system)` returns its objects and object groups as they were added, and the tree tools such as `Leaves` and `print_tree` reach into the groups. A [`System`](@ref) prints as this tree.

```@docs; canonical=false
BeamletOptics.children(::System)
```

## Changing a system

The objects of a [`System`](@ref) can be changed after its construction, e.g. to build up a setup
Expand Down
2 changes: 1 addition & 1 deletion ext/RenderLive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ object, such that each object of a group can be moved on its own without renderi
function live_render!(ax::_RenderEnv, sys::BMO.AbstractSystem; kwargs...)
handles = AbstractObjectRenderHandle[]
# Avoid use of objects(sys), which flattens the groups
roots = collect(BMO.ObjectOrGroup, BMO._top_level(sys))
roots = collect(BMO.ObjectOrGroup, AbstractTrees.children(sys))
foreach(obj -> _live_render_leaves!(handles, ax, obj; kwargs...), roots)
return SystemRenderHandle(ax, sys, handles, roots)
end
Expand Down
4 changes: 2 additions & 2 deletions ext/RenderObjects.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ Render all objects contained in the `sys`tem. The `kwargs` are passed on to `ren
object, e.g. `show_bounding_sphere = true` to draw the bounding spheres of all shapes.
"""
function render!(ax::_RenderEnv, sys::BMO.AbstractSystem; kwargs...)
# Avoid use of objects(sys)
for _obj in sys.objects
# Groups unflattened, rendered as one
for _obj in AbstractTrees.children(sys)
render!(ax, _obj; kwargs...)
end
return nothing
Expand Down
5 changes: 3 additions & 2 deletions skills/beamletoptics/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ Useful non-exported helpers: `BeamletOptics.inch`, `lensmakers_eq(R1, R2, n)` (r
`divergence_angle(λ, w0, M2)`, `numerical_aperture(θ, n=1)` (also of a `PointSource` or a cone
sampling, public), `optical_path_length(beam)`,
`isparaxial(system, beam, θ=π/4)`, `fresnel_coefficients`, `beams(group)`,
`objects(system)` and `objects(group)` (public),
`list_subtypes(T)`, constants `Z_vacuum`, `c_vacuum`.
`objects(system)` and `objects(group)` (public, groups flattened in a system),
`AbstractTrees.children(system)` (the top-level objects and groups as added; a system is an
AbstractTrees tree, e.g. for `print_tree`), `list_subtypes(T)`, constants `Z_vacuum`, `c_vacuum`.

## Properties of objects

Expand Down
2 changes: 1 addition & 1 deletion src/AbstractTypes/AbstractBoundingSphere.jl
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ stored as `Float64`, with the radius enlarged by the rounding error of the shape
"""
function bounding_spheres(system::AbstractSystem)
table = BoundingSphereTable()
for object in _top_level(system)
for object in children(system)
_register!(table, object)
end
return table
Expand Down
9 changes: 6 additions & 3 deletions src/AbstractTypes/AbstractSystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ Subtypes of `AbstractSystem` must implement the following:
## Functions:

- `objects(system)`: returns the objects of the system with object groups flattened into their objects. There is no fallback
- `AbstractTrees.children(system)` (optional): returns the top-level objects and groups of the system, which the solver tests.
The default is `objects(system)`
- `refractive_index(system, λ::Real)` (optional): returns the refractive index of the surrounding medium at the wavelength `λ`.
The default is `1.0`; a system does not store a refractive index

The solver tests a ray against the top-level objects and groups of the system. For a custom system these are the result of `objects(system)`.
The built-in [`System`](@ref) tests the bounding sphere of a group first and then its objects one by one, a [`StaticSystem`](@ref)
flattens its groups when it is built.
The solver tests a ray against `children(system)`. The built-in [`System`](@ref) returns its groups unflattened: the solver tests
the bounding sphere of a group first and then its objects one by one. A [`StaticSystem`](@ref) flattens its groups when it is built.
"""
abstract type AbstractSystem end

AbstractTrees.children(system::AbstractSystem) = objects(system)

refractive_index(::AbstractSystem, λ::Real) = 1.0

# A system is not moved as a whole, its objects are
Expand Down
2 changes: 1 addition & 1 deletion src/ObjectGroups.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,4 @@ throws a `BoundsError`. The pose of the `group` and the pose of the object stay
"""
Base.popat!(group::ObjectGroup, i::Integer) = popat!(group.objects, i)

Base.show(::IO, ::MIME"text/plain", group::ObjectGroup) = print_tree(group)
Base.show(io::IO, ::MIME"text/plain", group::ObjectGroup) = print_tree(io, group)
24 changes: 13 additions & 11 deletions src/System.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,13 @@ function trace_system!(::AbstractSystem, beam::B; r_max = 0, kwargs...) where {B
return nothing
end

# The objects and object groups at the top level of the system. The tracing enters a group only if the
# ray reaches its bounding sphere, hence it does not use the flattened `objects(system)`.
_top_level(system::AbstractSystem) = objects(system)
_top_level(system::System) = system.objects
"""
AbstractTrees.children(system::System)

Returns the objects and object groups of the `system` as they were added, groups not flattened.
See also `objects(system)`, which flattens the groups.
"""
AbstractTrees.children(system::System) = system.objects

"""
trace_all(system::AbstractSystem, ray::AbstractRay)
Expand All @@ -143,7 +146,7 @@ faces, are its [`coincident_hits`](@ref). An object whose bounding sphere the `r
the closest hit found so far is not tested.
"""
@inline trace_all(system::AbstractSystem, ray::AbstractRay) =
closest_hits(_top_level(system), ray, nothing, nothing)
closest_hits(children(system), ray, nothing, nothing)

"""
closest_hits(parts, ray, result, skip)
Expand Down Expand Up @@ -196,7 +199,7 @@ function closest_hits(object::AbstractObject, ray::AbstractRay{R},
end

function initialize!(system::AbstractSystem)
foreach(initialize!, _top_level(system))
foreach(initialize!, children(system))
return nothing
end

Expand All @@ -210,7 +213,7 @@ lie beyond it, hence the `hint` speeds up the trace but does not change the clos
"""
@inline function trace_one(system::AbstractSystem, ray::AbstractRay{R}, hint::Hint) where {R}
seed::Nullable{Intersection{R}} = intersect3d(object(hint), ray)
result = closest_hits(_top_level(system), ray, seed, isnothing(seed) ? nothing : object(hint))
result = closest_hits(children(system), ray, seed, isnothing(seed) ? nothing : object(hint))
return first_of_shape(result, shape(hint))
end

Expand Down Expand Up @@ -560,7 +563,6 @@ function AbstractTrees.printnode(io::IO, node::ObjectOrGroup; kw...)
show(io, node)
end

Base.show(::IO, ::MIME"text/plain", system::System) =
for obj in system.objects
print_tree(obj)
end
AbstractTrees.printnode(io::IO, system::AbstractSystem; kw...) = print(io, nameof(typeof(system)))

Base.show(io::IO, ::MIME"text/plain", system::System) = print_tree(io, system)
17 changes: 17 additions & 0 deletions test/Rendering/TestRenderLive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ BMO.pickable_plots(::_Marker, plots) = filter(p -> !(p isa Makie.Lines), plots)
# An object without a shape, whose rendering throws
struct _Unrenderable <: BMO.AbstractObject{Float64} end

# A custom system with the required functions only, no `objects` field
struct _CustomSystem <: BMO.AbstractSystem
parts::Vector{BMO.AbstractObject}
end
BMO.objects(system::_CustomSystem) = system.parts

# An own system handle on the protocol, e.g. of a GUI that combines the handles of several systems
struct _Combined <: BMO.AbstractSystemRenderHandle
sys::System
Expand Down Expand Up @@ -696,6 +702,17 @@ Base.delete!(h::_Combined, oh::BMO.AbstractObjectRenderHandle) = (filter!(c -> c
@test length(ax2.scene.plots) > n + 2
end

@testset "Custom system without an objects field" begin
m = RoundPlanoMirror(0.025, 0.005)
sys = _CustomSystem([m])
n = length(ax.scene.plots)
render!(ax, sys)
@test length(ax.scene.plots) > n
h = live_render!(ax, sys)
@test length(AbstractTrees.children(h)) == 1
remove_render!(h)
end

@testset "show" begin
lens = SphericalLens(0.05, -0.05, 0.01, 0.02)
h = live_render!(ax, lens)
Expand Down
2 changes: 2 additions & 0 deletions test/TestObjectGroups.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ BMO.kinematic_trait_of(::StaticTestObject) = BMO.Static()
@test BMO.objects(objects) isa Vector{BMO.ObjectOrGroup}
@test BMO.objects(objects)[1] === center && BMO.objects(objects)[2] === circle
@test collect(AbstractTrees.children(objects)) == [center, circle]
# printed into the given IO
@test sprint(show, MIME"text/plain"(), objects) == sprint(print_tree, objects)
# only objects and groups are members, e.g. no shapes
@test_throws TypeError ObjectGroup([center, BMO.CylinderSDF(1e-3, 2e-3)])
end
Expand Down
19 changes: 19 additions & 0 deletions test/TestSystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module TestSystem

using BeamletOptics
using Test
using AbstractTrees: children, Leaves, print_tree
# using LinearAlgebra
# using GeometryBasics

Expand Down Expand Up @@ -59,6 +60,24 @@ end
system, beam; r_max = 10, check_invariant = true, threshold = 1e-6)
end

@testset "Tree of a system" begin
m1, m2, det = SquarePlanoMirror2D(0.01), SquarePlanoMirror2D(0.01), Detector(0.01)
group = ObjectGroup([m1, m2])
system = System([group, det])
# Top level as built, groups not flattened
@test all(children(system) .=== [group, det])
@test all(collect(Leaves(system)) .=== [m1, m2, det])
# Other systems default to their objects
static = StaticSystem([group, det])
@test children(static) === BMO.objects(static)
# One tree, printed into the given IO: root, group, two mirrors, detector
text = sprint(show, MIME"text/plain"(), system)
@test text == sprint(print_tree, system)
@test startswith(text, "System\n")
@test count(==('\n'), text) == 5
@test sprint(show, MIME"text/plain"(), System()) == "System\n"
end

# Setup circular multipass cell with flat mirrors
n_mirrors = 101
radius = 1
Expand Down
Loading