PICCANTE  0.4
The hottest HDR imaging library!
math.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_UTIL_MATH_HPP
19 #define PIC_UTIL_MATH_HPP
20 
21 #include <math.h>
22 #include <cmath>
23 #include <random>
24 #include <stdlib.h>
25 #include <set>
26 #include <limits>
27 
28 #include "../base.hpp"
29 
30 namespace pic {
31 
32 //Natural logarithm of 2
33 const float C_LOG_NAT_2 = 0.69314718055994530941723212145818f;
34 
35 //Reciprocal natural logarithm of 2
36 const float C_INV_LOG_NAT_2 = 1.4426950408889634073599246810019f;
37 const double C_INV_LOG_NAT_2_D = 1.4426950408889634073599246810019;
38 
39 //Epsilon
40 const float C_EPSILON = 1e-6f;
41 
42 //Square root of 2
43 const float C_SQRT_2 = 1.4142135623730950488016887242097f;
44 
45 //PI/4
46 const float C_PI_025 = 0.78539816339744830961566084581988f;
47 //PI/2
48 const float C_PI_05 = 1.5707963267948966192313216916398f;
49 //PI
50 const float C_PI = 3.1415926535897932384626433832795f;
51 //(PI *2)
52 const float C_PI_2 = 6.283185307179586476925286766559f;
53 // 1 / (PI *2)
54 const float C_INV_PI_2 = 0.159154943091895335768883763f;
55 //PI times 4
56 const float C_PI_4 = 12.566370614359172953850573533118f;
57 //One over PI times 4
58 const float C_INV_PI_4 = 0.07957747154594766788444188168626f;
59 //PI*PI*2
60 const float C_PI_2_2 = 19.739208802178717237668981999752f;
61 // 1/PI
62 const float C_INV_PI = 0.31830988618379067153776526745f;
63 // 180/PI
64 const float C_ONE_80_OVER_PI = 57.295779513082320876798154814105f;
65 // PI/180
66 const float C_PI_OVER_ONE_80 = 0.017453292519943295769236907685f;
67 
68 #ifndef MIN
69  #define MIN(a, b) (a < b ? a : b)
70 #endif
71 
72 #ifndef MAX
73  #define MAX(a, b) (a > b ? a : b)
74 #endif
75 
76 #ifndef CLAMP
77  #define CLAMP(x, a) (x >= a ? (a - 1) : (x < 0 ? 0 : x))
78 #endif
79 
80 #ifndef CLAMPi
81  #define CLAMPi(x, a, b) (x < a ? a : (x > b ? b : x))
82 #endif
83 
84 
85 #ifndef isnan
86 
92 template< typename T >
93 PIC_INLINE bool isnan(T value)
94 {
95  return value != value ;
96 }
97 
103 template< typename T > PIC_INLINE bool isinf(T value)
104 {
105  return std::numeric_limits<T>::has_infinity &&
106  (value == std::numeric_limits<T>::infinity() ||
107  value == -std::numeric_limits<T>::infinity());
108 }
109 
110 #endif
111 
119 PIC_INLINE bool equalf(float a, float b)
120 {
121  return ( fabsf(a - b) < C_EPSILON);
122 }
123 
129 {
130  return float(rand() % RAND_MAX) / float(RAND_MAX);
131 }
132 
138 PIC_INLINE float getRandom(unsigned int n)
139 {
140  return float(n) / 4294967295.0f;
141 }
142 
150 PIC_INLINE int getRandomInt(int n, int a, int b)
151 {
152  if(a < b) {
153  return n % (b - a);
154  } else {
155  return 0;
156  }
157 }
158 
164 PIC_INLINE float sFunction(float x)
165 {
166  float x2 = x * x;
167  return 3.0f * x2 - 2.0f * x2 * x;
168 }
169 
175 PIC_INLINE float sCurve5(float x)
176 {
177  float x2 = x * x;
178  float x4 = x2 * x2;
179 
180  return (6.0f * x - 15.0f) * x4 + 10.0f * x2 * x;
181 }
182 
188 PIC_INLINE float square(float x)
189 {
190  return x * x;
191 }
192 
198 PIC_INLINE float sqrtf_s(float x)
199 {
200  return sqrtf(MAX(x, 0.0f));
201 }
202 
210 template< class T >
211 PIC_INLINE T Clamp(T x, T a, T b)
212 {
213  if(x > b) {
214  return b;
215  }
216 
217  if(x < a) {
218  return a;
219  }
220 
221  return x;
222 }
223 
229 PIC_INLINE long lround(double x)
230 {
231  if(x > 0.0) {
232  return (x - floor(x) < 0.5) ? (long)floor(x) : (long)ceil(x);
233  } else {
234  return (x - floor(x) <= 0.5) ? (long)floor(x) : (long)ceil(x);
235  }
236 }
237 
243 PIC_INLINE float lround(float x)
244 {
245  if(x > 0.0f) {
246  return (x - floorf(x) < 0.5f) ? floorf(x) : ceilf(x);
247  } else {
248  return (x - floorf(x) <= 0.5f) ? floorf(x) : ceilf(x);
249  }
250 }
251 
259 PIC_INLINE float lerp(float t, float x0, float x1)
260 {
261  return x0 + t * (x1 - x0);
262 }
263 
271 PIC_INLINE float SmoothStep(float a, float b, float value)
272 {
273  float x = Clamp<float>((value - a) / (b - a), 0.0f, 1.0f);
274  return x * x * (-2.0f * x + 3.0f);
275 }
276 
282 inline float Deg2Rad(float deg)
283 {
284  return deg * C_PI_OVER_ONE_80;
285 }
286 
292 PIC_INLINE float Rad2Deg(float rad)
293 {
294  return rad * C_ONE_80_OVER_PI;
295 }
296 
302 PIC_INLINE int log2(int n)
303 {
304  int val = 1;
305  int lg = 0;
306 
307  while(val < n) {
308  val = val << 1;
309  lg++;
310  }
311 
312  if(val != n) {
313  lg--;
314  }
315 
316  return lg;
317 }
318 
324 PIC_INLINE int pow2(int n)
325 {
326  return 1 << n;
327 }
328 
334 PIC_INLINE float log10PlusOne(float x)
335 {
336  return log10f(x + 1.0f);
337 }
338 
344 PIC_INLINE float expfMinusOne(float x)
345 {
346  float tmp = powf(10.0f, x) - 1.0f;
347  return MAX(tmp, 0.0f);
348 }
349 
356 {
357  return log10f(x + 1e-7f);
358 }
359 
366 {
367  return MAX(powf(10.0f, x) - 1e-7f, 0.0f);
368 }
369 
375 PIC_INLINE float log2f(float x)
376 {
377  return logf(x) * C_INV_LOG_NAT_2;
378 }
379 
385 PIC_INLINE double log2(double x)
386 {
387  return log(x) * C_INV_LOG_NAT_2_D;
388 }
389 
396 {
397  return logf(x + 1e-6f) * C_INV_LOG_NAT_2;
398 }
399 
405 PIC_INLINE float pow2f(float x)
406 {
407  return powf(2.0f, x);
408 }
409 
416 PIC_INLINE int powint(int x, int b)
417 {
418  int ret = 1;
419 
420  for(int i = 0; i < b; i++) {
421  ret *= x;
422  }
423 
424  return ret;
425 }
426 
434 PIC_INLINE void getRandomPermutation(std::mt19937 &m, unsigned int *perm, unsigned int nPerm, unsigned int n)
435 {
436  std::set< unsigned int > checker;
437 
438  unsigned int tmp = m() % n;
439  checker.insert(tmp);
440  perm[0] = tmp;
441  unsigned int index = 1;
442 
443  while(index < nPerm) {
444  tmp = m() % n;
445 
446  if(checker.find(tmp) == checker.end()) {
447  perm[index] = tmp;
448  index++;
449  }
450  }
451 }
452 
460 PIC_INLINE float normalDistribution(float x, float mu = 0.0f, float sigma = 1.0f)
461 {
462  float ret;
463 
464  float sigma_sq_2 = sigma * sigma * 2.0f;
465  float d = x - mu;
466  ret = exp(-(d * d) / sigma_sq_2) / sqrtf(sigma_sq_2 * C_PI);
467 
468  return ret;
469 }
470 
478 float normalCDF(float x, float mu, float sigma)
479 {
480  float t = (x - mu) / (sigma * C_SQRT_2);
481  return (1.0f + std::erf(t)) * 0.5f;
482 }
483 
490 PIC_INLINE float betaFunction(float A, float B, float step = 1e-4)
491 {
492  if(step <= 0.0f || step >= 1.0f) {
493  step = 1e-4f;
494  }
495 
496  float A1 = A - 1.0f;
497  float B1 = B - 1.0f;
498 
499  float ret = 0.0f;
500 
501  int tot = 0;
502  for(float x = 0.0f; x <= 1.0f; x += step) {
503  ret += powf(x, A1) * powf(1.0f - x, B1);
504  tot++;
505  }
506 
507  return ret / float(tot);
508 }
509 
518 PIC_INLINE float betaPDFwithBeta(float x, float A, float B, float betaAB)
519 {
520  if(x < 0.0f || x > 1.0f) {
521  return -1.0f;
522  }
523 
524  float ret = powf(x, A - 1.0f) * powf(1.0f - x, B - 1.0f);
525 
526  return ret / betaAB;
527 }
528 
536 PIC_INLINE float betaPDF(float x, float A, float B)
537 {
538  return betaPDFwithBeta(x, A, B, betaFunction(A, B));
539 }
540 
546 PIC_INLINE float sigmoid(float x)
547 {
548  return x / (x + 1.0f);
549 }
550 
556 PIC_INLINE float sigmoidInv(float x)
557 {
558  return x / (1.0f - x);
559 }
560 
567 {
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));
571 }
572 
573 } // end namespace pic
574 
575 #endif
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