Math vector class.
This commit is contained in:
parent
93a7796c5a
commit
07225e3aea
2 changed files with 145 additions and 0 deletions
56
src/vector.hpp
Normal file
56
src/vector.hpp
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
#ifndef idid0528646832E04CF08E9785B66CFE0BD1
|
||||||
|
#define idid0528646832E04CF08E9785B66CFE0BD1
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <ciso646>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
namespace cloonel {
|
||||||
|
template <typename T, uint32_t S>
|
||||||
|
class Vector {
|
||||||
|
template <typename U, uint32_t R> friend class Vector;
|
||||||
|
public:
|
||||||
|
Vector ( void ) = default;
|
||||||
|
explicit Vector ( T parValue );
|
||||||
|
template <typename U> Vector ( const Vector<U, S>& parOther );
|
||||||
|
template <typename = std::enable_if<S == 2> > Vector ( T parX, T parY ) : m_mem {parX, parY} {}
|
||||||
|
template <typename = std::enable_if<S == 3> > Vector ( T parX, T parY, T parZ ) : m_mem {parX, parY, parZ} {}
|
||||||
|
template <typename = std::enable_if<S == 4> > Vector ( T parX, T parY, T parZ, T parW ) : m_mem {parX, parY, parZ, parW} {}
|
||||||
|
|
||||||
|
~Vector ( void ) noexcept = default;
|
||||||
|
|
||||||
|
enum {
|
||||||
|
Dimension = S
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename = std::enable_if< 4 >= S> > T& x ( void ) { return m_mem[0]; }
|
||||||
|
template <typename = std::enable_if< 4 >= S> > const T& x ( void ) const { return m_mem[0]; }
|
||||||
|
template <typename = std::enable_if<S >= 2 and 4 >= S> > T& y ( void ) { return m_mem[1]; }
|
||||||
|
template <typename = std::enable_if<S >= 2 and 4 >= S> > const T& y ( void ) const { return m_mem[1]; }
|
||||||
|
template <typename = std::enable_if<S >= 3 and 4 >= S> > T& z ( void ) { return m_mem[2]; }
|
||||||
|
template <typename = std::enable_if<S >= 3 and 4 >= S> > const T& z ( void ) const { return m_mem[2]; }
|
||||||
|
template <typename = std::enable_if<S >= 4 and 4 >= S> > T& w ( void ) { return m_mem[3]; }
|
||||||
|
template <typename = std::enable_if<S >= 4 and 4 >= S> > const T& w ( void ) const { return m_mem[3]; }
|
||||||
|
|
||||||
|
template <typename U> const Vector& operator+= ( const Vector<U, S>& parOther );
|
||||||
|
template <typename U> const Vector& operator-= ( const Vector<U, S>& parOther );
|
||||||
|
template <typename U> const Vector& operator*= ( const Vector<U, S>& parOther );
|
||||||
|
template <typename U> const Vector& operator/= ( const Vector<U, S>& parOther );
|
||||||
|
private:
|
||||||
|
T m_mem[S];
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, typename U, uint32_t S>
|
||||||
|
Vector<typename std::common_type<T, U>::type, S> operator+ ( const Vector<T, S>& parA, const Vector<U, S>& parB ) __attribute__((pure));
|
||||||
|
template <typename T, typename U, uint32_t S>
|
||||||
|
Vector<typename std::common_type<T, U>::type, S> operator- ( const Vector<T, S>& parA, const Vector<U, S>& parB ) __attribute__((pure));
|
||||||
|
template <typename T, typename U, uint32_t S>
|
||||||
|
Vector<typename std::common_type<T, U>::type, S> operator* ( const Vector<T, S>& parA, const Vector<U, S>& parB ) __attribute__((pure));
|
||||||
|
template <typename T, typename U, uint32_t S>
|
||||||
|
Vector<typename std::common_type<T, U>::type, S> operator/ ( const Vector<T, S>& parA, const Vector<U, S>& parB ) __attribute__((pure));
|
||||||
|
|
||||||
|
typedef Vector<float, 2> float2;
|
||||||
|
typedef Vector<uint16_t, 2> ushort2;
|
||||||
|
} //namespace cloonel
|
||||||
|
#include "vector.inl"
|
||||||
|
#endif
|
89
src/vector.inl
Normal file
89
src/vector.inl
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
namespace cloonel {
|
||||||
|
///-------------------------------------------------------------------------
|
||||||
|
///-------------------------------------------------------------------------
|
||||||
|
template <typename T, uint32_t S>
|
||||||
|
Vector<T, S>::Vector (T parValue) :
|
||||||
|
m_mem {parValue}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
///-------------------------------------------------------------------------
|
||||||
|
///-------------------------------------------------------------------------
|
||||||
|
template <typename T, uint32_t S>
|
||||||
|
template <typename U>
|
||||||
|
Vector<T, S>::Vector (const Vector<U, S>& parOther) {
|
||||||
|
for (uint32_t z = 0; z < S; ++z) {
|
||||||
|
m_mem[z] = parOther.m_mem[z];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///-------------------------------------------------------------------------
|
||||||
|
///-------------------------------------------------------------------------
|
||||||
|
template <typename T, uint32_t S>
|
||||||
|
template <typename U>
|
||||||
|
const Vector<T, S>& Vector<T, S>::operator+= (const Vector<U, S>& parOther) {
|
||||||
|
for (uint32_t z = 0; z < S; ++z) {
|
||||||
|
m_mem[z] += parOther.m_mem[z];
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
template <typename T, uint32_t S>
|
||||||
|
template <typename U>
|
||||||
|
const Vector<T, S>& Vector<T, S>::operator-= (const Vector<U, S>& parOther) {
|
||||||
|
for (uint32_t z = 0; z < S; ++z) {
|
||||||
|
m_mem[z] -= parOther.m_mem[z];
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
template <typename T, uint32_t S>
|
||||||
|
template <typename U>
|
||||||
|
const Vector<T, S>& Vector<T, S>::operator*= (const Vector<U, S>& parOther) {
|
||||||
|
for (uint32_t z = 0; z < S; ++z) {
|
||||||
|
m_mem[z] *= parOther.m_mem[z];
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
template <typename T, uint32_t S>
|
||||||
|
template <typename U>
|
||||||
|
const Vector<T, S>& Vector<T, S>::operator/= (const Vector<U, S>& parOther) {
|
||||||
|
for (uint32_t z = 0; z < S; ++z) {
|
||||||
|
m_mem[z] /= parOther.m_mem[z];
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
///-------------------------------------------------------------------------
|
||||||
|
///-------------------------------------------------------------------------
|
||||||
|
template <typename T, typename U, uint32_t S>
|
||||||
|
inline
|
||||||
|
Vector<typename std::common_type<T, U>::type, S> operator+ (const Vector<T, S>& parA, const Vector<U, S>& parB) {
|
||||||
|
typedef typename std::common_type<T, U>::type RetType;
|
||||||
|
Vector<RetType, S> retVal(parA);
|
||||||
|
parA += parB;
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
template <typename T, typename U, uint32_t S>
|
||||||
|
inline
|
||||||
|
Vector<typename std::common_type<T, U>::type, S> operator- (const Vector<T, S>& parA, const Vector<U, S>& parB) {
|
||||||
|
typedef typename std::common_type<T, U>::type RetType;
|
||||||
|
Vector<RetType, S> retVal(parA);
|
||||||
|
parA -= parB;
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
template <typename T, typename U, uint32_t S>
|
||||||
|
inline
|
||||||
|
Vector<typename std::common_type<T, U>::type, S> operator* (const Vector<T, S>& parA, const Vector<U, S>& parB) {
|
||||||
|
typedef typename std::common_type<T, U>::type RetType;
|
||||||
|
Vector<RetType, S> retVal(parA);
|
||||||
|
parA *= parB;
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
template <typename T, typename U, uint32_t S>
|
||||||
|
inline
|
||||||
|
Vector<typename std::common_type<T, U>::type, S> operator/ (const Vector<T, S>& parA, const Vector<U, S>& parB) {
|
||||||
|
typedef typename std::common_type<T, U>::type RetType;
|
||||||
|
Vector<RetType, S> retVal(parA);
|
||||||
|
parA /= parB;
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
} //namespace cloonel
|
Loading…
Reference in a new issue