src/basics.h

Thu, 20 Jun 2019 08:54:35 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 20 Jun 2019 08:54:35 +0300
changeset 1440
265b2e95a8e8
parent 1433
bd3a9e237ef5
permissions
-rw-r--r--

uuid things

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>
1428
ece049033adc fixed a crash when trying to open a document for the 3rd time after closing it 2 times
Teemu Piippo <teemu@hecknology.net>
parents: 1419
diff changeset
22 #include <memory>
1380
bc799b965418 added a status bar and moved printed messages there
Teemu Piippo <teemu@hecknology.net>
parents: 1370
diff changeset
23 #include <QDate>
1136
9a7a945c0e93 Rearranged declarations in basics.h
Teemu Piippo <teemu@hecknology.net>
parents: 1135
diff changeset
24 #include <QFile>
1333
ebcb0335d467 removed unused include
Teemu Piippo <teemu@hecknology.net>
parents: 1332
diff changeset
25 #include <QLineF>
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
26 #include <QMatrix4x4>
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
27 #include <QMetaType>
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
28 #include <QObject>
1333
ebcb0335d467 removed unused include
Teemu Piippo <teemu@hecknology.net>
parents: 1332
diff changeset
29 #include <QPointF>
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
30 #include <QSet>
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
31 #include <QString>
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
32 #include <QStringList>
1320
bdb4804bc09c Moved includes, added squared() function
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
33 #include <QVariant>
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
34 #include <QVector>
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
35 #include <QVector3D>
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
36 #include "generics/functions.h"
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
38 #ifndef __GNUC__
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
39 # 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
40 #endif
8d22e1dd272d ported qOverload to drop minimum Qt requirement down to 5.5
Teemu Piippo <teemu@hecknology.net>
parents: 1308
diff changeset
41
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
42 #define DEFINE_CLASS(SELF, SUPER) \
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
43 public: \
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
44 using Self = SELF; \
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
45 using Super = SUPER;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
46
1328
d68d1ce89d05 merged mathfunctions.cpp into algorithms/geometry.cpp
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
47 class LDObject;
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
48
1440
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1433
diff changeset
49 class Uuid
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1433
diff changeset
50 {
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1433
diff changeset
51 public:
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1433
diff changeset
52 constexpr Uuid() : a{0}, b{0} {}
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1433
diff changeset
53 Uuid(Uuid&&) = default;
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1433
diff changeset
54 Uuid(const Uuid&) = default;
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1433
diff changeset
55 QString toString() const;
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1433
diff changeset
56 Uuid& operator++();
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1433
diff changeset
57
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1433
diff changeset
58 private:
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1433
diff changeset
59 quint64 a;
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1433
diff changeset
60 quint64 b;
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1433
diff changeset
61 };
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1433
diff changeset
62
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1433
diff changeset
63 constexpr Uuid NULL_UUID = {};
265b2e95a8e8 uuid things
Teemu Piippo <teemu@hecknology.net>
parents: 1433
diff changeset
64
655
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 enum Axis
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 {
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67 X,
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 Y,
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 Z
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70 };
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71
1305
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1291
diff changeset
72 enum Winding
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1291
diff changeset
73 {
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1291
diff changeset
74 NoWinding,
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1291
diff changeset
75 CounterClockwise,
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1291
diff changeset
76 Clockwise,
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1291
diff changeset
77 };
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1291
diff changeset
78
1417
ed39bfca7a67 refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents: 1404
diff changeset
79 struct CircularSection
ed39bfca7a67 refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents: 1404
diff changeset
80 {
ed39bfca7a67 refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents: 1404
diff changeset
81 int segments = 16;
ed39bfca7a67 refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents: 1404
diff changeset
82 int divisions = 16;
ed39bfca7a67 refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents: 1404
diff changeset
83 };
ed39bfca7a67 refactored the segments/divisions editor in MainWindow to a new widget
Teemu Piippo <teemu@hecknology.net>
parents: 1404
diff changeset
84
1418
503d4e7e27c9 used the new CircularSectionEditor in CircularPrimitiveEditor
Teemu Piippo <teemu@hecknology.net>
parents: 1417
diff changeset
85 Q_DECLARE_METATYPE(CircularSection)
503d4e7e27c9 used the new CircularSectionEditor in CircularPrimitiveEditor
Teemu Piippo <teemu@hecknology.net>
parents: 1417
diff changeset
86 bool operator==(const CircularSection& one, const CircularSection& other);
503d4e7e27c9 used the new CircularSectionEditor in CircularPrimitiveEditor
Teemu Piippo <teemu@hecknology.net>
parents: 1417
diff changeset
87 bool operator!=(const CircularSection& one, const CircularSection& other);
503d4e7e27c9 used the new CircularSectionEditor in CircularPrimitiveEditor
Teemu Piippo <teemu@hecknology.net>
parents: 1417
diff changeset
88
1319
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 * Special operator definition that implements the XOR operator for windings.
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
91 * However, if either winding is NoWinding, then this function returns NoWinding.
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
92 */
1430
6ce6d3da584f refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1428
diff changeset
93 Winding constexpr operator^(Winding one, Winding other)
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
94 {
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
95 if (one == NoWinding or other == NoWinding)
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
96 return NoWinding;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
97 else
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
98 return static_cast<Winding>(static_cast<int>(one) ^ static_cast<int>(other));
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
99 }
1305
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1291
diff changeset
100
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
101 inline Winding& operator^=(Winding& one, Winding other)
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
102 {
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
103 one = one ^ other;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
104 return one;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
105 }
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
106
1430
6ce6d3da584f refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1428
diff changeset
107 constexpr double pi = 3.14159265358979323846;
6ce6d3da584f refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1428
diff changeset
108 constexpr double inf = std::numeric_limits<double>::infinity();
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
109
1428
ece049033adc fixed a crash when trying to open a document for the 3rd time after closing it 2 times
Teemu Piippo <teemu@hecknology.net>
parents: 1419
diff changeset
110 template<typename T>
ece049033adc fixed a crash when trying to open a document for the 3rd time after closing it 2 times
Teemu Piippo <teemu@hecknology.net>
parents: 1419
diff changeset
111 unsigned int qHash(const std::unique_ptr<T>& pointer)
ece049033adc fixed a crash when trying to open a document for the 3rd time after closing it 2 times
Teemu Piippo <teemu@hecknology.net>
parents: 1419
diff changeset
112 {
ece049033adc fixed a crash when trying to open a document for the 3rd time after closing it 2 times
Teemu Piippo <teemu@hecknology.net>
parents: 1419
diff changeset
113 return qHash(pointer.get());
ece049033adc fixed a crash when trying to open a document for the 3rd time after closing it 2 times
Teemu Piippo <teemu@hecknology.net>
parents: 1419
diff changeset
114 }
ece049033adc fixed a crash when trying to open a document for the 3rd time after closing it 2 times
Teemu Piippo <teemu@hecknology.net>
parents: 1419
diff changeset
115
1430
6ce6d3da584f refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1428
diff changeset
116 inline void offset(QMatrix4x4& matrix, const QVector3D& vector)
6ce6d3da584f refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1428
diff changeset
117 {
6ce6d3da584f refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1428
diff changeset
118 matrix(0, 3) += vector.x();
6ce6d3da584f refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1428
diff changeset
119 matrix(1, 3) += vector.y();
6ce6d3da584f refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1428
diff changeset
120 matrix(2, 3) += vector.z();
6ce6d3da584f refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1428
diff changeset
121 }
6ce6d3da584f refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1428
diff changeset
122
1433
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents: 1431
diff changeset
123 template<typename... Ts>
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents: 1431
diff changeset
124 inline void ignore(Ts&&...) {}
bd3a9e237ef5 reimplented shortcuts in the config dialog using model/view programming
Teemu Piippo <teemu@hecknology.net>
parents: 1431
diff changeset
125
1390
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
126 qreal determinant(qreal a, qreal b, qreal c, qreal d);
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
127 qreal determinant(qreal a, qreal b, qreal c, qreal d, qreal e, qreal f, qreal g, qreal h, qreal i);
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
128 qreal determinant(const QMatrix2x2& matrix);
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
129 qreal determinant(const QMatrix3x3& matrix);
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
130 qreal determinant(const QMatrix4x4& matrix);
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
131 QString formatFileSize(qint64 size);
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
132 int gcd(int a, int b);
1419
f7c53002a990 replaced uses of QList with QVector
Teemu Piippo <teemu@hecknology.net>
parents: 1418
diff changeset
133 QString joinStrings(const QVector<class StringFormatArg>& values, QString delimeter = " ");
1350
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1333
diff changeset
134 QString largeNumberRep(int number);
1352
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1350
diff changeset
135 double log1000(double x);
1324
563a9b65777b roundToDecimals no longer needs an lvalue. applyToMatrix removed.
Teemu Piippo <teemu@hecknology.net>
parents: 1323
diff changeset
136 double roundToDecimals(double value, int decimals);
1323
05b3e173c900 Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1320
diff changeset
137 class QSettings& settingsObject();
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
138 void simplify(int& numerator, int& denominator);
1350
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1333
diff changeset
139 QString superscript(int number);
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1333
diff changeset
140 QString subscript(int number);
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1333
diff changeset
141 QString fractionRep(int numerator, int denominator);
1390
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
142 qreal vectorAngle(const QVector3D& vec_1, const QVector3D& vec_2);
1389
3fb8ad4d27b1 refactored signal blocking
Teemu Piippo <teemu@hecknology.net>
parents: 1380
diff changeset
143 void withSignalsBlocked(QObject* object, std::function<void()> function);

mercurial