mirror of
https://github.com/orange-cpp/omath.git
synced 2026-02-13 15:03:27 +00:00
now template
This commit is contained in:
@@ -18,10 +18,10 @@ namespace omath
|
||||
};
|
||||
|
||||
|
||||
class Color final : public Vector4
|
||||
class Color final : public Vector4<float>
|
||||
{
|
||||
public:
|
||||
constexpr Color(float r, float g, float b, float a) : Vector4(r,g,b,a)
|
||||
constexpr Color(const float r, const float g, const float b, const float a) : Vector4(r,g,b,a)
|
||||
{
|
||||
Clamp(0.f, 1.f);
|
||||
}
|
||||
|
||||
@@ -333,21 +333,21 @@ namespace omath
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
|
||||
[[nodiscard]]
|
||||
constexpr static Mat<1, 4, Type, St> MatRowFromVector(const Vector3& vector) noexcept
|
||||
constexpr static Mat<1, 4, Type, St> MatRowFromVector(const Vector3<Type>& vector) noexcept
|
||||
{
|
||||
return {{vector.x, vector.y, vector.z, 1}};
|
||||
}
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
|
||||
[[nodiscard]]
|
||||
constexpr static Mat<4, 1, Type, St> MatColumnFromVector(const Vector3& vector) noexcept
|
||||
constexpr static Mat<4, 1, Type, St> MatColumnFromVector(const Vector3<Type>& vector) noexcept
|
||||
{
|
||||
return {{vector.x}, {vector.y}, {vector.z}, {1}};
|
||||
}
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
|
||||
[[nodiscard]]
|
||||
constexpr Mat<4, 4, Type, St> MatTranslation(const Vector3& diff) noexcept
|
||||
constexpr Mat<4, 4, Type, St> MatTranslation(const Vector3<Type>& diff) noexcept
|
||||
{
|
||||
return
|
||||
{
|
||||
@@ -399,8 +399,8 @@ namespace omath
|
||||
|
||||
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
|
||||
[[nodiscard]]
|
||||
static Mat<4, 4, Type, St> MatCameraView(const Vector3& forward, const Vector3& right, const Vector3& up,
|
||||
const Vector3& cameraOrigin) noexcept
|
||||
static Mat<4, 4, Type, St> MatCameraView(const Vector3<Type>& forward, const Vector3<Type>& right,
|
||||
const Vector3<Type>& up, const Vector3<Type>& cameraOrigin) noexcept
|
||||
{
|
||||
return Mat<4, 4, Type, St>
|
||||
{
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
#include <initializer_list>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include "Vector3.hpp"
|
||||
|
||||
namespace omath
|
||||
{
|
||||
class Vector3;
|
||||
|
||||
class Matrix final
|
||||
{
|
||||
@@ -19,10 +19,10 @@ namespace omath
|
||||
static Matrix ToScreenMatrix(float screenWidth, float screenHeight);
|
||||
|
||||
[[nodiscard]]
|
||||
static Matrix TranslationMatrix(const Vector3& diff);
|
||||
static Matrix TranslationMatrix(const Vector3<float>& diff);
|
||||
|
||||
[[nodiscard]]
|
||||
static Matrix OrientationMatrix(const Vector3& forward, const Vector3& right, const Vector3& up);
|
||||
static Matrix OrientationMatrix(const Vector3<float>& forward, const Vector3<float>& right, const Vector3<float>& up);
|
||||
|
||||
[[nodiscard]]
|
||||
static Matrix ProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
|
||||
|
||||
@@ -16,12 +16,12 @@ namespace omath
|
||||
{
|
||||
}
|
||||
|
||||
Vector3 m_vertex1;
|
||||
Vector3 m_vertex2;
|
||||
Vector3 m_vertex3;
|
||||
Vector3<float> m_vertex1;
|
||||
Vector3<float> m_vertex2;
|
||||
Vector3<float> m_vertex3;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector3 CalculateNormal() const
|
||||
constexpr Vector3<float> CalculateNormal() const
|
||||
{
|
||||
const auto b = SideBVector();
|
||||
const auto a = SideAVector();
|
||||
@@ -41,7 +41,7 @@ namespace omath
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector3 SideAVector() const
|
||||
constexpr Vector3<float> SideAVector() const
|
||||
{
|
||||
return m_vertex1 - m_vertex2;
|
||||
}
|
||||
@@ -61,12 +61,12 @@ namespace omath
|
||||
return std::abs(sideA*sideA + sideB*sideB - hypot*hypot) <= 0.0001f;
|
||||
}
|
||||
[[nodiscard]]
|
||||
constexpr Vector3 SideBVector() const
|
||||
constexpr Vector3<float> SideBVector() const
|
||||
{
|
||||
return m_vertex3 - m_vertex2;
|
||||
}
|
||||
[[nodiscard]]
|
||||
constexpr Vector3 MidPoint() const
|
||||
constexpr Vector3<float> MidPoint() const
|
||||
{
|
||||
return (m_vertex1 + m_vertex2 + m_vertex3) / 3;
|
||||
}
|
||||
|
||||
@@ -8,16 +8,18 @@
|
||||
|
||||
namespace omath
|
||||
{
|
||||
|
||||
template<class Type> requires std::is_arithmetic_v<Type>
|
||||
class Vector2
|
||||
{
|
||||
public:
|
||||
float x = 0.f;
|
||||
float y = 0.f;
|
||||
Type x = static_cast<Type>(0);
|
||||
Type y = static_cast<Type>(0);
|
||||
|
||||
// Constructors
|
||||
constexpr Vector2() = default;
|
||||
|
||||
constexpr Vector2(const float x, const float y) : x(x), y(y) {}
|
||||
constexpr Vector2(const Type& x, const Type& y) : x(x), y(y) {}
|
||||
|
||||
// Equality operators
|
||||
[[nodiscard]]
|
||||
@@ -65,7 +67,7 @@ namespace omath
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector2& operator*=(const float fl)
|
||||
constexpr Vector2& operator*=(const Type& fl)
|
||||
{
|
||||
x *= fl;
|
||||
y *= fl;
|
||||
@@ -73,7 +75,7 @@ namespace omath
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector2& operator/=(const float fl)
|
||||
constexpr Vector2& operator/=(const Type& fl)
|
||||
{
|
||||
x /= fl;
|
||||
y /= fl;
|
||||
@@ -81,7 +83,7 @@ namespace omath
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector2& operator+=(const float fl)
|
||||
constexpr Vector2& operator+=(const Type& fl)
|
||||
{
|
||||
x += fl;
|
||||
y += fl;
|
||||
@@ -89,7 +91,7 @@ namespace omath
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr Vector2& operator-=(const float fl)
|
||||
constexpr Vector2& operator-=(const Type& fl)
|
||||
{
|
||||
x -= fl;
|
||||
y -= fl;
|
||||
@@ -98,45 +100,45 @@ namespace omath
|
||||
}
|
||||
|
||||
// Basic vector operations
|
||||
[[nodiscard]] float DistTo(const Vector2& vOther) const
|
||||
[[nodiscard]] Type DistTo(const Vector2& vOther) const
|
||||
{
|
||||
return std::sqrt(DistToSqr(vOther));
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr float DistToSqr(const Vector2& vOther) const
|
||||
[[nodiscard]] constexpr Type DistToSqr(const Vector2& vOther) const
|
||||
{
|
||||
return (x - vOther.x) * (x - vOther.x) + (y - vOther.y) * (y - vOther.y);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr float Dot(const Vector2& vOther) const
|
||||
[[nodiscard]] constexpr Type Dot(const Vector2& vOther) const
|
||||
{
|
||||
return x * vOther.x + y * vOther.y;
|
||||
}
|
||||
|
||||
#ifndef _MSC_VER
|
||||
[[nodiscard]] constexpr float Length() const
|
||||
[[nodiscard]] constexpr Type& Length() const
|
||||
{
|
||||
return std::hypot(x, y);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector2 Normalized() const
|
||||
{
|
||||
const float len = Length();
|
||||
const Type len = Length();
|
||||
return len > 0.f ? *this / len : *this;
|
||||
}
|
||||
#else
|
||||
[[nodiscard]] float Length() const
|
||||
[[nodiscard]] Type Length() const
|
||||
{
|
||||
return std::hypot(x, y);
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector2 Normalized() const
|
||||
{
|
||||
const float len = Length();
|
||||
const Type len = Length();
|
||||
return len > 0.f ? *this / len : *this;
|
||||
}
|
||||
#endif
|
||||
[[nodiscard]] constexpr float LengthSqr() const
|
||||
[[nodiscard]] constexpr Type LengthSqr() const
|
||||
{
|
||||
return x * x + y * y;
|
||||
}
|
||||
@@ -187,13 +189,13 @@ namespace omath
|
||||
}
|
||||
|
||||
// Sum of elements
|
||||
[[nodiscard]] constexpr float Sum() const
|
||||
[[nodiscard]] constexpr Type Sum() const
|
||||
{
|
||||
return x + y;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr std::tuple<float, float> AsTuple() const
|
||||
constexpr std::tuple<Type, Type> AsTuple() const
|
||||
{
|
||||
return std::make_tuple(x, y);
|
||||
}
|
||||
|
||||
@@ -20,16 +20,17 @@ namespace omath
|
||||
IMPOSSIBLE_BETWEEN_ANGLE,
|
||||
};
|
||||
|
||||
class Vector3 : public Vector2
|
||||
template<class Type> requires std::is_arithmetic_v<Type>
|
||||
class Vector3 : public Vector2<Type>
|
||||
{
|
||||
public:
|
||||
float z = 0.f;
|
||||
constexpr Vector3(const float x, const float y, const float z) : Vector2(x, y), z(z) { }
|
||||
constexpr Vector3() : Vector2() {};
|
||||
Type z = 0.f;
|
||||
constexpr Vector3(const Type& x, const Type& y, const Type& z) : Vector2<Type>(x, y), z(z) { }
|
||||
constexpr Vector3() : Vector2<Type>() {};
|
||||
|
||||
[[nodiscard]] constexpr bool operator==(const Vector3& src) const
|
||||
{
|
||||
return Vector2::operator==(src) && (src.z == z);
|
||||
return Vector2<Type>::operator==(src) && (src.z == z);
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool operator!=(const Vector3& src) const
|
||||
@@ -39,7 +40,7 @@ namespace omath
|
||||
|
||||
constexpr Vector3& operator+=(const Vector3& v)
|
||||
{
|
||||
Vector2::operator+=(v);
|
||||
Vector2<Type>::operator+=(v);
|
||||
z += v.z;
|
||||
|
||||
return *this;
|
||||
@@ -47,7 +48,7 @@ namespace omath
|
||||
|
||||
constexpr Vector3& operator-=(const Vector3& v)
|
||||
{
|
||||
Vector2::operator-=(v);
|
||||
Vector2<Type>::operator-=(v);
|
||||
z -= v.z;
|
||||
|
||||
return *this;
|
||||
@@ -55,7 +56,7 @@ namespace omath
|
||||
|
||||
constexpr Vector3& operator*=(const float fl)
|
||||
{
|
||||
Vector2::operator*=(fl);
|
||||
Vector2<Type>::operator*=(fl);
|
||||
z *= fl;
|
||||
|
||||
return *this;
|
||||
@@ -63,7 +64,7 @@ namespace omath
|
||||
|
||||
constexpr Vector3& operator*=(const Vector3& v)
|
||||
{
|
||||
Vector2::operator*=(v);
|
||||
Vector2<Type>::operator*=(v);
|
||||
z *= v.z;
|
||||
|
||||
return *this;
|
||||
@@ -71,7 +72,7 @@ namespace omath
|
||||
|
||||
constexpr Vector3& operator/=(const Vector3& v)
|
||||
{
|
||||
Vector2::operator/=(v);
|
||||
Vector2<Type>::operator/=(v);
|
||||
z /= v.z;
|
||||
|
||||
return *this;
|
||||
@@ -79,7 +80,7 @@ namespace omath
|
||||
|
||||
constexpr Vector3& operator+=(const float fl)
|
||||
{
|
||||
Vector2::operator+=(fl);
|
||||
Vector2<Type>::operator+=(fl);
|
||||
z += fl;
|
||||
|
||||
return *this;
|
||||
@@ -87,7 +88,7 @@ namespace omath
|
||||
|
||||
constexpr Vector3& operator/=(const float fl)
|
||||
{
|
||||
Vector2::operator/=(fl);
|
||||
Vector2<Type>::operator/=(fl);
|
||||
z /= fl;
|
||||
|
||||
return *this;
|
||||
@@ -95,7 +96,7 @@ namespace omath
|
||||
|
||||
constexpr Vector3& operator-=(const float fl)
|
||||
{
|
||||
Vector2::operator-=(fl);
|
||||
Vector2<Type>::operator-=(fl);
|
||||
z -= fl;
|
||||
|
||||
return *this;
|
||||
@@ -103,7 +104,7 @@ namespace omath
|
||||
|
||||
constexpr Vector3& Abs()
|
||||
{
|
||||
Vector2::Abs();
|
||||
Vector2<Type>::Abs();
|
||||
z = z < 0.f ? -z : z;
|
||||
|
||||
return *this;
|
||||
@@ -116,7 +117,7 @@ namespace omath
|
||||
|
||||
[[nodiscard]] constexpr float Dot(const Vector3& vOther) const
|
||||
{
|
||||
return Vector2::Dot(vOther) + z * vOther.z;
|
||||
return Vector2<Type>::Dot(vOther) + z * vOther.z;
|
||||
}
|
||||
|
||||
#ifndef _MSC_VER
|
||||
@@ -142,7 +143,7 @@ namespace omath
|
||||
#else
|
||||
[[nodiscard]] float Length() const
|
||||
{
|
||||
return std::hypot(x, y, z);
|
||||
return std::hypot(this->x, this->y, z);
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector3 Normalized() const
|
||||
@@ -152,9 +153,9 @@ namespace omath
|
||||
return length != 0 ? *this / length : *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] float Length2D() const
|
||||
[[nodiscard]] Type Length2D() const
|
||||
{
|
||||
return Vector2::Length();
|
||||
return Vector2<Type>::Length();
|
||||
}
|
||||
|
||||
[[nodiscard]] float DistTo(const Vector3& vOther) const
|
||||
@@ -166,51 +167,52 @@ namespace omath
|
||||
|
||||
[[nodiscard]] constexpr float LengthSqr() const
|
||||
{
|
||||
return Vector2::LengthSqr() + z * z;
|
||||
return Vector2<Type>::LengthSqr() + z * z;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator-() const
|
||||
{
|
||||
return {-x, -y, -z};
|
||||
|
||||
return Vector3<Type>{-this->x, -this->y, -z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator+(const Vector3& v) const
|
||||
{
|
||||
return {x + v.x, y + v.y, z + v.z};
|
||||
return {this->x + v.x, this->y + v.y, z + v.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator-(const Vector3& v) const
|
||||
{
|
||||
return {x - v.x, y - v.y, z - v.z};
|
||||
return {this->x - v.x, this->y - v.y, z - v.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator*(const float fl) const
|
||||
{
|
||||
return {x * fl, y * fl, z * fl};
|
||||
return {this->x * fl, this->y * fl, z * fl};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator*(const Vector3& v) const
|
||||
{
|
||||
return {x * v.x, y * v.y, z * v.z};
|
||||
return {this->x * v.x, this->y * v.y, z * v.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator/(const float fl) const
|
||||
{
|
||||
return {x / fl, y / fl, z / fl};
|
||||
return {this->x / fl, this->y / fl, z / fl};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 operator/(const Vector3& v) const
|
||||
{
|
||||
return {x / v.x, y / v.y, z / v.z};
|
||||
return {this->x / v.x, this->y / v.y, z / v.z};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Vector3 Cross(const Vector3 &v) const
|
||||
{
|
||||
return
|
||||
{
|
||||
y * v.z - z * v.y,
|
||||
z * v.x - x * v.z,
|
||||
x * v.y - y * v.x
|
||||
this->y * v.z - z * v.y,
|
||||
z * v.x - this->x * v.z,
|
||||
this->x * v.y - this->y * v.x
|
||||
};
|
||||
}
|
||||
[[nodiscard]] constexpr float Sum() const
|
||||
@@ -239,14 +241,25 @@ namespace omath
|
||||
|
||||
[[nodiscard]] constexpr float Sum2D() const
|
||||
{
|
||||
return Vector2::Sum();
|
||||
return Vector2<Type>::Sum();
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector3 ViewAngleTo(const Vector3& other) const;
|
||||
|
||||
[[nodiscard]] constexpr std::tuple<float, float, float> AsTuple() const
|
||||
{
|
||||
return std::make_tuple(x, y, z);
|
||||
return std::make_tuple(this->x, this->y, z);
|
||||
}
|
||||
|
||||
[[nodiscard]] Vector3 ViewAngleTo(const Vector3 &other) const
|
||||
{
|
||||
const float distance = DistTo(other);
|
||||
const auto delta = other - *this;
|
||||
|
||||
return
|
||||
{
|
||||
angles::RadiansToDegrees(std::asin(delta.z / distance)),
|
||||
angles::RadiansToDegrees(std::atan2(delta.y, delta.x)),
|
||||
0
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -254,9 +267,9 @@ namespace omath
|
||||
namespace std
|
||||
{
|
||||
template<>
|
||||
struct hash<omath::Vector3>
|
||||
struct hash<omath::Vector3<float>>
|
||||
{
|
||||
std::size_t operator()(const omath::Vector3& vec) const noexcept
|
||||
std::size_t operator()(const omath::Vector3<float>& vec) const noexcept
|
||||
{
|
||||
std::size_t hash = 0;
|
||||
constexpr std::hash<float> hasher;
|
||||
|
||||
@@ -8,18 +8,19 @@
|
||||
|
||||
namespace omath
|
||||
{
|
||||
class Vector4 : public Vector3
|
||||
template <class Type>
|
||||
class Vector4 : public Vector3<Type>
|
||||
{
|
||||
public:
|
||||
float w;
|
||||
Type w;
|
||||
|
||||
constexpr Vector4(const float x, const float y, const float z, const float w) : Vector3(x, y, z), w(w) {}
|
||||
constexpr Vector4() : Vector3(), w(0.f) {};
|
||||
constexpr Vector4(const Type& x, const Type& y, const Type& z, const Type& w) : Vector3<Type>(x, y, z), w(w) {}
|
||||
constexpr Vector4() : Vector3<Type>(), w(0) {};
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr bool operator==(const Vector4& src) const
|
||||
{
|
||||
return Vector3::operator==(src) && w == src.w;
|
||||
return Vector3<Type>::operator==(src) && w == src.w;
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
@@ -30,7 +31,7 @@ namespace omath
|
||||
|
||||
constexpr Vector4& operator+=(const Vector4& v)
|
||||
{
|
||||
Vector3::operator+=(v);
|
||||
Vector3<Type>::operator+=(v);
|
||||
w += v.w;
|
||||
|
||||
return *this;
|
||||
@@ -38,7 +39,7 @@ namespace omath
|
||||
|
||||
constexpr Vector4& operator-=(const Vector4& v)
|
||||
{
|
||||
Vector3::operator-=(v);
|
||||
Vector3<Type>::operator-=(v);
|
||||
w -= v.w;
|
||||
|
||||
return *this;
|
||||
@@ -46,7 +47,7 @@ namespace omath
|
||||
|
||||
constexpr Vector4& operator*=(const float scalar)
|
||||
{
|
||||
Vector3::operator*=(scalar);
|
||||
Vector3<Type>::operator*=(scalar);
|
||||
w *= scalar;
|
||||
|
||||
return *this;
|
||||
@@ -54,7 +55,7 @@ namespace omath
|
||||
|
||||
constexpr Vector4& operator*=(const Vector4& v)
|
||||
{
|
||||
Vector3::operator*=(v);
|
||||
Vector3<Type>::operator*=(v);
|
||||
w *= v.w;
|
||||
|
||||
return *this;
|
||||
@@ -62,7 +63,7 @@ namespace omath
|
||||
|
||||
constexpr Vector4& operator/=(const float scalar)
|
||||
{
|
||||
Vector3::operator/=(scalar);
|
||||
Vector3<Type>::operator/=(scalar);
|
||||
w /= scalar;
|
||||
|
||||
return *this;
|
||||
@@ -70,35 +71,38 @@ namespace omath
|
||||
|
||||
constexpr Vector4& operator/=(const Vector4& v)
|
||||
{
|
||||
Vector3::operator/=(v);
|
||||
Vector3<Type>::operator/=(v);
|
||||
w /= v.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr float LengthSqr() const
|
||||
[[nodiscard]] constexpr Type LengthSqr() const
|
||||
{
|
||||
return Vector3::LengthSqr() + w * w;
|
||||
return Vector3<Type>::LengthSqr() + w * w;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr float Dot(const Vector4& vOther) const
|
||||
{
|
||||
return Vector3::Dot(vOther) + w * vOther.w;
|
||||
return Vector3<Type>::Dot(vOther) + w * vOther.w;
|
||||
}
|
||||
|
||||
[[nodiscard]] float Length() const;
|
||||
[[nodiscard]] Vector3<Type> Length() const
|
||||
{
|
||||
return std::sqrt(LengthSqr());
|
||||
}
|
||||
|
||||
constexpr Vector4& Abs()
|
||||
{
|
||||
Vector3::Abs();
|
||||
Vector3<Type>::Abs();
|
||||
w = w < 0.f ? -w : w;
|
||||
|
||||
return *this;
|
||||
}
|
||||
constexpr Vector4& Clamp(const float min, const float max)
|
||||
{
|
||||
x = std::clamp(x, min, max);
|
||||
y = std::clamp(y, min, max);
|
||||
z = std::clamp(z, min, max);
|
||||
this->x = std::clamp(this->x, min, max);
|
||||
this->y = std::clamp(this->y, min, max);
|
||||
this->z = std::clamp(this->z, min, max);
|
||||
|
||||
return *this;
|
||||
}
|
||||
@@ -106,49 +110,49 @@ namespace omath
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator-() const
|
||||
{
|
||||
return {-x, -y, -z, -w};
|
||||
return {-this->x, -this->y, -this->z, -w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator+(const Vector4& v) const
|
||||
{
|
||||
return {x + v.x, y + v.y, z + v.z, w + v.w};
|
||||
return {this->x + v.x, this->y + v.y, this->z + v.z, w + v.w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator-(const Vector4& v) const
|
||||
{
|
||||
return {x - v.x, y - v.y, z - v.z, w - v.w};
|
||||
return {this->x - v.x, this->y - v.y, this->z - v.z, w - v.w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator*(const float scalar) const
|
||||
{
|
||||
return {x * scalar, y * scalar, z * scalar, w * scalar};
|
||||
return {this->x * scalar, this->y * scalar, this->z * scalar, w * scalar};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator*(const Vector4& v) const
|
||||
{
|
||||
return {x * v.x, y * v.y, z * v.z, w * v.w};
|
||||
return {this->x * v.x, this->y * v.y, this->z * v.z, w * v.w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator/(const float scalar) const
|
||||
{
|
||||
return {x / scalar, y / scalar, z / scalar, w / scalar};
|
||||
return {this->x / scalar, this->y / scalar, this->z / scalar, w / scalar};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector4 operator/(const Vector4& v) const
|
||||
{
|
||||
return {x / v.x, y / v.y, z / v.z, w / v.w};
|
||||
return {this->x / v.x, this->y / v.y, this->z / v.z, w / v.w};
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr float Sum() const
|
||||
constexpr Type Sum() const
|
||||
{
|
||||
return Vector3::Sum() + w;
|
||||
return Vector3<Type>::Sum() + w;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -11,14 +11,14 @@ namespace omath::collision
|
||||
class Ray
|
||||
{
|
||||
public:
|
||||
Vector3 start;
|
||||
Vector3 end;
|
||||
Vector3<float> start;
|
||||
Vector3<float> end;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3 DirectionVector() const;
|
||||
Vector3<float> DirectionVector() const;
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3 DirectionVectorNormalized() const;
|
||||
Vector3<float> DirectionVectorNormalized() const;
|
||||
};
|
||||
class LineTracer
|
||||
{
|
||||
@@ -27,12 +27,12 @@ namespace omath::collision
|
||||
|
||||
|
||||
[[nodiscard]]
|
||||
static bool CanTraceLine(const Ray& ray, const Triangle<Vector3>& triangle);
|
||||
static bool CanTraceLine(const Ray& ray, const Triangle<Vector3<float>>& triangle);
|
||||
|
||||
|
||||
// Realization of Möller–Trumbore intersection algorithm
|
||||
// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
|
||||
[[nodiscard]]
|
||||
static Vector3 GetRayHitPoint(const Ray& ray, const Triangle<Vector3>& triangle);
|
||||
static Vector3<float> GetRayHitPoint(const Ray& ray, const Triangle<Vector3<float>>& triangle);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -10,9 +10,9 @@ namespace omath::opengl
|
||||
class Camera final : public projection::Camera<Mat4x4, ViewAngles>
|
||||
{
|
||||
public:
|
||||
Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
|
||||
void LookAt(const Vector3& target) override;
|
||||
void LookAt(const Vector3<float>& target) override;
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix() const override;
|
||||
[[nodiscard]] Mat4x4 CalcProjectionMatrix() const override;
|
||||
};
|
||||
|
||||
@@ -10,9 +10,9 @@
|
||||
|
||||
namespace omath::opengl
|
||||
{
|
||||
constexpr Vector3 kAbsUp = {0, 1, 0};
|
||||
constexpr Vector3 kAbsRight = {1, 0, 0};
|
||||
constexpr Vector3 kAbsForward = {0, 0, -1};
|
||||
constexpr Vector3<float> kAbsUp = {0, 1, 0};
|
||||
constexpr Vector3<float> kAbsRight = {1, 0, 0};
|
||||
constexpr Vector3<float> kAbsForward = {0, 0, -1};
|
||||
|
||||
using Mat4x4 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
|
||||
using Mat3x3 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
namespace omath::opengl
|
||||
{
|
||||
[[nodiscard]]
|
||||
inline Vector3 ForwardVector(const ViewAngles& angles)
|
||||
inline Vector3<float> ForwardVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsForward);
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace omath::opengl
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
inline Vector3 RightVector(const ViewAngles& angles)
|
||||
inline Vector3<float> RightVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsRight);
|
||||
|
||||
@@ -24,14 +24,14 @@ namespace omath::opengl
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
inline Vector3 UpVector(const ViewAngles& angles)
|
||||
inline Vector3<float> UpVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsUp);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
}
|
||||
|
||||
[[nodiscard]] inline Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3& cam_origin)
|
||||
[[nodiscard]] inline Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
|
||||
{
|
||||
return MatCameraView<float, MatStoreType::COLUMN_MAJOR>(-ForwardVector(angles), RightVector(angles), UpVector(angles), cam_origin);
|
||||
}
|
||||
|
||||
@@ -10,9 +10,9 @@ namespace omath::source
|
||||
class Camera final : public projection::Camera<Mat4x4, ViewAngles>
|
||||
{
|
||||
public:
|
||||
Camera(const Vector3& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
|
||||
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
|
||||
void LookAt(const Vector3& target) override;
|
||||
void LookAt(const Vector3<float>& target) override;
|
||||
[[nodiscard]] Mat4x4 CalcViewMatrix() const override;
|
||||
[[nodiscard]] Mat4x4 CalcProjectionMatrix() const override;
|
||||
};
|
||||
|
||||
@@ -9,9 +9,9 @@
|
||||
#include <omath/ViewAngles.hpp>
|
||||
namespace omath::source
|
||||
{
|
||||
constexpr Vector3 kAbsUp = {0, 0, 1};
|
||||
constexpr Vector3 kAbsRight = {0, -1, 0};
|
||||
constexpr Vector3 kAbsForward = {1, 0, 0};
|
||||
constexpr Vector3<float> kAbsUp = {0, 0, 1};
|
||||
constexpr Vector3<float> kAbsRight = {0, -1, 0};
|
||||
constexpr Vector3<float> kAbsForward = {1, 0, 0};
|
||||
|
||||
using Mat4x4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
using Mat3x3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
namespace omath::source
|
||||
{
|
||||
[[nodiscard]]
|
||||
inline Vector3 ForwardVector(const ViewAngles& angles)
|
||||
inline Vector3<float> ForwardVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsForward);
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace omath::source
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
inline Vector3 RightVector(const ViewAngles& angles)
|
||||
inline Vector3<float> RightVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsRight);
|
||||
|
||||
@@ -23,14 +23,14 @@ namespace omath::source
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
inline Vector3 UpVector(const ViewAngles& angles)
|
||||
inline Vector3<float> UpVector(const ViewAngles& angles)
|
||||
{
|
||||
const auto vec = MatRotation(angles) * MatColumnFromVector(kAbsUp);
|
||||
|
||||
return {vec.At(0, 0), vec.At(1, 0), vec.At(2, 0)};
|
||||
}
|
||||
|
||||
[[nodiscard]] inline Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3& cam_origin)
|
||||
[[nodiscard]] inline Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin)
|
||||
{
|
||||
return MatCameraView(ForwardVector(angles), RightVector(angles), UpVector(angles), cam_origin);
|
||||
}
|
||||
|
||||
@@ -14,12 +14,17 @@ namespace omath::pathfinding
|
||||
{
|
||||
public:
|
||||
[[nodiscard]]
|
||||
static std::vector<Vector3> FindPath(const Vector3& start, const Vector3& end, const NavigationMesh& navMesh);
|
||||
static std::vector<Vector3<float>> FindPath(const Vector3<float>& start, const Vector3<float>& end,
|
||||
const NavigationMesh& navMesh);
|
||||
|
||||
private:
|
||||
[[nodiscard]]
|
||||
static std::vector<Vector3> ReconstructFinalPath(const std::unordered_map<Vector3, PathNode>& closedList, const Vector3& current);
|
||||
static std::vector<Vector3<float>>
|
||||
ReconstructFinalPath(const std::unordered_map<Vector3<float>, PathNode>& closedList,
|
||||
const Vector3<float>& current);
|
||||
|
||||
[[nodiscard]]
|
||||
static auto GetPerfectNode(const std::unordered_map<Vector3, PathNode>& openList, const Vector3& endVertex);
|
||||
static auto GetPerfectNode(const std::unordered_map<Vector3<float>, PathNode>& openList,
|
||||
const Vector3<float>& endVertex);
|
||||
};
|
||||
}
|
||||
} // namespace omath::pathfinding
|
||||
|
||||
@@ -22,17 +22,17 @@ namespace omath::pathfinding
|
||||
public:
|
||||
|
||||
[[nodiscard]]
|
||||
std::expected<Vector3, std::string> GetClosestVertex(const Vector3& point) const;
|
||||
std::expected<Vector3<float>, std::string> GetClosestVertex(const Vector3<float>& point) const;
|
||||
|
||||
|
||||
[[nodiscard]]
|
||||
const std::vector<Vector3>& GetNeighbors(const Vector3& vertex) const;
|
||||
const std::vector<Vector3<float>>& GetNeighbors(const Vector3<float>& vertex) const;
|
||||
|
||||
[[nodiscard]]
|
||||
bool Empty() const;
|
||||
[[nodiscard]] std::vector<uint8_t> Serialize() const;
|
||||
void Deserialize(const std::vector<uint8_t>& raw);
|
||||
|
||||
std::unordered_map<Vector3, std::vector<Vector3>> m_verTextMap;
|
||||
std::unordered_map<Vector3<float>, std::vector<Vector3<float>>> m_verTextMap;
|
||||
};
|
||||
}
|
||||
@@ -13,7 +13,7 @@ namespace omath::projectile_prediction
|
||||
{
|
||||
public:
|
||||
[[nodiscard]]
|
||||
virtual std::optional<Vector3> MaybeCalculateAimPoint(const Projectile& projectile,
|
||||
virtual std::optional<Vector3<float>> MaybeCalculateAimPoint(const Projectile& projectile,
|
||||
const Target& target) const = 0;
|
||||
virtual ~ProjPredEngine() = default;
|
||||
};
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace omath::projectile_prediction
|
||||
class ProjPredEngineAVX2 final : public ProjPredEngine
|
||||
{
|
||||
public:
|
||||
[[nodiscard]] std::optional<Vector3> MaybeCalculateAimPoint(const Projectile& projectile,
|
||||
[[nodiscard]] std::optional<Vector3<float>> MaybeCalculateAimPoint(const Projectile& projectile,
|
||||
const Target& target) const override;
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace omath::projectile_prediction
|
||||
~ProjPredEngineAVX2() override = default;
|
||||
|
||||
private:
|
||||
[[nodiscard]] static std::optional<float> CalculatePitch(const Vector3& projOrigin, const Vector3& targetPos,
|
||||
[[nodiscard]] static std::optional<float> CalculatePitch(const Vector3<float>& projOrigin, const Vector3<float>& targetPos,
|
||||
float bulletGravity, float v0, float time);
|
||||
const float m_gravityConstant;
|
||||
const float m_simulationTimeStep;
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace omath::projectile_prediction
|
||||
float distanceTolerance);
|
||||
|
||||
[[nodiscard]]
|
||||
std::optional<Vector3> MaybeCalculateAimPoint(const Projectile& projectile,
|
||||
std::optional<Vector3<float>> MaybeCalculateAimPoint(const Projectile& projectile,
|
||||
const Target& target) const override;
|
||||
|
||||
private:
|
||||
@@ -31,11 +31,11 @@ namespace omath::projectile_prediction
|
||||
|
||||
[[nodiscard]]
|
||||
std::optional<float> MaybeCalculateProjectileLaunchPitchAngle(const Projectile& projectile,
|
||||
const Vector3& targetPosition) const;
|
||||
const Vector3<float>& targetPosition) const;
|
||||
|
||||
|
||||
[[nodiscard]]
|
||||
bool IsProjectileReachedTarget(const Vector3& targetPosition, const Projectile& projectile, float pitch,
|
||||
bool IsProjectileReachedTarget(const Vector3<float>& targetPosition, const Projectile& projectile, float pitch,
|
||||
float time) const;
|
||||
};
|
||||
} // namespace omath::projectile_prediction
|
||||
|
||||
@@ -12,9 +12,9 @@ namespace omath::projectile_prediction
|
||||
public:
|
||||
|
||||
[[nodiscard]]
|
||||
Vector3 PredictPosition(float pitch, float yaw, float time, float gravity) const;
|
||||
Vector3<float> PredictPosition(float pitch, float yaw, float time, float gravity) const;
|
||||
|
||||
Vector3 m_origin;
|
||||
Vector3<float> m_origin;
|
||||
float m_launchSpeed{};
|
||||
float m_gravityScale{};
|
||||
};
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace omath::projectile_prediction
|
||||
public:
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector3 PredictPosition(const float time, const float gravity) const
|
||||
constexpr Vector3<float> PredictPosition(const float time, const float gravity) const
|
||||
{
|
||||
auto predicted = m_origin + m_velocity * time;
|
||||
|
||||
@@ -22,8 +22,8 @@ namespace omath::projectile_prediction
|
||||
return predicted;
|
||||
}
|
||||
|
||||
Vector3 m_origin;
|
||||
Vector3 m_velocity;
|
||||
Vector3<float> m_origin;
|
||||
Vector3<float> m_velocity;
|
||||
bool m_isAirborne{};
|
||||
};
|
||||
}
|
||||
@@ -31,7 +31,7 @@ namespace omath::projection
|
||||
{
|
||||
public:
|
||||
virtual ~Camera() = default;
|
||||
Camera(const Vector3& position, const ViewAnglesType& viewAngles, const ViewPort& viewPort,
|
||||
Camera(const Vector3<float>& position, const ViewAnglesType& viewAngles, const ViewPort& viewPort,
|
||||
const FieldOfView& fov, const float near, const float far) :
|
||||
m_viewPort(viewPort), m_fieldOfView(fov), m_farPlaneDistance(far), m_nearPlaneDistance(near),
|
||||
m_viewAngles(viewAngles), m_origin(position)
|
||||
@@ -39,7 +39,7 @@ namespace omath::projection
|
||||
|
||||
}
|
||||
|
||||
virtual void LookAt(const Vector3& target) = 0;
|
||||
virtual void LookAt(const Vector3<float>& target) = 0;
|
||||
|
||||
[[nodiscard]] virtual Mat4x4Type CalcViewMatrix() const = 0;
|
||||
|
||||
@@ -74,7 +74,7 @@ namespace omath::projection
|
||||
m_viewProjectionMatrix = CalcViewProjectionMatrix();
|
||||
}
|
||||
|
||||
void SetOrigin(const Vector3& origin)
|
||||
void SetOrigin(const Vector3<float>& origin)
|
||||
{
|
||||
m_origin = origin;
|
||||
m_viewProjectionMatrix = CalcViewProjectionMatrix();
|
||||
@@ -106,12 +106,12 @@ namespace omath::projection
|
||||
return m_viewAngles;
|
||||
}
|
||||
|
||||
[[nodiscard]] const Vector3& GetOrigin() const
|
||||
[[nodiscard]] const Vector3<float>& GetOrigin() const
|
||||
{
|
||||
return m_origin;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<Vector3, Error> WorldToScreen(const Vector3& worldPosition) const
|
||||
[[nodiscard]] std::expected<Vector3<float>, Error> WorldToScreen(const Vector3<float>& worldPosition) const
|
||||
{
|
||||
if (!m_viewProjectionMatrix.has_value())
|
||||
m_viewProjectionMatrix = CalcViewProjectionMatrix();
|
||||
@@ -143,7 +143,7 @@ namespace omath::projection
|
||||
|
||||
|
||||
ViewAnglesType m_viewAngles;
|
||||
Vector3 m_origin;
|
||||
Vector3<float> m_origin;
|
||||
|
||||
private:
|
||||
template<class Type>
|
||||
|
||||
Reference in New Issue
Block a user