Files
LifeProject/LifeCore/math/Vector3D.h
T
2025-05-13 02:46:21 +03:00

180 lines
3.3 KiB
C++

#ifndef __VECTOR3D__
#define __VECTOR3D__
#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
template<typename T>
struct Vector3D
{
public:
T X, Y, Z;
INLINE Vector3D<T>(T x, T y, T z)
{
this->X = x;
this->Y = y;
this->Z = z;
}
INLINE Vector3D<T>(void)
{
this->X = 0.0f;
this->Y = 0.0f;
this->Z = 0.0f;
}
INLINE Vector3D<T>(T val)
{
this->X = val;
this->Y = val;
this->Z = val;
}
INLINE Vector3D<T>& operator=(const Vector3D<T>& other)
{
X = other.X;
Y = other.Y;
Z = other.Z;
return *this;
}
//unary operators
INLINE Vector3D<T> operator -() const
{
return Vector3D<T>(-X, -Y, -Z);
}
INLINE Vector3D<T> operator +() const
{
return *this;
}
//binary operators
INLINE Vector3D<T> operator +(const Vector3D<T> &other) const
{
return Vector3D<T>(X + other.X, Y + other.Y, Z + other.Z);
}
INLINE Vector3D<T> operator -(const Vector3D<T> &other) const
{
return Vector3D<T>(X - other.X, Y - other.Y, Z - other.Z);
}
INLINE Vector3D<T> operator *(T other) const
{
return Vector3D<T>(X * other, Y * other, Z * other);
}
INLINE Vector3D<T> operator /(T other) const
{
return Vector3D<T>(X / other, Y / other, Z / other);
}
INLINE operator T * ()
{
return &X;
}
INLINE operator const T * () const
{
return &X;
}
INLINE bool operator == (const Vector3D<T>& vector) const
{
return (X == vector.X && Y == vector.Y && Z == vector.Z);
}
INLINE bool operator != (const Vector3D<T>& vector) const
{
return (X != vector.X || Y != vector.Y || Z != vector.Z);
}
INLINE Vector3D<T>& operator += (const Vector3D<T>& Vector)
{
X += Vector.X;
Y += Vector.Y;
Z += Vector.Z;
return *this;
}
INLINE Vector3D<T>& operator -= (const Vector3D<T>& Vector)
{
X -= Vector.X;
Y -= Vector.Y;
Z -= Vector.Z;
return *this;
}
INLINE Vector3D<T>& operator *= (T Value)
{
X *= Value;
Y *= Value;
Z *= Value;
return *this;
}
INLINE Vector3D<T>& operator /= (T Value)
{
//assert(Value && "Zero Value");
*this *= (1.0f / Value);
return *this;
}
INLINE Vector3D<T>& operator += (T Value)
{
X += Value;
Y += Value;
Z += Value;
return *this;
}
INLINE Vector3D<T>& operator -= (T Value)
{
X -= Value;
Y -= Value;
Z -= Value;
return *this;
}
INLINE Vector3D<T>& operator /= (const Vector3D<T>& vector)
{
X /= vector.X;
Y /= vector.Y;
Z /= vector.Z;
return *this;
}
INLINE Vector3D<T>& operator *= (const Vector3D<T>& vector)
{
X *= vector.X;
Y *= vector.Y;
Z *= vector.Z;
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 Vector3D<T>& v) const
{
return X > v.X && Y > v.Y && Z > v.Z;
}
INLINE bool operator < (const Vector3D<T>& v) const
{
return X < v.X && Y < v.Y && Z < v.Z;
}
};
typedef Vector3D<float> Vec3f;
typedef Vector3D<int> Vec3i;
typedef Vector3D<float> float3;
}
#endif