Fri, 23 Mar 2018 12:51:18 +0200
Begin rework to add support for multiple libraries
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 |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
3 | * Copyright (C) 2013 - 2017 Teemu Piippo |
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" |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
21 | |
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 | * Returns whether or not the document is flat. |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
24 | * 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
|
25 | */ |
1304 | 26 | bool isflat(Model* model, Axis* flatDimension) |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
27 | { |
1304 | 28 | // The dimensions that this model is potentially flat in. |
29 | QVector<Axis> dimensions = {X, Y, Z}; | |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
30 | |
1304 | 31 | // Iterate through everything in the subfile. If there is any vertex with a coordinate not at |
32 | // 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
|
33 | for (LDObject* subfileObject : model->objects()) |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
34 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
35 | for (int i = 0; i < subfileObject->numVertices(); ++i) |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
36 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
37 | Vertex const& v_i = subfileObject->vertex(i); |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
38 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
39 | if (not qFuzzyCompare(v_i.x(), 0.f)) |
1304 | 40 | dimensions.removeOne(X); |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
41 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
42 | if (not qFuzzyCompare(v_i.y(), 0.f)) |
1304 | 43 | dimensions.removeOne(Y); |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
44 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
45 | if (not qFuzzyCompare(v_i.z(), 0.f)) |
1304 | 46 | dimensions.removeOne(Z); |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
47 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
48 | |
1304 | 49 | // If there are no more dimensions left, we can exit the loop. |
50 | if (dimensions.isEmpty()) | |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
51 | break; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
52 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
53 | |
1304 | 54 | if (dimensions.size() == 1) |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
55 | { |
1304 | 56 | // The model is flat in one dimension, return that. |
57 | // If the model is flat in two or three dimensions, it's not really a valid model. | |
58 | *flatDimension = dimensions[0]; | |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
59 | return true; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
60 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
61 | else |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
62 | { |
1304 | 63 | // The model is not flat. |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
64 | return false; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
65 | } |
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 | /* |
1304 | 69 | * 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
|
70 | */ |
1304 | 71 | Matrix flipmatrix(Axis dimension) |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
72 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
73 | Matrix result = Matrix::identity; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
74 | |
1304 | 75 | switch (dimension) |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
76 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
77 | case X: |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
78 | result(0, 0) = -1; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
79 | break; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
80 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
81 | case Y: |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
82 | result(1, 1) = -1; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
83 | break; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
84 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
85 | case Z: |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
86 | result(2, 2) = -1; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
87 | break; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
88 | } |
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 | return result; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
91 | } |
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 | * 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
|
95 | */ |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
96 | void invert(LDObject* obj, DocumentManager* context) |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
97 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
98 | if (obj->numPolygonVertices() > 0) |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
99 | { |
1304 | 100 | // 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
|
101 | QVector<Vertex> vertices; |
1303 | 102 | vertices.resize(obj->numPolygonVertices()); |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
103 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
104 | for (int i = 0; i < vertices.size(); i += 1) |
1303 | 105 | vertices[vertices.size() - 1 - i] = obj->vertex(i); |
106 | ||
107 | for (int i = 0; i < vertices.size(); i += 1) | |
108 | obj->setVertex(i, vertices[i]); | |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
109 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
110 | else if (obj->type() == LDObjectType::SubfileReference) |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
111 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
112 | // 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
|
113 | Model model {context}; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
114 | LDSubfileReference* reference = static_cast<LDSubfileReference*>(obj); |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
115 | reference->fileInfo(context)->inlineContents(model, true, false); |
1304 | 116 | Axis flatDimension; |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
117 | |
1304 | 118 | if (::isflat(&model, &flatDimension)) |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
119 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
120 | reference->setTransformationMatrix( |
1304 | 121 | reference->transformationMatrix() * ::flipmatrix(flatDimension) |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
122 | ); |
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 | else |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
125 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
126 | // Subfile is not flat. Resort to invertnext. |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
127 | reference->setInverted(not reference->isInverted()); |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
128 | } |
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 | } |
1305
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
131 | |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
132 | void invertPolygon(LDPolygon& polygon) |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
133 | { |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
134 | switch (polygon.numPolygonVertices()) |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
135 | { |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
136 | case 2: |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
137 | case 3: |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
138 | 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
|
139 | break; |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
140 | |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
141 | case 4: |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
142 | 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
|
143 | break; |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
144 | } |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
145 | } |