2013-10-03 23:19:28 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2012 Pavel Krajcevski
|
|
|
|
*
|
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
|
* warranty. In no event will the authors be held liable for any damages
|
|
|
|
* arising from the use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any purpose,
|
|
|
|
* including commercial applications, and to alter it and redistribute it
|
|
|
|
* freely, subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
* claim that you wrote the original software. If you use this software
|
|
|
|
* in a product, an acknowledgment in the product documentation would be
|
|
|
|
* appreciated but is not required.
|
|
|
|
*
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
* misrepresented as being the original software.
|
|
|
|
*
|
|
|
|
* 3. This notice may not be removed or altered from any source
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#ifndef BASE_INCLUDE_VECTORBASE_H_
|
|
|
|
#define BASE_INCLUDE_VECTORBASE_H_
|
|
|
|
|
|
|
|
// !FIXME! For sqrt function. This increases compilation time by a LOT
|
|
|
|
// but I couldn't guarantee any faster general-purpose implementation
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
namespace FasTC {
|
|
|
|
|
2014-02-20 20:49:35 +01:00
|
|
|
enum EVectorType {
|
|
|
|
eVectorType_Scalar,
|
|
|
|
eVectorType_Vector,
|
|
|
|
eVectorType_Matrix
|
|
|
|
};
|
|
|
|
|
2013-10-03 23:19:28 +02:00
|
|
|
template <typename T, const int N>
|
|
|
|
class VectorBase {
|
|
|
|
protected:
|
|
|
|
|
|
|
|
// Vector representation
|
|
|
|
T vec[N];
|
|
|
|
|
|
|
|
public:
|
2013-10-15 06:31:33 +02:00
|
|
|
typedef T ScalarType;
|
2013-10-03 23:19:28 +02:00
|
|
|
|
|
|
|
VectorBase() { }
|
|
|
|
VectorBase(const VectorBase<T, N> &other) {
|
|
|
|
for(int i = 0; i < N; i++) vec[i] = other[i];
|
|
|
|
}
|
|
|
|
|
2014-02-17 00:29:08 +01:00
|
|
|
explicit VectorBase(const T *_vec) {
|
2013-10-03 23:19:28 +02:00
|
|
|
for(int i = 0; i < N; i++) {
|
|
|
|
vec[i] = _vec[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-08 21:06:20 +02:00
|
|
|
static const int Size = N;
|
|
|
|
|
2013-10-03 23:19:28 +02:00
|
|
|
// Accessors
|
|
|
|
T &operator()(int idx) { return vec[idx]; }
|
|
|
|
T &operator[](int idx) { return vec[idx]; }
|
|
|
|
const T &operator()(int idx) const { return vec[idx]; }
|
|
|
|
const T &operator[](int idx) const { return vec[idx]; }
|
|
|
|
|
|
|
|
// Allow casts to the respective array representation...
|
2014-02-16 23:17:25 +01:00
|
|
|
operator const T *() const { return vec; }
|
2013-10-03 23:19:28 +02:00
|
|
|
VectorBase<T, N> &operator=(const T *v) {
|
|
|
|
for(int i = 0; i < N; i++)
|
|
|
|
vec[i] = v[i];
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allows casting to other vector types if the underlying type system does as well...
|
|
|
|
template<typename _T>
|
|
|
|
operator VectorBase<_T, N>() const {
|
2014-02-16 23:17:25 +01:00
|
|
|
VectorBase<_T, N> ret;
|
|
|
|
for(int i = 0; i < N; i++) {
|
|
|
|
ret[i] = static_cast<_T>(vec[i]);
|
|
|
|
}
|
|
|
|
return ret;
|
2013-10-03 23:19:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Vector operations
|
|
|
|
template<typename _T>
|
|
|
|
T Dot(const VectorBase<_T, N> &v) const {
|
|
|
|
T sum = 0;
|
|
|
|
for(int i = 0; i < N; i++)
|
|
|
|
sum += vec[i] * v[i];
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
T LengthSq() const { return this->Dot(*this); }
|
2014-03-12 07:41:23 +01:00
|
|
|
T Length() const { return static_cast<T>(sqrt(LengthSq())); }
|
2014-02-16 18:30:37 +01:00
|
|
|
|
|
|
|
void Normalize() {
|
|
|
|
T len = Length();
|
|
|
|
for(int i = 0; i < N; i++) {
|
|
|
|
vec[i] /= len;
|
|
|
|
}
|
|
|
|
}
|
2013-10-03 23:19:28 +02:00
|
|
|
};
|
2013-10-08 21:06:20 +02:00
|
|
|
|
|
|
|
// Operators
|
|
|
|
template<typename VectorTypeOne, typename VectorTypeTwo>
|
2013-10-09 02:29:56 +02:00
|
|
|
static inline VectorTypeOne VectorAddition(const VectorTypeOne &v1,
|
|
|
|
const VectorTypeTwo &v2) {
|
2014-02-17 19:02:43 +01:00
|
|
|
VectorTypeOne a(v1);
|
2013-10-09 02:29:56 +02:00
|
|
|
for(int i = 0; i < VectorTypeOne::Size; i++) {
|
2014-03-12 07:41:23 +01:00
|
|
|
a(i) += static_cast<VectorTypeOne::ScalarType>(v2[i]);
|
2013-10-09 02:29:56 +02:00
|
|
|
}
|
2013-10-08 21:06:20 +02:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-10-09 02:29:56 +02:00
|
|
|
template<typename VectorTypeOne, typename VectorTypeTwo>
|
|
|
|
static inline VectorTypeOne operator+(const VectorTypeOne &v1,
|
|
|
|
const VectorTypeTwo &v2) {
|
|
|
|
return VectorAddition(v1, v2);
|
|
|
|
}
|
|
|
|
|
2013-10-08 21:06:20 +02:00
|
|
|
template<typename VectorTypeOne, typename VectorTypeTwo>
|
|
|
|
static inline VectorTypeOne &operator+=(VectorTypeOne &v1,
|
|
|
|
const VectorTypeTwo &v2) {
|
2013-10-09 02:29:56 +02:00
|
|
|
return v1 = VectorAddition(v1, v2);
|
2013-10-08 21:06:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename VectorTypeOne, typename VectorTypeTwo>
|
2013-10-09 02:29:56 +02:00
|
|
|
static inline VectorTypeOne VectorSubtraction(const VectorTypeOne &v1,
|
|
|
|
const VectorTypeTwo &v2) {
|
2014-02-17 19:02:43 +01:00
|
|
|
VectorTypeOne a(v1);
|
2013-10-09 02:29:56 +02:00
|
|
|
for(int i = 0; i < VectorTypeOne::Size; i++) {
|
2014-03-12 07:41:23 +01:00
|
|
|
a(i) -= static_cast<VectorTypeOne::ScalarType>(v2[i]);
|
2013-10-09 02:29:56 +02:00
|
|
|
}
|
2013-10-08 21:06:20 +02:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-10-09 02:29:56 +02:00
|
|
|
template<typename VectorTypeOne, typename VectorTypeTwo>
|
|
|
|
static inline VectorTypeOne operator-(const VectorTypeOne &v1,
|
|
|
|
const VectorTypeTwo &v2) {
|
|
|
|
return VectorSubtraction(v1, v2);
|
|
|
|
}
|
|
|
|
|
2013-10-08 21:06:20 +02:00
|
|
|
template<typename VectorTypeOne, typename VectorTypeTwo>
|
|
|
|
static inline VectorTypeOne &operator-=(VectorTypeOne &v1,
|
|
|
|
const VectorTypeTwo &v2) {
|
2013-10-09 02:29:56 +02:00
|
|
|
return v1 = VectorSubtraction(v1, v2);
|
2013-10-08 21:06:20 +02:00
|
|
|
}
|
|
|
|
|
2013-10-09 00:39:32 +02:00
|
|
|
template<typename T>
|
|
|
|
class VectorTraits {
|
|
|
|
public:
|
2014-02-20 20:49:35 +01:00
|
|
|
static const EVectorType kVectorType = eVectorType_Scalar;
|
2013-10-09 00:39:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, const int N>
|
|
|
|
class VectorTraits<VectorBase<T, N> > {
|
|
|
|
public:
|
2014-02-20 20:49:35 +01:00
|
|
|
static const EVectorType kVectorType = eVectorType_Vector;
|
2013-10-09 00:39:32 +02:00
|
|
|
};
|
|
|
|
|
2014-02-20 20:49:35 +01:00
|
|
|
#define REGISTER_VECTOR_TYPE(TYPE) \
|
|
|
|
template<> \
|
|
|
|
class VectorTraits< TYPE > { \
|
|
|
|
public: \
|
|
|
|
static const EVectorType kVectorType = eVectorType_Vector; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define REGISTER_ONE_TEMPLATE_VECTOR_TYPE(TYPE) \
|
|
|
|
template<typename T> \
|
|
|
|
class VectorTraits< TYPE <T> > { \
|
|
|
|
public: \
|
|
|
|
static const EVectorType kVectorType = eVectorType_Vector; \
|
2013-10-08 21:06:20 +02:00
|
|
|
}
|
|
|
|
|
2014-02-20 20:49:35 +01:00
|
|
|
template<typename VectorType, typename ScalarType>
|
|
|
|
static inline VectorType ScalarMultiply(const VectorType &v, const ScalarType &s) {
|
|
|
|
VectorType a(v);
|
|
|
|
for(int i = 0; i < VectorType::Size; i++)
|
|
|
|
a(i) = static_cast<typename VectorType::ScalarType>(a(i) * s);
|
|
|
|
return a;
|
2013-10-09 00:39:32 +02:00
|
|
|
}
|
|
|
|
|
2014-02-20 20:49:35 +01:00
|
|
|
template<
|
|
|
|
EVectorType kVectorTypeOne,
|
|
|
|
EVectorType kVectorTypeTwo,
|
|
|
|
typename TypeOne,
|
|
|
|
typename TypeTwo>
|
|
|
|
class MultSwitch {
|
2013-10-09 00:39:32 +02:00
|
|
|
private:
|
|
|
|
const TypeOne &m_A;
|
|
|
|
const TypeTwo &m_B;
|
|
|
|
public:
|
2014-02-20 20:49:35 +01:00
|
|
|
typedef TypeOne ResultType;
|
2013-10-09 00:39:32 +02:00
|
|
|
|
2014-02-20 20:49:35 +01:00
|
|
|
MultSwitch(const TypeOne &a, const TypeTwo &b)
|
2013-10-09 00:39:32 +02:00
|
|
|
: m_A(a), m_B(b) { }
|
|
|
|
|
2014-02-21 22:11:49 +01:00
|
|
|
ResultType GetMultiplication() const { return m_A * m_B; }
|
2013-10-09 00:39:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename TypeOne, typename TypeTwo>
|
2014-02-20 20:49:35 +01:00
|
|
|
class MultSwitch<
|
|
|
|
eVectorType_Scalar,
|
|
|
|
eVectorType_Vector,
|
|
|
|
TypeOne, TypeTwo> {
|
2013-10-09 00:39:32 +02:00
|
|
|
private:
|
|
|
|
const TypeOne &m_A;
|
|
|
|
const TypeTwo &m_B;
|
|
|
|
|
|
|
|
public:
|
2014-02-20 20:49:35 +01:00
|
|
|
typedef TypeTwo ResultType;
|
2013-10-09 00:39:32 +02:00
|
|
|
|
2014-02-20 20:49:35 +01:00
|
|
|
MultSwitch(const TypeOne &a, const TypeTwo &b)
|
2013-10-09 00:39:32 +02:00
|
|
|
: m_A(a), m_B(b) { }
|
|
|
|
|
2014-02-21 22:11:49 +01:00
|
|
|
ResultType GetMultiplication() const { return ScalarMultiply(m_B, m_A); }
|
2013-10-09 00:39:32 +02:00
|
|
|
};
|
|
|
|
|
2014-02-20 20:49:35 +01:00
|
|
|
template<typename TypeOne, typename TypeTwo>
|
|
|
|
class MultSwitch<
|
|
|
|
eVectorType_Vector,
|
|
|
|
eVectorType_Scalar,
|
|
|
|
TypeOne, TypeTwo> {
|
|
|
|
private:
|
|
|
|
const TypeOne &m_A;
|
|
|
|
const TypeTwo &m_B;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef TypeOne ResultType;
|
|
|
|
|
|
|
|
MultSwitch(const TypeOne &a, const TypeTwo &b)
|
|
|
|
: m_A(a), m_B(b) { }
|
|
|
|
|
2014-02-21 22:11:49 +01:00
|
|
|
ResultType GetMultiplication() const { return ScalarMultiply(m_A, m_B); }
|
2014-02-20 20:49:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename TypeOne, typename TypeTwo>
|
|
|
|
class MultSwitch<
|
|
|
|
eVectorType_Vector,
|
|
|
|
eVectorType_Vector,
|
|
|
|
TypeOne, TypeTwo> {
|
|
|
|
private:
|
|
|
|
const TypeOne &m_A;
|
|
|
|
const TypeTwo &m_B;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef typename TypeOne::ScalarType ResultType;
|
|
|
|
|
|
|
|
MultSwitch(const TypeOne &a, const TypeTwo &b)
|
|
|
|
: m_A(a), m_B(b) { }
|
|
|
|
|
2014-02-21 22:11:49 +01:00
|
|
|
ResultType GetMultiplication() const { return m_A.Dot(m_B); }
|
2014-02-20 20:49:35 +01:00
|
|
|
};
|
2013-10-08 21:06:20 +02:00
|
|
|
|
2013-10-09 00:39:32 +02:00
|
|
|
template<typename TypeOne, typename TypeTwo>
|
|
|
|
static inline
|
2014-02-20 20:49:35 +01:00
|
|
|
typename MultSwitch<
|
|
|
|
VectorTraits<TypeOne>::kVectorType,
|
|
|
|
VectorTraits<TypeTwo>::kVectorType,
|
|
|
|
TypeOne, TypeTwo
|
|
|
|
>::ResultType
|
2013-10-09 00:39:32 +02:00
|
|
|
operator*(const TypeOne &v1, const TypeTwo &v2) {
|
2014-02-20 20:49:35 +01:00
|
|
|
typedef MultSwitch<
|
|
|
|
VectorTraits<TypeOne>::kVectorType,
|
|
|
|
VectorTraits<TypeTwo>::kVectorType,
|
|
|
|
TypeOne, TypeTwo
|
|
|
|
> VSwitch;
|
|
|
|
return VSwitch(v1, v2).GetMultiplication();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename VectorType, typename ScalarType>
|
|
|
|
static inline VectorType &operator*=(VectorType &v, const ScalarType &s) {
|
|
|
|
return v = v * s;
|
2013-10-08 21:06:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename VectorType, typename ScalarType>
|
2013-10-09 00:39:32 +02:00
|
|
|
static inline VectorType ScalarDivide(const VectorType &v, const ScalarType &s) {
|
2014-02-17 19:02:43 +01:00
|
|
|
VectorType a(v);
|
2013-10-08 21:06:20 +02:00
|
|
|
for(int i = 0; i < VectorType::Size; i++)
|
2014-02-20 01:36:28 +01:00
|
|
|
a(i) = static_cast<typename VectorType::ScalarType>(a(i) / s);
|
2013-10-08 21:06:20 +02:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2013-10-09 00:39:32 +02:00
|
|
|
template<typename TypeOne, typename TypeTwo>
|
2014-02-20 20:49:35 +01:00
|
|
|
static inline TypeOne operator/(const TypeOne &v1, const TypeTwo &v2) {
|
|
|
|
return ScalarDivide(v1, v2);
|
2013-10-08 21:06:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename VectorType, typename ScalarType>
|
|
|
|
static inline VectorType &operator/=(VectorType &v, const ScalarType &s) {
|
2013-10-09 02:29:56 +02:00
|
|
|
return v = ScalarDivide(v, s);
|
2013-10-08 21:06:20 +02:00
|
|
|
}
|
2013-10-03 23:19:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // BASE_INCLUDE_VECTORBASE_H_
|