OpenImageIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Macros | Functions | Variables
fmath.h File Reference
#include <cmath>
#include <limits>
#include <typeinfo>
#include <algorithm>
#include <stdint.h>
#include "version.h"

Go to the source code of this file.

Classes

struct  is_same< T, U >
 
struct  is_same< T, T >
 
struct  DataProxy< I, E >
 
struct  ConstDataProxy< I, E >
 
struct  DataArrayProxy< I, E >
 
struct  ConstDataArrayProxy< I, E >
 

Macros

#define M_PI   3.1415926535897932
 
#define M_PI_2   1.5707963267948966
 
#define M_TWO_PI   (M_PI * 2.0)
 
#define M_1_PI   0.318309886183790671538
 
#define M_2_PI   0.63661977236758134
 
#define M_SQRT2   1.414135623730950
 
#define M_SQRT1_2   0.7071067811865475
 
#define M_LN2   0.6931471805599453
 
#define M_LN10   2.3025850929940457
 
#define HUGE_FLOAT   ((float)1.0e38)
 
#define UNINITIALIZED_FLOAT   (- std::numeric_limits<float>::max())
 

Functions

template<class T >
clamp (T a, T l, T h)
 
uint32_t clamped_mult32 (uint32_t a, uint32_t b)
 
uint64_t clamped_mult64 (uint64_t a, uint64_t b)
 
template<typename S , typename D , typename F >
scaled_conversion (const S &src, F scale, F min, F max)
 
template<typename S , typename D >
void convert_type (const S *src, D *dst, size_t n, D _zero=0, D _one=1, D _min=std::numeric_limits< D >::min(), D _max=std::numeric_limits< D >::max())
 
template<typename S , typename D >
convert_type (const S &src)
 
template<unsigned int FROM_BITS, unsigned int TO_BITS>
unsigned int bit_range_convert (unsigned int in)
 
unsigned int bit_range_convert (unsigned int in, unsigned int FROM_BITS, unsigned int TO_BITS)
 
template<class T , class Q >
lerp (T v0, T v1, Q x)
 
template<class T , class Q >
bilerp (T v0, T v1, T v2, T v3, Q s, Q t)
 
template<class T , class Q >
void bilerp (const T *v0, const T *v1, const T *v2, const T *v3, Q s, Q t, int n, T *result)
 
template<class T , class Q >
void bilerp_mad (const T *v0, const T *v1, const T *v2, const T *v3, Q s, Q t, Q scale, int n, T *result)
 
template<class T , class Q >
trilerp (T v0, T v1, T v2, T v3, T v4, T v5, T v6, T v7, Q s, Q t, Q r)
 
template<class T , class Q >
void trilerp (const T *v0, const T *v1, const T *v2, const T *v3, const T *v4, const T *v5, const T *v6, const T *v7, Q s, Q t, Q r, int n, T *result)
 
template<class T , class Q >
void trilerp_mad (const T *v0, const T *v1, const T *v2, const T *v3, const T *v4, const T *v5, const T *v6, const T *v7, Q s, Q t, Q r, Q scale, int n, T *result)
 
int RoundToInt (double val)
 
int RoundToInt (float val)
 
int FloorToInt (double val)
 
int FloorToInt (float val)
 
int CeilToInt (double val)
 
int CeilToInt (float val)
 
int FloatToInt (double val)
 
int FloatToInt (float val)
 
float floorfrac (float x, int *xint)
 
float radians (float deg)
 
float degrees (float rad)
 
float fast_expf (float x)
 Fast float exp.
 
void float_to_rational (float f, unsigned int &num, unsigned int &den)
 
void float_to_rational (float f, int &num, int &den)
 
void sincos (float x, float *sine, float *cosine)
 
void sincos (double x, double *sine, double *cosine)
 
float safe_asinf (float x)
 
float safe_acosf (float x)
 
double safe_sqrt (double x)
 
float safe_sqrtf (float x)
 
float safe_inversesqrt (float x)
 Safe (clamping) inverse sqrt.
 
float safe_log (float x)
 
float safe_log2 (float x)
 
float safe_log10 (float x)
 
float safe_logb (float x)
 
template<class T , class Func >
invert (Func &func, T y, T xmin=0.0, T xmax=1.0, int maxiters=32, T eps=1.0e-6, bool *brack=0)
 

Variables

 OIIO_NAMESPACE_ENTER
 

Detailed Description

A variety of floating-point math helper routines (and, slight misnomer, some int stuff as well).

Function Documentation

template<class T , class Q >
T bilerp ( v0,
v1,
v2,
v3,
s,
t 
)
inline

Bilinearly interoplate values v0-v3 (v0 upper left, v1 upper right, v2 lower left, v3 lower right) at coordinates (s,t) and return the result. This is a template, and so should work for any types.

template<class T , class Q >
void bilerp ( const T *  v0,
const T *  v1,
const T *  v2,
const T *  v3,
s,
t,
int  n,
T *  result 
)
inline

Bilinearly interoplate arrays of values v0-v3 (v0 upper left, v1 upper right, v2 lower left, v3 lower right) at coordinates (s,t), storing the results in 'result'. These are all vectors, so do it for each of 'n' contiguous values (using the same s,t interpolants).

template<class T , class Q >
void bilerp_mad ( const T *  v0,
const T *  v1,
const T *  v2,
const T *  v3,
s,
t,
scale,
int  n,
T *  result 
)
inline

Bilinearly interoplate arrays of values v0-v3 (v0 upper left, v1 upper right, v2 lower left, v3 lower right) at coordinates (s,t), SCALING the interpolated value by 'scale' and then ADDING to 'result'. These are all vectors, so do it for each of 'n' contiguous values (using the same s,t interpolants).

template<unsigned int FROM_BITS, unsigned int TO_BITS>
unsigned int bit_range_convert ( unsigned int  in)
inline

Helper function to convert channel values between different bit depths. Roughly equivalent to:

out = round (in * (pow (2, TO_BITS) - 1) / (pow (2, FROM_BITS) - 1));

but utilizes an integer math trick for speed. It can be proven that the absolute error of this method is less or equal to 1, with an average error (with respect to the entire domain) below 0.2.

It is assumed that the original value is a valid FROM_BITS integer, i.e. shifted fully to the right.

int CeilToInt ( double  val)
inline

Fast (int)ceil(val) See Michael Herf's "Know Your FPU" page: http://www.stereopsis.com/sree/fpu2006.html

template<class T >
T clamp ( a,
l,
h 
)
inline

clamp a to bounds [l,h].

uint32_t clamped_mult32 ( uint32_t  a,
uint32_t  b 
)
inline

Multiply two unsigned 32-bit ints safely, carefully checking for overflow, and clamping to uint32_t's maximum value.

uint64_t clamped_mult64 ( uint64_t  a,
uint64_t  b 
)
inline

Multiply two unsigned 64-bit ints safely, carefully checking for overflow, and clamping to uint64_t's maximum value.

template<typename S , typename D >
void convert_type ( const S *  src,
D *  dst,
size_t  n,
_zero = 0,
_one = 1,
_min = std::numeric_limits<D>::min(),
_max = std::numeric_limits<D>::max() 
)

Convert n consecutive values from the type of S to the type of D. The conversion is not a simple cast, but correctly remaps the 0.0->1.0 range from and to the full positive range of integral types. Take a memcpy shortcut if both types are the same and no conversion is necessary. Optional arguments can give nonstandard quantizations.

template<typename S , typename D >
D convert_type ( const S &  src)

Convert a single value from the type of S to the type of D. The conversion is not a simple cast, but correctly remaps the 0.0->1.0 range from and to the full positive range of integral types. Take a copy shortcut if both types are the same and no conversion is necessary.

float degrees ( float  rad)
inline

Convert radians to degrees

float fast_expf ( float  x)
inline

Fast float exp.

void float_to_rational ( float  f,
unsigned int &  num,
unsigned int &  den 
)
inline

Simple conversion of a (presumably non-negative) float into a rational. This does not attempt to find the simplest fraction that approximates the float, for example 52.83 will simply return 5283/100. This does not attempt to gracefully handle floats that are out of range that could be easily int/int.

void float_to_rational ( float  f,
int &  num,
int &  den 
)
inline

Simple conversion of a float into a rational. This does not attempt to find the simplest fraction that approximates the float, for example 52.83 will simply return 5283/100. This does not attempt to gracefully handle floats that are out of range that could be easily int/int.

int FloatToInt ( double  val)
inline

Fast (int)val See Michael Herf's "Know Your FPU" page: http://www.stereopsis.com/sree/fpu2006.html

float floorfrac ( float  x,
int *  xint 
)
inline

Return (x-floor(x)) and put (int)floor(x) in *xint. This is similar to the built-in modf, but returns a true int, always rounds down (compared to modf which rounds toward 0), and always returns frac >= 0 (comapred to modf which can return <0 if x<0).

int FloorToInt ( double  val)
inline

Fast (int)floor(val) See Michael Herf's "Know Your FPU" page: http://www.stereopsis.com/sree/fpu2006.html

template<class T , class Func >
T invert ( Func &  func,
y,
xmin = 0.0,
xmax = 1.0,
int  maxiters = 32,
eps = 1.0e-6,
bool *  brack = 0 
)

Solve for the x for which func(x) == y on the interval [xmin,xmax]. Use a maximum of maxiter iterations, and stop any time the remaining search interval or the function evaluations <= eps. If brack is non-NULL, set it to true if y is in [f(xmin), f(xmax)], otherwise false (in which case the caller should know that the results may be unreliable. Results are undefined if the function is not monotonic on that interval or if there are multiple roots in the interval (it may not converge, or may converge to any of the roots without telling you that there are more than one).

template<class T , class Q >
T lerp ( v0,
v1,
x 
)
inline

Linearly interpolate values v0-v1 at x: v0*(1-x) + v1*x. This is a template, and so should work for any types.

float radians ( float  deg)
inline

Convert degrees to radians.

int RoundToInt ( double  val)
inline

Fast rounding to nearest integer. See Michael Herf's "Know Your FPU" page: http://www.stereopsis.com/sree/fpu2006.html

float safe_acosf ( float  x)
inline

Safe (clamping) arccosine.

float safe_asinf ( float  x)
inline

Safe (clamping) arcsine.

float safe_inversesqrt ( float  x)
inline

Safe (clamping) inverse sqrt.

double safe_sqrt ( double  x)
inline

Safe (clamping) sqrt.

template<typename S , typename D , typename F >
D scaled_conversion ( const S &  src,
scale,
min,
max 
)

Multiply src by scale, clamp to [min,max], and round to the nearest D (presumed to be integer). This is just a helper for the convert_type templates, it probably has no other use.

template<class T , class Q >
T trilerp ( v0,
v1,
v2,
v3,
v4,
v5,
v6,
v7,
s,
t,
r 
)
inline

Trilinearly interoplate arrays of values v0-v7 (v0 upper left top, v1 upper right top, ...) at coordinates (s,t,r), and return the result. This is a template, and so should work for any types.

template<class T , class Q >
void trilerp ( const T *  v0,
const T *  v1,
const T *  v2,
const T *  v3,
const T *  v4,
const T *  v5,
const T *  v6,
const T *  v7,
s,
t,
r,
int  n,
T *  result 
)
inline

Trilinearly interoplate arrays of values v0-v7 (v0 upper left top, v1 upper right top, ...) at coordinates (s,t,r), storing the results in 'result'. These are all vectors, so do it for each of 'n' contiguous values (using the same s,t,r interpolants).

template<class T , class Q >
void trilerp_mad ( const T *  v0,
const T *  v1,
const T *  v2,
const T *  v3,
const T *  v4,
const T *  v5,
const T *  v6,
const T *  v7,
s,
t,
r,
scale,
int  n,
T *  result 
)
inline

Trilinearly interoplate arrays of values v0-v7 (v0 upper left top, v1 upper right top, ...) at coordinates (s,t,r), SCALING the interpolated value by 'scale' and then ADDING to 'result'. These are all vectors, so do it for each of 'n' contiguous values (using the same s,t,r interpolants).