JD2022-TU1/main/extern/dtest/dtcore/inc/dtmath.h

63 lines
1.8 KiB
C++

#pragma once
#include "dtcore/inc/dtdefinitions.h"
#include "dtcore/inc/dtconstant.h"
#include "dtcore/inc/dterror.h"
namespace dt
{
class Math
{
public:
template<typename T> static const T& Min(const T& lhs, const T& rhs);
template<typename T> static const T& Max(const T& lhs, const T& rhs);
template<typename T> static const T& Clamp(const T& v, const T& lo, const T& hi);
DT_CORE_DLL_ENTRY static void InitializeRandomSeed(U32 seed);
DT_CORE_DLL_ENTRY static int RandomRange();
DT_CORE_DLL_ENTRY static int Random();
DT_CORE_DLL_ENTRY static float UnitRandom();
DT_CORE_DLL_ENTRY static float WrapRandom();
DT_CORE_DLL_ENTRY static float IntervalRandom(float high, float low = 0.0f);
template<typename T> static void Swap(T& lhs, T& rhs);
template<typename T, typename U> static U Narrow(const T& rhs);
};
//////////////////////////////////////////////////////////////////////////
template<class T> inline const T& Math::Min(const T& lhs, const T& rhs)
{
return lhs < rhs ? lhs : rhs;
}
template<class T> inline const T& Math::Max(const T& lhs, const T& rhs)
{
return lhs > rhs ? lhs : rhs;
}
template<class T> inline const T& Math::Clamp(const T& v, const T& lo, const T& hi)
{
return (v < lo) ? lo : (hi < v) ? hi : v;
}
template<class T> inline void Math::Swap(T& lhs, T& rhs)
{
T temp = lhs;
lhs = rhs;
rhs = temp;
}
template<class T, class U> inline U Math::Narrow(const T& rhs)
{
U result = static_cast<U>(rhs);
#pragma warning(push)
#pragma warning(disable: 4389) // signed/unsigned mismatch
DT_ASSERT(result == rhs);
#pragma warning(pop)
return result;
}
}