src/misc.h

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

mercurial