#ifndef TimeUtils_DEFINED
#define TimeUtils_DEFINED
#include "include/core/SkTypes.h"
#include "include/private/base/SkFloatingPoint.h"
#include <climits>
#include <cmath>
namespace TimeUtils {
using MSec = uint32_t;
static constexpr MSec MSecMax = INT32_MAX;
static inline MSec NanosToMSec(double nanos) {
const double msec = nanos * 1e-6;
SkASSERT(MSecMax >= msec);
return static_cast<MSec>(msec);
}
static inline double NanosToSeconds(double nanos) {
return nanos * 1e-9;
}
static inline float Scaled(float time, float speed, float period = 0) {
double value = time * speed;
if (period) {
value = ::fmod(value, (double)(period));
}
return (float)value;
}
static inline float PingPong(double time,
float period,
float phase,
float ends,
float mid) {
double value = ::fmod(time + phase, period);
double half = period / 2.0;
double diff = ::fabs(value - half);
return (float)(ends + (1.0 - diff / half) * (mid - ends));
}
static inline float SineWave(double time,
float periodInSecs,
float phaseInSecs,
float min,
float max) {
if (periodInSecs < 0.f) {
return (min + max) / 2.f;
}
double t = NanosToSeconds(time) + phaseInSecs;
t *= 2 * SK_FloatPI / periodInSecs;
float halfAmplitude = (max - min) / 2.f;
return halfAmplitude * std::sin(t) + halfAmplitude + min;
}
}
#endif