mirror of
https://github.com/jaredtao/TaoQuick.git
synced 2025-01-31 21:22:58 +08:00
add MathHelp
This commit is contained in:
parent
e2f2105847
commit
8851d9f8fd
39
3rdparty/TaoCommon/src/TaoCommon/Common/MathHelp.h
vendored
Normal file
39
3rdparty/TaoCommon/src/TaoCommon/Common/MathHelp.h
vendored
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <cmath>
|
||||||
|
#include <QtGlobal>
|
||||||
|
#include <algorithm>
|
||||||
|
//inRange 检查 value 小于等于 max, 大于等于min
|
||||||
|
|
||||||
|
//inRange<T>通用模版函数
|
||||||
|
template<typename T>
|
||||||
|
static bool inRange(const T &value, const T &min, const T &max)
|
||||||
|
{
|
||||||
|
if (min <= value && value <= max) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//inRange<double> 模版偏特化,遇到double时,使用std的浮点数比较代替 常规比较。规避精度误差
|
||||||
|
template<>
|
||||||
|
static bool inRange<double>(const double &value, const double &min, const double &max)
|
||||||
|
{
|
||||||
|
if (std::isgreaterequal(value, min) && std::islessequal(value, max)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//inRange<float> 模版偏特化,遇到float时,使用std的浮点数比较代替 常规比较。规避精度误差
|
||||||
|
template<>
|
||||||
|
static bool inRange<float>(const float &value, const float &min, const float &max)
|
||||||
|
{
|
||||||
|
if (std::isgreaterequal(value, min) && std::islessequal(value, max)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//clamp, 限制 value在 [min - Max]区间.
|
||||||
|
template<typename T>
|
||||||
|
static T clamp(const T &value, const T &min, const T &max)
|
||||||
|
{
|
||||||
|
return std::max(min,std::min(value, max));
|
||||||
|
}
|
@ -11,6 +11,7 @@ HEADERS += \
|
|||||||
$$PWD/Common/Package.h \
|
$$PWD/Common/Package.h \
|
||||||
$$PWD/Common/PropertyHelper.h \
|
$$PWD/Common/PropertyHelper.h \
|
||||||
$$PWD/Common/Subject.h \
|
$$PWD/Common/Subject.h \
|
||||||
|
$$PWD/Common/MathHelp.h \
|
||||||
$$PWD/Frameless/TaoFrameLessView.h \
|
$$PWD/Frameless/TaoFrameLessView.h \
|
||||||
$$PWD/Logger/Logger.h \
|
$$PWD/Logger/Logger.h \
|
||||||
$$PWD/Logger/LoggerTemplate.h \
|
$$PWD/Logger/LoggerTemplate.h \
|
||||||
|
Loading…
x
Reference in New Issue
Block a user