Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sampleplotting #109

Merged
merged 8 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 6 additions & 1 deletion docs/src/tutorial_lit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ using Unitful
T = Float32
simulation = Simulation{T}(SSD_examples[:InvertedCoax])

plot(simulation.detector, size = (700, 700))
plot(simulation.detector)
# This will use the defaul plotting method (SSD_style = :wireframe). You can also use SSD_style = :samplesurface, which will show the "solid" geometry.


plot(simulation.detector, SSD_style = :samplesurface, alpha_factor = 1)
# alpha_factor ϵ [0,1) will increase transparency and alpa_factor > 1 will increase opacity.

# One can also have a look at how the initial conditions look like on the grid (its starts with a very coarse grid):

Expand Down
4 changes: 2 additions & 2 deletions src/Geometries/LinePrimitives/LinePrimitives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ struct PartialCircle{T,N,S} <: AbstractLine{T,N,S}
Rotate::AbstractMatrix{T}
end

function PartialCircle(r::T, phiStart::T, phiStop::T, Translate::CartesianVector{T} = CartesianVector{T}([0,0,0]), Rotate::Rotation{3,Float32} = RotZ{T}(0)) where {T}
function PartialCircle(r::T, phiStart::T, phiStop::T, Translate::CartesianVector{T} = CartesianVector{T}([0,0,0]), Rotate::Rotation{3,T} = RotZ{T}(0)) where {T}
PartialCircle{T, 3, :cartesian}(r, phiStart, phiStop, Translate, Rotate)
end

function PartialCircle(r::T, phiStart::T, phiStop::T, Translate::CartesianVector{T}, Rotate::Missing) where {T}
PartialCircle{T, 3, :cartesian}(r, phiStart, phiStop, Translate, RotZ{T}(0))
end

function PartialCircle(r::T, phiStart::T, phiStop::T, Translate::Missing, Rotate::Rotation{3,Float32}) where {T}
function PartialCircle(r::T, phiStart::T, phiStop::T, Translate::Missing, Rotate::Rotation{3,T}) where {T}
PartialCircle{T, 3, :cartesian}(r, phiStart, phiStop, CartesianVector{T}([0,0,0]), Rotate)
end

Expand Down
97 changes: 64 additions & 33 deletions src/Geometries/VolumePrimitives/HexagonalPrism.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,44 +63,75 @@ function (+)(hp::HexagonalPrism{T}, translate::Union{CartesianVector{T}, Missing
end
end

function LineSegments(hp::HexagonalPrism{T})::Vector{AbstractLine{T,3,:cartesian}} where {T <: SSDFloat}
pts_top_outer = []
pts_bottom_outer = []
pts_top_inner = []
pts_bottom_inner = []
#find all vertices, this loop has been tested and works
for φ in deg2rad.(30:60:330) .- hp.rotZ
pt_top_outer = CartesianPoint{T}(hp.translate.x + hp.rOuter * cos(φ), hp.translate.y + hp.rOuter * sin(φ), hp.translate.z + hp.h/2)
push!(pts_top_outer, pt_top_outer)
pt_top_inner = CartesianPoint{T}(hp.translate.x + hp.rInner * cos(φ), hp.translate.y + hp.rInner * sin(φ), hp.translate.z + hp.h/2)
push!(pts_top_inner, pt_top_inner)
pt_bottom_outer = CartesianPoint{T}(hp.translate.x + hp.rOuter * cos(φ), hp.translate.y + hp.rOuter * sin(φ), hp.translate.z -hp.h/2)
push!(pts_bottom_outer, pt_bottom_outer)
pt_bottom_inner = CartesianPoint{T}(hp.translate.x + hp.rInner * cos(φ), hp.translate.y + hp.rInner * sin(φ), hp.translate.z -hp.h/2)
push!(pts_bottom_inner, pt_bottom_inner)
end

#create Linesegments connecting the vertices
lines = LineSegment{T, 3, :cartesian}[]
N = length(pts_top_outer)
for i in 1:N
push!(lines, LineSegment(pts_top_outer[i%N+1], pts_top_outer[i])) #top outer hexagon
push!(lines, LineSegment(pts_bottom_outer[i%N+1], pts_bottom_outer[i])) #bottom outer hexagon
push!(lines, LineSegment(pts_bottom_outer[i], pts_top_outer[i])) #lines connecting outer hexagons
if hp.rInner > 0
push!(lines, LineSegment(pts_bottom_inner[i%N+1], pts_bottom_inner[i])) #bottom inner hexagon
push!(lines, LineSegment(pts_top_inner[i%N+1], pts_top_inner[i])) #top inner hexagon
push!(lines, LineSegment(pts_bottom_inner[i], pts_top_inner[i])) #lines connecting inner hexagons
push!(lines, LineSegment(pts_top_outer[i], pts_top_inner[i])) #lines connecting hexagons
push!(lines, LineSegment(pts_bottom_outer[i], pts_bottom_inner[i])) #lines connecting hexagons
end
end
return lines
end
# Also a plot recipe for this new primitive should be provided:
@recipe function f(hp::HexagonalPrism{T}) where {T <: SSDFloat}
label --> "HexagonalPrism"
@recipe function f(hp::HexagonalPrism{T}; seriescolor = :purple, SSD_style = :wireframe, detector = missing, contact = missing, alpha_factor = 1) where {T <: SSDFloat}
linewidth --> 2
@series begin
pts_top_outer = []
pts_bottom_outer = []
pts_top_inner = []
pts_bottom_inner = []

#find all vertices, this loop has been tested and works
for φ in deg2rad.(30:60:330) .- hp.rotZ
pt_top_outer = CartesianPoint{T}(hp.translate.x + hp.rOuter * cos(φ), hp.translate.y + hp.rOuter * sin(φ), hp.translate.z + hp.h/2)
push!(pts_top_outer, pt_top_outer)
pt_top_inner = CartesianPoint{T}(hp.translate.x + hp.rInner * cos(φ), hp.translate.y + hp.rInner * sin(φ), hp.translate.z + hp.h/2)
push!(pts_top_inner, pt_top_inner)
pt_bottom_outer = CartesianPoint{T}(hp.translate.x + hp.rOuter * cos(φ), hp.translate.y + hp.rOuter * sin(φ), hp.translate.z -hp.h/2)
push!(pts_bottom_outer, pt_bottom_outer)
pt_bottom_inner = CartesianPoint{T}(hp.translate.x + hp.rInner * cos(φ), hp.translate.y + hp.rInner * sin(φ), hp.translate.z -hp.h/2)
push!(pts_bottom_inner, pt_bottom_inner)
seriescolor --> seriescolor
label --> "HexagonalPrism"
[]
end
label := ""
seriescolor := seriescolor
α = 1
st = :path
if SSD_style == :wireframe
plotobject = LineSegments(hp)
elseif SSD_style == :samplesurface
st = :scatter
grid = Grid(detector)
coord_sys = get_coordinate_system(grid)
points = 100
if coord_sys == :cylindrical
sampling_vector = T.([width(grid.r.interval)/points, width(grid.r.interval)/points, width(grid.z.interval)/points])
plotobject = CylindricalPoint.(sample(hp, sampling_vector))
elseif coord_sys == :cartesian
sampling_vector = T.([width(grid.x.interval)/points, width(grid.y.interval)/points, width(grid.z.interval)/points])
plotobject = CartesianPoint{T}.(sample(hp, sampling_vector))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CartesianPoint{T} is not needed.

end

#create Linesegments connecting the vertices
lines = LineSegment{T, 3, :cartesian}[]
N = length(pts_top_outer)
for i in 1:N
push!(lines, LineSegment(pts_top_outer[i%N+1], pts_top_outer[i])) #top outer hexagon
push!(lines, LineSegment(pts_bottom_outer[i%N+1], pts_bottom_outer[i])) #bottom outer hexagon
push!(lines, LineSegment(pts_bottom_outer[i], pts_top_outer[i])) #lines connecting outer hexagons
if hp.rInner > 0
push!(lines, LineSegment(pts_bottom_inner[i%N+1], pts_bottom_inner[i])) #bottom inner hexagon
push!(lines, LineSegment(pts_top_inner[i%N+1], pts_top_inner[i])) #top inner hexagon
push!(lines, LineSegment(pts_bottom_inner[i], pts_top_inner[i])) #lines connecting inner hexagons
push!(lines, LineSegment(pts_top_outer[i], pts_top_inner[i])) #lines connecting hexagons
push!(lines, LineSegment(pts_bottom_outer[i], pts_bottom_inner[i])) #lines connecting hexagons
end
for neg_geo in contact.geometry_negative
filter!(x -> !(x in neg_geo), plotobject)
end
lines
α = min(alpha_factor*max(1-length(plotobject)/3000,0.05),1)
end
seriestype := st
markerstrokewidth := 0
seriesalpha := α
plotobject
end

# For proper grid creation we also need the function get_important_points:
Expand Down
8 changes: 4 additions & 4 deletions src/Geometries/VolumePrimitives/Tube.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ function Tube{T}(dict::Dict{Any, Any}, inputunit_dict::Dict{String,Unitful.Units
else
@warn "please specify a height of the Tube 'h'"
end
φ_interval = if haskey(dict, "phi")
Interval(geom_round(T(ustrip(uconvert(u"rad", T(dict["phi"]["from"]) * inputunit_dict["angle"])))),
geom_round(T(ustrip(uconvert(u"rad", T(dict["phi"]["to"]) * inputunit_dict["angle"])))))

φ_interval = if haskey(dict, "phi")
Interval(geom_round(T(ustrip(uconvert(u"rad", T(dict["phi"]["from"]) * inputunit_dict["angle"])))),
geom_round(T(ustrip(uconvert(u"rad", T(dict["phi"]["to"]) * inputunit_dict["angle"])))))
else
Interval(T(0), geom_round(T(2π)))
end
Expand Down
84 changes: 78 additions & 6 deletions src/Geometries/VolumePrimitives/plot_recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function LineSegments(t::Tube{T})::Vector{AbstractLine{T,3,:cartesian}} where {T
return ls
end

@recipe function f(t::Tube{T}; n = 30, seriescolor = :green) where {T}
@recipe function f(t::Tube{T}; n = 30, seriescolor = :green, SSD_style = :wireframe, detector = missing, contact = missing, alpha_factor = 1) where {T}
linewidth --> 2
n --> n
@series begin
Expand All @@ -36,7 +36,31 @@ end
end
label := ""
seriescolor := seriescolor
LineSegments(t)
α = 1
st = :path
if SSD_style == :wireframe
plotobject = LineSegments(t)
elseif SSD_style == :samplesurface
st = :scatter
grid = Grid(detector)
coord_sys = get_coordinate_system(grid)
points = 100
if coord_sys == :cylindrical
sampling_vector = T.([width(grid.r.interval)/points, π/points, width(grid.z.interval)/points])
plotobject = CylindricalPoint{T}.(sample(t, sampling_vector))
elseif coord_sys == :cartesian
sampling_vector = T.([width(grid.x.interval)/points, width(grid.y.interval)/points, width(grid.z.interval)/points])
plotobject = CartesianPoint{T}.(sample(t, sampling_vector))
end
for neg_geo in contact.geometry_negative
filter!(x -> !(x in neg_geo), plotobject)
end
α = min(alpha_factor*max(1-length(plotobject)/3000,0.05),1)
end
seriestype := st
markerstrokewidth := 0
seriesalpha := α
plotobject
end


Expand Down Expand Up @@ -69,7 +93,7 @@ function LineSegments(c::Cone{T})::Vector{AbstractLine{T, 3, :cartesian}} where
return ls
end

@recipe function f(c::Cone{T}; n = 30, seriescolor = :orange) where {T}
@recipe function f(c::Cone{T}; n = 30, seriescolor = :orange, SSD_style = :wireframe, detector = missing, contact = missing, alpha_factor = 1) where {T}
linewidth --> 2
n --> n
@series begin
Expand All @@ -79,7 +103,31 @@ end
end
seriescolor := seriescolor
label := ""
LineSegments(c)
α = 1
st = :path
if SSD_style == :wireframe
plotobject = LineSegments(c)
elseif SSD_style == :samplesurface
st = :scatter
grid = Grid(detector)
coord_sys = get_coordinate_system(grid)
points = 100
if coord_sys == :cylindrical
sampling_vector = T.([width(grid.r.interval)/points, π/points, width(grid.z.interval)/points])
plotobject = CylindricalPoint{T}.(sample(c, sampling_vector))
elseif coord_sys == :cartesian
sampling_vector = T.([width(grid.x.interval)/points, width(grid.y.interval)/points, width(grid.z.interval)/points])
plotobject = CartesianPoint{T}.(sample(t, sampling_vector))
end
for neg_geo in contact.geometry_negative
filter!(x -> !(x in neg_geo), plotobject)
end
α = min(alpha_factor*max(1-length(plotobject)/3000,0.05),1)
end
seriestype := st
markerstrokewidth := 0
seriesalpha := α
plotobject
end

function LineSegments(t::Torus{T})::Vector{AbstractLine{T,3,:cartesian}} where {T <: SSDFloat}
Expand Down Expand Up @@ -108,7 +156,7 @@ function LineSegments(t::Torus{T})::Vector{AbstractLine{T,3,:cartesian}} where {
return ls
end

@recipe function f(t::Torus{T}; n = 30, seriescolor = :green) where {T}
@recipe function f(t::Torus{T}; n = 30, seriescolor = :red, SSD_style = :wireframe, detector = missing, contact = missing, alpha_factor = 1) where {T}
linewidth --> 2
n --> n
@series begin
Expand All @@ -118,5 +166,29 @@ end
end
label := ""
seriescolor := seriescolor
LineSegments(t)
α = 1
st = :path
if SSD_style == :wireframe
plotobject = LineSegments(t)
elseif SSD_style == :samplesurface
st = :scatter
grid = Grid(detector)
coord_sys = get_coordinate_system(grid)
points = 100
if coord_sys == :cylindrical
sampling_vector = T.([width(grid.r.interval)/points, π/points, π/points])
plotobject = CylindricalPoint{T}.(sample(t, sampling_vector))
elseif coord_sys == :cartesian
sampling_vector = T.([width(grid.x.interval)/points, width(grid.y.interval)/points, width(grid.z.interval)/points])
plotobject = CartesianPoint{T}.(sample(t, sampling_vector))
end
for neg_geo in contact.geometry_negative
filter!(x -> !(x in neg_geo), plotobject)
end
α = min(alpha_factor*max(1-length(plotobject)/3000,0.05),1)
end
seriestype := st
markerstrokewidth := 0
seriesalpha := α
plotobject
end
18 changes: 14 additions & 4 deletions src/SolidStateDetector/plot_recipes.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
@recipe function f(det::SolidStateDetector{T}; n = 30, φ = missing, seriescolor = missing, label = missing) where {T}

@recipe function f(det::SolidStateDetector{T}; SSD_style = :wireframe, n = 30, φ = missing, seriescolor = missing, label = missing, alpha_factor = 1) where {T}
if !(SSD_style in [:wireframe, :samplesurface])
@warn "Chose SSD_style from [:wireframe, :samplesurface]. Defaulting to :wireframe"
SSD_style = :wireframe
end
clabel = (ismissing(label) ? map(c -> (c.name == "" ? c.id : c.name), det.contacts) : label)
if !(typeof(clabel) <: AbstractArray) clabel = [clabel] end
ccolor = (ismissing(seriescolor) ? map(c -> c.id, det.contacts) : seriescolor)
Expand All @@ -16,6 +19,9 @@
@series begin
label := ""
n --> n
SSD_style --> SSD_style
detector --> det
alpha_factor --> alpha_factor
contact
end
@series begin
Expand All @@ -30,7 +36,7 @@
end


@recipe function f(contact::Contact{T}; n = 30, seriescolor = missing) where {T}
@recipe function f(contact::Contact{T}; SSD_style = :wireframe, n = 30, seriescolor = missing, detector = missing, alpha_factor = 1) where {T}
ccolor = (ismissing(seriescolor) ? contact.id : seriescolor)
@series begin
seriescolor := ccolor
Expand All @@ -42,6 +48,10 @@ end
seriescolor := ccolor
label := ""
n --> n
SSD_style --> SSD_style
detector --> detector
contact --> contact
alpha_factor --> alpha_factor
c
end
end
Expand Down Expand Up @@ -249,4 +259,4 @@ end

end
end
=#
=#