Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Utils.hh
1 #ifndef __Base_Math_Utils_hh__
2 #define __Base_Math_Utils_hh__
3 
4 #define _USE_MATH_DEFINES
5 #include <math.h>
6 #include <iostream>
7 #include <limits>
8 
9 
10 // digits used for cout,cerr console display
11 
12 #ifndef CONSOLE_PRECISION_DIGITS
13 # define CONSOLE_PRECISION_DIGITS 10
14 #endif // CONSOLE_PRECISION_DIGITS
15 
16 
17 namespace BIAS {
18 
19 
20 
21  /** @brief computes sin and cosinus with float precision
22 
23  Cg equivalent function.
24  @author Jan Woetzel */
25  inline void sincos(const float xRad, float & s, float & c)
26  {
27  s = sin(xRad);
28  c = cos(xRad);
29  }
30  inline void sincos(const double xRad, double & s, double & c)
31  {
32  s = sin(xRad);
33  c = cos(xRad);
34  }
35 
36  /** @brief linear interpolation
37  Cg equivalent function.
38  @author Jan Woetzel */
39  inline float lerp(const float a, const float b, const float f)
40  {
41  return (1-f)*a +b*f;
42  }
43  inline double lerp(const double a, const double b, const double f)
44  {
45  return (1-f)*a +b*f;
46  }
47 
48 
49 
50  /** @brief Helper returning true on console ostreams like cerr, cout
51  Useful to distinguish between console (cout,cerr) and other i.e. fout fstreams.
52  @author Jan Woetzel */
53  inline
54  bool IsConsoleStream(const std::ostream & os)
55  {
56  std::streambuf *pb = os.rdbuf();
57  // if pb is one of the following we are streaming to a console
58  // that may need different formatting
59  return
60  ( (pb==std::cout.rdbuf())
61  || (pb==std::cerr.rdbuf()) );
62  }
63 
64 
65  /** @brief determien the number of deciamls to store, w.g. to file
66 
67  @returns the number of decimals between
68  lower bound guaranteed precision (digits10)
69  and the upper bound max. precision (nr. of binary digits)
70  @author Jan Woetzel */
71 #ifdef WIN32
72 # pragma warning(push, 2)
73 #endif // WIN32
74  template <class T>
75  inline
76  unsigned int DecimalsToStore()
77  {
78  if (std::numeric_limits<T>::is_integer){
79  // integer types can display exactly one deciaml more than their guaranteed precision digits
80  return std::numeric_limits<T>::digits10 +1;
81  } else {
82  //return std::numeric_limits<T>::digits10; // lower bound
83  return std::numeric_limits<T>::digits; // upper bound
84  }
85  }
86 #ifdef WIN32
87 # pragma warning(pop)
88 #endif // WIN32
89 
90 } // namespace
91 
92 #endif // __Operators_hh__
bool IsConsoleStream(const std::ostream &os)
Helper returning true on console ostreams like cerr, cout Useful to distinguish between console (cout...
Definition: Utils.hh:54
void sincos(const float xRad, float &s, float &c)
computes sin and cosinus with float precision
Definition: Utils.hh:25
unsigned int DecimalsToStore()
determien the number of deciamls to store, w.g.
Definition: Utils.hh:76
float lerp(const float a, const float b, const float f)
linear interpolation Cg equivalent function.
Definition: Utils.hh:39