Vector rotation using CoordinateTransformations.jl

It looks like vector rotation does not work or I am missing something:

using CoordinateTransformations, Rotations, StaticArrays, AngleBetweenVectors
julia> vec = [1.0,2.0,3.0]
3-element Vector{Float64}:
 1.0
 2.0
 3.0

julia> rotated = LinearMap(RotZ(0))(vec)
3-element SVector{3, Float64} with indices SOneTo(3):
 1.0
 2.0
 3.0

julia> angle(vec, Vector(rotated)) # OK
0.0

julia> rotated = LinearMap(RotZ(2pi))(vec)
3-element SVector{3, Float64} with indices SOneTo(3):
 1.0000000000000004
 1.9999999999999998
 3.0

julia> angle(vec, Vector(rotated)) # OK
1.5700924586837752e-16

julia> rotated = LinearMap(RotZ(0.42))(vec)
3-element SVector{3, Float64} with indices SOneTo(3):
 0.09756803419316795
 2.2339383336841867
 3.0

julia> angle(vec, Vector(rotated)) # Not OK
0.24980625896217093

julia> rotated = LinearMap(RotZ(0.7))(vec)
3-element SVector{3, Float64} with indices SOneTo(3):
 -0.5235931871908935
  2.1739020618066682
  3.0

julia> angle(vec, Vector(rotated)) # Not OK
0.4127652411766173

I.e. rotating by 0 or 2pi results a correct angle but rotation angles in between do not.

Any suggestions?

RotZ is a rotation around the Z-axis. The angle between your vector and the rotated vector may not be the angle rotated if your vector has a Z-component.

To make this obvious, note that a rotation around the Z-axis has no effect on a unit vector pointing in the Z-direction.

julia> v = [1., 0., 0]
3-element Vector{Float64}:
 1.0
 0.0
 0.0

julia> v2 = LinearMap(RotZ(0.42))(v)
3-element SVector{3, Float64} with indices SOneTo(3):
 0.9130889403123083
 0.40776045305957015
 0.0

julia> angle(v, Vector(v2))
0.42

julia> v3 = Float64[0,1,0]
3-element Vector{Float64}:
 0.0
 1.0
 0.0

julia> v4 = LinearMap(RotZ(0.42))(v3)
3-element SVector{3, Float64} with indices SOneTo(3):
 -0.40776045305957015
  0.9130889403123083
  0.0

julia> angle(v3, Vector(v4))
0.42

julia> v5 = Float64[0,0,1]
3-element Vector{Float64}:
 0.0
 0.0
 1.0

julia> v6 = LinearMap(RotZ(0.42))(v5)
3-element SVector{3, Float64} with indices SOneTo(3):
 0.0
 0.0
 1.0

julia> angle(v5, Vector(v6))
0.0
4 Likes

That is of course true, I was somehow thinking in 2D :flushed:. Many thanks your answer!