src/basics.cpp

Wed, 30 May 2018 22:31:06 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 30 May 2018 22:31:06 +0300
changeset 1390
3eace926af7f
parent 1389
3fb8ad4d27b1
child 1418
503d4e7e27c9
permissions
-rw-r--r--

added draw plane feature (doesn't work with circle draw quite right yet)

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: 1325
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
b376645315ab - renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
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
1323
05b3e173c900 Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
19 #include <QApplication>
05b3e173c900 Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
20 #include <QSettings>
1318
568fcfc6da71 removed unnecessary files and includes
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
21 #include <QLineF>
568fcfc6da71 removed unnecessary files and includes
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
22 #include "basics.h"
568fcfc6da71 removed unnecessary files and includes
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
23 #include "types/vertex.h"
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
24 #include "format.h"
1323
05b3e173c900 Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
25 #include "version.h"
1036
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
26
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
27 int gcd(int a, int b)
1036
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
28 {
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
29 while (b != 0)
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
30 {
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
31 int temp = a;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
32 a = b;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
33 b = temp % b;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
34 }
1036
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
35
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
36 return a;
1031
55c0d3beea0d removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
37 }
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
38
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
39 void simplify(int& numerator, int& denominator)
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
40 {
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
41 int factor = gcd(numerator, denominator);
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
42 numerator /= factor;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
43 denominator /= factor;
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
44 }
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1177
diff changeset
45
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
46
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
47 QString joinStrings(const QList<StringFormatArg>& values, QString delimeter)
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1177
diff changeset
48 {
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
49 QStringList list;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
50
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
51 for (const StringFormatArg& arg : values)
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
52 list << arg.text();
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1177
diff changeset
53
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
54 return list.join(delimeter);
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
55 }
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
56
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
57
1324
563a9b65777b roundToDecimals no longer needs an lvalue. applyToMatrix removed.
Teemu Piippo <teemu@hecknology.net>
parents: 1323
diff changeset
58 double roundToDecimals(double value, int decimals)
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
59 {
1364
f6a3640b1818 fix warnings
Teemu Piippo <teemu@hecknology.net>
parents: 1352
diff changeset
60 decimals = max(0, decimals);
f6a3640b1818 fix warnings
Teemu Piippo <teemu@hecknology.net>
parents: 1352
diff changeset
61
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
62 if (decimals == 0)
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1177
diff changeset
63 {
1324
563a9b65777b roundToDecimals no longer needs an lvalue. applyToMatrix removed.
Teemu Piippo <teemu@hecknology.net>
parents: 1323
diff changeset
64 return round(value);
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1177
diff changeset
65 }
1364
f6a3640b1818 fix warnings
Teemu Piippo <teemu@hecknology.net>
parents: 1352
diff changeset
66 else
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1177
diff changeset
67 {
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
68 qreal coefficient = pow(10, decimals);
1324
563a9b65777b roundToDecimals no longer needs an lvalue. applyToMatrix removed.
Teemu Piippo <teemu@hecknology.net>
parents: 1323
diff changeset
69 return round(value * coefficient) / coefficient;
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1177
diff changeset
70 }
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1177
diff changeset
71 }
1305
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1181
diff changeset
72
1352
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
73 double log1000(double x)
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
74 {
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
75 return log10(x) / 3.0;
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
76 }
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
77
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
78 /*
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
79 * Returns a string representation of the provided file size.
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
80 */
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
81 QString formatFileSize(qint64 size)
1308
dcc8c02530c2 Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents: 1305
diff changeset
82 {
1352
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
83 static const QString suffixes[] = {
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
84 QObject::tr("bytes"),
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
85 QObject::tr("kB", "kilobytes"),
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
86 QObject::tr("MB", "megabytes"),
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
87 QObject::tr("GB", "gigabytes"),
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
88 QObject::tr("TB", "terabytes"),
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
89 QObject::tr("PB", "petabytes"),
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
90 QObject::tr("EB", "exabytes"),
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
91 QObject::tr("ZB", "zettabytes"),
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
92 QObject::tr("YB", "yottabytes")
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
93 };
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
94
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
95 if (size == 1)
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
96 {
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
97 return QObject::tr("1 byte");
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
98 }
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
99 else if (size > 0)
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
100 {
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
101 // Find out which suffix to use by the use of 1000-base logarithm:
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
102 int magnitude = static_cast<int>(floor(log1000(size)));
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
103 // ensure that magnitude is within bounds (even if we somehow get 1000s of yottabytes)
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
104 magnitude = qBound(0, magnitude, countof(suffixes) - 1);
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
105 // prepare the representation
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
106 return QString::number(size / pow(1000, magnitude), 'g', 3) + " " + suffixes[magnitude];
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
107 }
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
108 else
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
109 {
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
110 return QString::number(size) + " " + suffixes[0];
55fbbd4bdf86 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 1351
diff changeset
111 }
1308
dcc8c02530c2 Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents: 1305
diff changeset
112 }
1323
05b3e173c900 Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
113
05b3e173c900 Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
114 /*
05b3e173c900 Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
115 * Returns a settings object that interfaces the ini file.
05b3e173c900 Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
116 */
05b3e173c900 Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
117 QSettings& settingsObject()
05b3e173c900 Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
118 {
05b3e173c900 Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
119 static QSettings settings {
05b3e173c900 Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
120 qApp->applicationDirPath() + "/" UNIXNAME ".ini",
05b3e173c900 Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
121 QSettings::IniFormat
05b3e173c900 Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
122 };
05b3e173c900 Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
123 return settings;
05b3e173c900 Config is now a namespace
Teemu Piippo <teemu@hecknology.net>
parents: 1319
diff changeset
124 }
1350
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
125
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
126 QString largeNumberRep(int number)
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
127 {
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
128 if (number < 0)
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
129 {
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
130 return "-" + largeNumberRep(-number);
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
131 }
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
132 else
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
133 {
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
134 QString rep;
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
135
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
136 while (number >= 1000)
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
137 {
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
138 rep = " " + QString::number(number % 1000);
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
139 number /= 1000;
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
140 }
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
141
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
142 return QString::number(number) + rep;
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
143 }
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
144 }
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
145
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
146 static const QString superscripts = "⁰¹²³⁴⁵⁶⁷⁸⁹";
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
147 static const QString subscripts = "₀₁₂₃₄₅₆₇₈₉";
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
148
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
149 static QString customNumberRep(int number, const QString& script, const QString& minus)
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
150 {
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
151 if (number < 0)
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
152 {
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
153 return minus + customNumberRep(-number, script, minus);
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
154 }
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
155 else
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
156 {
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
157 QString rep = "";
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
158 for (QChar character : QString::number(number))
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
159 {
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
160 if (character >= '0' and character <= '9')
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
161 rep += script[character.unicode() - '0'];
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
162 else
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
163 rep += character;
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
164 }
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
165 return rep;
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
166 }
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
167 }
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
168
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
169 QString superscript(int number)
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
170 {
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
171 return customNumberRep(number, superscripts, "⁻");
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
172 }
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
173
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
174 QString subscript(int number)
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
175 {
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
176 return customNumberRep(number, subscripts, "₋");
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
177 }
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
178
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
179 QString fractionRep(int numerator, int denominator)
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
180 {
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
181 return superscript(numerator) + "⁄" + subscript(denominator);
eb2d3bc4fc73 better representation of fractions and large numbers
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
182 }
1389
3fb8ad4d27b1 refactored signal blocking
Teemu Piippo <teemu@hecknology.net>
parents: 1364
diff changeset
183
3fb8ad4d27b1 refactored signal blocking
Teemu Piippo <teemu@hecknology.net>
parents: 1364
diff changeset
184 /*
3fb8ad4d27b1 refactored signal blocking
Teemu Piippo <teemu@hecknology.net>
parents: 1364
diff changeset
185 * Temporarily blocks the signals of `object` and calls `function`. Returns whatever `function`
3fb8ad4d27b1 refactored signal blocking
Teemu Piippo <teemu@hecknology.net>
parents: 1364
diff changeset
186 * returns. Assumes that the function does not change the signal blocking status of `object`.
3fb8ad4d27b1 refactored signal blocking
Teemu Piippo <teemu@hecknology.net>
parents: 1364
diff changeset
187 */
3fb8ad4d27b1 refactored signal blocking
Teemu Piippo <teemu@hecknology.net>
parents: 1364
diff changeset
188 void withSignalsBlocked(QObject* object, std::function<void()> function)
3fb8ad4d27b1 refactored signal blocking
Teemu Piippo <teemu@hecknology.net>
parents: 1364
diff changeset
189 {
3fb8ad4d27b1 refactored signal blocking
Teemu Piippo <teemu@hecknology.net>
parents: 1364
diff changeset
190 bool wasBlockingSignals = object->signalsBlocked();
3fb8ad4d27b1 refactored signal blocking
Teemu Piippo <teemu@hecknology.net>
parents: 1364
diff changeset
191 object->blockSignals(true);
3fb8ad4d27b1 refactored signal blocking
Teemu Piippo <teemu@hecknology.net>
parents: 1364
diff changeset
192 function();
3fb8ad4d27b1 refactored signal blocking
Teemu Piippo <teemu@hecknology.net>
parents: 1364
diff changeset
193 object->blockSignals(wasBlockingSignals);
3fb8ad4d27b1 refactored signal blocking
Teemu Piippo <teemu@hecknology.net>
parents: 1364
diff changeset
194 }
1390
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
195
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
196 /*
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
197 * Returns the angle between two vectors
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
198 */
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
199 qreal vectorAngle(const QVector3D& vec_1, const QVector3D& vec_2)
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
200 {
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
201 return acos(QVector3D::dotProduct(vec_1, vec_2) / vec_1.length() / vec_2.length());
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
202 }
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
203
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
204 /*
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
205 * Computes the determinant of a 2×2 matrix with each variable passed in row-major order.
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
206 */
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
207 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
208 {
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
209 return a * d - b * c;
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
210 }
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
211
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
212 /*
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
213 * Computes the determinant of a 3×3 matrix with each variable passed in row-major order.
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
214 */
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
215 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
216 {
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
217 return a*e*i + b*f*g + c*d*h - a*f*h - b*d*i - c*e*g;
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
218 }
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
219
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
220 /*
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
221 * Computes the determinant of a 2×2 matrix.
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
222 */
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
223 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
224 {
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
225 return matrix(0, 0) * matrix(1, 1) - matrix(0, 1) * matrix(1, 0);
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
226 }
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
227
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
228 /*
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
229 * Computes the determinant of a 3×3 matrix.
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
230 */
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
231 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
232 {
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
233 return sum(
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
234 +matrix(0, 0) * matrix(1, 1) * matrix(2, 2),
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
235 -matrix(0, 0) * matrix(1, 2) * matrix(2, 1),
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
236 -matrix(0, 1) * matrix(1, 0) * matrix(2, 2),
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
237 +matrix(0, 1) * matrix(1, 2) * matrix(2, 0),
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
238 +matrix(0, 2) * matrix(1, 0) * matrix(2, 1),
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
239 -matrix(0, 2) * matrix(1, 1) * matrix(2, 0));
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
240 }
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
241
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
242 /*
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
243 * Computes the determinant of a 4×4 matrix.
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
244 */
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
245 qreal determinant(const QMatrix4x4& matrix)
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
246 {
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
247 qreal sum = 0;
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
248
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
249 for (int column : {0, 1, 2, 3})
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
250 {
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
251 int column_1 = (column >= 1) ? 0 : 1;
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
252 int column_2 = (column >= 2) ? 1 : 2;
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
253 int column_3 = (column >= 3) ? 2 : 3;
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
254 sum += ((column % 1) ? -1 : 1) * determinant(
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
255 matrix(1, column_1), matrix(1, column_2), matrix(1, column_3),
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
256 matrix(2, column_1), matrix(2, column_2), matrix(2, column_3),
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
257 matrix(3, column_1), matrix(3, column_2), matrix(3, column_3));
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
258 }
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
259
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
260 return sum;
3eace926af7f added draw plane feature (doesn't work with circle draw quite right yet)
Teemu Piippo <teemu@hecknology.net>
parents: 1389
diff changeset
261 }

mercurial