JuliaGeometry / Rotations.jl

Julia implementations for different rotation parameterizations
https://juliageometry.github.io/Rotations.jl
MIT License
176 stars 44 forks source link

Add new way to represent a rotation matrix (vector rotation) #235

Closed zhy91 closed 2 years ago

zhy91 commented 2 years ago

Introduction

Now, Rotations.jl contains two ways to represent rotation matrices: Euler Angles and Rotating around an axis.

However, vector rotation, i.e., rotating from one vector to another, is sometimes needed. Is it possible to add this way?

Mathematical method

References:

  1. https://www.theochem.ru.nl/~pwormer/Knowino/knowino.org/wiki/Rotation_matrix.html#Vector_rotation
  2. https://math.stackexchange.com/questions/180418/calculate-rotation-matrix-to-align-vector-a-to-vector-b-in-3d/476311#476311

By the way, such rotation has infinite solutions. The above method gives only one of them.

Simple example

This is a simple example written by myself:

function vector_rotate(f::T, t::T) where T<:AbstractVector{Float64}
    v = f × t
    c = f ⋅ t
    u = [   0 -v[3]  v[2];
         v[3]     0 -v[1];
        -v[2]  v[1]     0]
    E = Matrix{Float64}(I, 3, 3)
    R = (c ≈ -1.0) ? (-E) : (E + u + u * u / (1 + c))
    return R
end

Thank you all!

zhy91 commented 2 years ago

Sorry for my stupid issue. :<

I soon realize that u and c in the above example are actually the axis and angle in the way "Rotating around an axis". Therefore, the new way is not necessary.

tamasgal commented 12 months ago

Sorry for digging this out but I just wanted to create a similar issue and now scratching my head how you solved it.

I guess that we can use RotationVecGenerator with f × t as an axis, something like: R = RotationVecGenerator((a × b)...) but how do we pass the angle?

hyrodium commented 12 months ago

If you need to create a rotation from an angle a and its axis [v1, v2, v3], you can use AngleAxis(a, v1, v2, v3). If you need to create a rotation that rotates a vector u to a vector v, you can use rotation_between(u,v).

tamasgal commented 12 months ago

Ah yes, makes sense, thanks!