1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013, 2014 Santeri 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 "Types.h" |
|
26 #include "Misc.h" |
|
27 #include "LDObject.h" |
|
28 #include "Document.h" |
|
29 |
|
30 // ============================================================================= |
|
31 // |
|
32 Vertex::Vertex (double x, double y, double z) |
|
33 { |
|
34 m_coords[X] = x; |
|
35 m_coords[Y] = y; |
|
36 m_coords[Z] = z; |
|
37 } |
|
38 |
|
39 // ============================================================================= |
|
40 // |
|
41 void Vertex::move (const Vertex& other) |
|
42 { |
|
43 for_axes (ax) |
|
44 m_coords[ax] += other[ax]; |
|
45 } |
|
46 |
|
47 // ============================================================================= |
|
48 // |
|
49 double Vertex::distanceTo (const Vertex& other) const |
|
50 { |
|
51 double dx = abs (x() - other.x()); |
|
52 double dy = abs (y() - other.y()); |
|
53 double dz = abs (z() - other.z()); |
|
54 return sqrt ((dx * dx) + (dy * dy) + (dz * dz)); |
|
55 } |
|
56 |
|
57 // ============================================================================= |
|
58 // |
|
59 Vertex Vertex::midpoint (const Vertex& other) |
|
60 { |
|
61 Vertex mid; |
|
62 |
|
63 for_axes (ax) |
|
64 mid[ax] = (getCoordinate (ax) + other[ax]) / 2; |
|
65 |
|
66 return mid; |
|
67 } |
|
68 |
|
69 // ============================================================================= |
|
70 // |
|
71 QString Vertex::toString (bool mangled) const |
|
72 { |
|
73 QString formatstr = "%1 %2 %3"; |
|
74 |
|
75 if (mangled) |
|
76 formatstr = "(%1, %2, %3)"; |
|
77 |
|
78 return format (formatstr, x(), y(), z()); |
|
79 } |
|
80 |
|
81 // ============================================================================= |
|
82 // |
|
83 void Vertex::transform (const Matrix& matr, const Vertex& pos) |
|
84 { |
|
85 double x2 = (matr[0] * x()) + (matr[1] * y()) + (matr[2] * z()) + pos[X]; |
|
86 double y2 = (matr[3] * x()) + (matr[4] * y()) + (matr[5] * z()) + pos[Y]; |
|
87 double z2 = (matr[6] * x()) + (matr[7] * y()) + (matr[8] * z()) + pos[Z]; |
|
88 |
|
89 x() = x2; |
|
90 y() = y2; |
|
91 z() = z2; |
|
92 } |
|
93 |
|
94 // ============================================================================= |
|
95 // |
|
96 Vertex Vertex::operator-() const |
|
97 { |
|
98 return Vertex (-m_coords[X], -m_coords[Y], -m_coords[Z]); |
|
99 } |
|
100 |
|
101 // ============================================================================= |
|
102 // |
|
103 bool Vertex::operator!= (const Vertex& other) const |
|
104 { |
|
105 return !operator== (other); |
|
106 } |
|
107 |
|
108 // ============================================================================= |
|
109 // |
|
110 bool Vertex::operator== (const Vertex& other) const |
|
111 { |
|
112 return getCoordinate (X) == other[X] && |
|
113 getCoordinate (Y) == other[Y] && |
|
114 getCoordinate (Z) == other[Z]; |
|
115 } |
|
116 |
|
117 // ============================================================================= |
|
118 // |
|
119 Vertex& Vertex::operator/= (const double d) |
|
120 { |
|
121 for_axes (ax) |
|
122 m_coords[ax] /= d; |
|
123 |
|
124 return *this; |
|
125 } |
|
126 |
|
127 // ============================================================================= |
|
128 // |
|
129 Vertex Vertex::operator/ (const double d) const |
|
130 { |
|
131 Vertex other (*this); |
|
132 return other /= d; |
|
133 } |
|
134 |
|
135 // ============================================================================= |
|
136 // |
|
137 Vertex& Vertex::operator+= (const Vertex& other) |
|
138 { |
|
139 move (other); |
|
140 return *this; |
|
141 } |
|
142 |
|
143 // ============================================================================= |
|
144 // |
|
145 Vertex Vertex::operator+ (const Vertex& other) const |
|
146 { |
|
147 Vertex newvert (*this); |
|
148 newvert.move (other); |
|
149 return newvert; |
|
150 } |
|
151 |
|
152 // ============================================================================= |
|
153 // |
|
154 int Vertex::operator< (const Vertex& other) const |
|
155 { |
|
156 if (operator== (other)) |
|
157 return false; |
|
158 |
|
159 if (getCoordinate (X) < other[X]) |
|
160 return true; |
|
161 |
|
162 if (getCoordinate (X) > other[X]) |
|
163 return false; |
|
164 |
|
165 if (getCoordinate (Y) < other[Y]) |
|
166 return true; |
|
167 |
|
168 if (getCoordinate (Y) > other[Y]) |
|
169 return false; |
|
170 |
|
171 return getCoordinate (Z) < other[Z]; |
|
172 } |
|
173 |
|
174 // ============================================================================= |
|
175 // |
|
176 Matrix::Matrix (double vals[]) |
|
177 { |
|
178 for (int i = 0; i < 9; ++i) |
|
179 m_vals[i] = vals[i]; |
|
180 } |
|
181 |
|
182 // ============================================================================= |
|
183 // |
|
184 Matrix::Matrix (double fillval) |
|
185 { |
|
186 for (int i = 0; i < 9; ++i) |
|
187 m_vals[i] = fillval; |
|
188 } |
|
189 |
|
190 // ============================================================================= |
|
191 // |
|
192 Matrix::Matrix (const std::initializer_list< double >& vals) |
|
193 { |
|
194 assert (vals.size() == 9); |
|
195 memcpy (&m_vals[0], & (*vals.begin()), sizeof m_vals); |
|
196 } |
|
197 |
|
198 // ============================================================================= |
|
199 // |
|
200 void Matrix::dump() const |
|
201 { |
|
202 for (int i = 0; i < 3; ++i) |
|
203 { |
|
204 for (int j = 0; j < 3; ++j) |
|
205 print ("%1\t", m_vals[ (i * 3) + j]); |
|
206 |
|
207 print ("\n"); |
|
208 } |
|
209 } |
|
210 |
|
211 // ============================================================================= |
|
212 // |
|
213 QString Matrix::toString() const |
|
214 { |
|
215 QString val; |
|
216 |
|
217 for (int i = 0; i < 9; ++i) |
|
218 { |
|
219 if (i > 0) |
|
220 val += ' '; |
|
221 |
|
222 val += QString::number (m_vals[i]); |
|
223 } |
|
224 |
|
225 return val; |
|
226 } |
|
227 |
|
228 // ============================================================================= |
|
229 // |
|
230 void Matrix::zero() |
|
231 { |
|
232 memset (&m_vals[0], 0, sizeof m_vals); |
|
233 } |
|
234 |
|
235 // ============================================================================= |
|
236 // |
|
237 Matrix Matrix::mult (const Matrix& other) const |
|
238 { |
|
239 Matrix val; |
|
240 val.zero(); |
|
241 |
|
242 for (int i = 0; i < 3; ++i) |
|
243 for (int j = 0; j < 3; ++j) |
|
244 for (int k = 0; k < 3; ++k) |
|
245 val[(i * 3) + j] += m_vals[(i * 3) + k] * other[(k * 3) + j]; |
|
246 |
|
247 return val; |
|
248 } |
|
249 |
|
250 // ============================================================================= |
|
251 // |
|
252 Matrix& Matrix::operator= (const Matrix& other) |
|
253 { |
|
254 memcpy (&m_vals[0], &other.m_vals[0], sizeof m_vals); |
|
255 return *this; |
|
256 } |
|
257 |
|
258 // ============================================================================= |
|
259 // |
|
260 double Matrix::getDeterminant() const |
|
261 { |
|
262 return (value (0) * value (4) * value (8)) + |
|
263 (value (1) * value (5) * value (6)) + |
|
264 (value (2) * value (3) * value (7)) - |
|
265 (value (2) * value (4) * value (6)) - |
|
266 (value (1) * value (3) * value (8)) - |
|
267 (value (0) * value (5) * value (7)); |
|
268 } |
|
269 |
|
270 // ============================================================================= |
|
271 // |
|
272 bool Matrix::operator== (const Matrix& other) const |
|
273 { |
|
274 for (int i = 0; i < 9; ++i) |
|
275 if (value (i) != other[i]) |
|
276 return false; |
|
277 |
|
278 return true; |
|
279 } |
|
280 |
|
281 // ============================================================================= |
|
282 // |
|
283 LDBoundingBox::LDBoundingBox() |
|
284 { |
|
285 reset(); |
|
286 } |
|
287 |
|
288 // ============================================================================= |
|
289 // |
|
290 void LDBoundingBox::calculateFromCurrentDocument() |
|
291 { |
|
292 reset(); |
|
293 |
|
294 if (!getCurrentDocument()) |
|
295 return; |
|
296 |
|
297 for (LDObject* obj : getCurrentDocument()->objects()) |
|
298 calcObject (obj); |
|
299 } |
|
300 |
|
301 // ============================================================================= |
|
302 // |
|
303 void LDBoundingBox::calcObject (LDObject* obj) |
|
304 { |
|
305 switch (obj->type()) |
|
306 { |
|
307 case LDObject::ELine: |
|
308 case LDObject::ETriangle: |
|
309 case LDObject::EQuad: |
|
310 case LDObject::ECondLine: |
|
311 { |
|
312 for (int i = 0; i < obj->vertices(); ++i) |
|
313 calcVertex (obj->vertex (i)); |
|
314 } break; |
|
315 |
|
316 case LDObject::ESubfile: |
|
317 { |
|
318 LDSubfile* ref = static_cast<LDSubfile*> (obj); |
|
319 LDObjectList objs = ref->inlineContents (true, false); |
|
320 |
|
321 for (LDObject * obj : objs) |
|
322 { |
|
323 calcObject (obj); |
|
324 obj->destroy(); |
|
325 } |
|
326 } |
|
327 break; |
|
328 |
|
329 default: |
|
330 break; |
|
331 } |
|
332 } |
|
333 |
|
334 // ============================================================================= |
|
335 // |
|
336 LDBoundingBox& LDBoundingBox::operator<< (const Vertex& v) |
|
337 { |
|
338 calcVertex (v); |
|
339 return *this; |
|
340 } |
|
341 |
|
342 // ============================================================================= |
|
343 // |
|
344 LDBoundingBox& LDBoundingBox::operator<< (LDObject* obj) |
|
345 { |
|
346 calcObject (obj); |
|
347 return *this; |
|
348 } |
|
349 |
|
350 // ============================================================================= |
|
351 // |
|
352 void LDBoundingBox::calcVertex (const Vertex& vertex) |
|
353 { |
|
354 for_axes (ax) |
|
355 { |
|
356 m_vertex0[ax] = min (vertex[ax], m_vertex0[ax]); |
|
357 m_vertex1[ax] = max (vertex[ax], m_vertex1[ax]); |
|
358 } |
|
359 |
|
360 setEmpty (false); |
|
361 } |
|
362 |
|
363 // ============================================================================= |
|
364 // |
|
365 void LDBoundingBox::reset() |
|
366 { |
|
367 m_vertex0[X] = m_vertex0[Y] = m_vertex0[Z] = 10000.0; |
|
368 m_vertex1[X] = m_vertex1[Y] = m_vertex1[Z] = -10000.0; |
|
369 setEmpty (true); |
|
370 } |
|
371 |
|
372 // ============================================================================= |
|
373 // |
|
374 double LDBoundingBox::longestMeasurement() const |
|
375 { |
|
376 double xscale = (m_vertex0[X] - m_vertex1[X]); |
|
377 double yscale = (m_vertex0[Y] - m_vertex1[Y]); |
|
378 double zscale = (m_vertex0[Z] - m_vertex1[Z]); |
|
379 double size = zscale; |
|
380 |
|
381 if (xscale > yscale) |
|
382 { |
|
383 if (xscale > zscale) |
|
384 size = xscale; |
|
385 } |
|
386 elif (yscale > zscale) |
|
387 size = yscale; |
|
388 |
|
389 if (abs (size) >= 2.0f) |
|
390 return abs (size / 2); |
|
391 |
|
392 return 1.0f; |
|
393 } |
|
394 |
|
395 // ============================================================================= |
|
396 // |
|
397 Vertex LDBoundingBox::center() const |
|
398 { |
|
399 return Vertex ( |
|
400 (m_vertex0[X] + m_vertex1[X]) / 2, |
|
401 (m_vertex0[Y] + m_vertex1[Y]) / 2, |
|
402 (m_vertex0[Z] + m_vertex1[Z]) / 2); |
|
403 } |
|