18 |
18 |
19 #include "main.h" |
19 #include "main.h" |
20 #include "model.h" |
20 #include "model.h" |
21 #include "gl/common.h" |
21 #include "gl/common.h" |
22 #include "invert.h" |
22 #include "invert.h" |
23 |
23 #include "documentmanager.h" |
24 #if 0 |
24 #include "modeleditor.h" |
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 |
25 |
72 /* |
26 /* |
73 * Returns a matrix that causes a flip on the given dimension. |
27 * Returns a matrix that causes a flip on the given dimension. |
74 */ |
28 */ |
75 glm::mat4 math::flipmatrix(const Axis dimension) |
29 glm::mat4 math::flipmatrix(const Axis dimension) |
77 glm::mat4 result = glm::mat4(); |
31 glm::mat4 result = glm::mat4(); |
78 const int k = static_cast<int>(dimension); |
32 const int k = static_cast<int>(dimension); |
79 result[k][k] = -1; |
33 result[k][k] = -1; |
80 return result; |
34 return result; |
81 } |
35 } |
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 |
36 |
133 /* |
37 /* |
134 * Inverts the winding of a polygon. |
38 * Inverts the winding of a polygon. |
135 */ |
39 */ |
136 void gl::invert(gl::Polygon& polygon) |
40 void gl::invert(gl::Polygon& polygon) |