QCAD
Open Source 2D CAD
Loading...
Searching...
No Matches
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 "RS.h"
26
27#include <QMetaType>
28
29#include <cmath>
30#include <limits>
31
32#if QT_VERSION >= 0x060000
33class QJSEngine;
34#endif
35class RVector;
36
37#ifndef M_PI
38#define M_PI 3.14159265358979323846264338327950288
39#endif
40
41#ifndef M_PI_2
42#define M_PI_2 1.57079632679489661923132169163975144
43#endif
44
45#ifndef M_PI_4
46#define M_PI_4 0.785398163397448309615660845819875721
47#endif
48
49#ifndef M_LN10
50#define M_LN10 2.30258509299404568401799145468436421
51#endif
52
53#ifndef M_LN2
54#define M_LN2 0.693147180559945309417232121458176568
55#endif
56
57
58
59// Somewhere in the guts of Visual C++ a macro 'max' is defined which
60// breaks std::numeric_limits<double>::max(). This fix is not correct
61// but good enough for now.
62#ifdef _MSC_VER
63#define RMAXDOUBLE 1e300
64#define RMINDOUBLE -1e300
65#else
66#define RMAXDOUBLE std::numeric_limits<double>::max()
67#define RMINDOUBLE -std::numeric_limits<double>::max()
68#endif
69
70#define RMAXINT INT_MAX
71#define RMININT INT_MIN
72
73#ifndef RNANDOUBLE
74#define RNANDOUBLE std::numeric_limits<double>::quiet_NaN()
75#endif
76
77#ifndef RINFDOUBLE
78#define RINFDOUBLE std::numeric_limits<double>::infinity()
79#endif
80
81
89public:
90 static void init();
91 static void uninit();
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);
161
162 static QString getMd5Hash(const QString& data);
163
167 static void getQuadRoots(double p[], double r[][5]);
171 static void getCubicRoots(double p[], double r[][5]);
175 static void getBiQuadRoots(double p[], double r[][5]);
176private:
177 static QString lastError;
178 static QString mathExt;
179
180#if QT_VERSION >= 0x060000
181 static QJSEngine* jsEngine;
182#endif
183};
184
186
187#endif
void init(void basePath)
Definition AddBlockInit.js:2
Q_DECLARE_METATYPE(RMath *)
Math functions.
Definition RMath.h:88
static QString mathExt
Definition RMath.h:178
static int mround(double v)
Rounds the given double to the next int.
Definition RMath.h:96
static QString lastError
Copyright (c) 2011-2018 by Andrew Mustun.
Definition RMath.h:177
static void swap(T &a, T &b)
Swaps two given values.
Definition RMath.h:109
static const double PointTolerance
Copyright (c) 2011-2018 by Andrew Mustun.
Definition RS.h:920
static const double AngleTolerance
Definition RS.h:921
Represents a 3d vector (x/y/z).
Definition RVector.h:47
#define QCADCORE_EXPORT
Definition core_global.h:10
void angleToString(void num, void decimals)
Definition library.js:1059
void rad2deg(void val)
Converts radiant to degrees.
Definition library.js:256
void deg2rad(void val)
Converts degrees to radiant.
Definition library.js:759
char s
Definition opennurbs_string.cpp:32
#define NULL
Definition opennurbs_system.h:256