src/basics.h

Thu, 27 Feb 2020 11:56:41 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 27 Feb 2020 11:56:41 +0200
changeset 57
5c0005f63319
parent 55
cb81ecb5fb23
child 70
f21b800b02a4
permissions
-rw-r--r--

use glm::unProject to implement screenToModelCoordinates

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>
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 #include <QMatrix4x4>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 #include <QObject>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 #include <QPointF>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 #include <QSet>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 #include <QString>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 #include <QStringList>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 #include <QVariant>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 #include <QVector>
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 #include <QVector3D>
33
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
35 #include <glm/glm.hpp>
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 using GLRotationMatrix = QMatrix4x4;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 enum Axis
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 {
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
41 X = 0,
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
42 Y = 1,
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
43 Z = 2
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 };
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
46 enum Result
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
47 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
48 Success = 0,
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
49 PartialSuccess,
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
50 Failure
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
51 };
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 constexpr bool failed(Result r)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
54 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
55 return r == Failure;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
56 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
57
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 enum Winding
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 {
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 NoWinding,
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
61 Anticlockwise,
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 Clockwise,
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 };
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 * Special operator definition that implements the XOR operator for windings.
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 * However, if either winding is NoWinding, then this function returns NoWinding.
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 */
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 inline Winding operator^(Winding one, Winding other)
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 {
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 if (one == NoWinding or other == NoWinding)
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 return NoWinding;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 else
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 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
75 }
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 inline Winding& operator^=(Winding& one, Winding other)
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 {
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 one = one ^ other;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 return one;
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 }
21
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
82
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
83 template<typename T, int N>
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
84 constexpr int countof(T(&)[N])
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
85 {
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
86 return N;
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
87 }
33
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
88
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
89 /**
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
90 * @brief casts @c x to a suitable unsigned integer
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
91 */
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
92 template<typename T>
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
93 constexpr auto unsigned_cast(T x)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
94 -> 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
95 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
96 return static_cast<std::make_unsigned_t<T>>(x);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
97 }
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 * @brief casts @c x to a suitable signed integer
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
101 */
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
102 template<typename T>
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
103 constexpr auto signed_cast(T x)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
104 -> 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
105 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
106 return static_cast<std::make_signed_t<T>>(x);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
107 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
108
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
109
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
110 /**
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
111 * @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
112 * @param[in] x floating point value to cast
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
113 * @returns float
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
114 */
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
115 template<typename T>
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
116 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
117 {
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
118 return static_cast<float>(x);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
119 }
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 /**
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
122 * @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
123 * @param[in] x floating point value to cast
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
124 * @returns double
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
125 */
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
126 template<typename T>
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
127 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
128 {
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
129 return static_cast<double>(x);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
130 }
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
131
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
132 template<int N, typename T, glm::qualifier Q>
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
133 inline QPoint toQPoint(const glm::vec<N, T, Q>& vec)
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
134 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
135 return {static_cast<int>(vec.x), static_cast<int>(vec.y)};
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
136 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
137
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
138 template<int N, typename T, glm::qualifier Q>
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
139 inline QPointF toQPointF(const glm::vec<N, T, Q>& vec)
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
140 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
141 return {toDouble(vec.x), toDouble(vec.y)};
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
142 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
143
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
144 inline glm::vec2 toVec2(const QPoint& point)
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
145 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
146 return {point.x(), point.y()};
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
147 }
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 inline glm::vec2 toVec2(const QPointF& point)
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
150 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
151 return {point.x(), point.y()};
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
33
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
154 Q_DECLARE_METATYPE(glm::vec3)
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
155 Q_DECLARE_METATYPE(glm::mat4)

mercurial