QCAD
Open Source 2D CAD
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.14159265358979323846264338327950288
36 #endif
37 
38 #ifndef M_PI_2
39 #define M_PI_2 1.57079632679489661923132169163975144
40 #endif
41 
42 #ifndef M_PI_4
43 #define M_PI_4 0.785398163397448309615660845819875721
44 #endif
45 
46 #ifndef M_LN10
47 #define M_LN10 2.30258509299404568401799145468436421
48 #endif
49 
50 #ifndef M_LN2
51 #define M_LN2 0.693147180559945309417232121458176568
52 #endif
53 
54 
55 
56 // Somewhere in the guts of Visual C++ a macro 'max' is defined which
57 // breaks std::numeric_limits<double>::max(). This fix is not correct
58 // but good enough for now.
59 #ifdef _MSC_VER
60 #define RMAXDOUBLE 1e300
61 #define RMINDOUBLE -1e300
62 #else
63 #define RMAXDOUBLE std::numeric_limits<double>::max()
64 #define RMINDOUBLE -std::numeric_limits<double>::max()
65 #endif
66 
67 #define RMAXINT INT_MAX
68 #define RMININT INT_MIN
69 
70 #ifndef RNANDOUBLE
71 #define RNANDOUBLE std::numeric_limits<double>::quiet_NaN()
72 #endif
73 
74 #ifndef RINFDOUBLE
75 #define RINFDOUBLE std::numeric_limits<double>::infinity()
76 #endif
77 
78 
86 public:
87  static void init() {
88  }
89 
90  static void uninit() {
91  }
92 
96  static inline int mround(double v) {
97 #if defined(__GCC2x__) || defined(_MSC_VER)
98  return (v-floor(v)<0.5 ? (int)floor(v) : (int)ceil(v));
99 #else
100  return (int) round(v);
101 #endif
102  }
103 
108  template <class T>
109  static void swap( T &a, T &b) {
110  const T ttmp(a);
111  a=b;
112  b=ttmp;
113  }
114 
115  static double trunc(double v);
116 
117  static double pow(double x, double y);
118 
119  static bool isNormal(double v);
120  static bool isNaN(double v);
121  static bool isInf(double v);
122 
123  static bool isSane(double v);
124 
125  static double eval(const QString& expression, bool* ok = NULL);
126  static QString getError();
127  static bool hasError();
128  //static double stringToAngle(const QString& str);
129  static QString angleToString(double a);
130  static QString trimTrailingZeroes(const QString& s);
131 
132  static double rad2deg(double a);
133  static double deg2rad(double a);
134  static double rad2gra(double a);
135  static double gra2deg(double a);
136 
137  static int getGcd(int a, int b);
138  static bool isAngleBetween(double a, double a1, double a2, bool reversed);
139  static double getNormalizedAngle(double a);
140  static double getRelativeAngle(double a, double baseAngle);
141  static double getAngleDifference(double a1, double a2);
142  static double getAngleDifference180(double a1, double a2);
143  static double makeAngleReadable(double angle, bool readable = true, bool* corrected = NULL);
144  static bool isAngleReadable(double angle, double tolerance = 0.01);
145  static bool isSameDirection(double dir1, double dir2, double tol = RS::AngleTolerance);
146  static int absmod(int a, int b);
147 
148  static QString toFractionString(double v, int maxDenominator);
149  static void toFraction(double v, int maxDenominator, int& number, int& numerator, int& denominator);
150  static void simplify(int numerator, int denominator, int& numeratorRes, int& denominatorRes);
151 
152  static bool fuzzyCompare(double v1, double v2, double tolerance =
154 
155  static double parseScale(const QString& scaleString);
156 
160  static void getQuadRoots(double p[], double r[][5]);
164  static void getCubicRoots(double p[], double r[][5]);
168  static void getBiQuadRoots(double p[], double r[][5]);
169 private:
170  static QString lastError;
171  static QString mathExt;
172 };
173 
175 
176 #endif
static const double AngleTolerance
Definition: RS.h:552
#define QCADCORE_EXPORT
Definition: core_global.h:10
static QString lastError
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RMath.h:170
static void swap(T &a, T &b)
Swaps two given values.
Definition: RMath.h:109
static const double PointTolerance
Copyright (c) 2011-2016 by Andrew Mustun.
Definition: RS.h:551
Q_DECLARE_METATYPE(RMath *)
Math functions.
Definition: RMath.h:85
void deg2rad(void val)
Converts degrees to radiant.
Definition: library.js:324
static int mround(double v)
Rounds the given double to the next int.
Definition: RMath.h:96
static void uninit()
Definition: RMath.h:90
void rad2deg(void val)
Converts radiant to degrees.
Definition: library.js:490
static QString mathExt
Definition: RMath.h:171
static void init()
Definition: RMath.h:87
void angleToString(void num, void decimals)
Definition: library.js:500