18 #ifndef PIC_UTIL_MATH_HPP 19 #define PIC_UTIL_MATH_HPP 28 #include "../base.hpp" 33 const float C_LOG_NAT_2 = 0.69314718055994530941723212145818f;
43 const float C_SQRT_2 = 1.4142135623730950488016887242097f;
46 const float C_PI_025 = 0.78539816339744830961566084581988f;
48 const float C_PI_05 = 1.5707963267948966192313216916398f;
50 const float C_PI = 3.1415926535897932384626433832795f;
52 const float C_PI_2 = 6.283185307179586476925286766559f;
56 const float C_PI_4 = 12.566370614359172953850573533118f;
58 const float C_INV_PI_4 = 0.07957747154594766788444188168626f;
60 const float C_PI_2_2 = 19.739208802178717237668981999752f;
62 const float C_INV_PI = 0.31830988618379067153776526745f;
69 #define MIN(a, b) (a < b ? a : b) 73 #define MAX(a, b) (a > b ? a : b) 77 #define CLAMP(x, a) (x >= a ? (a - 1) : (x < 0 ? 0 : x)) 81 #define CLAMPi(x, a, b) (x < a ? a : (x > b ? b : x)) 92 template<
typename T >
95 return value != value ;
105 return std::numeric_limits<T>::has_infinity &&
106 (value == std::numeric_limits<T>::infinity() ||
107 value == -std::numeric_limits<T>::infinity());
130 return float(rand() % RAND_MAX) / float(RAND_MAX);
140 return float(n) / 4294967295.0f;
167 return 3.0f * x2 - 2.0f * x2 * x;
180 return (6.0f * x - 15.0f) * x4 + 10.0f * x2 * x;
200 return sqrtf(
MAX(x, 0.0f));
232 return (x - floor(x) < 0.5) ? (long)floor(x) : (long)ceil(x);
234 return (x - floor(x) <= 0.5) ? (long)floor(x) : (long)ceil(x);
246 return (x - floorf(x) < 0.5f) ? floorf(x) : ceilf(x);
248 return (x - floorf(x) <= 0.5f) ? floorf(x) : ceilf(x);
261 return x0 + t * (x1 - x0);
273 float x = Clamp<float>((value - a) / (b - a), 0.0f, 1.0f);
274 return x * x * (-2.0f * x + 3.0f);
336 return log10f(x + 1.0f);
346 float tmp = powf(10.0f, x) - 1.0f;
347 return MAX(tmp, 0.0f);
357 return log10f(x + 1e-7f);
367 return MAX(powf(10.0f, x) - 1e-7f, 0.0f);
407 return powf(2.0f, x);
420 for(
int i = 0; i < b; i++) {
436 std::set< unsigned int > checker;
438 unsigned int tmp = m() % n;
441 unsigned int index = 1;
443 while(index < nPerm) {
446 if(checker.find(tmp) == checker.end()) {
464 float sigma_sq_2 = sigma * sigma * 2.0f;
466 ret = exp(-(d * d) / sigma_sq_2) / sqrtf(sigma_sq_2 *
C_PI);
480 float t = (x - mu) / (sigma *
C_SQRT_2);
481 return (1.0f + std::erf(t)) * 0.5f;
492 if(step <= 0.0f || step >= 1.0f) {
502 for(
float x = 0.0f; x <= 1.0f; x += step) {
503 ret += powf(x, A1) * powf(1.0f - x, B1);
507 return ret / float(tot);
520 if(x < 0.0f || x > 1.0f) {
524 float ret = powf(x, A - 1.0f) * powf(1.0f - x, B - 1.0f);
548 return x / (x + 1.0f);
558 return x / (1.0f - x);
568 float t0 = powf(x, 1.0f / 2.2f) * 255.0f;
569 float t1 =
CLAMPi(t0, 0.0f, 255.0f);
570 return float(
int(t1));
PIC_INLINE float getRandombase()
Randombase returns a number in [0, 1] based on rand().
Definition: math.hpp:128
PIC_INLINE float sFunction(float x)
sFunction evaluates a cubic s-function.
Definition: math.hpp:164
PIC_INLINE float SmoothStep(float a, float b, float value)
SmoothStep smoothes a value from a to b using a cube S-Shape.
Definition: math.hpp:271
const float C_INV_PI_4
Definition: math.hpp:58
PIC_INLINE int log2(int n)
log2 computes logarithm in base 2 for integers.
Definition: math.hpp:302
PIC_INLINE float lerp(float t, float x0, float x1)
lerp evaluates linear interpolation
Definition: math.hpp:259
const float C_PI_025
Definition: math.hpp:46
PIC_INLINE int powint(int x, int b)
powint computes power function for integer values.
Definition: math.hpp:416
PIC_INLINE long lround(double x)
lround rounds double numbers properly.
Definition: math.hpp:229
const float C_INV_PI_2
Definition: math.hpp:54
const float C_PI_OVER_ONE_80
Definition: math.hpp:66
PIC_INLINE float log10fPlusEpsilon(float x)
log10fPlusEpsilon
Definition: math.hpp:355
const float C_ONE_80_OVER_PI
Definition: math.hpp:64
const float C_EPSILON
Definition: math.hpp:40
const float C_PI
Definition: math.hpp:50
PIC_INLINE float betaPDFwithBeta(float x, float A, float B, float betaAB)
betaPDFwithBeta
Definition: math.hpp:518
PIC_INLINE float simple8bitWithGamma(float x)
simple8bitWithGamma
Definition: math.hpp:566
PIC_INLINE bool isinf(T value)
isinf is it a Inf value?
Definition: math.hpp:103
const float C_PI_2
Definition: math.hpp:52
PIC_INLINE int getRandomInt(int n, int a, int b)
getRandomInt
Definition: math.hpp:150
float normalCDF(float x, float mu, float sigma)
normalCDF
Definition: math.hpp:478
const float C_LOG_NAT_2
Definition: math.hpp:33
PIC_INLINE float log2fPlusEpsilon(float x)
log2fPlusEpsilon
Definition: math.hpp:395
PIC_INLINE float sCurve5(float x)
sCurve5 evaluates a quintic S-Shape: 6x^5-15x^4+10x^3
Definition: math.hpp:175
PIC_INLINE bool isnan(T value)
isnan is it a NaN?
Definition: math.hpp:93
PIC_INLINE float betaPDF(float x, float A, float B)
betaPDF
Definition: math.hpp:536
const double C_INV_LOG_NAT_2_D
Definition: math.hpp:37
PIC_INLINE float expfMinusOne(float x)
expMinusOne
Definition: math.hpp:344
PIC_INLINE T Clamp(T x, T a, T b)
Clamp clamps a value, x, in the bound [a,b].
Definition: math.hpp:211
PIC_INLINE float square(float x)
Square applies square function to a value.
Definition: math.hpp:188
#define PIC_INLINE
Definition: base.hpp:33
PIC_INLINE float powf10fMinusEpsilon(float x)
powf10fMinusEpsilon
Definition: math.hpp:365
PIC_INLINE float log2f(float x)
log2f logarithm in base 2 for floating point
Definition: math.hpp:375
const float C_SQRT_2
Definition: math.hpp:43
PIC_INLINE float pow2f(float x)
pow2f
Definition: math.hpp:405
PIC_INLINE float Rad2Deg(float rad)
Rad2Deg converts angles expressed in radians into angles expressed in degrees.
Definition: math.hpp:292
const float C_INV_LOG_NAT_2
Definition: math.hpp:36
PIC_INLINE float sqrtf_s(float x)
sqrtf_s
Definition: math.hpp:198
#define CLAMPi(x, a, b)
Definition: math.hpp:81
const float C_PI_2_2
Definition: math.hpp:60
PIC_INLINE int pow2(int n)
pow2 computes 2^n.
Definition: math.hpp:324
PIC_INLINE float log10PlusOne(float x)
logf10PlusOne computes log10 of a value plus 1.
Definition: math.hpp:334
Definition: bilateral_separation.hpp:25
PIC_INLINE float sigmoid(float x)
sigmoid
Definition: math.hpp:546
PIC_INLINE float getRandom(unsigned int n)
Random returns a number in [0, 2^32 - 1] to a float in [0, 1].
Definition: math.hpp:138
#define MAX(a, b)
Definition: math.hpp:73
float Deg2Rad(float deg)
Deg2Rad converts angles expressed in degrees into angles expressed in radians.
Definition: math.hpp:282
PIC_INLINE bool equalf(float a, float b)
equalf checks if two float values are the same or not.
Definition: math.hpp:119
PIC_INLINE float betaFunction(float A, float B, float step=1e-4)
betaFunction
Definition: math.hpp:490
const float C_INV_PI
Definition: math.hpp:62
PIC_INLINE void getRandomPermutation(std::mt19937 &m, unsigned int *perm, unsigned int nPerm, unsigned int n)
getRandomPermutation computes a random permutation.
Definition: math.hpp:434
const float C_PI_4
Definition: math.hpp:56
PIC_INLINE float sigmoidInv(float x)
sigmoidInv
Definition: math.hpp:556
PIC_INLINE float normalDistribution(float x, float mu=0.0f, float sigma=1.0f)
normalDistribution
Definition: math.hpp:460
const float C_PI_05
Definition: math.hpp:48