Skip to content

Commit

Permalink
Merge pull request #39 from ederag/Axis3
Browse files Browse the repository at this point in the history
Fix scene and buttons
  • Loading branch information
brianguenter authored Sep 5, 2024
2 parents 5b3fdab + 9b5eae9 commit c15efe4
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 222 deletions.
53 changes: 21 additions & 32 deletions src/Vis/Emitters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,31 @@ const MARKER_SIZE = 1
#-------------------------------------
# draw debug information - local axes and positions
#-------------------------------------
function maybe_draw_debug_info(scene::Makie.LScene, o::Origins.AbstractOriginDistribution; transform::Geometry.Transform = Transform(), debug::Bool=false, kwargs...) where {T<:Real}
function maybe_draw_debug_info(ax::Makie.AbstractAxis, o::Origins.AbstractOriginDistribution; transform::Geometry.Transform = Transform(), debug::Bool=false, kwargs...) where {T<:Real}

dir = forward(transform)
uv = SVector{3}(right(transform))
vv = SVector{3}(up(transform))
pos = origin(transform)

if (debug)
# this is a stupid hack to force makie to render in 3d - for some scenes, makie decide with no apparent reason to show in 2d instead of 3d
Makie.scatter!(scene, [pos[1], pos[1]+0.1], [pos[2], pos[2]+0.1], [pos[3], pos[3]+0.1], color=:red, markersize=0)

# draw the origin and normal of the surface
Makie.scatter!(scene, pos, color=:blue, markersize = MARKER_SIZE * visual_size(o))
Makie.scatter!(ax, pos, color=:blue, markersize = MARKER_SIZE * visual_size(o))

# normal
arrow_size = ARRROW_SIZE * visual_size(o)
arrow_start = pos
arrow_end = dir * ARRROW_LENGTH * visual_size(o)
Makie.arrows!(scene.scene, [Makie.Point3f(arrow_start)], [Makie.Point3f(arrow_end)], arrowsize=arrow_size, linewidth=arrow_size * 0.5, linecolor=:blue, arrowcolor=:blue)
Makie.arrows!(ax, [Makie.Point3f(arrow_start)], [Makie.Point3f(arrow_end)], arrowsize=arrow_size, linewidth=arrow_size * 0.5, linecolor=:blue, arrowcolor=:blue)
arrow_end = uv * 0.5 * ARRROW_LENGTH * visual_size(o)
Makie.arrows!(scene.scene, [Makie.Point3f(arrow_start)], [Makie.Point3f(arrow_end)], arrowsize= 0.5 * arrow_size, linewidth=arrow_size * 0.5, linecolor=:red, arrowcolor=:red)
Makie.arrows!(ax, [Makie.Point3f(arrow_start)], [Makie.Point3f(arrow_end)], arrowsize= 0.5 * arrow_size, linewidth=arrow_size * 0.5, linecolor=:red, arrowcolor=:red)
arrow_end = vv * 0.5 * ARRROW_LENGTH * visual_size(o)
Makie.arrows!(scene.scene, [Makie.Point3f(arrow_start)], [Makie.Point3f(arrow_end)], arrowsize= 0.5 * arrow_size, linewidth=arrow_size * 0.5, linecolor=:green, arrowcolor=:green)
Makie.arrows!(ax, [Makie.Point3f(arrow_start)], [Makie.Point3f(arrow_end)], arrowsize= 0.5 * arrow_size, linewidth=arrow_size * 0.5, linecolor=:green, arrowcolor=:green)

# draw all the samples origins
positions = map(x -> transform*x, collect(o))
positions = collect(Makie.Point3f, positions)
Makie.scatter!(scene, positions, color=:green, markersize = MARKER_SIZE * visual_size(o))

# positions = collect(Makie.Point3f, o)
# Makie.scatter!(scene, positions, color=:green, markersize = MARKER_SIZE * visual_size(o))
Makie.scatter!(ax, positions, color=:green, markersize = MARKER_SIZE * visual_size(o))
end

end
Expand All @@ -63,14 +57,14 @@ end
#-------------------------------------
# draw point origin
#-------------------------------------
function OpticSim.Vis.draw!(scene::Makie.LScene, o::Origins.Point; transform::Geometry.Transform = Transform(), kwargs...) where {T<:Real}
maybe_draw_debug_info(scene, o; transform=transform, kwargs...)
function OpticSim.Vis.draw!(ax::Makie.AbstractAxis, o::Origins.Point; transform::Geometry.Transform = Transform(), kwargs...) where {T<:Real}
maybe_draw_debug_info(ax, o; transform=transform, kwargs...)
end

#-------------------------------------
# draw RectGrid and RectUniform origins
#-------------------------------------
function OpticSim.Vis.draw!(scene::Makie.LScene, o::Union{Origins.RectGrid, Origins.RectUniform}; transform::Geometry.Transform = Transform(), kwargs...) where {T<:Real}
function OpticSim.Vis.draw!(ax::Makie.AbstractAxis, o::Union{Origins.RectGrid, Origins.RectUniform}; transform::Geometry.Transform = Transform(), kwargs...) where {T<:Real}
dir = forward(transform)
uv = SVector{3}(right(transform))
vv = SVector{3}(up(transform))
Expand All @@ -81,16 +75,16 @@ function OpticSim.Vis.draw!(scene::Makie.LScene, o::Union{Origins.RectGrid, Orig
plane = OpticSim.Plane(dir, pos)
rect = OpticSim.Rectangle(plane, o.width / 2, o.height / 2, uv, vv)

OpticSim.Vis.draw!(scene, rect; kwargs...)
OpticSim.Vis.draw!(ax, rect; kwargs...)

maybe_draw_debug_info(scene, o; transform=transform, kwargs...)
maybe_draw_debug_info(ax, o; transform=transform, kwargs...)
end


#-------------------------------------
# draw hexapolar origin
#-------------------------------------
function OpticSim.Vis.draw!(scene::Makie.LScene, o::Origins.Hexapolar; transform::Geometry.Transform = Transform(), kwargs...) where {T<:Real}
function OpticSim.Vis.draw!(ax::Makie.AbstractAxis, o::Origins.Hexapolar; transform::Geometry.Transform = Transform(), kwargs...) where {T<:Real}
dir = forward(transform)
uv = SVector{3}(right(transform))
vv = SVector{3}(up(transform))
Expand All @@ -99,17 +93,17 @@ function OpticSim.Vis.draw!(scene::Makie.LScene, o::Origins.Hexapolar; transform
plane = OpticSim.Plane(dir, pos)
ellipse = OpticSim.Ellipse(plane, o.halfsizeu, o.halfsizev, uv, vv)

OpticSim.Vis.draw!(scene, ellipse; kwargs...)
OpticSim.Vis.draw!(ax, ellipse; kwargs...)

maybe_draw_debug_info(scene, o; transform=transform, kwargs...)
maybe_draw_debug_info(ax, o; transform=transform, kwargs...)
end

#-------------------------------------
# draw source
#-------------------------------------
function OpticSim.Vis.draw!(scene::Makie.LScene, s::S; parent_transform::Geometry.Transform = Transform(), debug::Bool=false, kwargs...) where {T<:Real,S<:Sources.AbstractSource{T}}
function OpticSim.Vis.draw!(ax::Makie.AbstractAxis, s::S; parent_transform::Geometry.Transform = Transform(), debug::Bool=false, kwargs...) where {T<:Real,S<:Sources.AbstractSource{T}}

OpticSim.Vis.draw!(scene, Emitters.Sources.origins(s); transform=parent_transform * Emitters.Sources.transform(s), debug=debug, kwargs...)
OpticSim.Vis.draw!(ax, Emitters.Sources.origins(s); transform=parent_transform * Emitters.Sources.transform(s), debug=debug, kwargs...)

if (debug)
m = zeros(T, length(s), 7)
Expand All @@ -121,21 +115,16 @@ function OpticSim.Vis.draw!(scene::Makie.LScene, s::S; parent_transform::Geometr

m[:, 4:6] .*= m[:, 7] * ARRROW_LENGTH * visual_size(Emitters.Sources.origins(s))

# Makie.arrows!(scene, [Makie.Point3f(origin(ray))], [Makie.Point3f(rayscale * direction(ray))]; kwargs..., arrowsize = min(0.05, rayscale * 0.05), arrowcolor = color, linecolor = color, linewidth = 2)
color = :yellow
arrow_size = ARRROW_SIZE * visual_size(Emitters.Sources.origins(s))
Makie.arrows!(scene, m[:,1], m[:,2], m[:,3], m[:,4], m[:,5], m[:,6]; kwargs..., arrowcolor=color, linecolor=color, arrowsize=arrow_size, linewidth=arrow_size*0.5)
Makie.arrows!(ax, m[:,1], m[:,2], m[:,3], m[:,4], m[:,5], m[:,6]; kwargs..., arrowcolor=color, linecolor=color, arrowsize=arrow_size, linewidth=arrow_size*0.5)
end

# for ray in o
# OpticSim.Vis.draw!(scene, ray)
# end
end

#-------------------------------------
# draw optical rays
#-------------------------------------
function OpticSim.Vis.draw!(scene::Makie.LScene, rays::AbstractVector{OpticSim.OpticalRay{T, 3}};
function OpticSim.Vis.draw!(ax::Makie.AbstractAxis, rays::AbstractVector{OpticSim.OpticalRay{T, 3}};
debug::Bool = false, # make sure debug does not end up in kwargs (Makie would error)
kwargs...
) where {T<:Real}
Expand All @@ -147,14 +136,14 @@ function OpticSim.Vis.draw!(scene::Makie.LScene, rays::AbstractVector{OpticSim.O
end

color = :green
Makie.linesegments!(scene, m[:,1], m[:,2], m[:,3]; kwargs..., color = color, linewidth = 2, )
Makie.linesegments!(ax, m[:,1], m[:,2], m[:,3]; kwargs..., color = color, linewidth = 2, )
end

#-------------------------------------
# draw composite source
#-------------------------------------
function OpticSim.Vis.draw!(scene::Makie.LScene, s::Sources.CompositeSource{T}; parent_transform::Geometry.Transform = Transform(), kwargs...) where {T<:Real}
function OpticSim.Vis.draw!(ax::Makie.AbstractAxis, s::Sources.CompositeSource{T}; parent_transform::Geometry.Transform = Transform(), kwargs...) where {T<:Real}
for source in s.sources
OpticSim.Vis.draw!(scene, source; parent_transform=parent_transform*Emitters.Sources.transform(s), kwargs...)
OpticSim.Vis.draw!(ax, source; parent_transform=parent_transform*Emitters.Sources.transform(s), kwargs...)
end
end
Loading

0 comments on commit c15efe4

Please sign in to comment.