PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/TCSystem/Modules/Math/inc/TCMathUtil.h

https://bitbucket.org/the____tiger/tcfilesync
C++ Header | 256 lines | 108 code | 31 blank | 117 comment | 13 complexity | 075841d57ca8c9174381bd30f33411a7 MD5 | raw file
Possible License(s): LGPL-2.1
  1. //*******************************************************************************
  2. //
  3. // ******* *** *** *
  4. // * * * *
  5. // * * * *****
  6. // * * *** * * ** * ** ***
  7. // * * * * * * * **** * * *
  8. // * * * * * * * * * * *
  9. // * *** *** * ** ** ** * *
  10. // *
  11. //*******************************************************************************
  12. // see http://sourceforge.net/projects/tcsystem/ for details.
  13. // Copyright (C) 2003 - 2012 Thomas Goessler. All Rights Reserved.
  14. //*******************************************************************************
  15. //
  16. // TCSystem is the legal property of its developers.
  17. // Please refer to the COPYRIGHT file distributed with this source distribution.
  18. //
  19. // This library is free software; you can redistribute it and/or
  20. // modify it under the terms of the GNU Lesser General Public
  21. // License as published by the Free Software Foundation; either
  22. // version 2.1 of the License, or (at your option) any later version.
  23. //
  24. // This library is distributed in the hope that it will be useful,
  25. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. // Lesser General Public License for more details.
  28. //
  29. // You should have received a copy of the GNU Lesser General Public
  30. // License along with this library; if not, write to the Free Software
  31. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  32. //*******************************************************************************
  33. // $Id$
  34. //*******************************************************************************
  35. #ifndef _TC_MATH_UTIL_H_
  36. #define _TC_MATH_UTIL_H_
  37. #include "TCTypes.h"
  38. #include "TCMathApi.h"
  39. #include <cmath>
  40. #include <limits>
  41. #undef PI
  42. namespace tc
  43. {
  44. /**
  45. * @brief tc::math is intended to provide methods to perform common math operations.
  46. *
  47. * These include providing constants such as Pi; conversion
  48. * from degrees to radians; vector operations such as dot and cross products
  49. * matrix determinant for 2x2 and 3x3 matrices; and random
  50. * number generation. Note that these are not all implemented yet.
  51. */
  52. namespace math
  53. {
  54. /**
  55. * @addtogroup TC_MATH
  56. * @{
  57. */
  58. /**
  59. * @file
  60. * @brief This file provides the definition of math utilities in the namespace tc::math
  61. * @author Thomas Goessler
  62. */
  63. /** The constant value of PI */
  64. const double PI = 3.1415926535897932385E0;
  65. /** The constant value one Megabyte in bytes */
  66. const double MEGA_BYTES = 1024. * 1024.;
  67. /** @return the absolute value of the given value */
  68. template <class T>
  69. inline T Abs(const T& x)
  70. {
  71. return (x < 0) ? -x : x;
  72. }
  73. /** integer version of pow(..) */
  74. template<class T>
  75. inline T Pow(T x, uint32_t y)
  76. {
  77. if (y == 0)
  78. {
  79. return 1;
  80. }
  81. T ret = x;
  82. while (--y)
  83. {
  84. ret*=x;
  85. }
  86. return ret;
  87. }
  88. /**
  89. * @brief aligns a given number to 32
  90. * @param size - value that should be aligned
  91. * @return size aligned to 32 bytes
  92. */
  93. inline uint32_t Align32(uint32_t size)
  94. {
  95. return ((size + 31) & (~31));
  96. }
  97. /** @brief Compare two double values if equal by checking their difference */
  98. inline bool Compare(double val1, double val2)
  99. {
  100. return Abs(val1 - val2) < std::numeric_limits<double>::epsilon();
  101. }
  102. /**
  103. * @brief aligns a given number to 16
  104. * @param size - value that should be aligned
  105. * @return size aligned to 16 bytes
  106. */
  107. inline uint32_t Align16(uint32_t size)
  108. {
  109. return ((size + 15) & (~15));
  110. }
  111. /**
  112. * Function converts angle in degrees to angle in radians
  113. * @param val angle value in degrees
  114. * @return angle value in radians
  115. */
  116. template <class T>
  117. inline double Deg2Rad(T val)
  118. {
  119. return static_cast<double>(val) * PI / 180.0;
  120. }
  121. /**
  122. * Function converts angle in radians to angle in degrees
  123. * @param val angle value in radians
  124. * @return angle value in degrees
  125. */
  126. template <class T>
  127. inline double Rad2Deg(T val)
  128. {
  129. return static_cast<double>(val) * 180.0 / PI;
  130. }
  131. /**
  132. * Function converts a value in the range between minValue and maxValue to
  133. * a value between 0 and one
  134. * @param minValue The value defines the minuimum when the value is 0
  135. * @param maxValue The value defines the maximum when the value is 1
  136. * @param value The value which should be converted to a value between 0 and one
  137. * @return The converted value between 0 and 0
  138. */
  139. template <class T>
  140. inline T Normalize(T minValue, T maxValue, T value)
  141. {
  142. // check if min and max was exchanged
  143. T startSize = (T)0.;
  144. T multiplier = (T)1.;
  145. if (maxValue < minValue)
  146. {
  147. Swap(minValue, maxValue);
  148. startSize = (T)1.;
  149. multiplier = (T)-1.;
  150. }
  151. value = Max(minValue, value);
  152. value = Min(maxValue, value);
  153. if (maxValue - minValue)
  154. return (T)(startSize + multiplier * ((value - minValue) / (maxValue - minValue)));
  155. else
  156. return (T)1.;
  157. }
  158. /**
  159. * This function calculates sin(x)/x in a safe way, i.e. without overflows near 0.
  160. * @param x Trivial.
  161. * @return sin(x)/x.
  162. */
  163. template <class T>
  164. inline double SinXOverX(T x)
  165. {
  166. if (1.0 + x*x > 1.0)
  167. return std::sin(x)/x;
  168. else
  169. return 1;
  170. }
  171. /**
  172. * Round any kind of value to the first significant value
  173. *
  174. * e.g. 53465 > 50000
  175. * 0.0875 > 0.08
  176. * -234 > -200
  177. *
  178. * @param val The value which to round
  179. * @return the rounded value
  180. */
  181. template <class T>
  182. inline T RoundToFirstSignificantValue(T val)
  183. {
  184. // on 0 we do nothing
  185. if (val == (T)0) return (T)0;
  186. double logRange = std::log10(Abs(val));
  187. int32_t logRangeFloor = (int32_t)std::floor(logRange);
  188. double normalized = (int32_t)std::pow(10.0, logRange - logRangeFloor);
  189. int32_t i;
  190. if (logRangeFloor > 0)
  191. for (i=0; i<logRangeFloor; i++) normalized *= 10.;
  192. else {
  193. int32_t absLogRangeFloor = Abs(logRangeFloor);
  194. for (i=0; i<absLogRangeFloor; i++) normalized *= 0.1;
  195. }
  196. if (val < (T)0) normalized = -normalized;
  197. return (T)normalized;
  198. }
  199. /** compare double or float value with its specified system epsilon */
  200. template <class T>
  201. bool Compare(const T& val1, const T& val2)
  202. {
  203. return Abs(val1 - val2) > std::numeric_limits<T>::epsilon();
  204. }
  205. /**
  206. * function returns a true value if value is
  207. * "not-a-number" (NaN), and false otherwise.
  208. */
  209. TCMATH_API bool IsNaN(double val);
  210. /**
  211. * @return true value if value is between -INF and +INF
  212. * false other wise
  213. */
  214. TCMATH_API bool IsFinite(double val);
  215. /**
  216. * @return true value if value is infinite
  217. * false other wise
  218. */
  219. TCMATH_API bool IsInf(double val);
  220. /**
  221. * @}
  222. */
  223. } // namespace math
  224. } // namespace tc
  225. #endif // _TC_MATH_UTIL_H_