mirror of https://github.com/AxioDL/zeus.git
Merge pull request #12 from lioncash/matrix
CMatrix3f/CMatrix4f: Use std::array where applicable
This commit is contained in:
commit
3d4d304db5
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
#include "zeus/CVector3f.hpp"
|
#include "zeus/CVector3f.hpp"
|
||||||
|
@ -20,7 +21,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr CMatrix3f(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
|
constexpr CMatrix3f(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
|
||||||
: m{{m00, m10, m20}, {m01, m11, m21}, {m02, m12, m22}} {}
|
: m{{{m00, m10, m20}, {m01, m11, m21}, {m02, m12, m22}}} {}
|
||||||
|
|
||||||
CMatrix3f(const CVector3f& scaleVec) {
|
CMatrix3f(const CVector3f& scaleVec) {
|
||||||
m[0][0] = scaleVec[0];
|
m[0][0] = scaleVec[0];
|
||||||
|
@ -30,17 +31,9 @@ public:
|
||||||
|
|
||||||
CMatrix3f(float scale) : CMatrix3f(CVector3f(scale)) {}
|
CMatrix3f(float scale) : CMatrix3f(CVector3f(scale)) {}
|
||||||
|
|
||||||
constexpr CMatrix3f(const CVector3f& r0, const CVector3f& r1, const CVector3f& r2) {
|
constexpr CMatrix3f(const CVector3f& r0, const CVector3f& r1, const CVector3f& r2) : m{{r0, r1, r2}} {}
|
||||||
m[0] = r0;
|
|
||||||
m[1] = r1;
|
|
||||||
m[2] = r2;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr CMatrix3f(const CMatrix3f& other) {
|
constexpr CMatrix3f(const CMatrix3f& other) = default;
|
||||||
m[0] = other.m[0];
|
|
||||||
m[1] = other.m[1];
|
|
||||||
m[2] = other.m[2];
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr CMatrix3f(const simd<float>& r0, const simd<float>& r1, const simd<float>& r2) {
|
constexpr CMatrix3f(const simd<float>& r0, const simd<float>& r1, const simd<float>& r2) {
|
||||||
m[0].mSimd = r0;
|
m[0].mSimd = r0;
|
||||||
|
@ -78,12 +71,7 @@ public:
|
||||||
|
|
||||||
CMatrix3f(const CQuaternion& quat);
|
CMatrix3f(const CQuaternion& quat);
|
||||||
|
|
||||||
CMatrix3f& operator=(const CMatrix3f& other) {
|
CMatrix3f& operator=(const CMatrix3f& other) = default;
|
||||||
m[0] = other.m[0];
|
|
||||||
m[1] = other.m[1];
|
|
||||||
m[2] = other.m[2];
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
CVector3f operator*(const CVector3f& other) const {
|
CVector3f operator*(const CVector3f& other) const {
|
||||||
return m[0].mSimd * other.mSimd.shuffle<0, 0, 0, 0>() + m[1].mSimd * other.mSimd.shuffle<1, 1, 1, 1>() +
|
return m[0].mSimd * other.mSimd.shuffle<0, 0, 0, 0>() + m[1].mSimd * other.mSimd.shuffle<1, 1, 1, 1>() +
|
||||||
|
@ -91,12 +79,12 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
CVector3f& operator[](size_t i) {
|
CVector3f& operator[](size_t i) {
|
||||||
assert(i < 3);
|
assert(i < m.size());
|
||||||
return m[i];
|
return m[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
const CVector3f& operator[](size_t i) const {
|
const CVector3f& operator[](size_t i) const {
|
||||||
assert(i < 3);
|
assert(i < m.size());
|
||||||
return m[i];
|
return m[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,14 +142,15 @@ public:
|
||||||
m[2][0] * (m[0][1] * m[1][2] - m[1][1] * m[0][2]);
|
m[2][0] * (m[0][1] * m[1][2] - m[1][1] * m[0][2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
CVector3f m[3];
|
std::array<CVector3f, 3> m;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline CMatrix3f operator*(const CMatrix3f& lhs, const CMatrix3f& rhs) {
|
inline CMatrix3f operator*(const CMatrix3f& lhs, const CMatrix3f& rhs) {
|
||||||
simd<float> v[3];
|
std::array<simd<float>, 3> v;
|
||||||
for (int i = 0; i < 3; ++i)
|
for (size_t i = 0; i < v.size(); ++i) {
|
||||||
v[i] = lhs.m[0].mSimd * rhs[i].mSimd.shuffle<0, 0, 0, 0>() + lhs.m[1].mSimd * rhs[i].mSimd.shuffle<1, 1, 1, 1>() +
|
v[i] = lhs.m[0].mSimd * rhs[i].mSimd.shuffle<0, 0, 0, 0>() + lhs.m[1].mSimd * rhs[i].mSimd.shuffle<1, 1, 1, 1>() +
|
||||||
lhs.m[2].mSimd * rhs[i].mSimd.shuffle<2, 2, 2, 2>();
|
lhs.m[2].mSimd * rhs[i].mSimd.shuffle<2, 2, 2, 2>();
|
||||||
|
}
|
||||||
return CMatrix3f(v[0], v[1], v[2]);
|
return CMatrix3f(v[0], v[1], v[2]);
|
||||||
}
|
}
|
||||||
} // namespace zeus
|
} // namespace zeus
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
#include "zeus/CMatrix3f.hpp"
|
#include "zeus/CMatrix3f.hpp"
|
||||||
|
@ -20,7 +21,7 @@ public:
|
||||||
|
|
||||||
constexpr CMatrix4f(float m00, float m01, float m02, float m03, float m10, float m11, float m12, float m13, float m20,
|
constexpr CMatrix4f(float m00, float m01, float m02, float m03, float m10, float m11, float m12, float m13, float m20,
|
||||||
float m21, float m22, float m23, float m30, float m31, float m32, float m33)
|
float m21, float m22, float m23, float m30, float m31, float m32, float m33)
|
||||||
: m{{m00, m10, m20, m30}, {m01, m11, m21, m31}, {m02, m12, m22, m32}, {m03, m13, m23, m33}} {}
|
: m{{{m00, m10, m20, m30}, {m01, m11, m21, m31}, {m02, m12, m22, m32}, {m03, m13, m23, m33}}} {}
|
||||||
|
|
||||||
CMatrix4f(const CVector3f& scaleVec) {
|
CMatrix4f(const CVector3f& scaleVec) {
|
||||||
m[0][0] = scaleVec[0];
|
m[0][0] = scaleVec[0];
|
||||||
|
@ -29,19 +30,10 @@ public:
|
||||||
m[3][3] = 1.0f;
|
m[3][3] = 1.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr CMatrix4f(const CVector4f& r0, const CVector4f& r1, const CVector4f& r2, const CVector4f& r3) {
|
constexpr CMatrix4f(const CVector4f& r0, const CVector4f& r1, const CVector4f& r2, const CVector4f& r3)
|
||||||
m[0] = r0;
|
: m{{r0, r1, r2, r3}} {}
|
||||||
m[1] = r1;
|
|
||||||
m[2] = r2;
|
|
||||||
m[3] = r3;
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr CMatrix4f(const CMatrix4f& other) {
|
constexpr CMatrix4f(const CMatrix4f& other) = default;
|
||||||
m[0] = other.m[0];
|
|
||||||
m[1] = other.m[1];
|
|
||||||
m[2] = other.m[2];
|
|
||||||
m[3] = other.m[3];
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr CMatrix4f(const simd<float>& r0, const simd<float>& r1, const simd<float>& r2, const simd<float>& r3) {
|
constexpr CMatrix4f(const simd<float>& r0, const simd<float>& r1, const simd<float>& r2, const simd<float>& r3) {
|
||||||
m[0].mSimd = r0;
|
m[0].mSimd = r0;
|
||||||
|
@ -57,13 +49,7 @@ public:
|
||||||
m[3].mSimd = CVector4f(0.f, 0.f, 0.f, 1.0f).mSimd;
|
m[3].mSimd = CVector4f(0.f, 0.f, 0.f, 1.0f).mSimd;
|
||||||
}
|
}
|
||||||
|
|
||||||
CMatrix4f& operator=(const CMatrix4f& other) {
|
CMatrix4f& operator=(const CMatrix4f& other) = default;
|
||||||
m[0] = other.m[0];
|
|
||||||
m[1] = other.m[1];
|
|
||||||
m[2] = other.m[2];
|
|
||||||
m[3] = other.m[3];
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
CVector4f operator*(const CVector4f& other) const {
|
CVector4f operator*(const CVector4f& other) const {
|
||||||
return m[0].mSimd * other.mSimd.shuffle<0, 0, 0, 0>() + m[1].mSimd * other.mSimd.shuffle<1, 1, 1, 1>() +
|
return m[0].mSimd * other.mSimd.shuffle<0, 0, 0, 0>() + m[1].mSimd * other.mSimd.shuffle<1, 1, 1, 1>() +
|
||||||
|
@ -71,12 +57,12 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
CVector4f& operator[](size_t i) {
|
CVector4f& operator[](size_t i) {
|
||||||
assert(i < 4);
|
assert(i < m.size());
|
||||||
return m[i];
|
return m[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
const CVector4f& operator[](size_t i) const {
|
const CVector4f& operator[](size_t i) const {
|
||||||
assert(i < 4);
|
assert(i < m.size());
|
||||||
return m[i];
|
return m[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,15 +79,16 @@ public:
|
||||||
return xfVec.toVec3f() / xfVec.w();
|
return xfVec.toVec3f() / xfVec.w();
|
||||||
}
|
}
|
||||||
|
|
||||||
CVector4f m[4];
|
std::array<CVector4f, 4> m;
|
||||||
};
|
};
|
||||||
extern const CMatrix4f skIdentityMatrix4f;
|
extern const CMatrix4f skIdentityMatrix4f;
|
||||||
|
|
||||||
inline CMatrix4f operator*(const CMatrix4f& lhs, const CMatrix4f& rhs) {
|
inline CMatrix4f operator*(const CMatrix4f& lhs, const CMatrix4f& rhs) {
|
||||||
simd<float> v[4];
|
std::array<simd<float>, 4> v;
|
||||||
for (int i = 0; i < 4; ++i)
|
for (size_t i = 0; i < v.size(); ++i) {
|
||||||
v[i] = lhs.m[0].mSimd * rhs[i].mSimd.shuffle<0, 0, 0, 0>() + lhs.m[1].mSimd * rhs[i].mSimd.shuffle<1, 1, 1, 1>() +
|
v[i] = lhs.m[0].mSimd * rhs[i].mSimd.shuffle<0, 0, 0, 0>() + lhs.m[1].mSimd * rhs[i].mSimd.shuffle<1, 1, 1, 1>() +
|
||||||
lhs.m[2].mSimd * rhs[i].mSimd.shuffle<2, 2, 2, 2>() + lhs.m[3].mSimd * rhs[i].mSimd.shuffle<3, 3, 3, 3>();
|
lhs.m[2].mSimd * rhs[i].mSimd.shuffle<2, 2, 2, 2>() + lhs.m[3].mSimd * rhs[i].mSimd.shuffle<3, 3, 3, 3>();
|
||||||
|
}
|
||||||
return CMatrix4f(v[0], v[1], v[2], v[3]);
|
return CMatrix4f(v[0], v[1], v[2], v[3]);
|
||||||
}
|
}
|
||||||
} // namespace zeus
|
} // namespace zeus
|
||||||
|
|
Loading…
Reference in New Issue