common.h

#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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值