utility: generic vector
Changed old 2d vector template to a generic vector class that can have any amount of dimensions. Added implementations for 2d and 4d and typedefs for int and float. Updated the unit tests accordingly fortune cookie message = pray for what you want but work for the things you need
This commit is contained in:
+73
-163
@@ -1,69 +1,64 @@
|
||||
#ifndef VECTOR_2_H
|
||||
#define VECTOR_2_H
|
||||
|
||||
#include <cmath>
|
||||
#include <ostream>
|
||||
#include "utility/logging/logging.h"
|
||||
#include "utility/Property.h"
|
||||
|
||||
#include "VectorBase.h"
|
||||
|
||||
template<class T>
|
||||
class Vector2
|
||||
class Vector2 : public VectorBase<T, 2>
|
||||
{
|
||||
public:
|
||||
Vector2();
|
||||
Vector2(const T x, const T y);
|
||||
Vector2(const Vector2<T>& vector);
|
||||
~Vector2();
|
||||
Vector2(const T& x, const T& y);
|
||||
Vector2(const VectorBase<T, 2>& vector);
|
||||
virtual ~Vector2();
|
||||
|
||||
float getLengthSquared() const;
|
||||
float getLength() const;
|
||||
T getValue(const unsigned int index) const;
|
||||
void setValue(const unsigned int index, const T& value);
|
||||
|
||||
T& operator[](const unsigned int index);
|
||||
|
||||
Property<T> x;
|
||||
Property<T> y;
|
||||
|
||||
Vector2<T> normalize();
|
||||
Vector2<T> normalized() const;
|
||||
|
||||
// checks whether all values are the same
|
||||
bool isEqual(const Vector2<T>& other) const;
|
||||
// checks if the memory address is the same
|
||||
bool isSame(const Vector2<T>& other) const;
|
||||
template<class U>
|
||||
void operator=(Vector2<U>& other);
|
||||
|
||||
void operator=(Vector2<T>& other);
|
||||
|
||||
Vector2<T> operator+(const Vector2<T>& other) const;
|
||||
Vector2<T> operator-(const Vector2<T>& other) const;
|
||||
Vector2<T> operator*(const T scalar) const;
|
||||
T operator*(const Vector2<T>& other) const;
|
||||
Vector2<T> operator/(const T scalar) const;
|
||||
|
||||
Vector2<T> operator+=(const Vector2<T>& other);
|
||||
Vector2<T> operator-=(const Vector2<T>& other);
|
||||
Vector2<T> operator*=(const T scalar);
|
||||
Vector2<T> operator/=(const T scalar);
|
||||
|
||||
// checks whether all values are the same
|
||||
bool operator==(const Vector2<T>& other) const;
|
||||
// checks whether at least one value is different
|
||||
bool operator!=(const Vector2<T>& other) const;
|
||||
|
||||
T x;
|
||||
T y;
|
||||
protected:
|
||||
static const unsigned int m_xIndex = 0;
|
||||
static const unsigned int m_yIndex = 1;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
Vector2<T>::Vector2()
|
||||
: x(0)
|
||||
, y(0)
|
||||
: VectorBase<T, 2>()
|
||||
, x(&m_values[m_xIndex])
|
||||
, y(&m_values[m_yIndex])
|
||||
{
|
||||
setValue(m_xIndex, 0);
|
||||
setValue(m_yIndex, 0);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector2<T>::Vector2(const T x, const T y)
|
||||
: x(x)
|
||||
, y(y)
|
||||
Vector2<T>::Vector2(const T& x, const T& y)
|
||||
: VectorBase<T, 2>()
|
||||
, x(&m_values[m_xIndex])
|
||||
, y(&m_values[m_yIndex])
|
||||
{
|
||||
setValue(m_xIndex, x);
|
||||
setValue(m_yIndex, y);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector2<T>::Vector2(const Vector2<T> &vector)
|
||||
: x(vector.x)
|
||||
, y(vector.y)
|
||||
Vector2<T>::Vector2(const VectorBase<T, 2>& vector)
|
||||
: VectorBase<T, 2>(vector)
|
||||
, x(&m_values[m_xIndex])
|
||||
, y(&m_values[m_yIndex])
|
||||
{
|
||||
}
|
||||
|
||||
@@ -73,148 +68,63 @@ Vector2<T>::~Vector2()
|
||||
}
|
||||
|
||||
template<class T>
|
||||
float Vector2<T>::getLengthSquared() const
|
||||
T Vector2<T>::getValue(const unsigned int index) const
|
||||
{
|
||||
return static_cast<float>(x * x) + static_cast<float>(y * y);
|
||||
try
|
||||
{
|
||||
return VectorBase<T, 2>::getValue(index);
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
LOG_ERROR(e.what());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
float Vector2<T>::getLength() const
|
||||
void Vector2<T>::setValue(const unsigned int index, const T& value)
|
||||
{
|
||||
return std::sqrt(getLengthSquared());
|
||||
try
|
||||
{
|
||||
VectorBase<T, 2>::setValue(index, value);
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
LOG_ERROR(e.what());
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T& Vector2<T>::operator[](const unsigned int index)
|
||||
{
|
||||
try
|
||||
{
|
||||
return VectorBase<T, 2>::[index];
|
||||
}
|
||||
catch(std::exception& e)
|
||||
{
|
||||
LOG_ERROR(e.what());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector2<T> Vector2<T>::normalize()
|
||||
{
|
||||
float length = getLength();
|
||||
|
||||
x /= length;
|
||||
y /= length;
|
||||
|
||||
return *this;
|
||||
return VectorBase<T, 2>::normalize();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector2<T> Vector2<T>::normalized() const
|
||||
{
|
||||
float length = getLength();
|
||||
|
||||
return Vector2<T>(static_cast<float>(x) / length, static_cast<float>(y) / length);
|
||||
return VectorBase<T, 2>::normalized();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool Vector2<T>::isEqual(const Vector2<T>& other) const
|
||||
template<class U>
|
||||
void Vector2<T>::operator=(Vector2<U>& other)
|
||||
{
|
||||
return ((x == other.x) && (y == other.y));
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool Vector2<T>::isSame(const Vector2<T>& other) const
|
||||
{
|
||||
return &other == this;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
void Vector2<T>::operator=(Vector2<T>& other)
|
||||
{
|
||||
if (isSame(other) || isEqual(other))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
x = other.x;
|
||||
y = other.y;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector2<T> Vector2<T>::operator+(const Vector2<T>& other) const
|
||||
{
|
||||
Vector2<T> result(*this);
|
||||
return (result += other);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector2<T> Vector2<T>::operator-(const Vector2<T>& other) const
|
||||
{
|
||||
Vector2<T> result(*this);
|
||||
return (result -= other);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector2<T> Vector2<T>::operator*(const T scalar) const
|
||||
{
|
||||
Vector2<T> result(*this);
|
||||
return (result *= scalar);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T Vector2<T>::operator*(const Vector2<T>& other) const
|
||||
{
|
||||
return x * other.x + y * other.y;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector2<T> Vector2<T>::operator/(const T scalar) const
|
||||
{
|
||||
Vector2<T> result(*this);
|
||||
return (result /= scalar);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector2<T> Vector2<T>::operator+=(const Vector2<T>& other)
|
||||
{
|
||||
x += other.x;
|
||||
y += other.y;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector2<T> Vector2<T>::operator-=(const Vector2<T>& other)
|
||||
{
|
||||
x -= other.x;
|
||||
y -= other.y;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector2<T> Vector2<T>::operator*=(const T scalar)
|
||||
{
|
||||
x *= scalar;
|
||||
y *= scalar;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
Vector2<T> Vector2<T>::operator/=(const T scalar)
|
||||
{
|
||||
x /= scalar;
|
||||
y /= scalar;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool Vector2<T>::operator==(const Vector2<T>& other) const
|
||||
{
|
||||
return isEqual(other);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool Vector2<T>::operator!=(const Vector2<T>& other) const
|
||||
{
|
||||
return !isEqual(other);
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
std::ostream& operator<<(std::ostream& ostream, const Vector2<T>& vector)
|
||||
{
|
||||
ostream << "[" << vector.x << ", " << vector.y << "]";
|
||||
return ostream;
|
||||
this->assign(other);
|
||||
}
|
||||
|
||||
typedef Vector2<float> Vec2f;
|
||||
|
||||
Reference in New Issue
Block a user