mirror of
https://github.com/orange-cpp/omath.git
synced 2026-08-07 21:42:05 +00:00
Compare commits
10 Commits
8a591ce769
...
v5.4.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 306096af14 | |||
| 502cfb431c | |||
| 8ccc9459d4 | |||
| 2ee485297f | |||
| 6a5771e57d | |||
| 58c082d399 | |||
| 11e6f67e23 | |||
| d7f9ab6e85 | |||
| ceecdde9dc | |||
| a546c1a6d1 |
@@ -873,7 +873,7 @@ namespace omath
|
||||
} // namespace omath
|
||||
|
||||
template<size_t Rows, size_t Columns, class Type, omath::MatStoreType StoreType>
|
||||
struct std::formatter<omath::Mat<Rows, Columns, Type, StoreType>> // NOLINT(*-dcl58-cpp)
|
||||
struct std::formatter<omath::Mat<Rows, Columns, Type, StoreType>> final // NOLINT(*-dcl58-cpp)
|
||||
{
|
||||
using MatType = omath::Mat<Rows, Columns, Type, StoreType>;
|
||||
[[nodiscard("You must use parse iterator")]]
|
||||
|
||||
@@ -265,13 +265,14 @@ namespace omath
|
||||
};
|
||||
} // namespace omath
|
||||
|
||||
template<> struct std::hash<omath::Vector2<float>>
|
||||
template<class Type>
|
||||
struct std::hash<omath::Vector2<Type>> final // NOLINT(*-dcl58-cpp)
|
||||
{
|
||||
[[nodiscard("You must use hash value")]]
|
||||
std::size_t operator()(const omath::Vector2<float>& vec) const noexcept
|
||||
std::size_t operator()(const omath::Vector2<Type>& vec) const noexcept
|
||||
{
|
||||
std::size_t hash = 0;
|
||||
constexpr std::hash<float> hasher;
|
||||
constexpr std::hash<Type> hasher;
|
||||
|
||||
hash ^= hasher(vec.x) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
||||
hash ^= hasher(vec.y) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
||||
@@ -281,10 +282,10 @@ template<> struct std::hash<omath::Vector2<float>>
|
||||
};
|
||||
|
||||
template<class Type>
|
||||
struct std::formatter<omath::Vector2<Type>> // NOLINT(*-dcl58-cpp)
|
||||
struct std::formatter<omath::Vector2<Type>> final // NOLINT(*-dcl58-cpp)
|
||||
{
|
||||
[[nodiscard("You must use parse iterator")]]
|
||||
static constexpr auto parse(std::format_parse_context& ctx)
|
||||
static constexpr auto parse(std::format_parse_context& ctx) noexcept
|
||||
{
|
||||
return ctx.begin();
|
||||
}
|
||||
|
||||
@@ -318,14 +318,15 @@ namespace omath
|
||||
};
|
||||
} // namespace omath
|
||||
|
||||
template<> struct std::hash<omath::Vector3<float>>
|
||||
template<class Type>
|
||||
struct std::hash<omath::Vector3<Type>> final // NOLINT(*-dcl58-cpp)
|
||||
{
|
||||
// NOTE: Cannot be constexpr because of MSVC
|
||||
[[nodiscard("You must use hash value")]]
|
||||
std::size_t operator()(const omath::Vector3<float>& vec) const noexcept
|
||||
std::size_t operator()(const omath::Vector3<Type>& vec) const noexcept
|
||||
{
|
||||
std::size_t hash = 0;
|
||||
constexpr std::hash<float> hasher;
|
||||
constexpr std::hash<Type> hasher;
|
||||
|
||||
hash ^= hasher(vec.x) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
||||
hash ^= hasher(vec.y) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
||||
@@ -336,10 +337,10 @@ template<> struct std::hash<omath::Vector3<float>>
|
||||
};
|
||||
|
||||
template<class Type>
|
||||
struct std::formatter<omath::Vector3<Type>> // NOLINT(*-dcl58-cpp)
|
||||
struct std::formatter<omath::Vector3<Type>> final // NOLINT(*-dcl58-cpp)
|
||||
{
|
||||
[[nodiscard("You must use parse iterator")]]
|
||||
static constexpr auto parse(std::format_parse_context& ctx)
|
||||
static constexpr auto parse(std::format_parse_context& ctx) noexcept
|
||||
{
|
||||
return ctx.begin();
|
||||
}
|
||||
|
||||
@@ -225,13 +225,14 @@ namespace omath
|
||||
};
|
||||
} // namespace omath
|
||||
|
||||
template<> struct std::hash<omath::Vector4<float>>
|
||||
template<class Type>
|
||||
struct std::hash<omath::Vector4<Type>> final // NOLINT(*-dcl58-cpp)
|
||||
{
|
||||
[[nodiscard("You must use hash value")]]
|
||||
std::size_t operator()(const omath::Vector4<float>& vec) const noexcept
|
||||
std::size_t operator()(const omath::Vector4<Type>& vec) const noexcept
|
||||
{
|
||||
std::size_t hash = 0;
|
||||
constexpr std::hash<float> hasher;
|
||||
constexpr std::hash<Type> hasher;
|
||||
|
||||
hash ^= hasher(vec.x) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
||||
hash ^= hasher(vec.y) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
|
||||
@@ -240,12 +241,11 @@ template<> struct std::hash<omath::Vector4<float>>
|
||||
return hash;
|
||||
}
|
||||
};
|
||||
|
||||
template<class Type>
|
||||
struct std::formatter<omath::Vector4<Type>> // NOLINT(*-dcl58-cpp)
|
||||
struct std::formatter<omath::Vector4<Type>> final // NOLINT(*-dcl58-cpp)
|
||||
{
|
||||
[[nodiscard("You must use parse iterator")]]
|
||||
static constexpr auto parse(std::format_parse_context& ctx)
|
||||
static constexpr auto parse(std::format_parse_context& ctx) noexcept
|
||||
{
|
||||
return ctx.begin();
|
||||
}
|
||||
|
||||
@@ -13,12 +13,12 @@ namespace omath::pathfinding
|
||||
class Astar final
|
||||
{
|
||||
public:
|
||||
[[nodiscard]]
|
||||
[[nodiscard("You forgot to use returned path")]]
|
||||
static std::vector<Vector3<float>> find_path(const Vector3<float>& start, const Vector3<float>& end,
|
||||
const NavigationMesh& nav_mesh) noexcept;
|
||||
|
||||
private:
|
||||
[[nodiscard]]
|
||||
[[nodiscard("You forgot to use reconstructed path")]]
|
||||
static std::vector<Vector3<float>>
|
||||
reconstruct_final_path(const std::unordered_map<Vector3<float>, PathNode>& closed_list,
|
||||
const Vector3<float>& current) noexcept;
|
||||
|
||||
@@ -238,7 +238,8 @@ namespace omath::projection
|
||||
return m_view_projection_matrix.value();
|
||||
}
|
||||
|
||||
[[nodiscard("You must use view matrix")]] constexpr const Mat4X4Type& get_view_matrix() const noexcept
|
||||
[[nodiscard("You must use view matrix")]]
|
||||
constexpr const Mat4X4Type& get_view_matrix() const noexcept
|
||||
{
|
||||
if (!m_view_matrix.has_value())
|
||||
m_view_matrix = calc_view_matrix();
|
||||
@@ -295,27 +296,32 @@ namespace omath::projection
|
||||
m_projection_matrix = std::nullopt;
|
||||
}
|
||||
|
||||
[[nodiscard("You must use field of view")]] constexpr const FieldOfView& get_field_of_view() const noexcept
|
||||
[[nodiscard("You must use field of view")]]
|
||||
constexpr const FieldOfView& get_field_of_view() const noexcept
|
||||
{
|
||||
return m_field_of_view;
|
||||
}
|
||||
|
||||
[[nodiscard("You must use near plane")]] constexpr const NumericType& get_near_plane() const noexcept
|
||||
[[nodiscard("You must use near plane")]]
|
||||
constexpr const NumericType& get_near_plane() const noexcept
|
||||
{
|
||||
return m_near_plane_distance;
|
||||
}
|
||||
|
||||
[[nodiscard("You must use far plane")]] constexpr const NumericType& get_far_plane() const noexcept
|
||||
[[nodiscard("You must use far plane")]]
|
||||
constexpr const NumericType& get_far_plane() const noexcept
|
||||
{
|
||||
return m_far_plane_distance;
|
||||
}
|
||||
|
||||
[[nodiscard("You must use view angles")]] constexpr const ViewAnglesType& get_view_angles() const noexcept
|
||||
[[nodiscard("You must use view angles")]]
|
||||
constexpr const ViewAnglesType& get_view_angles() const noexcept
|
||||
{
|
||||
return m_view_angles;
|
||||
}
|
||||
|
||||
[[nodiscard("You must use origin")]] constexpr const Vector3<NumericType>& get_origin() const noexcept
|
||||
[[nodiscard("You must use origin")]]
|
||||
constexpr const Vector3<NumericType>& get_origin() const noexcept
|
||||
{
|
||||
return m_origin;
|
||||
}
|
||||
@@ -460,6 +466,23 @@ namespace omath::projection
|
||||
return false;
|
||||
}
|
||||
|
||||
[[nodiscard("You must view camera space coordinates")]]
|
||||
constexpr Vector3<NumericType> world_to_view_coordinates(const Vector3<NumericType>& world_coordinates) const noexcept
|
||||
{
|
||||
if consteval
|
||||
{
|
||||
const auto view_coordinates =
|
||||
calc_view_matrix()
|
||||
* mat_column_from_vector<NumericType, Mat4X4Type::get_store_ordering()>(world_coordinates);
|
||||
|
||||
return {view_coordinates.at(0, 0), view_coordinates.at(1, 0), view_coordinates.at(2, 0)};
|
||||
}
|
||||
const auto view_coordinates =
|
||||
get_view_matrix()
|
||||
* mat_column_from_vector<NumericType, Mat4X4Type::get_store_ordering()>(world_coordinates);
|
||||
|
||||
return {view_coordinates.at(0, 0), view_coordinates.at(1, 0), view_coordinates.at(2, 0)};
|
||||
}
|
||||
[[nodiscard("You must use view port position")]] constexpr std::expected<Vector3<NumericType>, Error>
|
||||
world_to_view_port(const Vector3<NumericType>& world_position,
|
||||
const ViewPortClipping& clipping = ViewPortClipping::AUTO) const noexcept
|
||||
|
||||
@@ -21,36 +21,20 @@
|
||||
|
||||
namespace omath::rev_eng
|
||||
{
|
||||
template<std::size_t N>
|
||||
struct FixedString final
|
||||
{
|
||||
char data[N]{};
|
||||
// ReSharper disable once CppNonExplicitConvertingConstructor
|
||||
constexpr FixedString(const char (&str)[N]) noexcept // NOLINT(*-explicit-constructor)
|
||||
{
|
||||
for (std::size_t i = 0; i < N; ++i)
|
||||
data[i] = str[i];
|
||||
}
|
||||
// ReSharper disable once CppNonExplicitConversionOperator
|
||||
constexpr operator std::string_view() const noexcept // NOLINT(*-explicit-constructor)
|
||||
{
|
||||
return {data, N - 1};
|
||||
}
|
||||
};
|
||||
template<std::size_t N>
|
||||
FixedString(const char (&)[N]) -> FixedString<N>;
|
||||
|
||||
class InternalReverseEngineeredObject
|
||||
{
|
||||
protected:
|
||||
template<class Type>
|
||||
[[nodiscard]] Type& get_by_offset(const std::ptrdiff_t offset)
|
||||
[[nodiscard("You must use value")]]
|
||||
Type& get_by_offset(const std::ptrdiff_t offset) noexcept
|
||||
{
|
||||
return *reinterpret_cast<Type*>(reinterpret_cast<std::uintptr_t>(this) + offset);
|
||||
}
|
||||
|
||||
template<class Type>
|
||||
[[nodiscard]] const Type& get_by_offset(const std::ptrdiff_t offset) const
|
||||
[[nodiscard("You must use value")]]
|
||||
const Type& get_by_offset(const std::ptrdiff_t offset) const noexcept
|
||||
{
|
||||
return *reinterpret_cast<Type*>(reinterpret_cast<std::uintptr_t>(this) + offset);
|
||||
}
|
||||
@@ -76,14 +60,16 @@ namespace omath::rev_eng
|
||||
return reinterpret_cast<MethodType>(const_cast<void*>(ptr))(this, arg_list...);
|
||||
}
|
||||
|
||||
template<FixedString ModuleName, FixedString Pattern, class ReturnType>
|
||||
template<PatternScanner::ConstevalPattern ModuleName, PatternScanner::ConstevalPattern Pattern,
|
||||
class ReturnType>
|
||||
ReturnType call_method(auto... arg_list)
|
||||
{
|
||||
static const auto* address = resolve_pattern(ModuleName, Pattern);
|
||||
return call_method<ReturnType>(address, arg_list...);
|
||||
}
|
||||
|
||||
template<FixedString ModuleName, FixedString Pattern, class ReturnType>
|
||||
template<PatternScanner::ConstevalPattern ModuleName, PatternScanner::ConstevalPattern Pattern,
|
||||
class ReturnType>
|
||||
ReturnType call_method(auto... arg_list) const
|
||||
{
|
||||
static const auto* address = resolve_pattern(ModuleName, Pattern);
|
||||
@@ -91,14 +77,15 @@ namespace omath::rev_eng
|
||||
}
|
||||
|
||||
template<class ReturnType>
|
||||
ReturnType call_method(const std::string_view& module_name,const std::string_view& pattern, auto... arg_list)
|
||||
ReturnType call_method(const std::string_view& module_name, const std::string_view& pattern, auto... arg_list)
|
||||
{
|
||||
static const auto* address = resolve_pattern(module_name, pattern);
|
||||
return call_method<ReturnType>(address, arg_list...);
|
||||
}
|
||||
|
||||
template<class ReturnType>
|
||||
ReturnType call_method(const std::string_view& module_name,const std::string_view& pattern, auto... arg_list) const
|
||||
ReturnType call_method(const std::string_view& module_name, const std::string_view& pattern,
|
||||
auto... arg_list) const
|
||||
{
|
||||
static const auto* address = resolve_pattern(module_name, pattern);
|
||||
return call_method<ReturnType>(address, arg_list...);
|
||||
@@ -119,26 +106,24 @@ namespace omath::rev_eng
|
||||
template<std::ptrdiff_t TableOffset, std::size_t Id, class ReturnType>
|
||||
ReturnType call_virtual_method(auto... arg_list)
|
||||
{
|
||||
auto sub_this = reinterpret_cast<void*>(
|
||||
reinterpret_cast<std::uintptr_t>(this) + TableOffset);
|
||||
auto sub_this = reinterpret_cast<void*>(reinterpret_cast<std::uintptr_t>(this) + TableOffset);
|
||||
const auto vtable = *reinterpret_cast<void***>(sub_this);
|
||||
#ifdef _MSC_VER
|
||||
using Fn = ReturnType(__thiscall*)(void*, decltype(arg_list)...);
|
||||
#else
|
||||
using Fn = ReturnType(*)(void*, decltype(arg_list)...);
|
||||
using Fn = ReturnType (*)(void*, decltype(arg_list)...);
|
||||
#endif
|
||||
return reinterpret_cast<Fn>(vtable[Id])(sub_this, arg_list...);
|
||||
}
|
||||
template<std::ptrdiff_t TableOffset, std::size_t Id, class ReturnType>
|
||||
ReturnType call_virtual_method(auto... arg_list) const
|
||||
{
|
||||
auto sub_this = reinterpret_cast<const void*>(
|
||||
reinterpret_cast<std::uintptr_t>(this) + TableOffset);
|
||||
auto sub_this = reinterpret_cast<const void*>(reinterpret_cast<std::uintptr_t>(this) + TableOffset);
|
||||
const auto vtable = *reinterpret_cast<void* const* const*>(sub_this);
|
||||
#ifdef _MSC_VER
|
||||
using Fn = ReturnType(__thiscall*)(const void*, decltype(arg_list)...);
|
||||
#else
|
||||
using Fn = ReturnType(*)(const void*, decltype(arg_list)...);
|
||||
using Fn = ReturnType (*)(const void*, decltype(arg_list)...);
|
||||
#endif
|
||||
return reinterpret_cast<Fn>(vtable[Id])(sub_this, arg_list...);
|
||||
}
|
||||
@@ -161,8 +146,8 @@ namespace omath::rev_eng
|
||||
return reinterpret_cast<const void*>(*result);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
static const void* get_module_base(const std::string_view module_name)
|
||||
[[nodiscard("You forgot to use module base address")]]
|
||||
static const void* get_module_base(const std::string_view module_name) noexcept
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return GetModuleHandleA(module_name.data());
|
||||
|
||||
@@ -154,10 +154,11 @@ namespace omath
|
||||
} // namespace omath
|
||||
|
||||
template<class T, T MinV, T MaxV, omath::AngleFlags F>
|
||||
struct std::formatter<omath::Angle<T, MinV, MaxV, F>, char> // NOLINT(*-dcl58-cpp)
|
||||
struct std::formatter<omath::Angle<T, MinV, MaxV, F>, char> final // NOLINT(*-dcl58-cpp)
|
||||
{
|
||||
using AngleT = omath::Angle<T, MinV, MaxV, F>;
|
||||
|
||||
[[nodiscard]]
|
||||
static constexpr auto parse(std::format_parse_context& ctx)
|
||||
{
|
||||
return ctx.begin();
|
||||
@@ -174,10 +175,11 @@ struct std::formatter<omath::Angle<T, MinV, MaxV, F>, char> // NOLINT(*-dcl58-cp
|
||||
|
||||
// wchar_t formatter
|
||||
template<class T, T MinV, T MaxV, omath::AngleFlags F>
|
||||
struct std::formatter<omath::Angle<T, MinV, MaxV, F>, wchar_t> // NOLINT(*-dcl58-cpp)
|
||||
struct std::formatter<omath::Angle<T, MinV, MaxV, F>, wchar_t> final // NOLINT(*-dcl58-cpp)
|
||||
{
|
||||
using AngleT = omath::Angle<T, MinV, MaxV, F>;
|
||||
|
||||
[[nodiscard]]
|
||||
static constexpr auto parse(std::wformat_parse_context& ctx)
|
||||
{
|
||||
return ctx.begin();
|
||||
@@ -194,10 +196,11 @@ struct std::formatter<omath::Angle<T, MinV, MaxV, F>, wchar_t> // NOLINT(*-dcl58
|
||||
|
||||
// wchar_t formatter
|
||||
template<class T, T MinV, T MaxV, omath::AngleFlags F>
|
||||
struct std::formatter<omath::Angle<T, MinV, MaxV, F>, char8_t> // NOLINT(*-dcl58-cpp)
|
||||
struct std::formatter<omath::Angle<T, MinV, MaxV, F>, char8_t> final // NOLINT(*-dcl58-cpp)
|
||||
{
|
||||
using AngleT = omath::Angle<T, MinV, MaxV, F>;
|
||||
|
||||
[[nodiscard]]
|
||||
static constexpr auto parse(std::wformat_parse_context& ctx)
|
||||
{
|
||||
return ctx.begin();
|
||||
|
||||
@@ -38,17 +38,24 @@ namespace omath
|
||||
friend unit_test_pattern_scan_consteval_spacing_and_case_Test;
|
||||
|
||||
public:
|
||||
template<std::size_t N>
|
||||
template<std::size_t Length>
|
||||
struct ConstevalPattern final
|
||||
{
|
||||
char value[N]{};
|
||||
char value[Length]{};
|
||||
|
||||
// ReSharper disable once CppNonExplicitConvertingConstructor
|
||||
constexpr ConstevalPattern(const char (&text)[N]) // NOLINT(*-explicit-constructor)
|
||||
constexpr ConstevalPattern(const char (&text)[Length]) // NOLINT(*-explicit-constructor)
|
||||
{
|
||||
std::ranges::copy(text, value);
|
||||
}
|
||||
[[nodiscard("You forgot to use string")]]
|
||||
constexpr operator std::string_view() const noexcept // NOLINT(*-explicit-constructor)
|
||||
{
|
||||
return {value, Length - 1};
|
||||
}
|
||||
};
|
||||
template<std::size_t N>
|
||||
ConstevalPattern(const char (&)[N]) -> ConstevalPattern<N>;
|
||||
|
||||
[[nodiscard]]
|
||||
static std::span<std::byte>::iterator scan_for_pattern(const std::span<std::byte>& range,
|
||||
|
||||
@@ -580,6 +580,48 @@ TEST(UnitTestProjection, AabbUnityEngineStraddlesNearNotCulled)
|
||||
EXPECT_FALSE(cam.is_aabb_culled_by_frustum(aabb));
|
||||
}
|
||||
|
||||
TEST(UnitTestProjection, WorldToViewCoordinates_TranslatedSourceCamera)
|
||||
{
|
||||
constexpr float k_eps = 1e-4f;
|
||||
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
|
||||
auto cam = omath::source_engine::Camera({10.f, 20.f, 30.f}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||
|
||||
const auto view_coordinates = cam.world_to_view_coordinates({15.f, 12.f, 37.f});
|
||||
|
||||
EXPECT_NEAR(view_coordinates.x, 8.f, k_eps);
|
||||
EXPECT_NEAR(view_coordinates.y, 7.f, k_eps);
|
||||
EXPECT_NEAR(view_coordinates.z, 5.f, k_eps);
|
||||
}
|
||||
|
||||
TEST(UnitTestProjection, WorldToViewCoordinates_RotatedSourceCamera)
|
||||
{
|
||||
constexpr float k_eps = 1e-4f;
|
||||
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
|
||||
const omath::source_engine::ViewAngles angles{omath::source_engine::PitchAngle::from_degrees(0.f),
|
||||
omath::source_engine::YawAngle::from_degrees(90.f),
|
||||
omath::source_engine::RollAngle::from_degrees(0.f)};
|
||||
auto cam = omath::source_engine::Camera({10.f, 20.f, 30.f}, angles, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||
|
||||
const auto view_coordinates = cam.world_to_view_coordinates({14.f, 26.f, 38.f});
|
||||
|
||||
EXPECT_NEAR(view_coordinates.x, 4.f, k_eps);
|
||||
EXPECT_NEAR(view_coordinates.y, 8.f, k_eps);
|
||||
EXPECT_NEAR(view_coordinates.z, 6.f, k_eps);
|
||||
}
|
||||
|
||||
TEST(UnitTestProjection, WorldToViewCoordinates_ColumnMajorOpenGlCamera)
|
||||
{
|
||||
constexpr float k_eps = 1e-4f;
|
||||
constexpr auto fov = omath::projection::FieldOfView::from_degrees(90.f);
|
||||
auto cam = omath::opengl_engine::Camera({10.f, 20.f, 30.f}, {}, {1920.f, 1080.f}, fov, 0.01f, 1000.f);
|
||||
|
||||
const auto view_coordinates = cam.world_to_view_coordinates({14.f, 26.f, 22.f});
|
||||
|
||||
EXPECT_NEAR(view_coordinates.x, 4.f, k_eps);
|
||||
EXPECT_NEAR(view_coordinates.y, 6.f, k_eps);
|
||||
EXPECT_NEAR(view_coordinates.z, -8.f, k_eps);
|
||||
}
|
||||
|
||||
TEST(UnitTestProjection, CalcViewAnglesFromViewMatrix_LookingForward)
|
||||
{
|
||||
constexpr float k_eps = 1e-4f;
|
||||
|
||||
Reference in New Issue
Block a user