Wed, 22 Jan 2020 22:43:28 +0200
added FindGLM.cmake
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 "matrix.h" | |
22 | #include "gl/common.h" | |
21 | 23 | #include "invert.h" |
19 | 24 | |
25 | #if 0 | |
26 | /* | |
27 | * Returns whether or not the document is flat. | |
28 | * If it is flat, the result is stored in *axis. | |
29 | */ | |
30 | bool isflat(Model* model, Axis* flatDimension) | |
31 | { | |
32 | // The dimensions that this model is potentially flat in. | |
33 | QVector<Axis> dimensions = {X, Y, Z}; | |
34 | ||
35 | // Iterate through everything in the subfile. If there is any vertex with a coordinate not at | |
36 | // zero, the subfile is not flat in that dimension. | |
37 | for (LDObject* subfileObject : model->objects()) | |
38 | { | |
39 | for (int i = 0; i < subfileObject->numVertices(); ++i) | |
40 | { | |
41 | Vertex const& v_i = subfileObject->vertex(i); | |
42 | ||
43 | if (not qFuzzyCompare(v_i.x, 0.0)) | |
44 | dimensions.removeOne(X); | |
45 | ||
46 | if (not qFuzzyCompare(v_i.y, 0.0)) | |
47 | dimensions.removeOne(Y); | |
48 | ||
49 | if (not qFuzzyCompare(v_i.z, 0.0)) | |
50 | dimensions.removeOne(Z); | |
51 | } | |
52 | ||
53 | // If there are no more dimensions left, we can exit the loop. | |
54 | if (dimensions.isEmpty()) | |
55 | break; | |
56 | } | |
57 | ||
58 | if (dimensions.size() == 1) | |
59 | { | |
60 | // The model is flat in one dimension, return that. | |
61 | // If the model is flat in two or three dimensions, it's not really a valid model. | |
62 | *flatDimension = dimensions[0]; | |
63 | return true; | |
64 | } | |
65 | else | |
66 | { | |
67 | // The model is not flat. | |
68 | return false; | |
69 | } | |
70 | } | |
71 | #endif | |
72 | ||
73 | /* | |
74 | * Returns a matrix that causes a flip on the given dimension. | |
75 | */ | |
21 | 76 | Matrix4x4 math::flipmatrix(const Axis dimension) |
19 | 77 | { |
78 | Matrix4x4 result = identity4x4; | |
21 | 79 | result(static_cast<int>(dimension), static_cast<int>(dimension)) = -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 | } |