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
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ with every release, so it must describe exactly that release.
- [test/TestAgentSkill.jl](test/TestAgentSkill.jl) catches mechanical drift only. It fails if
the export table in `API.md` differs from `names(BeamletOptics)`, if
`metadata.beamletoptics-version` in `SKILL.md` differs from the major.minor version in
`Project.toml`, if `install_agent_skill` breaks, or if a headless template (01–06) throws.
`Project.toml`, if `install_agent_skill` breaks, or if a headless template (all but `07_render_system.jl`) throws.
Passing tests do not show that the skill is correct: a changed meaning, e.g. a flipped sign
convention, still runs. Checking that is the author's job (above).
- On a minor version bump, review the whole skill and update both the version field and the
Expand Down
2 changes: 1 addition & 1 deletion docs/src/api/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ Depth = 2

## Public API for dependent packages

Besides the exported names, a few names are declared `public` in `src/Exports.jl` without being exported, e.g. `BeamletOptics.is_static`, `BeamletOptics.hit_count`, the progress interface `BeamletOptics.ProgressSink`, the sampling of the sources `BeamletOptics.AbstractSampling`, the optional `BeamletOptics.bounding_sphere_of` of a shape (see [Bounding sphere pretest](@ref)), or `BeamletOptics.relaunch!` and the beamlet hit records (`BeamletOptics.AstigmaticGaussianBeamletHit(agb, id)`, `BeamletOptics.beamlet_hit_field`) for components that re-emit light, e.g. solvers coupled through a field (see [Re-emitting components](@ref)). Packages built on BMO, such as [BeamletOpticsGUI](https://github.com/StackEnjoyer/BeamletOpticsGUI.jl), may rely on them like on the exported API; other unexported names are internal and may change in any release. Whether a name is public can be checked with `Base.ispublic(BeamletOptics, name)`. Their docstrings are listed in the [Reference](@ref), the render handle protocol is described on the [Live rendering](@ref "Render handle protocol") page. A new `AbstractObject` subtype can add a method of [`properties`](@ref) to list its own parameters, see [Inspecting components](@ref).
Besides the exported names, a few names are declared `public` in `src/Exports.jl` without being exported, e.g. `BeamletOptics.is_static`, `BeamletOptics.hit_count`, the progress interface `BeamletOptics.ProgressSink`, the sampling of the sources `BeamletOptics.AbstractSampling`, the optional `BeamletOptics.bounding_sphere_of` of a shape (see [Bounding sphere pretest](@ref)), `BeamletOptics.AbstractObjectGroup` for own assembly types of separate parts (see [Assemblies: group or multi-shape object](@ref)), or `BeamletOptics.relaunch!` and the beamlet hit records (`BeamletOptics.AstigmaticGaussianBeamletHit(agb, id)`, `BeamletOptics.beamlet_hit_field`) for components that re-emit light, e.g. solvers coupled through a field (see [Re-emitting components](@ref)). Packages built on BMO, such as [BeamletOpticsGUI](https://github.com/StackEnjoyer/BeamletOpticsGUI.jl), may rely on them like on the exported API; other unexported names are internal and may change in any release. Whether a name is public can be checked with `Base.ispublic(BeamletOptics, name)`. Their docstrings are listed in the [Reference](@ref), the render handle protocol is described on the [Live rendering](@ref "Render handle protocol") page. A new `AbstractObject` subtype can add a method of [`properties`](@ref) to list its own parameters, see [Inspecting components](@ref).
2 changes: 2 additions & 0 deletions docs/src/api/geometry.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ BeamletOptics.AbstractObject

An `AbstractObject` can consist of multiple `AbstractShape`s or even multiple subsidiary `AbstractObject`s, facilitating the creation of composite optical elements. For example, a lens with an anti-reflective coating could be represented as the substrate and a separate model for the coating, each with its own geometric and optical properties. In general, an `object` can have the `SingleShape` or a `MultiShape` trait. The [`BeamletOptics.AbstractShapeTrait`](@ref) allows the solver and kinematic API to apply different implementations of basis functions via multiple dispatch.

The solver treats a `MultiShape` object as one optical element: whichever of its parts a ray hits, [`BeamletOptics.interact3d`](@ref) is called for the object, which must find that part, e.g. via `shape(intersection(ray))`, and forward the beam to it. This is meant for parts that interact as one, e.g. the prisms and the coating of a [`CubeBeamsplitter`](@ref) or the lenses of a [`DoubletLens`](@ref), which hand the ray on to each other with a [`BeamletOptics.Hint`](@ref). Parts that are separate and interact on their own belong in an [`ObjectGroup`](@ref) or an own subtype of [`BeamletOptics.AbstractObjectGroup`](@ref) instead, see [Assemblies: group or multi-shape object](@ref).

```@docs; canonical=false
BeamletOptics.SingleShape
BeamletOptics.MultiShape
Expand Down
1 change: 1 addition & 0 deletions docs/src/api/kinematics_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ The primitives `translate3d!(::Movable, x, offset)` and `rotate3d!(::Movable, x,
- [`BeamletOptics.AbstractObject`](@ref): the primitives forward to the [`BeamletOptics.AbstractShapeTrait`](@ref) of the object, see [`BeamletOptics.SingleShape`](@ref) and [`BeamletOptics.MultiShape`](@ref)
- [`BeamletOptics.AbstractRay`](@ref): only subtypes carrying direction-dependent data (e.g. the field vector of a [`PolarizedRay`](@ref)) need their own `rotate3d!`
- [`BeamletOptics.AbstractBeam`](@ref): either implement `_component_beams` or the two primitives; every move resets the beam via `empty!`
- [`BeamletOptics.AbstractObjectGroup`](@ref): moves all members of `objects(group)` rigidly about the group `center`
- [`BeamletOptics.AbstractBeamGroup`](@ref): moves its `beams` rigidly about the group `center`
13 changes: 12 additions & 1 deletion docs/src/basics/kinematics/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,16 @@ For the easier representation of a group of [`BeamletOptics.AbstractObject`](@re

```@docs; canonical=false
ObjectGroup
set_pivot3d!(::ObjectGroup, ::Any)
set_pivot3d!(::BeamletOptics.AbstractObjectGroup, ::Any)
```

## Assemblies: group or multi-shape object

An assembly such as a telescope, a periscope or an interferometer arm can be modeled in two ways, and the choice follows from whether its parts touch:

| Parts of the assembly | Model | `interact3d` |
|---|---|---|
| separate, with air between them | an [`ObjectGroup`](@ref) or an own subtype of [`BeamletOptics.AbstractObjectGroup`](@ref) | none: each part is traced and interacts on its own |
| touching and interacting as one, e.g. cemented or with a shared face | an [`BeamletOptics.AbstractObject`](@ref) with the [`BeamletOptics.MultiShape`](@ref) trait, like the [`CubeBeamsplitter`](@ref) | required: the solver hands every hit to the object, which forwards it to the part that was hit |

The solver can find out by itself which of several separate parts a ray hits next. Parts that touch need a [`BeamletOptics.Hint`](@ref) across their shared face, which only the component knows. An own subtype of `AbstractObjectGroup` adds named parts and an own constructor to a group and needs no `interact3d` method; its fields and its constructor check are listed in the docstring of [`BeamletOptics.AbstractObjectGroup`](@ref), and the agent skill has a complete example in `templates/08_custom_group.jl`. A `MultiShape` object without an `interact3d` method stops every ray that hits it, with the warning `No interact3d method defined for the MultiShape object`.
4 changes: 2 additions & 2 deletions docs/src/basics/systems.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ popat!(system, 1) # removes the first top-level object and returns i
A [`StaticSystem`](@ref) can not be changed.

```@docs; canonical=false
Base.push!(::System, ::Vararg{BeamletOptics.AbstractObject})
Base.push!(::System, ::Vararg{BeamletOptics.ObjectOrGroup})
Base.pop!(::System)
Base.popat!(::System, ::Integer)
Base.delete!(::System, ::BeamletOptics.AbstractObject)
Base.delete!(::System, ::BeamletOptics.ObjectOrGroup)
```

## Solving systems
Expand Down
3 changes: 2 additions & 1 deletion docs/src/basics/visualization/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ Optical components are rendered by drawing their shape. Example renderings of th

## Components

The generic method below covers every [`BeamletOptics.AbstractObject`](@ref), including groups of components such as an [`ObjectGroup`](@ref), whose members are rendered one after another.
The generic methods below cover every [`BeamletOptics.AbstractObject`](@ref) and every group of components such as an [`ObjectGroup`](@ref), whose members are rendered one after another.

```@docs
render!(::Union{GLMakie.Axis3, GLMakie.LScene}, ::BeamletOptics.AbstractObject)
render!(::Union{GLMakie.Axis3, GLMakie.LScene}, ::BeamletOptics.AbstractObjectGroup)
```

### Materials
Expand Down
4 changes: 2 additions & 2 deletions docs/src/basics/visualization/live_rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ An object that is added to a system at runtime, see [Changing a system](@ref), i
handle of the system, and removed from it again, by:

```@docs; canonical=false
BeamletOptics.live_render!(::BeamletOptics.AbstractSystemRenderHandle, ::BeamletOptics.AbstractObject)
BeamletOptics.remove_render!(::BeamletOptics.AbstractSystemRenderHandle, ::BeamletOptics.AbstractObject)
BeamletOptics.live_render!(::BeamletOptics.AbstractSystemRenderHandle, ::BeamletOptics.ObjectOrGroup)
BeamletOptics.remove_render!(::BeamletOptics.AbstractSystemRenderHandle, ::BeamletOptics.ObjectOrGroup)
```

```julia
Expand Down
13 changes: 10 additions & 3 deletions ext/RenderBoundingSphere.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,18 @@ render_bounding_sphere!(ax::_RenderEnv, ::BMO.SingleShape, obj; kwargs...) =

# The solver tests the parts one by one, hence one sphere per part, and the main sphere of the object
# first, which is drawn in `main_color`
function render_bounding_sphere!(ax::_RenderEnv, ::BMO.MultiShape, obj; main_color = :orange, kwargs...)
for part in BMO.shape(obj)
render_bounding_sphere!(ax::_RenderEnv, ::BMO.MultiShape, obj; kwargs...) =
_render_part_spheres!(ax, obj, BMO.shape(obj); kwargs...)

# An object group: the spheres of its objects, then the main sphere of the group
render_bounding_sphere!(ax::_RenderEnv, group::BMO.AbstractObjectGroup; kwargs...) =
_render_part_spheres!(ax, group, BMO.objects(group); kwargs...)

function _render_part_spheres!(ax::_RenderEnv, whole, parts; main_color = :orange, kwargs...)
for part in parts
render_bounding_sphere!(ax, part; main_color, kwargs...)
end
render_bounding_sphere!(ax, BMO.bounding_sphere_of(obj); kwargs..., color = main_color)
render_bounding_sphere!(ax, BMO.bounding_sphere_of(whole); kwargs..., color = main_color)
return nothing
end

Expand Down
2 changes: 1 addition & 1 deletion ext/RenderCamera.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function render_lcs!(
return nothing
end

function render_lcs!(ax::LScene, object::BMO.AbstractObject; scale::Real = 10, show_labels::Bool = false)
function render_lcs!(ax::LScene, object::BMO.ObjectOrGroup; scale::Real = 10, show_labels::Bool = false)
render_lcs!(ax, BMO.position(object), BMO.orientation(object); scale, show_labels)
return nothing
end
64 changes: 43 additions & 21 deletions ext/RenderLive.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Live rendering of objects and systems, see the render handle protocol in `src/Re
"""
ObjectRenderHandle <: AbstractObjectRenderHandle

Live rendering handle of an `AbstractObject`, see [`live_render!`](@ref), or of anything with a pose
that is drawn by a function, see `live_render!(draw, ax, x)`.
Live rendering handle of an `AbstractObject` or an object group, see [`live_render!`](@ref), or of
anything with a pose that is drawn by a function, see `live_render!(draw, ax, x)`.

The plots are generated once by the `draw` function in the reference pose `P0`, `R0` of the object.
Afterwards, the rigid transformation from the reference pose to the current pose is applied as the
Expand All @@ -23,7 +23,7 @@ mutable struct ObjectRenderHandle{O} <: AbstractObjectRenderHandle
# reference pose
P0::Point3{Float64}
R0::Matrix{Float64}
# reference poses of the MultiShape subparts
# reference poses of the subparts, see `_rigid_parts`
subP0::Vector{Point3{Float64}}
subR0::Vector{Matrix{Float64}}
# last rendered pose
Expand All @@ -50,11 +50,21 @@ function _direction_frame(d)
return hcat(x, y, cross(x, y))
end

function _subposes(obj)
(obj isa BMO.AbstractObject && BMO.shape_trait_of(obj) isa BMO.MultiShape) ||
return Point3{Float64}[], Matrix{Float64}[]
parts = BMO.shape(obj)
return [_pose(p)[1] for p in parts], [_pose(p)[2] for p in parts]
"""
_rigid_parts(x)

Returns the subparts of `x` that can be moved on their own, i.e. the parts of a `MultiShape` object
or the objects of an object group. Anything else has none.
"""
_rigid_parts(::Any) = ()
_rigid_parts(obj::BMO.AbstractObject) = _rigid_parts(BMO.shape_trait_of(obj), obj)
_rigid_parts(::BMO.AbstractShapeTrait, obj) = ()
_rigid_parts(::BMO.MultiShape, obj) = BMO.shape(obj)
_rigid_parts(group::BMO.AbstractObjectGroup) = BMO.objects(group)

function _subposes(x)
parts = _rigid_parts(x)
return Point3{Float64}[_pose(p)[1] for p in parts], Matrix{Float64}[_pose(p)[2] for p in parts]
end

"""Returns the plots that are added to the `ax` by `f()`."""
Expand All @@ -74,6 +84,17 @@ function live_render!(ax::_RenderEnv, obj::BMO.AbstractObject; kwargs...)
return live_render!(() -> render!(ax, obj; kwargs...), ax, obj)
end

"""
live_render!(ax, group::AbstractObjectGroup; kwargs...)

Renders the `group` via [`render!`](@ref) and returns one `ObjectRenderHandle` for the whole group.
If an object of the group is moved on its own, the group is rendered again by
[`update_render!`](@ref).
"""
function live_render!(ax::_RenderEnv, group::BMO.AbstractObjectGroup; kwargs...)
return live_render!(() -> render!(ax, group; kwargs...), ax, group)
end

"""
live_render!(draw, ax, x)

Expand Down Expand Up @@ -125,11 +146,12 @@ end
"""
_is_rigid(h::ObjectRenderHandle; atol = 1e-6, rtol = 1e-6)

Checks if all subparts of a `MultiShape` object still match a rigid motion of the whole object.
Checks if all subparts of a `MultiShape` object or of an object group still match a rigid motion
of the whole.
"""
function _is_rigid(h::ObjectRenderHandle; atol = 1e-6, rtol = 1e-6)
isempty(h.subP0) && return true
parts = BMO.shape(h.obj)
parts = _rigid_parts(h.obj)
length(parts) != length(h.subP0) && return false
P, R = _pose(h.obj)
Rd = R * h.R0'
Expand All @@ -155,8 +177,8 @@ end
"""
update_render!(h::ObjectRenderHandle)

Applies the current pose of the object to its plots. If the subparts of a `MultiShape` object have
not been moved rigidly, the object is rendered again.
Applies the current pose of the object to its plots. If the subparts of a `MultiShape` object or of
an object group have not been moved rigidly, the object is rendered again.
"""
function update_render!(h::ObjectRenderHandle)
# The pose of a MultiShape object is the pose of its first part, hence check the other parts first
Expand Down Expand Up @@ -210,7 +232,7 @@ mutable struct SystemRenderHandle{S <: BMO.AbstractSystem} <: AbstractSystemRend
ax::_RenderEnv
sys::S
handles::Vector{AbstractObjectRenderHandle}
parent::IdDict{BMO.AbstractObject, BMO.AbstractObject}
parent::IdDict{BMO.ObjectOrGroup, BMO.AbstractObjectGroup}
end

function Base.show(io::IO, h::SystemRenderHandle)
Expand Down Expand Up @@ -241,7 +263,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[]
parent = IdDict{BMO.AbstractObject, BMO.AbstractObject}()
parent = IdDict{BMO.ObjectOrGroup, BMO.AbstractObjectGroup}()
# Avoid use of objects(sys), which flattens the groups
foreach(obj -> _live_render_leaves!(handles, parent, ax, obj; kwargs...), sys.objects)
return SystemRenderHandle(ax, sys, handles, parent)
Expand All @@ -257,21 +279,21 @@ function _live_render_leaves!(handles, parent, ax::_RenderEnv, obj::BMO.Abstract
end

function _live_render_leaves!(handles, parent, ax::_RenderEnv, group::BMO.AbstractObjectGroup; kwargs...)
for child in BMO.shape(group)
for child in BMO.objects(group)
parent[child] = group
_live_render_leaves!(handles, parent, ax, child; kwargs...)
end
return nothing
end

"""
live_render!(h::SystemRenderHandle, obj::AbstractObject; kwargs...)
live_render!(h::SystemRenderHandle, obj; kwargs...)

Live-renders `obj` into the axis of `h` and adds its object handles to `h`, see
[`live_render!`](@ref). Returns the new object handles. If an object of `obj` can not be rendered,
Live-renders `obj`, an object or object group, into the axis of `h` and adds its object handles to
`h`, see [`live_render!`](@ref). Returns the new object handles. If an object of `obj` can not be rendered,
`h` and its axis are left as they were.
"""
function live_render!(h::SystemRenderHandle, obj::BMO.AbstractObject; kwargs...)
function live_render!(h::SystemRenderHandle, obj::BMO.ObjectOrGroup; kwargs...)
for leaf in BMO.Leaves(obj)
any(oh -> rendered(oh) === leaf, h.handles) && throw(ArgumentError(
"the $(nameof(typeof(leaf))) is already rendered by the system handle"))
Expand All @@ -291,8 +313,8 @@ function live_render!(h::SystemRenderHandle, obj::BMO.AbstractObject; kwargs...)
end

# Also forgets the hierarchy of the groups of `obj`
function remove_render!(h::SystemRenderHandle, obj::BMO.AbstractObject)
invoke(remove_render!, Tuple{AbstractSystemRenderHandle, BMO.AbstractObject}, h, obj)
function remove_render!(h::SystemRenderHandle, obj::BMO.ObjectOrGroup)
invoke(remove_render!, Tuple{AbstractSystemRenderHandle, BMO.ObjectOrGroup}, h, obj)
foreach(o -> delete!(h.parent, o), BMO.PreOrderDFS(obj))
return nothing
end
41 changes: 32 additions & 9 deletions ext/RenderObjects.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ for each `object`.
"""
render!(ax::_RenderEnv, object::BMO.AbstractObject; kwargs...) = _render!(ax, object; kwargs...)

"""
render!(ax, group::AbstractObjectGroup; edges = nothing, show_bounding_sphere = false, kwargs...)

Renders all objects of the `group`, merged like the parts of a `MultiShape` object, see
`_render_parts!`. Nested groups are merged into the meshes of the outermost group. The `kwargs`,
e.g. `material` or `color`, and an explicit `edges` apply to all objects. `show_bounding_sphere`
is consumed by the outermost group, which draws the spheres of all its objects, see
[`render_bounding_sphere!`](@ref).
"""
function render!(ax::_RenderEnv, group::BMO.AbstractObjectGroup; show_bounding_sphere::Bool = false, kwargs...)
_render_parts!(ax, BMO.objects(group); kwargs...)
show_bounding_sphere && render_bounding_sphere!(ax, group)
return nothing
end

# Dispatch helper fct. for RenderPolarizers.jl, do not remove.
# Consumes `show_bounding_sphere`: the trait methods and the parts of a `MultiShape` object never see
# the keyword, the outermost object draws the spheres of all its parts.
Expand Down Expand Up @@ -93,22 +108,30 @@ class and the look, see `_default_edges`, i.e. mechanics parts have no edges. Ne
parts are merged into the meshes of the outermost object. The `kwargs`, e.g. `material` or
`color`, and an explicit `edges` apply to all parts.
"""
function render!(ax::_RenderEnv, ::BMO.MultiShape, obj; edges::Union{Nothing, Bool} = nothing,
kwargs...)
render!(ax::_RenderEnv, ::BMO.MultiShape, obj; kwargs...) = _render_parts!(ax, BMO.shape(obj); kwargs...)

"""
_render_parts!(ax, parts; edges = nothing, kwargs...)

Renders the `parts` of a `MultiShape` object or the objects of an object group and merges their
analytic meshes, see `render!(ax, ::MultiShape, obj)`. Within an enclosing call the meshes are
left to the collector of the outermost one.
"""
function _render_parts!(ax::_RenderEnv, parts; edges::Union{Nothing, Bool} = nothing, kwargs...)
kw = isnothing(edges) ? kwargs : (; kwargs..., edges)
if !isnothing(_MESH_COLLECTOR[])
for _obj in BMO.shape(obj)
render!(ax, _obj; kw...)
for part in parts
render!(ax, part; kw...)
end
return nothing
end
parts = Any[]
with(_MESH_COLLECTOR => parts) do
for _obj in BMO.shape(obj)
render!(ax, _obj; kw...)
collected = Any[]
with(_MESH_COLLECTOR => collected) do
for part in parts
render!(ax, part; kw...)
end
end
_plot_collected!(ax, parts)
_plot_collected!(ax, collected)
return nothing
end

Expand Down
Loading
Loading