changed code style

This commit is contained in:
2025-05-03 20:31:59 +03:00
parent be3fae63b8
commit df6d75e554
70 changed files with 1258 additions and 1312 deletions

View File

@@ -11,7 +11,7 @@
namespace omath::primitives
{
[[nodiscard]]
std::array<Triangle<Vector3<float>>, 12> CreateBox(const Vector3<float>& top, const Vector3<float>& bottom,
const Vector3<float>& dirForward, const Vector3<float>& dirRight,
std::array<Triangle<Vector3<float>>, 12> create_box(const Vector3<float>& top, const Vector3<float>& bottom,
const Vector3<float>& dir_forward, const Vector3<float>& dir_right,
float ratio = 4.f);
}

View File

@@ -3,10 +3,9 @@
//
#pragma once
#include "omath/angles.hpp"
#include <algorithm>
#include <utility>
#include "omath/angles.hpp"
namespace omath
{
@@ -17,14 +16,14 @@ namespace omath
};
template<class Type = float, Type min = Type(0), Type max = Type(360), AngleFlags flags = AngleFlags::Normalized>
requires std::is_arithmetic_v<Type>
requires std::is_arithmetic_v<Type>
class Angle
{
Type m_angle;
constexpr explicit Angle(const Type& degrees)
{
if constexpr (flags == AngleFlags::Normalized)
m_angle = angles::WrapAngle(degrees, min, max);
m_angle = angles::wrap_angle(degrees, min, max);
else if constexpr (flags == AngleFlags::Clamped)
m_angle = std::clamp(degrees, min, max);
@@ -37,17 +36,17 @@ namespace omath
public:
[[nodiscard]]
constexpr static Angle FromDegrees(const Type& degrees)
constexpr static Angle from_degrees(const Type& degrees)
{
return Angle{degrees};
}
constexpr Angle() : m_angle(0)
constexpr Angle(): m_angle(0)
{
}
[[nodiscard]]
constexpr static Angle FromRadians(const Type& degrees)
constexpr static Angle from_radians(const Type& degrees)
{
return Angle{angles::RadiansToDegrees<Type>(degrees)};
return Angle{angles::radians_to_degrees<Type>(degrees)};
}
[[nodiscard]]
@@ -57,51 +56,51 @@ namespace omath
}
[[nodiscard]]
constexpr Type AsDegrees() const
constexpr Type as_degrees() const
{
return m_angle;
}
[[nodiscard]]
constexpr Type AsRadians() const
constexpr Type as_radians() const
{
return angles::DegreesToRadians(m_angle);
return angles::degrees_to_radians(m_angle);
}
[[nodiscard]]
Type Sin() const
Type sin() const
{
return std::sin(AsRadians());
return std::sin(as_radians());
}
[[nodiscard]]
Type Cos() const
Type cos() const
{
return std::cos(AsRadians());
return std::cos(as_radians());
}
[[nodiscard]]
Type Tan() const
Type tan() const
{
return std::tan(AsRadians());
return std::tan(as_radians());
}
[[nodiscard]]
Type Atan() const
Type atan() const
{
return std::atan(AsRadians());
return std::atan(as_radians());
}
[[nodiscard]]
Type Cot() const
Type cot() const
{
return Cos() / Sin();
return cos() / sin();
}
constexpr Angle& operator+=(const Angle& other)
{
if constexpr (flags == AngleFlags::Normalized)
m_angle = angles::WrapAngle(m_angle + other.m_angle, min, max);
m_angle = angles::wrap_angle(m_angle + other.m_angle, min, max);
else if constexpr (flags == AngleFlags::Clamped)
m_angle = std::clamp(m_angle + other.m_angle, min, max);
@@ -115,7 +114,8 @@ namespace omath
}
[[nodiscard]]
constexpr std::partial_ordering operator<=>(const Angle& other) const = default;
constexpr std::partial_ordering operator<=>(const Angle& other) const
= default;
constexpr Angle& operator-=(const Angle& other)
{
@@ -126,7 +126,7 @@ namespace omath
constexpr Angle& operator+(const Angle& other)
{
if constexpr (flags == AngleFlags::Normalized)
return {angles::WrapAngle(m_angle + other.m_angle, min, max)};
return {angles::wrap_angle(m_angle + other.m_angle, min, max)};
else if constexpr (flags == AngleFlags::Clamped)
return {std::clamp(m_angle + other.m_angle, min, max)};

View File

@@ -10,43 +10,43 @@ namespace omath::angles
{
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]] constexpr Type RadiansToDegrees(const Type& radians)
[[nodiscard]] constexpr Type radians_to_degrees(const Type& radians)
{
return radians * (Type(180) / std::numbers::pi_v<Type>);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]] constexpr Type DegreesToRadians(const Type& degrees)
[[nodiscard]] constexpr Type degrees_to_radians(const Type& degrees)
{
return degrees * (std::numbers::pi_v<Type> / Type(180));
}
template<class type>
requires std::is_floating_point_v<type>
[[nodiscard]] type HorizontalFovToVertical(const type& horFov, const type& aspect)
[[nodiscard]] type horizontal_fov_to_vertical(const type& horizontal_fov, const type& aspect)
{
const auto fovRad = DegreesToRadians(horFov);
const auto fov_rad = degrees_to_radians(horizontal_fov);
const auto vertFov = type(2) * std::atan(std::tan(fovRad / type(2)) / aspect);
const auto vert_fov = type(2) * std::atan(std::tan(fov_rad / type(2)) / aspect);
return RadiansToDegrees(vertFov);
return radians_to_degrees(vert_fov);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]] Type VerticalFovToHorizontal(const Type& vertFov, const Type& aspect)
[[nodiscard]] Type vertical_fov_to_horizontal(const Type& vertical_fov, const Type& aspect)
{
const auto fovRad = DegreesToRadians(vertFov);
const auto fov_as_radians = degrees_to_radians(vertical_fov);
const auto horFov = Type(2) * std::atan(std::tan(fovRad / Type(2)) * aspect);
const auto horizontal_fov = Type(2) * std::atan(std::tan(fov_as_radians / Type(2)) * aspect);
return RadiansToDegrees(horFov);
return radians_to_degrees(horizontal_fov);
}
template<class Type>
requires std::is_arithmetic_v<Type>
[[nodiscard]] Type WrapAngle(const Type& angle, const Type& min, const Type& max)
[[nodiscard]] Type wrap_angle(const Type& angle, const Type& min, const Type& max)
{
if (angle <= max && angle >= min)
return angle;
@@ -60,4 +60,4 @@ namespace omath::angles
return wrappedAngle + min;
}
}
} // namespace omath::angles

View File

@@ -14,25 +14,24 @@ namespace omath::collision
Vector3<float> start;
Vector3<float> end;
bool infinite_length = false;
[[nodiscard]]
Vector3<float> DirectionVector() const;
[[nodiscard]]
Vector3<float> DirectionVectorNormalized() const;
Vector3<float> direction_vector() const;
[[nodiscard]]
Vector3<float> direction_vector_normalized() const;
};
class LineTracer
{
public:
LineTracer() = delete;
[[nodiscard]]
static bool CanTraceLine(const Ray& ray, const Triangle<Vector3<float>>& triangle);
static bool can_trace_line(const Ray& ray, const Triangle<Vector3<float>>& triangle);
// Realization of MöllerTrumbore intersection algorithm
// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm
[[nodiscard]]
static Vector3<float> GetRayHitPoint(const Ray& ray, const Triangle<Vector3<float>>& triangle);
static Vector3<float> get_ray_hit_point(const Ray& ray, const Triangle<Vector3<float>>& triangle);
};
}
} // namespace omath::collision

View File

@@ -4,95 +4,93 @@
#pragma once
#include <cstdint>
#include "omath/vector3.hpp"
#include "omath/vector4.hpp"
#include <cstdint>
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
namespace omath
{
struct HSV
struct Hsv
{
float hue{};
float saturation{};
float value{};
};
class Color final : public Vector4<float>
{
public:
constexpr Color(const float r, const float g, const float b, const 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);
clamp(0.f, 1.f);
}
constexpr explicit Color() = default;
[[nodiscard]]
constexpr static Color FromRGBA(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a)
constexpr static Color from_rgba(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a)
{
return Color{Vector4(r, g, b, a) / 255.f};
}
[[nodiscard]]
constexpr static Color FromHSV(float hue, const float saturation, const float value)
constexpr static Color from_hsv(float hue, const float saturation, const float value)
{
float r{}, g{}, b{};
hue = std::clamp(hue, 0.f, 1.f);
const int i = static_cast<int>(hue * 6.f);
const float f = hue * 6 - i;
const float f = hue * 6.f - static_cast<float>(i);
const float p = value * (1 - saturation);
const float q = value * (1 - f * saturation);
const float t = value * (1 - (1 - f) * saturation);
switch (i % 6)
{
case 0:
r = value, g = t, b = p;
break;
case 1:
r = q, g = value, b = p;
break;
case 2:
r = p, g = value, b = t;
break;
case 3:
r = p, g = q, b = value;
break;
case 4:
r = t, g = p, b = value;
break;
case 5:
r = value, g = p, b = q;
break;
case 0:
r = value, g = t, b = p;
break;
case 1:
r = q, g = value, b = p;
break;
case 2:
r = p, g = value, b = t;
break;
case 3:
r = p, g = q, b = value;
break;
case 4:
r = t, g = p, b = value;
break;
case 5:
r = value, g = p, b = q;
break;
default:
return {0.f, 0.f, 0.f, 0.f};
default:
return {0.f, 0.f, 0.f, 0.f};
}
return {r, g, b, 1.f};
}
[[nodiscard]]
constexpr static Color FromHSV(const HSV& hsv)
constexpr static Color from_hsv(const Hsv& hsv)
{
return FromHSV(hsv.hue, hsv.saturation, hsv.value);
return from_hsv(hsv.hue, hsv.saturation, hsv.value);
}
[[nodiscard]]
constexpr HSV ToHSV() const
constexpr Hsv to_hsv() const
{
HSV hsvData;
Hsv hsv_data;
const float& red = x;
const float& green = y;
@@ -102,70 +100,69 @@ namespace omath
const float min = std::min({red, green, blue});
const float delta = max - min;
if (delta == 0.f)
hsvData.hue = 0.f;
hsv_data.hue = 0.f;
else if (max == red)
hsvData.hue = 60.f * (std::fmodf(((green - blue) / delta), 6.f));
hsv_data.hue = 60.f * (std::fmodf(((green - blue) / delta), 6.f));
else if (max == green)
hsvData.hue = 60.f * (((blue - red) / delta) + 2.f);
hsv_data.hue = 60.f * (((blue - red) / delta) + 2.f);
else if (max == blue)
hsvData.hue = 60.f * (((red - green) / delta) + 4.f);
hsv_data.hue = 60.f * (((red - green) / delta) + 4.f);
if (hsvData.hue < 0.f)
hsvData.hue += 360.f;
if (hsv_data.hue < 0.f)
hsv_data.hue += 360.f;
hsvData.hue /= 360.f;
hsvData.saturation = max == 0.f ? 0.f : delta / max;
hsvData.value = max;
hsv_data.hue /= 360.f;
hsv_data.saturation = max == 0.f ? 0.f : delta / max;
hsv_data.value = max;
return hsvData;
return hsv_data;
}
constexpr explicit Color(const Vector4& vec) : Vector4(vec)
constexpr explicit Color(const Vector4& vec): Vector4(vec)
{
Clamp(0.f, 1.f);
clamp(0.f, 1.f);
}
constexpr void SetHue(const float hue)
constexpr void set_hue(const float hue)
{
auto hsv = ToHSV();
auto hsv = to_hsv();
hsv.hue = hue;
*this = FromHSV(hsv);
*this = from_hsv(hsv);
}
constexpr void SetSaturation(const float saturation)
constexpr void set_saturation(const float saturation)
{
auto hsv = ToHSV();
auto hsv = to_hsv();
hsv.saturation = saturation;
*this = FromHSV(hsv);
*this = from_hsv(hsv);
}
constexpr void SetValue(const float value)
constexpr void set_value(const float value)
{
auto hsv = ToHSV();
auto hsv = to_hsv();
hsv.value = value;
*this = FromHSV(hsv);
*this = from_hsv(hsv);
}
[[nodiscard]]
constexpr Color Blend(const Color& other, float ratio) const
constexpr Color blend(const Color& other, float ratio) const
{
ratio = std::clamp(ratio, 0.f, 1.f);
return Color(*this * (1.f - ratio) + other * ratio);
}
[[nodiscard]] static constexpr Color Red()
[[nodiscard]] static constexpr Color red()
{
return {1.f, 0.f, 0.f, 1.f};
}
[[nodiscard]] static constexpr Color Green()
[[nodiscard]] static constexpr Color green()
{
return {0.f, 1.f, 0.f, 1.f};
}
[[nodiscard]] static constexpr Color Blue()
[[nodiscard]] static constexpr Color blue()
{
return {0.f, 0.f, 1.f, 1.f};
}

View File

@@ -8,14 +8,15 @@
namespace omath::iw_engine
{
class Camera final : public projection::Camera<Mat4x4, ViewAngles>
class Camera final : public projection::Camera<Mat4X4, ViewAngles>
{
public:
Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
Camera(const Vector3<float>& position, const ViewAngles& view_angles, const projection::ViewPort& view_port,
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
void LookAt(const Vector3<float>& target) override;
void look_at(const Vector3<float>& target) override;
protected:
[[nodiscard]] Mat4x4 CalcViewMatrix() const override;
[[nodiscard]] Mat4x4 CalcProjectionMatrix() const override;
[[nodiscard]] Mat4X4 calc_view_matrix() const override;
[[nodiscard]] Mat4X4 calc_projection_matrix() const override;
};
}
} // namespace omath::iw_engine

View File

@@ -3,23 +3,23 @@
//
#pragma once
#include <omath/vector3.hpp>
#include <omath/mat.hpp>
#include <omath/angle.hpp>
#include <omath/mat.hpp>
#include <omath/vector3.hpp>
#include <omath/view_angles.hpp>
namespace omath::iw_engine
{
constexpr Vector3<float> kAbsUp = {0, 0, 1};
constexpr Vector3<float> kAbsRight = {0, -1, 0};
constexpr Vector3<float> kAbsForward = {1, 0, 0};
constexpr Vector3<float> k_abs_up = {0, 0, 1};
constexpr Vector3<float> k_abs_right = {0, -1, 0};
constexpr Vector3<float> k_abs_forward = {1, 0, 0};
using Mat4x4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
using Mat3x3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
using Mat1x3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>;
using Mat4X4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
using Mat3X3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
using Mat1X3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>;
using PitchAngle = Angle<float, -89.f, 89.f, AngleFlags::Clamped>;
using YawAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
using RollAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
using ViewAngles = omath::ViewAngles<PitchAngle, YawAngle, RollAngle>;
}
} // namespace omath::iw_engine

View File

@@ -8,19 +8,19 @@
namespace omath::iw_engine
{
[[nodiscard]]
Vector3<float> ForwardVector(const ViewAngles& angles);
Vector3<float> forward_vector(const ViewAngles& angles);
[[nodiscard]]
Vector3<float> RightVector(const ViewAngles& angles);
Vector3<float> right_vector(const ViewAngles& angles);
[[nodiscard]]
Vector3<float> UpVector(const ViewAngles& angles);
Vector3<float> up_vector(const ViewAngles& angles);
[[nodiscard]]
Mat4x4 RotationMatrix(const ViewAngles& angles);
Mat4X4 rotation_matrix(const ViewAngles& angles);
[[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
[[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
[[nodiscard]]
Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far);
} // namespace omath::iw_engine

View File

@@ -7,13 +7,13 @@
namespace omath::opengl_engine
{
class Camera final : public projection::Camera<Mat4x4, ViewAngles>
class Camera final : public projection::Camera<Mat4X4, ViewAngles>
{
public:
Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
Camera(const Vector3<float>& position, const ViewAngles& view_angles, const projection::ViewPort& view_port,
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
void LookAt(const Vector3<float>& target) override;
[[nodiscard]] Mat4x4 CalcViewMatrix() const override;
[[nodiscard]] Mat4x4 CalcProjectionMatrix() const override;
void look_at(const Vector3<float>& target) override;
[[nodiscard]] Mat4X4 calc_view_matrix() const override;
[[nodiscard]] Mat4X4 calc_projection_matrix() const override;
};
}
} // namespace omath::opengl_engine

View File

@@ -10,17 +10,16 @@
namespace omath::opengl_engine
{
constexpr Vector3<float> kAbsUp = {0, 1, 0};
constexpr Vector3<float> kAbsRight = {1, 0, 0};
constexpr Vector3<float> kAbsForward = {0, 0, -1};
constexpr Vector3<float> k_abs_up = {0, 1, 0};
constexpr Vector3<float> k_abs_right = {1, 0, 0};
constexpr Vector3<float> k_abs_forward = {0, 0, -1};
using Mat4x4 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
using Mat3x3 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
using Mat1x3 = Mat<1, 3, float, MatStoreType::COLUMN_MAJOR>;
using Mat4X4 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
using Mat3X3 = Mat<4, 4, float, MatStoreType::COLUMN_MAJOR>;
using Mat1X3 = Mat<1, 3, float, MatStoreType::COLUMN_MAJOR>;
using PitchAngle = Angle<float, -90.f, 90.f, AngleFlags::Clamped>;
using YawAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
using RollAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
using ViewAngles = omath::ViewAngles<PitchAngle, YawAngle, RollAngle>;
}
} // namespace omath::opengl_engine

View File

@@ -8,19 +8,19 @@
namespace omath::opengl_engine
{
[[nodiscard]]
Vector3<float> ForwardVector(const ViewAngles& angles);
Vector3<float> forward_vector(const ViewAngles& angles);
[[nodiscard]]
Vector3<float> RightVector(const ViewAngles& angles);
Vector3<float> right_vector(const ViewAngles& angles);
[[nodiscard]]
Vector3<float> UpVector(const ViewAngles& angles);
Vector3<float> up_vector(const ViewAngles& angles);
[[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
[[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
[[nodiscard]]
Mat4x4 RotationMatrix(const ViewAngles& angles);
Mat4X4 rotation_matrix(const ViewAngles& angles);
[[nodiscard]]
Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far);
} // namespace omath::opengl_engine

View File

@@ -7,14 +7,15 @@
namespace omath::source_engine
{
class Camera final : public projection::Camera<Mat4x4, ViewAngles>
class Camera final : public projection::Camera<Mat4X4, ViewAngles>
{
public:
Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
Camera(const Vector3<float>& position, const ViewAngles& view_angles, const projection::ViewPort& view_port,
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
void LookAt(const Vector3<float>& target) override;
void look_at(const Vector3<float>& target) override;
protected:
[[nodiscard]] Mat4x4 CalcViewMatrix() const override;
[[nodiscard]] Mat4x4 CalcProjectionMatrix() const override;
[[nodiscard]] Mat4X4 calc_view_matrix() const override;
[[nodiscard]] Mat4X4 calc_projection_matrix() const override;
};
}
} // namespace omath::source_engine

View File

@@ -3,23 +3,23 @@
//
#pragma once
#include <omath/vector3.hpp>
#include <omath/mat.hpp>
#include <omath/angle.hpp>
#include <omath/mat.hpp>
#include <omath/vector3.hpp>
#include <omath/view_angles.hpp>
namespace omath::source_engine
{
constexpr Vector3<float> kAbsUp = {0, 0, 1};
constexpr Vector3<float> kAbsRight = {0, -1, 0};
constexpr Vector3<float> kAbsForward = {1, 0, 0};
constexpr Vector3<float> k_abs_up = {0, 0, 1};
constexpr Vector3<float> k_abs_right = {0, -1, 0};
constexpr Vector3<float> k_abs_forward = {1, 0, 0};
using Mat4x4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
using Mat3x3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
using Mat1x3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>;
using Mat4X4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
using Mat3X3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
using Mat1X3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>;
using PitchAngle = Angle<float, -89.f, 89.f, AngleFlags::Clamped>;
using YawAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
using RollAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
using ViewAngles = omath::ViewAngles<PitchAngle, YawAngle, RollAngle>;
} // namespace omath::source
} // namespace omath::source_engine

View File

@@ -7,19 +7,19 @@
namespace omath::source_engine
{
[[nodiscard]]
Vector3<float> ForwardVector(const ViewAngles& angles);
Vector3<float> forward_vector(const ViewAngles& angles);
[[nodiscard]]
Mat4x4 RotationMatrix(const ViewAngles& angles);
Mat4X4 rotation_matrix(const ViewAngles& angles);
[[nodiscard]]
Vector3<float> RightVector(const ViewAngles& angles);
Vector3<float> right_vector(const ViewAngles& angles);
[[nodiscard]]
Vector3<float> UpVector(const ViewAngles& angles);
Vector3<float> up_vector(const ViewAngles& angles);
[[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
[[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
[[nodiscard]]
Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
} // namespace omath::source
Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far);
} // namespace omath::source_engine

View File

@@ -8,14 +8,15 @@
namespace omath::unity_engine
{
class Camera final : public projection::Camera<Mat4x4, ViewAngles>
class Camera final : public projection::Camera<Mat4X4, ViewAngles>
{
public:
Camera(const Vector3<float>& position, const ViewAngles& viewAngles, const projection::ViewPort& viewPort,
Camera(const Vector3<float>& position, const ViewAngles& view_angles, const projection::ViewPort& view_port,
const Angle<float, 0.f, 180.f, AngleFlags::Clamped>& fov, float near, float far);
void LookAt(const Vector3<float>& target) override;
void look_at(const Vector3<float>& target) override;
protected:
[[nodiscard]] Mat4x4 CalcViewMatrix() const override;
[[nodiscard]] Mat4x4 CalcProjectionMatrix() const override;
[[nodiscard]] Mat4X4 calc_view_matrix() const override;
[[nodiscard]] Mat4X4 calc_projection_matrix() const override;
};
}
} // namespace omath::unity_engine

View File

@@ -4,23 +4,23 @@
#pragma once
#include <omath/vector3.hpp>
#include <omath/mat.hpp>
#include <omath/angle.hpp>
#include <omath/mat.hpp>
#include <omath/vector3.hpp>
#include <omath/view_angles.hpp>
namespace omath::unity_engine
{
constexpr Vector3<float> kAbsUp = {0, 1, 0};
constexpr Vector3<float> kAbsRight = {1, 0, 0};
constexpr Vector3<float> kAbsForward = {0, 0, 1};
constexpr Vector3<float> k_abs_up = {0, 1, 0};
constexpr Vector3<float> k_abs_right = {1, 0, 0};
constexpr Vector3<float> k_abs_forward = {0, 0, 1};
using Mat4x4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
using Mat3x3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
using Mat1x3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>;
using Mat4X4 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
using Mat3X3 = Mat<4, 4, float, MatStoreType::ROW_MAJOR>;
using Mat1X3 = Mat<1, 3, float, MatStoreType::ROW_MAJOR>;
using PitchAngle = Angle<float, -90.f, 90.f, AngleFlags::Clamped>;
using YawAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
using RollAngle = Angle<float, -180.f, 180.f, AngleFlags::Normalized>;
using ViewAngles = omath::ViewAngles<PitchAngle, YawAngle, RollAngle>;
} // namespace omath::source
} // namespace omath::unity_engine

View File

@@ -8,19 +8,19 @@
namespace omath::unity_engine
{
[[nodiscard]]
Vector3<float> ForwardVector(const ViewAngles& angles);
Vector3<float> forward_vector(const ViewAngles& angles);
[[nodiscard]]
Vector3<float> RightVector(const ViewAngles& angles);
Vector3<float> right_vector(const ViewAngles& angles);
[[nodiscard]]
Vector3<float> UpVector(const ViewAngles& angles);
Vector3<float> up_vector(const ViewAngles& angles);
[[nodiscard]] Mat4x4 CalcViewMatrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
[[nodiscard]] Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin);
[[nodiscard]]
Mat4x4 RotationMatrix(const ViewAngles& angles);
Mat4X4 rotation_matrix(const ViewAngles& angles);
[[nodiscard]]
Mat4x4 CalcPerspectiveProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
} // namespace omath::source
Mat4X4 calc_perspective_projection_matrix(float field_of_view, float aspect_ratio, float near, float far);
} // namespace omath::unity_engine

View File

@@ -2,21 +2,19 @@
// Created by vlad on 9/29/2024.
//
#pragma once
#include "omath/vector3.hpp"
#include <algorithm>
#include <array>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <stdexcept>
#include <utility>
#include "omath/vector3.hpp"
#include <numeric>
#ifdef near
#undef near
#endif
#ifdef far
#undef far
#endif
@@ -34,25 +32,22 @@ namespace omath
COLUMN_MAJOR
};
template<typename M1, typename M2>
concept MatTemplateEqual =
(M1::rows == M2::rows) && (M1::columns == M2::columns) &&
std::is_same_v<typename M1::value_type, typename M2::value_type> &&
(M1::store_type == M2::store_type);
template<typename M1, typename M2> concept MatTemplateEqual
= (M1::rows == M2::rows) && (M1::columns == M2::columns)
&& std::is_same_v<typename M1::value_type, typename M2::value_type> && (M1::store_type == M2::store_type);
template<size_t Rows = 0, size_t Columns = 0, class Type = float, MatStoreType StoreType = MatStoreType::ROW_MAJOR>
requires std::is_arithmetic_v<Type>
requires std::is_arithmetic_v<Type>
class Mat final
{
public:
constexpr Mat() noexcept
{
Clear();
clear();
}
[[nodiscard]]
constexpr static MatStoreType GetStoreOrdering() noexcept
constexpr static MatStoreType get_store_ordering() noexcept
{
return StoreType;
}
@@ -61,24 +56,24 @@ namespace omath
if (rows.size() != Rows)
throw std::invalid_argument("Initializer list rows size does not match template parameter Rows");
auto rowIt = rows.begin();
for (size_t i = 0; i < Rows; ++i, ++rowIt)
auto row_it = rows.begin();
for (size_t i = 0; i < Rows; ++i, ++row_it)
{
if (rowIt->size() != Columns)
if (row_it->size() != Columns)
throw std::invalid_argument(
"All rows must have the same number of columns as template parameter Columns");
auto colIt = rowIt->begin();
for (size_t j = 0; j < Columns; ++j, ++colIt)
auto col_it = row_it->begin();
for (size_t j = 0; j < Columns; ++j, ++col_it)
{
At(i, j) = std::move(*colIt);
at(i, j) = std::move(*col_it);
}
}
}
constexpr explicit Mat(const Type* rawData)
constexpr explicit Mat(const Type* raw_data)
{
std::copy_n(rawData, Rows * Columns, m_data.begin());
std::copy_n(raw_data, Rows * Columns, m_data.begin());
}
constexpr Mat(const Mat& other) noexcept
@@ -89,13 +84,13 @@ namespace omath
[[nodiscard]]
constexpr Type& operator[](const size_t row, const size_t col)
{
return At(row, col);
return at(row, col);
}
[[nodiscard]]
constexpr Type& operator[](const size_t row, const size_t col) const
{
return At(row, col);
return at(row, col);
}
constexpr Mat(Mat&& other) noexcept
@@ -104,35 +99,35 @@ namespace omath
}
[[nodiscard]]
static constexpr size_t RowCount() noexcept
static constexpr size_t row_count() noexcept
{
return Rows;
}
[[nodiscard]]
static constexpr size_t ColumnsCount() noexcept
static constexpr size_t columns_count() noexcept
{
return Columns;
}
[[nodiscard]]
static consteval MatSize Size() noexcept
static consteval MatSize size() noexcept
{
return {Rows, Columns};
}
[[nodiscard]]
constexpr const Type& At(const size_t rowIndex, const size_t columnIndex) const
constexpr const Type& at(const size_t row_index, const size_t column_index) const
{
#if !defined(NDEBUG) && defined(OMATH_SUPRESS_SAFETY_CHECKS)
if (rowIndex >= Rows || columnIndex >= Columns)
if (row_index >= Rows || column_index >= Columns)
throw std::out_of_range("Index out of range");
#endif
if constexpr (StoreType == MatStoreType::ROW_MAJOR)
return m_data[rowIndex * Columns + columnIndex];
return m_data[row_index * Columns + column_index];
else if constexpr (StoreType == MatStoreType::COLUMN_MAJOR)
return m_data[rowIndex + columnIndex * Rows];
return m_data[row_index + column_index * Rows];
else
{
@@ -141,30 +136,29 @@ namespace omath
}
}
[[nodiscard]] constexpr Type& At(const size_t rowIndex, const size_t columnIndex)
[[nodiscard]] constexpr Type& at(const size_t row_index, const size_t column_index)
{
return const_cast<Type&>(std::as_const(*this).At(rowIndex, columnIndex));
return const_cast<Type&>(std::as_const(*this).at(row_index, column_index));
}
[[nodiscard]]
constexpr Type Sum() const noexcept
constexpr Type sum() const noexcept
{
return std::accumulate(m_data.begin(), m_data.end(), Type(0));
return std::accumulate(m_data.begin(), m_data.end(), static_cast<Type>(0));
}
constexpr void Clear() noexcept
constexpr void clear() noexcept
{
Set(0);
set(static_cast<Type>(0));
}
constexpr void Set(const Type& value) noexcept
constexpr void set(const Type& value) noexcept
{
std::ranges::fill(m_data, value);
}
// Operator overloading for multiplication with another Mat
template<size_t OtherColumns>
[[nodiscard]]
template<size_t OtherColumns> [[nodiscard]]
constexpr Mat<Rows, OtherColumns, Type, StoreType>
operator*(const Mat<Columns, OtherColumns, Type, StoreType>& other) const
{
@@ -175,20 +169,19 @@ namespace omath
{
Type sum = 0;
for (size_t k = 0; k < Columns; ++k)
sum += At(i, k) * other.At(k, j);
result.At(i, j) = sum;
sum += at(i, k) * other.at(k, j);
result.at(i, j) = sum;
}
return result;
}
constexpr Mat& operator*=(const Type& f) noexcept
{
std::ranges::for_each(m_data, [&f](auto& val) {val *= f;});
std::ranges::for_each(m_data, [&f](auto& val) { val *= f; });
return *this;
}
template<size_t OtherColumns>
constexpr Mat<Rows, OtherColumns, Type, StoreType>
template<size_t OtherColumns> constexpr Mat<Rows, OtherColumns, Type, StoreType>
operator*=(const Mat<Columns, OtherColumns, Type, StoreType>& other)
{
return *this = *this * other;
@@ -204,7 +197,7 @@ namespace omath
constexpr Mat& operator/=(const Type& value) noexcept
{
std::ranges::for_each(m_data, [&value](auto& val) {val /= value;});
std::ranges::for_each(m_data, [&value](auto& val) { val /= value; });
return *this;
}
@@ -238,7 +231,7 @@ namespace omath
Mat<Columns, Rows, Type, StoreType> transposed;
for (size_t i = 0; i < Rows; ++i)
for (size_t j = 0; j < Columns; ++j)
transposed.At(j, i) = At(i, j);
transposed.at(j, i) = at(i, j);
return transposed;
}
@@ -249,17 +242,17 @@ namespace omath
static_assert(Rows == Columns, "Determinant is only defined for square matrices.");
if constexpr (Rows == 1)
return At(0, 0);
return at(0, 0);
if constexpr (Rows == 2)
return At(0, 0) * At(1, 1) - At(0, 1) * At(1, 0);
return at(0, 0) * at(1, 1) - at(0, 1) * at(1, 0);
if constexpr (Rows > 2)
{
Type det = 0;
for (size_t column = 0; column < Columns; ++column)
{
const Type cofactor = At(0, column) * AlgComplement(0, column);
const Type cofactor = at(0, column) * alg_complement(0, column);
det += cofactor;
}
return det;
@@ -268,9 +261,9 @@ namespace omath
}
[[nodiscard]]
constexpr Mat<Rows - 1, Columns - 1, Type, StoreType> Strip(const size_t row, const size_t column) const
constexpr Mat<Rows - 1, Columns - 1, Type, StoreType> strip(const size_t row, const size_t column) const
{
static_assert(Rows-1 > 0 && Columns-1 > 0);
static_assert(Rows - 1 > 0 && Columns - 1 > 0);
Mat<Rows - 1, Columns - 1, Type, StoreType> result;
for (size_t i = 0, m = 0; i < Rows; ++i)
{
@@ -280,7 +273,7 @@ namespace omath
{
if (j == column)
continue;
result.At(m, n) = At(i, j);
result.at(m, n) = at(i, j);
++n;
}
++m;
@@ -289,32 +282,32 @@ namespace omath
}
[[nodiscard]]
constexpr Type Minor(const size_t row, const size_t column) const
constexpr Type minor(const size_t row, const size_t column) const
{
return Strip(row, column).Determinant();
return strip(row, column).Determinant();
}
[[nodiscard]]
constexpr Type AlgComplement(const size_t row, const size_t column) const
constexpr Type alg_complement(const size_t row, const size_t column) const
{
const auto minor = Minor(row, column);
return (row + column + 2) % 2 == 0 ? minor: -minor;
const auto minor_value = minor(row, column);
return (row + column + 2) % 2 == 0 ? minor_value : -minor_value;
}
[[nodiscard]]
constexpr const std::array<Type, Rows * Columns>& RawArray() const
constexpr const std::array<Type, Rows * Columns>& raw_array() const
{
return m_data;
}
[[nodiscard]]
constexpr std::array<Type, Rows * Columns>& RawArray()
constexpr std::array<Type, Rows * Columns>& raw_array()
{
return m_data;
}
[[nodiscard]]
std::string ToString() const noexcept
std::string to_string() const noexcept
{
std::ostringstream oss;
oss << "[[";
@@ -326,7 +319,7 @@ namespace omath
for (size_t j = 0; j < Columns; ++j)
{
oss << std::setw(9) << std::fixed << std::setprecision(3) << At(i, j);
oss << std::setw(9) << std::fixed << std::setprecision(3) << at(i, j);
if (j != Columns - 1)
oss << ", ";
}
@@ -349,18 +342,18 @@ namespace omath
// Static methods that return fixed-size matrices
[[nodiscard]]
constexpr static Mat<4, 4> ToScreenMat(const Type& screenWidth, const Type& screenHeight) noexcept
constexpr static Mat<4, 4> to_screen_mat(const Type& screen_width, const Type& screen_height) noexcept
{
return {
{screenWidth / 2, 0, 0, 0},
{0, -screenHeight / 2, 0, 0},
{screen_width / 2, 0, 0, 0},
{0, -screen_height / 2, 0, 0},
{0, 0, 1, 0},
{screenWidth / 2, screenHeight / 2, 0, 1},
{screen_width / 2, screen_height / 2, 0, 1},
};
}
[[nodiscard]]
constexpr std::optional<Mat> Inverted() const
constexpr std::optional<Mat> inverted() const
{
const auto det = Determinant();
@@ -372,33 +365,32 @@ namespace omath
for (std::size_t row = 0; row < Rows; row++)
for (std::size_t column = 0; column < Rows; column++)
result.At(row, column) = transposed.AlgComplement(row, column);
result.at(row, column) = transposed.alg_complement(row, column);
result /= det;
return {result};
}
private:
std::array<Type, Rows * Columns> m_data;
};
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
[[nodiscard]]
constexpr static Mat<1, 4, Type, St> MatRowFromVector(const Vector3<Type>& vector) noexcept
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR> [[nodiscard]]
constexpr static Mat<1, 4, Type, St> mat_row_from_vector(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<Type>& vector) noexcept
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR> [[nodiscard]]
constexpr static Mat<4, 1, Type, St> mat_column_from_vector(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<Type>& diff) noexcept
constexpr Mat<4, 4, Type, St> mat_translation(const Vector3<Type>& diff) noexcept
{
return
{
@@ -411,38 +403,38 @@ namespace omath
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR, class Angle>
[[nodiscard]]
Mat<4, 4, Type, St> MatRotationAxisX(const Angle& angle) noexcept
Mat<4, 4, Type, St> mat_rotation_axis_x(const Angle& angle) noexcept
{
return
{
{1, 0, 0, 0},
{0, angle.Cos(), -angle.Sin(), 0},
{0, angle.Sin(), angle.Cos(), 0},
{0, angle.cos(), -angle.sin(), 0},
{0, angle.sin(), angle.cos(), 0},
{0, 0, 0, 1}
};
}
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR, class Angle>
[[nodiscard]]
Mat<4, 4, Type, St> MatRotationAxisY(const Angle& angle) noexcept
Mat<4, 4, Type, St> mat_rotation_axis_y(const Angle& angle) noexcept
{
return
{
{angle.Cos(), 0, angle.Sin(), 0},
{angle.cos(), 0, angle.sin(), 0},
{0 , 1, 0, 0},
{-angle.Sin(), 0, angle.Cos(), 0},
{-angle.sin(), 0, angle.cos(), 0},
{0 , 0, 0, 1}
};
}
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR, class Angle>
[[nodiscard]]
Mat<4, 4, Type, St> MatRotationAxisZ(const Angle& angle) noexcept
Mat<4, 4, Type, St> mat_rotation_axis_z(const Angle& angle) noexcept
{
return
{
{angle.Cos(), -angle.Sin(), 0, 0},
{angle.Sin(), angle.Cos(), 0, 0},
{angle.cos(), -angle.sin(), 0, 0},
{angle.sin(), angle.cos(), 0, 0},
{ 0, 0, 1, 0},
{ 0, 0, 0, 1},
};
@@ -450,8 +442,8 @@ namespace omath
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
[[nodiscard]]
static Mat<4, 4, Type, St> MatCameraView(const Vector3<Type>& forward, const Vector3<Type>& right,
const Vector3<Type>& up, const Vector3<Type>& cameraOrigin) noexcept
static Mat<4, 4, Type, St> mat_camera_view(const Vector3<Type>& forward, const Vector3<Type>& right,
const Vector3<Type>& up, const Vector3<Type>& camera_origin) noexcept
{
return Mat<4, 4, Type, St>
{
@@ -460,31 +452,31 @@ namespace omath
{forward.x, forward.y, forward.z, 0},
{0, 0, 0, 1},
} * MatTranslation<Type, St>(-cameraOrigin);
} * mat_translation<Type, St>(-camera_origin);
}
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
[[nodiscard]]
Mat<4, 4, Type, St> MatPerspectiveLeftHanded(const float fieldOfView, const float aspectRatio, const float near,
const float far) noexcept
Mat<4, 4, Type, St> mat_perspective_left_handed(const float field_of_view, const float aspect_ratio,
const float near, const float far) noexcept
{
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f);
return {{1.f / (aspectRatio * fovHalfTan), 0.f, 0.f, 0.f},
{0.f, 1.f / fovHalfTan, 0.f, 0.f},
return {{1.f / (aspect_ratio * fov_half_tan), 0.f, 0.f, 0.f},
{0.f, 1.f / fov_half_tan, 0.f, 0.f},
{0.f, 0.f, (far + near) / (far - near), -(2.f * near * far) / (far - near)},
{0.f, 0.f, 1.f, 0.f}};
}
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
[[nodiscard]]
Mat<4, 4, Type, St> MatPerspectiveRightHanded(const float fieldOfView, const float aspectRatio, const float near,
const float far) noexcept
Mat<4, 4, Type, St> mat_perspective_right_handed(const float field_of_view, const float aspect_ratio,
const float near, const float far) noexcept
{
const float fovHalfTan = std::tan(angles::DegreesToRadians(fieldOfView) / 2.f);
const float fov_half_tan = std::tan(angles::degrees_to_radians(field_of_view) / 2.f);
return {{1.f / (aspectRatio * fovHalfTan), 0.f, 0.f, 0.f},
{0.f, 1.f / fovHalfTan, 0.f, 0.f},
return {{1.f / (aspect_ratio * fov_half_tan), 0.f, 0.f, 0.f},
{0.f, 1.f / fov_half_tan, 0.f, 0.f},
{0.f, 0.f, -(far + near) / (far - near), -(2.f * near * far) / (far - near)},
{0.f, 0.f, -1.f, 0.f}};
}

View File

@@ -1,8 +1,8 @@
#pragma once
#include "omath/vector3.hpp"
#include <initializer_list>
#include <memory>
#include <string>
#include "omath/vector3.hpp"
namespace omath
{
@@ -16,51 +16,51 @@ namespace omath
Matrix(const std::initializer_list<std::initializer_list<float>>& rows);
[[nodiscard]]
static Matrix ToScreenMatrix(float screenWidth, float screenHeight);
static Matrix to_screen_matrix(float screen_width, float screen_height);
[[nodiscard]]
static Matrix TranslationMatrix(const Vector3<float>& diff);
static Matrix translation_matrix(const Vector3<float>& diff);
[[nodiscard]]
static Matrix OrientationMatrix(const Vector3<float>& forward, const Vector3<float>& right, const Vector3<float>& up);
static Matrix orientation_matrix(const Vector3<float>& forward, const Vector3<float>& right,
const Vector3<float>& up);
[[nodiscard]]
static Matrix ProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);
static Matrix projection_matrix(float field_of_view, float aspect_ratio, float near, float far);
Matrix(const Matrix& other);
Matrix(size_t rows, size_t columns, const float* pRaw);
Matrix(size_t rows, size_t columns, const float* raw_data);
Matrix(Matrix&& other) noexcept;
[[nodiscard]]
size_t RowCount() const noexcept;
size_t row_count() const noexcept;
[[nodiscard]]
float& operator[](size_t row, size_t column);
[[nodiscard]]
size_t ColumnsCount() const noexcept;
size_t columns_count() const noexcept;
[[nodiscard]]
std::pair<size_t, size_t> Size() const noexcept;
std::pair<size_t, size_t> size() const noexcept;
[[nodiscard]]
float& At(size_t iRow, size_t iCol);
float& at(size_t row, size_t col);
[[nodiscard]]
float Sum();
float sum();
void SetDataFromRaw(const float* pRawMatrix);
void set_data_from_raw(const float* raw_matrix);
[[nodiscard]]
Matrix Transpose() const;
Matrix transpose() const;
void Set(float val);
void set(float val);
[[nodiscard]]
const float& At(size_t iRow, size_t iCol) const;
const float& at(size_t row, size_t col) const;
Matrix operator*(const Matrix& other) const;
@@ -72,22 +72,22 @@ namespace omath
Matrix& operator/=(float f);
void Clear();
void clear();
[[nodiscard]]
Matrix Strip(size_t row, size_t column) const;
Matrix strip(size_t row, size_t column) const;
[[nodiscard]]
float Minor(size_t i, size_t j) const;
float minor(size_t i, size_t j) const;
[[nodiscard]]
float AlgComplement(size_t i, size_t j) const;
float alg_complement(size_t i, size_t j) const;
[[nodiscard]]
float Determinant() const;
float determinant() const;
[[nodiscard]]
const float* Raw() const;
const float* raw() const;
Matrix& operator=(const Matrix& other);
@@ -96,7 +96,7 @@ namespace omath
Matrix operator/(float f) const;
[[nodiscard]]
std::string ToString() const;
std::string to_string() const;
~Matrix();

View File

@@ -3,9 +3,9 @@
//
#pragma once
#include <vector>
#include "omath/pathfinding/navigation_mesh.hpp"
#include "omath/vector3.hpp"
#include <vector>
namespace omath::pathfinding
{
@@ -14,17 +14,17 @@ namespace omath::pathfinding
{
public:
[[nodiscard]]
static std::vector<Vector3<float>> FindPath(const Vector3<float>& start, const Vector3<float>& end,
const NavigationMesh& navMesh);
static std::vector<Vector3<float>> find_path(const Vector3<float>& start, const Vector3<float>& end,
const NavigationMesh& nav_mesh);
private:
[[nodiscard]]
static std::vector<Vector3<float>>
ReconstructFinalPath(const std::unordered_map<Vector3<float>, PathNode>& closedList,
const Vector3<float>& current);
reconstruct_final_path(const std::unordered_map<Vector3<float>, PathNode>& closed_list,
const Vector3<float>& current);
[[nodiscard]]
static auto GetPerfectNode(const std::unordered_map<Vector3<float>, PathNode>& openList,
const Vector3<float>& endVertex);
static auto get_perfect_node(const std::unordered_map<Vector3<float>, PathNode>& open_list,
const Vector3<float>& endVertex);
};
} // namespace omath::pathfinding

View File

@@ -4,10 +4,10 @@
#pragma once
#include "omath/vector3.hpp"
#include <expected>
#include <string>
#include <vector>
#include "omath/vector3.hpp"
namespace omath::pathfinding
{
@@ -21,18 +21,18 @@ namespace omath::pathfinding
{
public:
[[nodiscard]]
std::expected<Vector3<float>, std::string> GetClosestVertex(const Vector3<float>& point) const;
std::expected<Vector3<float>, std::string> get_closest_vertex(const Vector3<float>& point) const;
[[nodiscard]]
const std::vector<Vector3<float>>& GetNeighbors(const Vector3<float>& vertex) const;
const std::vector<Vector3<float>>& get_neighbors(const Vector3<float>& vertex) const;
[[nodiscard]]
bool Empty() const;
bool empty() const;
[[nodiscard]] std::vector<uint8_t> Serialize() const;
[[nodiscard]] std::vector<uint8_t> serialize() const;
void Deserialize(const std::vector<uint8_t>& raw);
void deserialize(const std::vector<uint8_t>& raw);
std::unordered_map<Vector3<float>, std::vector<Vector3<float>>> m_verTextMap;
std::unordered_map<Vector3<float>, std::vector<Vector3<float>>> m_vertex_map;
};
} // namespace omath::pathfinding

View File

@@ -6,15 +6,14 @@
#include "omath/projectile_prediction/target.hpp"
#include "omath/vector3.hpp"
namespace omath::projectile_prediction
{
class ProjPredEngine
{
public:
[[nodiscard]]
virtual std::optional<Vector3<float>> MaybeCalculateAimPoint(const Projectile& projectile,
const Target& target) const = 0;
virtual std::optional<Vector3<float>> maybe_calculate_aim_point(const Projectile& projectile,
const Target& target) const = 0;
virtual ~ProjPredEngine() = default;
};
} // namespace omath::projectile_prediction

View File

@@ -6,21 +6,23 @@
namespace omath::projectile_prediction
{
class ProjPredEngineAVX2 final : public ProjPredEngine
class ProjPredEngineAvx2 final : public ProjPredEngine
{
public:
[[nodiscard]] std::optional<Vector3<float>> MaybeCalculateAimPoint(const Projectile& projectile,
const Target& target) const override;
[[nodiscard]] std::optional<Vector3<float>> maybe_calculate_aim_point(const Projectile& projectile,
const Target& target) const override;
ProjPredEngineAVX2(float gravityConstant, float simulationTimeStep, float maximumSimulationTime);
~ProjPredEngineAVX2() override = default;
ProjPredEngineAvx2(float gravity_constant, float simulation_time_step, float maximum_simulation_time);
~ProjPredEngineAvx2() override = default;
private:
[[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;
const float m_maximumSimulationTime;
[[nodiscard]] static std::optional<float> calculate_pitch(const Vector3<float>& proj_origin,
const Vector3<float>& target_pos,
float bullet_gravity, float v0, float time);
// We use [[maybe_unused]] here since AVX2 is not available for ARM and ARM64 CPU
[[maybe_unused]] const float m_gravity_constant;
[[maybe_unused]] const float m_simulation_time_step;
[[maybe_unused]] const float m_maximum_simulation_time;
};
} // namespace omath::projectile_prediction

View File

@@ -4,38 +4,36 @@
#pragma once
#include <optional>
#include "omath/projectile_prediction/proj_pred_engine.hpp"
#include "omath/projectile_prediction/projectile.hpp"
#include "omath/projectile_prediction/target.hpp"
#include "omath/vector3.hpp"
#include <optional>
namespace omath::projectile_prediction
{
class ProjPredEngineLegacy final : public ProjPredEngine
{
public:
explicit ProjPredEngineLegacy(float gravityConstant, float simulationTimeStep, float maximumSimulationTime,
float distanceTolerance);
explicit ProjPredEngineLegacy(float gravity_constant, float simulation_time_step, float maximum_simulation_time,
float distance_tolerance);
[[nodiscard]]
std::optional<Vector3<float>> MaybeCalculateAimPoint(const Projectile& projectile,
const Target& target) const override;
std::optional<Vector3<float>> maybe_calculate_aim_point(const Projectile& projectile,
const Target& target) const override;
private:
const float m_gravityConstant;
const float m_simulationTimeStep;
const float m_maximumSimulationTime;
const float m_distanceTolerance;
const float m_gravity_constant;
const float m_simulation_time_step;
const float m_maximum_simulation_time;
const float m_distance_tolerance;
[[nodiscard]]
std::optional<float> MaybeCalculateProjectileLaunchPitchAngle(const Projectile& projectile,
const Vector3<float>& targetPosition) const;
std::optional<float> maybe_calculate_projectile_launch_pitch_angle(const Projectile& projectile,
const Vector3<float>& target_position) const;
[[nodiscard]]
bool IsProjectileReachedTarget(const Vector3<float>& targetPosition, const Projectile& projectile, float pitch,
float time) const;
bool is_projectile_reached_target(const Vector3<float>& target_position, const Projectile& projectile,
float pitch, float time) const;
};
} // namespace omath::projectile_prediction

View File

@@ -10,12 +10,11 @@ namespace omath::projectile_prediction
class Projectile final
{
public:
[[nodiscard]]
Vector3<float> PredictPosition(float pitch, float yaw, float time, float gravity) const;
Vector3<float> predict_position(float pitch, float yaw, float time, float gravity) const;
Vector3<float> m_origin;
float m_launchSpeed{};
float m_gravityScale{};
float m_launch_speed{};
float m_gravity_scale{};
};
}
} // namespace omath::projectile_prediction

View File

@@ -10,13 +10,12 @@ namespace omath::projectile_prediction
class Target final
{
public:
[[nodiscard]]
constexpr Vector3<float> PredictPosition(const float time, const float gravity) const
constexpr Vector3<float> predict_position(const float time, const float gravity) const
{
auto predicted = m_origin + m_velocity * time;
if (m_isAirborne)
if (m_is_airborne)
predicted.z -= gravity * std::pow(time, 2.f) * 0.5f;
return predicted;
@@ -24,6 +23,6 @@ namespace omath::projectile_prediction
Vector3<float> m_origin;
Vector3<float> m_velocity;
bool m_isAirborne{};
bool m_is_airborne{};
};
}
} // namespace omath::projectile_prediction

View File

@@ -4,12 +4,12 @@
#pragma once
#include "omath/projection/error_codes.hpp"
#include <expected>
#include <omath/angle.hpp>
#include <omath/mat.hpp>
#include <omath/vector3.hpp>
#include <type_traits>
#include "omath/projection/error_codes.hpp"
namespace omath::projection
{
@@ -26,156 +26,149 @@ namespace omath::projection
};
using FieldOfView = Angle<float, 0.f, 180.f, AngleFlags::Clamped>;
template<class Mat4x4Type, class ViewAnglesType>
template<class Mat4X4Type, class ViewAnglesType>
class Camera
{
public:
virtual ~Camera() = default;
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)
Camera(const Vector3<float>& position, const ViewAnglesType& view_angles, const ViewPort& view_port,
const FieldOfView& fov, const float near, const float far)
: m_view_port(view_port), m_field_of_view(fov), m_far_plane_distance(far), m_near_plane_distance(near),
m_view_angles(view_angles), m_origin(position)
{
}
protected:
virtual void LookAt(const Vector3<float>& target) = 0;
virtual void look_at(const Vector3<float>& target) = 0;
[[nodiscard]] virtual Mat4x4Type CalcViewMatrix() const = 0;
[[nodiscard]] virtual Mat4X4Type calc_view_matrix() const = 0;
[[nodiscard]] virtual Mat4x4Type CalcProjectionMatrix() const = 0;
[[nodiscard]] virtual Mat4X4Type calc_projection_matrix() const = 0;
[[nodiscard]] Mat4x4Type CalcViewProjectionMatrix() const
[[nodiscard]] Mat4X4Type calc_view_projection_matrix() const
{
return CalcProjectionMatrix() * CalcViewMatrix();
return calc_projection_matrix() * calc_view_matrix();
}
public:
[[nodiscard]] const Mat4x4Type& GetViewProjectionMatrix() const
[[nodiscard]] const Mat4X4Type& get_view_projection_matrix() const
{
if (!m_viewProjectionMatrix.has_value())
m_viewProjectionMatrix = CalcViewProjectionMatrix();
if (!m_view_projection_matrix.has_value())
m_view_projection_matrix = calc_view_projection_matrix();
return m_viewProjectionMatrix.value();
return m_view_projection_matrix.value();
}
void SetFieldOfView(const FieldOfView& fov)
void set_field_of_view(const FieldOfView& fov)
{
m_fieldOfView = fov;
m_viewProjectionMatrix = std::nullopt;
m_field_of_view = fov;
m_view_projection_matrix = std::nullopt;
}
void SetNearPlane(const float near)
void set_near_plane(const float near)
{
m_nearPlaneDistance = near;
m_viewProjectionMatrix = std::nullopt;
m_near_plane_distance = near;
m_view_projection_matrix = std::nullopt;
}
void SetFarPlane(const float far)
void set_far_plane(const float far)
{
m_farPlaneDistance = far;
m_viewProjectionMatrix = std::nullopt;
m_far_plane_distance = far;
m_view_projection_matrix = std::nullopt;
}
void SetViewAngles(const ViewAnglesType& viewAngles)
void set_view_angles(const ViewAnglesType& view_angles)
{
m_viewAngles = viewAngles;
m_viewProjectionMatrix = std::nullopt;
m_view_angles = view_angles;
m_view_projection_matrix = std::nullopt;
}
void SetOrigin(const Vector3<float>& origin)
void set_origin(const Vector3<float>& origin)
{
m_origin = origin;
m_viewProjectionMatrix = std::nullopt;
m_view_projection_matrix = std::nullopt;
}
void SetViewPort(const ViewPort& viewPort)
void set_view_port(const ViewPort& view_port)
{
m_viewPort = viewPort;
m_viewProjectionMatrix = std::nullopt;
m_view_port = view_port;
m_view_projection_matrix = std::nullopt;
}
[[nodiscard]] const FieldOfView& GetFieldOfView() const
[[nodiscard]] const FieldOfView& get_field_of_view() const
{
return m_fieldOfView;
return m_field_of_view;
}
[[nodiscard]] const float& GetNearPlane() const
[[nodiscard]] const float& get_near_plane() const
{
return m_nearPlaneDistance;
return m_near_plane_distance;
}
[[nodiscard]] const float& GetFarPlane() const
[[nodiscard]] const float& get_far_plane() const
{
return m_farPlaneDistance;
return m_far_plane_distance;
}
[[nodiscard]] const ViewAnglesType& GetViewAngles() const
[[nodiscard]] const ViewAnglesType& get_view_angles() const
{
return m_viewAngles;
return m_view_angles;
}
[[nodiscard]] const Vector3<float>& GetOrigin() const
[[nodiscard]] const Vector3<float>& get_origin() const
{
return m_origin;
}
[[nodiscard]] std::expected<Vector3<float>, Error> WorldToScreen(const Vector3<float>& worldPosition) const
[[nodiscard]] std::expected<Vector3<float>, Error> world_to_screen(const Vector3<float>& world_position) const
{
auto normalizedCords = WorldToViewPort(worldPosition);
auto normalized_cords = world_to_view_port(world_position);
if (!normalizedCords.has_value())
return std::unexpected{normalizedCords.error()};
if (!normalized_cords.has_value())
return std::unexpected{normalized_cords.error()};
return NdcToScreenPosition(*normalizedCords);
return ndc_to_screen_position(*normalized_cords);
}
[[nodiscard]] std::expected<Vector3<float>, Error> WorldToViewPort(const Vector3<float>& worldPosition) const
[[nodiscard]] std::expected<Vector3<float>, Error>
world_to_view_port(const Vector3<float>& world_position) const
{
auto projected = GetViewProjectionMatrix() *
MatColumnFromVector<float, Mat4x4Type::GetStoreOrdering()>(worldPosition);
auto projected = get_view_projection_matrix()
* mat_column_from_vector<float, Mat4X4Type::get_store_ordering()>(world_position);
if (projected.At(3, 0) == 0.0f)
if (projected.at(3, 0) == 0.0f)
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
projected /= projected.At(3, 0);
projected /= projected.at(3, 0);
if (IsNdcOutOfBounds(projected))
if (is_ndc_out_of_bounds(projected))
return std::unexpected(Error::WORLD_POSITION_IS_OUT_OF_SCREEN_BOUNDS);
return Vector3<float>{projected.At(0, 0), projected.At(1, 0), projected.At(2, 0)};
return Vector3<float>{projected.at(0, 0), projected.at(1, 0), projected.at(2, 0)};
}
protected:
ViewPort m_viewPort{};
Angle<float, 0.f, 180.f, AngleFlags::Clamped> m_fieldOfView;
ViewPort m_view_port{};
Angle<float, 0.f, 180.f, AngleFlags::Clamped> m_field_of_view;
mutable std::optional<Mat4x4Type> m_viewProjectionMatrix;
mutable std::optional<Mat4X4Type> m_view_projection_matrix;
float m_farPlaneDistance;
float m_nearPlaneDistance;
float m_far_plane_distance;
float m_near_plane_distance;
ViewAnglesType m_viewAngles;
ViewAnglesType m_view_angles;
Vector3<float> m_origin;
private:
template<class Type>
[[nodiscard]]
constexpr static bool IsNdcOutOfBounds(const Type& ndc)
template<class Type> [[nodiscard]]
constexpr static bool is_ndc_out_of_bounds(const Type& ndc)
{
return std::ranges::any_of(ndc.RawArray(), [](const auto& val) { return val < -1 || val > 1; });
return std::ranges::any_of(ndc.raw_array(), [](const auto& val) { return val < -1 || val > 1; });
}
[[nodiscard]] Vector3<float> NdcToScreenPosition(const Vector3<float>& ndc) const
[[nodiscard]] Vector3<float> ndc_to_screen_position(const Vector3<float>& ndc) const
{
return
{
(ndc.x + 1.f) / 2.f * m_viewPort.m_width,
(1.f - ndc.y) / 2.f * m_viewPort.m_height,
ndc.z
};
return {(ndc.x + 1.f) / 2.f * m_view_port.m_width, (1.f - ndc.y) / 2.f * m_view_port.m_height, ndc.z};
}
};
} // namespace omath::projection

View File

@@ -5,7 +5,6 @@
#pragma once
#include <cstdint>
namespace omath::projection
{
enum class Error : uint16_t

View File

@@ -6,15 +6,14 @@
namespace omath
{
/*
|\
| \
a | \ hypot
| \
-----
b
*/
/*
|\
| \
a | \ hypot
| \
-----
b
*/
template<class Vector>
class Triangle final
@@ -31,52 +30,53 @@ namespace omath
Vector3<float> m_vertex3;
[[nodiscard]]
constexpr Vector3<float> CalculateNormal() const
constexpr Vector3<float> calculate_normal() const
{
const auto b = SideBVector();
const auto a = SideAVector();
return b.Cross(a).Normalized();
const auto b = side_b_vector();
const auto a = side_a_vector();
return b.cross(a).normalized();
}
[[nodiscard]]
float SideALength() const
float side_a_length() const
{
return m_vertex1.DistTo(m_vertex2);
return m_vertex1.distance_to(m_vertex2);
}
[[nodiscard]]
float SideBLength() const
float side_b_length() const
{
return m_vertex3.DistTo(m_vertex2);
return m_vertex3.distance_to(m_vertex2);
}
[[nodiscard]]
constexpr Vector3<float> SideAVector() const
constexpr Vector3<float> side_a_vector() const
{
return m_vertex1 - m_vertex2;
}
[[nodiscard]]
constexpr float Hypot() const
constexpr float hypot() const
{
return m_vertex1.DistTo(m_vertex3);
return m_vertex1.distance_to(m_vertex3);
}
[[nodiscard]]
constexpr bool IsRectangular() const
constexpr bool is_rectangular() const
{
const auto sideA = SideALength();
const auto sideB = SideBLength();
const auto hypot = Hypot();
const auto side_a = side_a_length();
const auto side_b = side_b_length();
const auto hypot_value = hypot();
return std::abs(sideA*sideA + sideB*sideB - hypot*hypot) <= 0.0001f;
return std::abs(side_a * side_a + side_b * side_b - hypot_value * hypot_value) <= 0.0001f;
}
[[nodiscard]]
constexpr Vector3<float> SideBVector() const
constexpr Vector3<float> side_b_vector() const
{
return m_vertex3 - m_vertex2;
}
[[nodiscard]]
constexpr Vector3<float> MidPoint() const
constexpr Vector3<float> mid_point() const
{
return (m_vertex1 + m_vertex2 + m_vertex3) / 3;
}

View File

@@ -10,12 +10,11 @@
#include <imgui.h>
#endif
namespace omath
{
template<class Type>
requires std::is_arithmetic_v<Type>
requires std::is_arithmetic_v<Type>
class Vector2
{
public:
@@ -25,7 +24,7 @@ namespace omath
// Constructors
constexpr Vector2() = default;
constexpr Vector2(const Type& x, const Type& y) : x(x), y(y)
constexpr Vector2(const Type& x, const Type& y): x(x), y(y)
{
}
@@ -108,30 +107,30 @@ namespace omath
}
// Basic vector operations
[[nodiscard]] Type DistTo(const Vector2& vOther) const
[[nodiscard]] Type distance_to(const Vector2& other) const
{
return std::sqrt(DistToSqr(vOther));
return std::sqrt(distance_to_sqr(other));
}
[[nodiscard]] constexpr Type DistToSqr(const Vector2& vOther) const
[[nodiscard]] constexpr Type distance_to_sqr(const Vector2& other) const
{
return (x - vOther.x) * (x - vOther.x) + (y - vOther.y) * (y - vOther.y);
return (x - other.x) * (x - other.x) + (y - other.y) * (y - other.y);
}
[[nodiscard]] constexpr Type Dot(const Vector2& vOther) const
[[nodiscard]] constexpr Type dot(const Vector2& other) const
{
return x * vOther.x + y * vOther.y;
return x * other.x + y * other.y;
}
#ifndef _MSC_VER
[[nodiscard]] constexpr Type Length() const
[[nodiscard]] constexpr Type length() const
{
return std::hypot(this->x, this->y);
}
[[nodiscard]] constexpr Vector2 Normalized() const
[[nodiscard]] constexpr Vector2 normalized() const
{
const Type len = Length();
const Type len = length();
return len > 0.f ? *this / len : *this;
}
#else
@@ -146,12 +145,12 @@ namespace omath
return len > 0.f ? *this / len : *this;
}
#endif
[[nodiscard]] constexpr Type LengthSqr() const
[[nodiscard]] constexpr Type length_sqr() const
{
return x * x + y * y;
}
constexpr Vector2& Abs()
constexpr Vector2& abs()
{
// FIXME: Replace with std::abs, if it will become constexprable
x = x < 0 ? -x : x;
@@ -186,20 +185,20 @@ namespace omath
}
// Sum of elements
[[nodiscard]] constexpr Type Sum() const
[[nodiscard]] constexpr Type sum() const
{
return x + y;
}
[[nodiscard]]
constexpr std::tuple<Type, Type> AsTuple() const
constexpr std::tuple<Type, Type> as_tuple() const
{
return std::make_tuple(x, y);
}
#ifdef OMATH_IMGUI_INTEGRATION
[[nodiscard]]
ImVec2 ToImVec2() const
ImVec2 to_im_vec2() const
{
return {static_cast<float>(this->x), static_cast<float>(this->y)};
}

View File

@@ -4,11 +4,11 @@
#pragma once
#include "omath/angle.hpp"
#include "omath/vector2.hpp"
#include <cstdint>
#include <expected>
#include <functional>
#include "omath/angle.hpp"
#include "omath/vector2.hpp"
namespace omath
{
@@ -18,13 +18,16 @@ namespace omath
IMPOSSIBLE_BETWEEN_ANGLE,
};
template<class Type> requires std::is_arithmetic_v<Type>
template<class Type>
requires std::is_arithmetic_v<Type>
class Vector3 : public Vector2<Type>
{
public:
Type z = static_cast<Type>(0);
constexpr Vector3(const Type& x, const Type& y, const Type& z) : Vector2<Type>(x, y), z(z) { }
constexpr Vector3() : Vector2<Type>() {};
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
{
@@ -100,72 +103,71 @@ namespace omath
return *this;
}
constexpr Vector3& Abs()
constexpr Vector3& abs()
{
Vector2<Type>::Abs();
Vector2<Type>::abs();
z = z < 0.f ? -z : z;
return *this;
}
[[nodiscard]] constexpr Type DistToSqr(const Vector3& vOther) const
[[nodiscard]] constexpr Type distance_to_sqr(const Vector3& other) const
{
return (*this - vOther).LengthSqr();
return (*this - other).length_sqr();
}
[[nodiscard]] constexpr Type Dot(const Vector3& vOther) const
[[nodiscard]] constexpr Type dot(const Vector3& other) const
{
return Vector2<Type>::Dot(vOther) + z * vOther.z;
return Vector2<Type>::dot(other) + z * other.z;
}
#ifndef _MSC_VER
[[nodiscard]] constexpr Type Length() const
[[nodiscard]] constexpr Type length() const
{
return std::hypot(this->x, this->y, z);
}
[[nodiscard]] constexpr Type Length2D() const
[[nodiscard]] constexpr Type length_2d() const
{
return Vector2<Type>::Length();
return Vector2<Type>::length();
}
[[nodiscard]] Type DistTo(const Vector3& vOther) const
[[nodiscard]] Type distance_to(const Vector3& other) const
{
return (*this - vOther).Length();
return (*this - other).length();
}
[[nodiscard]] constexpr Vector3 Normalized() const
[[nodiscard]] constexpr Vector3 normalized() const
{
const Type length = this->Length();
const Type length_value = this->length();
return length != 0 ? *this / length : *this;
return length_value != 0 ? *this / length_value : *this;
}
#else
[[nodiscard]] Type Length() const
[[nodiscard]] Type length() const
{
return std::hypot(this->x, this->y, z);
}
[[nodiscard]] Vector3 Normalized() const
[[nodiscard]] Vector3 normalized() const
{
const Type length = this->Length();
return length != 0 ? *this / length : *this;
}
[[nodiscard]] Type Length2D() const
[[nodiscard]] Type length_2d() const
{
return Vector2<Type>::Length();
}
[[nodiscard]] Type DistTo(const Vector3& vOther) const
[[nodiscard]] Type distance_to(const Vector3& vOther) const
{
return (*this - vOther).Length();
}
#endif
[[nodiscard]] constexpr Type LengthSqr() const
[[nodiscard]] constexpr Type length_sqr() const
{
return Vector2<Type>::LengthSqr() + z * z;
return Vector2<Type>::length_sqr() + z * z;
}
[[nodiscard]] constexpr Vector3 operator-() const
@@ -203,79 +205,69 @@ namespace omath
return {this->x / v.x, this->y / v.y, z / v.z};
}
[[nodiscard]] constexpr Vector3 Cross(const Vector3 &v) const
[[nodiscard]] constexpr Vector3 cross(const Vector3& v) const
{
return
{
this->y * v.z - z * v.y,
z * v.x - this->x * v.z,
this->x * v.y - this->y * v.x
};
return {this->y * v.z - z * v.y, z * v.x - this->x * v.z, this->x * v.y - this->y * v.x};
}
[[nodiscard]] constexpr Type Sum() const
[[nodiscard]] constexpr Type sum() const
{
return Sum2D() + z;
return sum_2d() + z;
}
[[nodiscard]] std::expected<Angle<float, 0.f, 180.f, AngleFlags::Clamped>, Vector3Error>
AngleBetween(const Vector3& other) const
angle_between(const Vector3& other) const
{
const auto bottom = Length() * other.Length();
const auto bottom = length() * other.length();
if (bottom == 0.f)
return std::unexpected(Vector3Error::IMPOSSIBLE_BETWEEN_ANGLE);
return Angle<float, 0.f, 180.f, AngleFlags::Clamped>::FromRadians(std::acos(Dot(other) / bottom));
return Angle<float, 0.f, 180.f, AngleFlags::Clamped>::from_radians(std::acos(dot(other) / bottom));
}
[[nodiscard]] bool IsPerpendicular(const Vector3& other) const
[[nodiscard]] bool is_perpendicular(const Vector3& other) const
{
if (const auto angle = AngleBetween(other))
return angle->AsDegrees() == 90.f;
if (const auto angle = angle_between(other))
return angle->as_degrees() == 90.f;
return false;
}
[[nodiscard]] constexpr Type Sum2D() const
[[nodiscard]] constexpr Type sum_2d() const
{
return Vector2<Type>::Sum();
return Vector2<Type>::sum();
}
[[nodiscard]] constexpr std::tuple<Type, Type, Type> AsTuple() const
[[nodiscard]] constexpr std::tuple<Type, Type, Type> as_tuple() const
{
return std::make_tuple(this->x, this->y, z);
}
[[nodiscard]] Vector3 ViewAngleTo(const Vector3 &other) const
[[nodiscard]] Vector3 view_angle_to(const Vector3& other) const
{
const float distance = DistTo(other);
const float distance = distance_to(other);
const auto delta = other - *this;
return
{
angles::RadiansToDegrees(std::asin(delta.z / distance)),
angles::RadiansToDegrees(std::atan2(delta.y, delta.x)),
0
};
return {angles::radians_to_degrees(std::asin(delta.z / distance)),
angles::radians_to_degrees(std::atan2(delta.y, delta.x)), 0};
}
};
}
} // namespace omath
// ReSharper disable once CppRedundantNamespaceDefinition
namespace std
{
template<>
struct hash<omath::Vector3<float>>
template<> struct hash<omath::Vector3<float>>
{
std::size_t operator()(const omath::Vector3<float>& vec) const noexcept
{
std::size_t hash = 0;
constexpr std::hash<float> hasher;
hash ^= hasher(vec.x) + 0x9e3779b9 + (hash<<6) + (hash>>2);
hash ^= hasher(vec.y) + 0x9e3779b9 + (hash<<6) + (hash>>2);
hash ^= hasher(vec.z) + 0x9e3779b9 + (hash<<6) + (hash>>2);
hash ^= hasher(vec.x) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
hash ^= hasher(vec.y) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
hash ^= hasher(vec.z) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
return hash;
}
};
}
} // namespace std

View File

@@ -6,17 +6,18 @@
#include <algorithm>
#include <omath/vector3.hpp>
namespace omath
{
template <class Type>
template<class Type>
class Vector4 : public Vector3<Type>
{
public:
Type w;
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) {};
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
@@ -77,29 +78,29 @@ namespace omath
return *this;
}
[[nodiscard]] constexpr Type LengthSqr() const
[[nodiscard]] constexpr Type length_sqr() const
{
return Vector3<Type>::LengthSqr() + w * w;
return Vector3<Type>::length_sqr() + w * w;
}
[[nodiscard]] constexpr Type Dot(const Vector4& vOther) const
[[nodiscard]] constexpr Type dot(const Vector4& other) const
{
return Vector3<Type>::Dot(vOther) + w * vOther.w;
return Vector3<Type>::dot(other) + w * other.w;
}
[[nodiscard]] Vector3<Type> Length() const
[[nodiscard]] Vector3<Type> length() const
{
return std::sqrt(LengthSqr());
return std::sqrt(length_sqr());
}
constexpr Vector4& Abs()
constexpr Vector4& abs()
{
Vector3<Type>::Abs();
Vector3<Type>::abs();
w = w < 0.f ? -w : w;
return *this;
}
constexpr Vector4& Clamp(const Type& min, const Type& max)
constexpr Vector4& clamp(const Type& min, const Type& max)
{
this->x = std::clamp(this->x, min, max);
this->y = std::clamp(this->y, min, max);
@@ -151,23 +152,22 @@ namespace omath
}
[[nodiscard]]
constexpr Type Sum() const
constexpr Type sum() const
{
return Vector3<Type>::Sum() + w;
return Vector3<Type>::sum() + w;
}
#ifdef OMATH_IMGUI_INTEGRATION
[[nodiscard]]
ImVec4 ToImVec4() const
{
return
{
static_cast<float>(this->x),
static_cast<float>(this->y),
static_cast<float>(this->z),
static_cast<float>(w),
return {
static_cast<float>(this->x),
static_cast<float>(this->y),
static_cast<float>(this->z),
static_cast<float>(w),
};
}
#endif
};
}
} // namespace omath

View File

@@ -12,4 +12,4 @@ namespace omath
YawType yaw;
RollType roll;
};
}
} // namespace omath