No worries.
I am not sure what you mean by the different signature. Since Scene
is an alias for Vector{T} where T <: Primitive
, shouldn’t the two be the same? Or is there a difference?
I learnt too late about static vectors, so Vec3 is a homemade type. It’s not too complex and like previously said I already used it without issues, so it shouldn’t pose problems, but if it’s better I could migrate to SVector{3, Float64}
instead.
Here is the Vec3
file, which is loaded in before the rest. Again Float
is an alias for Float64
just to simplify a little bit
struct Vec3 <: Number
x::Float
y::Float
z::Float
end
Vec3(v::Real) = (vf = convert(Float, v); Vec3(vf, vf, vf))
Vec3(v::Vector{Real}) = Vec3(convert(Float, v[1]), convert(Float, v[2]), convert(Float, v[3]))
const ZERO = Vec3(0, 0, 0)
const ORIGIN = ZERO
Base.zero(::Vec3) = ZERO
const I = Vec3(1, 0, 0)
const J = Vec3(0, 1, 0)
const K = Vec3(0, 0, 1)
function Base.getindex(v::Vec3, i::Integer)
i == 1 && return v.x
i == 2 && return v.y
i == 3 && return v.z
throw(BoundsError("cannot access a 3-element vector at position $i"))
end
Base.firstindex(::Vec3) = 1
Base.lastindex(::Vec3) = 3
Base.iterate(v::Vec3, state=1) = state > 3 ? nothing : (v[state], state+1)
Base.IteratorSize(::Type{Vec3}) = HasLength(3)
Base.IteratorEltype(::Type{Vec3}) = HasElType()
Base.eltype(::Type{Vec3}) = Float
Base.length(::Vec3) = 3
Base.:+(v1::Vec3, v2::Vec3) = Vec3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z)
Base.:+(v::Vec3, c::Real) = Vec3(v.x+c, v.y+c, v.z+c)
Base.:+(c::Real, v::Vec3) = v+c
Base.:-(v1::Vec3) = Vec3(-v1.x, -v2.x, -v3.x)
Base.:-(v1::Vec3, v2::Vec3) = Vec3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z)
Base.:-(v::Vec3, c::Real) = Vec3(v.x-c, v.y-c, v.z-c)
Base.:-(c::Real, v::Vec3) = v-c
Base.:*(v1::Vec3, v2::Vec3) = Vec3(v1.x*v2.x, v1.y*v2.y, v1.z*v2.z)
Base.:*(v::Vec3, c::Real) = Vec3(c*v.x, c*v.y, c*v.z)
Base.:*(c::Real, v::Vec3) = v*c
Base.:/(v1::Vec3, v2::Vec3) = Vec3(v1.x/v2.x, v1.y/v2.y, v1.z/v2.z)
Base.:/(v::Vec3, c::Real) = Vec3(v.x/c, v.y/c, v.z/c)
Base.:^(v1::Vec3, v2::Vec3) = Vec3(v1.x^v2.x, v1.y^v2.y, v1.z^v2.z)
Base.:^(v::Vec3, c::Real) = Vec3(v.x^c, v.y^c, v.z^c)
LinearAlgebra.:⋅(v1::Vec3, v2::Vec3) = v1.x*v2.x + v1.y*v2.y + v1.z*v2.z
×(v1::Vec3, v2::Vec3) = Vec3(
v1.y*v2.z - v1.z*v2.y,
v1.z*v2.x - v1.x*v2.z,
v1.x*v2.y - v1.y*v2.x
)
LinearAlgebra.norm(v::Vec3) = sqrt(v⋅v)
normSq(v::Vec3) = v⋅v
LinearAlgebra.normalize(v::Vec3) = v/norm(v)
Base.min(v::Vec3, a::Real) = Vec3(min(v.x, a), min(v.y, a), min(v.z, a))
Base.min(a::Real, v::Vec3) = Vec3(min(v.x, a), min(v.y, a), min(v.z, a))
Base.min(v1::Vec3, v2::Vec3) = Vec3(min(v1.x, v2.x), min(v1.y, v2.y), min(v1.z, v2.z))
Base.max(v::Vec3, a::Real) = Vec3(max(v.x, a), max(v.y, a), max(v.z, a))
Base.max(a::Real, v::Vec3) = Vec3(max(v.x, a), max(v.y, a), max(v.z, a))
Base.max(v1::Vec3, v2::Vec3) = Vec3(max(v1.x, v2.x), max(v1.y, v2.y), max(v1.z, v2.z))
Base.clamp(v::Vec3, a::Real, b::Real) = Vec3(
min(max(v.x, a), b),
min(max(v.y, a), b),
min(max(v.z, a), b)
)
Base.clamp(v::Vec3, a::Vec3, b::Vec3) = Vec3(
min(max(v.x, a.x), b.x),
min(max(v.y, a.y), b.y),
min(max(v.z, a.z), b.z)
)
Base.mod(v::Vec3, a::Real) = Vec3(mod(v.x, a), mod(v.y, a), mod(v.z, a))
Base.mod(a::Real, v::Vec3) = Vec3(mod(v.x, a), mod(v.y, a), mod(v.z, a))
Base.mod(v1::Vec3, v2::Vec3) = Vec3(mod(v1.x, v2.x), mod(v1.y, v2.y), mod(v1.z, v2.z))
Base.abs(v::Vec3) = Vec3(abs(v.x), abs(v.y), abs(v.z))
Also, thank you very much for the help