From 79e1cc0c4f8466bdc7ac3058b907aebcfc3c870f Mon Sep 17 00:00:00 2001 From: uitt_hu Date: Sat, 7 Nov 2026 23:53:57 +0100 Subject: [PATCH 1/2] Internal: AbstractTrees.children(system) replaces _top_level (#234) A System returns its objects and groups as built, other systems objects(system). A System and an ObjectGroup print as one tree into the given IO. Co-Authored-By: Claude Opus 5.5 --- docs/src/api/core.md | 4 ++-- docs/src/basics/systems.md | 6 ++++++ ext/RenderLive.jl | 2 +- skills/beamletoptics/API.md | 5 +++-- src/AbstractTypes/AbstractBoundingSphere.jl | 2 +- src/AbstractTypes/AbstractSystem.jl | 9 +++++--- src/ObjectGroups.jl | 2 +- src/System.jl | 24 +++++++++++---------- test/TestObjectGroups.jl | 2 ++ test/TestSystem.jl | 19 ++++++++++++++++ 10 files changed, 54 insertions(+), 21 deletions(-) diff --git a/docs/src/api/core.md b/docs/src/api/core.md index 99a554863..f63d40158 100644 --- a/docs/src/api/core.md +++ b/docs/src/api/core.md @@ -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(["trace_all
best hit so far: none"]) EO(["trace_one
best hit so far: hinted object"]) - N{"another top-level
object or group?"} + N{"another object or group
of children(system)?"} H{"is it the
hinted object?"} BH{"best hit
so far?"} BY{"isbeyond
does its sphere start
behind the best hit?
"} diff --git a/docs/src/basics/systems.md b/docs/src/basics/systems.md index 968ea3205..de45ac828 100644 --- a/docs/src/basics/systems.md +++ b/docs/src/basics/systems.md @@ -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 diff --git a/ext/RenderLive.jl b/ext/RenderLive.jl index 242cbb1f4..aedabe357 100644 --- a/ext/RenderLive.jl +++ b/ext/RenderLive.jl @@ -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 diff --git a/skills/beamletoptics/API.md b/skills/beamletoptics/API.md index f3a0c11bf..332729495 100644 --- a/skills/beamletoptics/API.md +++ b/skills/beamletoptics/API.md @@ -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 diff --git a/src/AbstractTypes/AbstractBoundingSphere.jl b/src/AbstractTypes/AbstractBoundingSphere.jl index e90a84b77..0354c45e8 100644 --- a/src/AbstractTypes/AbstractBoundingSphere.jl +++ b/src/AbstractTypes/AbstractBoundingSphere.jl @@ -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 diff --git a/src/AbstractTypes/AbstractSystem.jl b/src/AbstractTypes/AbstractSystem.jl index a01fdbb12..6e6c493b9 100644 --- a/src/AbstractTypes/AbstractSystem.jl +++ b/src/AbstractTypes/AbstractSystem.jl @@ -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 diff --git a/src/ObjectGroups.jl b/src/ObjectGroups.jl index 6e032acf3..9f213be4a 100644 --- a/src/ObjectGroups.jl +++ b/src/ObjectGroups.jl @@ -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) diff --git a/src/System.jl b/src/System.jl index 6296ceb7e..d6087a454 100644 --- a/src/System.jl +++ b/src/System.jl @@ -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) @@ -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) @@ -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 @@ -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 @@ -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) diff --git a/test/TestObjectGroups.jl b/test/TestObjectGroups.jl index 507867ffa..ab4cecb72 100644 --- a/test/TestObjectGroups.jl +++ b/test/TestObjectGroups.jl @@ -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 diff --git a/test/TestSystem.jl b/test/TestSystem.jl index 5aff6bcec..ddef05951 100644 --- a/test/TestSystem.jl +++ b/test/TestSystem.jl @@ -2,6 +2,7 @@ module TestSystem using BeamletOptics using Test +using AbstractTrees: children, Leaves, print_tree # using LinearAlgebra # using GeometryBasics @@ -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 From 3a907a387e5375fd9fae9ced4b3858f93a6c330d Mon Sep 17 00:00:00 2001 From: uitt_hu Date: Sat, 7 Nov 2026 23:59:52 +0100 Subject: [PATCH 2/2] Bugfix: render! of a system reads children(system), not the objects field (review r1 F1) Co-Authored-By: Claude Opus 5.5 --- ext/RenderObjects.jl | 4 ++-- test/Rendering/TestRenderLive.jl | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/ext/RenderObjects.jl b/ext/RenderObjects.jl index 5036cd0d8..9479c425d 100644 --- a/ext/RenderObjects.jl +++ b/ext/RenderObjects.jl @@ -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 diff --git a/test/Rendering/TestRenderLive.jl b/test/Rendering/TestRenderLive.jl index eb199491d..a49084ffa 100644 --- a/test/Rendering/TestRenderLive.jl +++ b/test/Rendering/TestRenderLive.jl @@ -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 @@ -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)