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 #ifndef RINFDOUBLE
59 #define RINFDOUBLE std::numeric_limits<double>::infinity()
60 #endif
61 
62 
70 public:
71  static void init() {
72  }
73 
74  static void uninit() {
75  }
76 
80  static inline int mround(double v) {
81 #if defined(__GCC2x__) || defined(_MSC_VER)
82  return (v-floor(v)<0.5 ? (int)floor(v) : (int)ceil(v));
83 #else
84  return (int) round(v);
85 #endif
86  }
87 
92  template <class T>
93  static void swap( T &a, T &b) {
94  const T ttmp(a);
95  a=b;
96  b=ttmp;
97  }
98 
99  static double trunc(double v);
100 
101  static double pow(double x, double y);
102 
103  static bool isNormal(double v);
104  static bool isNaN(double v);
105  static bool isInf(double v);
106 
107  static double eval(const QString& expression, bool* ok = NULL);
108  static QString getError();
109  static bool hasError();
110  //static double stringToAngle(const QString& str);
111  static QString angleToString(double a);
112 
113  static double rad2deg(double a);
114  static double deg2rad(double a);
115  static double rad2gra(double a);
116  static double gra2deg(double a);
117 
118  static int getGcd(int a, int b);
119  static bool isAngleBetween(double a, double a1, double a2, bool reversed);
120  static double getNormalizedAngle(double a);
121  static double getRelativeAngle(double a, double baseAngle);
122  static double getAngleDifference(double a1, double a2);
123  static double getAngleDifference180(double a1, double a2);
124  static double makeAngleReadable(double angle, bool readable = true, bool* corrected = NULL);
125  static bool isAngleReadable(double angle, double tolerance = 0.01);
126  static bool isSameDirection(double dir1, double dir2, double tol = RS::AngleTolerance);
127  static int absmod(int a, int b);
128 
129  static QString toFractionString(double v, int maxDenominator);
130  static void toFraction(double v, int maxDenominator, int& number, int& numerator, int& denominator);
131  static void simplify(int numerator, int denominator, int& numeratorRes, int& denominatorRes);
132 
133  static bool fuzzyCompare(double v1, double v2, double tolerance =
135 
136  static double parseScale(const QString& scaleString);
137 
141  static void getQuadRoots(double p[], double r[][5]);
145  static void getCubicRoots(double p[], double r[][5]);
149  static void getBiQuadRoots(double p[], double r[][5]);
150 private:
151  static QString lastError;
152 };
153 
155 
156 #endif