1
0
Fork 0
spectre/include/Spectre/Math/Vector4.inl
Henrik Hautakoski e10daeaaa6
Move everything from global namespace to "sp" namespace
When writing the X11 (linux) implementation there was a problem with X11 defining a "Display" type and we also have a Display class in the engine.

So to fix that problem and minimize the risk for running into other name conflicts. We move everything from global namespace.
2019-09-30 19:10:17 +02:00

258 lines
5.1 KiB
C++

#include <math.h>
#include "Vector4.h"
namespace sp {
template <typename T>
Vector4<T>::Vector4()
{
x = y = z = w = T(0);
}
template <typename T>
Vector4<T>::Vector4(T s)
{
x = y = z = w = s;
}
template <typename T>
Vector4<T>::Vector4(T _x, T _y, T _z)
{
x = _x; y = _y; z = _z; w = T(1);
}
template <typename T>
Vector4<T>::Vector4(T _x, T _y, T _z, T _w)
{
x = _x; y = _y; z = _z; w = _w;
}
template <typename T>
template <typename U>
Vector4<T>::Vector4(const Vector4<U>& vec)
{
x = T(vec.x); y = T(vec.y); z = T(vec.z); w = T(vec.w);
}
template <typename T>
float Vector4<T>::length() const
{
return std::sqrt((x * x) + (y * y) + (z * z) + (w * w));
}
template <typename T>
Vector4<T>& Vector4<T>::normalize()
{
float il = 1.0f / length();
x *= il; y *= il; z *= il; w *= il;
return *this;
}
// ---------
// Compare
// ---------
template <typename T>
bool operator==(const Vector4<T>& a, const Vector4<T>& b)
{
return a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w;
}
template <typename T>
bool operator!=(const Vector4<T>& a, const Vector4<T>& b)
{
return !(a == b);
}
template <typename T>
bool operator<(const Vector4<T>& a, const Vector4<T>& b)
{
return a.x < b.x && a.y < b.y && a.z < b.z && a.w < b.w;
}
template <typename T>
bool operator<=(const Vector4<T>& a, const Vector4<T>& b)
{
return !(a > b);
}
template <typename T>
bool operator>(const Vector4<T>& a, const Vector4<T>& b)
{
return a.x > b.x && a.y > b.y && a.z > b.z && a.w > b.w;
}
template <typename T>
bool operator>=(const Vector4<T>& a, const Vector4<T>& b)
{
return !(a < b);
}
// ------------
// Arithmetic
// ------------
template <typename T>
inline Vector4<T> operator+(const Vector4<T>& a, const Vector4<T>& b)
{
return Vector4<T>(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
}
template <typename T>
inline Vector4<T> operator-(const Vector4<T>& a, const Vector4<T>& b)
{
return Vector4<T>(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
}
template <typename T>
inline Vector4<T> operator/(const Vector4<T>& a, const Vector4<T>& b)
{
return Vector4<T>(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w);
}
template <typename T>
inline Vector4<T> operator*(const Vector4<T>& a, const Vector4<T>& b)
{
return Vector4<T>(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
}
template <typename T>
inline Vector4<T>& operator+=(Vector4<T>& a, const Vector4<T>& b)
{
a.x += b.x; a.y += b.y; a.z += b.z; a.w += b.w;
return a;
}
template <typename T>
inline Vector4<T>& operator-=(Vector4<T>& a, const Vector4<T>& b)
{
a.x -= b.x; a.y -= b.y; a.z -= b.z; a.w -= b.w;
return a;
}
template <typename T>
inline Vector4<T>& operator/=(Vector4<T>& a, const Vector4<T>& b)
{
a.x /= b.x; a.y /= b.y; a.z /= b.z; a.w /= b.w;
return a;
}
template <typename T>
inline Vector4<T>& operator*=(Vector4<T>& a, const Vector4<T>& b)
{
a.x *= b.x; a.y *= b.y; a.z *= b.z; a.w *= b.w;
return a;
}
// -------------------
// Scalar Arithmetic
// -------------------
template <typename T>
inline Vector4<T> operator+(const Vector4<T>& v, T s)
{
return Vector4<T>(v.x + s, v.y + s, v.z + s, v.w + s);
}
template <typename T>
inline Vector4<T> operator+(T s, const Vector4<T>& v)
{
return v + s;
}
template <typename T>
inline Vector4<T> operator-(const Vector4<T>& v, T s)
{
return Vector4<T>(v.x - s, v.y - s, v.z - s, v.w - s);
}
template <typename T>
inline Vector4<T> operator-(T s, const Vector4<T>& v)
{
return Vector4<T>(s - v.x, s - v.y, s - v.z, s - v.w);
}
template <typename T>
inline Vector4<T> operator/(const Vector4<T>& v, T s)
{
return Vector4<T>(v.x / s, v.y / s, v.z / s, v.w / s);
}
template <typename T>
inline Vector4<T> operator/(T s, const Vector4<T>& v)
{
return Vector4<T>(s / v.x, s / v.y, s / v.z, s / v.w);
}
template <typename T>
inline Vector4<T> operator*(const Vector4<T>& v, T s)
{
return Vector4<T>(v.x * s, v.y * s, v.z * s, v.w * s);
}
template <typename T>
inline Vector4<T> operator*(T s, const Vector4<T>& v)
{
return v * s;
}
// ----------------
// Scalar compare
// ----------------
template <typename T>
inline bool operator<(const Vector4<T>& v, T s)
{
return v.x < s && v.y < s && v.z < s && v.w < s;
}
template <typename T>
inline bool operator<(T s, const Vector4<T>& v)
{
return s < v.x && s < v.y && s < v.z && s < v.w;
}
template <typename T>
inline bool operator<=(const Vector4<T>& v, T s)
{
return v.x <= s && v.y <= s && v.z <= s && v.w <= s;
}
template <typename T>
inline bool operator<=(T s, const Vector4<T>& v)
{
return s <= v.x && s <= v.y && s <= v.z && s <= v.w;
}
template <typename T>
inline bool operator>(const Vector4<T>& v, T s)
{
return v.x > s && v.y > s && v.z > s && v.w > s;
}
template <typename T>
inline bool operator>(T s, const Vector4<T>& v)
{
return s > v.x && s > v.y && s > v.z && s > v.w;
}
template <typename T>
inline bool operator>=(const Vector4<T>& v, T s)
{
return v.x >= s && v.y >= s && v.z >= s && v.w >= s;
}
template <typename T>
inline bool operator>=(T s, const Vector4<T>& v)
{
return s >= v.x && s >= v.y && s >= v.z && s >= v.w;
}
template <typename T>
inline std::ostream& operator<<(std::ostream &s, const Vector4<T>& v)
{
return s << "(" << v.x << "," << v.y << "," << v.z << "," << v.w << ")";
}
} // namespace sp