The shortest rotation taking the direction of from to that of to
(OpenSceneGraph's osg::Quat::makeRotate(from, to)); a half turn about a
perpendicular axis if they are opposite, the identity if either is zero.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=wp), | intent(in) | :: | from(3) |
directions (any length) |
||
| real(kind=wp), | intent(in) | :: | to(3) |
directions (any length) |
pure function quat_from_vectors(from, to) result(q) real(wp), intent(in) :: from(3), to(3) !! directions (any length) real(wp) :: q(4) real(wp), parameter :: eps = 1.0e-12_wp real(wp) :: u(3), v(3), d, axis(3) q = quat_identity if (norm2(from) == 0.0_wp .or. norm2(to) == 0.0_wp) return u = from / norm2(from) v = to / norm2(to) d = dot_product(u, v) if (d >= 1.0_wp - eps) return if (d <= -1.0_wp + eps) then axis = cross(u, [1.0_wp, 0.0_wp, 0.0_wp]) if (norm2(axis) < 1.0e-6_wp) axis = cross(u, [0.0_wp, 1.0_wp, 0.0_wp]) q = [axis / norm2(axis), 0.0_wp] return end if q = quat_normalize([cross(u, v), 1.0_wp + d]) end function quat_from_vectors