qua: distance func

This commit is contained in:
BlackMATov
2021-02-09 19:00:34 +07:00
parent 3d521fd745
commit c15095c098
2 changed files with 13 additions and 0 deletions

View File

@@ -244,6 +244,12 @@ namespace vmath_hpp
return length2(vec{xs});
}
template < typename T >
[[nodiscard]] T distance(const qua<T>& xs, const qua<T>& ys) {
const qua zs = xs * conjugate(ys);
return T(2) * atan2(length(zs.v), abs(zs.s));
}
template < typename T >
[[nodiscard]] constexpr qua<T> normalize(const qua<T>& xs) {
return qua(normalize(vec{xs}));

View File

@@ -154,6 +154,13 @@ TEST_CASE("vmath/qua_fun") {
STATIC_REQUIRE(length2(fqua(10.f,0.f,0.f,0.f)) == uapprox(100.f));
STATIC_REQUIRE(length2(fqua(-10.f,0.f,0.f,0.f)) == uapprox(100.f));
REQUIRE(distance(qrotate_z(radians(0.f)) * 2.f, qrotate_z(radians(0.f)) * 1.5f) == uapprox(radians(0.f)));
REQUIRE(distance(qrotate_z(radians(0.f)) * 3.f, qrotate_z(radians(360.f)) * 2.5f) == uapprox(radians(0.f)));
REQUIRE(distance(qrotate_z(radians(0.f)) * 4.f, qrotate_z(radians(180.f)) * 3.5f) == uapprox(radians(180.f)));
REQUIRE(distance(qrotate_z(radians(180.f)) * 5.f, qrotate_z(radians(0.f)) * 4.5f) == uapprox(radians(180.f)));
REQUIRE(distance(qrotate_z(radians(15.f)) * 6.f, qrotate_z(radians(350.f)) * 5.5f) == uapprox(radians(25.f)));
REQUIRE(distance(qrotate_z(radians(350.f)) * 7.f, qrotate_z(radians(15.f)) * 6.5f) == uapprox(radians(25.f)));
REQUIRE(normalize(fqua(0.5f,0.f,0.f,0.f)).v == uapprox3(1.f,0.f,0.f));
}