Browse Source

Add type casting functions for vector, matrix, and quaternion types

master
C. J. Howard 1 year ago
parent
commit
2b657f34e2
3 changed files with 74 additions and 0 deletions
  1. +26
    -0
      src/math/matrix-functions.hpp
  2. +23
    -0
      src/math/quaternion-functions.hpp
  3. +25
    -0
      src/math/vector-functions.hpp

+ 26
- 0
src/math/matrix-functions.hpp View File

@ -308,6 +308,19 @@ matrix transpose(const matrix& m);
template <class T>
matrix<T, 4, 4> transpose(const matrix<T, 4, 4>& 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 <class T2, class T1, std::size_t N, std::size_t M>
matrix<T2, N, M> type_cast(const matrix<T1, N, M>& m);
template <class T>
matrix<T, 2, 2> add(const matrix<T, 2, 2>& x, const matrix<T, 2, 2>& y)
{
@ -841,6 +854,19 @@ matrix transpose(const matrix& m)
}};
}
/// @private
template <class T2, class T1, std::size_t N, std::size_t M, std::size_t... I>
inline matrix<T2, N, M> type_cast(const matrix<T1, N, M>& m, std::index_sequence<I...>)
{
return {type_cast<T2>(m[I])...};
}
template <class T2, class T1, std::size_t N, std::size_t M>
matrix<T2, N, M> type_cast(const matrix<T1, N, M>& m)
{
return type_cast<T2>(m, std::make_index_sequence<N>{});
}
/// @}
} // namespace math

+ 23
- 0
src/math/quaternion-functions.hpp View File

@ -221,6 +221,17 @@ quaternion sub(const quaternion& x, const quaternion& y);
template <class T>
quaternion<T> quaternion_cast(const matrix<T, 3, 3>& 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 <class T2, class T1>
quaternion<T2> type_cast(const quaternion<T1>& q);
template <class T>
inline quaternion<T> add(const quaternion<T>& x, const quaternion<T>& y)
{
@ -459,6 +470,18 @@ quaternion quaternion_cast(const matrix& m)
return {r, i.x, i.y, i.z};
}
template <class T2, class T1>
inline quaternion<T2> type_cast(const quaternion<T1>& q)
{
return quaternion<T2>
{
static_cast<T2>(q.w),
static_cast<T2>(q.x),
static_cast<T2>(q.y),
static_cast<T2>(q.z)
};
}
/// @}
} // namespace math

+ 25
- 0
src/math/vector-functions.hpp View File

@ -305,6 +305,18 @@ vector sub(const vector& x, const vector& y);
template <std::size_t... Indices, class T, std::size_t N>
vector<T, sizeof...(Indices)> swizzle(const vector<T, N>& 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 <class T2, class T1, std::size_t N>
vector<T2, N> type_cast(const vector<T1, N>& v);
/// @private
template <class T, std::size_t N, std::size_t... I>
inline vector<T, N> add(const vector<T, N>& x, const vector<T, N>& y, std::index_sequence<I...>)
@ -619,6 +631,19 @@ inline vector swizzle(const vector& v)
return { v[Indices]... };
}
/// @private
template <class T2, class T1, std::size_t N, std::size_t... I>
inline vector<T2, N> type_cast(const vector<T1, N>& v, std::index_sequence<I...>)
{
return {static_cast<T2>(v[I])...};
}
template <class T2, class T1, std::size_t N>
inline vector<T2, N> type_cast(const vector<T1, N>& v)
{
return type_cast<T2>(v, std::make_index_sequence<N>{});
}
/// @}
} // namespace math

Loading…
Cancel
Save