src/basics.h

Sat, 24 Mar 2018 13:03:02 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 24 Mar 2018 13:03:02 +0200
changeset 1328
d68d1ce89d05
parent 1326
69a90bd2dba2
child 1332
1fffb24d4ffe
permissions
-rw-r--r--

merged mathfunctions.cpp into algorithms/geometry.cpp

655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
1326
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1324
diff changeset
3 * Copyright (C) 2013 - 2018 Teemu Piippo
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1006
a6b462051ae0 Copyright header fix, some minor stuff
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1002
diff changeset
8 * (at your option) any later version.
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 *
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
19 #pragma once
1320
bdb4804bc09c Moved includes, added squared() function
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
20 #include <cstdio>
bdb4804bc09c Moved includes, added squared() function
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
21 #include <cstdlib>
1136
9a7a945c0e93 Rearranged declarations in basics.h
Teemu Piippo <teemu@hecknology.net>
parents: 1135
diff changeset
22 #include <QFile>
1177
8661b9237ed5 Added support for ideal co-ordinates, which should make writing editing tools a lot easier. For some reason they don't yet work with the back and right cameras.
Teemu Piippo <teemu@hecknology.net>
parents: 1162
diff changeset
23 #include <QMatrix4x4>
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
24 #include <QMetaType>
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
25 #include <QObject>
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
26 #include <QSet>
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
27 #include <QString>
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
28 #include <QStringList>
1320
bdb4804bc09c Moved includes, added squared() function
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
29 #include <QTextFormat>
bdb4804bc09c Moved includes, added squared() function
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
30 #include <QVariant>
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
31 #include <QVector>
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
32 #include <QVector3D>
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
33 #include "generics/functions.h"
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
35 #ifndef __GNUC__
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
36 # define __attribute__(X)
1311
8d22e1dd272d ported qOverload to drop minimum Qt requirement down to 5.5
Teemu Piippo <teemu@hecknology.net>
parents: 1308
diff changeset
37 #endif
8d22e1dd272d ported qOverload to drop minimum Qt requirement down to 5.5
Teemu Piippo <teemu@hecknology.net>
parents: 1308
diff changeset
38
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
39 #define DEFINE_CLASS(SELF, SUPER) \
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
40 public: \
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
41 using Self = SELF; \
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
42 using Super = SUPER;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
43
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
44 // TODO: get rid of this
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
45 #ifdef WIN32
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
46 # define DIRSLASH "\\"
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
47 # define DIRSLASH_CHAR '\\'
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
48 #else // WIN32
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
49 # define DIRSLASH "/"
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
50 # define DIRSLASH_CHAR '/'
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
51 #endif // WIN32
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
52
1328
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
53 class LDObject;
1177
8661b9237ed5 Added support for ideal co-ordinates, which should make writing editing tools a lot easier. For some reason they don't yet work with the back and right cameras.
Teemu Piippo <teemu@hecknology.net>
parents: 1162
diff changeset
54 using GLRotationMatrix = QMatrix4x4;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 enum Axis
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 X,
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59 Y,
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 Z
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 };
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62
1305
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1291
diff changeset
63 enum Winding
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1291
diff changeset
64 {
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1291
diff changeset
65 NoWinding,
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1291
diff changeset
66 CounterClockwise,
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1291
diff changeset
67 Clockwise,
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1291
diff changeset
68 };
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1291
diff changeset
69
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
70 /*
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
71 * Special operator definition that implements the XOR operator for windings.
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
72 * However, if either winding is NoWinding, then this function returns NoWinding.
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
73 */
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
74 inline Winding operator^(Winding one, Winding other)
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
75 {
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
76 if (one == NoWinding or other == NoWinding)
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
77 return NoWinding;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
78 else
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
79 return static_cast<Winding>(static_cast<int>(one) ^ static_cast<int>(other));
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
80 }
1305
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1291
diff changeset
81
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
82 inline Winding& operator^=(Winding& one, Winding other)
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
83 {
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
84 one = one ^ other;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
85 return one;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
86 }
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
87
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
88 static const double pi = 3.14159265358979323846;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
89
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
90 /*
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
91 * Returns the norm of a vector.
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
92 */
1064
4c7a353cf583 Replaced '.length()' with 'length()' where appropriate
Teemu Piippo <teemu@hecknology.net>
parents: 1063
diff changeset
93 static inline qreal abs(const QVector3D &vector)
4c7a353cf583 Replaced '.length()' with 'length()' where appropriate
Teemu Piippo <teemu@hecknology.net>
parents: 1063
diff changeset
94 {
4c7a353cf583 Replaced '.length()' with 'length()' where appropriate
Teemu Piippo <teemu@hecknology.net>
parents: 1063
diff changeset
95 return vector.length();
4c7a353cf583 Replaced '.length()' with 'length()' where appropriate
Teemu Piippo <teemu@hecknology.net>
parents: 1063
diff changeset
96 }
4c7a353cf583 Replaced '.length()' with 'length()' where appropriate
Teemu Piippo <teemu@hecknology.net>
parents: 1063
diff changeset
97
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
98 QString formatFileSize(qint64 size);
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
99 int gcd(int a, int b);
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
100 QString joinStrings(const QList<class StringFormatArg>& values, QString delimeter = " ");
1324
563a9b65777b roundToDecimals no longer needs an lvalue. applyToMatrix removed.
Teemu Piippo <teemu@hecknology.net>
parents: 1323
diff changeset
101 double roundToDecimals(double value, int decimals);
1323
05b3e173c900 Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1320
diff changeset
102 class QSettings& settingsObject();
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
103 void simplify(int& numerator, int& denominator);

mercurial