25 #include "types.h" |
25 #include "types.h" |
26 #include "misc.h" |
26 #include "misc.h" |
27 #include "ldtypes.h" |
27 #include "ldtypes.h" |
28 #include "file.h" |
28 #include "file.h" |
29 |
29 |
|
30 // ============================================================================= |
|
31 // ----------------------------------------------------------------------------- |
30 str DoFormat (List<StringFormatArg> args) { |
32 str DoFormat (List<StringFormatArg> args) { |
31 assert (args.size() >= 1); |
33 assert (args.size() >= 1); |
32 str text = args[0].value(); |
34 str text = args[0].value(); |
33 |
35 |
34 for (uchar i = 1; i < args.size(); ++i) |
36 for (uchar i = 1; i < args.size(); ++i) |
35 text = text.arg (args[i].value()); |
37 text = text.arg (args[i].value()); |
36 |
38 |
37 return text; |
39 return text; |
38 } |
40 } |
39 |
41 |
|
42 // ============================================================================= |
|
43 // ----------------------------------------------------------------------------- |
40 vertex::vertex (double x, double y, double z) { |
44 vertex::vertex (double x, double y, double z) { |
41 m_coords[X] = x; |
45 m_coords[X] = x; |
42 m_coords[Y] = y; |
46 m_coords[Y] = y; |
43 m_coords[Z] = z; |
47 m_coords[Z] = z; |
44 } |
48 } |
45 |
49 |
46 // ============================================================================= |
50 // ============================================================================= |
|
51 // ----------------------------------------------------------------------------- |
47 void vertex::move (const vertex& other) { |
52 void vertex::move (const vertex& other) { |
48 for (const Axis ax : g_Axes) |
53 for (const Axis ax : g_Axes) |
49 m_coords[ax] += other[ax]; |
54 m_coords[ax] += other[ax]; |
50 } |
55 } |
51 |
56 |
52 // ============================================================================= |
57 // ============================================================================= |
|
58 // ----------------------------------------------------------------------------- |
53 vertex vertex::midpoint (const vertex& other) { |
59 vertex vertex::midpoint (const vertex& other) { |
54 vertex mid; |
60 vertex mid; |
55 |
61 |
56 for (const Axis ax : g_Axes) |
62 for (const Axis ax : g_Axes) |
57 mid[ax] = (m_coords[ax] + other[ax]) / 2; |
63 mid[ax] = (m_coords[ax] + other[ax]) / 2; |
58 |
64 |
59 return mid; |
65 return mid; |
60 } |
66 } |
61 |
67 |
62 // ============================================================================= |
68 // ============================================================================= |
|
69 // ----------------------------------------------------------------------------- |
63 str vertex::stringRep (bool mangled) const { |
70 str vertex::stringRep (bool mangled) const { |
64 str fmtstr = "%1 %2 %3"; |
71 str fmtstr = "%1 %2 %3"; |
65 if (mangled) |
72 if (mangled) |
66 fmtstr = "(%1, %2, %3)"; |
73 fmtstr = "(%1, %2, %3)"; |
67 |
74 |
68 return fmt (fmtstr, coord (X), coord (Y), coord (Z)); |
75 return fmt (fmtstr, coord (X), coord (Y), coord (Z)); |
69 } |
76 } |
70 |
77 |
71 // ============================================================================= |
78 // ============================================================================= |
|
79 // ----------------------------------------------------------------------------- |
72 void vertex::transform (matrix matr, vertex pos) { |
80 void vertex::transform (matrix matr, vertex pos) { |
73 double x2 = (matr[0] * x()) + (matr[1] * y()) + (matr[2] * z()) + pos[X]; |
81 double x2 = (matr[0] * x()) + (matr[1] * y()) + (matr[2] * z()) + pos[X]; |
74 double y2 = (matr[3] * x()) + (matr[4] * y()) + (matr[5] * z()) + pos[Y]; |
82 double y2 = (matr[3] * x()) + (matr[4] * y()) + (matr[5] * z()) + pos[Y]; |
75 double z2 = (matr[6] * x()) + (matr[7] * y()) + (matr[8] * z()) + pos[Z]; |
83 double z2 = (matr[6] * x()) + (matr[7] * y()) + (matr[8] * z()) + pos[Z]; |
76 |
84 |
77 x() = x2; |
85 x() = x2; |
78 y() = y2; |
86 y() = y2; |
79 z() = z2; |
87 z() = z2; |
80 } |
88 } |
81 |
89 |
|
90 // ============================================================================= |
|
91 // ----------------------------------------------------------------------------- |
82 vertex vertex::operator-() const { |
92 vertex vertex::operator-() const { |
83 return vertex (-m_coords[X], -m_coords[Y], -m_coords[Z]); |
93 return vertex (-m_coords[X], -m_coords[Y], -m_coords[Z]); |
84 } |
94 } |
85 |
95 |
|
96 // ============================================================================= |
|
97 // ----------------------------------------------------------------------------- |
86 bool vertex::operator!= (const vertex& other) const { |
98 bool vertex::operator!= (const vertex& other) const { |
87 return !operator== (other); |
99 return !operator== (other); |
88 } |
100 } |
89 |
101 |
|
102 // ============================================================================= |
|
103 // ----------------------------------------------------------------------------- |
90 double& vertex::operator[] (const Axis ax) { |
104 double& vertex::operator[] (const Axis ax) { |
91 return coord ((ushort) ax); |
105 return coord ((ushort) ax); |
92 } |
106 } |
93 |
107 |
94 const double& vertex::operator[] (const Axis ax) const { |
108 const double& vertex::operator[] (const Axis ax) const { |
101 |
115 |
102 const double& vertex::operator[] (const int ax) const { |
116 const double& vertex::operator[] (const int ax) const { |
103 return coord (ax); |
117 return coord (ax); |
104 } |
118 } |
105 |
119 |
|
120 // ============================================================================= |
|
121 // ----------------------------------------------------------------------------- |
106 bool vertex::operator== (const vertex& other) const { |
122 bool vertex::operator== (const vertex& other) const { |
107 return coord (X) == other[X] && |
123 return coord (X) == other[X] && |
108 coord (Y) == other[Y] && |
124 coord (Y) == other[Y] && |
109 coord (Z) == other[Z]; |
125 coord (Z) == other[Z]; |
110 } |
126 } |
111 |
127 |
|
128 // ============================================================================= |
|
129 // ----------------------------------------------------------------------------- |
112 vertex& vertex::operator/= (const double d) { |
130 vertex& vertex::operator/= (const double d) { |
113 for (const Axis ax : g_Axes) |
131 for (const Axis ax : g_Axes) |
114 m_coords[ax] /= d; |
132 m_coords[ax] /= d; |
115 |
133 |
116 return *this; |
134 return *this; |
117 } |
135 } |
118 |
136 |
|
137 // ============================================================================= |
|
138 // ----------------------------------------------------------------------------- |
119 vertex vertex::operator/ (const double d) const { |
139 vertex vertex::operator/ (const double d) const { |
120 vertex other (*this); |
140 vertex other (*this); |
121 return other /= d; |
141 return other /= d; |
122 } |
142 } |
123 |
143 |
|
144 // ============================================================================= |
|
145 // ----------------------------------------------------------------------------- |
124 vertex& vertex::operator+= (const vertex& other) { |
146 vertex& vertex::operator+= (const vertex& other) { |
125 move (other); |
147 move (other); |
126 return *this; |
148 return *this; |
127 } |
149 } |
128 |
150 |
|
151 // ============================================================================= |
|
152 // ----------------------------------------------------------------------------- |
129 vertex vertex::operator+ (const vertex& other) const { |
153 vertex vertex::operator+ (const vertex& other) const { |
130 vertex newvert (*this); |
154 vertex newvert (*this); |
131 newvert.move (other); |
155 newvert.move (other); |
132 return newvert; |
156 return newvert; |
133 } |
157 } |
134 |
158 |
|
159 // ============================================================================= |
|
160 // ----------------------------------------------------------------------------- |
135 int vertex::operator< (const vertex& other) const { |
161 int vertex::operator< (const vertex& other) const { |
136 if (operator== (other)) |
162 if (operator== (other)) |
137 return false; |
163 return false; |
138 |
164 |
139 if (coord (X) < other[X]) |
165 if (coord (X) < other[X]) |
150 |
176 |
151 return coord (Z) < other[Z]; |
177 return coord (Z) < other[Z]; |
152 } |
178 } |
153 |
179 |
154 // ============================================================================= |
180 // ============================================================================= |
|
181 // ----------------------------------------------------------------------------- |
155 matrix::matrix (double vals[]) { |
182 matrix::matrix (double vals[]) { |
156 for (short i = 0; i < 9; ++i) |
183 for (short i = 0; i < 9; ++i) |
157 m_vals[i] = vals[i]; |
184 m_vals[i] = vals[i]; |
158 } |
185 } |
159 |
186 |
|
187 // ============================================================================= |
|
188 // ----------------------------------------------------------------------------- |
160 matrix::matrix (double fillval) { |
189 matrix::matrix (double fillval) { |
161 for (short i = 0; i < 9; ++i) |
190 for (short i = 0; i < 9; ++i) |
162 m_vals[i] = fillval; |
191 m_vals[i] = fillval; |
163 } |
192 } |
164 |
193 |
|
194 // ============================================================================= |
|
195 // ----------------------------------------------------------------------------- |
165 matrix::matrix (initlist<double> vals) { |
196 matrix::matrix (initlist<double> vals) { |
166 assert (vals.size() == 9); |
197 assert (vals.size() == 9); |
167 memcpy (&m_vals[0], & (*vals.begin()), sizeof m_vals); |
198 memcpy (&m_vals[0], & (*vals.begin()), sizeof m_vals); |
168 } |
199 } |
169 |
200 |
|
201 // ============================================================================= |
|
202 // ----------------------------------------------------------------------------- |
170 void matrix::puts() const { |
203 void matrix::puts() const { |
171 for (short i = 0; i < 3; ++i) { |
204 for (short i = 0; i < 3; ++i) { |
172 for (short j = 0; j < 3; ++j) |
205 for (short j = 0; j < 3; ++j) |
173 print ("%1\t", m_vals[ (i * 3) + j]); |
206 print ("%1\t", m_vals[ (i * 3) + j]); |
174 |
207 |
175 print ("\n"); |
208 print ("\n"); |
176 } |
209 } |
177 } |
210 } |
178 |
211 |
179 // ============================================================================= |
212 // ============================================================================= |
|
213 // ----------------------------------------------------------------------------- |
180 str matrix::stringRep() const { |
214 str matrix::stringRep() const { |
181 str val; |
215 str val; |
182 |
216 |
183 for (short i = 0; i < 9; ++i) { |
217 for (short i = 0; i < 9; ++i) { |
184 if (i > 0) |
218 if (i > 0) |
207 |
243 |
208 return val; |
244 return val; |
209 } |
245 } |
210 |
246 |
211 // ============================================================================= |
247 // ============================================================================= |
|
248 // ----------------------------------------------------------------------------- |
212 matrix& matrix::operator= (matrix other) { |
249 matrix& matrix::operator= (matrix other) { |
213 memcpy (&m_vals[0], &other.m_vals[0], sizeof (double) * 9); |
250 memcpy (&m_vals[0], &other.m_vals[0], sizeof (double) * 9); |
214 return *this; |
251 return *this; |
215 } |
252 } |
216 |
253 |
217 // ============================================================================= |
254 // ============================================================================= |
|
255 // ----------------------------------------------------------------------------- |
218 double matrix::determinant() const { |
256 double matrix::determinant() const { |
219 return (val (0) * val (4) * val (8)) + |
257 return (val (0) * val (4) * val (8)) + |
220 (val (1) * val (5) * val (6)) + |
258 (val (1) * val (5) * val (6)) + |
221 (val (2) * val (3) * val (7)) - |
259 (val (2) * val (3) * val (7)) - |
222 (val (2) * val (4) * val (6)) - |
260 (val (2) * val (4) * val (6)) - |
223 (val (1) * val (3) * val (8)) - |
261 (val (1) * val (3) * val (8)) - |
224 (val (0) * val (5) * val (7)); |
262 (val (0) * val (5) * val (7)); |
225 } |
263 } |
226 |
264 |
227 // ============================================================================= |
265 // ============================================================================= |
|
266 // ----------------------------------------------------------------------------- |
228 StringFormatArg::StringFormatArg (const str& v) { |
267 StringFormatArg::StringFormatArg (const str& v) { |
229 m_val = v; |
268 m_val = v; |
230 } |
269 } |
231 |
270 |
232 StringFormatArg::StringFormatArg (const char& v) { |
271 StringFormatArg::StringFormatArg (const char& v) { |
335 delete m_file; |
379 delete m_file; |
336 m_file = null; |
380 m_file = null; |
337 return false; |
381 return false; |
338 } |
382 } |
339 |
383 |
|
384 // ============================================================================= |
|
385 // ----------------------------------------------------------------------------- |
340 File::iterator File::begin() { |
386 File::iterator File::begin() { |
341 return iterator (this); |
387 return iterator (this); |
342 } |
388 } |
343 |
389 |
344 File::iterator& File::end() { |
390 File::iterator& File::end() { |
345 return m_endIterator; |
391 return m_endIterator; |
346 } |
392 } |
347 |
393 |
|
394 // ============================================================================= |
|
395 // ----------------------------------------------------------------------------- |
348 void File::write (str msg) { |
396 void File::write (str msg) { |
349 m_file->write (msg.toUtf8(), msg.length()); |
397 m_file->write (msg.toUtf8(), msg.length()); |
350 } |
398 } |
351 |
399 |
|
400 // ============================================================================= |
|
401 // ----------------------------------------------------------------------------- |
352 bool File::readLine (str& line) { |
402 bool File::readLine (str& line) { |
353 if (!m_textstream || m_textstream->atEnd()) |
403 if (!m_textstream || m_textstream->atEnd()) |
354 return false; |
404 return false; |
355 |
405 |
356 line = m_textstream->readLine(); |
406 line = m_textstream->readLine(); |
357 return true; |
407 return true; |
358 } |
408 } |
359 |
409 |
|
410 // ============================================================================= |
|
411 // ----------------------------------------------------------------------------- |
360 bool File::atEnd() const { |
412 bool File::atEnd() const { |
361 if (!m_textstream) |
413 if (!m_textstream) |
362 fatal ("cannot use atEnd on a null file"); |
414 fatal ("cannot use atEnd on a null file"); |
363 |
415 |
364 return m_textstream->atEnd(); |
416 return m_textstream->atEnd(); |
365 } |
417 } |
366 |
418 |
|
419 // ============================================================================= |
|
420 // ----------------------------------------------------------------------------- |
367 bool File::isNull() const { |
421 bool File::isNull() const { |
368 return m_file == null; |
422 return m_file == null; |
369 } |
423 } |
370 |
424 |
371 bool File::operator!() const { |
425 bool File::operator!() const { |
372 return isNull(); |
426 return isNull(); |
373 } |
427 } |
374 |
428 |
|
429 // ============================================================================= |
|
430 // ----------------------------------------------------------------------------- |
375 void File::close() { |
431 void File::close() { |
376 if (!m_file) |
432 if (!m_file) |
377 return; |
433 return; |
378 |
434 |
379 delete m_file; |
435 delete m_file; |
383 delete m_textstream; |
439 delete m_textstream; |
384 m_textstream = null; |
440 m_textstream = null; |
385 } |
441 } |
386 } |
442 } |
387 |
443 |
|
444 // ============================================================================= |
|
445 // ----------------------------------------------------------------------------- |
388 bool File::flush() { |
446 bool File::flush() { |
389 return m_file->flush(); |
447 return m_file->flush(); |
390 } |
448 } |
391 |
449 |
|
450 // ============================================================================= |
|
451 // ----------------------------------------------------------------------------- |
392 File::operator bool() const { |
452 File::operator bool() const { |
393 return !isNull(); |
453 return !isNull(); |
394 } |
454 } |
395 |
455 |
|
456 // ============================================================================= |
|
457 // ----------------------------------------------------------------------------- |
396 void File::rewind() { |
458 void File::rewind() { |
397 m_file->seek (0); |
459 m_file->seek (0); |
398 } |
460 } |
399 |
461 |
|
462 // ============================================================================= |
|
463 // ----------------------------------------------------------------------------- |
400 File::iterator::iterator (File* f) : m_file (f) { |
464 File::iterator::iterator (File* f) : m_file (f) { |
401 operator++(); |
465 operator++(); |
402 } |
466 } |
403 |
467 |
|
468 // ============================================================================= |
|
469 // ----------------------------------------------------------------------------- |
404 void File::iterator::operator++() { |
470 void File::iterator::operator++() { |
405 m_gotdata = m_file->readLine (m_text); |
471 m_gotdata = m_file->readLine (m_text); |
406 } |
472 } |
407 |
473 |
|
474 // ============================================================================= |
|
475 // ----------------------------------------------------------------------------- |
408 str File::iterator::operator*() { |
476 str File::iterator::operator*() { |
409 return m_text; |
477 return m_text; |
410 } |
478 } |
411 |
479 |
|
480 // ============================================================================= |
412 // The prime contestant for the weirdest operator== 2013 award? |
481 // The prime contestant for the weirdest operator== 2013 award? |
|
482 // ----------------------------------------------------------------------------- |
413 bool File::iterator::operator== (File::iterator& other) { |
483 bool File::iterator::operator== (File::iterator& other) { |
414 return (other.m_file == null && !m_gotdata); |
484 return (other.m_file == null && !m_gotdata); |
415 } |
485 } |
416 |
486 |
|
487 // ============================================================================= |
|
488 // ----------------------------------------------------------------------------- |
417 bool File::iterator::operator!= (File::iterator& other) { |
489 bool File::iterator::operator!= (File::iterator& other) { |
418 return !operator== (other); |
490 return !operator== (other); |
419 } |
491 } |
420 |
492 |
421 // ============================================================================= |
493 // ============================================================================= |
422 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
494 // ----------------------------------------------------------------------------- |
423 // ============================================================================= |
|
424 LDBoundingBox::LDBoundingBox() { |
495 LDBoundingBox::LDBoundingBox() { |
425 reset(); |
496 reset(); |
426 } |
497 } |
427 |
498 |
428 // ============================================================================= |
499 // ============================================================================= |
429 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
500 // ----------------------------------------------------------------------------- |
430 // ============================================================================= |
|
431 void LDBoundingBox::calculate() { |
501 void LDBoundingBox::calculate() { |
432 reset(); |
502 reset(); |
433 |
503 |
434 if (!LDFile::current()) |
504 if (!LDFile::current()) |
435 return; |
505 return; |
466 default: |
535 default: |
467 break; |
536 break; |
468 } |
537 } |
469 } |
538 } |
470 |
539 |
|
540 // ============================================================================= |
|
541 // ----------------------------------------------------------------------------- |
471 LDBoundingBox& LDBoundingBox::operator<< (const vertex& v) { |
542 LDBoundingBox& LDBoundingBox::operator<< (const vertex& v) { |
472 calcVertex (v); |
543 calcVertex (v); |
473 return *this; |
544 return *this; |
474 } |
545 } |
475 |
546 |
|
547 // ============================================================================= |
|
548 // ----------------------------------------------------------------------------- |
476 LDBoundingBox& LDBoundingBox::operator<< (LDObject* obj) { |
549 LDBoundingBox& LDBoundingBox::operator<< (LDObject* obj) { |
477 calcObject (obj); |
550 calcObject (obj); |
478 return *this; |
551 return *this; |
479 } |
552 } |
480 |
553 |
481 // ============================================================================= |
554 // ============================================================================= |
482 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
555 // ----------------------------------------------------------------------------- |
483 // ============================================================================= |
|
484 void LDBoundingBox::calcVertex (const vertex& v) { |
556 void LDBoundingBox::calcVertex (const vertex& v) { |
485 for (const Axis ax : g_Axes) { |
557 for (const Axis ax : g_Axes) { |
486 if (v[ax] < m_v0[ax]) |
558 if (v[ax] < m_v0[ax]) |
487 m_v0[ax] = v[ax]; |
559 m_v0[ax] = v[ax]; |
488 |
560 |
492 |
564 |
493 m_empty = false; |
565 m_empty = false; |
494 } |
566 } |
495 |
567 |
496 // ============================================================================= |
568 // ============================================================================= |
497 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
569 // ----------------------------------------------------------------------------- |
498 // ============================================================================= |
|
499 void LDBoundingBox::reset() { |
570 void LDBoundingBox::reset() { |
500 m_v0[X] = m_v0[Y] = m_v0[Z] = 0x7FFFFFFF; |
571 m_v0[X] = m_v0[Y] = m_v0[Z] = 0x7FFFFFFF; |
501 m_v1[X] = m_v1[Y] = m_v1[Z] = 0xFFFFFFFF; |
572 m_v1[X] = m_v1[Y] = m_v1[Z] = 0xFFFFFFFF; |
502 |
573 |
503 m_empty = true; |
574 m_empty = true; |
504 } |
575 } |
505 |
576 |
506 // ============================================================================= |
577 // ============================================================================= |
507 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
578 // ----------------------------------------------------------------------------- |
508 // ============================================================================= |
|
509 double LDBoundingBox::size() const { |
579 double LDBoundingBox::size() const { |
510 double xscale = (m_v0[X] - m_v1[X]); |
580 double xscale = (m_v0[X] - m_v1[X]); |
511 double yscale = (m_v0[Y] - m_v1[Y]); |
581 double yscale = (m_v0[Y] - m_v1[Y]); |
512 double zscale = (m_v0[Z] - m_v1[Z]); |
582 double zscale = (m_v0[Z] - m_v1[Z]); |
513 double size = zscale; |
583 double size = zscale; |