#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <assert.h>
#include <cmath>
class Vector2;
class Vector3;
class Vector4;
class Matrix;
typedef unsigned char uchar;
typedef unsigned int uint;
//常用常量
static const float PI = 3.14159265358979323846f;
static const float InvPI = 0.31830988618379067154f;
static const float Inv2PI = 0.15915494309189533577f;
static const float Inv4PI = 0.07957747154594766788f;
static const float PIOver2 = 1.57079632679489661923f;
static const float PIOver4 = 0.78539816339744830961f;
static const float Sqrt2 = 1.41421356237309504880f;
static const float Deg2Rad = PI / 180.0f;
static const float Rad2Deg = 180.0f / PI;
static const float EPSILON = FLT_EPSILON;
static const float UCHAR2Float01 = 1.0f / 255;
//三角函数
inline float Sin(const float rad){ return std::sin(rad); };
inline float Asin(const float val){ return std::asin(val); };
inline float Cos(const float rad){ return std::cos(rad); };
inline float Acos(const float val){ return std::acos(val); };
inline float Tan(const float rad){ return std::tan(rad); };
inline float Atan(const float val){ return std::atan(val); };
inline float Sqrt(const float val){ return std::sqrt(val); };
inline float Pow(const float val, const float n){ return std::pow(val, n); };
inline float Log2(const float val){ return std::log2f(val); };
inline int Abs(const int val){ return std::abs(val); };
inline float Abs(const float val){ return std::abs(val); };
inline float Floor(const float val){ return std::floor(val); };
inline float Ceil(const float val){ return std::ceil(val); };
//取大值
inline float Max(const float val0, const float val1)
{
if (val0 < val1)
{
return val1;
}
else
{
return val0;
}
}
//取小值
inline float Min(const float val0, const float val1)
{
if (val0 < val1)
{
return val0;
}
else
{
return val1;
}
}
//夹具函数
inline float Clamp(const float val, const float min, const float max)
{
if (val < min)
{
return min;
}
else if (val > max)
{
return max;
}
else
{
return val;
}
}
//夹具函数
inline int Clamp(const int val, const int min, const int max)
{
if (val < min)
{
return min;
}
else if (val > max)
{
return max;
}
else
{
return val;
}
}
//夹具函数
inline int Clamp(const uchar val, const uchar min, const uchar max)
{
uint tmp = static_cast<uint>(val);
if (tmp < min)
{
return min;
}
else if (tmp > max)
{
return max;
}
else
{
return static_cast<uchar>(val);
}
}
//饱和函数
inline float Saturate(const float val)
{
return Clamp(val, 0.0f, 1.0f);
}
//线性插值函数v = v1 + (v2 -v1) * t
inline float Lerp(const float v1,const float v2,const float t){ return v1 + t * (v2 - v1); };
//将角度转成弧度
inline float Radians(const float deg){ return Deg2Rad * deg; };
//将弧度转成角度
inline float Degrees(const float rad){ return Rad2Deg * rad; };
/**
*判断一个单精度浮点数是否等于0
*/
inline bool Equal0(const float val)
{
return (val > -EPSILON && val < EPSILON);
}
/**
* 判断两个浮点数是否相等
*/
inline bool Equal(const float val0, const float val1)
{
return Equal0(val0 - val1);
}
/**
* 判断一个数值是否是NaN
*/
template <typename T>
inline bool IsNaN(const T &val)
{
return std::isnan(val);
}
/**
* 整数不会出现NaN
*/
template <>
inline bool IsNaN(const int &val)
{
return false;
}