From 4a8c69e4f4b1d814803bcf61da0defc634cbc737 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Tue, 26 Jan 2021 01:57:39 +0700 Subject: [PATCH] qua: dot, length, length2, normalize --- headers/vmath.hpp/vmath_qua_fun.hpp | 24 ++++++++++++++++++++++++ untests/vmath_qua_fun_tests.cpp | 12 ++++++++++++ 2 files changed, 36 insertions(+) diff --git a/headers/vmath.hpp/vmath_qua_fun.hpp b/headers/vmath.hpp/vmath_qua_fun.hpp index 4a09b99..41149aa 100644 --- a/headers/vmath.hpp/vmath_qua_fun.hpp +++ b/headers/vmath.hpp/vmath_qua_fun.hpp @@ -167,5 +167,29 @@ namespace vmath_hpp } } +// +// Geometric Functions +// + +namespace vmath_hpp +{ + template < typename T > + [[nodiscard]] constexpr T dot(const qua& xs, const qua& ys) { + return dot(vec{xs}, vec{ys}); + } + + template < typename T > + [[nodiscard]] T length(const qua& xs) { + return length(vec{xs}); + } + + template < typename T > + [[nodiscard]] constexpr T length2(const qua& xs) { + return length2(vec{xs}); + } + + template < typename T > + [[nodiscard]] constexpr qua normalize(const qua& xs) { + return qua(normalize(vec{xs})); } } diff --git a/untests/vmath_qua_fun_tests.cpp b/untests/vmath_qua_fun_tests.cpp index d4b2737..2b085de 100644 --- a/untests/vmath_qua_fun_tests.cpp +++ b/untests/vmath_qua_fun_tests.cpp @@ -69,4 +69,16 @@ TEST_CASE("vmath/qua_fun") { REQUIRE_FALSE(any(isinf(fqua(1,1,1,1)))); REQUIRE(all(isfinite(fqua(1,1,1,1)))); } + + SUBCASE("Geometric Functions") { + STATIC_REQUIRE(dot(qua(1,2,3,4),qua(3,4,5,6)) == 50); + + REQUIRE(length(fqua(10.f,0.f,0.f,0.f)) == uapprox(10.f)); + REQUIRE(length(fqua(-10.f,0.f,0.f,0.f)) == uapprox(10.f)); + + 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(normalize(fqua(0.5f,0.f,0.f,0.f)).v == uapprox3(1.f,0.f,0.f)); + } }