src/main.h

Wed, 25 May 2022 12:01:58 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 25 May 2022 12:01:58 +0300
changeset 189
815fbaae9cb2
parent 188
64ea7282611e
child 191
d355d4c52d51
permissions
-rw-r--r--

cleanup, gl::Compiler changed to gl::ModelShaders

24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
1 /*
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
2 * LDForge: LDraw parts authoring CAD
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
4 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
6 * it under the terms of the GNU General Public License as published by
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
8 * (at your option) any later version.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
9 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
10 * This program is distributed in the hope that it will be useful,
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
13 * GNU General Public License for more details.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
14 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
15 * You should have received a copy of the GNU General Public License
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
17 */
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 22
diff changeset
18
0
f9f4d4d6f162 initial commit
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #pragma once
2
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
20 #include <QString>
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
21 #include <QVector>
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
22 #include <QSet>
96
165777a20dc7 added tool base code
Teemu Piippo <teemu@hecknology.net>
parents: 81
diff changeset
23 #include <QDebug>
188
64ea7282611e more work on circle tool + cleanup
Teemu Piippo <teemu@hecknology.net>
parents: 148
diff changeset
24 #include <compare>
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
25 #include <memory>
3
55a55a9ec2c2 Added lots of code
Teemu Piippo <teemu@hecknology.net>
parents: 2
diff changeset
26 #include "basics.h"
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents: 7
diff changeset
27 #include "utility.h"
20
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents: 18
diff changeset
28 #include "maths.h"
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
29 #include "geometry.h"
63
f7dd937667a5 omg functional programming
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
30 #include "functional.h"
2
Teemu Piippo <teemu@hecknology.net>
parents: 0
diff changeset
31
7
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
32 namespace settingGroups
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
33 {
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
34 // List of setting groups
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
35 constexpr char mainwindow[] = "mainwindow";
68443f5be176 added the settings editor
Teemu Piippo <teemu@hecknology.net>
parents: 3
diff changeset
36 }
21
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
37
35
98906a94732f renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
38 namespace ldraw
21
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
39 {
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
40 class Object;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
41
21
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
42 // Uniquely identifies a model body object
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
43 template<typename T>
21
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
44 struct Id
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
45 {
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
46 std::int32_t value;
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
47 template<typename A, typename B>
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
48 static constexpr bool is_base_or_base_of = std::disjunction_v<std::is_base_of<A, B>, std::is_base_of<B, A>>;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
49 template<typename R, typename = std::enable_if_t<is_base_or_base_of<T, R>>>
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
50 constexpr bool operator<(ldraw::Id<R> other) const
26
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 return this->value < other.value;
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
53 }
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
54 friend constexpr unsigned int qHash(ldraw::Id<T> id)
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
55 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
56 return qHash(id.value);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
57 }
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
58 // Allow comparing ids as long as they are related
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
59 template<typename R, typename = std::enable_if_t<is_base_or_base_of<T, R>>>
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
60 friend bool operator==(ldraw::Id<T> one, ldraw::Id<R> other)
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
61 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
62 return one.value == other.value;
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
63 }
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
64 // Allow upcasting
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
65 template<typename R, typename = std::enable_if_t<std::is_base_of_v<R, T>>>
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
66 constexpr operator Id<R>() const
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
67 {
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
68 return Id<R>{this->value};
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
69 }
21
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
70 };
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
71
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
72 using id_t = Id<Object>;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
73 using triangleid_t = Id<class Triangle>;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
74 using quadrilateralid_t = Id<class Quadrilateral>;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
75 using edgeid_t = Id<class EdgeLine>;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
76 using conditionaledgeid_t = Id<class ConditionalEdge>;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
77 using subfileid_t = Id<class SubfileReference>;
141
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 134
diff changeset
78 using commentid_t = Id<class Comment>;
185eb297dc1e Saving works now
Teemu Piippo <teemu@hecknology.net>
parents: 134
diff changeset
79 using metacommandid_t = Id<class MetaCommand>;
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
80
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
81 constexpr struct NullId
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
82 {
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
83 template<typename T>
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
84 constexpr operator Id<T>() const
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
85 {
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
86 return Id<T>{0};
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
87 }
81
62373840e33a object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
88 static constexpr decltype(ldraw::id_t::value) value = 0;
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
89 } NULL_ID = {};
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
90
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
91 template<typename T>
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
92 inline bool operator==(Id<T> one, decltype(NULL_ID))
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
93 {
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
94 return one.value == 0;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
95 }
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
96
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
97 template<typename T>
107
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 101
diff changeset
98 inline bool operator!=(Id<T> one, decltype(NULL_ID))
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 101
diff changeset
99 {
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 101
diff changeset
100 return one.value != 0;
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 101
diff changeset
101 }
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 101
diff changeset
102
02f142b399b1 Move selection logic into select tool
Teemu Piippo <teemu@hecknology.net>
parents: 101
diff changeset
103 template<typename T>
73
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
104 inline bool operator<(Id<T> one, decltype(NULL_ID))
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
105 {
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
106 return one.value < 0;
97df974b5ed5 ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
107 }
21
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
108 }
22
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
109
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
110 constexpr std::size_t operator""_z(const unsigned long long int x)
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
111 {
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
112 return static_cast<std::size_t>(x);
6da867fa5429 commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
113 }
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
114
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
115 inline QString operator""_q(const char* string, const unsigned long int length)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
116 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
117 Q_UNUSED(length)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
118 return QString{string};
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
119 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
120
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
121 inline QPointF pointToPointF(const QPoint& point)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
122 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
123 return {static_cast<qreal>(point.x()), static_cast<qreal>(point.y())};
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
124 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
125
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
126 inline QPoint pointFToPoint(const QPointF& point)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
127 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
128 return {static_cast<int>(std::round(point.x())), static_cast<int>(std::round(point.y()))};
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
129 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
130
113
c0d064521ee0 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 107
diff changeset
131 inline QSizeF sizeToSizeF(const QSize& size)
c0d064521ee0 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 107
diff changeset
132 {
c0d064521ee0 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 107
diff changeset
133 return {static_cast<qreal>(size.width()), static_cast<qreal>(size.height())};
c0d064521ee0 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 107
diff changeset
134 }
c0d064521ee0 refactor
Teemu Piippo <teemu@hecknology.net>
parents: 107
diff changeset
135
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
136 /**
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
137 * \brief Hints to the specified vector that a certain amount of new elements are going to be added.
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
138 * \param vector vector to consider
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
139 * \param amount amount of new elements to expect
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
140 */
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
141 template<typename T>
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
142 void reserveMore(std::vector<T>& vector, std::size_t amount)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
143 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
144 vector.reserve(vector.size() + amount);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
145 }
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
146
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
147 inline QString vectorToString(const glm::vec2& vec)
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
148 {
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
149 return "(%1, %2)"_q
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
150 .arg(toDouble(vec.x))
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
151 .arg(toDouble(vec.y));
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
152 }
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
153
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
154 inline QString vectorToString(const glm::vec3& vec)
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
155 {
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
156 return "(%1, %2, %3)"_q
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
157 .arg(toDouble(vec.x))
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
158 .arg(toDouble(vec.y))
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
159 .arg(toDouble(vec.z));
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
160 }
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
161
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
162 inline QString vectorToString(const glm::vec4& vec)
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
163 {
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
164 return "(%1, %2, %3, %4)"_q
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
165 .arg(toDouble(vec.x))
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
166 .arg(toDouble(vec.y))
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
167 .arg(toDouble(vec.z))
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
168 .arg(toDouble(vec.w));
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 63
diff changeset
169 }
97
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
170
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
171 template<typename K, typename V>
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
172 struct KeyValuePair
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
173 {
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
174 K key;
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
175 V value;
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
176 };
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
177
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
178 template<typename K, typename V, typename IteratorType>
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
179 struct MapItemsIterator : IteratorType
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
180 {
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
181 template<typename... Ts>
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
182 MapItemsIterator(Ts&&... args) : IteratorType{args...} {}
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
183 auto operator*() const
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
184 {
101
910890292639 added references to items()
Teemu Piippo <teemu@hecknology.net>
parents: 97
diff changeset
185 return KeyValuePair<const K&, V&>{this->key(), this->value()};
97
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
186 }
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
187 };
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
188
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
189 template<typename K, typename V, typename MapType, typename IteratorType>
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
190 struct MapItems
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
191 {
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
192 MapType& map;
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
193 IteratorType begin()
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
194 {
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
195 return IteratorType(this->map.begin());
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
196 }
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
197
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
198 IteratorType end()
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
199 {
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
200 return IteratorType(this->map.end());
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
201 }
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
202 };
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
203
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
204 /*
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
205 * Python's dict.items for QMap: use in a for loop to iterate a map to
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
206 * get both keys and values. Iteration yields KeyValuePairs.
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
207 */
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
208 template<typename K, typename V>
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
209 auto items(const QMap<K, V>& map)
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
210 {
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
211 return MapItems<
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
212 const K&,
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
213 const V&,
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
214 const QMap<K, V>,
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
215 MapItemsIterator<K, const V, typename QMap<K, V>::const_iterator>
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
216 >{map};
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
217 }
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
218
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
219 template<typename K, typename V>
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
220 auto items(QMap<K, V>& map)
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
221 {
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
222 return MapItems<
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
223 const K&,
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
224 V&,
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
225 QMap<K, V>,
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
226 MapItemsIterator<K, const V, typename QMap<K, V>::iterator>
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
227 >{map};
d9a3b153f679 work on tools
Teemu Piippo <teemu@hecknology.net>
parents: 96
diff changeset
228 }
132
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
229
148
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
230 template<typename T, typename IdentifierType>
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
231 struct TypeValue
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
232 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
233 T value;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
234 bool operator==(TypeValue<T, IdentifierType> other) const
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
235 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
236 return value == other.value;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
237 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
238 bool operator!=(TypeValue<T, IdentifierType> other) const
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
239 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
240 return value != other.value;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
241 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
242 bool operator<(TypeValue<T, IdentifierType> other) const
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
243 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
244 return value < other.value;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
245 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
246 bool operator>(TypeValue<T, IdentifierType> other) const
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
247 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
248 return value > other.value;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
249 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
250 bool operator<=(TypeValue<T, IdentifierType> other) const
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
251 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
252 return value <= other.value;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
253 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
254 bool operator>=(TypeValue<T, IdentifierType> other) const
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
255 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
256 return value >= other.value;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
257 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
258 };
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
259
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
260 template<typename T, typename R>
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
261 int qHash(TypeValue<T, R> value)
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
262 {
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
263 return qHash(value.value);
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
264 }
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
265
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
266 using ModelId = TypeValue<int, struct TypeValueModelId>;
e1ced2523cad reworking
Teemu Piippo <teemu@hecknology.net>
parents: 141
diff changeset
267
132
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
268 /**
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
269 * Iterates a @c glm::mat
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
270 */
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
271 template<int X, int Y, typename T, glm::qualifier Q, typename Fn>
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
272 void iter_matrix(const glm::mat<X, Y, T, Q>& matrix, Fn&& fn)
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
273 {
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
274 for (int i = 0; i < X; ++i)
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
275 {
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
276 for (int j = 0; j < Y; ++j)
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
277 {
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
278 fn(i, j, matrix[i][j]);
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
279 }
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
280 }
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
281 }
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
282
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
283 QDataStream& operator<<(QDataStream&, const glm::vec3&);
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
284 QDataStream& operator>>(QDataStream&, glm::vec3&);
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
285
134
f77d2230e87c Add remaining serialize methods
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
286 template<int X, int Y, typename T, glm::qualifier Q>
132
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
287 QDataStream& operator<<(QDataStream& stream, const glm::mat<X, Y, T, Q>& mat)
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
288 {
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
289 iter_matrix(mat, [&stream](int, int, float x)
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
290 {
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
291 stream << x;
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
292 });
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
293 return stream;
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
294 }
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
295
134
f77d2230e87c Add remaining serialize methods
Teemu Piippo <teemu@hecknology.net>
parents: 132
diff changeset
296 template<int X, int Y, typename T, glm::qualifier Q>
132
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
297 QDataStream& operator>>(QDataStream& stream, glm::mat<X, Y, T, Q>& mat)
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
298 {
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
299 iter_matrix(mat, [&stream](int, int, float x)
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
300 {
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
301 stream >> x;
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
302 });
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
303 return stream;
488d0ba6070b Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents: 113
diff changeset
304 }

mercurial