QCAD
Open Source 2D CAD
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
RMath.h
Go to the documentation of this file.
1 
20 #ifndef RMATH_H
21 #define RMATH_H
22 
23 #include "../core_global.h"
24 
25 #include <QMetaType>
26 
27 #include <cstdio>
28 #include <cmath>
29 #include <cerrno>
30 #include <limits>
31 
32 #include "RVector.h"
33 
34 #ifndef M_PI
35 #define M_PI 3.14159265358979323846
36 #endif
37 
38 #ifndef M_PI_2
39 #define M_PI_2 1.57079632679489661923
40 #endif
41 
42 
43 // Somewhere in the guts of Visual C++ a macro 'max' is defined which
44 // breaks std::numeric_limits<double>::max(). This fix is not correct
45 // but good enough for now.
46 #ifdef _MSC_VER
47 #define RMAXDOUBLE 1e300
48 #define RMINDOUBLE -1e300
49 #else
50 #define RMAXDOUBLE std::numeric_limits<double>::max()
51 #define RMINDOUBLE -std::numeric_limits<double>::max()
52 #endif
53 
54 #ifndef RNANDOUBLE
55 #define RNANDOUBLE std::numeric_limits<double>::quiet_NaN()
56 #endif
57 
58 
66 public:
67  static void init() {
68  }
69 
70  static void uninit() {
71  }
72 
76  static inline int mround(double v) {
77 #if defined(__GCC2x__) || defined(_MSC_VER)
78  return (v-floor(v)<0.5 ? (int)floor(v) : (int)ceil(v));
79 #else
80  return (int) round(v);
81 #endif
82  }
83 
88  template <class T>
89  static void swap( T &a, T &b) {
90  const T ttmp(a);
91  a=b;
92  b=ttmp;
93  }
94 
95  static double trunc(double v);
96 
97  static double pow(double x, double y);
98 
99  static bool isNormal(double v);
100  static bool isNaN(double v);
101  static bool isInf(double v);
102 
103  static double eval(const QString& expression, bool* ok = NULL);
104  static QString getError();
105  static bool hasError();
106  //static double stringToAngle(const QString& str);
107  static QString angleToString(double a);
108 
109  static double rad2deg(double a);
110  static double deg2rad(double a);
111  static double rad2gra(double a);
112  static double gra2deg(double a);
113 
114  static int getGcd(int a, int b);
115  static bool isAngleBetween(double a, double a1, double a2, bool reversed);
116  static double getNormalizedAngle(double a);
117  static double getRelativeAngle(double a, double baseAngle);
118  static double getAngleDifference(double a1, double a2);
119  static double getAngleDifference180(double a1, double a2);
120  static double makeAngleReadable(double angle, bool readable = true,
121  bool* corrected = NULL);
122  static bool isAngleReadable(double angle, double tolerance =
124  static bool isSameDirection(double dir1, double dir2, double tol =
126  static int absmod(int a, int b);
127 
128  static QString toFractionString(double v, int maxDenominator);
129  static void toFraction(double v, int maxDenominator, int& number, int& numerator, int& denominator);
130  static void simplify(int numerator, int denominator, int& numeratorRes, int& denominatorRes);
131 
132  static bool fuzzyCompare(double v1, double v2, double tolerance =
134 
135  static double parseScale(const QString& scaleString);
136 
140  static void getQuadRoots(double p[], double r[][5]);
144  static void getCubicRoots(double p[], double r[][5]);
148  static void getBiQuadRoots(double p[], double r[][5]);
149 private:
150  static QString lastError;
151 };
152 
154 
155 #endif