From 2b657f34e26a5b1cdbb81c92ea2f2ad0b2549406 Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Thu, 8 Oct 2020 05:03:04 -0700 Subject: [PATCH] Add type casting functions for vector, matrix, and quaternion types --- src/math/matrix-functions.hpp | 26 ++++++++++++++++++++++++++ src/math/quaternion-functions.hpp | 23 +++++++++++++++++++++++ src/math/vector-functions.hpp | 25 +++++++++++++++++++++++++ 3 files changed, 74 insertions(+) diff --git a/src/math/matrix-functions.hpp b/src/math/matrix-functions.hpp index 8b35302..31ed26e 100644 --- a/src/math/matrix-functions.hpp +++ b/src/math/matrix-functions.hpp @@ -308,6 +308,19 @@ matrix transpose(const matrix& m); template matrix transpose(const matrix& m); +/** + * Types casts each matrix element and returns a matrix of the casted type. + * + * @tparam T2 Target matrix element type. + * @tparam T1 Source matrix element type. + * @tparam N Number of columns. + * @tparam M Number of rows. + * @param m Matrix to type cast. + * @return Type-casted matrix. + */ +template +matrix type_cast(const matrix& m); + template matrix add(const matrix& x, const matrix& y) { @@ -841,6 +854,19 @@ matrix transpose(const matrix& m) }}; } +/// @private +template +inline matrix type_cast(const matrix& m, std::index_sequence) +{ + return {type_cast(m[I])...}; +} + +template +matrix type_cast(const matrix& m) +{ + return type_cast(m, std::make_index_sequence{}); +} + /// @} } // namespace math diff --git a/src/math/quaternion-functions.hpp b/src/math/quaternion-functions.hpp index 41565f2..e87f19c 100644 --- a/src/math/quaternion-functions.hpp +++ b/src/math/quaternion-functions.hpp @@ -221,6 +221,17 @@ quaternion sub(const quaternion& x, const quaternion& y); template quaternion quaternion_cast(const matrix& m); +/** + * Types casts each quaternion component and returns a quaternion of the casted type. + * + * @tparam T2 Target quaternion component type. + * @tparam T1 Source quaternion component type. + * @param q Quaternion to type cast. + * @return Type-casted quaternion. + */ +template +quaternion type_cast(const quaternion& q); + template inline quaternion add(const quaternion& x, const quaternion& y) { @@ -459,6 +470,18 @@ quaternion quaternion_cast(const matrix& m) return {r, i.x, i.y, i.z}; } +template +inline quaternion type_cast(const quaternion& q) +{ + return quaternion + { + static_cast(q.w), + static_cast(q.x), + static_cast(q.y), + static_cast(q.z) + }; +} + /// @} } // namespace math diff --git a/src/math/vector-functions.hpp b/src/math/vector-functions.hpp index c939fa9..cea9f1f 100644 --- a/src/math/vector-functions.hpp +++ b/src/math/vector-functions.hpp @@ -305,6 +305,18 @@ vector sub(const vector& x, const vector& y); template vector swizzle(const vector& v); +/** + * Types casts each vector component and returns a vector of the casted type. + * + * @tparam T2 Target vector component type. + * @tparam T1 Source vector component type. + * @tparam N Number of dimensions. + * @param v Vector to type cast. + * @return Type-casted vector. + */ +template +vector type_cast(const vector& v); + /// @private template inline vector add(const vector& x, const vector& y, std::index_sequence) @@ -619,6 +631,19 @@ inline vector swizzle(const vector& v) return { v[Indices]... }; } +/// @private +template +inline vector type_cast(const vector& v, std::index_sequence) +{ + return {static_cast(v[I])...}; +} + +template +inline vector type_cast(const vector& v) +{ + return type_cast(v, std::make_index_sequence{}); +} + /// @} } // namespace math