Thu, 27 Feb 2020 11:56:41 +0200
use glm::unProject to implement screenToModelCoordinates
3 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
24 | 3 | * Copyright (C) 2013 - 2020 Teemu Piippo |
3 | 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 | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
20 | #include <algorithm> |
3 | 21 | #include <cstdio> |
22 | #include <cstdlib> | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
23 | #include <cstring> |
3 | 24 | #include <cmath> |
53 | 25 | #include <optional> |
3 | 26 | #include <QMatrix4x4> |
27 | #include <QObject> | |
28 | #include <QPointF> | |
29 | #include <QSet> | |
30 | #include <QString> | |
31 | #include <QStringList> | |
32 | #include <QVariant> | |
33 | #include <QVector> | |
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 | 36 | |
37 | using GLRotationMatrix = QMatrix4x4; | |
38 | ||
39 | enum Axis | |
40 | { | |
21 | 41 | X = 0, |
42 | Y = 1, | |
43 | Z = 2 | |
3 | 44 | }; |
45 | ||
26 | 46 | enum Result |
47 | { | |
48 | Success = 0, | |
49 | PartialSuccess, | |
50 | Failure | |
51 | }; | |
52 | ||
53 | constexpr bool failed(Result r) | |
54 | { | |
55 | return r == Failure; | |
56 | } | |
57 | ||
3 | 58 | enum Winding |
59 | { | |
60 | NoWinding, | |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
7
diff
changeset
|
61 | Anticlockwise, |
3 | 62 | Clockwise, |
63 | }; | |
64 | ||
65 | /* | |
66 | * Special operator definition that implements the XOR operator for windings. | |
67 | * However, if either winding is NoWinding, then this function returns NoWinding. | |
68 | */ | |
69 | inline Winding operator^(Winding one, Winding other) | |
70 | { | |
71 | if (one == NoWinding or other == NoWinding) | |
72 | return NoWinding; | |
73 | else | |
74 | return static_cast<Winding>(static_cast<int>(one) ^ static_cast<int>(other)); | |
75 | } | |
76 | ||
77 | inline Winding& operator^=(Winding& one, Winding other) | |
78 | { | |
79 | one = one ^ other; | |
80 | return one; | |
81 | } | |
21 | 82 | |
83 | template<typename T, int N> | |
84 | constexpr int countof(T(&)[N]) | |
85 | { | |
86 | return N; | |
87 | } | |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
88 | |
51 | 89 | /** |
90 | * @brief casts @c x to a suitable unsigned integer | |
91 | */ | |
92 | template<typename T> | |
93 | constexpr auto unsigned_cast(T x) | |
94 | -> std::enable_if_t<std::is_integral_v<T>, std::make_unsigned_t<T>> | |
95 | { | |
96 | return static_cast<std::make_unsigned_t<T>>(x); | |
97 | } | |
98 | ||
99 | /** | |
100 | * @brief casts @c x to a suitable signed integer | |
101 | */ | |
102 | template<typename T> | |
103 | constexpr auto signed_cast(T x) | |
104 | -> std::enable_if_t<std::is_integral_v<T>, std::make_signed_t<T>> | |
105 | { | |
106 | return static_cast<std::make_signed_t<T>>(x); | |
107 | } | |
108 | ||
53 | 109 | |
110 | /** | |
55 | 111 | * @brief casts floating point values to float, converting non-floating point values causes an error |
112 | * @param[in] x floating point value to cast | |
53 | 113 | * @returns float |
114 | */ | |
115 | template<typename T> | |
55 | 116 | auto toFloat(T x) -> std::enable_if_t<std::is_floating_point_v<T>, float> |
53 | 117 | { |
118 | return static_cast<float>(x); | |
119 | } | |
120 | ||
121 | /** | |
55 | 122 | * @brief casts floating point values to double, converting non-floating point values causes an error |
123 | * @param[in] x floating point value to cast | |
53 | 124 | * @returns double |
125 | */ | |
126 | template<typename T> | |
55 | 127 | auto toDouble(T x) -> std::enable_if_t<std::is_floating_point_v<T>, double> |
53 | 128 | { |
129 | return static_cast<double>(x); | |
130 | } | |
55 | 131 | |
132 | template<int N, typename T, glm::qualifier Q> | |
133 | inline QPoint toQPoint(const glm::vec<N, T, Q>& vec) | |
134 | { | |
135 | return {static_cast<int>(vec.x), static_cast<int>(vec.y)}; | |
136 | } | |
137 | ||
138 | template<int N, typename T, glm::qualifier Q> | |
139 | inline QPointF toQPointF(const glm::vec<N, T, Q>& vec) | |
140 | { | |
141 | return {toDouble(vec.x), toDouble(vec.y)}; | |
142 | } | |
143 | ||
144 | inline glm::vec2 toVec2(const QPoint& point) | |
145 | { | |
146 | return {point.x(), point.y()}; | |
147 | } | |
148 | ||
149 | inline glm::vec2 toVec2(const QPointF& point) | |
150 | { | |
151 | return {point.x(), point.y()}; | |
152 | } | |
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) |