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 bool isBetween(double value, double limit1, double limit2, bool inclusive, double tolerance = RS::PointTolerance);
138 
139  static int getGcd(int a, int b);
140  static bool isAngleBetween(double a, double a1, double a2, bool reversed);
141  static double getNormalizedAngle(double a);
142  static double getRelativeAngle(double a, double baseAngle);
143  static double getAngleDifference(double a1, double a2);
144  static double getAngleDifference180(double a1, double a2);
145  static double makeAngleReadable(double angle, bool readable = true, bool* corrected = NULL);
146  static bool isAngleReadable(double angle, double tolerance = 0.01);
147  static bool isSameDirection(double dir1, double dir2, double tol = RS::AngleTolerance);
148  static int absmod(int a, int b);
149 
150  static QString toFractionString(double v, int maxDenominator);
151  static void toFraction(double v, int maxDenominator, int& number, int& numerator, int& denominator);
152  static void simplify(int numerator, int denominator, int& numeratorRes, int& denominatorRes);
153 
154  static bool fuzzyCompare(double v1, double v2, double tolerance = RS::PointTolerance);
155  static bool fuzzyAngleCompare(double v1, double v2, double tolerance = RS::AngleTolerance);
156 
157  static bool containsFuzzy(const QList<double>& values, double v, double tol = RS::PointTolerance);
158 
159  static double parseScale(const QString& scaleString);
160  static RVector parseCoordinate(const QString& coordinateString, const RVector& relativeZero = RVector::nullVector);
161 
165  static void getQuadRoots(double p[], double r[][5]);
169  static void getCubicRoots(double p[], double r[][5]);
173  static void getBiQuadRoots(double p[], double r[][5]);
174 private:
175  static QString lastError;
176  static QString mathExt;
177 };
178 
180 
181 #endif
deg2rad
void deg2rad(void val)
Converts degrees to radiant.
Definition: library.js:343
RS::AngleTolerance
static const double AngleTolerance
Definition: RS.h:700
RMath::uninit
static void uninit()
Definition: RMath.h:90
RVector::nullVector
static const RVector nullVector
null vector
Definition: RVector.h:327
RS::PointTolerance
static const double PointTolerance
Copyright (c) 2011-2018 by Andrew Mustun.
Definition: RS.h:699
RVector
Represents a 3d vector (x/y/z).
Definition: RVector.h:46
RMath::mround
static int mround(double v)
Rounds the given double to the next int.
Definition: RMath.h:96
rad2deg
void rad2deg(void val)
Converts radiant to degrees.
Definition: library.js:597
RVector.h
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(RMath *)
RMath::init
static void init()
Definition: RMath.h:87
RMath::lastError
static QString lastError
Copyright (c) 2011-2018 by Andrew Mustun.
Definition: RMath.h:175
RMath::swap
static void swap(T &a, T &b)
Swaps two given values.
Definition: RMath.h:109
RMath::mathExt
static QString mathExt
Definition: RMath.h:176
RMath
Math functions.
Definition: RMath.h:85
QCADCORE_EXPORT
#define QCADCORE_EXPORT
Definition: core_global.h:10
angleToString
void angleToString(void num, void decimals)
Definition: library.js:132