Wed, 22 Sep 2021 13:28:53 +0300
Document model.h
19 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
24 | 3 | * Copyright (C) 2013 - 2020 Teemu Piippo |
19 | 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 | ||
19 | #include "main.h" | |
20 | #include "model.h" | |
21 | #include "gl/common.h" | |
21 | 22 | #include "invert.h" |
19 | 23 | |
24 | #if 0 | |
25 | /* | |
26 | * Returns whether or not the document is flat. | |
27 | * If it is flat, the result is stored in *axis. | |
28 | */ | |
29 | bool isflat(Model* model, Axis* flatDimension) | |
30 | { | |
31 | // The dimensions that this model is potentially flat in. | |
32 | QVector<Axis> dimensions = {X, Y, Z}; | |
33 | ||
34 | // Iterate through everything in the subfile. If there is any vertex with a coordinate not at | |
35 | // zero, the subfile is not flat in that dimension. | |
36 | for (LDObject* subfileObject : model->objects()) | |
37 | { | |
38 | for (int i = 0; i < subfileObject->numVertices(); ++i) | |
39 | { | |
40 | Vertex const& v_i = subfileObject->vertex(i); | |
41 | ||
42 | if (not qFuzzyCompare(v_i.x, 0.0)) | |
43 | dimensions.removeOne(X); | |
44 | ||
45 | if (not qFuzzyCompare(v_i.y, 0.0)) | |
46 | dimensions.removeOne(Y); | |
47 | ||
48 | if (not qFuzzyCompare(v_i.z, 0.0)) | |
49 | dimensions.removeOne(Z); | |
50 | } | |
51 | ||
52 | // If there are no more dimensions left, we can exit the loop. | |
53 | if (dimensions.isEmpty()) | |
54 | break; | |
55 | } | |
56 | ||
57 | if (dimensions.size() == 1) | |
58 | { | |
59 | // The model is flat in one dimension, return that. | |
60 | // If the model is flat in two or three dimensions, it's not really a valid model. | |
61 | *flatDimension = dimensions[0]; | |
62 | return true; | |
63 | } | |
64 | else | |
65 | { | |
66 | // The model is not flat. | |
67 | return false; | |
68 | } | |
69 | } | |
70 | #endif | |
71 | ||
72 | /* | |
73 | * Returns a matrix that causes a flip on the given dimension. | |
74 | */ | |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
24
diff
changeset
|
75 | glm::mat4 math::flipmatrix(const Axis dimension) |
19 | 76 | { |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
24
diff
changeset
|
77 | glm::mat4 result = glm::mat4(); |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
24
diff
changeset
|
78 | const int k = static_cast<int>(dimension); |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
24
diff
changeset
|
79 | result[k][k] = -1; |
19 | 80 | return result; |
81 | } | |
82 | ||
83 | #if 0 | |
84 | /* | |
85 | * Inverts an LDObject so that its winding is changed. | |
86 | */ | |
87 | void invert(LDObject* obj, DocumentManager* context) | |
88 | { | |
89 | if (obj->numPolygonVertices() > 0) | |
90 | { | |
91 | // Object is vertex based, so change the order of the vertices. | |
92 | QVector<Vertex> vertices; | |
93 | vertices.resize(obj->numPolygonVertices()); | |
94 | ||
95 | for (int i = 0; i < vertices.size(); i += 1) | |
96 | vertices[vertices.size() - 1 - i] = obj->vertex(i); | |
97 | ||
98 | for (int i = 0; i < vertices.size(); i += 1) | |
99 | obj->setVertex(i, vertices[i]); | |
100 | } | |
101 | else if (obj->type() == LDObjectType::SubfileReference) | |
102 | { | |
103 | // Check whether subfile is flat. If it is, flip it on the axis on which it is flat. | |
104 | Model model {context}; | |
105 | LDSubfileReference* reference = static_cast<LDSubfileReference*>(obj); | |
106 | reference->fileInfo(context)->inlineContents(model, true, false); | |
107 | Axis flatDimension; | |
108 | ||
109 | if (::isflat(&model, &flatDimension)) | |
110 | { | |
111 | reference->setTransformationMatrix( | |
112 | reference->transformationMatrix() * ::flipmatrix(flatDimension) | |
113 | ); | |
114 | } | |
115 | else | |
116 | { | |
117 | // Subfile is not flat. Resort to invertnext. | |
118 | reference->setInverted(not reference->isInverted()); | |
119 | } | |
120 | } | |
121 | else if (obj->type() == LDObjectType::CircularPrimitive) | |
122 | { | |
123 | auto primitive = static_cast<LDCircularPrimitive*>(obj); | |
124 | ||
125 | if (primitive->isFlat()) | |
126 | primitive->setTransformationMatrix(primitive->transformationMatrix() * ::flipmatrix(Y)); | |
127 | else | |
128 | primitive->setInverted(not primitive->isInverted()); | |
129 | } | |
130 | } | |
131 | #endif | |
132 | ||
133 | /* | |
134 | * Inverts the winding of a polygon. | |
135 | */ | |
21 | 136 | void gl::invert(gl::Polygon& polygon) |
19 | 137 | { |
138 | switch (polygon.numPolygonVertices()) | |
139 | { | |
140 | case 2: | |
141 | case 3: | |
21 | 142 | // 0 1 => 1 0 |
143 | // 0 1 2 => 1 0 2 | |
19 | 144 | std::swap(polygon.vertices[0], polygon.vertices[1]); |
145 | break; | |
146 | case 4: | |
21 | 147 | // 0 1 2 3 => 0 3 2 1 |
19 | 148 | std::swap(polygon.vertices[1], polygon.vertices[3]); |
149 | break; | |
150 | } | |
151 | } |