1
/****************************************************************************
3
** This file is part of the LibreCAD project, a 2D CAD program
5
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
9
** This file may be distributed and/or modified under the terms of the
10
** GNU General Public License version 2 as published by the Free Software
11
** Foundation and appearing in the file gpl-2.0.txt included in the
12
** packaging of this file.
14
** This program is distributed in the hope that it will be useful,
15
** but WITHOUT ANY WARRANTY; without even the implied warranty of
16
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
** GNU General Public License for more details.
19
** You should have received a copy of the GNU General Public License
20
** along with this program; if not, write to the Free Software
21
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
** This copyright notice MUST APPEAR in all copies of the script!
25
**********************************************************************/
30
// no idea why, but doesn't link without that under win32 / bcc55:
40
// RVT port abs issue on latest compiler?
47
#include "rs_vector.h"
51
//#define min(x,y) (x<y ? x : y)
52
//#define max(x,y) (x>y ? x : y)
55
#define ARAD 57.29577951308232
57
#define RS_TOLERANCE 1.0e-10
59
#define RS_TOLERANCE15 1.5e-15
60
#define RS_TOLERANCE2 1.0e-20
61
#define RS_TOLERANCE_ANGLE 1.0e-8
63
//typedef unsigned int uint;
71
static int round(double v);
72
static double pow(double x, double y);
73
static RS_Vector pow(RS_Vector x, double y);
75
//static double abs(double v);
76
//static int abs(int v);
77
static double rad2deg(double a);
78
static double deg2rad(double a);
79
static double rad2gra(double a);
80
static int findGCD(int a, int b);
81
static bool isAngleBetween(double a,
83
bool reversed = false);
84
static double correctAngle(double a);
85
static double getAngleDifference(double a1, double a2, bool reversed = false);
86
static double makeAngleReadable(double angle, bool readable=true,
87
bool* corrected=NULL);
88
static bool isAngleReadable(double angle);
89
static bool isSameDirection(double dir1, double dir2, double tol);
90
static double eval(const QString& expr, double def=0.0);
92
static bool cmpDouble(double v1, double v2, double tol=0.001);
93
//swap of two variables
95
static void swap( T &a, T &b) {
101
static double eval(const QString& expr, bool* ok);
103
static std::vector<double> quadraticSolver(const std::vector<double>& ce);
104
static std::vector<double> cubicSolver(const std::vector<double>& ce);
106
* x^4 + ce[0] x^3 + ce[1] x^2 + ce[2] x + ce[3] = 0
107
@ce, a vector of size 4 contains the coefficient in order
108
@return, a vector contains real roots
110
static std::vector<double> quarticSolver(const std::vector<double>& ce);
112
* ce[4] x^4 + ce[3] x^3 + ce[2] x^2 + ce[1] x + ce[0] = 0
113
@ce, a vector of size 5 contains the coefficient in order
114
@return, a vector contains real roots
116
static std::vector<double> quarticSolverFull(const std::vector<double>& ce);
117
//solver for linear equation set
119
* Solve linear equation set
120
*@ mt holds the augmented matrix
121
*@ sn holds the solution
122
*@ return true, if the equation set has a unique solution, return false otherwise
126
static bool linearSolver(const QVector<QVector<double> >& m, QVector<double>& sn);
128
/** solver quadratic simultaneous equations of a set of two **/
129
/* solve the following quadratic simultaneous equations,
130
* ma000 x^2 + ma011 y^2 - 1 =0
131
* ma100 x^2 + 2 ma101 xy + ma111 y^2 + mb10 x + mb11 y +mc1 =0
133
*@m, a vector of size 8 contains coefficients in the strict order of:
134
ma000 ma011 ma100 ma101 ma111 mb10 mb11 mc1
135
*@return a RS_VectorSolutions contains real roots (x,y)
137
static RS_VectorSolutions simultaneousQuadraticSolver(const std::vector<double>& m);
139
/** solver quadratic simultaneous equations of a set of two **/
140
/* solve the following quadratic simultaneous equations,
141
* ma000 x^2 + ma001 xy + ma011 y^2 + mb00 x + mb01 y + mc0 =0
142
* ma100 x^2 + ma101 xy + ma111 y^2 + mb10 x + mb11 y + mc1 =0
144
*@m, a vector of size 2 each contains a vector of size 6 coefficients in the strict order of:
145
ma000 ma001 ma011 mb00 mb01 mc0
146
ma100 ma101 ma111 mb10 mb11 mc1
147
*@return a RS_VectorSolutions contains real roots (x,y)
149
static RS_VectorSolutions simultaneousQuadraticSolverFull(const std::vector<std::vector<double> >& m);
150
static RS_VectorSolutions simultaneousQuadraticSolverMixed(const std::vector<std::vector<double> >& m);
152
/** verify a solution for simultaneousQuadratic
153
*@m the coefficient matrix
154
*@v, a candidate to verify
155
*@return true, for a valid solution
157
static bool simultaneousQuadraticVerify(const std::vector<std::vector<double> >& m, const RS_Vector& v);
158
/** wrapper for elliptic integral **/
160
* wrapper of elliptic integral of the second type, Legendre form
161
*@k the elliptic modulus or eccentricity
162
*@phi elliptic angle, must be within range of [0, M_PI]
166
static double ellipticIntegral_2(const double& k, const double& phi);
168
static QString doubleToString(double value, double prec);
169
static QString doubleToString(double value, int prec);