149 lines
3.1 KiB
C++
149 lines
3.1 KiB
C++
#ifndef _HEADER_TEA_MATH_VECTOR3
|
|
#define _HEADER_TEA_MATH_VECTOR3
|
|
|
|
namespace TeaLib
|
|
{
|
|
namespace Math
|
|
{
|
|
template<typename T>
|
|
class Vector3
|
|
{
|
|
public:
|
|
Vector3() : x(0), y(0), z(0){};
|
|
Vector3(const T a_X, const T a_Y, const T a_Z) : x(a_X), y(a_Y), z(a_Z){};
|
|
|
|
Vector3& operator+=(const Vector3& rhs)
|
|
{
|
|
this->x += rhs.x;
|
|
this->y += rhs.y;
|
|
this->z += rhs.z;
|
|
return *this;
|
|
}
|
|
Vector3& operator-=(const Vector3& rhs)
|
|
{
|
|
this->x -= rhs.x;
|
|
this->y -= rhs.y;
|
|
this->z -= rhs.z;
|
|
return *this;
|
|
}
|
|
Vector3& operator*=(const float& rhs)
|
|
{
|
|
this->x *= rhs;
|
|
this->y *= rhs;
|
|
this->z *= rhs;
|
|
return *this;
|
|
}
|
|
Vector3& operator/=(const float& rhs)
|
|
{
|
|
this->x /= rhs;
|
|
this->y /= rhs;
|
|
this->z /= rhs;
|
|
return *this;
|
|
}
|
|
|
|
Vector3 operator+(const Vector3& rhs)
|
|
{
|
|
Vector3 retVal;
|
|
retVal.x = this->x + rhs.x;
|
|
retVal.y = this->y + rhs.y;
|
|
retVal.z = this->z + rhs.z;
|
|
return retVal;
|
|
}
|
|
Vector3 operator-(const Vector3& rhs)
|
|
{
|
|
Vector3 retVal;
|
|
retVal.x = this->x - rhs.x;
|
|
retVal.y = this->y - rhs.y;
|
|
retVal.z = this->z - rhs.z;
|
|
return retVal;
|
|
}
|
|
Vector3 operator*(const float& rhs)
|
|
{
|
|
Vector3 retVal;
|
|
retVal.x = this->x * rhs;
|
|
retVal.y = this->y * rhs;
|
|
retVal.z = this->z * rhs;
|
|
return retVal;
|
|
}
|
|
friend Vector3 operator*(const float& lhs, const Vector3& rhs)
|
|
{
|
|
Vector3 retVal;
|
|
retVal.x = rhs.x * lhs;
|
|
retVal.y = rhs.y * lhs;
|
|
retVal.z = rhs.z * lhs;
|
|
return retVal;
|
|
}
|
|
Vector3 operator/(const float& rhs)
|
|
{
|
|
Vector3 retVal;
|
|
retVal.x = this->x / rhs;
|
|
retVal.y = this->y / rhs;
|
|
retVal.z = this->z / rhs;
|
|
return retVal;
|
|
}
|
|
Vector3 operator-()
|
|
{
|
|
Vector3 retVal;
|
|
retVal.x = -this->x;
|
|
retVal.y = -this->y;
|
|
retVal.z = -this->z;
|
|
return retVal;
|
|
}
|
|
|
|
bool operator==(const Vector3& rhs)
|
|
{
|
|
return ((this->x == rhs.x) && (this->y == rhs.y) && (this->z == rhs.z));
|
|
}
|
|
|
|
|
|
float LengthSquared()
|
|
{
|
|
return ((x*x) + (y*y) + (z*z));
|
|
}
|
|
float Length()
|
|
{
|
|
return sqrtf(LengthSquared());
|
|
}
|
|
|
|
Vector3 Normalized()
|
|
{
|
|
return ((*this) / Length());
|
|
}
|
|
|
|
T x, y, z;
|
|
|
|
public:
|
|
static Vector3 Cross(Vector3& lhs, Vector3& rhs)
|
|
{
|
|
Vector3 retVal;
|
|
|
|
retVal.x = lhs.y * rhs.z - lhs.z * rhs.y;
|
|
retVal.y = lhs.z * rhs.x - lhs.x * rhs.z;
|
|
retVal.z = lhs.x * rhs.y - lhs.y * rhs.x;
|
|
|
|
return retVal;
|
|
}
|
|
static float Dot(Vector3& lhs, Vector3& rhs)
|
|
{
|
|
float retVal;
|
|
|
|
retVal = lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
|
|
|
|
return retVal;
|
|
}
|
|
static Vector3 Lerp(Vector3& a_Source, Vector3& a_Destination, float a_Scale)
|
|
{
|
|
Vector3 between = a_Destination - a_Source;
|
|
|
|
return a_Source + between * a_Scale;
|
|
}
|
|
};
|
|
|
|
typedef Vector3<float> Vector3f;
|
|
typedef Vector3<int> Vector3i;
|
|
typedef Vector3<unsigned int> Vector3u;
|
|
typedef Vector3<long> Vector3l;
|
|
|
|
};
|
|
};
|
|
#endif |