Skip to content
This repository has been archived by the owner on Oct 23, 2022. It is now read-only.

Support Makie’s modern interface – removed explicit references to AbstractPlotting and MakieLayout #214

Merged
merged 4 commits into from
Jul 4, 2021
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
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Format = "1fa38f19-a742-5d3f-a2b9-30dd87b9d5f8"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
ImageView = "86fae568-95e7-573e-a6b2-d8a6b900c9ef"
Images = "916415d5-f1e6-5110-898d-aaa5f9f070e0"
Expand Down
6 changes: 2 additions & 4 deletions samples/notebooks/BasicCSG.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ begin
import PlutoUI

using Makie
using Makie.AbstractPlotting
using Makie.AbstractPlotting.MakieLayout

defs = OpticSim.NotebooksUtils.Defs("ran")

Expand Down Expand Up @@ -233,9 +231,9 @@ end
begin
function Vis.scene(resolution = (1000, 1000))
# @info "RG: Vis.Scene Replacement"
scene, layout = MakieLayout.layoutscene(resolution = resolution)
scene, layout = Makie.layoutscene(resolution = resolution)
Vis.set_current_main_scene(scene)
lscene = layout[1, 1] = MakieLayout.LScene(scene, scenekw = (camera = Makie.cam3d_cad!, axis_type = Makie.axis3d!, raw = false))
lscene = layout[1, 1] = Makie.LScene(scene, scenekw = (camera = Makie.cam3d_cad!, axis_type = Makie.axis3d!, raw = false))
Vis.set_current_3d_scene(lscene)
return scene, lscene
end
Expand Down
8 changes: 3 additions & 5 deletions samples/notebooks/EmittersIntro.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ begin
import PlutoUI

using Makie
using Makie.AbstractPlotting
using Makie.AbstractPlotting.MakieLayout

defs = OpticSim.NotebooksUtils.Defs("ran")

Expand Down Expand Up @@ -235,9 +233,9 @@ end
begin
function Vis.scene(resolution = (1000, 1000))
# @info "RG: Vis.Scene Replacement"
scene, layout = MakieLayout.layoutscene(resolution = resolution)
scene, layout = Makie.layoutscene(resolution = resolution)
Vis.set_current_main_scene(scene)
lscene = layout[1, 1] = MakieLayout.LScene(scene, scenekw = (camera = Makie.cam3d_cad!, axis_type = Makie.axis3d!, raw = false))
lscene = layout[1, 1] = Makie.LScene(scene, scenekw = (camera = Makie.cam3d_cad!, axis_type = Makie.axis3d!, raw = false))
Vis.set_current_3d_scene(lscene)
return scene, lscene
end
Expand Down Expand Up @@ -346,7 +344,7 @@ begin
local s2 = Sources.Source(sourcenum = 2, origins=Origins.Hexapolar(5, 8.0, 8.0), directions=Directions.Constant(), transform=Transform(zeros(Geometry.Vec3), rotationY(deg2rad(-8)) * unitZ3()))

# create the "ray generator"
local combined_sources = Sources.CompositeSource(Transform(Geometry.Vec3(0.0, 0.0, 10.0), unitZ3() * -1), [s1 s2])
local combined_sources = Sources.CompositeSource(Transform(Geometry.Vec3(0.0, 0.0, 10.0), unitZ3() * -1), [s1, s2])

# and draw the system + the generated rays
Vis.drawtracerays(sys, raygenerator = combined_sources, resolution=resolution(), test = true, trackallrays = true, colorbysourcenum = true, drawgen = false)
Expand Down
6 changes: 2 additions & 4 deletions samples/notebooks/Samples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ begin
import PlutoUI

using Makie
using Makie.AbstractPlotting
using Makie.AbstractPlotting.MakieLayout

defs = OpticSim.NotebooksUtils.Defs("ran")

Expand Down Expand Up @@ -204,9 +202,9 @@ end
begin
function Vis.scene(resolution = (1000, 1000))
# @info "RG: Vis.Scene Replacement"
scene, layout = MakieLayout.layoutscene(resolution = resolution)
scene, layout = Makie.layoutscene(resolution = resolution)
Vis.set_current_main_scene(scene)
lscene = layout[1, 1] = MakieLayout.LScene(scene, scenekw = (camera = Makie.cam3d_cad!, axis_type = Makie.axis3d!, raw = false))
lscene = layout[1, 1] = Makie.LScene(scene, scenekw = (camera = Makie.cam3d_cad!, axis_type = Makie.axis3d!, raw = false))
Vis.set_current_3d_scene(lscene)
return scene, lscene
end
Expand Down
21 changes: 15 additions & 6 deletions src/NotebooksUtils/NotebooksUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import PlutoUI
import Format

import Makie
import Makie.AbstractPlotting
import Makie.AbstractPlotting.MakieLayout
import WGLMakie
import GLMakie

Expand Down Expand Up @@ -250,13 +248,24 @@ function SetBackend(defs::Defs, be::String)
if (be == "Web")
@info "Makie backend set to WEB (WGLMakie)"
WGLMakie.activate!()
AbstractPlotting.__init__()
AbstractPlotting.inline!(true)
# this try and catch is for Makie versions below 0.13 (where Abstract Plotting was removed and renamed to Makie)
# the display stack used to get shuffled around such that the Makie display did not take priority.
# with Makie v0.13 and above is should not be nececery but i didn't find a clean way to test for the Makie version
try
Makie.AbstractPlotting.__init__()
Makie.AbstractPlotting.inline!(true)
catch e
Makie.inline!(true) # for version 0.13 and above
end
else
@info "Makie backend set to STATIC (GLMakie)"
GLMakie.activate!()
AbstractPlotting.__init__()
AbstractPlotting.inline!(true)
try
Makie.AbstractPlotting.__init__()
Makie.AbstractPlotting.inline!(true)
catch e
Makie.inline!(true) # for version 0.13 and above
end
end

end
Expand Down
11 changes: 9 additions & 2 deletions src/OpticSim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using Revise

# included here to allow a call to the activate! during the initialization
import GLMakie
import Makie.AbstractPlotting
import Makie

include("constants.jl")
include("utilities.jl")
Expand All @@ -43,7 +43,14 @@ function __init__()
# this call is to try and keep the original behevior of Makie's default backend after adding the WGLMakie backend to the package
try
GLMakie.activate!()
AbstractPlotting.__init__()
# this try and catch is for Makie versions below 0.13 (where Abstract Plotting was removed and renamed to Makie)
# the display stack used to get shuffled around such that the Makie display did not take priority.
# with Makie v0.13 and above is should not be nececery but i didn't find a clean way to test for the Makie version
try
Makie.AbstractPlotting.__init__()
catch e
# do nothing
end
catch e
@warn "Unable to activate! the GLMakie backend\n$e"
end
Expand Down
29 changes: 13 additions & 16 deletions src/Vis/Emitters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ using Distributions
using StaticArrays

import Makie
import Makie.AbstractPlotting
import Makie.AbstractPlotting.MakieLayout

using .Emitters
using .Emitters.Spectrum
Expand All @@ -26,7 +24,7 @@ const MARKER_SIZE = 15
#-------------------------------------
# draw debug information - local axes and positions
#-------------------------------------
function maybe_draw_debug_info(scene::MakieLayout.LScene, o::Origins.AbstractOriginDistribution; transform::Geometry.Transform = Transform(), debug::Bool=false, kwargs...) where {T<:Real}
function maybe_draw_debug_info(scene::Makie.LScene, o::Origins.AbstractOriginDistribution; transform::Geometry.Transform = Transform(), debug::Bool=false, kwargs...) where {T<:Real}

dir = forward(transform)
uv = SVector{3}(right(transform))
Expand All @@ -35,26 +33,26 @@ function maybe_draw_debug_info(scene::MakieLayout.LScene, o::Origins.AbstractOri

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

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

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

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

Expand All @@ -64,15 +62,14 @@ end
#-------------------------------------
# draw point origin
#-------------------------------------
# function OpticSim.Vis.draw!(scene::MakieLayout.LScene, o::Origins.Point; transform::Geometry.Transform = Transform(), kwargs...) where {T<:Real}
function OpticSim.Vis.draw!(scene::MakieLayout.LScene, o::Origins.Point; transform::Geometry.Transform = Transform(), kwargs...) where {T<:Real}
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...)
end

#-------------------------------------
# draw RectGrid and RectUniform origins
#-------------------------------------
function OpticSim.Vis.draw!(scene::MakieLayout.LScene, o::Union{Origins.RectGrid, Origins.RectUniform}; transform::Geometry.Transform = Transform(), kwargs...) where {T<:Real}
function OpticSim.Vis.draw!(scene::Makie.LScene, 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 @@ -92,7 +89,7 @@ end
#-------------------------------------
# draw hexapolar origin
#-------------------------------------
function OpticSim.Vis.draw!(scene::MakieLayout.LScene, o::Origins.Hexapolar; transform::Geometry.Transform = Transform(), kwargs...) where {T<:Real}
function OpticSim.Vis.draw!(scene::Makie.LScene, 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 @@ -109,7 +106,7 @@ end
#-------------------------------------
# draw source
#-------------------------------------
function OpticSim.Vis.draw!(scene::MakieLayout.LScene, s::Sources.Source{T}; parent_transform::Geometry.Transform = Transform(), debug::Bool=false, kwargs...) where {T<:Real}
function OpticSim.Vis.draw!(scene::Makie.LScene, s::Sources.Source{T}; parent_transform::Geometry.Transform = Transform(), debug::Bool=false, kwargs...) where {T<:Real}

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

Expand All @@ -136,7 +133,7 @@ end
#-------------------------------------
# draw optical rays
#-------------------------------------
function OpticSim.Vis.draw!(scene::MakieLayout.LScene, rays::AbstractVector{OpticSim.OpticalRay{T, 3}}; kwargs...) where {T<:Real}
function OpticSim.Vis.draw!(scene::Makie.LScene, rays::AbstractVector{OpticSim.OpticalRay{T, 3}}; kwargs...) where {T<:Real}
m = zeros(T, length(rays)*2, 3)
for (index, optical_ray) in enumerate(rays)
ray = OpticSim.ray(optical_ray)
Expand All @@ -151,7 +148,7 @@ end
#-------------------------------------
# draw composite source
#-------------------------------------
function OpticSim.Vis.draw!(scene::MakieLayout.LScene, s::Sources.CompositeSource{T}; parent_transform::Geometry.Transform = Transform(), kwargs...) where {T<:Real}
function OpticSim.Vis.draw!(scene::Makie.LScene, 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*s.transform, kwargs...)
end
Expand Down
2 changes: 1 addition & 1 deletion src/Vis/Vis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Vis
# If using precompiled system image (which we always are) you have to run AbstractPlotting.__init__() after loading Makie
# during precompilation, the display stack gets shuffled around such that the Makie display does not take priority.
# See https://discourse.julialang.org/t/makie-doesnt-display-plot-when-using-a-custom-julia-sysimage/38515.
__init__() = AbstractPlotting.__init__()
__init__() = try Makie.AbstractPlotting.__init__() catch e end # for versions of Makie below 0.13

include("Visualization.jl")
include("Emitters.jl")
Expand Down
Loading