Skip to content

Commit

Permalink
Use C++20 std::numbers instead of Methane::Data::Constants
Browse files Browse the repository at this point in the history
  • Loading branch information
egorodet committed Jan 7, 2025
1 parent b5f087f commit b41b7c1
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 73 deletions.
2 changes: 1 addition & 1 deletion Apps/03-TexturedCube/TexturedCubeApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void TexturedCubeApp::Init()

bool TexturedCubeApp::Animate(double, double delta_seconds)
{
const float rotation_angle_rad = static_cast<float>(delta_seconds * 360.F / 4.F) * gfx::ConstFloat::RadPerDeg;
const float rotation_angle_rad = Methane::Data::DegreeToRadians(static_cast<float>(delta_seconds * 360.F / 4.F));
const hlslpp::float3x3 light_rotate_matrix = hlslpp::float3x3::rotation_axis(m_camera.GetOrientation().up, rotation_angle_rad);
m_shader_uniforms.light_position = hlslpp::mul(m_shader_uniforms.light_position, light_rotate_matrix);
m_camera.Rotate(m_camera.GetOrientation().up, static_cast<float>(delta_seconds * 360.F / 8.F));
Expand Down
7 changes: 4 additions & 3 deletions Apps/06-CubeMapArray/CubeMapArrayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Tutorial demonstrating textured cube rendering with Methane graphics API
#include <Methane/Data/TimeAnimation.h>

#include <cmath>
#include <numbers>

namespace Methane::Tutorials
{
Expand Down Expand Up @@ -58,7 +59,7 @@ CubeMapArrayApp::CubeMapArrayApp()
}(),
GetUserInterfaceTutorialAppSettings(AppOptions::GetDefaultWithColorDepthAndAnim()),
"Methane tutorial of cube-map array texturing")
, m_model_matrix(hlslpp::mul(hlslpp::float4x4::scale(g_model_scale), hlslpp::float4x4::rotation_z(gfx::ConstFloat::Pi))) // NOSONAR
, m_model_matrix(hlslpp::mul(hlslpp::float4x4::scale(g_model_scale), hlslpp::float4x4::rotation_z(std::numbers::pi))) // NOSONAR
{
// NOTE: Near and Far values are swapped in camera parameters (1st value is near = max depth, 2nd value is far = min depth)
// for Reversed-Z buffer values range [ near: 1, far 0], instead of [ near 0, far 1]
Expand Down Expand Up @@ -201,8 +202,8 @@ bool CubeMapArrayApp::Animate(double, double delta_seconds)
{
m_camera.Rotate(m_camera.GetOrientation().up, static_cast<float>(delta_seconds * 360.0 / 16.0));
m_model_matrix = hlslpp::mul(m_model_matrix,
hlslpp::mul(hlslpp::float4x4::rotation_z(static_cast<float>(delta_seconds * gfx::ConstDouble::Pi / 2.0)),
hlslpp::float4x4::rotation_y(static_cast<float>(delta_seconds * gfx::ConstDouble::Pi / 4.0))));
hlslpp::mul(hlslpp::float4x4::rotation_z(static_cast<float>(delta_seconds * std::numbers::pi / 2.0)),
hlslpp::float4x4::rotation_y(static_cast<float>(delta_seconds * std::numbers::pi / 4.0))));
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion Apps/07-ParallelRendering/ParallelRenderingApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Tutorial demonstrating parallel rendering with Methane graphics API
#include <cmath>
#include <random>
#include <algorithm>
#include <numbers>

namespace Methane::Tutorials
{
Expand Down Expand Up @@ -369,7 +370,7 @@ bool ParallelRenderingApp::Animate(double, double delta_seconds)
META_FUNCTION_TASK();
m_camera.Rotate(m_camera.GetOrientation().up, static_cast<float>(delta_seconds * 360.0 / 16.0));

const double delta_angle_rad = delta_seconds * gfx::ConstDouble::Pi;
const double delta_angle_rad = delta_seconds * std::numbers::pi;
tf::Taskflow task_flow;
task_flow.for_each(m_cube_array_parameters.begin(), m_cube_array_parameters.end(),
[delta_angle_rad](CubeParameters& cube_params)
Expand Down
50 changes: 0 additions & 50 deletions Modules/Data/Types/Include/Methane/Data/Constants.hpp

This file was deleted.

13 changes: 13 additions & 0 deletions Modules/Data/Types/Include/Methane/Data/Math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Math primitive functions.
#include <type_traits>
#include <cstdlib>
#include <cmath>
#include <numbers>

#include <Methane/Checks.hpp>

Expand Down Expand Up @@ -86,4 +87,16 @@ constexpr T DivCeil(T numerator, T denominator) noexcept
}
}

template<std::floating_point T>
[[nodiscard]] constexpr T DegreeToRadians(T degrees) noexcept
{
return degrees * std::numbers::pi_v<T> / T(180.);
}

template<std::floating_point T>
[[nodiscard]] constexpr T RadiansToDegrees(T degrees) noexcept
{
return degrees * T(180.) / std::numbers::pi_v<T>;
}

} // namespace Methane::Data
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Interactive action-camera for rotating, moving and zooming with mouse and keyboa

#include <Methane/Graphics/ActionCamera.h>
#include <Methane/Data/TimeAnimation.h>
#include <Methane/Data/Math.hpp>
#include <Methane/Instrumentation.h>
#include <Methane/Checks.hpp>

Expand Down Expand Up @@ -175,7 +176,7 @@ void ActionCamera::StartRotateAction(KeyboardAction rotate_action, const hlslpp:
if (StartKeyboardAction(rotate_action, duration_sec))
return;

const float angle_rad_per_second = m_rotate_angle_per_second * ConstFloat::RadPerDeg;
const float angle_rad_per_second = Methane::Data::DegreeToRadians(m_rotate_angle_per_second);
m_animations.push_back(
std::make_shared<Data::TimeAnimation>([this, angle_rad_per_second, rotation_axis_in_view](double elapsed_seconds, double delta_seconds)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Arc-ball camera rotation with mouse handling.
#include <Methane/Checks.hpp>

#include <cmath>
#include <numbers>

namespace Methane::Graphics
{
Expand Down Expand Up @@ -77,7 +78,7 @@ void ArcBallCamera::MouseDrag(const Point2I& mouse_screen_pos)
RotateInView(rotation_axis, rotation_angle, m_mouse_pressed_orientation);

// NOTE: fixes rotation axis flip at angles approaching to 180 degrees
if (std::abs(rotation_angle) > ConstFloat::PiDiv2)
if (std::abs(rotation_angle) > std::numbers::pi / 2.f)
{
m_mouse_pressed_orientation = GetOrientation();
m_mouse_pressed_on_sphere = mouse_current_on_sphere;
Expand Down
5 changes: 3 additions & 2 deletions Modules/Graphics/Camera/Sources/Methane/Graphics/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Camera helper implementation allowing to generate view and projection matrices.

#include <Methane/Graphics/Camera.h>
#include <Methane/Graphics/TypeFormatters.hpp>
#include <Methane/Data/Math.hpp>
#include <Methane/Instrumentation.h>
#include <Methane/Checks.hpp>

Expand Down Expand Up @@ -84,7 +85,7 @@ void Camera::UpdateProjectionSettings()
void Camera::Rotate(const hlslpp::float3& axis, float angle_deg) noexcept
{
META_FUNCTION_TASK();
const hlslpp::float3x3 rotation_matrix = hlslpp::float3x3::rotation_axis(axis, angle_deg * ConstFloat::RadPerDeg);
const hlslpp::float3x3 rotation_matrix = hlslpp::float3x3::rotation_axis(axis, Data::DegreeToRadians(angle_deg));
const hlslpp::float3 new_look_dir = hlslpp::mul(GetLookDirection(), rotation_matrix);
SetOrientationEye(GetOrientation().aim - new_look_dir);
}
Expand Down Expand Up @@ -179,7 +180,7 @@ hlslpp::float4 Camera::TransformViewToWorld(const hlslpp::float4& view_pos, cons
float Camera::GetFovAngleY() const noexcept
{
META_FUNCTION_TASK();
float fov_angle_y = m_parameters.fov_deg * ConstFloat::RadPerDeg;
float fov_angle_y = Data::DegreeToRadians(m_parameters.fov_deg);
if (m_aspect_ratio != 0.F && m_aspect_ratio < 1.0F)
{
fov_angle_y /= (0.5F + m_aspect_ratio / 2.F);
Expand Down
2 changes: 0 additions & 2 deletions Modules/Graphics/Mesh/Include/Methane/Graphics/SphereMesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ Sphere mesh generator with customizable vertex type

#include "BaseMesh.hpp"

#include <Methane/Data/Constants.hpp>

namespace Methane::Graphics
{

Expand Down
6 changes: 0 additions & 6 deletions Modules/Graphics/Types/Include/Methane/Graphics/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Methane primitive graphics types.
#pragma once

#include <Methane/Data/Types.h>
#include <Methane/Data/Constants.hpp>

#include <string>
#include <cstdint>
Expand Down Expand Up @@ -54,11 +53,6 @@ using RawVector2F = Data::RawVector2F;
using RawVector3F = Data::RawVector3F;
using RawVector4F = Data::RawVector4F;

template<typename T>
using Constants = Data::Constants<T>;
using ConstFloat = Data::ConstFloat;
using ConstDouble = Data::ConstDouble;

enum class PixelFormat : uint32_t
{
Unknown = 0U,
Expand Down
13 changes: 7 additions & 6 deletions Tests/Graphics/Camera/ArcBallCameraTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Arc-Ball camera unit tests

#include <Methane/Graphics/ArcBallCamera.h>
#include <Methane/Data/Types.h>
#include <Methane/Data/Math.hpp>
#include <Methane/Checks.hpp>
#include <Methane/HlslCatchHelpers.hpp>

Expand Down Expand Up @@ -101,7 +102,7 @@ inline void TestDependentCameraRotation(ArcBallCamera::Pivot view_pivot,

inline Camera::Orientation RotateOrientation(const Camera::Orientation& orientation, const ArcBallCamera::Pivot pivot, const hlslpp::float3& axis, float angle_degrees)
{
hlslpp::float3x3 rotation_matrix = hlslpp::float3x3::rotation_axis(hlslpp::normalize(axis), angle_degrees * ConstFloat::RadPerDeg);
hlslpp::float3x3 rotation_matrix = hlslpp::float3x3::rotation_axis(hlslpp::normalize(axis), Data::DegreeToRadians(angle_degrees));
const hlslpp::float3 look_dir = hlslpp::mul(orientation.aim - orientation.eye, rotation_matrix);
const hlslpp::float3 up_dir = hlslpp::mul(orientation.up, rotation_matrix);

Expand Down Expand Up @@ -149,7 +150,7 @@ TEST_CASE("View arc-ball camera rotation around Aim pivot < 90 degrees", "[camer
// Lower equality precision because of integer screen-space coordinates use
const float test_equality_epsilon = 0.1F;
const float test_angle_deg = 45.f;
const float test_angle_rad = test_angle_deg * ConstFloat::RadPerDeg;
const float test_angle_rad = Data::DegreeToRadians(test_angle_deg);

SECTION("Around X axis")
{
Expand Down Expand Up @@ -213,7 +214,7 @@ TEST_CASE("View arc-ball camera rotation around Aim pivot > 90 degrees", "[camer
// Lower equality precision because of integer screen-space coordinates use
const float test_equality_epsilon = 0.1F;
const float test_angle_deg = 135.f;
const float test_angle_rad = test_angle_deg * ConstFloat::RadPerDeg;
const float test_angle_rad = Data::DegreeToRadians(test_angle_deg);

SECTION("Around X axis")
{
Expand Down Expand Up @@ -277,7 +278,7 @@ TEST_CASE("View arc-ball camera rotation around Eye pivot < 90 degrees", "[camer
// Lower equality precision because of integer screen-space coordinates use
const float test_equality_epsilon = 0.1F;
const float test_angle_deg = 45.f;
const float test_angle_rad = test_angle_deg * ConstFloat::RadPerDeg;
const float test_angle_rad = Data::DegreeToRadians(test_angle_deg);

SECTION("Around X axis")
{
Expand Down Expand Up @@ -341,7 +342,7 @@ TEST_CASE("View arc-ball camera rotation around Eye pivot > 90 degrees", "[camer
// Lower equality precision because of integer screen-space coordinates use
const float test_equality_epsilon = 0.1F;
const float test_angle_deg = 135.f;
const float test_angle_rad = test_angle_deg * ConstFloat::RadPerDeg;
const float test_angle_rad = Data::DegreeToRadians(test_angle_deg);

SECTION("Around X axis")
{
Expand Down Expand Up @@ -407,7 +408,7 @@ TEST_CASE("Dependent arc-ball camera rotation around Aim pivot < 90 degrees", "[
// Lower equality precision because of integer screen-space coordinates use
const float test_equality_epsilon = 0.1F;
const float test_angle_deg = 45.f;
const float test_angle_rad = test_angle_deg * ConstFloat::RadPerDeg;
const float test_angle_rad = Data::DegreeToRadians(test_angle_deg);

SECTION("Around X axis")
{
Expand Down

0 comments on commit b41b7c1

Please sign in to comment.