src/miscallenous.h

changeset 655
b376645315ab
child 733
cc39df788660
equal deleted inserted replaced
654:a74f2ff353b8 655:b376645315ab
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 #pragma once
20 #include <QVector>
21 #include "configuration.h"
22 #include "main.h"
23 #include "basics.h"
24
25 #define NUM_PRIMES 500
26
27 class LDDocument;
28 class QColor;
29 class QAction;
30
31 // Prime numbers
32 extern const int g_primes[NUM_PRIMES];
33
34 // Returns whether a given string represents a floating point number.
35 bool numeric (const QString& tok);
36
37 // Simplifies the given fraction.
38 void simplify (int& numer, int& denom);
39
40 void roundToDecimals (double& a, int decimals);
41
42 QString join (QList< StringFormatArg > vals, QString delim = " ");
43
44 // Grid stuff
45 struct gridinfo
46 {
47 const char* const name;
48 float* const confs[4];
49 };
50
51 extern_cfg (Int, grid);
52 static const int g_NumGrids = 3;
53 extern const gridinfo g_GridInfo[3];
54
55 inline const gridinfo& currentGrid()
56 {
57 return g_GridInfo[grid];
58 }
59
60 // =============================================================================
61 enum ERotationPoint
62 {
63 EObjectOrigin,
64 EWorldOrigin,
65 ECustomPoint
66 };
67
68 Vertex rotPoint (const LDObjectList& objs);
69 void configRotationPoint();
70
71 // =============================================================================
72 namespace Grid
73 {
74 enum Type
75 {
76 Coarse,
77 Medium,
78 Fine
79 };
80
81 enum Config
82 {
83 X,
84 Y,
85 Z,
86 Angle
87 };
88
89 double snap (double value, const Grid::Config axis);
90 }
91
92 // =============================================================================
93 // Plural expression
94 template<class T> static inline const char* plural (T n)
95 {
96 return (n != 1) ? "s" : "";
97 }
98
99 // =============================================================================
100 // Templated clamp
101 template<class T> static inline T clamp (T a, T min, T max)
102 {
103 return (a > max) ? max : (a < min) ? min : a;
104 }
105
106 // Templated minimum
107 template<class T> static inline T min (T a, T b)
108 {
109 return (a < b) ? a : b;
110 }
111
112 // Templated maximum
113 template<class T> static inline T max (T a, T b)
114 {
115 return (a > b) ? a : b;
116 }
117
118 // Templated absolute value
119 template<class T> static inline T abs (T a)
120 {
121 return (a >= 0) ? a : -a;
122 }
123
124 template<class T> inline bool isZero (T a)
125 {
126 return abs<T> (a) < 0.0001;
127 }
128
129 template<class T> inline bool isInteger (T a)
130 {
131 return isZero (a - (int) a);
132 }
133
134 template<class T> void removeDuplicates (QList<T>& a)
135 {
136 std::sort (a.begin(), a.end());
137 a.erase (std::unique (a.begin(), a.end()), a.end());
138 }

mercurial