qua: qrotate(from, to)

This commit is contained in:
BlackMATov
2021-02-10 17:52:53 +07:00
parent 7c79e05c89
commit 914ef69521
2 changed files with 80 additions and 16 deletions

View File

@@ -942,6 +942,33 @@ namespace vmath_hpp
}
}
template < typename T >
[[nodiscard]] qua<T> qrotate(const qua<T>& q, const mat<T, 3>& m) {
return q * qrotate(m);
}
template < typename T >
[[nodiscard]] qua<T> qrotate(const vec<T, 3>& from, const vec<T, 3>& to) {
/// REFERENCE:
/// http://lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final
const T n = sqrt(length2(from) * length2(to));
const T s = dot(from, to) + n;
if ( s < T(0.000001) * n ) {
return abs(from.x) > abs(from.z)
? normalize(qua{vec{-from.y, from.x, T(0)}, T(0)})
: normalize(qua{vec{T(0), -from.z, from.y}, T(0)});
}
return normalize(qua{cross(from, to), s});
}
template < typename T >
[[nodiscard]] qua<T> qrotate(const qua<T>& q, const vec<T, 3>& from, const vec<T, 3>& to) {
return q * qrotate(from, to);
}
template < typename T >
[[nodiscard]] qua<T> qrotate(T angle, const vec<T, 3>& axis) {
/// REFERENCE: