Sat, 24 Mar 2018 12:54:28 +0200
Happy new year 2018
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
1 | /* |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
2 | * LDForge: LDraw parts authoring CAD |
1326 | 3 | * Copyright (C) 2013 - 2018 Teemu Piippo |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
4 | * |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
5 | * This program is free software: you can redistribute it and/or modify |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
6 | * it under the terms of the GNU General Public License as published by |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
7 | * the Free Software Foundation, either version 3 of the License, or |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
8 | * (at your option) any later version. |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
9 | * |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
10 | * This program is distributed in the hope that it will be useful, |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
13 | * GNU General Public License for more details. |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
14 | * |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
15 | * You should have received a copy of the GNU General Public License |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
17 | */ |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
18 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
19 | #include "../linetypes/modelobject.h" |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
20 | #include "../lddocument.h" |
1318
568fcfc6da71
removed unnecessary files and includes
Teemu Piippo <teemu@hecknology.net>
parents:
1313
diff
changeset
|
21 | #include "../glShared.h" |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
22 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
23 | /* |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
24 | * Returns whether or not the document is flat. |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
25 | * If it is flat, the result is stored in *axis. |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
26 | */ |
1304 | 27 | bool isflat(Model* model, Axis* flatDimension) |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
28 | { |
1304 | 29 | // The dimensions that this model is potentially flat in. |
30 | QVector<Axis> dimensions = {X, Y, Z}; | |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
31 | |
1304 | 32 | // Iterate through everything in the subfile. If there is any vertex with a coordinate not at |
33 | // zero, the subfile is not flat in that dimension. | |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
34 | for (LDObject* subfileObject : model->objects()) |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
35 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
36 | for (int i = 0; i < subfileObject->numVertices(); ++i) |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
37 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
38 | Vertex const& v_i = subfileObject->vertex(i); |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
39 | |
1313
4baed9f54de3
reworked Vertex, no longer a QVector3D subclass
Teemu Piippo <teemu@hecknology.net>
parents:
1305
diff
changeset
|
40 | if (not qFuzzyCompare(v_i.x, 0.0)) |
1304 | 41 | dimensions.removeOne(X); |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
42 | |
1313
4baed9f54de3
reworked Vertex, no longer a QVector3D subclass
Teemu Piippo <teemu@hecknology.net>
parents:
1305
diff
changeset
|
43 | if (not qFuzzyCompare(v_i.y, 0.0)) |
1304 | 44 | dimensions.removeOne(Y); |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
45 | |
1313
4baed9f54de3
reworked Vertex, no longer a QVector3D subclass
Teemu Piippo <teemu@hecknology.net>
parents:
1305
diff
changeset
|
46 | if (not qFuzzyCompare(v_i.z, 0.0)) |
1304 | 47 | dimensions.removeOne(Z); |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
48 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
49 | |
1304 | 50 | // If there are no more dimensions left, we can exit the loop. |
51 | if (dimensions.isEmpty()) | |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
52 | break; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
53 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
54 | |
1304 | 55 | if (dimensions.size() == 1) |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
56 | { |
1304 | 57 | // The model is flat in one dimension, return that. |
58 | // If the model is flat in two or three dimensions, it's not really a valid model. | |
59 | *flatDimension = dimensions[0]; | |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
60 | return true; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
61 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
62 | else |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
63 | { |
1304 | 64 | // The model is not flat. |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
65 | return false; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
66 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
67 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
68 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
69 | /* |
1304 | 70 | * Returns a matrix that causes a flip on the given dimension. |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
71 | */ |
1304 | 72 | Matrix flipmatrix(Axis dimension) |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
73 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
74 | Matrix result = Matrix::identity; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
75 | |
1304 | 76 | switch (dimension) |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
77 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
78 | case X: |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
79 | result(0, 0) = -1; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
80 | break; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
81 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
82 | case Y: |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
83 | result(1, 1) = -1; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
84 | break; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
85 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
86 | case Z: |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
87 | result(2, 2) = -1; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
88 | break; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
89 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
90 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
91 | return result; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
92 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
93 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
94 | /* |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
95 | * Inverts an LDObject so that its winding is changed. |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
96 | */ |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
97 | void invert(LDObject* obj, DocumentManager* context) |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
98 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
99 | if (obj->numPolygonVertices() > 0) |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
100 | { |
1304 | 101 | // Object is vertex based, so change the order of the vertices. |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
102 | QVector<Vertex> vertices; |
1303 | 103 | vertices.resize(obj->numPolygonVertices()); |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
104 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
105 | for (int i = 0; i < vertices.size(); i += 1) |
1303 | 106 | vertices[vertices.size() - 1 - i] = obj->vertex(i); |
107 | ||
108 | for (int i = 0; i < vertices.size(); i += 1) | |
109 | obj->setVertex(i, vertices[i]); | |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
110 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
111 | else if (obj->type() == LDObjectType::SubfileReference) |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
112 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
113 | // Check whether subfile is flat. If it is, flip it on the axis on which it is flat. |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
114 | Model model {context}; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
115 | LDSubfileReference* reference = static_cast<LDSubfileReference*>(obj); |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
116 | reference->fileInfo(context)->inlineContents(model, true, false); |
1304 | 117 | Axis flatDimension; |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
118 | |
1304 | 119 | if (::isflat(&model, &flatDimension)) |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
120 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
121 | reference->setTransformationMatrix( |
1304 | 122 | reference->transformationMatrix() * ::flipmatrix(flatDimension) |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
123 | ); |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
124 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
125 | else |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
126 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
127 | // Subfile is not flat. Resort to invertnext. |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
128 | reference->setInverted(not reference->isInverted()); |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
129 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
130 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
131 | } |
1305
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
132 | |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
133 | void invertPolygon(LDPolygon& polygon) |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
134 | { |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
135 | switch (polygon.numPolygonVertices()) |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
136 | { |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
137 | case 2: |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
138 | case 3: |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
139 | std::swap(polygon.vertices[0], polygon.vertices[1]); |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
140 | break; |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
141 | |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
142 | case 4: |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
143 | std::swap(polygon.vertices[1], polygon.vertices[3]); |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
144 | break; |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
145 | } |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
146 | } |