Sun, 03 Jul 2022 15:59:22 +0300
Fix polygonize not finding the normal properly
24 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2020 Teemu Piippo | |
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 | ||
3 | 19 | #pragma once |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
20 | #include <QAbstractListModel> |
3 | 21 | #include <memory> |
264
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
262
diff
changeset
|
22 | #include "src/basics.h" |
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
262
diff
changeset
|
23 | #include "src/colors.h" |
200 | 24 | |
25 | struct SubfileReference | |
26 | { | |
27 | QString name; | |
28 | glm::mat4 transformation; | |
29 | bool inverted = false; | |
30 | }; | |
31 | ||
32 | template<typename T> | |
33 | struct Colored : T | |
34 | { | |
35 | ldraw::Color color; | |
36 | }; | |
37 | ||
38 | struct Comment | |
39 | { | |
40 | QString text; | |
41 | }; | |
3 | 42 | |
200 | 43 | struct ParseError |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
44 | { |
200 | 45 | QString code; |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
46 | }; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
47 | |
200 | 48 | struct Empty {}; |
49 | ||
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
50 | struct CircularFraction |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
51 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
52 | unsigned int segments; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
53 | unsigned int divisions; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
54 | }; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
55 | |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
56 | constexpr bool operator<(const CircularFraction& p, const CircularFraction& q) |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
57 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
58 | // a/b < c/d |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
59 | // a < c * b / d |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
60 | // a * d < c * b |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
61 | return p.segments * q.divisions < q.segments / p.divisions; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
62 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
63 | |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
64 | struct CircularPrimitive |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
65 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
66 | enum Type |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
67 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
68 | Circle, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
69 | Disc, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
70 | Cylinder, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
71 | CylinderOpen, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
72 | CylinderClosed, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
73 | DiscNegative, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
74 | Chord, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
75 | } type; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
76 | static constexpr int NUM_TYPES = Chord + 1; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
77 | CircularFraction fraction; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
78 | glm::mat4 transformation; |
249
37d3c819cafa
Done some work on object editor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
242
diff
changeset
|
79 | bool inverted = false; |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
80 | }; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
81 | |
242
16855456992d
Substitute circular primitives in during file parsing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
233
diff
changeset
|
82 | constexpr char circularPrimitiveStems[CircularPrimitive::NUM_TYPES][5] = { |
16855456992d
Substitute circular primitives in during file parsing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
233
diff
changeset
|
83 | "edge", |
16855456992d
Substitute circular primitives in during file parsing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
233
diff
changeset
|
84 | "disc", |
16855456992d
Substitute circular primitives in during file parsing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
233
diff
changeset
|
85 | "cyli", |
16855456992d
Substitute circular primitives in during file parsing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
233
diff
changeset
|
86 | "cylo", |
16855456992d
Substitute circular primitives in during file parsing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
233
diff
changeset
|
87 | "cylc", |
16855456992d
Substitute circular primitives in during file parsing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
233
diff
changeset
|
88 | "ndis", |
16855456992d
Substitute circular primitives in during file parsing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
233
diff
changeset
|
89 | "chrd", |
16855456992d
Substitute circular primitives in during file parsing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
233
diff
changeset
|
90 | }; |
16855456992d
Substitute circular primitives in during file parsing
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
233
diff
changeset
|
91 | |
233 | 92 | Q_DECLARE_METATYPE(CircularPrimitive::Type) |
93 | ||
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
94 | struct CircleToolOptions |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
95 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
96 | CircularFraction fraction; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
97 | CircularPrimitive::Type type; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
98 | }; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
99 | |
200 | 100 | using ModelElement = std::variant< |
101 | Colored<SubfileReference>, | |
102 | Colored<LineSegment>, | |
103 | Colored<Triangle>, | |
104 | Colored<Quadrilateral>, | |
105 | Colored<ConditionalEdge>, | |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
106 | Colored<CircularPrimitive>, |
200 | 107 | Comment, |
108 | Empty, | |
109 | ParseError>; | |
110 | ||
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
111 | using PlainPolygonElement = std::variant< |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
112 | LineSegment, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
113 | Triangle, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
114 | Quadrilateral, |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
115 | ConditionalEdge>; |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
116 | |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
117 | using PolygonElement = Colored<PlainPolygonElement>; |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
118 | |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
119 | template<typename T> |
211 | 120 | struct remove_color {}; |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
121 | |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
122 | template<typename T> |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
123 | struct remove_color<Colored<T>> { using type = T; }; |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
124 | |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
125 | template<typename T> |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
126 | struct remove_color<Colored<T>&> { using type = T&; }; |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
127 | |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
128 | template<typename T> |
211 | 129 | struct remove_color<const Colored<T>&> { using type = const T&; }; |
130 | ||
131 | template<typename T> | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
132 | struct remove_color<Colored<T>&&> { using type = T&&; }; |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
133 | |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
134 | template<typename T> |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
135 | using remove_color_t = typename remove_color<T>::type; |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
136 | |
211 | 137 | static_assert(std::is_same_v<remove_color_t<Colored<Triangle>>, Triangle>); |
138 | static_assert(std::is_same_v<remove_color_t<Colored<Triangle>&>, Triangle&>); | |
139 | static_assert(std::is_same_v<remove_color_t<const Colored<Triangle>&>, const Triangle&>); | |
140 | ||
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
141 | template<typename T> |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
142 | constexpr remove_color_t<T&&> extract_colored(T&& x) |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
143 | { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
144 | return static_cast<remove_color_t<T&&>>(x); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
145 | } |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
146 | |
211 | 147 | template<typename Ret, typename Fn1, typename Fn2, typename Fn3, typename Fn4, typename T> |
148 | constexpr auto visitPolygon(Fn1&& f1, Fn2&& f2, Fn3&& f3, Fn4&& f4, T&& element) | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
149 | { |
211 | 150 | if (std::holds_alternative<LineSegment>(element)) { |
151 | return f1(std::get<LineSegment>(element)); | |
152 | } | |
153 | else if (std::holds_alternative<Triangle>(element)) { | |
154 | return f2(std::get<Triangle>(element)); | |
155 | } | |
156 | else if (std::holds_alternative<Quadrilateral>(element)) { | |
157 | return f3(std::get<Quadrilateral>(element)); | |
158 | } | |
159 | else { | |
160 | return f4(std::get<ConditionalEdge>(element)); | |
161 | } | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
162 | } |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
163 | |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
164 | template<typename T, typename Fn> |
211 | 165 | constexpr void visitPoints(Fn&& func, T&& element) |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
166 | { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
167 | visitPolygon<void>( |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
168 | [&func](transfer_cvref_t<T&&, LineSegment> edge) |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
169 | { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
170 | func(edge.p1); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
171 | func(edge.p2); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
172 | }, |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
173 | [&func](transfer_cvref_t<T&&, Triangle>& tri) |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
174 | { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
175 | func(tri.p1); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
176 | func(tri.p2); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
177 | func(tri.p3); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
178 | }, |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
179 | [&func](transfer_cvref_t<T&&, Quadrilateral>& quad) |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
180 | { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
181 | func(quad.p1); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
182 | func(quad.p2); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
183 | func(quad.p3); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
184 | func(quad.p4); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
185 | }, |
218
63125c36de73
Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
212
diff
changeset
|
186 | [&func](transfer_cvref_t<T&&, ConditionalEdge>& cedge) |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
187 | { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
188 | func(cedge.p1); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
189 | func(cedge.p2); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
190 | func(cedge.c1); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
191 | func(cedge.c2); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
192 | }, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
193 | element); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
194 | } |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
195 | |
200 | 196 | QString modelElementToString(const ModelElement& element); |
309
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
197 | struct ElementId |
200 | 198 | { |
199 | std::int32_t value; | |
309
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
200 | constexpr auto operator<=>(const ElementId& other) const = default; |
200 | 201 | }; |
202 | ||
309
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
203 | constexpr auto qHash(ElementId id) |
200 | 204 | { |
205 | return qHash(id.value); | |
206 | } | |
207 | ||
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
208 | class Model : public QAbstractListModel |
3 | 209 | { |
210 | Q_OBJECT | |
200 | 211 | struct Entry { |
212 | ModelElement data; | |
309
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
213 | ElementId id; |
200 | 214 | }; |
215 | std::vector<Entry> body; | |
309
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
216 | std::map<ElementId, std::size_t> positions; |
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
217 | ElementId runningId = {1}; |
3 | 218 | public: |
262
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
219 | explicit Model(QObject* parent); |
200 | 220 | virtual ~Model(); |
309
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
221 | ElementId append(const ModelElement& value); |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
249
diff
changeset
|
222 | const ModelElement& at(std::size_t position) const; |
309
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
223 | ElementId idAt(std::size_t position) const; |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
249
diff
changeset
|
224 | void assignAt(std::size_t position, const ModelElement& element); |
309
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
225 | std::optional<std::size_t> find(ElementId id) const; |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
249
diff
changeset
|
226 | void remove(std::size_t index); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
227 | int rowCount(const QModelIndex&) const override; |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
228 | QVariant data(const QModelIndex& index, int role) const override; |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
249
diff
changeset
|
229 | const ModelElement& operator[](std::size_t index) const; |
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
249
diff
changeset
|
230 | std::size_t size() const; |
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
249
diff
changeset
|
231 | auto operator[](const std::size_t index) { |
200 | 232 | struct { |
233 | Model& model; | |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
249
diff
changeset
|
234 | const std::size_t index; |
200 | 235 | operator const ModelElement&() { |
236 | return model.at(index); | |
237 | } | |
238 | auto& operator=(const ModelElement& newData) { | |
239 | model.assignAt(index, newData); | |
240 | return *this; | |
241 | } | |
242 | const auto* operator&() { | |
243 | return &(this->operator const ModelElement&()); | |
244 | } | |
245 | } result{*this, index}; | |
246 | return result; | |
247 | } | |
3 | 248 | }; |
249 | ||
151 | 250 | void save(const Model& model, QIODevice *device); |
200 | 251 | void updateHeaderNameField(Model& model, const QString &name); |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
252 | |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
253 | template<typename T> |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
254 | void iterate(const Model& model, std::function<void(const T&)> fn) |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
255 | { |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
249
diff
changeset
|
256 | for (std::size_t i = 0; i < model.size(); ++i) { |
212
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
257 | if (std::holds_alternative<T>(model[i])) { |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
258 | fn(std::get<T>(model[i])); |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
259 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
260 | } |
27259810da6d
Rewrite dependency loading
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
261 | } |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
262 | |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
263 | constexpr Colored<LineSegment> edge(const glm::vec3& p1, const glm::vec3& p2) |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
264 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
265 | return Colored<LineSegment>{{.p1 = p1, .p2 = p2}, EDGE_COLOR}; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
266 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
267 | |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
268 | constexpr Colored<Triangle> triangle(const glm::vec3& p1, const glm::vec3& p2, const glm::vec3& p3) |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
269 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
270 | return Colored<Triangle>{{.p1 = p1, .p2 = p2, .p3 = p3}, MAIN_COLOR}; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
271 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
272 | |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
273 | constexpr Colored<Quadrilateral> quadrilateral( |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
274 | const glm::vec3& p1, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
275 | const glm::vec3& p2, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
276 | const glm::vec3& p3, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
277 | const glm::vec3& p4) |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
278 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
279 | return Colored<Quadrilateral>{{.p1 = p1, .p2 = p2, .p3 = p3, .p4 = p4}, MAIN_COLOR}; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
218
diff
changeset
|
280 | } |
262
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
281 | |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
282 | struct AppendToModel |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
283 | { |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
284 | ModelElement newElement; |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
285 | }; |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
286 | |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
287 | struct DeleteFromModel |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
288 | { |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
289 | std::size_t position; |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
290 | }; |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
291 | |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
292 | struct ModifyModel |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
293 | { |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
294 | std::size_t position; |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
295 | ModelElement newElement; |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
296 | }; |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
297 | |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
298 | using ModelAction = std::variant< |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
299 | AppendToModel, |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
300 | DeleteFromModel, |
dc33f8a707c4
Add action to make a model unofficial (modifies the !LDRAW_ORG line)
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
301 | ModifyModel>; |