254 lines
5.1 KiB
C++
254 lines
5.1 KiB
C++
|
|
#include <math.h>
|
|
#include "Vector4.h"
|
|
|
|
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 << ")";
|
|
}
|