Bug fixes

This commit is contained in:
THoehne 2024-08-21 14:54:04 +02:00
parent 46997d0628
commit f52110471a
6 changed files with 167 additions and 83 deletions

View File

@ -14,16 +14,19 @@ namespace Phanes::Core::Math::Detail
template<RealType T, bool S>
struct compute_mat4_transpose {};
template<RealType T, bool S>
struct compute_mat4_mul {};
template<RealType T>
struct compute_mat4_det<T, false>
{
static constexpr T map(Phanes::Core::Math::TMatrix4<T, false>& m)
static constexpr T map(const Phanes::Core::Math::TMatrix4<T, false>& m)
{
const TVector3<T, false>& a = reinterpret_cast<TVector3<T, false>&>(m[0]);
const TVector3<T, false>& b = reinterpret_cast<TVector3<T, false>&>(m[1]);
const TVector3<T, false>& c = reinterpret_cast<TVector3<T, false>&>(m[2]);
const TVector3<T, false>& d = reinterpret_cast<TVector3<T, false>&>(m[3]);
const TVector3<T, false>& a = reinterpret_cast<const TVector3<T, false>&>(m[0]);
const TVector3<T, false>& b = reinterpret_cast<const TVector3<T, false>&>(m[1]);
const TVector3<T, false>& c = reinterpret_cast<const TVector3<T, false>&>(m[2]);
const TVector3<T, false>& d = reinterpret_cast<const TVector3<T, false>&>(m[3]);
const float& x = m(3, 0);
const float& y = m(3, 1);
@ -43,10 +46,10 @@ namespace Phanes::Core::Math::Detail
{
static constexpr bool map(Phanes::Core::Math::TMatrix4<T, false>& r, const Phanes::Core::Math::TMatrix4<T, false>& m)
{
const TVector3<T, false>& a = reinterpret_cast<TVector3<T, false>&>(m[0]);
const TVector3<T, false>& b = reinterpret_cast<TVector3<T, false>&>(m[1]);
const TVector3<T, false>& c = reinterpret_cast<TVector3<T, false>&>(m[2]);
const TVector3<T, false>& d = reinterpret_cast<TVector3<T, false>&>(m[3]);
const TVector3<T, false>& a = reinterpret_cast<const TVector3<T, false>&>(m[0]);
const TVector3<T, false>& b = reinterpret_cast<const TVector3<T, false>&>(m[1]);
const TVector3<T, false>& c = reinterpret_cast<const TVector3<T, false>&>(m[2]);
const TVector3<T, false>& d = reinterpret_cast<const TVector3<T, false>&>(m[3]);
const float& x = m(3, 0);
const float& y = m(3, 1);
@ -60,7 +63,7 @@ namespace Phanes::Core::Math::Detail
float _1_det = (T)1.0 / (DotP(s, v) + DotP(t, u));
if (_1_det == 0.0)
if (_1_det == (T)0.0)
{
return false;
}
@ -70,10 +73,10 @@ namespace Phanes::Core::Math::Detail
u *= _1_det;
v *= _1_det;
TVector3<T, false> r0 = Cross(b, v) + t * y;
TVector3<T, false> r1 = Cross(v, a) + t * x;
TVector3<T, false> r2 = Cross(d, u) + s * w;
TVector3<T, false> r3 = Cross(u, c) + s * z;
TVector3<T, false> r0 = CrossP(b, v) + t * y;
TVector3<T, false> r1 = CrossP(v, a) - t * x;
TVector3<T, false> r2 = CrossP(d, u) + s * w;
TVector3<T, false> r3 = CrossP(u, c) - s * z;
r = TMatrix4<T, false>(r0.x, r0.y, r0.z, -DotP(b, t),
r1.x, r1.y, r1.z, DotP(a, t),
@ -95,4 +98,39 @@ namespace Phanes::Core::Math::Detail
m(0, 3), m(1, 3), m(2, 3), m(3, 3));
}
};
template<RealType T>
struct compute_mat4_mul<T, false>
{
static constexpr void map(Phanes::Core::Math::TMatrix4<T, false>& r, const Phanes::Core::Math::TMatrix4<T, false>& m1, const Phanes::Core::Math::TMatrix4<T, false>& m2)
{
r(0, 0) = m1(0, 0) * m2(0, 0) + m1(0, 1) * m2(1, 0) + m1(0, 2) * m2(2, 0) + m1(0, 3) * m2(3, 0);
r(0, 1) = m1(0, 0) * m2(0, 1) + m1(0, 1) * m2(1, 1) + m1(0, 2) * m2(2, 1) + m1(0, 3) * m2(3, 1);
r(0, 2) = m1(0, 0) * m2(0, 2) + m1(0, 1) * m2(1, 2) + m1(0, 2) * m2(2, 2) + m1(0, 3) * m2(3, 2);
r(0, 3) = m1(0, 0) * m2(0, 3) + m1(0, 1) * m2(1, 3) + m1(0, 2) * m2(2, 3) + m1(0, 3) * m2(3, 3);
r(1, 0) = m1(1, 0) * m2(0, 0) + m1(1, 1) * m2(1, 0) + m1(1, 2) * m2(2, 0) + m1(1, 3) * m2(3, 0);
r(1, 1) = m1(1, 0) * m2(0, 1) + m1(1, 1) * m2(1, 1) + m1(1, 2) * m2(2, 1) + m1(1, 3) * m2(3, 1);
r(1, 2) = m1(1, 0) * m2(0, 2) + m1(1, 1) * m2(1, 2) + m1(1, 2) * m2(2, 2) + m1(1, 3) * m2(3, 2);
r(1, 3) = m1(1, 0) * m2(0, 3) + m1(1, 1) * m2(1, 3) + m1(1, 2) * m2(2, 3) + m1(1, 3) * m2(3, 3);
r(2, 0) = m1(2, 0) * m2(0, 0) + m1(2, 1) * m2(1, 0) + m1(2, 2) * m2(2, 0) + m1(2, 3) * m2(3, 0);
r(2, 1) = m1(2, 0) * m2(0, 1) + m1(2, 1) * m2(1, 1) + m1(2, 2) * m2(2, 1) + m1(2, 3) * m2(3, 1);
r(2, 2) = m1(2, 0) * m2(0, 2) + m1(2, 1) * m2(1, 2) + m1(2, 2) * m2(2, 2) + m1(2, 3) * m2(3, 2);
r(2, 3) = m1(2, 0) * m2(0, 3) + m1(2, 1) * m2(1, 3) + m1(2, 2) * m2(2, 3) + m1(2, 3) * m2(3, 3);
r(3, 0) = m1(3, 0) * m2(0, 0) + m1(3, 1) * m2(1, 0) + m1(3, 2) * m2(2, 0) + m1(3, 3) * m2(3, 0);
r(3, 1) = m1(3, 0) * m2(0, 1) + m1(3, 1) * m2(1, 1) + m1(3, 2) * m2(2, 1) + m1(3, 3) * m2(3, 1);
r(3, 2) = m1(3, 0) * m2(0, 2) + m1(3, 1) * m2(1, 2) + m1(3, 2) * m2(2, 2) + m1(3, 3) * m2(3, 2);
r(3, 3) = m1(3, 0) * m2(0, 3) + m1(3, 1) * m2(1, 3) + m1(3, 2) * m2(2, 3) + m1(3, 3) * m2(3, 3);
}
static constexpr void map(Phanes::Core::Math::TVector4<T, false>& r, const Phanes::Core::Math::TMatrix4<T, false>& m1, const Phanes::Core::Math::TVector4<T, false>& v)
{
r.x = m1(0, 0) * v.x + m1(0, 1) * v.y + m1(0, 2) * v.z + m1(0, 3) * v.w;
r.y = m1(1, 0) * v.x + m1(1, 1) * v.y + m1(1, 2) * v.z + m1(1, 3) * v.w;
r.z = m1(2, 0) * v.x + m1(2, 1) * v.y + m1(2, 2) * v.z + m1(2, 3) * v.w;
r.w = m1(3, 0) * v.x + m1(3, 1) * v.y + m1(3, 2) * v.z + m1(3, 3) * v.w;
}
};
}

View File

@ -101,17 +101,27 @@ namespace Phanes::Core::Math {
template<RealType T>
std::string ToString(const TMatrix2<T>& m)
{
return "([" + ToString(m(0, 0)) + ", " + ToString(m(0, 1)) + "], [" + ToString(m(1, 0)) + ", " + ToString(m(1, 1)) + "])";
return "([" + ToString(m(0, 0)) + ", " + ToString(m(0, 1)) + "], [" +
ToString(m(1, 0)) + ", " + ToString(m(1, 1)) + "])";
}
template<RealType T, bool S>
std::string ToString(const TMatrix3<T, S>& m)
{
return "([" + ToString(m(0, 0)) + ", " + ToString(m(0, 1)) + ", " + ToString(m(0, 2)) + "], [" + ToString(m(1, 0)) + ", " + ToString(m(1, 1)) + ", " + ToString(m(1, 2)) + "], [" + ToString(m(2, 0)) + ", " + ToString(m(2, 1)) + ", " + ToString(m(2, 2)) + "])";
return "([" + ToString(m(0, 0)) + ", " + ToString(m(0, 1)) + ", " + ToString(m(0, 2)) + "], [" +
ToString(m(1, 0)) + ", " + ToString(m(1, 1)) + ", " + ToString(m(1, 2)) + "], [" +
ToString(m(2, 0)) + ", " + ToString(m(2, 1)) + ", " + ToString(m(2, 2)) + "])";
}
//std::string toString(const Matrix3& v);
template<RealType T, bool S>
std::string ToString(const TMatrix4<T, S>& m)
{
return "([" + ToString(m(0, 0)) + ", " + ToString(m(0, 1)) + ", " + ToString(m(0, 2)) + ", " + ToString(m(0, 3)) + "], [" +
ToString(m(1, 0)) + ", " + ToString(m(1, 1)) + ", " + ToString(m(1, 2)) + ", " + ToString(m(1, 3)) + "], [" +
ToString(m(2, 0)) + ", " + ToString(m(2, 1)) + ", " + ToString(m(2, 2)) + ", " + ToString(m(2, 3)) + "], [" +
ToString(m(3, 0)) + ", " + ToString(m(3, 1)) + ", " + ToString(m(3, 2)) + ", " + ToString(m(3, 3)) + "])";
}
}

View File

@ -518,9 +518,9 @@ namespace Phanes::Core::Math {
template<RealType T, bool S>
bool IsIdentityMatrix(const TMatrix3<T, S>& m1)
{
return (abs(m1(0, 0) - (T)1.0) < P_FLT_INAC && m1(0, 1) < P_FLT_INAC && m1(0, 2) < P_FLT_INAC &&
m1(1, 0) < P_FLT_INAC && abs(m1(1, 1) - (T)1.0) < P_FLT_INAC && m1(1, 2) < P_FLT_INAC &&
m1(2, 0) < P_FLT_INAC && m1(2, 1) < P_FLT_INAC && abs(m1(2, 2) - (T)1.0) < P_FLT_INAC);
return (abs(m1(0, 0) - (T)1.0) < P_FLT_INAC && abs(m1(0, 1)) < P_FLT_INAC && abs(m1(0, 2)) < P_FLT_INAC &&
abs(m1(1, 0)) < P_FLT_INAC && abs(m1(1, 1) - (T)1.0) < P_FLT_INAC && abs(m1(1, 2)) < P_FLT_INAC &&
abs(m1(2, 0)) < P_FLT_INAC && abs(m1(2, 1)) < P_FLT_INAC && abs(m1(2, 2) - (T)1.0) < P_FLT_INAC);
}
} // Phanes::Core::Math

View File

@ -31,6 +31,62 @@ namespace Phanes::Core::Math {
T data[4][4];
};
public:
/// <summary>
/// Default constructor.
/// </summary>
TMatrix4() = default;
/// <summary>
/// Copy constructor
/// </summary>
/// <param name="v"></param>
TMatrix4(const TMatrix4<T, S>& m)
{
this->c0 = m.c0;
this->c1 = m.c1;
this->c2 = m.c2;
this->c3 = m.c3;
}
/// <summary>
/// Construct matrix with values.
/// </summary>
TMatrix4(float n00, float n01, float n02, float n03,
float n10, float n11, float n12, float n13,
float n20, float n21, float n22, float n23,
float n30, float n31, float n32, float n33)
{
this->c0 = TVector4<T, S>(n00, n10, n20, n30);
this->c1 = TVector4<T, S>(n01, n11, n21, n31);
this->c2 = TVector4<T, S>(n02, n12, n22, n32);
this->c3 = TVector4<T, S>(n03, n13, n23, n33);
}
/// <summary>
/// Construct matrix from columns.
/// </summary>
TMatrix4(const TVector4<T, S>& v0, const TVector4<T, S>& v1, const TVector4<T, S>& v2, const TVector4<T, S>& v3)
{
this->c0 = v0;
this->c1 = v1;
this->c2 = v2;
this->c3 = v3;
}
/// <summary>
/// Construct matrix from field of values.
/// </summary>
/// <param name="field"></param>
TMatrix4(T field[4][4])
{
this->c0 = TVector4(field[0]);
this->c1 = TVector4(field[1]);
this->c2 = TVector4(field[2]);
this->c3 = TVector4(field[3]);
}
public:
FORCEINLINE T& operator() (int n, int m)
@ -48,7 +104,7 @@ namespace Phanes::Core::Math {
}
FORCEINLINE const TVector4<T, S>& operator[] (int m) const
{
return (*reinterpret_cast<TVector4<T, S>*>(this->m[m]));
return (*reinterpret_cast<const TVector4<T, S>*>(this->data[m]));
}
};
@ -112,15 +168,7 @@ namespace Phanes::Core::Math {
}
template<RealType T, bool S>
TMatrix4<T, S> operator*= (TMatrix4<T, S>& m1, const TMatrix4<T, S>& m2)
{
m1.c0 *= m2.c0;
m1.c1 *= m2.c1;
m1.c2 *= m2.c2;
m1.c3 *= m2.c3;
return m1;
}
TMatrix4<T, S> operator*= (TMatrix4<T, S>& m1, const TMatrix4<T, S>& m2);
template<RealType T, bool S>
TMatrix4<T, S> operator/= (TMatrix4<T, S>& m1, T s)
@ -134,17 +182,6 @@ namespace Phanes::Core::Math {
return m1;
}
template<RealType T, bool S>
TMatrix4<T, S> operator/= (TMatrix4<T, S>& m1, const TMatrix4<T, S>& m2)
{
m1.c0 /= m2.c0;
m1.c1 /= m2.c1;
m1.c2 /= m2.c2;
m1.c3 /= m2.c3;
return m1;
}
template<RealType T, bool S>
TMatrix4<T, S> operator+ (const TMatrix4<T, S>& m1, T s)
{
@ -196,14 +233,7 @@ namespace Phanes::Core::Math {
}
template<RealType T, bool S>
TMatrix4<T, S> operator* (const TMatrix4<T, S>& m1, const TMatrix4<T, S>& m2)
{
return TMatrix4<T, S>(m1.c0 * m2.c0,
m1.c1 * m2.c1,
m1.c2 * m2.c2,
m1.c3 * m2.c3
);
}
TMatrix4<T, S> operator* (const TMatrix4<T, S>& m1, const TMatrix4<T, S>& m2);
template<RealType T, bool S>
TMatrix4<T, S> operator/ (const TMatrix4<T, S>& m1, T s)
@ -217,24 +247,7 @@ namespace Phanes::Core::Math {
}
template<RealType T, bool S>
TMatrix4<T, S> operator/ (const TMatrix4<T, S>& m1, const TMatrix4<T, S>& m2)
{
return TMatrix4<T, S>(m1.c0 / m2.c0,
m1.c1 / m2.c1,
m1.c2 / m2.c2,
m1.c3 / m2.c3
);
}
template<RealType T, bool S>
TVector4<T, S> operator* (const TMatrix4<T, S>& m1, const TVector4<T, S>& v)
{
return TVector4<T, S>(DotP(m1.c0, v),
DotP(m1.c1, v),
DotP(m1.c2, v),
DotP(m1.c3, v)
);
}
TVector4<T, S> operator* (const TMatrix4<T, S>& m1, const TVector4<T, S>& v);
template<RealType T, bool S>
bool operator== (const TMatrix4<T, S>& m1, const TMatrix4<T, S>& m2)
@ -269,18 +282,18 @@ namespace Phanes::Core::Math {
template<RealType T, bool S>
bool Inverse(TMatrix4<T, S>& a);
bool Inverse(const TMatrix4<T, S>& m, Ref<TMatrix4<T, S>> r);
template<RealType T, bool S>
TMatrix4<T, S> Transpose(const TMatrix4<T, S>& a);
template<RealType T, bool S>
FORCEINLINE bool IsIndentityMatrix(const TMatrix4<T, S>& m1)
FORCEINLINE bool IsIdentityMatrix(const TMatrix4<T, S>& m1)
{
return (abs(m1(0, 0) - (T)1.0) < P_FLT_INAC && abs(m1(0, 1) - (T)0.0) < P_FLT_INAC && abs(m1(0, 2) - (T)0.0) < P_FLT_INAC && abs(m1(0, 3) - (T)0.0) < P_FLT_INAC &&
abs(m1(1, 0) - (T)0.0) < P_FLT_INAC && abs(m1(1, 1) - (T)1.0) < P_FLT_INAC && abs(m1(1, 2) - (T)0.0) < P_FLT_INAC && abs(m1(1, 3) - (T)0.0) < P_FLT_INAC &&
abs(m1(2, 0) - (T)0.0) < P_FLT_INAC && abs(m1(2, 1) - (T)0.0) < P_FLT_INAC && abs(m1(2, 2) - (T)1.0) < P_FLT_INAC && abs(m1(2, 3) - (T)0.0) < P_FLT_INAC &&
abs(m1(3, 0) - (T)0.0) < P_FLT_INAC && abs(m1(3, 1) - (T)0.0) < P_FLT_INAC && abs(m1(3, 2) - (T)1.0) < P_FLT_INAC && abs(m1(3, 3) - (T)0.0) < P_FLT_INAC);
return (abs(m1(0, 0) - (T)1.0) < P_FLT_INAC && abs(m1(0, 1)) < P_FLT_INAC && abs(m1(0, 2)) < P_FLT_INAC && abs(m1(0, 3)) < P_FLT_INAC &&
abs(m1(1, 0)) < P_FLT_INAC && abs(m1(1, 1) - (T)1.0) < P_FLT_INAC && abs(m1(1, 2)) < P_FLT_INAC && abs(m1(1, 3)) < P_FLT_INAC &&
abs(m1(2, 0)) < P_FLT_INAC && abs(m1(2, 1)) < P_FLT_INAC && abs(m1(2, 2) - (T)1.0) < P_FLT_INAC && abs(m1(2, 3)) < P_FLT_INAC &&
abs(m1(3, 0)) < P_FLT_INAC && abs(m1(3, 1)) < P_FLT_INAC && abs(m1(3, 2)) < P_FLT_INAC && abs(m1(3, 3) - (T)0.0) < P_FLT_INAC);
}

View File

@ -25,22 +25,45 @@ namespace Phanes::Core::Math
template<RealType T, bool S>
TMatrix4<T, S> TransposeV(TMatrix4<T, S>& a)
{
return Detail::compute_mat4_transpose<T, S>::map(a, a);
Detail::compute_mat4_transpose<T, S>::map(a, a);
return a;
}
template<RealType T, bool S>
bool Inverse(TMatrix4<T, S>& a)
bool Inverse(const TMatrix4<T, S>& m, Ref<TMatrix4<T, S>> r)
{
TMatrix4<T, S> r;
return Detail::compute_mat4_inv<T, S>::map(r, a);
return r;
return Detail::compute_mat4_inv<T, S>::map(*r, m);
}
template<RealType T, bool S>
TMatrix4<T, S> Transpose(TMatrix4<T, S>& a)
{
TMatrix4<T, S> r;
return Detail::compute_mat4_transpose<T, S>::map(r, a);
Detail::compute_mat4_transpose<T, S>::map(r, a);
return r;
}
template<RealType T, bool S>
TMatrix4<T, S> operator*= (TMatrix4<T, S>& m1, const TMatrix4<T, S>& m2)
{
TMatrix4<T, S> r;
Detail::compute_mat4_mul<T, S>::map(r, m1, m2);
return (m1 = r);
}
template<RealType T, bool S>
TMatrix4<T, S> operator* (const TMatrix4<T, S>& m1, const TMatrix4<T, S>& m2)
{
TMatrix4<T, S> r;
Detail::compute_mat4_mul<T, S>::map(r, m1, m2);
return r;
}
template<RealType T, bool S>
TVector4<T, S> operator* (const TMatrix4<T, S>& m1, const TVector4<T, S>& v)
{
TVector4<T, S> r;
Detail::compute_mat4_mul<T, S>::map(r, m1, v);
return r;
}
}