Bug fixes.

This commit is contained in:
THoehne 2024-08-27 13:29:30 +02:00
parent 5bb287425b
commit 7ff9493708
10 changed files with 188 additions and 124 deletions

View File

@ -8,7 +8,7 @@
#include "Core/public/Math/IntVector2.hpp" #include "Core/public/Math/IntVector2.hpp"
#include "Core/public/Math/IntVector3.hpp" #include "Core/public/Math/IntVector3.hpp"
// #include "Core/public/Math/IntVector4.h" #include "Core/public/Math/IntVector4.hpp"
#ifndef P_DEBUG #ifndef P_DEBUG
#pragma warning(disable : 4244) #pragma warning(disable : 4244)
@ -29,9 +29,9 @@ namespace Phanes::Core::Math {
*/ */
template<IntType T> template<IntType T>
struct TIntPoint2 : public TIntVector2<T> { struct TIntPoint2 : public TIntVector2<T, false> {
using TIntVector2<T>::TIntVector2; using TIntVector2<T, false>::TIntVector2;
/** /**
* Creates IntPoint2 from IntPoint3's xy * Creates IntPoint2 from IntPoint3's xy
@ -59,12 +59,6 @@ namespace Phanes::Core::Math {
//} //}
}; };
template<IntType T, RealType Rt>
Rt Distance(const TIntPoint2<T>& p1, const TIntPoint2<T>& p2)
{
return Magnitude(p2 - p1);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -74,9 +68,9 @@ namespace Phanes::Core::Math {
template<IntType T> template<IntType T>
struct TIntPoint3 : public TIntVector3<T> { struct TIntPoint3 : public TIntVector3<T, false> {
using TIntVector3<T>::TIntVector3; using TIntVector3<T, false>::TIntVector3;
/** /**
* Creates IntPoint3 from IntPoint2's xy and zero * Creates IntPoint3 from IntPoint2's xy and zero
@ -105,12 +99,6 @@ namespace Phanes::Core::Math {
//} //}
}; };
template<IntType T, RealType Rt>
Rt Distance(const TIntPoint3<T>& p1, const TIntPoint3<T>& p2)
{
return Magnitude(p2 - p1);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -119,41 +107,39 @@ namespace Phanes::Core::Math {
*/ */
//template<typename T> template<IntType T>
//struct TIntPoint4 : public TIntVector4<T> { struct TIntPoint4 : public TIntVector4<T, false> {
// static_assert(std::is_integral_v(T), "T must be an integer type.");
// using IntVector4<T>::IntVector4; using TIntVector4<T, false>::TIntVector4;
// /** /**
// * Creates IntPoint4 from IntPoint2's xy and the last two zero * Creates IntPoint4 from IntPoint2's xy and the last two zero
// * *
// * @param a IntPoint2 one * @param a IntPoint2 one
// */ */
// PHANES_CORE_API IntPoint4(const IntPoint2<T>& a) TIntPoint4(const TIntPoint2<T>& a)
// { {
// this->components[0] = a.components[0]; this->data[0] = a.data[0];
// this->components[1] = a.components[1]; this->data[1] = a.data[1];
// this->components[2] = 0; this->data[2] = 0;
// this->components[3] = 0; this->data[3] = 0;
// } }
// /** /**
// * Creates IntPoint4 from IntPoint3's xyz and zero * Creates IntPoint4 from IntPoint3's xyz and zero
// * *
// * @param a IntPoint3 one * @param a IntPoint3 one
// */ */
// PHANES_CORE_API IntPoint4(const IntPoint3<T>& a)
// {
// this->components[0] = a.components[0];
// this->components[1] = a.components[1];
// this->components[2] = a.components[2];
// this->components[3] = 0;
// }
//};
TIntPoint4(const TIntPoint3<T>& a)
{
this->data[0] = a.data[0];
this->data[1] = a.data[1];
this->data[2] = a.data[2];
this->data[3] = 0;
}
};
} // phanes::core::math::coretypes } // phanes::core::math::coretypes

View File

@ -352,6 +352,7 @@ namespace Phanes::Core::Math {
// IntVector3 static function implementation // // IntVector3 static function implementation //
// ============================================== // // ============================================== //
/** /**
* Dot product of two vectors * Dot product of two vectors
* *

View File

@ -441,6 +441,7 @@ namespace Phanes::Core::Math {
// TIntVector4 functions // // TIntVector4 functions //
// ========================= // // ========================= //
template<IntType T> template<IntType T>
void Set(TIntVector4<T, false>& v1, TIntVector4<T, false>& v2) void Set(TIntVector4<T, false>& v1, TIntVector4<T, false>& v2)
{ {

View File

@ -22,11 +22,11 @@ namespace Phanes::Core::Math
/** Direction of line */ /** Direction of line */
TVector3<Real> direction; TVector3<Real, false> direction;
/** Base point of line */ /** Base point of line */
TVector3<Real> base; TVector3<Real, false> base;
public: public:
@ -36,7 +36,7 @@ namespace Phanes::Core::Math
* @param(p) Base of line * @param(p) Base of line
*/ */
TLine(const TVector3<T>& direction, const TVector3<T>& p) : direction(direction), base(p) {}; TLine(const TVector3<T, false>& direction, const TVector3<T, false>& p) : direction(direction), base(p) {};
}; };

View File

@ -27,9 +27,7 @@ namespace Phanes::Core::Math {
template<RealType T> struct TColor; template<RealType T> struct TColor;
template<RealType T> struct TLinearColor; template<RealType T> struct TLinearColor;
template<RealType T> struct TRay;
template<RealType T> struct TLine; template<RealType T> struct TLine;
template<RealType T> struct TPlane;
template<RealType T> struct TQuaternion; template<RealType T> struct TQuaternion;
template<RealType T> struct TTransform; template<RealType T> struct TTransform;
template<RealType T> struct TPoint2; template<RealType T> struct TPoint2;
@ -47,11 +45,50 @@ namespace Phanes::Core::Math {
template<IntType T, bool S> struct TIntVector2; template<IntType T, bool S> struct TIntVector2;
template<IntType T, bool S> struct TIntVector3; template<IntType T, bool S> struct TIntVector3;
template<IntType T, bool S> struct TIntVector4; template<IntType T, bool S> struct TIntVector4;
template<RealType T, bool S> struct TPlane;
template<RealType T, bool S> struct TRay;
/** /**
* Specific instantiation of forward declarations. * Specific instantiation of forward declarations.
*/ */
// TPoint2
typedef TPoint2<float> Point2;
typedef TPoint2<float> Point2f;
typedef TPoint2<double> Point2d;
// TPoint3
typedef TPoint3<float> Point3;
typedef TPoint3<float> Point3f;
typedef TPoint3<double> Point3d;
// TPoint4
typedef TPoint4<float> Point4;
typedef TPoint4<float> Point4f;
typedef TPoint4<double> Point4d;
// TIntPoint2
typedef TIntPoint2<int> IntPoint2;
typedef TIntPoint2<int> IntPoint2i;
typedef TIntPoint2<long> IntPoint2l;
// TIntPoint3
typedef TIntPoint3<int> IntPoint3;
typedef TIntPoint3<int> IntPoint3i;
typedef TIntPoint3<long> IntPoint3l;
// TIntPoint4
typedef TIntPoint4<int> IntPoint4;
typedef TIntPoint4<int> IntPoint4i;
typedef TIntPoint4<long> IntPoint4l;
// IntVetor2 // IntVetor2
typedef TIntVector2<int, false> IntVector2; typedef TIntVector2<int, false> IntVector2;
@ -123,7 +160,7 @@ namespace Phanes::Core::Math {
typedef TMatrix3<float, SIMD::use_simd<float, 3, true>::value> Matrix3Reg; typedef TMatrix3<float, SIMD::use_simd<float, 3, true>::value> Matrix3Reg;
typedef TMatrix3<float, SIMD::use_simd<float, 3, true>::value> Matrix3Regf; typedef TMatrix3<float, SIMD::use_simd<float, 3, true>::value> Matrix3Regf;
typedef TMatrix3<double, SIMD::use_simd<double, 3, true>::value> Matrix3Regd; typedef TMatrix3<double, SIMD::use_simd<double, 3, true>::value> Matrix3Regd;
typedef TMatrix3<double, SIMD::use_simd<double, 3, false>::value> Matrix3Regf64; typedef TMatrix3<double, SIMD::use_simd<double, 3, true>::value> Matrix3Regf64;
// Matrix4 // Matrix4
@ -134,7 +171,19 @@ namespace Phanes::Core::Math {
typedef TMatrix3<float, SIMD::use_simd<float, 4, true>::value> Matrix4Reg; typedef TMatrix3<float, SIMD::use_simd<float, 4, true>::value> Matrix4Reg;
typedef TMatrix3<float, SIMD::use_simd<float, 4, true>::value> Matrix4Regf; typedef TMatrix3<float, SIMD::use_simd<float, 4, true>::value> Matrix4Regf;
typedef TMatrix3<double, SIMD::use_simd<double, 4, true>::value> Matrix4Regd; typedef TMatrix3<double, SIMD::use_simd<double, 4, true>::value> Matrix4Regd;
typedef TMatrix3<double, SIMD::use_simd<double, 4, false>::value> Matrix4Regf64; typedef TMatrix3<double, SIMD::use_simd<double, 4, true>::value> Matrix4Regf64;
// TPlane
typedef TPlane<float, false> Plane;
typedef TPlane<float, false> Planef;
typedef TPlane<double, false> Planed;
typedef TPlane<float, SIMD::use_simd<float, 4, true>::value> PlaneReg;
typedef TPlane<float, SIMD::use_simd<double, 4, true>::value> PlaneRegd;
} // Phanes::Core::Math::coretypes } // Phanes::Core::Math::coretypes

View File

@ -28,10 +28,9 @@ namespace Phanes::Core::Math {
*/ */
template<RealType T> template<RealType T>
struct TPoint2 : public TVector2<T> { struct TPoint2 : public TVector2<T, false> {
static_assert(std::is_floating_point_v<T>, "T must be a floating point");
using TVector2<T>::TVector2; using TVector2<T, false>::TVector2;
using Real = T; using Real = T;
@ -84,10 +83,9 @@ namespace Phanes::Core::Math {
template<RealType T> template<RealType T>
struct TPoint3 : public TVector3<T> { struct TPoint3 : public TVector3<T, false> {
static_assert(std::is_floating_point_v(T), "T must be a floating point");
using TVector3<T>::TVector3; using TVector3<T, false>::TVector3;
using Real = T; using Real = T;
@ -141,52 +139,54 @@ namespace Phanes::Core::Math {
*/ */
//template<RealType T> template<RealType T>
//struct TPoint4 : public TVector4<T> { struct TPoint4 : public TVector4<T, false> {
// static_assert(std::is_floating_point_v(T), "T must be a floating point");
using TVector4<T, false>::TVector4;
/**
* Creates Point4 from Point2's xy and the last two zero
*
* @param a Point2 one
*/
TPoint4(const TPoint2<T>& p)
{
this->x = p.x;
this->y = p.y;
this->z = 0;
this->w = 0;
}
/**
* Creates Point4 from Point3's xyz and zero
*
* @param a Point3 one
*/
TPoint4(const TPoint3<T>& p)
{
this->x = p.x;
this->y = p.y;
this->z = p.z;
this->w = 0;
}
};
// using TVector4<T>::TVector4; /**
* Calculates distance between two points.
*
* @param(p1) Point one
* @param(p2) Point two
*
* @return Distance between two points.
*/
// /** template<RealType T>
// * Creates Point4 from Point2's xy and the last two zero T Distance(const TPoint4<T>& p1, const TPoint4<T>& p2)
// * {
// * @param a Point2 one return Magnitude(p2 - p1);
// */ }
// TPoint4(const TPoint2<T>& p)
// {
// this->x = p.x;
// this->y = p.y;
// this->z = 0;
// this->w = 0;
// }
// /**
// * Creates Point4 from Point3's xyz and zero
// *
// * @param a Point3 one
// */
// TPoint4(const TPoint3<T>& p)
// {
// this->x = p.x;
// this->y = p.y;
// this->z = p.z;
// this->w = 0;
// }
//};
///**
// * Calculates distance between two points.
// *
// * @param(p1) Point one
// * @param(p2) Point two
// *
// * @return Distance between two points.
// */
//template<RealType T>
//T Distance(const TPoint4<T>& p1, const TPoint4<T>& p2);
} // phanes::core::math::coretypes } // phanes::core::math::coretypes

View File

@ -10,24 +10,24 @@ namespace Phanes::Core::Math
// Ray with origin and direction (L = p + t * v) // Ray with origin and direction (L = p + t * v)
template<RealType T> template<RealType T, bool S>
struct TRay struct TRay
{ {
public: public:
using Real = T; using Real = T;
TVector3<Real> origin; TVector3<Real, S> origin;
TVector3<Real> direction; TVector3<Real, S> direction;
public: public:
/** Default constructor */ /** Default constructor */
TRay() = default; TRay() = default;
/** Copy constructor */ /** Copy constructor */
TRay(const TRay<Real>& r) : direction(r.direction), origin(r.origin) {}; TRay(const TRay<Real, S>& r) : direction(r.direction), origin(r.origin) {};
/** Move constructor */ /** Move constructor */
TRay(TRay<Real>&& r) : direction(std::move(r.direction)), origin(std::move(r.origin)) {}; TRay(TRay<Real, S>&& r) : direction(std::move(r.direction)), origin(std::move(r.origin)) {};
/** /**
* Construct ray from origin and direction. * Construct ray from origin and direction.
@ -36,7 +36,7 @@ namespace Phanes::Core::Math
* @param(origin) Origin * @param(origin) Origin
*/ */
TRay(const TVector3<Real>& direction, const TVector3<Real>& origin) : direction(direction), origin(origin) {}; TRay(const TVector3<Real, S>& direction, const TVector3<Real, S>& origin) : direction(direction), origin(origin) {};
}; };
@ -55,7 +55,7 @@ namespace Phanes::Core::Math
*/ */
template<RealType T> template<RealType T>
FORCEINLINE bool operator== (const TRay<T>& r1, const TRay<T>& r2) FORCEINLINE bool operator== (const TRay<T, false>& r1, const TRay<T, false>& r2)
{ {
return (r1.origin == r2.origin && r1.direction == r2.direction); return (r1.origin == r2.origin && r1.direction == r2.direction);
} }
@ -70,7 +70,7 @@ namespace Phanes::Core::Math
*/ */
template<RealType T> template<RealType T>
FORCEINLINE bool operator== (const TRay<T>& r1, const TRay<T>& r2) FORCEINLINE bool operator!= (const TRay<T, false>& r1, const TRay<T, false>& r2)
{ {
return (r1.origin != r2.origin || r1.direction != r2.direction); return (r1.origin != r2.origin || r1.direction != r2.direction);
} }
@ -89,8 +89,8 @@ namespace Phanes::Core::Math
* @return Point at t * @return Point at t
*/ */
template<RealType T> template<RealType T, bool S>
TVector3<T> PointAt(const TRay<T>& r1, T t) TVector3<T, S> PointAt(const TRay<T, S>& r1, T t)
{ {
return r1.origin + r1.direction * t; return r1.origin + r1.direction * t;
} }
@ -104,8 +104,8 @@ namespace Phanes::Core::Math
* @return parameter t * @return parameter t
*/ */
template<RealType T> template<RealType T, bool S>
TVector3<T> GetParameter(const TRay<T>& r1, const TVector3<T>& p1) TVector3<T, S> GetParameter(const TRay<T, S>& r1, const TVector3<T, S>& p1)
{ {
return DotP((p1 - r1.origin), r1.direction); return DotP((p1 - r1.origin), r1.direction);
} }
@ -119,8 +119,8 @@ namespace Phanes::Core::Math
* @return True, if both rays point in the same direction, false if not. * @return True, if both rays point in the same direction, false if not.
*/ */
template<RealType T> template<RealType T, bool S>
inline bool SameDirection(const TRay<T>& r1, const TRay<T>& r2) inline bool SameDirection(const TRay<T, S>& r1, const TRay<T, S>& r2)
{ {
return (r1.direction == r1.direction); return (r1.direction == r1.direction);
} }

View File

@ -785,16 +785,16 @@ namespace Phanes::Core::Math::Detail
// Second column // Second column
__m128 tmp0 = _mm_mul_ps(m1.c0.data, m2.c1.data); tmp0 = _mm_mul_ps(m1.c0.data, m2.c1.data);
__m128 tmp1 = _mm_mul_ps(m1.c1.data, m2.c1.data); tmp1 = _mm_mul_ps(m1.c1.data, m2.c1.data);
__m128 tmp2 = _mm_mul_ps(m1.c2.data, m2.c1.data); tmp2 = _mm_mul_ps(m1.c2.data, m2.c1.data);
r.c1.data = _mm_add_ps(_mm_add_ps(tmp0, tmp1), tmp2); r.c1.data = _mm_add_ps(_mm_add_ps(tmp0, tmp1), tmp2);
// Third column // Third column
__m128 tmp0 = _mm_mul_ps(m1.c0.data, m2.c2.data); tmp0 = _mm_mul_ps(m1.c0.data, m2.c2.data);
__m128 tmp1 = _mm_mul_ps(m1.c1.data, m2.c2.data); tmp1 = _mm_mul_ps(m1.c1.data, m2.c2.data);
__m128 tmp2 = _mm_mul_ps(m1.c2.data, m2.c2.data); tmp2 = _mm_mul_ps(m1.c2.data, m2.c2.data);
r.c2.data = _mm_add_ps(_mm_add_ps(tmp0, tmp1), tmp2); r.c2.data = _mm_add_ps(_mm_add_ps(tmp0, tmp1), tmp2);
} }

View File

@ -202,4 +202,7 @@ namespace Phanes::Core::Math
{ {
return --v1; return --v1;
} }
} }

View File

@ -1246,5 +1246,29 @@ namespace MatrixTests
0.356495f, -0.287009f, -0.024169f, 0.033232f)); 0.356495f, -0.287009f, -0.024169f, 0.033232f));
EXPECT_TRUE(PMath::Transpose(m0) == PMath::Matrix4(1.0f, 2.0f, 2.0f, 8.0f, 5.0f, 6.0f, -3.0f, -4.0f, 3.0f, 4.0f, 5.0f, 6.0f, 4.0f, 1.0f, 3.0f, -2.0f)); EXPECT_TRUE(PMath::Transpose(m0) == PMath::Matrix4(1.0f, 2.0f, 2.0f, 8.0f, 5.0f, 6.0f, -3.0f, -4.0f, 3.0f, 4.0f, 5.0f, 6.0f, 4.0f, 1.0f, 3.0f, -2.0f));
}
}
namespace Misc
{
TEST(Point, FloatPoint)
{
EXPECT_FLOAT_EQ(PMath::Distance(PMath::Point2(7.0f, 3.0f), PMath::Point2(4.0f, -1.0f)), 5.0f);
EXPECT_FLOAT_EQ(PMath::Distance(PMath::Point3(7.0f, 3.0f, 4.0f), PMath::Point3(4.0f, -1.0f, 3.0f)), 5.099019f);
EXPECT_FLOAT_EQ(PMath::Distance(PMath::Point4(7.0f, 3.0f, 4.0f, 5.0f), PMath::Point4(4.0f, -1.0f, 3.0f, -5.0f)), 11.224972f);
}
}
namespace Plane
{
TEST(Plane, OperatorTests)
{
PMath::Plane pl1;
PMath::Plane pl2;
} }
} }