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

Make rotations in degrees exact #197

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
12 changes: 7 additions & 5 deletions src/core_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,18 @@ Base.inv(r::RotMatrix) = RotMatrix(r.mat')
end

"""
struct Angle2d{T} <: Rotation{2,T}
theta::T
struct Angle2d{T,A} <: Rotation{2,T}
theta::A
end

A 2×2 rotation matrix parameterized by a 2D rotation by angle.
Only the angle is stored inside the `Angle2d` type, values
of `getindex` etc. are computed on the fly.
"""
struct Angle2d{T} <: Rotation{2,T}
theta::T
Angle2d{T}(theta) where T = new{T}(theta)
struct Angle2d{T,A} <: Rotation{2,T}
theta::A
Angle2d{T,A}(theta::Number) where{T,A} = new{T,A}(theta)
plut marked this conversation as resolved.
Show resolved Hide resolved
Angle2d{T}(theta::A) where {T,A} = new{T,A}(theta)
end

@inline function Angle2d(theta)
Expand All @@ -171,6 +172,7 @@ params(r::Angle2d) = SVector{1}(r.theta)

Angle2d(r::Rotation{2}) = Angle2d(rotation_angle(r))
Angle2d{T}(r::Rotation{2}) where {T} = Angle2d{T}(rotation_angle(r))
Angle2d{T,A}(r::Rotation{2}) where{T,A} = Angle2d{T,A}(rotation_angle(r))

Base.one(::Type{A}) where {A<: Angle2d} = A(0)

Expand Down
36 changes: 21 additions & 15 deletions src/euler_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
for axis in [:X, :Y, :Z]
RotType = Symbol("Rot" * string(axis))
@eval begin
struct $RotType{T} <: Rotation{3,T}
theta::T
$RotType{T}(theta) where {T} = new{T}(theta)
$RotType{T}(r::$RotType) where {T} = new{T}(r.theta)
struct $RotType{T,A} <: Rotation{3,T}
theta::A
$RotType{T,A}(theta) where{T,A} = new{T,A}(theta)
$RotType{T}(theta::A) where {T,A} = new{T,A}(theta)
$RotType{T,A}(r::$RotType) where{T,A} = new{T,A}(r.theta)
$RotType{T}(r::$RotType) where {T} = $RotType{T}(r.theta)
end

@inline function $RotType(theta)
Expand Down Expand Up @@ -220,11 +222,13 @@ for axis1 in [:X, :Y, :Z]
InvRotType = Symbol("Rot" * string(axis2) * string(axis1))

@eval begin
struct $RotType{T} <: Rotation{3,T}
theta1::T
theta2::T
$RotType{T}(theta1, theta2) where {T} = new{T}(theta1, theta2)
$RotType{T}(r::$RotType) where {T} = new{T}(r.theta1, r.theta2)
struct $RotType{T,A} <: Rotation{3,T}
theta1::A
theta2::A
$RotType{T,A}(theta1,theta2) where{T,A} = new{T,A}(theta1, theta2)
$RotType{T}(theta1::A, theta2::A) where {T,A} = new{T,A}(theta1, theta2)
$RotType{T,A}(r::$RotType) where{T,A} = new{T,A}(r.theta1, r.theta2)
$RotType{T}(r::$RotType) where {T} = $RotType{T}(r.theta1, r.theta2)
end

@inline function $RotType(theta1, theta2)
Expand Down Expand Up @@ -513,12 +517,14 @@ for axis1 in [:X, :Y, :Z]
Rot0Type = Symbol("Rot" * string(axis0))

@eval begin
struct $RotType{T} <: Rotation{3,T}
theta1::T
theta2::T
theta3::T
$RotType{T}(theta1, theta2, theta3) where {T} = new{T}(theta1, theta2, theta3)
$RotType{T}(r::$RotType) where {T} = new{T}(r.theta1, r.theta2, r.theta3)
struct $RotType{T,A} <: Rotation{3,T}
theta1::A
theta2::A
theta3::A
$RotType{T,A}(theta1,theta2,theta3) where{T,A} = new{T,A}(theta1, theta2, theta3)
$RotType{T}(theta1::A, theta2::A, theta3::A) where {T,A} = new{T,A}(theta1, theta2, theta3)
$RotType{T,A}(r::$RotType) where{T,A} = new{T,A}(r.theta1, r.theta2, r.theta3)
$RotType{T}(r::$RotType) where {T} = $RotType{T}(r.theta1, r.theta2, r.theta3)
end

@inline function $RotType(theta1, theta2, theta3)
Expand Down
3 changes: 3 additions & 0 deletions test/2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ using Unitful
# don't extraneously contain those units (see issue #55)
@test eltype(Angle2d(10u"°")) <: Real
@test eltype(Angle2d(20u"rad")) <: Real

# Check exactitude of degree rotations
@test Angle2d(360u"°") == [1 0;0 1]
plut marked this conversation as resolved.
Show resolved Hide resolved
end

###############################
Expand Down
2 changes: 1 addition & 1 deletion test/rotation_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ all_types = (RotMatrix{3}, AngleAxis, RotationVec,
rxyz = RotXYZ(1.0, 2.0, 3.0)
show(io, MIME("text/plain"), rxyz)
str = String(take!(io))
@test startswith(str, "3×3 RotXYZ{Float64}") && occursin("(1.0, 2.0, 3.0):", str)
@test startswith(str, "3×3 RotXYZ{Float64,") && occursin("(1.0, 2.0, 3.0):", str)
end

#########################################################################
Expand Down