|
| 1 | + |
| 2 | +import Quaternions.argq |
| 3 | + |
| 4 | +# creating random examples |
| 5 | +sample{T <: Integer}(QT::Type{Quaternion{T}}) = QT(rand(-100:100,4)..., false) |
| 6 | +sample{T <: AbstractFloat}(QT::Type{Quaternion{T}}) = QT(rand(Bool)? quatrand() : nquatrand()) |
| 7 | +sample{T <: Integer}(CT::Type{Complex{T}}) = CT(rand(-100:100,2)...) |
| 8 | +sample{T <: AbstractFloat}(CT::Type{Complex{T}}) = CT(randn(2)...) |
| 9 | +sample(T, n) = T[sample(T) for _ in 1:n] |
| 10 | + |
| 11 | +# test algebraic properties of quaternions |
| 12 | +for _ in 1:10, T in (Float32, Float64, Int32, Int64) |
| 13 | + q,q1,q2,q3 = sample(Quaternion{T}, 4) |
| 14 | + |
| 15 | + # skewfield |
| 16 | + test_group(q1,q2,q3, +, zero(q), -) |
| 17 | + test_group(q1,q2,q3, *, one(q), inv) |
| 18 | + test_multiplicative(q1,q2,*,norm) |
| 19 | + |
| 20 | + # complex embedding |
| 21 | + c1, c2 = sample(Complex{T}, 2) |
| 22 | + test_multiplicative(c1,c2,*, Quaternion) |
| 23 | + test_multiplicative(c1,c2,+, Quaternion) |
| 24 | +end |
| 25 | + |
| 26 | +let # test rotations |
| 27 | + qx = qrotation([1,0,0], pi/4) |
| 28 | + @test_approx_eq qx*qx qrotation([1,0,0], pi/2) |
| 29 | + @test_approx_eq qx^2 qrotation([1,0,0], pi/2) |
| 30 | + theta = pi/8 |
| 31 | + qx = qrotation([1,0,0], theta) |
| 32 | + c = cos(theta); s = sin(theta) |
| 33 | + Rx = [1 0 0; 0 c -s; 0 s c] |
| 34 | + @test_approx_eq rotationmatrix(qx) Rx |
| 35 | + theta = pi/6 |
| 36 | + qy = qrotation([0,1,0], theta) |
| 37 | + c = cos(theta); s = sin(theta) |
| 38 | + Ry = [c 0 s; 0 1 0; -s 0 c] |
| 39 | + @test_approx_eq rotationmatrix(qy) Ry |
| 40 | + theta = 4pi/3 |
| 41 | + qz = qrotation([0,0,1], theta) |
| 42 | + c = cos(theta); s = sin(theta) |
| 43 | + Rz = [c -s 0; s c 0; 0 0 1] |
| 44 | + @test_approx_eq rotationmatrix(qz) Rz |
| 45 | + |
| 46 | + @test_approx_eq rotationmatrix(qx*qy*qz) Rx*Ry*Rz |
| 47 | + @test_approx_eq rotationmatrix(qy*qx*qz) Ry*Rx*Rz |
| 48 | + @test_approx_eq rotationmatrix(qz*qx*qy) Rz*Rx*Ry |
| 49 | + |
| 50 | + a, b = qrotation([0,0,1], deg2rad(0)), qrotation([0,0,1], deg2rad(180)) |
| 51 | + @test_approx_eq slerp(a,b,0.0) a |
| 52 | + @test_approx_eq slerp(a,b,1.0) b |
| 53 | + @test_approx_eq slerp(a,b,0.5) qrotation([0,0,1], deg2rad(90)) |
| 54 | + |
| 55 | + @test_approx_eq angle(qrotation([1,0,0], 0)) 0 |
| 56 | + @test_approx_eq angle(qrotation([0,1,0], pi/4)) pi/4 |
| 57 | + @test_approx_eq angle(qrotation([0,0,1], pi/2)) pi/2 |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + let # test numerical stability of angle |
| 62 | + ax = randn(3) |
| 63 | + for θ in [1e-9, π - 1e-9] |
| 64 | + q = qrotation(ax, θ) |
| 65 | + @test_approx_eq angle(q) θ |
| 66 | + end |
| 67 | + end |
| 68 | +end |
| 69 | + |
| 70 | +for _ in 1:100 |
| 71 | + let # test specialfunctions |
| 72 | + c = Complex(randn(2)...) |
| 73 | + q,q2 = sample(Quaternion{Float64}, 4) |
| 74 | + unary_funs = [exp, log, sin, cos, sqrt, inv, conj, abs2, norm] |
| 75 | + # since every quaternion is conjugate to a complex number, |
| 76 | + # one can establish correctness as follows: |
| 77 | + for fun in unary_funs |
| 78 | + @test fun(Quaternion(c)) ≈ Quaternion(fun(c)) |
| 79 | + @test q2*fun(q)*inv(q2) ≈ fun(q2*q*inv(q2)) |
| 80 | + end |
| 81 | + |
| 82 | + @test exp(log(q)) ≈ q |
| 83 | + @test exp(zero(q)) ≈ one(q) |
| 84 | + end |
| 85 | + |
| 86 | + let # test qrotation and angleaxis inverse |
| 87 | + ax = randn(3); ax = ax/norm(ax) |
| 88 | + Θ = π * rand() |
| 89 | + q = qrotation(ax, Θ) |
| 90 | + @test angle(q) ≈ Θ |
| 91 | + @test axis(q) ≈ ax |
| 92 | + @test angleaxis(q)[1] ≈ Θ |
| 93 | + @test angleaxis(q)[2] ≈ ax |
| 94 | + end |
| 95 | + |
| 96 | + let # test argq |
| 97 | + q,q2 = sample(Quaternion{Float64}, 2) |
| 98 | + @test q2*argq(q)*inv(q2) ≈ argq(q2*q*inv(q2)) |
| 99 | + v = Quaternion(0, randn(3)...) |
| 100 | + @test argq(v)*norm(v) ≈ v |
| 101 | + end |
| 102 | + |
| 103 | + let # test normalize |
| 104 | + q = quatrand() |
| 105 | + @test norm(normalize(q)) ≈ 1 |
| 106 | + @test normalize(q).norm |
| 107 | + @test q ≈ norm(q) * normalize(q) |
| 108 | + qn = nquatrand() |
| 109 | + @test qn.norm |
| 110 | + @test normalize(qn) === qn |
| 111 | + end |
| 112 | + |
| 113 | + let # test slerp and linpol if q1 = 1 |
| 114 | + q1 = quat(1,0,0,0.) |
| 115 | + # there are numerical stability issues with slerp atm |
| 116 | + θ = clamp(rand() * 3.5, deg2rad(5e-1) ,π) |
| 117 | + ax = randn(3) |
| 118 | + q2 = qrotation(ax, θ) |
| 119 | + t = rand() |
| 120 | + slerp(q1, q2, 0.) ≈ q1 |
| 121 | + @test slerp(q1, q2, 0.) ≈ q1 |
| 122 | + @test slerp(q1, q2, 1.) ≈ q2 |
| 123 | + @test slerp(q1, q2, t) ≈ qrotation(ax, t*θ) |
| 124 | + @test norm(slerp(q1, q2, t)) ≈ 1 |
| 125 | + @test slerp(q1, q2, 0.5) ≈ qrotation(ax, 0.5*θ) |
| 126 | + @test linpol(q1, q2, 0.5) ≈ qrotation(ax, 0.5*θ) |
| 127 | + |
| 128 | + end |
| 129 | + let # test conjugation invariance |
| 130 | + q, q1, q2 = sample(Quaternion{Float64}, 3) |
| 131 | + ⊗(s, t) = s*t*inv(s) |
| 132 | + t = rand() |
| 133 | + @test q ⊗ slerp(q1, q2, t) ≈ slerp(q ⊗ q1, q ⊗ q2, t) |
| 134 | + @test q ⊗ linpol(q1, q2, t) ≈ linpol(q ⊗ q1, q ⊗ q2, t) |
| 135 | + end |
| 136 | +end |
0 commit comments