Mon, 21 Sep 2020 19:48:18 +0300
added a color select dialog
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> | |
90
e234edb5e613
remove dependency on glut, fixes
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
84 | constexpr int countof(T const (&)[N]) |
21 | 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 | |
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
132 | /** |
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
133 | * @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
|
134 | * @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
|
135 | * @returns qreal |
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
136 | */ |
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
137 | template<typename T> |
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
138 | 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
|
139 | { |
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
140 | return static_cast<qreal>(x); |
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
141 | } |
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
70
diff
changeset
|
142 | |
55 | 143 | template<int N, typename T, glm::qualifier Q> |
144 | inline QPoint toQPoint(const glm::vec<N, T, Q>& vec) | |
145 | { | |
146 | return {static_cast<int>(vec.x), static_cast<int>(vec.y)}; | |
147 | } | |
148 | ||
149 | template<int N, typename T, glm::qualifier Q> | |
150 | inline QPointF toQPointF(const glm::vec<N, T, Q>& vec) | |
151 | { | |
152 | return {toDouble(vec.x), toDouble(vec.y)}; | |
153 | } | |
154 | ||
155 | inline glm::vec2 toVec2(const QPoint& point) | |
156 | { | |
157 | return {point.x(), point.y()}; | |
158 | } | |
159 | ||
160 | inline glm::vec2 toVec2(const QPointF& point) | |
161 | { | |
162 | return {point.x(), point.y()}; | |
163 | } | |
164 | ||
70 | 165 | constexpr float PIf = static_cast<float>(M_PI); |
166 | constexpr double PI = M_PI; | |
167 | constexpr long double PIl = M_PIl; | |
168 | ||
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
169 | Q_DECLARE_METATYPE(glm::vec3) |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
170 | Q_DECLARE_METATYPE(glm::mat4) |