Tue, 19 Jun 2018 23:18:58 +0300
added support for inverting circular primitives
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" |
1412
f5eb947a2e7f
added support for inverting circular primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1403
diff
changeset
|
20 | #include "../linetypes/circularprimitive.h" |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
21 | #include "../lddocument.h" |
1318
568fcfc6da71
removed unnecessary files and includes
Teemu Piippo <teemu@hecknology.net>
parents:
1313
diff
changeset
|
22 | #include "../glShared.h" |
1301
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 | /* |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
25 | * Returns whether or not the document is flat. |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
26 | * 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
|
27 | */ |
1304 | 28 | bool isflat(Model* model, Axis* flatDimension) |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
29 | { |
1304 | 30 | // The dimensions that this model is potentially flat in. |
31 | QVector<Axis> dimensions = {X, Y, Z}; | |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
32 | |
1304 | 33 | // Iterate through everything in the subfile. If there is any vertex with a coordinate not at |
34 | // 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
|
35 | for (LDObject* subfileObject : model->objects()) |
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 | for (int i = 0; i < subfileObject->numVertices(); ++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 | Vertex const& v_i = subfileObject->vertex(i); |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
40 | |
1313
4baed9f54de3
reworked Vertex, no longer a QVector3D subclass
Teemu Piippo <teemu@hecknology.net>
parents:
1305
diff
changeset
|
41 | if (not qFuzzyCompare(v_i.x, 0.0)) |
1304 | 42 | dimensions.removeOne(X); |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
43 | |
1313
4baed9f54de3
reworked Vertex, no longer a QVector3D subclass
Teemu Piippo <teemu@hecknology.net>
parents:
1305
diff
changeset
|
44 | if (not qFuzzyCompare(v_i.y, 0.0)) |
1304 | 45 | dimensions.removeOne(Y); |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
46 | |
1313
4baed9f54de3
reworked Vertex, no longer a QVector3D subclass
Teemu Piippo <teemu@hecknology.net>
parents:
1305
diff
changeset
|
47 | if (not qFuzzyCompare(v_i.z, 0.0)) |
1304 | 48 | dimensions.removeOne(Z); |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
49 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
50 | |
1304 | 51 | // If there are no more dimensions left, we can exit the loop. |
52 | if (dimensions.isEmpty()) | |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
53 | break; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
54 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
55 | |
1304 | 56 | if (dimensions.size() == 1) |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
57 | { |
1304 | 58 | // The model is flat in one dimension, return that. |
59 | // If the model is flat in two or three dimensions, it's not really a valid model. | |
60 | *flatDimension = dimensions[0]; | |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
61 | return true; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
62 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
63 | else |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
64 | { |
1304 | 65 | // The model is not flat. |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
66 | return false; |
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 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
70 | /* |
1304 | 71 | * 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
|
72 | */ |
1403
7a2d84112983
replaced the Matrix class with QMatrix4x4
Teemu Piippo <teemu@hecknology.net>
parents:
1327
diff
changeset
|
73 | QMatrix4x4 flipmatrix(Axis dimension) |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
74 | { |
1403
7a2d84112983
replaced the Matrix class with QMatrix4x4
Teemu Piippo <teemu@hecknology.net>
parents:
1327
diff
changeset
|
75 | QMatrix4x4 result; |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
76 | |
1304 | 77 | switch (dimension) |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
78 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
79 | case X: |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
80 | result(0, 0) = -1; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
81 | break; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
82 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
83 | case Y: |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
84 | result(1, 1) = -1; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
85 | break; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
86 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
87 | case Z: |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
88 | result(2, 2) = -1; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
89 | break; |
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 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
92 | return result; |
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 | /* |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
96 | * 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
|
97 | */ |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
98 | void invert(LDObject* obj, DocumentManager* context) |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
99 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
100 | if (obj->numPolygonVertices() > 0) |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
101 | { |
1304 | 102 | // 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
|
103 | QVector<Vertex> vertices; |
1303 | 104 | vertices.resize(obj->numPolygonVertices()); |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
105 | |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
106 | for (int i = 0; i < vertices.size(); i += 1) |
1303 | 107 | vertices[vertices.size() - 1 - i] = obj->vertex(i); |
108 | ||
109 | for (int i = 0; i < vertices.size(); i += 1) | |
110 | obj->setVertex(i, vertices[i]); | |
1301
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 | else if (obj->type() == LDObjectType::SubfileReference) |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
113 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
114 | // 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
|
115 | Model model {context}; |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
116 | LDSubfileReference* reference = static_cast<LDSubfileReference*>(obj); |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
117 | reference->fileInfo(context)->inlineContents(model, true, false); |
1304 | 118 | Axis flatDimension; |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
119 | |
1304 | 120 | if (::isflat(&model, &flatDimension)) |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
121 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
122 | reference->setTransformationMatrix( |
1304 | 123 | reference->transformationMatrix() * ::flipmatrix(flatDimension) |
1301
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 | } |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
126 | else |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
127 | { |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
128 | // Subfile is not flat. Resort to invertnext. |
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
129 | reference->setInverted(not reference->isInverted()); |
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 | } |
1412
f5eb947a2e7f
added support for inverting circular primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1403
diff
changeset
|
132 | else if (obj->type() == LDObjectType::CircularPrimitive) |
f5eb947a2e7f
added support for inverting circular primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1403
diff
changeset
|
133 | { |
f5eb947a2e7f
added support for inverting circular primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1403
diff
changeset
|
134 | auto primitive = static_cast<LDCircularPrimitive*>(obj); |
f5eb947a2e7f
added support for inverting circular primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1403
diff
changeset
|
135 | |
f5eb947a2e7f
added support for inverting circular primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1403
diff
changeset
|
136 | if (primitive->isFlat()) |
f5eb947a2e7f
added support for inverting circular primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1403
diff
changeset
|
137 | primitive->setTransformationMatrix(primitive->transformationMatrix() * ::flipmatrix(Y)); |
f5eb947a2e7f
added support for inverting circular primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1403
diff
changeset
|
138 | else |
f5eb947a2e7f
added support for inverting circular primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1403
diff
changeset
|
139 | primitive->setInverted(not primitive->isInverted()); |
f5eb947a2e7f
added support for inverting circular primitives
Teemu Piippo <teemu@hecknology.net>
parents:
1403
diff
changeset
|
140 | } |
1301
4a4e9fb9da76
moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
141 | } |
1305
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
142 | |
1327 | 143 | /* |
144 | * Inverts the winding of a polygon. | |
145 | */ | |
1305
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
146 | void invertPolygon(LDPolygon& polygon) |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
147 | { |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
148 | switch (polygon.numPolygonVertices()) |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
149 | { |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
150 | case 2: |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
151 | case 3: |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
152 | 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
|
153 | break; |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
154 | |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
155 | case 4: |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
156 | 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
|
157 | break; |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
158 | } |
31627acdd4b5
Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents:
1304
diff
changeset
|
159 | } |