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

BrianGun/issue239 #240

Merged
merged 4 commits into from
Jul 20, 2021
Merged
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
44 changes: 5 additions & 39 deletions src/Geometry/Transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ using LinearAlgebra
`Vec3` defines a series of convenience constructors, so you can just type e.g. `Vec3(1, 2, 3)` or `Vec3([1.0, 2.0, 3.0])`.
It also supports comprehensions, and the `zeros()`, `ones()`, `fill()`, `rand()` and `randn()` functions, such as `Vec3(rand(3))`.
"""
struct Vec3{T} <: FieldVector{3, T}
_x::T
_y::T
_z::T
end
Vec3{T} = SVector{3,T}
export Vec3

# empty constructor - initialized with zeros
Expand Down Expand Up @@ -52,26 +48,11 @@ export unitX3, unitY3, unitZ3
`Vec4` defines a series of convenience constructors, so you can just type e.g. `Vec3(1, 2, 3, 4)` or `Vec3([1.0, 2.0, 3.0, 4.0])`.
It also supports comprehensions, and the `zeros()`, `ones()`, `fill()`, `rand()` and `randn()` functions, such as `Vec4(rand(4))`.
"""
struct Vec4{T} <: FieldVector{4, T}
_x::T
_y::T
_z::T
_w::T
end
export Vec4
Vec4{T} = SVector{4,T}

# empty constructor - initialized with zeros
Vec4(::Type{T} = Float64) where {T<:Real} = zeros(Vec4{T})

# convert vec3 to vec4
"""
Vec4(v::Vec3{T}) -> Vec4

Accept `Vec3` and create a `Vec4` type [v[1], v[2], v[3], 1]
"""
function Vec4(v::Vec3{T}) where {T<:Real}
return Vec4{T}(v[1], v[2], v[3], one(T))
end
export Vec4

"""
Vec4(v::SVector{3, T}) where {T<:Real} -> Vec4{T}
Expand Down Expand Up @@ -445,15 +426,6 @@ function world2local(t::Transform{T}) where {T<:Real}
end
export world2local

function Base.:*(t::Transform{T}, v::Vec3{T}) where {T<:Real}
res = t * Vec4(v)
if (t[4,4] == one(T))
return Vec3(res[1], res[2], res[3])
else
return Vec3(res[1]/res[4], res[2]/res[4], res[3]/res[4])
end
end

function Base.:*(t::Transform{T}, v::SVector{3,T}) where {T<:Real}
res = t * Vec4(v)
if (t[4,4] == one(T))
Expand Down Expand Up @@ -500,21 +472,15 @@ export decomposeRTS

Returns the rotation matrix of type `S` (default `Float64`) representing the rotation between vetors `a` and `b`, i.e. rotation(a,b) * a = b.
"""
rotmatbetween(a::Vec3{T}, b::Vec3{T}) where {T<:Real} = rotmatbetween(Float64, a, b)
function rotmatbetween(::Type{S}, a::Vec3{T}, b::Vec3{T}) where {T<:Real,S<:Real}
rotmatbetween(a::SVector{3,T}, b::SVector{3,T}) where {T<:Real} = rotmatbetween(Float64, a, b)
function rotmatbetween(::Type{S}, a::SVector{3,T}, b::SVector{3,T}) where {T<:Real,S<:Real}
# TODO: Brian, is there a hidden assumption that a and b are normalized?
v = cross(a, b)
c = dot(a, b)
V = SMatrix{3,3,S,9}(0, v[3], -v[2], -v[3], 0, v[1], v[2], -v[1], 0)
R = I + V + V^2 * one(T) / (one(T) + c)
return SMatrix{3,3,S,9}(R)
end
rotmatbetween(a::SVector{3,T}, b::SVector{3,T}) where {T<:Real} = rotmatbetween(Float64, Vec3(a), Vec3(b))
function rotmatbetween(type::Type{S}, a::SVector{3,T}, b::SVector{3,T}) where {T<:Real,S<:Real}
return rotmatbetween(type, Vec3(a), Vec3(b))
end
export rotmatbetween


"""
rotmatd([S::Type], θ::T, ϕ::T, ψ::T) -> SMatrix{3,3,S}
Expand Down