Skip to content

Commit

Permalink
Refactor ray tracing materials to dispatch output based on material t…
Browse files Browse the repository at this point in the history
…raits

Add AbstractReturnTrait & AbstractPolarizationTrait for materials that change raytracing output
  • Loading branch information
dominic-chang committed Nov 22, 2024
1 parent 1da85a9 commit 38aabab
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/Krang.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ include("metrics/Kerr/Kerr.jl")
include("cameras/camera_types.jl")
include("cameras/SlowLightIntensityCamera.jl")
include("cameras/IntensityCamera.jl")
include("materials/AbstractMaterialTypes.jl")
include("geometries/geometry_types.jl")
include("geometries/mesh_geometry.jl")
include("geometries/level_set_geometry.jl")
Expand Down
35 changes: 17 additions & 18 deletions src/geometries/geometry_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@ Abstract Geometry
"""
abstract type AbstractGeometry end

"""
$TYPEDEF
Abstract Material
"""
abstract type AbstractMaterial end

#TODO: replace with trait
isOcclusive(material::AbstractMaterial) = false
#TODO: replace with trait
isAxisymmetric(material::AbstractMaterial) = false
#TODO: replace with trait
isFastLight(material::AbstractMaterial) = false

struct Intersection{T}
ts::T
rs::T
Expand All @@ -29,8 +15,6 @@ struct Intersection{T}
νθ::Bool
end

yield(material::AbstractMaterial) = 0

"""
$TYPEDEF
Expand Down Expand Up @@ -59,15 +43,30 @@ struct ConeGeometry{T, A} <: AbstractGeometry
ConeGeometry(opening_angle::T, attributes::A) where {T,A} = new{T, A}(opening_angle,attributes)
end

function raytrace(pix::AbstractPixel{T}, mesh::Mesh{<:ConeGeometry{T,A}, <:AbstractMaterial}) where {T,A}
function raytrace(pixel::AbstractPixel, mesh::Mesh{<:ConeGeometry{T,A}, <:AbstractMaterial}) where {T,A}
return_trait = returnTrait(mesh.material)
return raytrace(return_trait, pixel, mesh)
end

function raytrace(::AbstractReturnTrait, pix::AbstractPixel, mesh::Mesh{<:ConeGeometry{T,A}, <:AbstractMaterial}) where {T,A}
observation = zero(T)
return _raytrace(observation, pix, mesh)
end

function raytrace(::SimplePolarizationTrait, pix::AbstractPixel, mesh::Mesh{<:ConeGeometry{T,A}, <:AbstractMaterial}) where {T,A}
observation = StokesParams(zero(T), zero(T), zero(T), zero(T))
return _raytrace(observation, pix, mesh)
end

function _raytrace(observation, pix::AbstractPixel, mesh::Mesh{<:ConeGeometry{T,A}, <:AbstractMaterial}) where {T,A}
#(;magnetic_field, fluid_velocity, spectral_index, R, p1, p2, subimgs) = linpol

geometry = mesh.geometry
material = mesh.material
θs = geometry.opening_angle
subimgs= material.subimgs

observation = zero(T)#StokesParams(zero(T), zero(T), zero(T), zero(T))
#observation = yield(material)#StokesParams(zero(T), zero(T), zero(T), zero(T))

isindir = false
for _ in 1:2 # Looping over isindir this way is needed to get Metal to work
Expand Down
24 changes: 24 additions & 0 deletions src/materials/AbstractMaterialTypes.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
$TYPEDEF
Abstract Material
"""
abstract type AbstractMaterial end

#TODO: replace with trait
isOcclusive(material::AbstractMaterial) = false
#TODO: replace with trait
isAxisymmetric(material::AbstractMaterial) = false
#TODO: replace with trait
isFastLight(material::AbstractMaterial) = false

yield(material::AbstractMaterial) = 0

abstract type AbstractReturnTrait end
abstract type AbstractPolarizationTrait <: AbstractReturnTrait end
struct SimplePolarizationTrait <: AbstractPolarizationTrait end
struct SimpleIntensityTrait <: AbstractReturnTrait end

function returnTrait(mat::AbstractMaterial)
return SimpleIntensityTrait()
end
4 changes: 4 additions & 0 deletions src/materials/ElectronSynchrotronPowerLawPolarization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ struct ElectronSynchrotronPowerLawPolarization{N, T} <: AbstractMaterial
end
end

function returnTrait(::ElectronSynchrotronPowerLawPolarization{N,T}) where {N,T}
return SimplePolarizationTrait()
end

function nan2zero(x)
return isnan(x) ? zero(eltype(x)) : x
end
Expand Down
19 changes: 15 additions & 4 deletions src/schemes/schemes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@ function render(camera::AbstractCamera, scene::Scene)
return render.(camera.screen.pixels, Ref(scene))
end

function render(pixel::AbstractPixel{T}, scene::Scene) where {T}
function render(pixel::AbstractPixel, scene::Scene)
render(returnTrait(scene[1].material), pixel, scene)
end

function render(::AbstractReturnTrait, pixel::AbstractPixel{T}, scene::Scene) where {T}
return _render(zero(T), pixel, scene)
end

function render(::AbstractPolarizationTrait, pixel::AbstractPixel{T}, scene::Scene) where {T}
return _render(StokesParams(zero(T), zero(T), zero(T), zero(T)), pixel, scene)
end

function _render(observation, pixel::AbstractPixel{T}, scene::Scene) where {T}
mesh = scene[1]
ans = zero(T)#mesh.material(pixel, mesh.geometry)

for itr in 1:length(scene)
mesh = scene[itr]
ans += raytrace(pixel, mesh)
observation += raytrace(pixel, mesh)
end
return ans
return observation
end

@kernel function _render!(store, pixels, mesh::Mesh)
Expand Down

0 comments on commit 38aabab

Please sign in to comment.