2023-01-04 10:25:21 +01:00
|
|
|
#pragma once
|
2021-06-17 23:21:29 -04:00
|
|
|
#include <cmath>
|
2016-07-17 23:37:24 -04:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2022-08-22 15:54:04 +02:00
|
|
|
#include <cstddef>
|
2012-07-27 20:06:17 +01:00
|
|
|
#include <vector>
|
2012-01-08 17:39:03 +00:00
|
|
|
|
2023-06-02 06:44:23 +02:00
|
|
|
template<class Signed>
|
|
|
|
inline std::pair<Signed, Signed> floorDiv(Signed a, Signed b)
|
|
|
|
{
|
|
|
|
auto quo = a / b;
|
|
|
|
auto rem = a % b;
|
|
|
|
if (a < Signed(0) && rem)
|
|
|
|
{
|
|
|
|
quo -= Signed(1);
|
|
|
|
rem += b;
|
|
|
|
}
|
|
|
|
return { quo, rem };
|
|
|
|
}
|
|
|
|
|
2023-06-10 12:28:30 +02:00
|
|
|
template<class Signed>
|
|
|
|
inline std::pair<Signed, Signed> ceilDiv(Signed a, Signed b)
|
|
|
|
{
|
|
|
|
return floorDiv(a + b - Signed(1), b);
|
|
|
|
}
|
|
|
|
|
2012-11-18 22:24:56 +00:00
|
|
|
//Linear interpolation
|
|
|
|
template <typename T> inline T LinearInterpolate(T val1, T val2, T lowerCoord, T upperCoord, T coord)
|
|
|
|
{
|
|
|
|
if(lowerCoord == upperCoord) return val1;
|
|
|
|
return (((val2 - val1) / (upperCoord - lowerCoord)) * (coord - lowerCoord)) + val1;
|
|
|
|
}
|
|
|
|
|
2021-02-15 21:11:10 +01:00
|
|
|
|
2012-01-08 17:39:03 +00:00
|
|
|
//Signum function
|
2021-02-15 21:11:10 +01:00
|
|
|
inline int isign(int i)
|
|
|
|
{
|
|
|
|
if (i<0)
|
|
|
|
return -1;
|
|
|
|
if (i>0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int isign(float i)
|
|
|
|
{
|
|
|
|
if (i<0)
|
|
|
|
return -1;
|
|
|
|
if (i>0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-08 17:39:03 +00:00
|
|
|
|
2024-02-06 14:14:00 +01:00
|
|
|
inline int iabs(int i)
|
|
|
|
{
|
|
|
|
return i * isign(i);
|
|
|
|
}
|
|
|
|
|
2021-02-15 21:11:10 +01:00
|
|
|
inline unsigned clamp_flt(float f, float min, float max)
|
|
|
|
{
|
|
|
|
if (f<min)
|
|
|
|
return 0;
|
|
|
|
if (f>max)
|
|
|
|
return 255;
|
|
|
|
return (int)(255.0f*(f-min)/(max-min));
|
|
|
|
}
|
2012-01-08 17:39:03 +00:00
|
|
|
|
2021-02-15 21:11:10 +01:00
|
|
|
inline float restrict_flt(float f, float min, float max)
|
|
|
|
{
|
2021-06-17 23:21:29 -04:00
|
|
|
// Fix crash in certain cases when f is nan
|
|
|
|
if (!std::isfinite(f))
|
2021-02-15 21:11:10 +01:00
|
|
|
return min;
|
2021-06-17 23:21:29 -04:00
|
|
|
if (f < min)
|
|
|
|
return min;
|
|
|
|
if (f > max)
|
2021-02-15 21:11:10 +01:00
|
|
|
return max;
|
|
|
|
return f;
|
|
|
|
}
|
2012-01-08 17:39:03 +00:00
|
|
|
|
|
|
|
void HSV_to_RGB(int h,int s,int v,int *r,int *g,int *b);
|
|
|
|
void RGB_to_HSV(int r,int g,int b,int *h,int *s,int *v);
|
2013-08-22 19:50:20 +04:00
|
|
|
|
2022-08-22 15:54:04 +02:00
|
|
|
class ByteString;
|
|
|
|
|
|
|
|
bool byteStringEqualsString(const ByteString &str, const char *data, size_t size);
|
|
|
|
template<size_t N>
|
|
|
|
// TODO: use std::literals::string_literals::operator""s if we get rid of ByteString
|
|
|
|
bool byteStringEqualsLiteral(const ByteString &str, const char (&lit)[N])
|
|
|
|
{
|
|
|
|
return byteStringEqualsString(str, lit, N - 1U);
|
|
|
|
}
|