removed gcem as dep

This commit is contained in:
2026-06-15 00:37:47 +03:00
parent 75c3f2409d
commit 47dcee037e
38 changed files with 1351 additions and 927 deletions
+12 -11
View File
@@ -8,7 +8,7 @@
namespace omath::cry_engine
{
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
inline constexpr Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z<float, MatStoreType::ROW_MAJOR>(angles.yaw)
* mat_rotation_axis_y<float, MatStoreType::ROW_MAJOR>(angles.roll)
@@ -16,7 +16,7 @@ namespace omath::cry_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> forward_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
@@ -24,7 +24,7 @@ namespace omath::cry_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> right_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
@@ -32,7 +32,7 @@ namespace omath::cry_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> up_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
@@ -40,26 +40,26 @@ namespace omath::cry_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
inline constexpr Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return mat_camera_view<float, MatStoreType::ROW_MAJOR>(forward_vector(angles), right_vector(angles),
up_vector(angles), cam_origin);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> extract_origin(const Mat4X4& mat) noexcept
inline constexpr Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> extract_scale(const Mat4X4& mat) noexcept
inline constexpr Vector3<float> extract_scale(const Mat4X4& mat) noexcept
{
return mat_extract_scale(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
inline constexpr ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
@@ -69,9 +69,10 @@ namespace omath::cry_engine
};
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_perspective_projection_matrix(
const float field_of_view, const float aspect_ratio, const float near_plane, const float far_plane,
const NDCDepthRange ndc_depth_range = NDCDepthRange::ZERO_TO_ONE) noexcept
inline constexpr Mat4X4
calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near_plane,
const float far_plane,
const NDCDepthRange ndc_depth_range = NDCDepthRange::ZERO_TO_ONE) noexcept
{
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return mat_perspective_left_handed_vertical_fov<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
@@ -4,7 +4,7 @@
#pragma once
#include "omath/engines/cry_engine/formulas.hpp"
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/projection/camera.hpp"
namespace omath::cry_engine
{
@@ -12,8 +12,8 @@ namespace omath::cry_engine
{
public:
[[nodiscard]]
OMATH_CONSTEXPR static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
constexpr static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_radians(internal::asin(direction.z)),
@@ -21,16 +21,15 @@ namespace omath::cry_engine
}
[[nodiscard]]
OMATH_CONSTEXPR static Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
constexpr static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return cry_engine::calc_view_matrix(angles, cam_origin);
}
[[nodiscard]]
OMATH_CONSTEXPR static Mat4X4
calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
const float near_plane, const float far_plane,
const NDCDepthRange ndc_depth_range) noexcept
constexpr static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near_plane,
const float far_plane,
const NDCDepthRange ndc_depth_range) noexcept
{
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near_plane, far_plane,
ndc_depth_range);
@@ -8,10 +8,10 @@
namespace omath::frostbite_engine
{
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
inline constexpr Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> forward_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
@@ -19,7 +19,7 @@ namespace omath::frostbite_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> right_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
@@ -27,7 +27,7 @@ namespace omath::frostbite_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> up_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
@@ -35,15 +35,14 @@ namespace omath::frostbite_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
inline constexpr Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return mat_camera_view<float, MatStoreType::ROW_MAJOR>(forward_vector(angles), right_vector(angles),
up_vector(angles), cam_origin);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
inline constexpr Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z<float, MatStoreType::ROW_MAJOR>(angles.roll)
* mat_rotation_axis_y<float, MatStoreType::ROW_MAJOR>(angles.yaw)
@@ -51,19 +50,19 @@ namespace omath::frostbite_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> extract_origin(const Mat4X4& mat) noexcept
inline constexpr Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> extract_scale(const Mat4X4& mat) noexcept
inline constexpr Vector3<float> extract_scale(const Mat4X4& mat) noexcept
{
return mat_extract_scale(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
inline constexpr ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
@@ -74,7 +73,7 @@ namespace omath::frostbite_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_perspective_projection_matrix(
inline constexpr Mat4X4 calc_perspective_projection_matrix(
const float field_of_view, const float aspect_ratio, const float near_plane, const float far_plane,
const NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept
{
@@ -4,7 +4,7 @@
#pragma once
#include "omath/engines/frostbite_engine/formulas.hpp"
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/projection/camera.hpp"
namespace omath::frostbite_engine
@@ -13,8 +13,8 @@ namespace omath::frostbite_engine
{
public:
[[nodiscard]]
OMATH_CONSTEXPR static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
constexpr static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
@@ -23,20 +23,19 @@ namespace omath::frostbite_engine
}
[[nodiscard]]
OMATH_CONSTEXPR static Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
constexpr static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return frostbite_engine::calc_view_matrix(angles, cam_origin);
}
[[nodiscard]]
OMATH_CONSTEXPR static Mat4X4
calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
const float near_plane, const float far_plane,
const NDCDepthRange ndc_depth_range) noexcept
constexpr static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near_plane,
const float far_plane,
const NDCDepthRange ndc_depth_range) noexcept
{
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near_plane, far_plane,
ndc_depth_range);
}
};
} // namespace omath::unreal_engine
} // namespace omath::frostbite_engine
+10 -11
View File
@@ -8,10 +8,10 @@
namespace omath::iw_engine
{
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
inline constexpr Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> forward_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
@@ -19,7 +19,7 @@ namespace omath::iw_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> right_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
@@ -27,7 +27,7 @@ namespace omath::iw_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> up_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
@@ -35,25 +35,25 @@ namespace omath::iw_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
inline constexpr Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z(angles.yaw) * mat_rotation_axis_y(angles.pitch) * mat_rotation_axis_x(angles.roll);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> extract_origin(const Mat4X4& mat) noexcept
inline constexpr Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> extract_scale(const Mat4X4& mat) noexcept
inline constexpr Vector3<float> extract_scale(const Mat4X4& mat) noexcept
{
return mat_extract_scale(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
inline constexpr ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
@@ -64,14 +64,13 @@ namespace omath::iw_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
inline constexpr Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return mat_camera_view(forward_vector(angles), right_vector(angles), up_vector(angles), cam_origin);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_perspective_projection_matrix(
inline constexpr Mat4X4 calc_perspective_projection_matrix(
const float field_of_view, const float aspect_ratio, const float near_plane, const float far_plane,
const NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept
{
@@ -4,7 +4,7 @@
#pragma once
#include "omath/engines/iw_engine/formulas.hpp"
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/projection/camera.hpp"
namespace omath::iw_engine
@@ -13,8 +13,8 @@ namespace omath::iw_engine
{
public:
[[nodiscard]]
OMATH_CONSTEXPR static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
constexpr static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
@@ -23,16 +23,15 @@ namespace omath::iw_engine
}
[[nodiscard]]
OMATH_CONSTEXPR static Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
constexpr static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return iw_engine::calc_view_matrix(angles, cam_origin);
}
[[nodiscard]]
OMATH_CONSTEXPR static Mat4X4
calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
const float near_plane, const float far_plane,
const NDCDepthRange ndc_depth_range) noexcept
constexpr static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near_plane,
const float far_plane,
const NDCDepthRange ndc_depth_range) noexcept
{
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near_plane, far_plane,
ndc_depth_range);
@@ -7,10 +7,10 @@
namespace omath::opengl_engine
{
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
inline constexpr Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> forward_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec =
rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_forward);
@@ -19,7 +19,7 @@ namespace omath::opengl_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> right_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec =
rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_right);
@@ -28,7 +28,7 @@ namespace omath::opengl_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> up_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector<float, MatStoreType::COLUMN_MAJOR>(k_abs_up);
@@ -36,14 +36,13 @@ namespace omath::opengl_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
inline constexpr Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return mat_look_at_right_handed(cam_origin, cam_origin + forward_vector(angles), up_vector(angles));
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
inline constexpr Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z<float, MatStoreType::COLUMN_MAJOR>(angles.roll)
* mat_rotation_axis_y<float, MatStoreType::COLUMN_MAJOR>(angles.yaw)
@@ -51,19 +50,19 @@ namespace omath::opengl_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> extract_origin(const Mat4X4& mat) noexcept
inline constexpr Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> extract_scale(const Mat4X4& mat) noexcept
inline constexpr Vector3<float> extract_scale(const Mat4X4& mat) noexcept
{
return mat_extract_scale(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
inline constexpr ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
@@ -74,19 +73,19 @@ namespace omath::opengl_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_perspective_projection_matrix(
inline constexpr Mat4X4 calc_perspective_projection_matrix(
const float field_of_view, const float aspect_ratio, const float near_plane, const float far_plane,
const NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept
{
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return mat_perspective_right_handed_vertical_fov<
float, MatStoreType::COLUMN_MAJOR, NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
return mat_perspective_right_handed_vertical_fov<float, MatStoreType::COLUMN_MAJOR,
NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
field_of_view, aspect_ratio, near_plane, far_plane);
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return mat_perspective_right_handed_vertical_fov<
float, MatStoreType::COLUMN_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
field_of_view, aspect_ratio, near_plane, far_plane);
return mat_perspective_right_handed_vertical_fov<float, MatStoreType::COLUMN_MAJOR,
NDCDepthRange::ZERO_TO_ONE>(field_of_view, aspect_ratio,
near_plane, far_plane);
std::unreachable();
}
@@ -4,7 +4,7 @@
#pragma once
#include "omath/engines/opengl_engine/formulas.hpp"
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/projection/camera.hpp"
namespace omath::opengl_engine
@@ -13,27 +13,25 @@ namespace omath::opengl_engine
{
public:
[[nodiscard]]
OMATH_CONSTEXPR static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
constexpr static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
return {PitchAngle::from_radians(internal::asin(direction.y)),
YawAngle::from_radians(-internal::atan2(direction.x, -direction.z)),
RollAngle::from_radians(0.f)};
YawAngle::from_radians(-internal::atan2(direction.x, -direction.z)), RollAngle::from_radians(0.f)};
}
[[nodiscard]]
OMATH_CONSTEXPR static Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
constexpr static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return opengl_engine::calc_view_matrix(angles, cam_origin);
}
[[nodiscard]]
OMATH_CONSTEXPR static Mat4X4
calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
const float near_plane, const float far_plane,
const NDCDepthRange ndc_depth_range) noexcept
constexpr static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near_plane,
const float far_plane,
const NDCDepthRange ndc_depth_range) noexcept
{
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near_plane, far_plane,
ndc_depth_range);
+13 -13
View File
@@ -9,10 +9,10 @@
namespace omath::rage_engine
{
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
inline constexpr Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> forward_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
@@ -20,7 +20,7 @@ namespace omath::rage_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> right_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
@@ -28,7 +28,7 @@ namespace omath::rage_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> up_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
@@ -36,15 +36,14 @@ namespace omath::rage_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
inline constexpr Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return mat_camera_view<float, MatStoreType::ROW_MAJOR>(forward_vector(angles), right_vector(angles),
up_vector(angles), cam_origin);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
inline constexpr Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z<float, MatStoreType::ROW_MAJOR>(angles.yaw)
* mat_rotation_axis_y<float, MatStoreType::ROW_MAJOR>(angles.roll)
@@ -52,19 +51,19 @@ namespace omath::rage_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> extract_origin(const Mat4X4& mat) noexcept
inline constexpr Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> extract_scale(const Mat4X4& mat) noexcept
inline constexpr Vector3<float> extract_scale(const Mat4X4& mat) noexcept
{
return mat_extract_scale(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
inline constexpr ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
@@ -75,9 +74,10 @@ namespace omath::rage_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_perspective_projection_matrix(
const float field_of_view, const float aspect_ratio, const float near_plane, const float far_plane,
const NDCDepthRange ndc_depth_range = NDCDepthRange::ZERO_TO_ONE) noexcept
inline constexpr Mat4X4
calc_perspective_projection_matrix(const float field_of_view, const float aspect_ratio, const float near_plane,
const float far_plane,
const NDCDepthRange ndc_depth_range = NDCDepthRange::ZERO_TO_ONE) noexcept
{
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return mat_perspective_left_handed_vertical_fov<float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
@@ -4,7 +4,7 @@
#pragma once
#include "omath/engines/rage_engine/formulas.hpp"
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/projection/camera.hpp"
namespace omath::rage_engine
@@ -13,8 +13,8 @@ namespace omath::rage_engine
{
public:
[[nodiscard]]
OMATH_CONSTEXPR static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
constexpr static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
@@ -23,16 +23,15 @@ namespace omath::rage_engine
}
[[nodiscard]]
OMATH_CONSTEXPR static Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
constexpr static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return rage_engine::calc_view_matrix(angles, cam_origin);
}
[[nodiscard]]
OMATH_CONSTEXPR static Mat4X4
calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
const float near_plane, const float far_plane,
const NDCDepthRange ndc_depth_range) noexcept
constexpr static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near_plane,
const float far_plane,
const NDCDepthRange ndc_depth_range) noexcept
{
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near_plane, far_plane,
ndc_depth_range);
@@ -7,10 +7,10 @@
namespace omath::source_engine
{
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
inline constexpr Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> forward_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
@@ -18,25 +18,25 @@ namespace omath::source_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
inline constexpr Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z(angles.yaw) * mat_rotation_axis_y(angles.pitch) * mat_rotation_axis_x(angles.roll);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> extract_origin(const Mat4X4& mat) noexcept
inline constexpr Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> extract_scale(const Mat4X4& mat) noexcept
inline constexpr Vector3<float> extract_scale(const Mat4X4& mat) noexcept
{
return mat_extract_scale(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
inline constexpr ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
@@ -47,7 +47,7 @@ namespace omath::source_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> right_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
@@ -55,7 +55,7 @@ namespace omath::source_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> up_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
@@ -63,14 +63,13 @@ namespace omath::source_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
inline constexpr Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return mat_camera_view(forward_vector(angles), right_vector(angles), up_vector(angles), cam_origin);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_perspective_projection_matrix(
inline constexpr Mat4X4 calc_perspective_projection_matrix(
const float field_of_view, const float aspect_ratio, const float near_plane, const float far_plane,
const NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept
{
@@ -4,7 +4,7 @@
#pragma once
#include "omath/engines/source_engine/formulas.hpp"
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/projection/camera.hpp"
namespace omath::source_engine
@@ -13,8 +13,8 @@ namespace omath::source_engine
{
public:
[[nodiscard]]
OMATH_CONSTEXPR static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
constexpr static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
@@ -23,16 +23,15 @@ namespace omath::source_engine
}
[[nodiscard]]
OMATH_CONSTEXPR static Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
constexpr static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return source_engine::calc_view_matrix(angles, cam_origin);
}
[[nodiscard]]
OMATH_CONSTEXPR static Mat4X4
calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
const float near_plane, const float far_plane,
const NDCDepthRange ndc_depth_range) noexcept
constexpr static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near_plane,
const float far_plane,
const NDCDepthRange ndc_depth_range) noexcept
{
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near_plane, far_plane,
ndc_depth_range);
+14 -15
View File
@@ -8,10 +8,10 @@
namespace omath::unity_engine
{
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
inline constexpr Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> forward_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
@@ -19,7 +19,7 @@ namespace omath::unity_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> right_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
@@ -27,7 +27,7 @@ namespace omath::unity_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> up_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<float> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
@@ -35,15 +35,14 @@ namespace omath::unity_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
inline constexpr Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return mat_camera_view<float, MatStoreType::ROW_MAJOR>(-forward_vector(angles), right_vector(angles),
up_vector(angles), cam_origin);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
inline constexpr Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z<float, MatStoreType::ROW_MAJOR>(angles.roll)
* mat_rotation_axis_y<float, MatStoreType::ROW_MAJOR>(angles.yaw)
@@ -51,19 +50,19 @@ namespace omath::unity_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> extract_origin(const Mat4X4& mat) noexcept
inline constexpr Vector3<float> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<float> extract_scale(const Mat4X4& mat) noexcept
inline constexpr Vector3<float> extract_scale(const Mat4X4& mat) noexcept
{
return mat_extract_scale(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
inline constexpr ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
@@ -74,17 +73,17 @@ namespace omath::unity_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_perspective_projection_matrix(
inline constexpr Mat4X4 calc_perspective_projection_matrix(
const float field_of_view, const float aspect_ratio, const float near_plane, const float far_plane,
const NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept
{
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return omath::mat_perspective_right_handed_vertical_fov<
float, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
return omath::mat_perspective_right_handed_vertical_fov<float, MatStoreType::ROW_MAJOR,
NDCDepthRange::ZERO_TO_ONE>(
field_of_view, aspect_ratio, near_plane, far_plane);
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return omath::mat_perspective_right_handed_vertical_fov<
float, MatStoreType::ROW_MAJOR, NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
return omath::mat_perspective_right_handed_vertical_fov<float, MatStoreType::ROW_MAJOR,
NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
field_of_view, aspect_ratio, near_plane, far_plane);
std::unreachable();
}
@@ -4,7 +4,7 @@
#pragma once
#include "omath/engines/unity_engine/formulas.hpp"
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/projection/camera.hpp"
namespace omath::unity_engine
@@ -13,8 +13,8 @@ namespace omath::unity_engine
{
public:
[[nodiscard]]
OMATH_CONSTEXPR static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
constexpr static ViewAngles calc_look_at_angle(const Vector3<float>& cam_origin,
const Vector3<float>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
@@ -23,16 +23,15 @@ namespace omath::unity_engine
}
[[nodiscard]]
OMATH_CONSTEXPR static Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<float>& cam_origin) noexcept
constexpr static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<float>& cam_origin) noexcept
{
return unity_engine::calc_view_matrix(angles, cam_origin);
}
[[nodiscard]]
OMATH_CONSTEXPR static Mat4X4
calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
const float near_plane, const float far_plane,
const NDCDepthRange ndc_depth_range) noexcept
constexpr static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const float near_plane,
const float far_plane,
const NDCDepthRange ndc_depth_range) noexcept
{
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near_plane, far_plane,
ndc_depth_range);
@@ -8,10 +8,10 @@
namespace omath::unreal_engine
{
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
inline constexpr Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept;
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<double> forward_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<double> forward_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_forward);
@@ -19,7 +19,7 @@ namespace omath::unreal_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<double> right_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<double> right_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_right);
@@ -27,7 +27,7 @@ namespace omath::unreal_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<double> up_vector(const ViewAngles& angles) noexcept
inline constexpr Vector3<double> up_vector(const ViewAngles& angles) noexcept
{
const auto vec = rotation_matrix(angles) * mat_column_from_vector(k_abs_up);
@@ -35,15 +35,14 @@ namespace omath::unreal_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<double>& cam_origin) noexcept
inline constexpr Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<double>& cam_origin) noexcept
{
return mat_camera_view<double, MatStoreType::ROW_MAJOR>(forward_vector(angles), right_vector(angles),
up_vector(angles), cam_origin);
up_vector(angles), cam_origin);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
inline constexpr Mat4X4 rotation_matrix(const ViewAngles& angles) noexcept
{
return mat_rotation_axis_z<double, MatStoreType::ROW_MAJOR>(angles.yaw)
* mat_rotation_axis_y<double, MatStoreType::ROW_MAJOR>(-angles.pitch)
@@ -51,19 +50,19 @@ namespace omath::unreal_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<double> extract_origin(const Mat4X4& mat) noexcept
inline constexpr Vector3<double> extract_origin(const Mat4X4& mat) noexcept
{
return mat_extract_origin(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR Vector3<double> extract_scale(const Mat4X4& mat) noexcept
inline constexpr Vector3<double> extract_scale(const Mat4X4& mat) noexcept
{
return mat_extract_scale(mat);
}
[[nodiscard]]
inline OMATH_CONSTEXPR ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
inline constexpr ViewAngles extract_rotation_angles(const Mat4X4& mat) noexcept
{
const auto angles = mat_extract_rotation_zyx(mat);
return {
@@ -74,17 +73,17 @@ namespace omath::unreal_engine
}
[[nodiscard]]
inline OMATH_CONSTEXPR Mat4X4 calc_perspective_projection_matrix(
inline constexpr Mat4X4 calc_perspective_projection_matrix(
const double field_of_view, const double aspect_ratio, const double near_plane, const double far_plane,
const NDCDepthRange ndc_depth_range = NDCDepthRange::NEGATIVE_ONE_TO_ONE) noexcept
{
if (ndc_depth_range == NDCDepthRange::ZERO_TO_ONE)
return mat_perspective_left_handed_horizontal_fov<
double, MatStoreType::ROW_MAJOR, NDCDepthRange::ZERO_TO_ONE>(
field_of_view, aspect_ratio, near_plane, far_plane);
return mat_perspective_left_handed_horizontal_fov<double, MatStoreType::ROW_MAJOR,
NDCDepthRange::ZERO_TO_ONE>(field_of_view, aspect_ratio,
near_plane, far_plane);
if (ndc_depth_range == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return mat_perspective_left_handed_horizontal_fov<
double, MatStoreType::ROW_MAJOR, NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
return mat_perspective_left_handed_horizontal_fov<double, MatStoreType::ROW_MAJOR,
NDCDepthRange::NEGATIVE_ONE_TO_ONE>(
field_of_view, aspect_ratio, near_plane, far_plane);
std::unreachable();
}
@@ -4,7 +4,7 @@
#pragma once
#include "omath/engines/unreal_engine/formulas.hpp"
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/projection/camera.hpp"
namespace omath::unreal_engine
@@ -13,8 +13,8 @@ namespace omath::unreal_engine
{
public:
[[nodiscard]]
OMATH_CONSTEXPR static ViewAngles calc_look_at_angle(const Vector3<double>& cam_origin,
const Vector3<double>& look_at) noexcept
constexpr static ViewAngles calc_look_at_angle(const Vector3<double>& cam_origin,
const Vector3<double>& look_at) noexcept
{
const auto direction = (look_at - cam_origin).normalized();
@@ -23,16 +23,15 @@ namespace omath::unreal_engine
}
[[nodiscard]]
OMATH_CONSTEXPR static Mat4X4 calc_view_matrix(const ViewAngles& angles,
const Vector3<double>& cam_origin) noexcept
constexpr static Mat4X4 calc_view_matrix(const ViewAngles& angles, const Vector3<double>& cam_origin) noexcept
{
return unreal_engine::calc_view_matrix(angles, cam_origin);
}
[[nodiscard]]
OMATH_CONSTEXPR static Mat4X4
calc_projection_matrix(const projection::FieldOfView& fov, const projection::ViewPort& view_port,
const double near_plane, const double far_plane,
const NDCDepthRange ndc_depth_range) noexcept
constexpr static Mat4X4 calc_projection_matrix(const projection::FieldOfView& fov,
const projection::ViewPort& view_port, const double near_plane,
const double far_plane,
const NDCDepthRange ndc_depth_range) noexcept
{
return calc_perspective_projection_matrix(fov.as_degrees(), view_port.aspect_ratio(), near_plane, far_plane,
ndc_depth_range);
+707
View File
@@ -0,0 +1,707 @@
//
// Created by orange on 6/11/2026.
//
#pragma once
#include <bit>
#include <cmath>
#include <cstdint>
#include <limits>
#include <numbers>
#include <type_traits>
namespace omath::internal
{
// Embedded subset of GCE-Math 1.18.0.
//
// Original project:
// GCE-Math: A C++ generalized constant expression-based math library
// Copyright 2016-2024 Keith O'Hara
// Licensed under the Apache License, Version 2.0.
namespace math_detail
{
using uint_t = unsigned int;
using llint_t = long long int;
using ullint_t = unsigned long long int;
template<class Type>
using limits = std::numeric_limits<Type>;
template<class Type>
constexpr Type pi = std::numbers::pi_v<Type>;
template<class Type>
constexpr Type half_pi = std::numbers::pi_v<Type> / static_cast<Type>(2);
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type abs(const Type value) noexcept
{
return value == Type{0} ? Type{0} : value < Type{0} ? -value : value;
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr int sgn(const Type value) noexcept
{
return value > Type{0} ? 1 : value < Type{0} ? -1 : 0;
}
[[nodiscard]]
constexpr bool is_odd(const llint_t value) noexcept
{
return (value & 1U) != 0;
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr bool is_nan(const Type value) noexcept
{
return value != value;
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr bool is_neginf(const Type value) noexcept
{
return value == -limits<Type>::infinity();
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr bool is_posinf(const Type value) noexcept
{
return value == limits<Type>::infinity();
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr bool is_inf(const Type value) noexcept
{
return is_neginf(value) || is_posinf(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr bool is_finite(const Type value) noexcept
{
return !is_nan(value) && !is_inf(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr bool any_nan(const Type x, const Type y) noexcept
{
return is_nan(x) || is_nan(y);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr bool any_inf(const Type x, const Type y) noexcept
{
return is_inf(x) || is_inf(y);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr bool all_finite(const Type x, const Type y) noexcept
{
return is_finite(x) && is_finite(y);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr bool signbit(const Type value) noexcept
{
if constexpr (std::is_same_v<Type, float>)
return (std::bit_cast<std::uint32_t>(value) & 0x80000000U) != 0;
else if constexpr (std::is_same_v<Type, double>)
return (std::bit_cast<std::uint64_t>(value) & 0x8000000000000000ULL) != 0;
else
return value < Type{0};
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr bool neg_zero(const Type value) noexcept
{
return value == Type{0} && signbit(value);
}
template<class Type, class ExpType>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type pow_integral_compute(const Type base, const ExpType exp_term) noexcept;
template<class Type, class ExpType>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type pow_integral_compute_recur(const Type base, const Type value, const ExpType exp_term) noexcept
{
return exp_term > ExpType{1}
? is_odd(static_cast<llint_t>(exp_term))
? pow_integral_compute_recur(base * base, value * base, exp_term / ExpType{2})
: pow_integral_compute_recur(base * base, value, exp_term / ExpType{2})
: exp_term == ExpType{1} ? value * base
: value;
}
template<class Type, class ExpType>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type pow_integral_sgn_check(const Type base, const ExpType exp_term) noexcept
{
if constexpr (std::is_signed_v<ExpType>)
return exp_term < ExpType{0} ? Type{1} / pow_integral_compute(base, -exp_term)
: pow_integral_compute_recur(base, Type{1}, exp_term);
else
return pow_integral_compute_recur(base, Type{1}, exp_term);
}
template<class Type, class ExpType>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type pow_integral_compute(const Type base, const ExpType exp_term) noexcept
{
return exp_term == ExpType{3} ? base * base * base
: exp_term == ExpType{2} ? base * base
: exp_term == ExpType{1} ? base
: exp_term == ExpType{0} ? Type{1}
: exp_term == limits<ExpType>::min() ? Type{0}
: exp_term == limits<ExpType>::max() ? limits<Type>::infinity()
: pow_integral_sgn_check(base, exp_term);
}
template<class Type, class ExpType>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type pow_integral(const Type base, const ExpType exp_term) noexcept
{
return pow_integral_compute(base, exp_term);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type sqrt_recur(const Type x, const Type xn, const int count) noexcept
{
return abs(xn - x / xn) / (Type{1} + xn) < limits<Type>::min() ? xn
: count < 100 ? sqrt_recur(x, Type{0.5} * (xn + x / xn), count + 1)
: xn;
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type sqrt_simplify(const Type x, const Type m_val) noexcept
{
return x > Type{1e+08} ? sqrt_simplify(x / Type{1e+08}, Type{1e+04} * m_val)
: x > Type{1e+06} ? sqrt_simplify(x / Type{1e+06}, Type{1e+03} * m_val)
: x > Type{1e+04} ? sqrt_simplify(x / Type{1e+04}, Type{1e+02} * m_val)
: x > Type{100} ? sqrt_simplify(x / Type{100}, Type{10} * m_val)
: x > Type{4} ? sqrt_simplify(x / Type{4}, Type{2} * m_val)
: m_val * sqrt_recur(x, x / Type{2}, 0);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type sqrt(const Type x) noexcept
{
return is_nan(x) ? limits<Type>::quiet_NaN()
: x < Type{0} ? limits<Type>::quiet_NaN()
: is_posinf(x) ? x
: limits<Type>::min() > abs(x) ? Type{0}
: limits<Type>::min() > abs(Type{1} - x) ? x
: sqrt_simplify(x, Type{1});
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type floor_int(const Type x, const Type x_whole) noexcept
{
return x_whole - static_cast<Type>((x < Type{0}) && (x < x_whole));
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type floor_check_internal(const Type x) noexcept
{
if constexpr (std::is_same_v<Type, float>)
return abs(x) >= 8388608.f ? x : floor_int(x, static_cast<float>(static_cast<int>(x)));
else if constexpr (std::is_same_v<Type, double>)
return abs(x) >= 4503599627370496. ? x : floor_int(x, static_cast<double>(static_cast<llint_t>(x)));
else
return abs(x) >= 9223372036854775808.l
? x
: static_cast<long double>(static_cast<ullint_t>(abs(x))) * sgn(x);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type floor(const Type x) noexcept
{
return is_nan(x) ? limits<Type>::quiet_NaN()
: !is_finite(x) ? x
: limits<Type>::min() > abs(x) ? x
: floor_check_internal(x);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type trunc_int(const Type x) noexcept
{
return static_cast<Type>(static_cast<llint_t>(x));
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type trunc_check_internal(const Type x) noexcept
{
if constexpr (std::is_same_v<Type, float>)
return abs(x) >= 8388608.f ? x : trunc_int(x);
else if constexpr (std::is_same_v<Type, double>)
return abs(x) >= 4503599627370496. ? x : trunc_int(x);
else
return abs(x) >= 9223372036854775808.l
? x
: static_cast<long double>(static_cast<ullint_t>(abs(x))) * sgn(x);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type trunc(const Type x) noexcept
{
return is_nan(x) ? limits<Type>::quiet_NaN()
: !is_finite(x) ? x
: limits<Type>::min() > abs(x) ? x
: trunc_check_internal(x);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type fmod(const Type x, const Type y) noexcept
{
return any_nan(x, y) || !all_finite(x, y) || limits<Type>::min() > abs(y) ? limits<Type>::quiet_NaN()
: x - trunc(x / y) * y;
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type tan_series_exp_long(const Type z) noexcept
{
return -Type{1} / z
+ (z / Type{3}
+ (pow_integral(z, 3) / Type{45}
+ (Type{2} * pow_integral(z, 5) / Type{945} + pow_integral(z, 7) / Type{4725})));
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type tan_series_exp(const Type x) noexcept
{
return limits<Type>::min() > abs(x - half_pi<Type>) ? Type{1.633124e+16}
: tan_series_exp_long(x - half_pi<Type>);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type tan_cf_recur(const Type xx, const int max_depth) noexcept
{
Type result = static_cast<Type>(2 * max_depth - 1);
for (int depth = max_depth - 1; depth >= 1; --depth)
result = static_cast<Type>(2 * depth - 1) - xx / result;
return result;
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type tan_cf_main(const Type x) noexcept
{
return x > Type{1.55} && x < Type{1.60} ? tan_series_exp(x)
: x > Type{1.4} ? x / tan_cf_recur(x * x, 45)
: x > Type{1} ? x / tan_cf_recur(x * x, 35)
: x / tan_cf_recur(x * x, 25);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type tan_begin(const Type x, const int count = 0) noexcept
{
return x > pi<Type> ? count > 1 ? limits<Type>::quiet_NaN()
: tan_begin(x - pi<Type> * floor(x / pi<Type>), count + 1)
: tan_cf_main(x);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type tan(const Type x) noexcept
{
return is_nan(x) ? limits<Type>::quiet_NaN()
: limits<Type>::min() > abs(x) ? Type{0}
: x < Type{0} ? -tan_begin(-x)
: tan_begin(x);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type sin(const Type x) noexcept
{
return is_nan(x) ? limits<Type>::quiet_NaN()
: limits<Type>::min() > abs(x) ? Type{0}
: limits<Type>::min() > abs(x - half_pi<Type>) ? Type{1}
: limits<Type>::min() > abs(x + half_pi<Type>) ? -Type{1}
: limits<Type>::min() > abs(x - pi<Type>) ? Type{0}
: limits<Type>::min() > abs(x + pi<Type>)
? -Type{0}
: (Type{2} * tan(x / Type{2})) / (Type{1} + tan(x / Type{2}) * tan(x / Type{2}));
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type cos(const Type x) noexcept
{
return is_nan(x) ? limits<Type>::quiet_NaN()
: limits<Type>::min() > abs(x) ? Type{1}
: limits<Type>::min() > abs(x - half_pi<Type>) ? Type{0}
: limits<Type>::min() > abs(x + half_pi<Type>) ? Type{0}
: limits<Type>::min() > abs(x - pi<Type>) ? -Type{1}
: limits<Type>::min() > abs(x + pi<Type>)
? -Type{1}
: (Type{1} - tan(x / Type{2}) * tan(x / Type{2}))
/ (Type{1} + tan(x / Type{2}) * tan(x / Type{2}));
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type atan_series_order_calc(const Type xx, const Type x_pow, const uint_t order) noexcept
{
return Type{1} / (static_cast<Type>((order - 1) * 4 - 1) * x_pow)
- Type{1} / (static_cast<Type>((order - 1) * 4 + 1) * x_pow * xx);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type atan_series_order(const Type x, const uint_t order_begin, const uint_t max_order) noexcept
{
if (max_order == 1)
return half_pi<Type> - Type{1} / x;
const Type xx = x * x;
Type result = atan_series_order_calc(xx, pow_integral(x, 4 * max_order - 5), max_order);
auto depth = max_order - 1;
while (depth > order_begin)
{
result += atan_series_order_calc(xx, pow_integral(x, 4 * depth - 5), depth);
--depth;
}
return result + half_pi<Type> - Type{1} / x;
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type atan_series_main(const Type x) noexcept
{
return x < Type{3} ? atan_series_order(x, 1U, 10U)
: x < Type{4} ? atan_series_order(x, 1U, 9U)
: x < Type{5} ? atan_series_order(x, 1U, 8U)
: x < Type{7} ? atan_series_order(x, 1U, 7U)
: x < Type{11} ? atan_series_order(x, 1U, 6U)
: x < Type{25} ? atan_series_order(x, 1U, 5U)
: x < Type{100} ? atan_series_order(x, 1U, 4U)
: x < Type{1000} ? atan_series_order(x, 1U, 3U)
: atan_series_order(x, 1U, 2U);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type atan_cf_recur(const Type xx, const uint_t depth_begin, const uint_t max_depth) noexcept
{
auto depth = max_depth - 1;
Type result = static_cast<Type>(2 * (depth + 1) - 1);
while (depth > depth_begin - 1)
{
result = static_cast<Type>(2 * depth - 1) + static_cast<Type>(depth * depth) * xx / result;
--depth;
}
return result;
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type atan_cf_main(const Type x) noexcept
{
return x < Type{0.5} ? x / atan_cf_recur(x * x, 1U, 15U)
: x < Type{1} ? x / atan_cf_recur(x * x, 1U, 25U)
: x < Type{1.5} ? x / atan_cf_recur(x * x, 1U, 35U)
: x < Type{2} ? x / atan_cf_recur(x * x, 1U, 45U)
: x / atan_cf_recur(x * x, 1U, 52U);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type atan_begin(const Type x) noexcept
{
return x > Type{2.5} ? atan_series_main(x) : atan_cf_main(x);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type atan(const Type x) noexcept
{
return is_nan(x) ? limits<Type>::quiet_NaN()
: limits<Type>::min() > abs(x) ? Type{0}
: x < Type{0} ? -atan_begin(-x)
: atan_begin(x);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type atan2(const Type y, const Type x) noexcept
{
return any_nan(y, x) ? limits<Type>::quiet_NaN()
: limits<Type>::min() > abs(x) ? limits<Type>::min() > abs(y)
? neg_zero(y) ? neg_zero(x) ? -pi<Type> : -Type{0}
: neg_zero(x) ? pi<Type>
: Type{0}
: y > Type{0} ? half_pi<Type>
: -half_pi<Type>
: x < Type{0} ? y < Type{0} ? atan(y / x) - pi<Type> : atan(y / x) + pi<Type>
: atan(y / x);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type asin_compute(const Type x) noexcept
{
return x > Type{1} ? limits<Type>::quiet_NaN()
: limits<Type>::min() > abs(x - Type{1}) ? half_pi<Type>
: limits<Type>::min() > abs(x) ? Type{0}
: atan(x / sqrt(Type{1} - x * x));
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type asin(const Type x) noexcept
{
return is_nan(x) ? limits<Type>::quiet_NaN() : x < Type{0} ? -asin_compute(-x) : asin_compute(x);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type acos_compute(const Type x) noexcept
{
return abs(x) > Type{1} ? limits<Type>::quiet_NaN()
: limits<Type>::min() > abs(x - Type{1}) ? Type{0}
: limits<Type>::min() > abs(x) ? half_pi<Type>
: atan(sqrt(Type{1} - x * x) / x);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type acos(const Type x) noexcept
{
return is_nan(x) ? limits<Type>::quiet_NaN() : x > Type{0} ? acos_compute(x) : pi<Type> - acos_compute(-x);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type hypot(const Type x, const Type y) noexcept
{
return any_nan(x, y) ? limits<Type>::quiet_NaN()
: any_inf(x, y) ? limits<Type>::infinity()
: limits<Type>::min() > abs(x) ? abs(y)
: limits<Type>::min() > abs(y) ? abs(x)
: abs(x) * sqrt(Type{1} + (y / x) * (y / x));
}
} // namespace math_detail
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type sin(const Type& value) noexcept
{
if consteval
{
return math_detail::sin(value);
}
return std::sin(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type cos(const Type& value) noexcept
{
if consteval
{
return math_detail::cos(value);
}
return std::cos(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type tan(const Type& value) noexcept
{
if consteval
{
return math_detail::tan(value);
}
return std::tan(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type atan(const Type& value) noexcept
{
if consteval
{
return math_detail::atan(value);
}
return std::atan(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type atan2(const Type& y, const Type& x) noexcept
{
if consteval
{
return math_detail::atan2(y, x);
}
return std::atan2(y, x);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type asin(const Type& value) noexcept
{
if consteval
{
return math_detail::asin(value);
}
return std::asin(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type acos(const Type& value) noexcept
{
if consteval
{
return math_detail::acos(value);
}
return std::acos(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type sqrt(const Type& value) noexcept
{
if consteval
{
return math_detail::sqrt(value);
}
return std::sqrt(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type hypot(const Type& x, const Type& y) noexcept
{
if consteval
{
return math_detail::hypot(x, y);
}
return std::hypot(x, y);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type hypot(const Type& x, const Type& y, const Type& z) noexcept
{
if consteval
{
return math_detail::sqrt(x * x + y * y + z * z);
}
return std::hypot(x, y, z);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type abs(const Type& value) noexcept
{
if consteval
{
return math_detail::abs(value);
}
return std::abs(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
constexpr Type fmod(const Type& dividend, const Type& divisor) noexcept
{
if consteval
{
return math_detail::fmod(dividend, divisor);
}
return std::fmod(dividend, divisor);
}
} // namespace omath::internal
@@ -1,185 +0,0 @@
//
// Created by orange on 6/11/2026.
//
#pragma once
#include <cmath>
#include <type_traits>
#ifdef OMATH_USE_GCEM
#include <gcem.hpp>
#define OMATH_CONSTEXPR constexpr
#else
#define OMATH_CONSTEXPR
#endif
namespace omath::internal
{
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
OMATH_CONSTEXPR Type sin(const Type& value) noexcept
{
#ifdef OMATH_USE_GCEM
if consteval
{
return gcem::sin(value);
}
#endif
return std::sin(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
OMATH_CONSTEXPR Type cos(const Type& value) noexcept
{
#ifdef OMATH_USE_GCEM
if consteval
{
return gcem::cos(value);
}
#endif
return std::cos(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
OMATH_CONSTEXPR Type tan(const Type& value) noexcept
{
#ifdef OMATH_USE_GCEM
if consteval
{
return gcem::tan(value);
}
#endif
return std::tan(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
OMATH_CONSTEXPR Type atan(const Type& value) noexcept
{
#ifdef OMATH_USE_GCEM
if consteval
{
return gcem::atan(value);
}
#endif
return std::atan(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
OMATH_CONSTEXPR Type atan2(const Type& y, const Type& x) noexcept
{
#ifdef OMATH_USE_GCEM
if consteval
{
return gcem::atan2(y, x);
}
#endif
return std::atan2(y, x);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
OMATH_CONSTEXPR Type asin(const Type& value) noexcept
{
#ifdef OMATH_USE_GCEM
if consteval
{
return gcem::asin(value);
}
#endif
return std::asin(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
OMATH_CONSTEXPR Type acos(const Type& value) noexcept
{
#ifdef OMATH_USE_GCEM
if consteval
{
return gcem::acos(value);
}
#endif
return std::acos(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
OMATH_CONSTEXPR Type sqrt(const Type& value) noexcept
{
#ifdef OMATH_USE_GCEM
if consteval
{
return gcem::sqrt(value);
}
#endif
return std::sqrt(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
OMATH_CONSTEXPR Type hypot(const Type& x, const Type& y) noexcept
{
#ifdef OMATH_USE_GCEM
if consteval
{
return gcem::hypot(x, y);
}
#endif
return std::hypot(x, y);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
OMATH_CONSTEXPR Type hypot(const Type& x, const Type& y, const Type& z) noexcept
{
#ifdef OMATH_USE_GCEM
if consteval
{
return gcem::sqrt(x * x + y * y + z * z);
}
#endif
return std::hypot(x, y, z);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
OMATH_CONSTEXPR Type abs(const Type& value) noexcept
{
#ifdef OMATH_USE_GCEM
if consteval
{
return gcem::abs(value);
}
#endif
return std::abs(value);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]]
OMATH_CONSTEXPR Type fmod(const Type& dividend, const Type& divisor) noexcept
{
#ifdef OMATH_USE_GCEM
if consteval
{
return gcem::fmod(dividend, divisor);
}
#endif
return std::fmod(dividend, divisor);
}
} // namespace omath::internal
+105 -122
View File
@@ -2,7 +2,7 @@
// Created by vlad on 9/29/2024.
//
#pragma once
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "vector3.hpp"
#include <algorithm>
#include <array>
@@ -43,12 +43,12 @@ namespace omath
enum class NDCDepthRange : uint8_t
{
NEGATIVE_ONE_TO_ONE = 0, // OpenGL: [-1.0, 1.0]
ZERO_TO_ONE // DirectX / Vulkan: [0.0, 1.0]
ZERO_TO_ONE // DirectX / Vulkan: [0.0, 1.0]
};
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>
@@ -202,7 +202,11 @@ namespace omath
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;
}
@@ -222,7 +226,11 @@ 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;
}
@@ -603,8 +611,7 @@ namespace omath
[[nodiscard("You must use translation matrix")]]
constexpr Mat<4, 4, Type, St> mat_translation(const Vector3<Type>& diff) noexcept
{
return
{
return {
{1, 0, 0, diff.x},
{0, 1, 0, diff.y},
{0, 0, 1, diff.z},
@@ -632,9 +639,10 @@ namespace omath
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
[[nodiscard("You must use extracted scale")]]
OMATH_CONSTEXPR Vector3<Type> mat_extract_scale(const Mat<4, 4, Type, St>& mat) noexcept
constexpr Vector3<Type> mat_extract_scale(const Mat<4, 4, Type, St>& mat) noexcept
{
auto column_length = [](const Type x, const Type y, const Type z) {
auto column_length = [](const Type x, const Type y, const Type z)
{
return static_cast<Type>(internal::sqrt(x * x + y * y + z * z));
};
@@ -672,96 +680,83 @@ namespace omath
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR, class Angle>
[[nodiscard("You must use rotation matrix")]]
OMATH_CONSTEXPR Mat<4, 4, Type, St> mat_rotation_axis_x(const Angle& angle) noexcept
constexpr 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, 0, 0, 1}
};
return {{1, 0, 0, 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("You must use rotation matrix")]]
OMATH_CONSTEXPR Mat<4, 4, Type, St> mat_rotation_axis_y(const Angle& angle) noexcept
constexpr Mat<4, 4, Type, St> mat_rotation_axis_y(const Angle& angle) noexcept
{
return
{
{angle.cos(), 0, angle.sin(), 0},
{0 , 1, 0, 0},
{-angle.sin(), 0, angle.cos(), 0},
{0 , 0, 0, 1}
};
return {{angle.cos(), 0, angle.sin(), 0}, {0, 1, 0, 0}, {-angle.sin(), 0, angle.cos(), 0}, {0, 0, 0, 1}};
}
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR, class Angle>
[[nodiscard("You must use rotation matrix")]]
OMATH_CONSTEXPR Mat<4, 4, Type, St> mat_rotation_axis_z(const Angle& angle) noexcept
constexpr 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},
{ 0, 0, 1, 0},
{ 0, 0, 0, 1},
return {
{angle.cos(), -angle.sin(), 0, 0},
{angle.sin(), angle.cos(), 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1},
};
}
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
[[nodiscard("You must use camera view matrix")]]
OMATH_CONSTEXPR 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
constexpr 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>
{
{right.x, right.y, right.z, 0},
{up.x, up.y, up.z, 0},
{forward.x, forward.y, forward.z, 0},
{0, 0, 0, 1},
} * mat_translation<Type, St>(-camera_origin);
return Mat<4, 4, Type, St>{
{right.x, right.y, right.z, 0},
{up.x, up.y, up.z, 0},
{forward.x, forward.y, forward.z, 0},
{0, 0, 0, 1},
}
* mat_translation<Type, St>(-camera_origin);
}
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR,
NDCDepthRange DepthRange = NDCDepthRange::NEGATIVE_ONE_TO_ONE>
[[nodiscard("You must use perspective matrix")]] OMATH_CONSTEXPR
Mat<4, 4, Type, St> mat_perspective_left_handed_vertical_fov(const Type field_of_view, const Type aspect_ratio,
const Type near, const Type far) noexcept
[[nodiscard("You must use perspective matrix")]] constexpr Mat<4, 4, Type, St>
mat_perspective_left_handed_vertical_fov(const Type field_of_view, const Type aspect_ratio, const Type near,
const Type far) noexcept
{
const auto fov_half_tan = internal::tan(angles::degrees_to_radians(field_of_view) / Type{2});
if constexpr (DepthRange == NDCDepthRange::ZERO_TO_ONE)
return {{Type{1} / (aspect_ratio * fov_half_tan), Type{0}, Type{0}, Type{0}},
{Type{0}, Type{1} / fov_half_tan, Type{0}, Type{0}},
{Type{0}, Type{0}, far / (far - near), -(near * far) / (far - near)},
{Type{0}, Type{0}, Type{1}, Type{0}}};
return {{Type{1} / (aspect_ratio * fov_half_tan), Type{0}, Type{0}, Type{0}},
{Type{0}, Type{1} / fov_half_tan, Type{0}, Type{0}},
{Type{0}, Type{0}, far / (far - near), -(near * far) / (far - near)},
{Type{0}, Type{0}, Type{1}, Type{0}}};
else if constexpr (DepthRange == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return {{Type{1} / (aspect_ratio * fov_half_tan), Type{0}, Type{0}, Type{0}},
{Type{0}, Type{1} / fov_half_tan, Type{0}, Type{0}},
{Type{0}, Type{0}, (far + near) / (far - near), -(Type{2} * near * far) / (far - near)},
{Type{0}, Type{0}, Type{1}, Type{0}}};
return {{Type{1} / (aspect_ratio * fov_half_tan), Type{0}, Type{0}, Type{0}},
{Type{0}, Type{1} / fov_half_tan, Type{0}, Type{0}},
{Type{0}, Type{0}, (far + near) / (far - near), -(Type{2} * near * far) / (far - near)},
{Type{0}, Type{0}, Type{1}, Type{0}}};
else
std::unreachable();
}
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR,
NDCDepthRange DepthRange = NDCDepthRange::NEGATIVE_ONE_TO_ONE>
[[nodiscard("You must use perspective matrix")]] OMATH_CONSTEXPR
Mat<4, 4, Type, St> mat_perspective_right_handed_vertical_fov(const Type field_of_view, const Type aspect_ratio,
const Type near, const Type far) noexcept
[[nodiscard("You must use perspective matrix")]] constexpr Mat<4, 4, Type, St>
mat_perspective_right_handed_vertical_fov(const Type field_of_view, const Type aspect_ratio, const Type near,
const Type far) noexcept
{
const auto fov_half_tan = internal::tan(angles::degrees_to_radians(field_of_view) / Type{2});
if constexpr (DepthRange == NDCDepthRange::ZERO_TO_ONE)
return {{Type{1} / (aspect_ratio * fov_half_tan), Type{0}, Type{0}, Type{0}},
{Type{0}, Type{1} / fov_half_tan, Type{0}, Type{0}},
{Type{0}, Type{0}, -far / (far - near), -(near * far) / (far - near)},
{Type{0}, Type{0}, -Type{1}, Type{0}}};
return {{Type{1} / (aspect_ratio * fov_half_tan), Type{0}, Type{0}, Type{0}},
{Type{0}, Type{1} / fov_half_tan, Type{0}, Type{0}},
{Type{0}, Type{0}, -far / (far - near), -(near * far) / (far - near)},
{Type{0}, Type{0}, -Type{1}, Type{0}}};
else if constexpr (DepthRange == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return {{Type{1} / (aspect_ratio * fov_half_tan), Type{0}, Type{0}, Type{0}},
{Type{0}, Type{1} / fov_half_tan, Type{0}, Type{0}},
{Type{0}, Type{0}, -(far + near) / (far - near), -(Type{2} * near * far) / (far - near)},
{Type{0}, Type{0}, -Type{1}, Type{0}}};
return {{Type{1} / (aspect_ratio * fov_half_tan), Type{0}, Type{0}, Type{0}},
{Type{0}, Type{1} / fov_half_tan, Type{0}, Type{0}},
{Type{0}, Type{0}, -(far + near) / (far - near), -(Type{2} * near * far) / (far - near)},
{Type{0}, Type{0}, -Type{1}, Type{0}}};
else
std::unreachable();
}
@@ -771,35 +766,33 @@ namespace omath
// X and Y scales derived as: X = 1 / tan(hfov/2), Y = aspect / tan(hfov/2).
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR,
NDCDepthRange DepthRange = NDCDepthRange::NEGATIVE_ONE_TO_ONE>
[[nodiscard("You must use perspective matrix")]] OMATH_CONSTEXPR
Mat<4, 4, Type, St> mat_perspective_left_handed_horizontal_fov(const Type horizontal_fov,
const Type aspect_ratio, const Type near,
const Type far) noexcept
[[nodiscard("You must use perspective matrix")]] constexpr Mat<4, 4, Type, St>
mat_perspective_left_handed_horizontal_fov(const Type horizontal_fov, const Type aspect_ratio, const Type near,
const Type far) noexcept
{
const auto inv_tan_half_hfov = Type{1} / internal::tan(angles::degrees_to_radians(horizontal_fov) / Type{2});
const auto x_axis = inv_tan_half_hfov;
const auto y_axis = inv_tan_half_hfov * aspect_ratio;
if constexpr (DepthRange == NDCDepthRange::ZERO_TO_ONE)
return {{x_axis, Type{0}, Type{0}, Type{0}},
{Type{0}, y_axis, Type{0}, Type{0}},
{Type{0}, Type{0}, far / (far - near), -(near * far) / (far - near)},
{Type{0}, Type{0}, Type{1}, Type{0}}};
return {{x_axis, Type{0}, Type{0}, Type{0}},
{Type{0}, y_axis, Type{0}, Type{0}},
{Type{0}, Type{0}, far / (far - near), -(near * far) / (far - near)},
{Type{0}, Type{0}, Type{1}, Type{0}}};
else if constexpr (DepthRange == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return {{x_axis, Type{0}, Type{0}, Type{0}},
{Type{0}, y_axis, Type{0}, Type{0}},
return {{x_axis, Type{0}, Type{0}, Type{0}},
{Type{0}, y_axis, Type{0}, Type{0}},
{Type{0}, Type{0}, (far + near) / (far - near), -(Type{2} * near * far) / (far - near)},
{Type{0}, Type{0}, Type{1}, Type{0}}};
{Type{0}, Type{0}, Type{1}, Type{0}}};
else
std::unreachable();
}
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR,
NDCDepthRange DepthRange = NDCDepthRange::NEGATIVE_ONE_TO_ONE>
[[nodiscard("You must use perspective matrix")]] OMATH_CONSTEXPR
Mat<4, 4, Type, St> mat_perspective_right_handed_horizontal_fov(const Type horizontal_fov,
const Type aspect_ratio, const Type near,
const Type far) noexcept
[[nodiscard("You must use perspective matrix")]] constexpr Mat<4, 4, Type, St>
mat_perspective_right_handed_horizontal_fov(const Type horizontal_fov, const Type aspect_ratio, const Type near,
const Type far) noexcept
{
const auto inv_tan_half_hfov = Type{1} / internal::tan(angles::degrees_to_radians(horizontal_fov) / Type{2});
@@ -807,70 +800,59 @@ namespace omath
const auto y_axis = inv_tan_half_hfov * aspect_ratio;
if constexpr (DepthRange == NDCDepthRange::ZERO_TO_ONE)
return {{x_axis, Type{0}, Type{0}, Type{0}},
{Type{0}, y_axis, Type{0}, Type{0}},
return {{x_axis, Type{0}, Type{0}, Type{0}},
{Type{0}, y_axis, Type{0}, Type{0}},
{Type{0}, Type{0}, -far / (far - near), -(near * far) / (far - near)},
{Type{0}, Type{0}, -Type{1}, Type{0}}};
{Type{0}, Type{0}, -Type{1}, Type{0}}};
else if constexpr (DepthRange == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return {{x_axis, Type{0}, Type{0}, Type{0}},
{Type{0}, y_axis, Type{0}, Type{0}},
return {{x_axis, Type{0}, Type{0}, Type{0}},
{Type{0}, y_axis, Type{0}, Type{0}},
{Type{0}, Type{0}, -(far + near) / (far - near), -(Type{2} * near * far) / (far - near)},
{Type{0}, Type{0}, -Type{1}, Type{0}}};
{Type{0}, Type{0}, -Type{1}, Type{0}}};
else
std::unreachable();
}
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR,
NDCDepthRange DepthRange = NDCDepthRange::NEGATIVE_ONE_TO_ONE>
[[nodiscard("You must use ortho matrix")]] OMATH_CONSTEXPR
Mat<4, 4, Type, St> mat_ortho_left_handed(const Type left, const Type right, const Type bottom, const Type top,
const Type near, const Type far) noexcept
[[nodiscard("You must use ortho matrix")]] constexpr Mat<4, 4, Type, St>
mat_ortho_left_handed(const Type left, const Type right, const Type bottom, const Type top, const Type near,
const Type far) noexcept
{
if constexpr (DepthRange == NDCDepthRange::ZERO_TO_ONE)
return
{
{ static_cast<Type>(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)},
{ 0.f, static_cast<Type>(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)},
{ 0.f, 0.f, static_cast<Type>(1) / (far - near), -near / (far - near) },
{ 0.f, 0.f, 0.f, 1.f }
};
return {{static_cast<Type>(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)},
{0.f, static_cast<Type>(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)},
{0.f, 0.f, static_cast<Type>(1) / (far - near), -near / (far - near)},
{0.f, 0.f, 0.f, 1.f}};
else if constexpr (DepthRange == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return
{
{ static_cast<Type>(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)},
{ 0.f, static_cast<Type>(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)},
{ 0.f, 0.f, static_cast<Type>(2) / (far - near), -(far + near) / (far - near) },
{ 0.f, 0.f, 0.f, 1.f }
};
return {{static_cast<Type>(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)},
{0.f, static_cast<Type>(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)},
{0.f, 0.f, static_cast<Type>(2) / (far - near), -(far + near) / (far - near)},
{0.f, 0.f, 0.f, 1.f}};
else
std::unreachable();
}
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR,
NDCDepthRange DepthRange = NDCDepthRange::NEGATIVE_ONE_TO_ONE>
[[nodiscard("You must use ortho matrix")]] OMATH_CONSTEXPR
Mat<4, 4, Type, St> mat_ortho_right_handed(const Type left, const Type right, const Type bottom, const Type top,
const Type near, const Type far) noexcept
[[nodiscard("You must use ortho matrix")]] constexpr Mat<4, 4, Type, St>
mat_ortho_right_handed(const Type left, const Type right, const Type bottom, const Type top, const Type near,
const Type far) noexcept
{
if constexpr (DepthRange == NDCDepthRange::ZERO_TO_ONE)
return
{
{ static_cast<Type>(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)},
{ 0.f, static_cast<Type>(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)},
{ 0.f, 0.f, -static_cast<Type>(1) / (far - near), -near / (far - near) },
{ 0.f, 0.f, 0.f, 1.f }
};
return {{static_cast<Type>(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)},
{0.f, static_cast<Type>(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)},
{0.f, 0.f, -static_cast<Type>(1) / (far - near), -near / (far - near)},
{0.f, 0.f, 0.f, 1.f}};
else if constexpr (DepthRange == NDCDepthRange::NEGATIVE_ONE_TO_ONE)
return
{
{ static_cast<Type>(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)},
{ 0.f, static_cast<Type>(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)},
{ 0.f, 0.f, -static_cast<Type>(2) / (far - near), -(far + near) / (far - near) },
{ 0.f, 0.f, 0.f, 1.f }
};
return {{static_cast<Type>(2) / (right - left), 0.f, 0.f, -(right + left) / (right - left)},
{0.f, static_cast<Type>(2) / (top - bottom), 0.f, -(top + bottom) / (top - bottom)},
{0.f, 0.f, -static_cast<Type>(2) / (far - near), -(far + near) / (far - near)},
{0.f, 0.f, 0.f, 1.f}};
else
std::unreachable();
}
template<class T = float, MatStoreType St = MatStoreType::COLUMN_MAJOR>
OMATH_CONSTEXPR Mat<4, 4, T, St> mat_look_at_left_handed(const Vector3<T>& eye, const Vector3<T>& center, const Vector3<T>& up)
constexpr Mat<4, 4, T, St> mat_look_at_left_handed(const Vector3<T>& eye, const Vector3<T>& center,
const Vector3<T>& up)
{
const Vector3<T> f = (center - eye).normalized();
const Vector3<T> s = f.cross(up).normalized();
@@ -879,7 +861,8 @@ namespace omath
}
template<class T = float, MatStoreType St = MatStoreType::COLUMN_MAJOR>
OMATH_CONSTEXPR Mat<4, 4, T, St> mat_look_at_right_handed(const Vector3<T>& eye, const Vector3<T>& center, const Vector3<T>& up)
constexpr Mat<4, 4, T, St> mat_look_at_right_handed(const Vector3<T>& eye, const Vector3<T>& center,
const Vector3<T>& up)
{
const Vector3<T> f = (center - eye).normalized();
const Vector3<T> s = f.cross(up).normalized();
+3 -3
View File
@@ -2,7 +2,7 @@
// Created by Orange on 11/13/2024.
//
#pragma once
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "vector3.hpp"
namespace omath
{
@@ -40,13 +40,13 @@ namespace omath
}
[[nodiscard]]
OMATH_CONSTEXPR Vector::ContainedType side_a_length() const
constexpr Vector::ContainedType side_a_length() const
{
return m_vertex1.distance_to(m_vertex2);
}
[[nodiscard]]
OMATH_CONSTEXPR Vector::ContainedType side_b_length() const
constexpr Vector::ContainedType side_b_length() const
{
return m_vertex3.distance_to(m_vertex2);
}
+8 -8
View File
@@ -3,7 +3,7 @@
//
#pragma once
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/internal/constexpr_math.hpp"
#include <cmath>
#include <format>
#include <tuple>
@@ -117,7 +117,7 @@ namespace omath
// Basic vector operations
[[nodiscard("You must use distance")]]
OMATH_CONSTEXPR Type distance_to(const Vector2& other) const noexcept
constexpr Type distance_to(const Vector2& other) const noexcept
{
return internal::sqrt(distance_to_sqr(other));
}
@@ -147,13 +147,13 @@ namespace omath
}
#else
[[nodiscard("You must use length")]]
OMATH_CONSTEXPR Type length() const noexcept
constexpr Type length() const noexcept
{
return internal::hypot(x, y);
}
[[nodiscard("You must use normalized vector")]]
OMATH_CONSTEXPR Vector2 normalized() const noexcept
constexpr Vector2 normalized() const noexcept
{
const Type len = length();
return len > static_cast<Type>(0) ? *this / len : *this;
@@ -217,24 +217,24 @@ namespace omath
}
[[nodiscard("You must use comparison result")]]
OMATH_CONSTEXPR bool operator<(const Vector2& other) const noexcept
constexpr bool operator<(const Vector2& other) const noexcept
{
return length() < other.length();
}
[[nodiscard("You must use comparison result")]]
OMATH_CONSTEXPR bool operator>(const Vector2& other) const noexcept
constexpr bool operator>(const Vector2& other) const noexcept
{
return length() > other.length();
}
[[nodiscard("You must use comparison result")]]
OMATH_CONSTEXPR bool operator<=(const Vector2& other) const noexcept
constexpr bool operator<=(const Vector2& other) const noexcept
{
return length() <= other.length();
}
[[nodiscard("You must use comparison result")]]
OMATH_CONSTEXPR bool operator>=(const Vector2& other) const noexcept
constexpr bool operator>=(const Vector2& other) const noexcept
{
return length() >= other.length();
}
+13 -14
View File
@@ -4,7 +4,7 @@
#pragma once
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/linear_algebra/vector2.hpp"
#include "omath/trigonometry/angle.hpp"
#include <cstdint>
@@ -150,7 +150,7 @@ namespace omath
{
return Vector2<Type>::length();
}
[[nodiscard("You must use distance")]] OMATH_CONSTEXPR Type distance_to(const Vector3& other) const noexcept
[[nodiscard("You must use distance")]] constexpr Type distance_to(const Vector3& other) const noexcept
{
return (*this - other).length();
}
@@ -162,13 +162,13 @@ namespace omath
}
#else
[[nodiscard("You must use length")]]
OMATH_CONSTEXPR Type length() const noexcept
constexpr Type length() const noexcept
{
return internal::hypot(this->x, this->y, this->z);
}
[[nodiscard("You must use normalized vector")]]
OMATH_CONSTEXPR Vector3 normalized() const noexcept
constexpr Vector3 normalized() const noexcept
{
const Type len = this->length();
@@ -176,13 +176,13 @@ namespace omath
}
[[nodiscard("You must use 2D length")]]
OMATH_CONSTEXPR Type length_2d() const noexcept
constexpr Type length_2d() const noexcept
{
return Vector2<Type>::length();
}
[[nodiscard("You must use distance")]]
OMATH_CONSTEXPR Type distance_to(const Vector3& v_other) const noexcept
constexpr Type distance_to(const Vector3& v_other) const noexcept
{
return (*this - v_other).length();
}
@@ -250,12 +250,12 @@ namespace omath
}
[[nodiscard("You must use direction check result")]]
OMATH_CONSTEXPR bool point_to_same_direction(const Vector3& other) const
constexpr bool point_to_same_direction(const Vector3& other) const
{
return dot(other) > static_cast<Type>(0);
}
[[nodiscard("You must use angle between vectors")]]
OMATH_CONSTEXPR std::expected<Angle<float, 0.f, 180.f, AngleFlags::Clamped>, Vector3Error>
constexpr std::expected<Angle<float, 0.f, 180.f, AngleFlags::Clamped>, Vector3Error>
angle_between(const Vector3& other) const noexcept
{
const auto bottom = length() * other.length();
@@ -266,8 +266,7 @@ namespace omath
}
[[nodiscard("You must use perpendicularity check result")]]
OMATH_CONSTEXPR bool is_perpendicular(const Vector3& other,
Type epsilon = static_cast<Type>(0.0001)) const noexcept
constexpr bool is_perpendicular(const Vector3& other, Type epsilon = static_cast<Type>(0.0001)) const noexcept
{
if (const auto angle = angle_between(other))
return std::abs(angle->as_degrees() - static_cast<Type>(90)) <= epsilon;
@@ -288,25 +287,25 @@ namespace omath
}
[[nodiscard("You must use comparison result")]]
OMATH_CONSTEXPR bool operator<(const Vector3& other) const noexcept
constexpr bool operator<(const Vector3& other) const noexcept
{
return length() < other.length();
}
[[nodiscard("You must use comparison result")]]
OMATH_CONSTEXPR bool operator>(const Vector3& other) const noexcept
constexpr bool operator>(const Vector3& other) const noexcept
{
return length() > other.length();
}
[[nodiscard("You must use comparison result")]]
OMATH_CONSTEXPR bool operator<=(const Vector3& other) const noexcept
constexpr bool operator<=(const Vector3& other) const noexcept
{
return length() <= other.length();
}
[[nodiscard("You must use comparison result")]]
OMATH_CONSTEXPR bool operator>=(const Vector3& other) const noexcept
constexpr bool operator>=(const Vector3& other) const noexcept
{
return length() >= other.length();
}
+32 -30
View File
@@ -6,7 +6,7 @@
#include "omath/3d_primitives/aabb.hpp"
#include "omath/3d_primitives/obb.hpp"
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/linear_algebra/mat.hpp"
#include "omath/linear_algebra/triangle.hpp"
#include "omath/linear_algebra/vector3.hpp"
@@ -104,7 +104,7 @@ namespace omath::projection
// NEGATIVE_ONE_TO_ONE) share the same m[0,0]/m[1,1] layout, so this works
// regardless of the NDC depth range.
[[nodiscard("You must use extracted projection params")]]
OMATH_CONSTEXPR static ProjectionParams extract_projection_params(const Mat4X4Type& proj_matrix) noexcept
constexpr static ProjectionParams extract_projection_params(const Mat4X4Type& proj_matrix) noexcept
{
// m[1,1] == 1 / tan(fov/2) => fov = 2 * atan(1 / m[1,1])
const auto f = proj_matrix.at(1, 1);
@@ -115,7 +115,7 @@ namespace omath::projection
}
[[nodiscard("You must use calculated view angles")]]
OMATH_CONSTEXPR static ViewAnglesType calc_view_angles_from_view_matrix(const Mat4X4Type& view_matrix) noexcept
constexpr static ViewAnglesType calc_view_angles_from_view_matrix(const Mat4X4Type& view_matrix) noexcept
{
Vector3<NumericType> forward_vector = {view_matrix[2, 0], view_matrix[2, 1], view_matrix[2, 2]};
if constexpr (axes.inverted_forward)
@@ -138,20 +138,20 @@ namespace omath::projection
};
}
OMATH_CONSTEXPR void look_at(const Vector3<NumericType>& target)
constexpr void look_at(const Vector3<NumericType>& target)
{
m_view_angles = TraitClass::calc_look_at_angle(m_origin, target);
m_view_projection_matrix = std::nullopt;
m_view_matrix = std::nullopt;
}
[[nodiscard("You must use calculated look-at angles")]]
OMATH_CONSTEXPR ViewAnglesType calc_look_at_angles(const Vector3<NumericType>& look_to) const
constexpr ViewAnglesType calc_look_at_angles(const Vector3<NumericType>& look_to) const
{
return TraitClass::calc_look_at_angle(m_origin, look_to);
}
[[nodiscard("You must use forward vector")]]
OMATH_CONSTEXPR Vector3<NumericType> get_forward() const noexcept
constexpr Vector3<NumericType> get_forward() const noexcept
{
if consteval
{
@@ -164,7 +164,7 @@ namespace omath::projection
}
[[nodiscard("You must use right vector")]]
OMATH_CONSTEXPR Vector3<NumericType> get_right() const noexcept
constexpr Vector3<NumericType> get_right() const noexcept
{
if consteval
{
@@ -177,7 +177,7 @@ namespace omath::projection
}
[[nodiscard("You must use up vector")]]
OMATH_CONSTEXPR Vector3<NumericType> get_up() const noexcept
constexpr Vector3<NumericType> get_up() const noexcept
{
if consteval
{
@@ -189,7 +189,7 @@ namespace omath::projection
return {view_matrix[1, 0], view_matrix[1, 1], view_matrix[1, 2]};
}
[[nodiscard("You must use absolute forward vector")]]
OMATH_CONSTEXPR Vector3<NumericType> get_abs_forward() const noexcept
constexpr Vector3<NumericType> get_abs_forward() const noexcept
{
if constexpr (axes.inverted_forward)
return -get_forward();
@@ -197,7 +197,7 @@ namespace omath::projection
}
[[nodiscard("You must use absolute right vector")]]
OMATH_CONSTEXPR Vector3<NumericType> get_abs_right() const noexcept
constexpr Vector3<NumericType> get_abs_right() const noexcept
{
if constexpr (axes.inverted_right)
return -get_right();
@@ -205,32 +205,32 @@ namespace omath::projection
}
[[nodiscard("You must use absolute up vector")]]
OMATH_CONSTEXPR Vector3<NumericType> get_abs_up() const noexcept
constexpr Vector3<NumericType> get_abs_up() const noexcept
{
return get_up();
}
[[nodiscard("You must use calculated view-projection matrix")]]
OMATH_CONSTEXPR Mat4X4Type calc_view_projection_matrix() const noexcept
constexpr Mat4X4Type calc_view_projection_matrix() const noexcept
{
return calc_projection_matrix() * calc_view_matrix();
}
[[nodiscard("You must use calculated view matrix")]]
OMATH_CONSTEXPR Mat4X4Type calc_view_matrix() const noexcept
constexpr Mat4X4Type calc_view_matrix() const noexcept
{
return TraitClass::calc_view_matrix(m_view_angles, m_origin);
}
[[nodiscard("You must use calculated projection matrix")]]
OMATH_CONSTEXPR Mat4X4Type calc_projection_matrix() const noexcept
constexpr Mat4X4Type calc_projection_matrix() const noexcept
{
return TraitClass::calc_projection_matrix(
m_field_of_view, m_view_port, m_near_plane_distance, m_far_plane_distance, depth_range);
return TraitClass::calc_projection_matrix(m_field_of_view, m_view_port, m_near_plane_distance,
m_far_plane_distance, depth_range);
}
[[nodiscard("You must use view-projection matrix")]]
OMATH_CONSTEXPR const Mat4X4Type& get_view_projection_matrix() const noexcept
constexpr const Mat4X4Type& get_view_projection_matrix() const noexcept
{
if (!m_view_projection_matrix.has_value())
m_view_projection_matrix = get_projection_matrix() * get_view_matrix();
@@ -238,14 +238,15 @@ namespace omath::projection
return m_view_projection_matrix.value();
}
[[nodiscard("You must use view matrix")]] OMATH_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();
return m_view_matrix.value();
}
[[nodiscard("You must use projection matrix")]] OMATH_CONSTEXPR const Mat4X4Type& get_projection_matrix() const noexcept
[[nodiscard("You must use projection matrix")]] constexpr const Mat4X4Type&
get_projection_matrix() const noexcept
{
if (!m_projection_matrix.has_value())
m_projection_matrix = calc_projection_matrix();
@@ -320,7 +321,7 @@ namespace omath::projection
}
template<ScreenStart screen_start = ScreenStart::TOP_LEFT_CORNER>
[[nodiscard("You must use screen position")]] OMATH_CONSTEXPR std::expected<Vector3<NumericType>, Error>
[[nodiscard("You must use screen position")]] constexpr std::expected<Vector3<NumericType>, Error>
world_to_screen(const Vector3<NumericType>& world_position) const noexcept
{
const auto normalized_cords = world_to_view_port(world_position);
@@ -336,7 +337,7 @@ namespace omath::projection
std::unreachable();
}
template<ScreenStart screen_start = ScreenStart::TOP_LEFT_CORNER>
[[nodiscard("You must use unclipped screen position")]] OMATH_CONSTEXPR std::expected<Vector3<NumericType>, Error>
[[nodiscard("You must use unclipped screen position")]] constexpr std::expected<Vector3<NumericType>, Error>
world_to_screen_unclipped(const Vector3<NumericType>& world_position) const noexcept
{
const auto normalized_cords = world_to_view_port(world_position, ViewPortClipping::MANUAL);
@@ -352,7 +353,7 @@ namespace omath::projection
std::unreachable();
}
[[nodiscard("You must use frustum culling result")]] OMATH_CONSTEXPR bool
[[nodiscard("You must use frustum culling result")]] constexpr bool
is_culled_by_frustum(const Triangle<Vector3<NumericType>>& triangle) const noexcept
{
// Transform to clip space (before perspective divide)
@@ -420,7 +421,7 @@ namespace omath::projection
return false;
}
[[nodiscard("You must use AABB frustum culling result")]] OMATH_CONSTEXPR bool
[[nodiscard("You must use AABB frustum culling result")]] constexpr bool
is_aabb_culled_by_frustum(const primitives::Aabb<NumericType>& aabb) const noexcept
{
// For each plane, find the AABB corner most in the direction of the plane normal
@@ -438,7 +439,7 @@ namespace omath::projection
return false;
}
[[nodiscard("You must use OBB frustum culling result")]] OMATH_CONSTEXPR bool
[[nodiscard("You must use OBB frustum culling result")]] constexpr bool
is_obb_culled_by_frustum(const primitives::Obb<NumericType>& obb) const noexcept
{
// For each plane, project the OBB extents onto the plane normal to get the
@@ -459,7 +460,7 @@ namespace omath::projection
return false;
}
[[nodiscard("You must use view port position")]] OMATH_CONSTEXPR std::expected<Vector3<NumericType>, Error>
[[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
{
@@ -502,10 +503,11 @@ namespace omath::projection
return project_to_view_port(projected);
}
[[nodiscard("You must use world position")]]
OMATH_CONSTEXPR std::expected<Vector3<NumericType>, Error>
constexpr std::expected<Vector3<NumericType>, Error>
view_port_to_world(const Vector3<NumericType>& ndc) const noexcept
{
auto view_port_to_world = [&ndc](const Mat4X4Type& view_projection) -> std::expected<Vector3<NumericType>, Error>
auto view_port_to_world =
[&ndc](const Mat4X4Type& view_projection) -> std::expected<Vector3<NumericType>, Error>
{
const auto inv_view_proj = view_projection.inverted();
@@ -536,7 +538,7 @@ namespace omath::projection
template<ScreenStart screen_start = ScreenStart::TOP_LEFT_CORNER>
[[nodiscard("You must use world position")]]
OMATH_CONSTEXPR std::expected<Vector3<NumericType>, Error>
constexpr std::expected<Vector3<NumericType>, Error>
screen_to_world(const Vector3<NumericType>& screen_pos) const noexcept
{
return view_port_to_world(screen_to_ndc<screen_start>(screen_pos));
@@ -544,7 +546,7 @@ namespace omath::projection
template<ScreenStart screen_start = ScreenStart::TOP_LEFT_CORNER>
[[nodiscard("You must use world position")]]
OMATH_CONSTEXPR std::expected<Vector3<NumericType>, Error>
constexpr std::expected<Vector3<NumericType>, Error>
screen_to_world(const Vector2<NumericType>& screen_pos) const noexcept
{
const auto& [x, y] = screen_pos;
@@ -579,7 +581,7 @@ namespace omath::projection
// Top = r3 - r1
// Near = r3 + r2 ([-1,1]) or r2 ([0,1])
// Far = r3 - r2
[[nodiscard("You must use frustum planes")]] OMATH_CONSTEXPR std::array<FrustumPlane, 6>
[[nodiscard("You must use frustum planes")]] constexpr std::array<FrustumPlane, 6>
extract_frustum_planes() const noexcept
{
const auto& m = get_view_projection_matrix();
+6 -6
View File
@@ -3,7 +3,7 @@
//
#pragma once
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/internal/constexpr_math.hpp"
#include "omath/trigonometry/angles.hpp"
#include <algorithm>
#include <format>
@@ -71,31 +71,31 @@ namespace omath
}
[[nodiscard]]
OMATH_CONSTEXPR Type sin() const noexcept
constexpr Type sin() const noexcept
{
return internal::sin(as_radians());
}
[[nodiscard]]
OMATH_CONSTEXPR Type cos() const noexcept
constexpr Type cos() const noexcept
{
return internal::cos(as_radians());
}
[[nodiscard]]
OMATH_CONSTEXPR Type tan() const noexcept
constexpr Type tan() const noexcept
{
return internal::tan(as_radians());
}
[[nodiscard]]
OMATH_CONSTEXPR Type atan() const noexcept
constexpr Type atan() const noexcept
{
return internal::atan(as_radians());
}
[[nodiscard]]
OMATH_CONSTEXPR Type cot() const noexcept
constexpr Type cot() const noexcept
{
return cos() / sin();
}
+6 -7
View File
@@ -3,7 +3,7 @@
//
#pragma once
#include "omath/internal/optional_constexpr_math.hpp"
#include "omath/internal/constexpr_math.hpp"
#include <cmath>
#include <numbers>
@@ -25,20 +25,19 @@ namespace omath::angles
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]] OMATH_CONSTEXPR Type horizontal_fov_to_vertical(const Type& horizontal_fov,
const Type& aspect) noexcept
[[nodiscard]] constexpr Type horizontal_fov_to_vertical(const Type& horizontal_fov, const Type& aspect) noexcept
{
const auto fov_rad = degrees_to_radians(horizontal_fov);
const auto vert_fov = static_cast<Type>(2) * internal::atan(internal::tan(fov_rad / static_cast<Type>(2)) / aspect);
const auto vert_fov =
static_cast<Type>(2) * internal::atan(internal::tan(fov_rad / static_cast<Type>(2)) / aspect);
return radians_to_degrees(vert_fov);
}
template<class Type>
requires std::is_floating_point_v<Type>
[[nodiscard]] OMATH_CONSTEXPR Type vertical_fov_to_horizontal(const Type& vertical_fov,
const Type& aspect) noexcept
[[nodiscard]] constexpr Type vertical_fov_to_horizontal(const Type& vertical_fov, const Type& aspect) noexcept
{
const auto fov_as_radians = degrees_to_radians(vertical_fov);
@@ -50,7 +49,7 @@ namespace omath::angles
template<class Type>
requires std::is_arithmetic_v<Type>
[[nodiscard]] OMATH_CONSTEXPR Type wrap_angle(const Type& angle, const Type& min, const Type& max) noexcept
[[nodiscard]] constexpr Type wrap_angle(const Type& angle, const Type& min, const Type& max) noexcept
{
if (angle <= max && angle >= min)
return angle;