2015-05-06 07:05:06 +00:00
|
|
|
#ifndef CVECTOR4F_HPP
|
|
|
|
#define CVECTOR4F_HPP
|
|
|
|
|
|
|
|
#include "Global.hpp"
|
|
|
|
#include "TVectorUnion.hpp"
|
|
|
|
#include "CVector3f.hpp"
|
2015-11-01 12:04:29 +00:00
|
|
|
#if ZE_ATHENA_TYPES
|
2015-05-06 07:05:06 +00:00
|
|
|
#include <Athena/IStreamReader.hpp>
|
2015-11-01 12:04:29 +00:00
|
|
|
#endif
|
2015-05-06 07:05:06 +00:00
|
|
|
#include <math.h>
|
|
|
|
#include <float.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2015-10-08 00:21:38 +00:00
|
|
|
namespace Zeus
|
|
|
|
{
|
2015-10-25 19:31:41 +00:00
|
|
|
class alignas(16) CVector4f
|
2015-05-06 07:05:06 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
ZE_DECLARE_ALIGNED_ALLOCATOR();
|
|
|
|
|
|
|
|
inline CVector4f() {zeroOut();}
|
|
|
|
#if __SSE__
|
|
|
|
CVector4f(const __m128& mVec128) : mVec128(mVec128) {}
|
|
|
|
#endif
|
2015-07-29 21:59:03 +00:00
|
|
|
#if ZE_ATHENA_TYPES
|
|
|
|
CVector4f(const atVec4f& vec)
|
|
|
|
#if __SSE__
|
|
|
|
: mVec128(vec.mVec128){}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
x = vec.vec[0], y = vec.vec[1], z = vec.vec[2], w = vec.vec[3];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2015-05-06 07:05:06 +00:00
|
|
|
CVector4f(float xyzw) {splat(xyzw);}
|
|
|
|
CVector4f(float x, float y, float z, float w) {v[0] = x; v[1] = y; v[2] = z; v[3] = w;}
|
2015-11-01 12:04:29 +00:00
|
|
|
#if ZE_ATHENA_TYPES
|
2015-05-06 07:05:06 +00:00
|
|
|
CVector4f(Athena::io::IStreamReader& input)
|
|
|
|
{
|
|
|
|
x = input.readFloat();
|
|
|
|
y = input.readFloat();
|
|
|
|
z = input.readFloat();
|
|
|
|
w = input.readFloat();
|
|
|
|
}
|
2015-11-01 12:04:29 +00:00
|
|
|
#endif
|
2015-05-06 07:05:06 +00:00
|
|
|
|
|
|
|
CVector4f(const CVector3f& other)
|
|
|
|
{
|
|
|
|
x = other.x;
|
|
|
|
y = other.y;
|
|
|
|
z = other.z;
|
|
|
|
w = 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator ==(const CVector4f& rhs) const
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion vec;
|
|
|
|
vec.mVec128 = _mm_cmpeq_ps(mVec128, rhs.mVec128);
|
|
|
|
return (vec.v[0] != 0 && vec.v[1] != 0 && vec.v[2] != 0 && vec.v[3] != 0);
|
|
|
|
#else
|
|
|
|
return (x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
inline bool operator !=(const CVector4f& rhs) const
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion vec;
|
|
|
|
vec.mVec128 = _mm_cmpneq_ps(mVec128, rhs.mVec128);
|
|
|
|
return (vec.v[0] != 0 && vec.v[1] != 0 && vec.v[2] != 0 && vec.v[3] != 0);
|
|
|
|
#else
|
|
|
|
return !(*this == rhs);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
inline bool operator <(const CVector4f& rhs) const
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion vec;
|
|
|
|
vec.mVec128 = _mm_cmplt_ps(mVec128, rhs.mVec128);
|
|
|
|
return (vec.v[0] != 0 || vec.v[1] != 0 || vec.v[2] != 0 || vec.v[3] != 0);
|
|
|
|
#else
|
|
|
|
return (x < rhs.x || y < rhs.y || z < rhs.z || w < rhs.w);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
inline bool operator <=(const CVector4f& rhs) const
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion vec;
|
|
|
|
vec.mVec128 = _mm_cmple_ps(mVec128, rhs.mVec128);
|
|
|
|
return (vec.v[0] != 0 || vec.v[1] != 0 || vec.v[2] != 0 || vec.v[3] != 0);
|
|
|
|
#else
|
|
|
|
return (x <= rhs.x || y <= rhs.y || z <= rhs.z || w <= rhs.w);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
inline bool operator >(const CVector4f& rhs) const
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion vec;
|
|
|
|
vec.mVec128 = _mm_cmpgt_ps(mVec128, rhs.mVec128);
|
|
|
|
return (vec.v[0] != 0 || vec.v[1] != 0 || vec.v[2] != 0 || vec.v[3] != 0);
|
|
|
|
#else
|
|
|
|
return (x > rhs.x || y > rhs.y || z > rhs.z || w > rhs.w);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
inline bool operator >=(const CVector4f& rhs) const
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion vec;
|
|
|
|
vec.mVec128 = _mm_cmpge_ps(mVec128, rhs.mVec128);
|
|
|
|
return (vec.v[0] != 0 || vec.v[1] != 0 || vec.v[2] != 0 || vec.v[3] != 0);
|
|
|
|
#else
|
|
|
|
return (x >= rhs.x || y >= rhs.y || z >= rhs.z || w >= rhs.w);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
inline CVector4f operator+(const CVector4f& rhs) const
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
return CVector4f(_mm_add_ps(mVec128, rhs.mVec128));
|
|
|
|
#else
|
|
|
|
return CVector4f(x + rhs.x, y + rhs.y, z + rhs.z, w + rhs.w);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
inline CVector4f operator-(const CVector4f& rhs) const
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
return CVector4f(_mm_sub_ps(mVec128, rhs.mVec128));
|
|
|
|
#else
|
|
|
|
return CVector4f(x - rhs.x, y - rhs.y, z - rhs.z, w - rhs.w);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
inline CVector4f operator-() const
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
return CVector4f(_mm_sub_ps(_mm_xor_ps(mVec128, mVec128), mVec128));
|
|
|
|
#else
|
|
|
|
return CVector4f(-x, -y, -z, -w);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
inline CVector4f operator*(const CVector4f& rhs) const
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
return CVector4f(_mm_mul_ps(mVec128, rhs.mVec128));
|
|
|
|
#else
|
|
|
|
return CVector4f(x * rhs.x, y * rhs.y, z * rhs.z, w * rhs.w);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
inline CVector4f operator/(const CVector4f& rhs) const
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
return CVector4f(_mm_div_ps(mVec128, rhs.mVec128));
|
|
|
|
#else
|
|
|
|
return CVector4f(x / rhs.x, y / rhs.y, z / rhs.z, w / rhs.w);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
inline CVector4f operator+(float val) const
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{val, val, val, val}};
|
|
|
|
return CVector4f(_mm_add_ps(mVec128, splat.mVec128));
|
|
|
|
#else
|
|
|
|
return CVector4f(x + val, y + val, z + val, w + val);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
inline CVector4f operator-(float val) const
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{val, val, val, val}};
|
|
|
|
return CVector4f(_mm_sub_ps(mVec128, splat.mVec128));
|
|
|
|
#else
|
|
|
|
return CVector4f(x - val, y - val, z - val, w - val);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
inline CVector4f operator*(float val) const
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{val, val, val, val}};
|
|
|
|
return CVector4f(_mm_mul_ps(mVec128, splat.mVec128));
|
|
|
|
#else
|
|
|
|
return CVector4f(x * val, y * val, z * val, w * val);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
inline CVector4f operator/(float val) const
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{val, val, val, val}};
|
|
|
|
return CVector4f(_mm_div_ps(mVec128, splat.mVec128));
|
|
|
|
#else
|
|
|
|
return CVector4f(x / val, y / val, z / val, w / val);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
inline const CVector4f& operator +=(const CVector4f& rhs)
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
mVec128 = _mm_add_ps(mVec128, rhs.mVec128);
|
|
|
|
#else
|
|
|
|
x += rhs.x; y += rhs.y; z += rhs.z; w += rhs.w;
|
|
|
|
#endif
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline const CVector4f& operator -=(const CVector4f& rhs)
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
mVec128 = _mm_sub_ps(mVec128, rhs.mVec128);
|
|
|
|
#else
|
|
|
|
x -= rhs.x; y -= rhs.y; z -= rhs.z; w -= rhs.w;
|
|
|
|
#endif
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline const CVector4f& operator *=(const CVector4f& rhs)
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
mVec128 = _mm_mul_ps(mVec128, rhs.mVec128);
|
|
|
|
#else
|
|
|
|
x *= rhs.x; y *= rhs.y; z *= rhs.z; w *= rhs.w;
|
|
|
|
#endif
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline const CVector4f& operator /=(const CVector4f& rhs)
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
mVec128 = _mm_div_ps(mVec128, rhs.mVec128);
|
|
|
|
#else
|
|
|
|
x /= rhs.x; y /= rhs.y; z /= rhs.z; w /= rhs.w;
|
|
|
|
#endif
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
inline void normalize()
|
|
|
|
{
|
2015-10-08 00:21:38 +00:00
|
|
|
float mag = magnitude();
|
2015-10-25 19:31:41 +00:00
|
|
|
mag = 1.0 / mag;
|
|
|
|
*this *= mag;
|
2015-05-06 07:05:06 +00:00
|
|
|
}
|
|
|
|
inline CVector4f normalized() const
|
|
|
|
{
|
2015-10-08 00:21:38 +00:00
|
|
|
float mag = magnitude();
|
2015-10-25 19:31:41 +00:00
|
|
|
mag = 1.0 / mag;
|
|
|
|
return *this * mag;
|
2015-05-06 07:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline float dot(const CVector4f& rhs) const
|
|
|
|
{
|
|
|
|
#if __SSE4_1__
|
|
|
|
TVectorUnion result;
|
|
|
|
result.mVec128 = _mm_dp_ps(mVec128, rhs.mVec128, 0xF1);
|
|
|
|
return result.v[0];
|
|
|
|
#elif __SSE__
|
|
|
|
TVectorUnion result;
|
|
|
|
result.mVec128 = _mm_mul_ps(mVec128, rhs.mVec128);
|
|
|
|
return result.v[0] + result.v[1] + result.v[2] + result.v[3];
|
|
|
|
#else
|
|
|
|
return (x * rhs.x) + (y * rhs.y) + (z * rhs.z) + (w * rhs.w);
|
|
|
|
#endif
|
|
|
|
}
|
2015-10-08 00:21:38 +00:00
|
|
|
inline float magSquared() const
|
2015-05-06 07:05:06 +00:00
|
|
|
{
|
|
|
|
#if __SSE4_1__
|
|
|
|
TVectorUnion result;
|
|
|
|
result.mVec128 = _mm_dp_ps(mVec128, mVec128, 0x71);
|
|
|
|
return result.v[0];
|
|
|
|
#elif __SSE__
|
|
|
|
TVectorUnion result;
|
|
|
|
result.mVec128 = _mm_mul_ps(mVec128, mVec128);
|
|
|
|
return result.v[0] + result.v[1] + result.v[2];
|
|
|
|
#else
|
|
|
|
return x*x + y*y + z*z + w*w;
|
|
|
|
#endif
|
|
|
|
}
|
2015-10-08 00:21:38 +00:00
|
|
|
inline float magnitude() const
|
2015-05-06 07:05:06 +00:00
|
|
|
{
|
2015-10-08 00:21:38 +00:00
|
|
|
return sqrtf(magSquared());
|
2015-05-06 07:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void zeroOut()
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
mVec128 = _mm_xor_ps(mVec128, mVec128);
|
|
|
|
#else
|
|
|
|
v[0] = 0.0; v[1] = 0.0; v[2] = 0.0; v[3] = 0.0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void splat(float xyzw)
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{xyzw, xyzw, xyzw, xyzw}};
|
|
|
|
mVec128 = splat.mVec128;
|
|
|
|
#else
|
|
|
|
v[0] = xyz; v[1] = xyz; v[2] = xyz; v[3] = xyzw;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline CVector4f lerp(const CVector4f& a, const CVector4f& b, float t)
|
|
|
|
{
|
|
|
|
return (a + (b - a) * t);
|
|
|
|
}
|
|
|
|
static inline CVector4f nlerp(const CVector4f& a, const CVector4f& b, float t)
|
|
|
|
{
|
|
|
|
return lerp(a, b, t).normalized();
|
|
|
|
}
|
|
|
|
|
2015-10-25 19:31:41 +00:00
|
|
|
inline bool canBeNormalized() const
|
2015-05-06 07:05:06 +00:00
|
|
|
{
|
2015-10-25 19:31:41 +00:00
|
|
|
const float epsilon = 1.1920929e-7f;
|
|
|
|
if (fabs(x) >= epsilon || fabs(y) >= epsilon || fabs(z) >= epsilon || fabs(w) >= epsilon)
|
|
|
|
return true;
|
|
|
|
return false;
|
2015-08-25 22:04:15 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 19:31:41 +00:00
|
|
|
inline bool isNormalized() const
|
|
|
|
{ return !canBeNormalized(); }
|
2015-05-06 07:05:06 +00:00
|
|
|
|
|
|
|
inline float& operator[](size_t idx) {return (&x)[idx];}
|
|
|
|
inline const float& operator[](size_t idx) const {return (&x)[idx];}
|
|
|
|
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
float x, y, z, w;
|
|
|
|
};
|
|
|
|
float v[4];
|
|
|
|
#if __SSE__
|
|
|
|
__m128 mVec128;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static const CVector4f skOne;
|
|
|
|
static const CVector4f skNegOne;
|
|
|
|
static const CVector4f skZero;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static inline CVector4f operator+(float lhs, const CVector4f& rhs)
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{lhs, lhs, lhs, lhs}};
|
|
|
|
return CVector4f(_mm_add_ps(splat.mVec128, rhs.mVec128));
|
|
|
|
#else
|
|
|
|
return CVector4f(lhs + rhs.x, lhs + rhs.y, lhs + rhs.z, lhs + rhs.w);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline CVector4f operator-(float lhs, const CVector4f& rhs)
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{lhs, lhs, lhs, lhs}};
|
|
|
|
return CVector4f(_mm_sub_ps(splat.mVec128, rhs.mVec128));
|
|
|
|
#else
|
|
|
|
return CVector4f(lhs - rhs.x, lhs - rhs.y, lhs - rhs.z, lhs - rhs.w);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline CVector4f operator*(float lhs, const CVector4f& rhs)
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{lhs, lhs, lhs, lhs}};
|
|
|
|
return CVector4f(_mm_mul_ps(splat.mVec128, rhs.mVec128));
|
|
|
|
#else
|
|
|
|
return CVector4f(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z, lhs * rhs.w);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline CVector4f operator/(float lhs, const CVector4f& rhs)
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
TVectorUnion splat = {{lhs, lhs, lhs, lhs}};
|
|
|
|
return CVector4f(_mm_div_ps(splat.mVec128, rhs.mVec128));
|
|
|
|
#else
|
|
|
|
return CVector4f(lhs / rhs.x, lhs / rhs.y, lhs / rhs.z, lhs / rhs.w);
|
|
|
|
#endif
|
|
|
|
}
|
2015-10-08 00:21:38 +00:00
|
|
|
}
|
2015-05-06 07:05:06 +00:00
|
|
|
|
|
|
|
#endif // CVECTOR4F_HPP
|