common/math: revise clamp() aligning with the std style

This commit is contained in:
Hermet Park 2025-01-07 18:31:41 +09:00 committed by Hermet Park
parent 2a6e3ca40a
commit da54c83b5f
2 changed files with 10 additions and 17 deletions

View file

@ -69,10 +69,9 @@ static inline bool equal(float a, float b)
template <typename T> template <typename T>
static inline void clamp(T& v, const T& min, const T& max) static inline constexpr const T& clamp(const T& v, const T& min, const T& max)
{ {
if (v < min) v = min; return std::min(std::max(v, min), max);
else if (v > max) v = max;
} }
/************************************************************************/ /************************************************************************/

View file

@ -156,27 +156,23 @@ float LottieTextRange::factor(float frameNo, float totalLen, float idx)
break; break;
} }
case Round: { case Round: {
idx += 0.5f - start; idx = tvg::clamp(idx + (0.5f - start), 0.0f, end - start);
clamp(idx, 0.0f, end - start);
auto range = 0.5f * (end - start); auto range = 0.5f * (end - start);
auto t = idx - range; auto t = idx - range;
f = tvg::equal(start, end) ? 0.0f : sqrtf(1.0f - t * t / (range * range)); f = tvg::equal(start, end) ? 0.0f : sqrtf(1.0f - t * t / (range * range));
break; break;
} }
case Smooth: { case Smooth: {
idx += 0.5f - start; idx = tvg::clamp(idx + (0.5f - start), 0.0f, end - start);
clamp(idx, 0.0f, end - start);
f = tvg::equal(start, end) ? 0.0f : 0.5f * (1.0f + cosf(MATH_PI * (1.0f + 2.0f * idx / (end - start)))); f = tvg::equal(start, end) ? 0.0f : 0.5f * (1.0f + cosf(MATH_PI * (1.0f + 2.0f * idx / (end - start))));
break; break;
} }
} }
clamp(f, 0.0f, 1.0f); f = tvg::clamp(f, 0.0f, 1.0f);
//apply easing //apply easing
auto minEase = this->minEase(frameNo); auto minEase = tvg::clamp(this->minEase(frameNo), -100.0f, 100.0f);
clamp(minEase, -100.0f, 100.0f); auto maxEase = tvg::clamp(this->maxEase(frameNo), -100.0f, 100.0f);
auto maxEase = this->maxEase(frameNo);
clamp(maxEase, -100.0f, 100.0f);
if (!tvg::zero(minEase) || !tvg::zero(maxEase)) { if (!tvg::zero(minEase) || !tvg::zero(maxEase)) {
Point in{1.0f, 1.0f}; Point in{1.0f, 1.0f};
Point out{0.0f, 0.0f}; Point out{0.0f, 0.0f};
@ -189,7 +185,7 @@ float LottieTextRange::factor(float frameNo, float totalLen, float idx)
interpolator->set(nullptr, in, out); interpolator->set(nullptr, in, out);
f = interpolator->progress(f); f = interpolator->progress(f);
} }
clamp(f, 0.0f, 1.0f); f = tvg::clamp(f, 0.0f, 1.0f);
return f * this->maxAmount(frameNo) * 0.01f; return f * this->maxAmount(frameNo) * 0.01f;
} }
@ -231,10 +227,8 @@ void LottieImage::update()
void LottieTrimpath::segment(float frameNo, float& start, float& end, LottieExpressions* exps) void LottieTrimpath::segment(float frameNo, float& start, float& end, LottieExpressions* exps)
{ {
start = this->start(frameNo, exps) * 0.01f; start = tvg::clamp(this->start(frameNo, exps) * 0.01f, 0.0f, 1.0f);
tvg::clamp(start, 0.0f, 1.0f); end = tvg::clamp(this->end(frameNo, exps) * 0.01f, 0.0f, 1.0f);
end = this->end(frameNo, exps) * 0.01f;
tvg::clamp(end, 0.0f, 1.0f);
auto o = fmodf(this->offset(frameNo, exps), 360.0f) / 360.0f; //0 ~ 1 auto o = fmodf(this->offset(frameNo, exps), 360.0f) / 360.0f; //0 ~ 1