QCAD Application Framework
CAD Application Development and Automation.
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
RMath.h
Go to the documentation of this file.
1 #ifndef RMATH_H
2 #define RMATH_H
3 
4 #include <QMetaType>
5 
6 #include <cstdio>
7 #include <cmath>
8 #include <cerrno>
9 #include <limits>
10 
11 #include "RVector.h"
12 
13 #ifndef M_PI
14 #define M_PI 3.14159265358979323846
15 #endif
16 
17 #ifndef M_PI_2
18 #define M_PI_2 1.57079632679489661923
19 #endif
20 
21 
22 /*
23 #ifndef exp10
24 #define exp10(x) pow(10,x)
25 #endif
26 */
27 
28 /*
29 #ifndef isnan
30 #ifdef Q_OS_MAC
31 // Mac OS X does not appear to have std::isnan
32 #define isnan(x) (std::fpclassify(x)==FP_NAN)
33 #elif defined Q_OS_WIN32
34 #include <cfloat>
35 #define isnan(x) _isnan(x)
36 #else
37 // Linux has std::isnan:
38 #define isnan(x) (std::isnan(x))
39 #endif
40 #endif
41 */
42 
43 //#define ARAD 57.29577951308232
44 
45 
46 // Somewhere in the guts of Visual C++ a macro 'max' is defined which
47 // breaks std::numeric_limits<double>::max(). This fix is not correct
48 // but good enough for now.
49 #ifdef _MSC_VER
50 #define RMAXDOUBLE 1e10
51 #define RMINDOUBLE -1e10
52 #else
53 #define RMAXDOUBLE std::numeric_limits<double>::max()
54 #define RMINDOUBLE -std::numeric_limits<double>::max()
55 #endif
56 
57 #ifndef RNANDOUBLE
58 #define RNANDOUBLE std::numeric_limits<double>::quiet_NaN()
59 #endif
60 
67 class RMath {
68 public:
72  static inline int mround(double v) {
73 #if defined(__GCC2x__) || defined(_MSC_VER)
74  return (v-floor(v)<0.5 ? (int)floor(v) : (int)ceil(v));
75 #else
76  return (int) round(v);
77 #endif
78  }
79 
84  template <class T>
85  static void swap( T &a, T &b) {
86  const T ttmp(a);
87  a=b;
88  b=ttmp;
89  }
90 
91  static double trunc(double v);
92 
93  static double pow(double x, double y);
94 
95  static bool isNormal(double v);
96  static bool isNaN(double v);
97  static bool isInf(double v);
98 
99  static double eval(const QString& expression, bool* ok = NULL);
100  static QString getError();
101  static bool hasError();
102  //static double stringToAngle(const QString& str);
103  static QString angleToString(double a);
104 
105  static double rad2deg(double a);
106  static double deg2rad(double a);
107  static double rad2gra(double a);
108  static double gra2deg(double a);
109 
110  static int getGcd(int a, int b);
111  static bool isAngleBetween(double a, double a1, double a2, bool reversed);
112  static double getNormalizedAngle(double a);
113  static double getAngleDifference(double a1, double a2);
114  static double getAngleDifference180(double a1, double a2);
115  static double makeAngleReadable(double angle, bool readable = true,
116  bool* corrected = NULL);
117  static bool isAngleReadable(double angle, double tolerance =
119  static bool isSameDirection(double dir1, double dir2, double tol =
121  static int absmod(int a, int b);
122 
123  static QString toFractionString(double v, int maxDenominator);
124  static void toFraction(double v, int maxDenominator, int& number, int& numerator, int& denominator);
125  static void simplify(int numerator, int denominator, int& numeratorRes, int& denominatorRes);
126 
127  static bool fuzzyCompare(double v1, double v2, double tolerance =
129 
130  static double parseScale(const QString& scaleString);
131 
135  static void getQuadRoots(double p[], double r[][5]);
139  static void getCubicRoots(double p[], double r[][5]);
143  static void getBiQuadRoots(double p[], double r[][5]);
144 private:
145  static QString lastError;
146 };
147 
149 
150 #endif