100 lines
4.3 KiB
C
100 lines
4.3 KiB
C
|
/****************************************************************************************
|
||
|
* \file SHMathHelpers.h
|
||
|
* \author Diren D Bharwani, diren.dbharwani, 390002520
|
||
|
* \brief Interface for various mathematical helper functions.
|
||
|
*
|
||
|
* \copyright Copyright (C) 2022 DigiPen Institute of Technology. Reproduction or
|
||
|
* disclosure of this file or its contents without the prior written consent
|
||
|
* of DigiPen Institute of Technology is prohibited.
|
||
|
****************************************************************************************/
|
||
|
|
||
|
#pragma once
|
||
|
|
||
|
#include <type_traits>
|
||
|
#include <numeric>
|
||
|
#include <numbers>
|
||
|
#include <random>
|
||
|
|
||
|
|
||
|
namespace SHADE
|
||
|
{
|
||
|
/*-----------------------------------------------------------------------------------*/
|
||
|
/* Concepts */
|
||
|
/*-----------------------------------------------------------------------------------*/
|
||
|
|
||
|
template <typename T>
|
||
|
concept IsArithmetic = std::is_arithmetic_v<T>;
|
||
|
|
||
|
template <typename T>
|
||
|
concept IsIntegral = std::is_integral_v<T>;
|
||
|
|
||
|
template <typename T>
|
||
|
concept IsFloatingPoint = std::is_floating_point_v<T>;
|
||
|
|
||
|
/*-----------------------------------------------------------------------------------*/
|
||
|
/* Type Definitions */
|
||
|
/*-----------------------------------------------------------------------------------*/
|
||
|
|
||
|
class SHMath
|
||
|
{
|
||
|
public:
|
||
|
/*---------------------------------------------------------------------------------*/
|
||
|
/* Static Data Members */
|
||
|
/*---------------------------------------------------------------------------------*/
|
||
|
|
||
|
/** Standard Epsilon value for comparing Single-Precision Floating-Point values. */
|
||
|
static constexpr float EPSILON = 0.001f;
|
||
|
|
||
|
/** Single-Precision Floating-Point value of infinity */
|
||
|
static constexpr float INF = std::numeric_limits<float>::infinity();
|
||
|
|
||
|
static constexpr float PI = std::numbers::pi_v<float>;
|
||
|
static constexpr float HALF_PI = PI * 0.5f;
|
||
|
static constexpr float TWO_PI = 2.0f * PI;
|
||
|
|
||
|
/*---------------------------------------------------------------------------------*/
|
||
|
/* Static Function Members */
|
||
|
/*---------------------------------------------------------------------------------*/
|
||
|
|
||
|
static void Initialise ();
|
||
|
|
||
|
template <IsArithmetic T>
|
||
|
[[nodiscard]] static constexpr T DegreesToRadians (T angleInDeg);
|
||
|
|
||
|
template <IsArithmetic T>
|
||
|
[[nodiscard]] static constexpr T RadiansToDegrees (T angleInRad);
|
||
|
|
||
|
template <IsArithmetic T>
|
||
|
[[nodiscard]] static T Lerp (T a, T b, T alpha);
|
||
|
|
||
|
template <IsArithmetic T>
|
||
|
[[nodiscard]] static T ClampedLerp (T a, T b, T alpha, T alphaMin, T alphaMax);
|
||
|
|
||
|
template <IsArithmetic T>
|
||
|
[[nodiscard]] static T Wrap (T value, T min, T max);
|
||
|
|
||
|
template <IsArithmetic T = float>
|
||
|
[[nodiscard]] static T GenerateRandomNumber (T lowerBound = 0, T upperBound = 1);
|
||
|
|
||
|
/**
|
||
|
* @brief Compares two floating-point values for equality within given tolerances.
|
||
|
* @tparam T A floating-point type
|
||
|
* @param lhs A floating-point value.
|
||
|
* @param rhs A floating-point value.
|
||
|
* @param absTolerance The absolute tolerance to compare the values for equality.
|
||
|
* @param relTolerance The relative tolerance for comparing large values for equality.
|
||
|
* @returns True if the values are equal within the specified tolerances.
|
||
|
*/
|
||
|
template <IsFloatingPoint T = float>
|
||
|
[[nodiscard]] static bool CompareFloat (T lhs, T rhs, T absTolerance = EPSILON, T relTolerance = EPSILON);
|
||
|
|
||
|
private:
|
||
|
/*---------------------------------------------------------------------------------*/
|
||
|
/* Static Data Members */
|
||
|
/*---------------------------------------------------------------------------------*/
|
||
|
static std::default_random_engine rng;
|
||
|
};
|
||
|
|
||
|
} // namespace SHADE
|
||
|
|
||
|
#include "SHMathHelpers.hpp"
|