src/basics.cpp

branch
projects
changeset 935
8d98ee0dc917
parent 931
85080f7a1c20
child 941
f895379d7fab
equal deleted inserted replaced
930:ab77deb851fa 935:8d98ee0dc917
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013 - 2015 Teemu Piippo
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 <QObject>
20 #include <QStringList>
21 #include <QTextStream>
22 #include <QFile>
23 #include <assert.h>
24 #include "main.h"
25 #include "basics.h"
26 #include "miscallenous.h"
27 #include "ldObject.h"
28 #include "ldDocument.h"
29
30 Vertex::Vertex() :
31 QVector3D() {}
32
33 Vertex::Vertex (const QVector3D& a) :
34 QVector3D (a) {}
35
36 Vertex::Vertex (qreal xpos, qreal ypos, qreal zpos) :
37 QVector3D(xpos, ypos, zpos) {}
38
39
40 void Vertex::transform (const Matrix& matr, const Vertex& pos)
41 {
42 double x2 = (matr[0] * x()) + (matr[1] * y()) + (matr[2] * z()) + pos.x();
43 double y2 = (matr[3] * x()) + (matr[4] * y()) + (matr[5] * z()) + pos.y();
44 double z2 = (matr[6] * x()) + (matr[7] * y()) + (matr[8] * z()) + pos.z();
45 setX (x2);
46 setY (y2);
47 setZ (z2);
48 }
49
50 void Vertex::apply (ApplyFunction func)
51 {
52 double newX = x(), newY = y(), newZ = z();
53 func (X, newX);
54 func (Y, newY);
55 func (Z, newZ);
56 *this = Vertex (newX, newY, newZ);
57 }
58
59 void Vertex::apply (ApplyConstFunction func) const
60 {
61 func (X, x());
62 func (Y, y());
63 func (Z, z());
64 }
65
66 double Vertex::operator[] (Axis ax) const
67 {
68 switch (ax)
69 {
70 case X: return x();
71 case Y: return y();
72 case Z: return z();
73 }
74
75 assert (false);
76 return 0.0;
77 }
78
79 void Vertex::setCoordinate (Axis ax, qreal value)
80 {
81 switch (ax)
82 {
83 case X: setX (value); break;
84 case Y: setY (value); break;
85 case Z: setZ (value); break;
86 }
87 }
88
89 QString Vertex::toString (bool mangled) const
90 {
91 if (mangled)
92 return format ("(%1, %2, %3)", x(), y(), z());
93
94 return format ("%1 %2 %3", x(), y(), z());
95 }
96
97 bool Vertex::operator< (const Vertex& other) const
98 {
99 if (x() != other.x()) return x() < other.x();
100 if (y() != other.y()) return y() < other.y();
101 if (z() != other.z()) return z() < other.z();
102 return false;
103 }
104
105 // =============================================================================
106 //
107 Matrix::Matrix (double vals[])
108 {
109 for (int i = 0; i < 9; ++i)
110 m_vals[i] = vals[i];
111 }
112
113 // =============================================================================
114 //
115 Matrix::Matrix (double fillval)
116 {
117 for (int i = 0; i < 9; ++i)
118 m_vals[i] = fillval;
119 }
120
121 // =============================================================================
122 //
123 Matrix::Matrix (const std::initializer_list<double>& vals)
124 {
125 assert (vals.size() == 9);
126 memcpy (&m_vals[0], vals.begin(), sizeof m_vals);
127 }
128
129 // =============================================================================
130 //
131 void Matrix::dump() const
132 {
133 for (int i = 0; i < 3; ++i)
134 {
135 for (int j = 0; j < 3; ++j)
136 print ("%1\t", m_vals[i * 3 + j]);
137
138 print ("\n");
139 }
140 }
141
142 // =============================================================================
143 //
144 QString Matrix::toString() const
145 {
146 QString val;
147
148 for (int i = 0; i < 9; ++i)
149 {
150 if (i > 0)
151 val += ' ';
152
153 val += QString::number (m_vals[i]);
154 }
155
156 return val;
157 }
158
159 // =============================================================================
160 //
161 void Matrix::zero()
162 {
163 memset (&m_vals[0], 0, sizeof m_vals);
164 }
165
166 // =============================================================================
167 //
168 Matrix Matrix::mult (const Matrix& other) const
169 {
170 Matrix val;
171 val.zero();
172
173 for (int i = 0; i < 3; ++i)
174 for (int j = 0; j < 3; ++j)
175 for (int k = 0; k < 3; ++k)
176 val[(i * 3) + j] += m_vals[(i * 3) + k] * other[(k * 3) + j];
177
178 return val;
179 }
180
181 // =============================================================================
182 //
183 Matrix& Matrix::operator= (const Matrix& other)
184 {
185 memcpy (&m_vals[0], &other.m_vals[0], sizeof m_vals);
186 return *this;
187 }
188
189 // =============================================================================
190 //
191 double Matrix::getDeterminant() const
192 {
193 return (value (0) * value (4) * value (8)) +
194 (value (1) * value (5) * value (6)) +
195 (value (2) * value (3) * value (7)) -
196 (value (2) * value (4) * value (6)) -
197 (value (1) * value (3) * value (8)) -
198 (value (0) * value (5) * value (7));
199 }
200
201 // =============================================================================
202 //
203 bool Matrix::operator== (const Matrix& other) const
204 {
205 for (int i = 0; i < 9; ++i)
206 {
207 if (value (i) != other[i])
208 return false;
209 }
210
211 return true;
212 }
213
214 // =============================================================================
215 //
216 LDBoundingBox::LDBoundingBox()
217 {
218 reset();
219 }
220
221 // =============================================================================
222 //
223 void LDBoundingBox::calculateFromCurrentDocument()
224 {
225 reset();
226
227 if (CurrentDocument() == null)
228 return;
229
230 for (LDObjectPtr obj : CurrentDocument()->objects())
231 calcObject (obj);
232 }
233
234 // =============================================================================
235 //
236 void LDBoundingBox::calcObject (LDObjectPtr obj)
237 {
238 switch (obj->type())
239 {
240 case OBJ_Line:
241 case OBJ_Triangle:
242 case OBJ_Quad:
243 case OBJ_CondLine:
244 {
245 for (int i = 0; i < obj->numVertices(); ++i)
246 calcVertex (obj->vertex (i));
247 } break;
248
249 case OBJ_Subfile:
250 {
251 LDSubfilePtr ref = obj.staticCast<LDSubfile>();
252 LDObjectList objs = ref->inlineContents (true, false);
253
254 for (LDObjectPtr obj : objs)
255 {
256 calcObject (obj);
257 obj->destroy();
258 }
259 }
260 break;
261
262 default:
263 break;
264 }
265 }
266
267 // =============================================================================
268 //
269 LDBoundingBox& LDBoundingBox::operator<< (const Vertex& v)
270 {
271 calcVertex (v);
272 return *this;
273 }
274
275 // =============================================================================
276 //
277 LDBoundingBox& LDBoundingBox::operator<< (LDObjectPtr obj)
278 {
279 calcObject (obj);
280 return *this;
281 }
282
283 // =============================================================================
284 //
285 void LDBoundingBox::calcVertex (const Vertex& vertex)
286 {
287 m_vertex0.setX (Min (vertex.x(), m_vertex0.x()));
288 m_vertex0.setY (Min (vertex.y(), m_vertex0.y()));
289 m_vertex0.setZ (Min (vertex.z(), m_vertex0.z()));
290 m_vertex1.setX (Max (vertex.x(), m_vertex1.x()));
291 m_vertex1.setY (Max (vertex.y(), m_vertex1.y()));
292 m_vertex1.setZ (Max (vertex.z(), m_vertex1.z()));
293 setEmpty (false);
294 }
295
296 // =============================================================================
297 //
298 void LDBoundingBox::reset()
299 {
300 m_vertex0 = Vertex (10000.0, 10000.0, 10000.0);
301 m_vertex1 = Vertex (-10000.0, -10000.0, -10000.0);
302 setEmpty (true);
303 }
304
305 // =============================================================================
306 //
307 double LDBoundingBox::longestMeasurement() const
308 {
309 double xscale = (m_vertex0.x() - m_vertex1.x());
310 double yscale = (m_vertex0.y() - m_vertex1.y());
311 double zscale = (m_vertex0.z() - m_vertex1.z());
312 double size = zscale;
313
314 if (xscale > yscale)
315 {
316 if (xscale > zscale)
317 size = xscale;
318 }
319 elif (yscale > zscale)
320 size = yscale;
321
322 if (Abs (size) >= 2.0)
323 return Abs (size / 2);
324
325 return 1.0;
326 }
327
328 // =============================================================================
329 //
330 Vertex LDBoundingBox::center() const
331 {
332 return Vertex (
333 (m_vertex0.x() + m_vertex1.x()) / 2,
334 (m_vertex0.y() + m_vertex1.y()) / 2,
335 (m_vertex0.z() + m_vertex1.z()) / 2);
336 }

mercurial