From 7b8139c100a16cb1d8322fa1b68dd343b410a474 Mon Sep 17 00:00:00 2001 From: Claudius 'keldu' Holeksa Date: Wed, 9 Oct 2024 15:10:50 +0200 Subject: Minor work on math stuff --- modules/codec/c++/data_math.hpp | 51 ++++++++++++++++++++++++++++++++++++++- modules/codec/c++/math.hpp | 12 +++++++++ modules/codec/c++/schema_math.hpp | 8 ++++-- 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 modules/codec/c++/math.hpp diff --git a/modules/codec/c++/data_math.hpp b/modules/codec/c++/data_math.hpp index 3bda481..a9eafff 100644 --- a/modules/codec/c++/data_math.hpp +++ b/modules/codec/c++/data_math.hpp @@ -2,15 +2,64 @@ #include "schema_math.hpp" +#include + namespace saw { template class data,encode::Native> { +public: + using Schema = schema::Quaternion; private: - std::array, 4u> vals_; + data,encode::Native> vals_; + + using NativeType = typename native_data_type::type; public: + data() = default; + + data(data i__, data j__, data k__, data w__): + vals_{i__,j__,k__,w__} + {} + + constexpr void normalize() { + data norm{}; + + for(data i = 0u; i < vals_.size(); ++i){ + norm += vals_.at(i) * vals_.at(i); + } + norm.set(std::sqrt(norm.get())); + for(data i = 0u; i < vals_.size(); ++i){ + vals_.at(i) = vals_.at(i) / norm; + } + } + constexpr data& at(data i){ + return vals_.at(i); + } + constexpr const data& at(data i) const { + return vals_.at(i); + } + + constexpr data operator*(const data& rhs) const { + data mult; + + data i_i{0u}; + data j_i{1u}; + data k_i{2u}; + data w_i{3u}; + + mult.at(i_i) = at(w_i) * rhs.at(i_i) + at(i_i) * rhs.at(w_i) + at(j_i) * rhs.at(k_i) - at(k_i) * rhs.at(j_i); + + mult.at(j_i) = at(w_i) * rhs.at(j_i) + at(j_i) * rhs.at(w_i) - at(i_i) * rhs.at(k_i) + at(k_i) * rhs.at(i_i); + + mult.at(k_i) = at(w_i) * rhs.at(k_i) + at(k_i) * rhs.at(w_i) + at(i_i) * rhs.at(j_i) - at(j_i) * rhs.at(i_i); + + mult.at(w_i) = at(w_i) * rhs.at(w_i) - at(i_i) * rhs.at(i_i) - at(j_i) * rhs.at(j_i) - at(k_i) * rhs.at(k_i); + + return mult; + } }; + } diff --git a/modules/codec/c++/math.hpp b/modules/codec/c++/math.hpp new file mode 100644 index 0000000..dd9e49b --- /dev/null +++ b/modules/codec/c++/math.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include "data_math.hpp" + +namespace saw { +namespace math { +template +data norm_2(const data& d){ + return {}; +} +} +} diff --git a/modules/codec/c++/schema_math.hpp b/modules/codec/c++/schema_math.hpp index 471409f..203b88a 100644 --- a/modules/codec/c++/schema_math.hpp +++ b/modules/codec/c++/schema_math.hpp @@ -8,9 +8,13 @@ template struct Complex {}; template -struct Quaternion {}; +struct Quaternion { + static_assert(is_primitive::value, "Quaternions assume a primitive type"); +}; template -struct Euler {}; +struct Euler { + static_assert(is_primitive::value, "Quaternions assume a primitive type"); +}; } } -- cgit v1.2.3