#pragma once #include "dtcore/inc/dtdefinitions.h" #include "dtcore/inc/dtconstant.h" #include "dtcore/inc/dterror.h" namespace dt { class Math { public: template static const T& Min(const T& lhs, const T& rhs); template static const T& Max(const T& lhs, const T& rhs); template 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 static void Swap(T& lhs, T& rhs); template static U Narrow(const T& rhs); }; ////////////////////////////////////////////////////////////////////////// template inline const T& Math::Min(const T& lhs, const T& rhs) { return lhs < rhs ? lhs : rhs; } template inline const T& Math::Max(const T& lhs, const T& rhs) { return lhs > rhs ? lhs : rhs; } template inline const T& Math::Clamp(const T& v, const T& lo, const T& hi) { return (v < lo) ? lo : (hi < v) ? hi : v; } template inline void Math::Swap(T& lhs, T& rhs) { T temp = lhs; lhs = rhs; rhs = temp; } template inline U Math::Narrow(const T& rhs) { U result = static_cast(rhs); #pragma warning(push) #pragma warning(disable: 4389) // signed/unsigned mismatch DT_ASSERT(result == rhs); #pragma warning(pop) return result; } }