2016-03-04 23:03:26 +00:00
|
|
|
#include "zeus/CMatrix3f.hpp"
|
|
|
|
#include "zeus/CQuaternion.hpp"
|
|
|
|
#include "zeus/Global.hpp"
|
2015-04-19 20:39:16 +00:00
|
|
|
|
2016-03-04 23:03:26 +00:00
|
|
|
namespace zeus
|
2015-10-08 00:21:38 +00:00
|
|
|
{
|
2015-04-19 20:39:16 +00:00
|
|
|
const CMatrix3f CMatrix3f::skIdentityMatrix3f = CMatrix3f();
|
|
|
|
|
|
|
|
CMatrix3f::CMatrix3f(const CQuaternion& quat)
|
|
|
|
{
|
|
|
|
CQuaternion nq = quat.normalized();
|
2016-04-07 03:39:24 +00:00
|
|
|
float x2 = nq.x * nq.x;
|
|
|
|
float y2 = nq.y * nq.y;
|
|
|
|
float z2 = nq.z * nq.z;
|
2016-07-08 18:42:42 +00:00
|
|
|
|
2015-04-19 20:39:16 +00:00
|
|
|
m[0][0] = 1.0 - 2.0 * y2 - 2.0 * z2;
|
2016-04-07 03:39:24 +00:00
|
|
|
m[1][0] = 2.0 * nq.x * nq.y - 2.0 * nq.z * nq.w;
|
|
|
|
m[2][0] = 2.0 * nq.x * nq.z + 2.0 * nq.y * nq.w;
|
2016-07-08 18:42:42 +00:00
|
|
|
|
2016-04-07 03:39:24 +00:00
|
|
|
m[0][1] = 2.0 * nq.x * nq.y + 2.0 * nq.z * nq.w;
|
2015-04-19 20:39:16 +00:00
|
|
|
m[1][1] = 1.0 - 2.0 * x2 - 2.0 * z2;
|
2016-04-07 03:39:24 +00:00
|
|
|
m[2][1] = 2.0 * nq.y * nq.z - 2.0 * nq.x * nq.w;
|
2016-07-08 18:42:42 +00:00
|
|
|
|
2016-04-07 03:39:24 +00:00
|
|
|
m[0][2] = 2.0 * nq.x * nq.z - 2.0 * nq.y * nq.w;
|
|
|
|
m[1][2] = 2.0 * nq.y * nq.z + 2.0 * nq.x * nq.w;
|
2015-04-19 20:39:16 +00:00
|
|
|
m[2][2] = 1.0 - 2.0 * x2 - 2.0 * y2;
|
2016-07-08 18:42:42 +00:00
|
|
|
|
2015-04-19 20:39:16 +00:00
|
|
|
m[0][3] = 0.0f;
|
|
|
|
m[1][3] = 0.0f;
|
|
|
|
m[2][3] = 0.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMatrix3f::transpose()
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
__m128 zero = _mm_xor_ps(vec[0].mVec128, vec[0].mVec128);
|
|
|
|
__m128 T0 = _mm_unpacklo_ps(vec[0].mVec128, vec[1].mVec128);
|
|
|
|
__m128 T2 = _mm_unpacklo_ps(vec[2].mVec128, zero);
|
|
|
|
__m128 T1 = _mm_unpackhi_ps(vec[0].mVec128, vec[1].mVec128);
|
|
|
|
__m128 T3 = _mm_unpackhi_ps(vec[2].mVec128, zero);
|
|
|
|
vec[0].mVec128 = _mm_movelh_ps(T0, T2);
|
|
|
|
vec[1].mVec128 = _mm_movehl_ps(T2, T0);
|
|
|
|
vec[2].mVec128 = _mm_movelh_ps(T1, T3);
|
|
|
|
#else
|
|
|
|
float tmp;
|
2016-07-08 18:42:42 +00:00
|
|
|
|
2015-04-19 20:39:16 +00:00
|
|
|
tmp = m[0][1];
|
|
|
|
m[0][1] = m[1][0];
|
|
|
|
m[1][0] = tmp;
|
2016-07-08 18:42:42 +00:00
|
|
|
|
2015-04-19 20:39:16 +00:00
|
|
|
tmp = m[0][2];
|
|
|
|
m[0][2] = m[2][0];
|
|
|
|
m[2][0] = tmp;
|
2016-07-08 18:42:42 +00:00
|
|
|
|
2015-04-19 20:39:16 +00:00
|
|
|
tmp = m[1][2];
|
|
|
|
m[1][2] = m[2][1];
|
|
|
|
m[2][1] = tmp;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
CMatrix3f CMatrix3f::transposed() const
|
|
|
|
{
|
|
|
|
#if __SSE__
|
|
|
|
__m128 zero = _mm_xor_ps(vec[0].mVec128, vec[0].mVec128);
|
|
|
|
__m128 T0 = _mm_unpacklo_ps(vec[0].mVec128, vec[1].mVec128);
|
|
|
|
__m128 T2 = _mm_unpacklo_ps(vec[2].mVec128, zero);
|
|
|
|
__m128 T1 = _mm_unpackhi_ps(vec[0].mVec128, vec[1].mVec128);
|
|
|
|
__m128 T3 = _mm_unpackhi_ps(vec[2].mVec128, zero);
|
|
|
|
return CMatrix3f(_mm_movelh_ps(T0, T2), _mm_movehl_ps(T2, T0), _mm_movelh_ps(T1, T3));
|
|
|
|
#else
|
|
|
|
CMatrix3f ret(*this);
|
|
|
|
float tmp;
|
2016-07-08 18:42:42 +00:00
|
|
|
|
2015-04-19 20:39:16 +00:00
|
|
|
tmp = ret.m[0][1];
|
|
|
|
ret.m[0][1] = ret.m[1][0];
|
|
|
|
ret.m[1][0] = tmp;
|
2016-07-08 18:42:42 +00:00
|
|
|
|
2015-04-19 20:39:16 +00:00
|
|
|
tmp = m[0][2];
|
|
|
|
ret.m[0][2] = ret.m[2][0];
|
|
|
|
ret.m[2][0] = tmp;
|
2016-07-08 18:42:42 +00:00
|
|
|
|
2015-04-19 20:39:16 +00:00
|
|
|
tmp = m[1][2];
|
|
|
|
ret.m[1][2] = ret.m[2][1];
|
|
|
|
ret.m[2][1] = tmp;
|
2016-07-08 18:42:42 +00:00
|
|
|
|
2015-04-19 20:39:16 +00:00
|
|
|
return ret;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
CMatrix3f CMatrix3f::inverted() const
|
|
|
|
{
|
2016-07-08 18:42:42 +00:00
|
|
|
float det = m[0][0] * m[1][1] * m[2][2] + m[1][0] * m[2][1] * m[0][2] + m[2][0] * m[0][1] * m[1][2] -
|
|
|
|
m[0][2] * m[1][1] * m[2][0] - m[1][2] * m[2][1] * m[0][0] - m[2][2] * m[0][1] * m[1][0];
|
|
|
|
|
2015-04-19 20:39:16 +00:00
|
|
|
if (det == 0.0)
|
|
|
|
return CMatrix3f();
|
2016-07-08 18:42:42 +00:00
|
|
|
|
2015-04-19 20:39:16 +00:00
|
|
|
det = 1.0f / det;
|
2016-07-08 18:42:42 +00:00
|
|
|
return CMatrix3f((m[1][1] * m[2][2] - m[1][2] * m[2][1]) * det, -(m[1][0] * m[2][2] - m[1][2] * m[2][0]) * det,
|
|
|
|
(m[1][0] * m[2][1] - m[1][1] * m[2][0]) * det, -(m[0][1] * m[2][2] - m[0][2] * m[2][1]) * det,
|
|
|
|
(m[0][0] * m[2][2] - m[0][2] * m[2][0]) * det, -(m[0][0] * m[2][1] - m[0][1] * m[2][0]) * det,
|
|
|
|
(m[0][1] * m[1][2] - m[0][2] * m[1][1]) * det, -(m[0][0] * m[1][2] - m[0][2] * m[1][0]) * det,
|
|
|
|
(m[0][0] * m[1][1] - m[0][1] * m[1][0]) * det);
|
2015-04-19 20:39:16 +00:00
|
|
|
}
|
2015-10-08 00:21:38 +00:00
|
|
|
}
|