src/basics.h

Wed, 25 May 2022 20:36:34 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 25 May 2022 20:36:34 +0300
changeset 199
6988973515d2
parent 196
6bcb284679d4
child 206
654661eab7f3
permissions
-rw-r--r--

Fix pick() picking from weird places on the screen with high DPI scaling

glReadPixels reads data from the frame buffer, which contains data after
high DPI scaling, so any reads to that need to take this scaling into account

3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #pragma once
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
20 #include <algorithm>
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 #include <cstdio>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 #include <cstdlib>
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
23 #include <cstring>
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 #include <cmath>
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
25 #include <optional>
96
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
26 #include <type_traits>
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 #include <QMatrix4x4>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 #include <QObject>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 #include <QPointF>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 #include <QSet>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 #include <QString>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 #include <QStringList>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 #include <QVariant>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 #include <QVector>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 #include <QVector3D>
33
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
36 #include <glm/glm.hpp>
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 using GLRotationMatrix = QMatrix4x4;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 enum Axis
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 {
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
42 X = 0,
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
43 Y = 1,
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
44 Z = 2
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 };
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
47 enum Result
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
48 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
49 Success = 0,
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
50 PartialSuccess,
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
51 Failure
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
52 };
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
53
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
54 constexpr bool failed(Result r)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
55 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
56 return r == Failure;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
57 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
58
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 enum Winding
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 {
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 NoWinding,
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
62 Anticlockwise,
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 Clockwise,
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 };
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 /*
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 * Special operator definition that implements the XOR operator for windings.
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 * However, if either winding is NoWinding, then this function returns NoWinding.
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 */
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 inline Winding operator^(Winding one, Winding other)
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 {
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 if (one == NoWinding or other == NoWinding)
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 return NoWinding;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 else
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 return static_cast<Winding>(static_cast<int>(one) ^ static_cast<int>(other));
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 }
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 inline Winding& operator^=(Winding& one, Winding other)
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 {
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 one = one ^ other;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 return one;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 }
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
83
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
84 template<typename T, int N>
90
e234edb5e613 remove dependency on glut, fixes
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
85 constexpr int countof(T const (&)[N])
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
86 {
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
87 return N;
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
88 }
33
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
89
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
90 /**
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
91 * @brief casts @c x to a suitable unsigned integer
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
92 */
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
93 template<typename T>
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
94 constexpr auto unsigned_cast(T x)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
95 -> std::enable_if_t<std::is_integral_v<T>, std::make_unsigned_t<T>>
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
96 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
97 return static_cast<std::make_unsigned_t<T>>(x);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
98 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
99
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
100 /**
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
101 * @brief casts @c x to a suitable signed integer
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
102 */
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
103 template<typename T>
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
104 constexpr auto signed_cast(T x)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
105 -> std::enable_if_t<std::is_integral_v<T>, std::make_signed_t<T>>
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
106 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
107 return static_cast<std::make_signed_t<T>>(x);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
108 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
109
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
110
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
111 /**
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
112 * @brief casts floating point values to float, converting non-floating point values causes an error
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
113 * @param[in] x floating point value to cast
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
114 * @returns float
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
115 */
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
116 template<typename T>
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
117 auto toFloat(T x) -> std::enable_if_t<std::is_floating_point_v<T>, float>
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
118 {
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
119 return static_cast<float>(x);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
120 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
121
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
122 /**
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
123 * @brief casts floating point values to double, converting non-floating point values causes an error
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
124 * @param[in] x floating point value to cast
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
125 * @returns double
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
126 */
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
127 template<typename T>
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
128 auto toDouble(T x) -> std::enable_if_t<std::is_floating_point_v<T>, double>
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
129 {
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
130 return static_cast<double>(x);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
131 }
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
132
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
133 /**
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
134 * @brief casts floating point values to qreal, converting non-floating point values causes an error
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
135 * @param[in] x floating point value to cast
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
136 * @returns qreal
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
137 */
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
138 template<typename T>
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
139 auto toQreal(T x) -> std::enable_if_t<std::is_floating_point_v<T>, qreal>
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
140 {
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
141 return static_cast<qreal>(x);
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
142 }
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 70
diff changeset
143
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
144 template<int N, typename T, glm::qualifier Q>
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
145 inline QPoint toQPoint(const glm::vec<N, T, Q>& vec)
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
146 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
147 return {static_cast<int>(vec.x), static_cast<int>(vec.y)};
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
148 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
149
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
150 template<int N, typename T, glm::qualifier Q>
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
151 inline QPointF toQPointF(const glm::vec<N, T, Q>& vec)
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
152 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
153 return {toDouble(vec.x), toDouble(vec.y)};
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
154 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
155
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
156 inline glm::vec2 toVec2(const QPoint& point)
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
157 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
158 return {point.x(), point.y()};
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
159 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
160
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
161 inline glm::vec2 toVec2(const QPointF& point)
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
162 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
163 return {point.x(), point.y()};
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
164 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
165
96
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
166 /*
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
167 * coalesce(arg1, arg2, ..., argn)
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
168 * Returns the first of the given arguments that evaluates to true.
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
169 */
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
170 template<typename T>
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
171 T coalesce(T&& arg)
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
172 {
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
173 // recursion base: 1 argument
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
174 return arg;
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
175 }
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
176
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
177 template<typename T, typename... Rest>
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
178 std::common_type_t<T, Rest...> coalesce(T&& arg, Rest&&... rest)
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
179 {
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
180 // general case: n arguments
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
181 return arg ? arg : coalesce(rest...);
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
182 }
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
183
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
184 /**
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
185 * @brief Finds an element in a map and possibly returns a reference to it if find
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
186 * @param map
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
187 * @param key
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
188 * @returns the value or nullptr if not found
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
189 */
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
190 template<typename T, typename R, typename K>
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
191 const R* findInMap(const std::map<T, R>& map, K&& key)
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
192 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
193 auto pair = map.find(key);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
194 if (pair != map.end())
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
195 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
196 return &pair->second;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
197 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
198 else
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
199 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
200 return nullptr;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
201 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
202 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
203
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
204 /**
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
205 * @brief Finds an element in a map and possibly returns a reference to it if find
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
206 * @param map
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
207 * @param key
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
208 * @returns the value or no value if not found
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
209 */
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
210 template<typename T, typename R, typename K>
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
211 R* findInMap(std::map<T, R>& map, K&& key)
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
212 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
213 auto pair = map.find(key);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
214 if (pair != map.end())
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
215 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
216 return &pair->second;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
217 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
218 else
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
219 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
220 return nullptr;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
221 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
222 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
223
191
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
224 template<typename T = float>
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
225 constexpr std::enable_if_t<std::is_floating_point_v<T>, T> pi = static_cast<T>(M_PIl);
196
6bcb284679d4 delete unneeded things
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
226 constexpr double infinity = std::numeric_limits<double>::infinity();
6bcb284679d4 delete unneeded things
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
227
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
228
33
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
229 Q_DECLARE_METATYPE(glm::vec3)
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
230 Q_DECLARE_METATYPE(glm::mat4)

mercurial