src/misc.h

changeset 603
47e7773c7841
parent 600
209e3f1f7b2c
child 604
01bdac75994a
equal deleted inserted replaced
602:ac1744536b33 603:47e7773c7841
43 43
44 str join (initlist<StringFormatArg> vals, str delim = " "); 44 str join (initlist<StringFormatArg> vals, str delim = " ");
45 45
46 // Grid stuff 46 // Grid stuff
47 struct gridinfo 47 struct gridinfo
48 { const char* const name; 48 {
49 const char* const name;
49 float* const confs[4]; 50 float* const confs[4];
50 }; 51 };
51 52
52 extern_cfg (Int, grid); 53 extern_cfg (Int, grid);
53 static const int g_NumGrids = 3; 54 static const int g_NumGrids = 3;
54 extern const gridinfo g_GridInfo[3]; 55 extern const gridinfo g_GridInfo[3];
55 56
56 inline const gridinfo& currentGrid() 57 inline const gridinfo& currentGrid()
57 { return g_GridInfo[grid]; 58 {
59 return g_GridInfo[grid];
58 } 60 }
59 61
60 // ============================================================================= 62 // =============================================================================
61 enum RotationPoint 63 enum RotationPoint
62 { ObjectOrigin, 64 {
65 ObjectOrigin,
63 WorldOrigin, 66 WorldOrigin,
64 CustomPoint 67 CustomPoint
65 }; 68 };
66 69
67 vertex rotPoint (const QList<LDObject*>& objs); 70 vertex rotPoint (const QList<LDObject*>& objs);
68 void configRotationPoint(); 71 void configRotationPoint();
69 72
70 // ============================================================================= 73 // =============================================================================
71 namespace Grid 74 namespace Grid
72 { enum Type 75 {
73 { Coarse, 76 enum Type
77 {
78 Coarse,
74 Medium, 79 Medium,
75 Fine 80 Fine
76 }; 81 };
77 82
78 enum Config 83 enum Config
79 { X, 84 {
85 X,
80 Y, 86 Y,
81 Z, 87 Z,
82 Angle 88 Angle
83 }; 89 };
84 90
85 double snap (double value, const Grid::Config axis); 91 double snap (double value, const Grid::Config axis);
86 } 92 }
87 93
88 // ----------------------------------------------------------------------------- 94 // -----------------------------------------------------------------------------
89 class InvokationDeferer : public QObject 95 class InvokationDeferer : public QObject
90 { Q_OBJECT 96 {
97 Q_OBJECT
91 98
92 public: 99 public:
93 using FunctionType = void(*)(); 100 using FunctionType = void(*)();
94 101
95 explicit InvokationDeferer (QObject* parent = 0); 102 explicit InvokationDeferer (QObject* parent = 0);
108 void invokeLater (InvokationDeferer::FunctionType func); 115 void invokeLater (InvokationDeferer::FunctionType func);
109 116
110 // ----------------------------------------------------------------------------- 117 // -----------------------------------------------------------------------------
111 // Plural expression 118 // Plural expression
112 template<class T> static inline const char* plural (T n) 119 template<class T> static inline const char* plural (T n)
113 { return (n != 1) ? "s" : ""; 120 {
121 return (n != 1) ? "s" : "";
114 } 122 }
115 123
116 // ----------------------------------------------------------------------------- 124 // -----------------------------------------------------------------------------
117 // Templated clamp 125 // Templated clamp
118 template<class T> static inline T clamp (T a, T min, T max) 126 template<class T> static inline T clamp (T a, T min, T max)
119 { return (a > max) ? max : (a < min) ? min : a; 127 {
128 return (a > max) ? max : (a < min) ? min : a;
120 } 129 }
121 130
122 // Templated minimum 131 // Templated minimum
123 template<class T> static inline T min (T a, T b) 132 template<class T> static inline T min (T a, T b)
124 { return (a < b) ? a : b; 133 {
134 return (a < b) ? a : b;
125 } 135 }
126 136
127 // Templated maximum 137 // Templated maximum
128 template<class T> static inline T max (T a, T b) 138 template<class T> static inline T max (T a, T b)
129 { return (a > b) ? a : b; 139 {
140 return (a > b) ? a : b;
130 } 141 }
131 142
132 // Templated absolute value 143 // Templated absolute value
133 template<class T> static inline T abs (T a) 144 template<class T> static inline T abs (T a)
134 { return (a >= 0) ? a : -a; 145 {
146 return (a >= 0) ? a : -a;
135 } 147 }
136 148
137 template<class T> inline bool isZero (T a) 149 template<class T> inline bool isZero (T a)
138 { return abs<T> (a) < 0.0001; 150 {
151 return abs<T> (a) < 0.0001;
139 } 152 }
140 153
141 template<class T> inline bool isInteger (T a) 154 template<class T> inline bool isInteger (T a)
142 { return isZero (a - (int) a); 155 {
156 return isZero (a - (int) a);
143 } 157 }
144 158
145 template<class T> void removeDuplicates (QList<T>& a) 159 template<class T> void removeDuplicates (QList<T>& a)
146 { std::sort (a.begin(), a.end()); 160 {
161 std::sort (a.begin(), a.end());
147 a.erase (std::unique (a.begin(), a.end()), a.end()); 162 a.erase (std::unique (a.begin(), a.end()), a.end());
148 } 163 }
149 164
150 #endif // LDFORGE_MISC_H 165 #endif // LDFORGE_MISC_H

mercurial