192 lines
3.6 KiB
C++
192 lines
3.6 KiB
C++
#ifndef __VECTOR4D__
|
|
#define __VECTOR4D__
|
|
#include "Math.h"
|
|
|
|
namespace LifeMath
|
|
{
|
|
#undef INLINE
|
|
#ifdef _WIN32
|
|
#ifdef _MSC_VER
|
|
#define INLINE __forceinline
|
|
#endif
|
|
#else
|
|
#ifdef __GNUC__
|
|
#define INLINE __attribute__((always_inline))
|
|
#endif
|
|
#endif
|
|
|
|
#ifndef INLINE
|
|
#define INLINE inline
|
|
#endif
|
|
|
|
// struct Vector4D;
|
|
// struct Vector4D;
|
|
|
|
template<typename T>
|
|
struct Vector4D
|
|
{
|
|
public:
|
|
T X, Y, Z, W;
|
|
INLINE Vector4D<T>(T x, T y, T z, T w)
|
|
{
|
|
this->X = x;
|
|
this->Y = y;
|
|
this->Z = z;
|
|
this->W = w;
|
|
}
|
|
INLINE Vector4D<T>(void)
|
|
{
|
|
this->X = 0;
|
|
this->Y = 0;
|
|
this->Z = 0;
|
|
this->W = 0;
|
|
}
|
|
|
|
INLINE Vector4D<T>(T val)
|
|
{
|
|
this->X = val;
|
|
this->Y = val;
|
|
this->Z = val;
|
|
this->W = val;
|
|
}
|
|
//unary operators
|
|
INLINE Vector4D<T> operator -() const
|
|
{
|
|
return Vector4D<T>(-X, -Y, -Z, -W);
|
|
}
|
|
|
|
INLINE Vector4D<T> operator +() const
|
|
{
|
|
return *this;
|
|
}
|
|
INLINE Vector4D<T>& operator=(const Vector4D<T>& other)
|
|
{
|
|
X = other.X;
|
|
Y = other.Y;
|
|
Z = other.Z;
|
|
W = other.W;
|
|
return *this;
|
|
}
|
|
//binary operators
|
|
INLINE Vector4D<T> operator +(const Vector4D<T> &other) const
|
|
{
|
|
return Vector4D<T>(X + other.X, Y + other.Y, Z + other.Z, W + other.W);
|
|
}
|
|
INLINE Vector4D<T> operator -(const Vector4D<T> &other) const
|
|
{
|
|
return Vector4D<T>(X - other.X, Y - other.Y, Z - other.Z, W - other.W);
|
|
}
|
|
INLINE Vector4D<T> operator *(T other) const
|
|
{
|
|
return Vector4D<T>(X * other, Y * other, Z * other, W * other);
|
|
}
|
|
INLINE Vector4D<T> operator /(T other) const
|
|
{
|
|
return Vector4D<T>(X / other, Y / other, Z / other, W / other);
|
|
}
|
|
|
|
INLINE operator T * ()
|
|
{
|
|
return &X;
|
|
}
|
|
INLINE operator const T * () const
|
|
{
|
|
return &X;
|
|
}
|
|
INLINE bool operator == (const Vector4D<T>& vector) const
|
|
{
|
|
return (X == vector.X && Y == vector.Y && Z == vector.Z && W == vector.W);
|
|
}
|
|
|
|
INLINE bool operator != (const Vector4D<T>& vector) const
|
|
{
|
|
return (X != vector.X || Y != vector.Y || Z != vector.Z || W != vector.W);
|
|
}
|
|
INLINE Vector4D<T>& operator += (const Vector4D<T>& Vector)
|
|
{
|
|
X += Vector.X;
|
|
Y += Vector.Y;
|
|
Z += Vector.Z;
|
|
W += Vector.W;
|
|
return *this;
|
|
}
|
|
INLINE Vector4D<T>& operator -= (const Vector4D<T>& Vector)
|
|
{
|
|
X -= Vector.X;
|
|
Y -= Vector.Y;
|
|
Z -= Vector.Z;
|
|
W -= Vector.W;
|
|
return *this;
|
|
}
|
|
INLINE Vector4D<T>& operator *= (T Value)
|
|
{
|
|
X *= Value;
|
|
Y *= Value;
|
|
Z *= Value;
|
|
W *= Value;
|
|
return *this;
|
|
}
|
|
INLINE Vector4D<T>& operator /= (T Value)
|
|
{
|
|
//assert(Value && "Zero Value");
|
|
*this *= (1.0f / Value);
|
|
return *this;
|
|
}
|
|
INLINE Vector4D<T>& operator += (T Value)
|
|
{
|
|
X += Value;
|
|
Y += Value;
|
|
Z += Value;
|
|
W += Value;
|
|
return *this;
|
|
}
|
|
INLINE Vector4D<T>& operator -= (T Value)
|
|
{
|
|
X -= Value;
|
|
Y -= Value;
|
|
Z -= Value;
|
|
W -= Value;
|
|
return *this;
|
|
}
|
|
INLINE Vector4D<T>& operator /= (const Vector4D<T>& vector)
|
|
{
|
|
X /= vector.X;
|
|
Y /= vector.Y;
|
|
Z /= vector.Z;
|
|
W /= vector.W;
|
|
return *this;
|
|
}
|
|
INLINE Vector4D<T>& operator *= (const Vector4D<T>& vector)
|
|
{
|
|
X *= vector.X;
|
|
Y *= vector.Y;
|
|
Z *= vector.Z;
|
|
W *= vector.W;
|
|
return *this;
|
|
}
|
|
|
|
INLINE T& operator [](int index)
|
|
{
|
|
//assert(index < 3 && "Out Of Range");
|
|
return *(index + (&X));
|
|
}
|
|
INLINE T operator [] (int index) const
|
|
{
|
|
//assert(index < 2 && "Out Of Range");
|
|
return * (index + (&X));
|
|
}
|
|
INLINE bool operator > (const Vector4D<T>& v) const
|
|
{
|
|
return X > v.X && Y > v.Y && Z > v.Z && W > v.W;
|
|
}
|
|
INLINE bool operator < (const Vector4D<T>& v) const
|
|
{
|
|
return X < v.X && Y < v.Y && Z < v.Z && W < v.W;
|
|
}
|
|
};
|
|
|
|
typedef Vector4D<float> Vec4f;
|
|
typedef Vector4D<int> Vec4i;
|
|
typedef Vector4D<float> float4;
|
|
}
|
|
#endif |