src/Misc.h

Wed, 05 Mar 2014 12:47:00 +0200

author
Santeri Piippo <crimsondusk64@gmail.com>
date
Wed, 05 Mar 2014 12:47:00 +0200
changeset 644
93dcd1a0e4bd
parent 642
751a8df42842
permissions
-rw-r--r--

- replaced the fmt/log/dlog/fprint macros in favor of template functions. renamed to format, print, dprint and fprint respectively.

629
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
1 /*
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2013, 2014 Santeri Piippo
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
4 *
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
8 * (at your option) any later version.
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
9 *
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
13 * GNU General Public License for more details.
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
14 *
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
17 */
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
18
641
425b169a82aa - changed header guards into #pragma once
Santeri Piippo <crimsondusk64@gmail.com>
parents: 639
diff changeset
19 #pragma once
629
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
20 #include <QVector>
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
21 #include "Configuration.h"
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
22 #include "Main.h"
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
23 #include "Types.h"
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
24
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
25 #define NUM_PRIMES 500
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
26
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
27 class LDDocument;
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
28 class QColor;
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
29 class QAction;
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
30
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
31 // Prime numbers
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
32 extern const int g_primes[NUM_PRIMES];
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
33
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
34 // Returns whether a given string represents a floating point number.
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
35 bool numeric (const QString& tok);
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
36
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
37 // Simplifies the given fraction.
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
38 void simplify (int& numer, int& denom);
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
39
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
40 void roundToDecimals (double& a, int decimals);
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
41
644
93dcd1a0e4bd - replaced the fmt/log/dlog/fprint macros in favor of template functions. renamed to format, print, dprint and fprint respectively.
Santeri Piippo <crimsondusk64@gmail.com>
parents: 642
diff changeset
42 QString join (QList< StringFormatArg > vals, QString delim = " ");
629
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
43
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
44 // Grid stuff
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
45 struct gridinfo
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
46 {
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
47 const char* const name;
642
751a8df42842 - migrated the codebase to use a simpler PROPERTY macro, arrrrrrrrrrrrrrrrrrrrrgh
Santeri Piippo <crimsondusk64@gmail.com>
parents: 641
diff changeset
48 float* const confs[4];
629
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
49 };
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
50
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
51 extern_cfg (Int, grid);
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
52 static const int g_NumGrids = 3;
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
53 extern const gridinfo g_GridInfo[3];
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
54
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
55 inline const gridinfo& currentGrid()
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
56 {
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
57 return g_GridInfo[grid];
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
58 }
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
59
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
60 // =============================================================================
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
61 enum ERotationPoint
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
62 {
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
63 EObjectOrigin,
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
64 EWorldOrigin,
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
65 ECustomPoint
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
66 };
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
67
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
68 Vertex rotPoint (const LDObjectList& objs);
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
69 void configRotationPoint();
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
70
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
71 // =============================================================================
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
72 namespace Grid
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
73 {
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
74 enum Type
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
75 {
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
76 Coarse,
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
77 Medium,
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
78 Fine
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
79 };
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
80
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
81 enum Config
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
82 {
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
83 X,
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
84 Y,
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
85 Z,
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
86 Angle
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
87 };
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
88
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
89 double snap (double value, const Grid::Config axis);
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
90 }
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
91
639
851634b85893 - unified separator styles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 629
diff changeset
92 // =============================================================================
629
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
93 // Plural expression
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
94 template<class T> static inline const char* plural (T n)
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
95 {
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
96 return (n != 1) ? "s" : "";
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
97 }
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
98
639
851634b85893 - unified separator styles
Santeri Piippo <crimsondusk64@gmail.com>
parents: 629
diff changeset
99 // =============================================================================
629
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
100 // Templated clamp
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
101 template<class T> static inline T clamp (T a, T min, T max)
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
102 {
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
103 return (a > max) ? max : (a < min) ? min : a;
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
104 }
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
105
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
106 // Templated minimum
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
107 template<class T> static inline T min (T a, T b)
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
108 {
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
109 return (a < b) ? a : b;
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
110 }
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
111
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
112 // Templated maximum
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
113 template<class T> static inline T max (T a, T b)
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
114 {
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
115 return (a > b) ? a : b;
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
116 }
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
117
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
118 // Templated absolute value
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
119 template<class T> static inline T abs (T a)
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
120 {
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
121 return (a >= 0) ? a : -a;
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
122 }
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
123
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
124 template<class T> inline bool isZero (T a)
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
125 {
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
126 return abs<T> (a) < 0.0001;
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
127 }
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
128
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
129 template<class T> inline bool isInteger (T a)
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
130 {
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
131 return isZero (a - (int) a);
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
132 }
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
133
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
134 template<class T> void removeDuplicates (QList<T>& a)
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
135 {
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
136 std::sort (a.begin(), a.end());
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
137 a.erase (std::unique (a.begin(), a.end()), a.end());
b75c6cce02e2 - refactored filenames
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff changeset
138 }

mercurial