qwt_math.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
  2. * Qwt Widget Library
  3. * Copyright (C) 1997 Josef Wilgen
  4. * Copyright (C) 2002 Uwe Rathmann
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Qwt License, Version 1.0
  8. *****************************************************************************/
  9. #ifndef QWT_MATH_H
  10. #define QWT_MATH_H
  11. #include "qwt_global.h"
  12. #if defined(_MSC_VER)
  13. /*
  14. Microsoft says:
  15. Define _USE_MATH_DEFINES before including math.h to expose these macro
  16. definitions for common math constants. These are placed under an #ifdef
  17. since these commonly-defined names are not part of the C/C++ standards.
  18. */
  19. #define _USE_MATH_DEFINES 1
  20. #endif
  21. #include <qpoint.h>
  22. #include <qmath.h>
  23. #include "qwt_global.h"
  24. #ifndef LOG10_2
  25. #define LOG10_2 0.30102999566398119802 /* log10(2) */
  26. #endif
  27. #ifndef LOG10_3
  28. #define LOG10_3 0.47712125471966243540 /* log10(3) */
  29. #endif
  30. #ifndef LOG10_5
  31. #define LOG10_5 0.69897000433601885749 /* log10(5) */
  32. #endif
  33. #ifndef M_2PI
  34. #define M_2PI 6.28318530717958623200 /* 2 pi */
  35. #endif
  36. #ifndef LOG_MIN
  37. //! Mininum value for logarithmic scales
  38. #define LOG_MIN 1.0e-100
  39. #endif
  40. #ifndef LOG_MAX
  41. //! Maximum value for logarithmic scales
  42. #define LOG_MAX 1.0e100
  43. #endif
  44. #ifndef M_E
  45. #define M_E 2.7182818284590452354 /* e */
  46. #endif
  47. #ifndef M_LOG2E
  48. #define M_LOG2E 1.4426950408889634074 /* log_2 e */
  49. #endif
  50. #ifndef M_LOG10E
  51. #define M_LOG10E 0.43429448190325182765 /* log_10 e */
  52. #endif
  53. #ifndef M_LN2
  54. #define M_LN2 0.69314718055994530942 /* log_e 2 */
  55. #endif
  56. #ifndef M_LN10
  57. #define M_LN10 2.30258509299404568402 /* log_e 10 */
  58. #endif
  59. #ifndef M_PI
  60. #define M_PI 3.14159265358979323846 /* pi */
  61. #endif
  62. #ifndef M_PI_2
  63. #define M_PI_2 1.57079632679489661923 /* pi/2 */
  64. #endif
  65. #ifndef M_PI_4
  66. #define M_PI_4 0.78539816339744830962 /* pi/4 */
  67. #endif
  68. #ifndef M_1_PI
  69. #define M_1_PI 0.31830988618379067154 /* 1/pi */
  70. #endif
  71. #ifndef M_2_PI
  72. #define M_2_PI 0.63661977236758134308 /* 2/pi */
  73. #endif
  74. #ifndef M_2_SQRTPI
  75. #define M_2_SQRTPI 1.12837916709551257390 /* 2/sqrt(pi) */
  76. #endif
  77. #ifndef M_SQRT2
  78. #define M_SQRT2 1.41421356237309504880 /* sqrt(2) */
  79. #endif
  80. #ifndef M_SQRT1_2
  81. #define M_SQRT1_2 0.70710678118654752440 /* 1/sqrt(2) */
  82. #endif
  83. QWT_EXPORT double qwtGetMin( const double *array, int size );
  84. QWT_EXPORT double qwtGetMax( const double *array, int size );
  85. /*!
  86. \brief Compare 2 values, relative to an interval
  87. Values are "equal", when :
  88. \f$\cdot value2 - value1 <= abs(intervalSize * 10e^{-6})\f$
  89. \param value1 First value to compare
  90. \param value2 Second value to compare
  91. \param intervalSize interval size
  92. \return 0: if equal, -1: if value2 > value1, 1: if value1 > value2
  93. */
  94. inline int qwtFuzzyCompare( double value1, double value2, double intervalSize )
  95. {
  96. const double eps = qAbs( 1.0e-6 * intervalSize );
  97. if ( value2 - value1 > eps )
  98. return -1;
  99. if ( value1 - value2 > eps )
  100. return 1;
  101. return 0;
  102. }
  103. inline bool qwtFuzzyGreaterOrEqual( double d1, double d2 )
  104. {
  105. return ( d1 >= d2 ) || qFuzzyCompare( d1, d2 );
  106. }
  107. inline bool qwtFuzzyLessOrEqual( double d1, double d2 )
  108. {
  109. return ( d1 <= d2 ) || qFuzzyCompare( d1, d2 );
  110. }
  111. //! Return the sign
  112. inline int qwtSign( double x )
  113. {
  114. if ( x > 0.0 )
  115. return 1;
  116. else if ( x < 0.0 )
  117. return ( -1 );
  118. else
  119. return 0;
  120. }
  121. //! Return the square of a number
  122. inline double qwtSqr( const double x )
  123. {
  124. return x * x;
  125. }
  126. /*!
  127. \brief Limit a value to fit into a specified interval
  128. \param x Input value
  129. \param x1 First interval boundary
  130. \param x2 Second interval boundary
  131. */
  132. template <class T>
  133. T qwtLim( const T& x, const T& x1, const T& x2 )
  134. {
  135. T rv;
  136. T xmin, xmax;
  137. xmin = qMin( x1, x2 );
  138. xmax = qMax( x1, x2 );
  139. if ( x < xmin )
  140. rv = xmin;
  141. else if ( x > xmax )
  142. rv = xmax;
  143. else
  144. rv = x;
  145. return rv;
  146. }
  147. inline QPoint qwtPolar2Pos( const QPoint &pole,
  148. double radius, double angle )
  149. {
  150. const double x = pole.x() + radius * qCos( angle );
  151. const double y = pole.y() - radius * qSin( angle );
  152. return QPoint( qRound( x ), qRound( y ) );
  153. }
  154. inline QPoint qwtDegree2Pos( const QPoint &pole,
  155. double radius, double angle )
  156. {
  157. return qwtPolar2Pos( pole, radius, angle / 180.0 * M_PI );
  158. }
  159. inline QPointF qwtPolar2Pos( const QPointF &pole,
  160. double radius, double angle )
  161. {
  162. const double x = pole.x() + radius * qCos( angle );
  163. const double y = pole.y() - radius * qSin( angle );
  164. return QPoint( qRound( x ), qRound( y ) );
  165. }
  166. inline QPointF qwtDegree2Pos( const QPointF &pole,
  167. double radius, double angle )
  168. {
  169. return qwtPolar2Pos( pole, radius, angle / 180.0 * M_PI );
  170. }
  171. #endif