diff --git a/src/Vis/Emitters.jl b/src/Vis/Emitters.jl index 24fdae3..ab2cfa9 100644 --- a/src/Vis/Emitters.jl +++ b/src/Vis/Emitters.jl @@ -24,7 +24,7 @@ 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)) @@ -32,29 +32,23 @@ function maybe_draw_debug_info(scene::Makie.LScene, o::Origins.AbstractOriginDis 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 @@ -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)) @@ -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)) @@ -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) @@ -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} @@ -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 diff --git a/src/Vis/Visualization.jl b/src/Vis/Visualization.jl index 9b3c656..e2dc116 100644 --- a/src/Vis/Visualization.jl +++ b/src/Vis/Visualization.jl @@ -55,11 +55,11 @@ end ############################################################################# -# all functions follow the pattern draw(obj) and draw!(scene, obj) where the first case draws the object in a blank scene and displays it, and -# the second case draws the object in an existing scene, draw!(obj) can also be used to draw the object in the current scene +# all functions follow the pattern draw(obj) and draw!(ax, obj) where the first case draws the object in a blank Axis and displays it, and +# the second case draws the object in an existing Axis, draw!(obj) can also be used to draw the object in the current Axis +# Those should be renamed (e.g. `scene()` returns a Figure), or removed? global current_main_scene = nothing -global current_layout_scene = nothing global current_3d_scene = nothing global current_mode = nothing # modes: nothing, :default -> Original Vis behavior # :pluto -> support pluto notebooks @@ -89,40 +89,37 @@ Create a new Makie scene with the given resolution including control buttons. function scene(resolution = (1000, 1000)) @assert resolution[1] > 0 && resolution[2] > 0 - # possible fix for fixing layoutscene no longer existing in Makie. Many other changes appear to be necessary - # in addition to this. the call to Makie.LScene errors and when fixed the call to Makie.Button errors. Looks like - # a lot of work. - # fig = Makie.Figure(resolution=resolution) - # lscene = Makie.LScene(fig[1, 1]) + fig = Makie.Figure(size = resolution) + global current_main_scene = fig - scene = layout = Makie.Figure(size = resolution) - global current_main_scene = scene - global current_layout_scene = layout - lscene = layout[1, 1] = Makie.LScene(scene, scenekw = (camera = Makie.cam3d_cad!, axis_type = Makie.axis3d!, raw = false)) - global current_3d_scene = lscene + ax = Makie.LScene(fig[1, 1]; + scenekw = (; camera = Makie.cam3d_cad!) + ) + global current_3d_scene = ax # in these modes we want to skip the creation of the utility buttons as these modes are not interactive if (get_current_mode() == :pluto || get_current_mode() == :docs) - return scene, lscene + return fig, ax end - threedbutton = Makie.Button(scene, label = "3D", buttoncolor = RGB(0.8, 0.8, 0.8), height = 40, width = 80) - twodxbutton = Makie.Button(scene, label = "2D-x", buttoncolor = RGB(0.8, 0.8, 0.8), height = 40, width = 80) - twodybutton = Makie.Button(scene, label = "2D-y", buttoncolor = RGB(0.8, 0.8, 0.8), height = 40, width = 80) - savebutton = Makie.Button(scene, label = "Screenshot", buttoncolor = RGB(0.8, 0.8, 0.8), height = 40, width = 160) + buttons_layout = Makie.GridLayout(fig[2, 1]; tellwidth = false) + threedbutton = Makie.Button(buttons_layout[1, 1], label = "3D", buttoncolor = RGB(0.8, 0.8, 0.8), height = 40, width = 80) + twodxbutton = Makie.Button(buttons_layout[1, 2], label = "2D-x", buttoncolor = RGB(0.8, 0.8, 0.8), height = 40, width = 80) + twodybutton = Makie.Button(buttons_layout[1, 3], label = "2D-y", buttoncolor = RGB(0.8, 0.8, 0.8), height = 40, width = 80) + savebutton = Makie.Button(buttons_layout[1, 4], label = "Screenshot", buttoncolor = RGB(0.8, 0.8, 0.8), height = 40, width = 160) Makie.on(threedbutton.clicks) do nclicks - make3d(lscene) + update_camera_orientation!(ax, π / 4, π / 4) yield() end Makie.on(twodybutton.clicks) do nclicks - make2dy(lscene) + update_camera_orientation!(ax, π / 2, 0.0) yield() end Makie.on(twodxbutton.clicks) do nclicks - make2dx(lscene) + update_camera_orientation!(ax, 0.0, 0.0) yield() end @@ -131,98 +128,25 @@ function scene(resolution = (1000, 1000)) yield() end - layout[2, 1] = Makie.grid!(hcat(threedbutton, twodxbutton, twodybutton, savebutton), tellwidth = false, tellheight = true) - return scene, lscene -end - -function make3d(scene::Makie.LScene = current_3d_scene) - s = scene.scene - # use 3d camera - Makie.cam3d_cad!(s) - # reset scene rotation - s.transformation.rotation[] = Makie.Quaternion(0.0, 0.0, 0.0, 1.0) - # show all the axis ticks - s[Makie.OldAxis].attributes.showticks[] = (true, true, true) - # reset tick and axis label rotation and position - rot = (Makie.Quaternion(0.0, 0.0, -0.7071067811865476, -0.7071067811865475), Makie.Quaternion(0.0, 0.0, 1.0, 0.0), Makie.Quaternion(0.0, 0.7071067811865475, 0.7071067811865476, 0.0)) - s[Makie.OldAxis].attributes.ticks.rotation[] = rot - s[Makie.OldAxis].attributes.names.rotation[] = rot - s[Makie.OldAxis].attributes.ticks.align = ((:left, :center), (:right, :center), (:right, :center)) - s[Makie.OldAxis].attributes.names.align = ((:left, :center), (:right, :center), (:right, :center)) - # reset scene limits to automatic - s.limits[] = Makie.Automatic() - Makie.update!(s) -end - -function make2dy(scene::Makie.LScene = current_3d_scene) - s = scene.scene - # use 2d camera - Makie.cam2d!(s) - - scene_transform = Makie.qrotation(SVector{3, Float64}(0, 1, 0), 0.5pi) - scene_transform_inv = Makie.qrotation(SVector{3, Float64}(0, 1, 0), -0.5pi) # to use with the ticks and names - - # set rotation to look onto yz plane - s.transformation.rotation[] = scene_transform - # hide x ticks - - # there is a bug in Makie 0.14.2 which causes an exception setting the X showticks to false. - # we work around it by making sure the labels we want to turn off are orthogonal to the view direction - # s[Makie.OldAxis].attributes.showticks[] = (false, true, true) - s[Makie.OldAxis].attributes.showticks[] = (true, true, true) - - # set tick and axis label rotation and position - s[Makie.OldAxis].attributes.ticks.rotation[] = (0.0, scene_transform_inv, scene_transform_inv) - s[Makie.OldAxis].attributes.names.rotation[] = s[Makie.OldAxis].attributes.ticks.rotation[] - s[Makie.OldAxis].attributes.ticks.align = ((:right, :center), (:right, :center), (:center, :right)) - s[Makie.OldAxis].attributes.names.align = ((:left, :center), (:left, :center), (:center, :left)) - # update the scene limits automatically to get true reference values - s.limits[] = Makie.Automatic() - Makie.update_limits!(s) - # manually set the scene limits to draw the axes correctly - o, w = Makie.origin(s.data_limits[]), Makie.widths(s.data_limits[]) - s.limits[] = Makie.FRect3D((1000.0f0, o[2], o[3]), (w[2], w[2], w[3])) - # set the eye (i.e. light) position to behind the camera - s.camera.eyeposition[] = (0, 0, -100) - Makie.update!(s) -end - -function make2dx(scene::Makie.LScene = current_3d_scene) - s = scene.scene - # use 2d camera - Makie.cam2d!(s) - - scene_transform= Makie.qrotation(SVector{3, Float64}(0, 0, 1), 0.5pi) * Makie.qrotation(SVector{3, Float64}(1, 0, 0), 0.5pi) - scene_transform_inv=Makie.qrotation(SVector{3, Float64}(1, 0, 0), -0.5pi) * Makie.qrotation(SVector{3, Float64}(0, 0, 1), -0.5pi) - - # set rotation to look onto yz plane - s.transformation.rotation[] = scene_transform - # hide y ticks - - # there is a bug in Makie 0.14.2 which causes an exception setting the X showticks to false. - # we work around it by making sure the labels we want to turn off are orthogonal to the view direction - s[Makie.OldAxis].attributes.showticks[] = (true, false, true) - - # set tick and axis label rotation and position - s[Makie.OldAxis].attributes.ticks.rotation[] = (scene_transform_inv, 0.0, scene_transform_inv) - s[Makie.OldAxis].attributes.names.rotation[] = s[Makie.OldAxis].attributes.ticks.rotation[] - s[Makie.OldAxis].attributes.ticks.align = ((:right, :center), (:right, :center), (:center, :center)) - s[Makie.OldAxis].attributes.names.align = ((:left, :center), (:right, :center), (:center, :center)) - # update the scene limits automatically to get true reference values - s.limits[] = Makie.Automatic() - Makie.update_limits!(s) - # manually set the scene limits to draw the axes correctly - o, w = Makie.origin(s.data_limits[]), Makie.widths(s.data_limits[]) - s.limits[] = Makie.FRect3D((o[1], -1000.0f0, o[3]), (w[1], w[1], w[3])) - # set the eye (i.e. light) position to behind the camera - s.camera.eyeposition[] = (0, 0, -100) - Makie.update!(s) + return fig, ax end + +""" + update_camera_orientation(ax::Makie.AbstractAxis, ϕ, θ) + +Set the camera position based on two angles 0 ≤ ϕ ≤ 2π and -pi/2 ≤ θ ≤ pi/2. +""" +function update_camera_orientation!(ax::Makie.AbstractAxis, ϕ, θ) + cam3d = Makie.cameracontrols(ax) + Makie.update_cam!(ax.scene, cam3d, ϕ, θ) +end + + """ draw(ob; resolution = (1000, 1000), kwargs...) -Draw an object in a new scene. +Draw an object in a new Axis. `kwargs` depends on the object type. """ function draw(ob; resolution = (1000, 1000), kwargs...) @@ -263,12 +187,7 @@ end Save the current Makie scene to an image file. """ function save(path::String) - # save closes the window so just display it again as a work-around - # for some reason the size isn't maintained automatically so we just reset it manually - size = Makie.size(current_main_scene) - Makie.save(path, current_3d_scene.scene) - Makie.resize!(current_main_scene, size) - display(current_main_scene) + Makie.save(path, current_main_scene; update = false) end function save(::Nothing) end @@ -321,7 +240,7 @@ Base.:*(a::Transform, p::GeometryBasics.PointMeta) = a * p.main Base.:*(a::Real, p::GeometryBasics.PointMeta{N,S}) where {S<:Real,N} = GeometryBasics.Point{N,S}((a * SVector{N,S}(p))...) Base.:*(a::Transform, p::GeometryBasics.Point{N,S}) where {S<:Real,N} = GeometryBasics.Point{N,S}((a.rotation * SVector{N,S}(p) + a.translation)...) -function draw!(scene::Makie.LScene, ob::AbstractString; +function draw!(ax::Makie.AbstractAxis, ob::AbstractString; color = :gray, debug::Bool = false, # make sure debug does not end up in kwargs (Makie would error) linewidth = 3, @@ -354,32 +273,37 @@ end ## GEOMETRY """ - draw!(scene::Makie.LScene, surf::Surface{T}; numdivisions = 20, normals = false, normalcolor = :blue, kwargs...) + draw!(ax::Makie.AbstractAxis, surf::Surface{T}; numdivisions = 20, normals = false, normalcolor = :blue, kwargs...) Transforms `surf` into a mesh using [`makemesh`](@ref) and draws the result. `normals` of the surface can be drawn at evenly sampled points with provided `normalcolor`. `numdivisions` determines the resolution with which the mesh is triangulated. `kwargs` is passed on to the [`TriangleMesh`](@ref) drawing function. """ -function draw!(scene::Makie.LScene, surf::Surface{T}; numdivisions::Int = 30, normals::Bool = false, normalcolor = :blue, kwargs...) where {T<:Real} +function draw!(ax::Makie.AbstractAxis, surf::Surface{T}; + numdivisions::Int = 30, + normals::Bool = false, + normalcolor = :blue, + kwargs... +) where {T<:Real} mesh = makemesh(surf, numdivisions) if nothing === mesh return end - draw!(scene, mesh; kwargs..., normals = false) + draw!(ax, mesh; kwargs..., normals = false) if normals ndirs = Makie.Point3f.(samplesurface(surf, normal, numdivisions ÷ 10)) norigins = Makie.Point3f.(samplesurface(surf, point, numdivisions ÷ 10)) - Makie.arrows!(scene, norigins, ndirs, arrowsize = 0.2, arrowcolor = normalcolor, linecolor = normalcolor, linewidth = 2) + Makie.arrows!(ax, norigins, ndirs, arrowsize = 0.2, arrowcolor = normalcolor, linecolor = normalcolor, linewidth = 2) end end """ - draw!(scene::Makie.LScene, tmesh::TriangleMesh{T}; linewidth = 3, shaded = true, wireframe = false, color = :orange, normals = false, normalcolor = :blue, transparency = false, kwargs...) + draw!(ax::Makie.AbstractAxis, tmesh::TriangleMesh{T}; linewidth = 3, shaded = true, wireframe = false, color = :orange, normals = false, normalcolor = :blue, transparency = false, kwargs...) Draw a [`TriangleMesh`](@ref), optionially with a visible `wireframe`. `kwargs` are passed on to [`Makie.mesh`](http://makie.juliaplots.org/stable/plotting_functions.html#mesh). """ -function draw!(scene::Makie.LScene, tmesh::TriangleMesh{T}; +function draw!(ax::Makie.AbstractAxis, tmesh::TriangleMesh{T}; debug::Bool = false, # make sure debug does not end up in kwargs (Makie would error) linewidth = 3, shaded::Bool = true, @@ -393,13 +317,13 @@ function draw!(scene::Makie.LScene, tmesh::TriangleMesh{T}; points, indices = makiemesh(tmesh) if length(points) > 0 && length(indices) > 0 shading = default_shading(shaded) - Makie.mesh!(scene, points, indices; kwargs..., color = color, shading, transparency = transparency, visible = shaded) + Makie.mesh!(ax, points, indices; kwargs..., color = color, shading, transparency = transparency, visible = shaded) if wireframe - mesh = scene.scene[end][1] + mesh = ax.scene[end][1] if shaded - Makie.wireframe!(scene, mesh, color = (:black, 0.1), linewidth = linewidth) + Makie.wireframe!(ax, mesh, color = (:black, 0.1), linewidth = linewidth) else - Makie.wireframe!(scene, mesh, color = color, linewidth = linewidth) + Makie.wireframe!(ax, mesh, color = color, linewidth = linewidth) end end end @@ -408,25 +332,28 @@ function draw!(scene::Makie.LScene, tmesh::TriangleMesh{T}; norigins = [Makie.Point3f(centroid(t)) for t in tmesh.triangles[1:10:end]] ndirs = [Makie.Point3f(normal(t)) for t in tmesh.triangles[1:10:end]] if length(norigins) > 0 - Makie.arrows!(scene, norigins, ndirs, arrowsize = 0.2, arrowcolor = normalcolor, linecolor = normalcolor, linewidth = 2) + Makie.arrows!(ax, norigins, ndirs, arrowsize = 0.2, arrowcolor = normalcolor, linecolor = normalcolor, linewidth = 2) end end end """ - draw!(scene::Makie.LScene, meshes::Vararg{S}; colors::Bool = false, kwargs...) where {T<:Real,S<:Union{TriangleMesh{T},Surface{T}}} + draw!(ax::Makie.AbstractAxis, meshes::Vararg{S}; colors::Bool = false, kwargs...) where {T<:Real,S<:Union{TriangleMesh{T},Surface{T}}} Draw a series of [`TriangleMesh`](@ref) or [`Surface`](@ref) objects, if `colors` is true then each mesh will be colored automatically with a diverse series of colors. `kwargs` are is passed on to the drawing function for each element. """ -function draw!(scene::Makie.LScene, meshes::Vararg{S}; colors::Bool = false, kwargs...) where {T<:Real,S<:Union{TriangleMesh{T},Surface{T}}} +function draw!(ax::Makie.AbstractAxis, meshes::Vararg{S}; + colors::Bool = false, + kwargs... +) where {T<:Real, S<:Union{TriangleMesh{T}, Surface{T}}} for i in 1:length(meshes) if colors col = indexedcolor2(i) else col = :orange end - draw!(scene, meshes[i]; kwargs..., color = col) + draw!(ax, meshes[i]; kwargs..., color = col) end end @@ -440,19 +367,19 @@ function draw(meshes::Vararg{S}; kwargs...) where {T<:Real,S<:Union{TriangleMesh end """ - draw!(scene::Makie.LScene, csg::Union{CSGTree,CSGGenerator}; numdivisions::Int = 20, kwargs...) + draw!(ax::Makie.AbstractAxis, csg::Union{CSGTree,CSGGenerator}; numdivisions::Int = 20, kwargs...) Convert a CSG object ([`CSGTree`](@ref) or [`CSGGenerator`](@ref)) to a mesh using [`makemesh`](@ref) with resolution set by `numdivisions` and draw the resulting [`TriangleMesh`](@ref). """ -draw!(scene::Makie.LScene, csg::CSGTree{T}; numdivisions::Int = 30, kwargs...) where {T<:Real} = draw!(scene, makemesh(csg, numdivisions); kwargs...) -draw!(scene::Makie.LScene, csg::CSGGenerator{T}; kwargs...) where {T<:Real} = draw!(scene, csg(); kwargs...) +draw!(ax::Makie.AbstractAxis, csg::CSGTree{T}; numdivisions::Int = 30, kwargs...) where {T<:Real} = draw!(ax, makemesh(csg, numdivisions); kwargs...) +draw!(ax::Makie.AbstractAxis, csg::CSGGenerator{T}; kwargs...) where {T<:Real} = draw!(ax, csg(); kwargs...) """ - draw!(scene::Makie.LScene, bbox::BoundingBox{T}; kwargs...) + draw!(ax::Makie.AbstractAxis, bbox::BoundingBox{T}; kwargs...) Draw a [`BoundingBox`](@ref) as a wireframe, ie series of lines. """ -function draw!(scene::Makie.LScene, bbox::BoundingBox{T}; kwargs...) where {T<:Real} +function draw!(ax::Makie.AbstractAxis, bbox::BoundingBox{T}; kwargs...) where {T<:Real} p1 = SVector{3,T}(bbox.xmin, bbox.ymin, bbox.zmin) p2 = SVector{3,T}(bbox.xmin, bbox.ymax, bbox.zmin) p3 = SVector{3,T}(bbox.xmin, bbox.ymax, bbox.zmax) @@ -461,33 +388,33 @@ function draw!(scene::Makie.LScene, bbox::BoundingBox{T}; kwargs...) where {T<:R p6 = SVector{3,T}(bbox.xmax, bbox.ymax, bbox.zmin) p7 = SVector{3,T}(bbox.xmax, bbox.ymax, bbox.zmax) p8 = SVector{3,T}(bbox.xmax, bbox.ymin, bbox.zmax) - Makie.linesegments!(scene, [p1, p2, p2, p3, p3, p4, p4, p1, p1, p5, p2, p6, p3, p7, p4, p8, p5, p6, p6, p7, p7, p8, p8, p5]; kwargs...) + Makie.linesegments!(ax, [p1, p2, p2, p3, p3, p4, p4, p1, p1, p5, p2, p6, p3, p7, p4, p8, p5, p6, p6, p7, p7, p8, p8, p5]; kwargs...) end ## OPTICS """ - draw!(scene::Makie.LScene, ass::LensAssembly; kwargs...) + draw!(ax::Makie.AbstractAxis, ass::LensAssembly; kwargs...) Draw each element in a [`LensAssembly`](@ref), with each element automatically colored differently. """ -function draw!(scene::Makie.LScene, ass::LensAssembly{T}; kwargs...) where {T<:Real} +function draw!(ax::Makie.AbstractAxis, ass::LensAssembly{T}; kwargs...) where {T<:Real} for (i, e) in enumerate(elements(ass)) - draw!(scene, e; kwargs..., color = indexedcolor2(i)) + draw!(ax, e; kwargs..., color = indexedcolor2(i)) end end """ - draw!(scene::Makie.LScene, sys::AbstractOpticalSystem; kwargs...) + draw!(ax::Makie.AbstractAxis, sys::AbstractOpticalSystem; kwargs...) Draw each element in the lens assembly of an [`AbstractOpticalSystem`](@ref), with each element automatically colored differently, as well as the detector of the system. """ -function draw!(scene::Makie.LScene, sys::CSGOpticalSystem{T}; kwargs...) where {T<:Real} - draw!(scene, sys.assembly; kwargs...) - draw!(scene, sys.detector; kwargs...) +function draw!(ax::Makie.AbstractAxis, sys::CSGOpticalSystem{T}; kwargs...) where {T<:Real} + draw!(ax, sys.assembly; kwargs...) + draw!(ax, sys.detector; kwargs...) end -draw!(scene::Makie.LScene, sys::AxisymmetricOpticalSystem{T}; kwargs...) where {T<:Real} = draw!(scene, sys.system; kwargs...) +draw!(ax::Makie.AbstractAxis, sys::AxisymmetricOpticalSystem{T}; kwargs...) where {T<:Real} = draw!(ax, sys.system; kwargs...) """ @@ -512,7 +439,7 @@ If `trackallrays` is true then ray paths from the emitter will be displayed othe By default only ray paths that eventually intersect the detector surface are displayed. If you want to display all ray paths set `rayfilter = nothing`. -Also `drawtracerays!` to add to an existing scene, with `drawsys` and `drawgen` to specify whether `system` and `raygenerator` should be drawn respectively. +Also `drawtracerays!` to add to an existing Axis, with `drawsys` and `drawgen` to specify whether `system` and `raygenerator` should be drawn respectively. """ function drawtracerays(system::Q; raygenerator::S = Source(transform = translation(0.0,0.0,10.0), origins = Origins.RectGrid(10.0,10.0,25,25),directions = Constant(0.0,0.0,-1.0)), test::Bool = false, trackallrays::Bool = false, colorbysourcenum::Bool = false, colorbynhits::Bool = false, rayfilter::Union{Nothing,Function} = onlydetectorrays, verbose::Bool = false, resolution::Tuple{Int,Int} = (1000, 1000), kwargs...) where {T<:Real,Q<:AbstractOpticalSystem{T},S<:AbstractRayGenerator{T}} verbose && println("Drawing System...") @@ -528,11 +455,11 @@ end drawtracerays!(system::Q; kwargs...) where {T<:Real,Q<:AbstractOpticalSystem{T}} = drawtracerays!(current_3d_scene, system; kwargs...) -function drawtracerays!(scene::Makie.LScene, system::Q; raygenerator::S = Source(transform = translation(0.0,0.0,10.0), origins = Origins.RectGrid(10.0,10.0,25,25),directions = Constant(0.0,0.0,-1.0)), test::Bool = false, trackallrays::Bool = false, colorbysourcenum::Bool = false, colorbynhits::Bool = false, rayfilter::Union{Nothing,Function} = onlydetectorrays, verbose::Bool = false, drawsys::Bool = false, drawgen::Bool = false, kwargs...) where {T<:Real,Q<:AbstractOpticalSystem{T},S<:AbstractRayGenerator{T}} +function drawtracerays!(ax::Makie.AbstractAxis, system::Q; raygenerator::S = Source(transform = translation(0.0,0.0,10.0), origins = Origins.RectGrid(10.0,10.0,25,25),directions = Constant(0.0,0.0,-1.0)), test::Bool = false, trackallrays::Bool = false, colorbysourcenum::Bool = false, colorbynhits::Bool = false, rayfilter::Union{Nothing,Function} = onlydetectorrays, verbose::Bool = false, drawsys::Bool = false, drawgen::Bool = false, kwargs...) where {T<:Real,Q<:AbstractOpticalSystem{T},S<:AbstractRayGenerator{T}} raylines = Vector{LensTrace{T,3}}(undef, 0) drawgen && draw!(scene, raygenerator; kwargs...) - drawsys && draw!(scene, system; kwargs...) + drawsys && draw!(ax, system; kwargs...) verbose && println("Tracing...") for (i, r) in enumerate(raygenerator) @@ -562,7 +489,7 @@ function drawtracerays!(scene::Makie.LScene, system::Q; raygenerator::S = Source verbose && print("\r") verbose && println("Drawing Rays...") - draw!(scene, raylines, colorbysourcenum = colorbysourcenum, colorbynhits = colorbynhits; kwargs...) + draw!(ax, raylines, colorbysourcenum = colorbysourcenum, colorbynhits = colorbynhits; kwargs...) end """ @@ -591,34 +518,34 @@ end ## RAYS, LINES AND POINTS """ - draw!(scene::Makie.LScene, rays::AbstractVector{<:AbstractRay{T,N}}; kwargs...) + draw!(ax::Makie.AbstractAxis, rays::AbstractVector{<:AbstractRay{T,N}}; kwargs...) Draw a vector of [`Ray`](@ref) or [`OpticalRay`](@ref) objects. """ -function draw!(scene::Makie.LScene, rays::AbstractVector{<:AbstractRay{T,N}}; kwargs...) where {T<:Real,N} +function draw!(ax::Makie.AbstractAxis, rays::AbstractVector{<:AbstractRay{T,N}}; kwargs...) where {T<:Real,N} for r in rays - draw!(scene, r; kwargs...) + draw!(ax, r; kwargs...) end end """ - draw!(scene::Makie.LScene, traces::AbstractVector{LensTrace{T,N}}; kwargs...) + draw!(ax::Makie.AbstractAxis, traces::AbstractVector{LensTrace{T,N}}; kwargs...) Draw a vector of [`LensTrace`](@ref) objects. """ -function draw!(scene::Makie.LScene, traces::AbstractVector{LensTrace{T,N}}; kwargs...) where {T<:Real,N} +function draw!(ax::Makie.AbstractAxis, traces::AbstractVector{LensTrace{T,N}}; kwargs...) where {T<:Real,N} for t in traces - draw!(scene, t; kwargs...) + draw!(ax, t; kwargs...) end end """ - draw!(scene::Makie.LScene, trace::LensTrace{T,N}; colorbysourcenum::Bool = false, colorbynhits::Bool = false, kwargs...) + draw!(ax::Makie.AbstractAxis, trace::LensTrace{T,N}; colorbysourcenum::Bool = false, colorbynhits::Bool = false, kwargs...) Draw a [`LensTrace`](@ref) as a line which can be colored automatically by its `sourcenum` or `nhits` attributes. The alpha is determined by the `power` attribute of `trace`. """ -function draw!(scene::Makie.LScene, trace::LensTrace{T,N}; colorbysourcenum::Bool = false, colorbynhits::Bool = false, kwargs...) where {T<:Real,N} +function draw!(ax::Makie.AbstractAxis, trace::LensTrace{T,N}; colorbysourcenum::Bool = false, colorbynhits::Bool = false, kwargs...) where {T<:Real,N} if colorbysourcenum color = indexedcolor(sourcenum(trace)) elseif colorbynhits @@ -626,17 +553,17 @@ function draw!(scene::Makie.LScene, trace::LensTrace{T,N}; colorbysourcenum::Boo else color = λtoRGB(wavelength(trace)) end - draw!(scene, (origin(ray(trace)), point(intersection(trace))); kwargs..., color = RGBA(color.r, color.g, color.b, sqrt(power(trace))), transparency = true) + draw!(ax, (origin(ray(trace)), point(intersection(trace))); kwargs..., color = RGBA(color.r, color.g, color.b, sqrt(power(trace))), transparency = true) end """ - draw!(scene::Makie.LScene, ray::OpticalRay{T,N}; colorbysourcenum::Bool = false, colorbynhits::Bool = false, kwargs...) + draw!(ax::Makie.AbstractAxis, ray::OpticalRay{T,N}; colorbysourcenum::Bool = false, colorbynhits::Bool = false, kwargs...) Draw an [`OpticalRay`](@ref) which can be colored automatically by its `sourcenum` or `nhits` attributes. The alpha of the ray is determined by the `power` attribute of `ray`. -`kwargs` are passed to `draw!(scene, ray::Ray)`. +`kwargs` are passed to `draw!(ax, ray::Ray)`. """ -function draw!(scene::Makie.LScene, r::OpticalRay{T,N}; colorbysourcenum::Bool = false, colorbynhits::Bool = false, kwargs...) where {T<:Real,N} +function draw!(ax::Makie.AbstractAxis, r::OpticalRay{T,N}; colorbysourcenum::Bool = false, colorbynhits::Bool = false, kwargs...) where {T<:Real,N} if colorbysourcenum color = indexedcolor(sourcenum(r)) elseif colorbynhits @@ -644,128 +571,128 @@ function draw!(scene::Makie.LScene, r::OpticalRay{T,N}; colorbysourcenum::Bool = else color = λtoRGB(wavelength(r)) end - draw!(scene, ray(r); kwargs..., color = RGBA(color.r, color.g, color.b, sqrt(power(r))), transparency = true, rayscale = power(r)) + draw!(ax, ray(r); kwargs..., color = RGBA(color.r, color.g, color.b, sqrt(power(r))), transparency = true, rayscale = power(r)) end """ - draw!(scene::Makie.LScene, ray::Ray{T,N}; color = :yellow, rayscale = 1.0, kwargs...) + draw!(ax::Makie.AbstractAxis, ray::Ray{T,N}; color = :yellow, rayscale = 1.0, kwargs...) Draw a [`Ray`](@ref) in a given `color` optionally scaling the size using `rayscale`. `kwargs` are passed to [`Makie.arrows`](http://makie.juliaplots.org/stable/plotting_functions.html#arrows). """ -function draw!(scene::Makie.LScene, ray::AbstractRay{T,N}; +function draw!(ax::Makie.AbstractAxis, ray::AbstractRay{T,N}; color = :yellow, debug::Bool = false, # make sure debug does not end up in kwargs (Makie would error) rayscale = 1.0, kwargs... ) where {T<:Real,N} arrow_size = min(0.05, rayscale * 0.05) - Makie.arrows!(scene, [Makie.Point3f(origin(ray))], [Makie.Point3f(rayscale * direction(ray))]; kwargs..., arrowsize = arrow_size, arrowcolor = color, linecolor = color, linewidth=arrow_size * 0.5) + Makie.arrows!(ax, [Makie.Point3f(origin(ray))], [Makie.Point3f(rayscale * direction(ray))]; kwargs..., arrowsize = arrow_size, arrowcolor = color, linecolor = color, linewidth=arrow_size * 0.5) end """ - draw!(scene::Makie.LScene, du::DisjointUnion{T}; kwargs...) + draw!(ax::Makie.AbstractAxis, du::DisjointUnion{T}; kwargs...) Draw each [`Interval`](@ref) in a [`DisjointUnion`](@ref). """ -function draw!(scene::Makie.LScene, du::DisjointUnion{T}; kwargs...) where {T<:Real} - draw!(scene, intervals(du); kwargs...) +function draw!(ax::Makie.AbstractAxis, du::DisjointUnion{T}; kwargs...) where {T<:Real} + draw!(ax, intervals(du); kwargs...) end """ - draw!(scene::Makie.LScene, intervals::AbstractVector{Interval{T}}; kwargs...) + draw!(ax::Makie.AbstractAxis, intervals::AbstractVector{Interval{T}}; kwargs...) Draw a vector of [`Interval`](@ref)s. """ -function draw!(scene::Makie.LScene, intervals::AbstractVector{Interval{T}}; kwargs...) where {T<:Real} +function draw!(ax::Makie.AbstractAxis, intervals::AbstractVector{Interval{T}}; kwargs...) where {T<:Real} for i in intervals - draw!(scene, i; kwargs...) + draw!(ax, i; kwargs...) end end """ - draw!(scene::Makie.LScene, interval::Interval{T}; kwargs...) + draw!(ax::Makie.AbstractAxis, interval::Interval{T}; kwargs...) Draw an [`Interval`](@ref) as a line with circles at each [`Intersection`](@ref) point. """ -function draw!(scene::Makie.LScene, interval::Interval{T}; kwargs...) where {T<:Real} +function draw!(ax::Makie.AbstractAxis, interval::Interval{T}; kwargs...) where {T<:Real} if !(interval isa EmptyInterval) l = lower(interval) u = upper(interval) if !(l isa RayOrigin) - draw!(scene, l) + draw!(ax, l) else @warn "Negative half space, can't draw ray origin" end if !(u isa Infinity) - draw!(scene, u) + draw!(ax, u) else @warn "Positive half space, can't draw end point" end if !(l isa RayOrigin) && !(u isa Infinity) - draw!(scene, (point(l), point(u)); kwargs...) + draw!(ax, (point(l), point(u)); kwargs...) end end end """ - draw!(scene::Makie.LScene, intersection::Intersection; normal::Bool = false, kwargs...) + draw!(ax::Makie.AbstractAxis, intersection::Intersection; normal::Bool = false, kwargs...) Draw an [`Intersection`](@ref) as a circle, optionally showing the surface normal at the point. """ -function draw!(scene::Makie.LScene, intersection::Intersection; normal::Bool = false, kwargs...) - draw!(scene, point(intersection); kwargs...) +function draw!(ax::Makie.AbstractAxis, intersection::Intersection; normal::Bool = false, kwargs...) + draw!(ax, point(intersection); kwargs...) if normal - draw!(scene, Ray(point(intersection), normal(intersection)); kwargs...) + draw!(ax, Ray(point(intersection), normal(intersection)); kwargs...) end end """ - draw!(scene::Makie.LScene, lines::AbstractVector{Tuple{AbstractVector{T},AbstractVector{T}}}; kwargs...) + draw!(ax::Makie.AbstractAxis, lines::AbstractVector{Tuple{AbstractVector{T},AbstractVector{T}}}; kwargs...) Draw a vector of lines. """ -function draw!(scene::Makie.LScene, lines::AbstractVector{Tuple{P,P}}; kwargs...) where {T<:Real,P<:AbstractVector{T}} +function draw!(ax::Makie.AbstractAxis, lines::AbstractVector{Tuple{P,P}}; kwargs...) where {T<:Real,P<:AbstractVector{T}} for l in lines - draw!(scene, l; kwargs...) + draw!(ax, l; kwargs...) end end """ - draw!(scene::Makie.LScene, line::Tuple{AbstractVector{T},AbstractVector{T}}; color = :yellow, kwargs...) + draw!(ax::Makie.AbstractAxis, line::Tuple{AbstractVector{T},AbstractVector{T}}; color = :yellow, kwargs...) Draw a line between two points, `kwargs` are passed to [`Makie.linesegments`](http://makie.juliaplots.org/stable/plotting_functions.html#linesegments). """ -function draw!(scene::Makie.LScene, line::Tuple{P,P}; +function draw!(ax::Makie.AbstractAxis, line::Tuple{P,P}; debug::Bool = false, # make sure debug does not end up in kwargs (Makie would error) color = :yellow, kwargs... ) where {T<:Real,P<:AbstractVector{T}} - Makie.linesegments!(scene, [line[1], line[2]]; kwargs..., color = color) + Makie.linesegments!(ax, [line[1], line[2]]; kwargs..., color = color) end """ - draw!(s::Makie.LScene, point::AbstractVector{T}; kwargs...) + draw!(ax::Makie.AbstractAxis, point::AbstractVector{T}; kwargs...) -Draw a single point, `kwargs` are passed to `draw!(scene, points::AbstractVector{AbstractVector{T}})`. +Draw a single point, `kwargs` are passed to `draw!(ax, points::AbstractVector{AbstractVector{T}})`. """ -function draw!(s::Makie.LScene, point::AbstractVector{T}; kwargs...) where {T<:Real} +function draw!(ax::Makie.AbstractAxis, point::AbstractVector{T}; kwargs...) where {T<:Real} draw!(s, [point]; kwargs...) end """ - draw!(scene::Makie.LScene, points::AbstractVector{AbstractVector{T}}; markersize = 20, color = :black, kwargs...) + draw!(ax::Makie.AbstractAxis, points::AbstractVector{AbstractVector{T}}; markersize = 20, color = :black, kwargs...) Draw a vector of points. `kwargs` are passed to [`Makie.scatter`](http://makie.juliaplots.org/stable/plotting_functions.html#scatter). """ -function draw!(scene::Makie.LScene, points::AbstractVector{P}; +function draw!(ax::Makie.AbstractAxis, points::AbstractVector{P}; debug::Bool = false, # make sure debug does not end up in kwargs (Makie would error) markersize = 20, color = :black, kwargs... ) where {T<:Real,P<:AbstractVector{T}} - Makie.scatter!(scene, points, markersize = markersize, color = color, strokewidth = 0; kwargs...) + Makie.scatter!(ax, points, markersize = markersize, color = color, strokewidth = 0; kwargs...) end #######################################################