src/basics.cpp

Sat, 24 Mar 2018 11:57:24 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 24 Mar 2018 11:57:24 +0200
changeset 1319
39d7a9642eea
parent 1318
568fcfc6da71
child 1323
05b3e173c900
permissions
-rw-r--r--

reorganized headers

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
1072
9ce9496427f2 Happy new year 2017!
Teemu Piippo <teemu@hecknology.net>
parents: 1068
diff changeset
3 * Copyright (C) 2013 - 2017 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
1318
568fcfc6da71 removed unnecessary files and includes
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
19 #include <QLineF>
568fcfc6da71 removed unnecessary files and includes
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
20 #include "basics.h"
568fcfc6da71 removed unnecessary files and includes
Teemu Piippo <teemu@hecknology.net>
parents: 1315
diff changeset
21 #include "types/vertex.h"
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
22 #include "format.h"
1036
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
23
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
24 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
25 {
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
26 while (b != 0)
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
27 {
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
28 int temp = a;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
29 a = b;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
30 b = temp % b;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
31 }
1036
993c46d7eb75 Replaced the ugly for_enum macro with a generator class
Teemu Piippo <teemu@compsta2.com>
parents: 1031
diff changeset
32
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
33 return a;
1031
55c0d3beea0d removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents: 1014
diff changeset
34 }
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
35
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
36
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
37 void simplify(int& numerator, int& denominator)
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 int factor = gcd(numerator, denominator);
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
40 numerator /= factor;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
41 denominator /= factor;
1053
2a48c0fff107 Refactor primitives
Teemu Piippo <teemu@hecknology.net>
parents: 1037
diff changeset
42 }
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1177
diff changeset
43
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
44
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
45 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
46 {
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
47 QStringList list;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
48
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
49 for (const StringFormatArg& arg : values)
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
50 list << arg.text();
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1177
diff changeset
51
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
52 return list.join(delimeter);
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
53 }
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
54
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 void roundToDecimals(double& value, int decimals)
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
57 {
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
58 if (decimals == 0)
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1177
diff changeset
59 {
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
60 value = round(value);
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1177
diff changeset
61 }
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
62 else if (decimals > 0)
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1177
diff changeset
63 {
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
64 qreal coefficient = pow(10, decimals);
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
65 value = round(value * coefficient) / coefficient;
1181
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1177
diff changeset
66 }
ca6d0ef9aadb Added polar grid rendering (which is disabled for now).
Teemu Piippo <teemu@hecknology.net>
parents: 1177
diff changeset
67 }
1305
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1181
diff changeset
68
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
69
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
70 void applyToMatrix(Matrix& a, ApplyToMatrixFunction func)
1305
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1181
diff changeset
71 {
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
72 for (int i = 0; i < 9; ++i)
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
73 func(i, a.value(i));
1305
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1181
diff changeset
74 }
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1181
diff changeset
75
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
76 void applyToMatrix(const Matrix& a, ApplyToMatrixConstFunction func)
1305
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1181
diff changeset
77 {
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
78 for (int i = 0; i < 9; ++i)
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
79 func(i, a.value(i));
1305
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1181
diff changeset
80 }
1308
dcc8c02530c2 Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents: 1305
diff changeset
81
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
82 QString formatFileSize(qint64 size)
1308
dcc8c02530c2 Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents: 1305
diff changeset
83 {
1319
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
84 static const QString suffixes[] = {" bytes", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
85 int magnitude = (size > 0) ? floor(log10(size) / 3.0 + 1e-10) : 0;
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
86 magnitude = qBound(0, magnitude, countof(suffixes) - 1);
39d7a9642eea reorganized headers
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
87 return QString::number(size / pow(1000, magnitude)) + suffixes[magnitude];
1308
dcc8c02530c2 Begin rework to add support for multiple libraries
Teemu Piippo <teemu@hecknology.net>
parents: 1305
diff changeset
88 }

mercurial