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

Commit

Permalink
more cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredclwong committed May 10, 2021
1 parent acdd656 commit 53cf644
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 88 deletions.
4 changes: 2 additions & 2 deletions src/Examples/docs_examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ function draw_zoomlenses(filenames::Vector{<:Union{Nothing,AbstractString}} = re

syss = [
AxisymmetricOpticalSystem{Float64}(DataFrame(
SurfaceType = ["Object", "Stop", "Standard", "Standard", "Standard", "Standard", "Standard", "Standard", "Standard", "Standard", "Standard", "Standard", "Standard", "Standard", "Standard", "Standard", :Image],
SurfaceType = ["Object", "Stop", "Standard", "Standard", "Standard", "Aspheric", "Standard", "Standard", "Aspheric", "Aspheric", "Standard", "Standard", "Standard", "Standard", "Standard", "Standard", "Image"],
Radius = [Inf64, Inf64, -1.6202203499676E+01, -4.8875855327468E+01, 1.5666614444619E+01, -4.2955326460481E+01, 1.0869565217391E+02, 2.3623907394283E+01, -1.6059097478722E+01, -4.2553191489362E+02, -3.5435861091425E+01, -1.4146272457208E+01, -2.5125628140704E+02, -2.2502250225023E+01, -1.0583130489999E+01, -4.4444444444444E+01, Inf64],
Aspherics = [missing, missing, missing, missing, missing, [(4, 1.0386E-04), (6, 1.4209E-07), (8, -8.8495E-09), (10, 1.2477E-10), (12, -1.0367E-12), (14, 3.6556E-15)], missing, missing, [(4, 4.2721E-05), (6, 1.2484E-07), (8, 9.7079E-09), (10, -1.8444E-10), (12, 1.8644E-12), (14, -7.7975E-15)], [(4, 1.1339E-04), (6, 4.8165E-07), (8, 1.8778E-08), (10, -5.7571E-10), (12, 8.9994E-12), (14, -4.6768E-14)], missing, missing, missing, missing, missing, missing, missing],
Parameters = [missing, missing, missing, missing, missing, [(4, 1.0386E-04), (6, 1.4209E-07), (8, -8.8495E-09), (10, 1.2477E-10), (12, -1.0367E-12), (14, 3.6556E-15)], missing, missing, [(4, 4.2721E-05), (6, 1.2484E-07), (8, 9.7079E-09), (10, -1.8444E-10), (12, 1.8644E-12), (14, -7.7975E-15)], [(4, 1.1339E-04), (6, 4.8165E-07), (8, 1.8778E-08), (10, -5.7571E-10), (12, 8.9994E-12), (14, -4.6768E-14)], missing, missing, missing, missing, missing, missing, missing],
Thickness = [Inf64, 0.0, 5.18, 0.10, 4.40, 0.16, 1.0, 4.96, zoom, 4.04, 1.35, 1.0, 2.80, 3.0, 1.22, dist, missing],
Material = [Air, Air, OHARA.S_LAH66, Air, NIKON.LLF6, Air, OHARA.S_TIH6, OHARA.S_FSL5, Air, OHARA.S_FSL5, Air, OHARA.S_LAL8, OHARA.S_FSL5, Air, OHARA.S_LAH66, Air, missing],
SemiDiameter = [Inf64, stop, 3.85433218451, 3.85433218451, 4.36304692871, 4.36304692871, 4.72505505439, 4.72505505439, 4.72505505439, 4.45240784026, 4.45240784026, 4.50974054117, 4.50974054117, 4.50974054117, 4.76271114409, 4.76271114409, 15.0]))
Expand Down
165 changes: 79 additions & 86 deletions src/Optical/OpticalSystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ function validate_axisymmetricopticalsystem_dataframe(prescription::DataFrame)
"Material" => GlassCat.AbstractGlass,
"SemiDiameter" => Real
]
supported_surface_types = ["Object", "Stop", "Image", "Standard", "Conic", "Aspheric", "Zernike"]
supported_surface_types = ["Object", "Stop", "Image", "Standard", "Aspheric", "Zernike"]

# check that required headers are present
@assert issubset(first.(required_headers), names(prescription))
Expand All @@ -254,6 +254,14 @@ function validate_axisymmetricopticalsystem_dataframe(prescription::DataFrame)
@assert findall(s->s==="Image", surface_types) == [nrow(prescription)]
end

function get_front_back_property(prescription::DataFrame, rownum::Int, property::String, default=nothing)
properties = (
property names(prescription) ?
[prescription[rownum, property], prescription[rownum + 1, property]] : repeat([missing], 2)
)
return replace(properties, missing => default)
end

"""
AxisymmetricOpticalSystem{T,C<:CSGOpticalSystem{T}} <: AbstractOpticalSystem{T}
Expand Down Expand Up @@ -295,124 +303,109 @@ struct AxisymmetricOpticalSystem{T,C<:CSGOpticalSystem{T}} <: AbstractOpticalSys

elements = Vector{Union{Surface{T},CSGTree{T}}}()
systemsemidiameter = zero(T)
sumstart = prescription[1, "SurfaceType"] == "Object" ? 2 : 1
firstelement = true

for i in 1:(nrow(prescription) - 1)
surfacetype = prescription[i, "SurfaceType"]
material = prescription[i, "Material"]

if surfacetype == "Stop"
stop = CircularAperture(
convert(T, prescription[i, "SemiDiameter"]),
SVector{3,T}(0.0, 0.0, 1.0),
SVector{3,T}(0.0, 0.0, convert(T, -sum(prescription[sumstart:(i - 1), "Thickness"])))
)
push!(elements, stop)
elseif material == OpticSim.GlassCat.Air || material === missing
continue
else
frontsurfacereflectance = backsurfacereflectance = frontconic = backconic = zero(T)
frontaspherics = backaspherics = nothing
# track sequential movement along the z-axis
vertices = convert(Vector{T}, -cumsum(replace(prescription[!, "Thickness"], Inf => 0, missing => 0)))

semidiameter::T = max(prescription[i, "SemiDiameter"], prescription[i + 1, "SemiDiameter"])
if firstelement
systemsemidiameter = semidiameter
firstelement = false
end
# pre-construct list of rows which we will skip over (e.g. air gaps, but never Stop surfaces)
# later on, this may get more complicated as we add in compound surfaces
function skip_row(i::Int)
return (
prescription[i, "SurfaceType"] != "Stop" &&
(prescription[i, "Material"] === missing || prescription[i, "Material"] == GlassCat.Air)
)
end
skips = skip_row.(1:nrow(prescription))

vertex::T = -sum(prescription[sumstart:(i - 1), "Thickness"])
frontradius::T = prescription[i, "Radius"]
backradius::T = prescription[i + 1, "Radius"]
for i in 2:nrow(prescription)-1
if skips[i]
continue
end

if "Reflectance" in names(prescription)
temp = prescription[i, "Reflectance"]
if temp !== missing
frontsurfacereflectance = temp
end
temp = prescription[i + 1, "Reflectance"]
if temp !== missing
backsurfacereflectance = temp
end
end
surface_type = prescription[i, "SurfaceType"]
material = prescription[i, "Material"]
lastmaterial = prescription[i - 1, "Material"]
nextmaterial = prescription[i + 1, "Material"]
thickness = convert(T, prescription[i, "Thickness"])

if "Conic" in names(prescription)
temp = prescription[i, "Conic"]
if temp !== missing
frontconic = temp
end
temp = prescription[i + 1, "Conic"]
if temp !== missing
backconic = temp
end
end
frontradius, backradius = get_front_back_property(prescription, i, "Radius")
frontsurfacereflectance, backsurfacereflectance = get_front_back_property(
prescription, i, "Reflectance", zero(T)
)

if "Aspherics" in names(prescription)
temp = prescription[i, "Aspherics"]
if temp !== missing
frontaspherics = temp
end
temp = prescription[i + 1, "Aspherics"]
if temp !== missing
backaspherics = temp
end
end
semidiameter = NaN

lastmaterial = prescription[i - 1, "Material"]
nextmaterial = prescription[i + 1, "Material"]

thickness = convert(T, prescription[i, "Thickness"])

if frontaspherics !== nothing || backaspherics !== nothing
newelt = AsphericLens(
material, vertex, frontradius, frontconic, frontaspherics, backradius, backconic,
backaspherics, thickness, semidiameter; lastmaterial, nextmaterial,
frontsurfacereflectance, backsurfacereflectance
)
elseif frontconic != zero(T) || backconic != zero(T)
newelt = ConicLens(
material, vertex, frontradius, frontconic, backradius, backconic, thickness,
semidiameter; lastmaterial, nextmaterial, frontsurfacereflectance,
backsurfacereflectance
)
if surface_type == "Stop"
newelement = CircularAperture(
convert(T, prescription[i, "SemiDiameter"]),
SVector{3,T}(0.0, 0.0, 1.0),
SVector{3,T}(0.0, 0.0, vertices[i-1])
)
elseif surface_type == "Standard"
semidiameter = convert(T, max(get_front_back_property(prescription, i, "SemiDiameter", zero(T))...))
frontconic, backconic = get_front_back_property(prescription, i, "Conic", zero(T))

if frontconic != zero(T) || backconic != zero(T)
newelement = ConicLens(
material, vertices[i-1], frontradius, frontconic, backradius, backconic, thickness,
semidiameter; lastmaterial, nextmaterial, frontsurfacereflectance, backsurfacereflectance
)()
else
newelt = SphericalLens(
material, vertex, frontradius, backradius, thickness, semidiameter; lastmaterial,
nextmaterial, frontsurfacereflectance, backsurfacereflectance
)
newelement = SphericalLens(
material, vertices[i-1], frontradius, backradius, thickness, semidiameter;
lastmaterial, nextmaterial, frontsurfacereflectance, backsurfacereflectance
)()
end
elseif surface_type == "Aspheric"
semidiameter = convert(T, max(get_front_back_property(prescription, i, "SemiDiameter", zero(T))...))
frontconic, backconic = get_front_back_property(prescription, i, "Conic", zero(T))
frontaspherics, backaspherics = get_front_back_property(prescription, i, "Parameters")

newelement = AsphericLens(
material, vertices[i-1], frontradius, frontconic, frontaspherics, backradius, backconic,
backaspherics, thickness, semidiameter; lastmaterial, nextmaterial, frontsurfacereflectance,
backsurfacereflectance
)()
else
error(
"Unsupported surface type \"$surface_type\". If you'd like to add support for this surface, ",
"please create an issue at https://github.com/microsoft/OpticSim.jl/issues/new."
)
end

push!(elements, newelt())
if firstelement && semidiameter !== NaN
systemsemidiameter = semidiameter
firstelement = false
end
end

indexofimage = findfirst(isequal("Image"), prescription[!, "SurfaceType"])
imagesize = prescription[indexofimage, "SemiDiameter"]
vertextoimage = convert(T, -sum(prescription[sumstart:(indexofimage - 1), "Thickness"]))
imagerad = prescription[indexofimage, "Radius"]
push!(elements, newelement)
end

# make the detector (Image)
imagesize = prescription[end, "SemiDiameter"]
imagerad = prescription[end, "Radius"]
if imagerad != zero(T) && imagerad != typemax(T)
det = SphericalCap(
abs(imagerad),
NaNsafeasin(imagesize / abs(imagerad)),
imagerad < 0 ? SVector{3,T}(0, 0, 1) : SVector{3,T}(0, 0, -1),
SVector{3,T}(0, 0, vertextoimage),
SVector{3,T}(0, 0, vertices[end-1]),
interface = opaqueinterface(T)
)
else
det = Rectangle(
convert(T, imagesize),
convert(T, imagesize),
SVector{3,T}(0, 0, 1),
SVector{3,T}(0, 0, vertextoimage),
SVector{3,T}(0, 0, vertices[end-1]),
interface = opaqueinterface(T)
)
end

system = CSGOpticalSystem(
OpticSim.LensAssembly(elements...), det, detectorpixelsx, detectorpixelsy, D; temperature, pressure
)

return new{T,typeof(system)}(system, prescription, systemsemidiameter)
end

Expand Down

0 comments on commit 53cf644

Please sign in to comment.