~librecad-dev/librecad/librecad

« back to all changes in this revision

Viewing changes to librecad/src/lib/math/rs_math.h

  • Committer: Scott Howard
  • Date: 2014-02-21 19:07:55 UTC
  • Revision ID: showard@debian.org-20140221190755-csjax9wb146hgdq4
first commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** This file is part of the LibreCAD project, a 2D CAD program
 
4
**
 
5
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
 
6
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
 
7
**
 
8
**
 
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.
 
13
**
 
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.
 
18
**
 
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
 
22
**
 
23
** This copyright notice MUST APPEAR in all copies of the script!
 
24
**
 
25
**********************************************************************/
 
26
 
 
27
#ifndef RS_MATH_H
 
28
#define RS_MATH_H
 
29
 
 
30
// no idea why, but doesn't link without that under win32 / bcc55:
 
31
#ifndef _MT
 
32
#define _MT
 
33
#endif
 
34
 
 
35
#include <cfloat>
 
36
#include <cmath>
 
37
#include <complex>
 
38
#include <errno.h>
 
39
 
 
40
// RVT port abs issue on latest compiler?
 
41
#include <cstdlib>
 
42
 
 
43
#include <QRegExp>
 
44
#include <QVector>
 
45
 
 
46
#include "rs.h"
 
47
#include "rs_vector.h"
 
48
 
 
49
 
 
50
//#ifdef __GNUC__
 
51
//#define min(x,y) (x<y ? x : y)
 
52
//#define max(x,y) (x>y ? x : y)
 
53
//#endif
 
54
 
 
55
#define ARAD 57.29577951308232
 
56
//tolerance
 
57
#define RS_TOLERANCE 1.0e-10
 
58
//squared tolerance
 
59
#define RS_TOLERANCE15 1.5e-15
 
60
#define RS_TOLERANCE2 1.0e-20
 
61
#define RS_TOLERANCE_ANGLE 1.0e-8
 
62
 
 
63
//typedef unsigned int uint;
 
64
 
 
65
 
 
66
/**
 
67
 * Math functions.
 
68
 */
 
69
class RS_Math {
 
70
public:
 
71
    static int round(double v);
 
72
    static double pow(double x, double y);
 
73
    static RS_Vector pow(RS_Vector x, double y);
 
74
 
 
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,
 
82
                               double a1, double a2,
 
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);
 
91
 
 
92
    static bool cmpDouble(double v1, double v2, double tol=0.001);
 
93
//swap of two variables
 
94
    template <class T>
 
95
    static void swap( T &a, T &b) {
 
96
        const T ttmp(a);
 
97
        a=b;
 
98
        b=ttmp;
 
99
    }
 
100
 
 
101
    static double eval(const QString& expr, bool* ok);
 
102
 
 
103
    static std::vector<double> quadraticSolver(const std::vector<double>& ce);
 
104
    static std::vector<double> cubicSolver(const std::vector<double>& ce);
 
105
    /** quartic solver
 
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
 
109
    **/
 
110
    static std::vector<double> quarticSolver(const std::vector<double>& ce);
 
111
    /** quartic solver
 
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
 
115
    **/
 
116
    static std::vector<double> quarticSolverFull(const std::vector<double>& ce);
 
117
    //solver for linear equation set
 
118
    /**
 
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
 
123
      *
 
124
      *@Author: Dongxu Li
 
125
      */
 
126
    static bool linearSolver(const QVector<QVector<double> >& m, QVector<double>& sn);
 
127
 
 
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
 
132
      *
 
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)
 
136
      */
 
137
    static RS_VectorSolutions simultaneousQuadraticSolver(const std::vector<double>& m);
 
138
 
 
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
 
143
      *
 
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)
 
148
      */
 
149
    static RS_VectorSolutions simultaneousQuadraticSolverFull(const std::vector<std::vector<double> >& m);
 
150
    static RS_VectorSolutions simultaneousQuadraticSolverMixed(const std::vector<std::vector<double> >& m);
 
151
 
 
152
    /** verify a solution for simultaneousQuadratic
 
153
      *@m the coefficient matrix
 
154
      *@v, a candidate to verify
 
155
      *@return true, for a valid solution
 
156
      **/
 
157
    static bool simultaneousQuadraticVerify(const std::vector<std::vector<double> >& m, const RS_Vector& v);
 
158
    /** wrapper for elliptic integral **/
 
159
    /**
 
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]
 
163
     *
 
164
     *Author: Dongxu Li
 
165
     */
 
166
    static double ellipticIntegral_2(const double& k, const double& phi);
 
167
 
 
168
    static QString doubleToString(double value, double prec);
 
169
    static QString doubleToString(double value, int prec);
 
170
 
 
171
    static void test();
 
172
    };
 
173
 
 
174
#endif