src/algorithms/invert.cpp

Tue, 03 Apr 2018 17:37:33 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 03 Apr 2018 17:37:33 +0300
changeset 1375
689121d162b6
parent 1327
b179ab2f2c4e
child 1403
7a2d84112983
permissions
-rw-r--r--

refactor

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
69a90bd2dba2 Happy new year 2018
Teemu Piippo <teemu@hecknology.net>
parents: 1318
diff changeset
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
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
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
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
29 // The dimensions that this model is potentially flat in.
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
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
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
32 // Iterate through everything in the subfile. If there is any vertex with a coordinate not at
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
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
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
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
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
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
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
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
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
50 // If there are no more dimensions left, we can exit the loop.
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
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
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
55 if (dimensions.size() == 1)
1301
4a4e9fb9da76 moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 {
1304
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
57 // The model is flat in one dimension, return that.
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
58 // If the model is flat in two or three dimensions, it's not really a valid model.
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
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
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
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
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
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
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
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
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
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
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
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
cb9ee14ab4cf reworked invert()
Teemu Piippo <teemu@hecknology.net>
parents: 1301
diff changeset
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
cb9ee14ab4cf reworked invert()
Teemu Piippo <teemu@hecknology.net>
parents: 1301
diff changeset
106 vertices[vertices.size() - 1 - i] = obj->vertex(i);
cb9ee14ab4cf reworked invert()
Teemu Piippo <teemu@hecknology.net>
parents: 1301
diff changeset
107
cb9ee14ab4cf reworked invert()
Teemu Piippo <teemu@hecknology.net>
parents: 1301
diff changeset
108 for (int i = 0; i < vertices.size(); i += 1)
cb9ee14ab4cf reworked invert()
Teemu Piippo <teemu@hecknology.net>
parents: 1301
diff changeset
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
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
117 Axis flatDimension;
1301
4a4e9fb9da76 moved inverting code into a new file
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
118
1304
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
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
bb3f43293cf8 added more comments
Teemu Piippo <teemu@hecknology.net>
parents: 1303
diff changeset
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
1327
b179ab2f2c4e added comment
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
133 /*
b179ab2f2c4e added comment
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
134 * Inverts the winding of a polygon.
b179ab2f2c4e added comment
Teemu Piippo <teemu@hecknology.net>
parents: 1326
diff changeset
135 */
1305
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1304
diff changeset
136 void invertPolygon(LDPolygon& polygon)
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1304
diff changeset
137 {
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1304
diff changeset
138 switch (polygon.numPolygonVertices())
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1304
diff changeset
139 {
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1304
diff changeset
140 case 2:
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1304
diff changeset
141 case 3:
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1304
diff changeset
142 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
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 case 4:
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1304
diff changeset
146 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
147 break;
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1304
diff changeset
148 }
31627acdd4b5 Bfc red/green view almost completely fixed
Teemu Piippo <teemu@hecknology.net>
parents: 1304
diff changeset
149 }

mercurial