src/ldtypes.cpp

changeset 460
b230ae09c8e5
parent 459
51cca3ce540d
child 464
3779a14d597c
equal deleted inserted replaced
459:51cca3ce540d 460:b230ae09c8e5
85 vertex v = getVertex (i); 85 vertex v = getVertex (i);
86 v[ax] = value; 86 v[ax] = value;
87 setVertex (i, v); 87 setVertex (i, v);
88 } 88 }
89 89
90 LDErrorObject::LDErrorObject() {} 90 LDError::LDError() {}
91 91
92 // ============================================================================= 92 // =============================================================================
93 // ----------------------------------------------------------------------------- 93 // -----------------------------------------------------------------------------
94 str LDCommentObject::raw() { 94 str LDComment::raw() {
95 return fmt ("0 %1", text); 95 return fmt ("0 %1", text);
96 } 96 }
97 97
98 // ============================================================================= 98 // =============================================================================
99 // ----------------------------------------------------------------------------- 99 // -----------------------------------------------------------------------------
100 str LDSubfileObject::raw() { 100 str LDSubfile::raw() {
101 str val = fmt ("1 %1 %2 ", color(), position()); 101 str val = fmt ("1 %1 %2 ", color(), position());
102 val += transform().stringRep(); 102 val += transform().stringRep();
103 val += ' '; 103 val += ' ';
104 val += fileInfo()->name(); 104 val += fileInfo()->name();
105 return val; 105 return val;
106 } 106 }
107 107
108 // ============================================================================= 108 // =============================================================================
109 // ----------------------------------------------------------------------------- 109 // -----------------------------------------------------------------------------
110 str LDLineObject::raw() { 110 str LDLine::raw() {
111 str val = fmt ("2 %1", color()); 111 str val = fmt ("2 %1", color());
112 112
113 for (ushort i = 0; i < 2; ++i) 113 for (ushort i = 0; i < 2; ++i)
114 val += fmt (" %1", getVertex (i)); 114 val += fmt (" %1", getVertex (i));
115 115
116 return val; 116 return val;
117 } 117 }
118 118
119 // ============================================================================= 119 // =============================================================================
120 // ----------------------------------------------------------------------------- 120 // -----------------------------------------------------------------------------
121 str LDTriangleObject::raw() { 121 str LDTriangle::raw() {
122 str val = fmt ("3 %1", color()); 122 str val = fmt ("3 %1", color());
123 123
124 for (ushort i = 0; i < 3; ++i) 124 for (ushort i = 0; i < 3; ++i)
125 val += fmt (" %1", getVertex (i)); 125 val += fmt (" %1", getVertex (i));
126 126
127 return val; 127 return val;
128 } 128 }
129 129
130 // ============================================================================= 130 // =============================================================================
131 // ----------------------------------------------------------------------------- 131 // -----------------------------------------------------------------------------
132 str LDQuadObject::raw() { 132 str LDQuad::raw() {
133 str val = fmt ("4 %1", color()); 133 str val = fmt ("4 %1", color());
134 134
135 for (ushort i = 0; i < 4; ++i) 135 for (ushort i = 0; i < 4; ++i)
136 val += fmt (" %1", getVertex (i)); 136 val += fmt (" %1", getVertex (i));
137 137
138 return val; 138 return val;
139 } 139 }
140 140
141 // ============================================================================= 141 // =============================================================================
142 // ----------------------------------------------------------------------------- 142 // -----------------------------------------------------------------------------
143 str LDCondLineObject::raw() { 143 str LDCndLine::raw() {
144 str val = fmt ("5 %1", color()); 144 str val = fmt ("5 %1", color());
145 145
146 // Add the coordinates 146 // Add the coordinates
147 for (ushort i = 0; i < 4; ++i) 147 for (ushort i = 0; i < 4; ++i)
148 val += fmt (" %1", getVertex (i)); 148 val += fmt (" %1", getVertex (i));
150 return val; 150 return val;
151 } 151 }
152 152
153 // ============================================================================= 153 // =============================================================================
154 // ----------------------------------------------------------------------------- 154 // -----------------------------------------------------------------------------
155 str LDErrorObject::raw() { 155 str LDError::raw() {
156 return contents; 156 return contents;
157 } 157 }
158 158
159 // ============================================================================= 159 // =============================================================================
160 // ----------------------------------------------------------------------------- 160 // -----------------------------------------------------------------------------
161 str LDVertexObject::raw() { 161 str LDVertex::raw() {
162 return fmt ("0 !LDFORGE VERTEX %1 %2", color(), pos); 162 return fmt ("0 !LDFORGE VERTEX %1 %2", color(), pos);
163 } 163 }
164 164
165 // ============================================================================= 165 // =============================================================================
166 // ----------------------------------------------------------------------------- 166 // -----------------------------------------------------------------------------
167 str LDEmptyObject::raw() { 167 str LDEmpty::raw() {
168 return ""; 168 return "";
169 } 169 }
170 170
171 // ============================================================================= 171 // =============================================================================
172 // ----------------------------------------------------------------------------- 172 // -----------------------------------------------------------------------------
173 const char* LDBFCObject::statements[] = { 173 const char* LDBFC::statements[] = {
174 "CERTIFY CCW", 174 "CERTIFY CCW",
175 "CCW", 175 "CCW",
176 "CERTIFY CW", 176 "CERTIFY CW",
177 "CW", 177 "CW",
178 "NOCERTIFY", 178 "NOCERTIFY",
181 "CLIP CCW", 181 "CLIP CCW",
182 "CLIP CW", 182 "CLIP CW",
183 "NOCLIP", 183 "NOCLIP",
184 }; 184 };
185 185
186 str LDBFCObject::raw() { 186 str LDBFC::raw() {
187 return fmt ("0 BFC %1", LDBFCObject::statements[type]); 187 return fmt ("0 BFC %1", LDBFC::statements[type]);
188 } 188 }
189 189
190 // ============================================================================= 190 // =============================================================================
191 // ----------------------------------------------------------------------------- 191 // -----------------------------------------------------------------------------
192 List<LDTriangleObject*> LDQuadObject::splitToTriangles() { 192 List<LDTriangle*> LDQuad::splitToTriangles() {
193 // Create the two triangles based on this quadrilateral: 193 // Create the two triangles based on this quadrilateral:
194 // 0---3 0---3 3 194 // 0---3 0---3 3
195 // | | | / /| 195 // | | | / /|
196 // | | ==> | / / | 196 // | | ==> | / / |
197 // | | |/ / | 197 // | | |/ / |
198 // 1---2 1 1---2 198 // 1---2 1 1---2
199 LDTriangleObject* tri1 = new LDTriangleObject (getVertex (0), getVertex (1), getVertex (3)); 199 LDTriangle* tri1 = new LDTriangle (getVertex (0), getVertex (1), getVertex (3));
200 LDTriangleObject* tri2 = new LDTriangleObject (getVertex (1), getVertex (2), getVertex (3)); 200 LDTriangle* tri2 = new LDTriangle (getVertex (1), getVertex (2), getVertex (3));
201 201
202 // The triangles also inherit the quad's color 202 // The triangles also inherit the quad's color
203 tri1->setColor (color()); 203 tri1->setColor (color());
204 tri2->setColor (color()); 204 tri2->setColor (color());
205 205
206 List<LDTriangleObject*> triangles; 206 List<LDTriangle*> triangles;
207 triangles << tri1; 207 triangles << tri1;
208 triangles << tri2; 208 triangles << tri2;
209 return triangles; 209 return triangles;
210 } 210 }
211 211
235 file()->addToHistory (new SwapHistory (id(), other->id())); 235 file()->addToHistory (new SwapHistory (id(), other->id()));
236 } 236 }
237 237
238 // ============================================================================= 238 // =============================================================================
239 // ----------------------------------------------------------------------------- 239 // -----------------------------------------------------------------------------
240 LDLineObject::LDLineObject (vertex v1, vertex v2) { 240 LDLine::LDLine (vertex v1, vertex v2) {
241 setVertex (0, v1); 241 setVertex (0, v1);
242 setVertex (1, v2); 242 setVertex (1, v2);
243 } 243 }
244 244
245 // ============================================================================= 245 // =============================================================================
263 // ============================================================================= 263 // =============================================================================
264 // ----------------------------------------------------------------------------- 264 // -----------------------------------------------------------------------------
265 static void transformObject (LDObject* obj, matrix transform, vertex pos, short parentcolor) { 265 static void transformObject (LDObject* obj, matrix transform, vertex pos, short parentcolor) {
266 switch (obj->getType()) { 266 switch (obj->getType()) {
267 case LDObject::Line: 267 case LDObject::Line:
268 case LDObject::CondLine: 268 case LDObject::CndLine:
269 case LDObject::Triangle: 269 case LDObject::Triangle:
270 case LDObject::Quad: 270 case LDObject::Quad:
271 for (short i = 0; i < obj->vertices(); ++i) { 271 for (short i = 0; i < obj->vertices(); ++i) {
272 vertex v = obj->getVertex (i); 272 vertex v = obj->getVertex (i);
273 v.transform (transform, pos); 273 v.transform (transform, pos);
276 276
277 break; 277 break;
278 278
279 case LDObject::Subfile: 279 case LDObject::Subfile:
280 { 280 {
281 LDSubfileObject* ref = static_cast<LDSubfileObject*> (obj); 281 LDSubfile* ref = static_cast<LDSubfile*> (obj);
282 matrix newMatrix = transform * ref->transform(); 282 matrix newMatrix = transform * ref->transform();
283 vertex newpos = ref->position(); 283 vertex newpos = ref->position();
284 284
285 newpos.transform (transform, pos); 285 newpos.transform (transform, pos);
286 ref->setPosition (newpos); 286 ref->setPosition (newpos);
296 obj->setColor (parentcolor); 296 obj->setColor (parentcolor);
297 } 297 }
298 298
299 // ============================================================================= 299 // =============================================================================
300 // ----------------------------------------------------------------------------- 300 // -----------------------------------------------------------------------------
301 List<LDObject*> LDSubfileObject::inlineContents (InlineFlags flags) { 301 List<LDObject*> LDSubfile::inlineContents (InlineFlags flags) {
302 List<LDObject*> objs = fileInfo()->inlineContents (flags); 302 List<LDObject*> objs = fileInfo()->inlineContents (flags);
303 303
304 // Transform the objects 304 // Transform the objects
305 for (LDObject* obj : objs) { 305 for (LDObject* obj : objs) {
306 // Set the parent now so we know what inlined this. 306 // Set the parent now so we know what inlined this.
450 } 450 }
451 451
452 // ============================================================================= 452 // =============================================================================
453 // ----------------------------------------------------------------------------- 453 // -----------------------------------------------------------------------------
454 void LDObject::move (vertex vect) { (void) vect; } 454 void LDObject::move (vertex vect) { (void) vect; }
455 void LDEmptyObject::move (vertex vect) { (void) vect; } 455 void LDEmpty::move (vertex vect) { (void) vect; }
456 void LDBFCObject::move (vertex vect) { (void) vect; } 456 void LDBFC::move (vertex vect) { (void) vect; }
457 void LDCommentObject::move (vertex vect) { (void) vect; } 457 void LDComment::move (vertex vect) { (void) vect; }
458 void LDErrorObject::move (vertex vect) { (void) vect; } 458 void LDError::move (vertex vect) { (void) vect; }
459 459
460 // ============================================================================= 460 // =============================================================================
461 // ----------------------------------------------------------------------------- 461 // -----------------------------------------------------------------------------
462 void LDVertexObject::move (vertex vect) { 462 void LDVertex::move (vertex vect) {
463 pos += vect; 463 pos += vect;
464 } 464 }
465 465
466 // ============================================================================= 466 // =============================================================================
467 // ----------------------------------------------------------------------------- 467 // -----------------------------------------------------------------------------
468 void LDSubfileObject::move (vertex vect) { 468 void LDSubfile::move (vertex vect) {
469 setPosition (position() + vect); 469 setPosition (position() + vect);
470 } 470 }
471 471
472 // ============================================================================= 472 // =============================================================================
473 // ----------------------------------------------------------------------------- 473 // -----------------------------------------------------------------------------
474 void LDLineObject::move (vertex vect) { 474 void LDLine::move (vertex vect) {
475 for (short i = 0; i < 2; ++i) 475 for (short i = 0; i < 2; ++i)
476 setVertex (i, getVertex (i) + vect); 476 setVertex (i, getVertex (i) + vect);
477 } 477 }
478 478
479 // ============================================================================= 479 // =============================================================================
480 // ----------------------------------------------------------------------------- 480 // -----------------------------------------------------------------------------
481 void LDTriangleObject::move (vertex vect) { 481 void LDTriangle::move (vertex vect) {
482 for (short i = 0; i < 3; ++i) 482 for (short i = 0; i < 3; ++i)
483 setVertex (i, getVertex (i) + vect); 483 setVertex (i, getVertex (i) + vect);
484 } 484 }
485 485
486 // ============================================================================= 486 // =============================================================================
487 // ----------------------------------------------------------------------------- 487 // -----------------------------------------------------------------------------
488 void LDQuadObject::move (vertex vect) { 488 void LDQuad::move (vertex vect) {
489 for (short i = 0; i < 4; ++i) 489 for (short i = 0; i < 4; ++i)
490 setVertex (i, getVertex (i) + vect); 490 setVertex (i, getVertex (i) + vect);
491 } 491 }
492 492
493 // ============================================================================= 493 // =============================================================================
494 // ----------------------------------------------------------------------------- 494 // -----------------------------------------------------------------------------
495 void LDCondLineObject::move (vertex vect) { 495 void LDCndLine::move (vertex vect) {
496 for (short i = 0; i < 4; ++i) 496 for (short i = 0; i < 4; ++i)
497 setVertex (i, getVertex (i) + vect); 497 setVertex (i, getVertex (i) + vect);
498 } 498 }
499 499
500 // ============================================================================= 500 // =============================================================================
501 // ----------------------------------------------------------------------------- 501 // -----------------------------------------------------------------------------
502 #define CHECK_FOR_OBJ(N) \ 502 #define CHECK_FOR_OBJ(N) \
503 if (type == LDObject::N) \ 503 if (type == LDObject::N) \
504 return new LD##N##Object; 504 return new LD##N;
505 505
506 LDObject* LDObject::getDefault (const LDObject::Type type) { 506 LDObject* LDObject::getDefault (const LDObject::Type type) {
507 CHECK_FOR_OBJ (Comment) 507 CHECK_FOR_OBJ (Comment)
508 CHECK_FOR_OBJ (BFC) 508 CHECK_FOR_OBJ (BFC)
509 CHECK_FOR_OBJ (Line) 509 CHECK_FOR_OBJ (Line)
510 CHECK_FOR_OBJ (CondLine) 510 CHECK_FOR_OBJ (CndLine)
511 CHECK_FOR_OBJ (Subfile) 511 CHECK_FOR_OBJ (Subfile)
512 CHECK_FOR_OBJ (Triangle) 512 CHECK_FOR_OBJ (Triangle)
513 CHECK_FOR_OBJ (Quad) 513 CHECK_FOR_OBJ (Quad)
514 CHECK_FOR_OBJ (Empty) 514 CHECK_FOR_OBJ (Empty)
515 CHECK_FOR_OBJ (BFC) 515 CHECK_FOR_OBJ (BFC)
520 } 520 }
521 521
522 // ============================================================================= 522 // =============================================================================
523 // ----------------------------------------------------------------------------- 523 // -----------------------------------------------------------------------------
524 void LDObject::invert() {} 524 void LDObject::invert() {}
525 void LDBFCObject::invert() {} 525 void LDBFC::invert() {}
526 void LDEmptyObject::invert() {} 526 void LDEmpty::invert() {}
527 void LDCommentObject::invert() {} 527 void LDComment::invert() {}
528 void LDErrorObject::invert() {} 528 void LDError::invert() {}
529 529
530 // ============================================================================= 530 // =============================================================================
531 // ----------------------------------------------------------------------------- 531 // -----------------------------------------------------------------------------
532 void LDTriangleObject::invert() { 532 void LDTriangle::invert() {
533 // Triangle goes 0 -> 1 -> 2, reversed: 0 -> 2 -> 1. 533 // Triangle goes 0 -> 1 -> 2, reversed: 0 -> 2 -> 1.
534 // Thus, we swap 1 and 2. 534 // Thus, we swap 1 and 2.
535 vertex tmp = getVertex (1); 535 vertex tmp = getVertex (1);
536 setVertex (1, getVertex (2)); 536 setVertex (1, getVertex (2));
537 setVertex (2, tmp); 537 setVertex (2, tmp);
539 return; 539 return;
540 } 540 }
541 541
542 // ============================================================================= 542 // =============================================================================
543 // ----------------------------------------------------------------------------- 543 // -----------------------------------------------------------------------------
544 void LDQuadObject::invert() { 544 void LDQuad::invert() {
545 // Quad: 0 -> 1 -> 2 -> 3 545 // Quad: 0 -> 1 -> 2 -> 3
546 // rev: 0 -> 3 -> 2 -> 1 546 // rev: 0 -> 3 -> 2 -> 1
547 // Thus, we swap 1 and 3. 547 // Thus, we swap 1 and 3.
548 vertex tmp = getVertex (1); 548 vertex tmp = getVertex (1);
549 setVertex (1, getVertex (3)); 549 setVertex (1, getVertex (3));
550 setVertex (3, tmp); 550 setVertex (3, tmp);
551 } 551 }
552 552
553 // ============================================================================= 553 // =============================================================================
554 // ----------------------------------------------------------------------------- 554 // -----------------------------------------------------------------------------
555 void LDSubfileObject::invert() { 555 void LDSubfile::invert() {
556 // Subfiles are inverted when they're prefixed with 556 // Subfiles are inverted when they're prefixed with
557 // a BFC INVERTNEXT statement. Thus we need to toggle this status. 557 // a BFC INVERTNEXT statement. Thus we need to toggle this status.
558 // For flat primitives it's sufficient that the determinant is 558 // For flat primitives it's sufficient that the determinant is
559 // flipped but I don't have a method for checking flatness yet. 559 // flipped but I don't have a method for checking flatness yet.
560 // Food for thought... 560 // Food for thought...
561 561
562 ulong idx = getIndex(); 562 ulong idx = getIndex();
563 563
564 if (idx > 0) { 564 if (idx > 0) {
565 LDBFCObject* bfc = dynamic_cast<LDBFCObject*> (prev()); 565 LDBFC* bfc = dynamic_cast<LDBFC*> (prev());
566 566
567 if (bfc && bfc->type == LDBFCObject::InvertNext) { 567 if (bfc && bfc->type == LDBFC::InvertNext) {
568 // This is prefixed with an invertnext, thus remove it. 568 // This is prefixed with an invertnext, thus remove it.
569 file()->forgetObject (bfc); 569 file()->forgetObject (bfc);
570 delete bfc; 570 delete bfc;
571 return; 571 return;
572 } 572 }
573 } 573 }
574 574
575 // Not inverted, thus prefix it with a new invertnext. 575 // Not inverted, thus prefix it with a new invertnext.
576 LDBFCObject* bfc = new LDBFCObject (LDBFCObject::InvertNext); 576 LDBFC* bfc = new LDBFC (LDBFC::InvertNext);
577 file()->insertObj (idx, bfc); 577 file()->insertObj (idx, bfc);
578 } 578 }
579 579
580 // ============================================================================= 580 // =============================================================================
581 // ----------------------------------------------------------------------------- 581 // -----------------------------------------------------------------------------
585 vertex tmp = line->getVertex (0); 585 vertex tmp = line->getVertex (0);
586 line->setVertex (0, line->getVertex (1)); 586 line->setVertex (0, line->getVertex (1));
587 line->setVertex (1, tmp); 587 line->setVertex (1, tmp);
588 } 588 }
589 589
590 void LDLineObject::invert() { 590 void LDLine::invert() {
591 invertLine (this); 591 invertLine (this);
592 } 592 }
593 593
594 void LDCondLineObject::invert() { 594 void LDCndLine::invert() {
595 invertLine (this); 595 invertLine (this);
596 } 596 }
597 597
598 void LDVertexObject::invert() {} 598 void LDVertex::invert() {}
599 599
600 // ============================================================================= 600 // =============================================================================
601 // ----------------------------------------------------------------------------- 601 // -----------------------------------------------------------------------------
602 LDLineObject* LDCondLineObject::demote() { 602 LDLine* LDCndLine::demote() {
603 LDLineObject* repl = new LDLineObject; 603 LDLine* repl = new LDLine;
604 604
605 for (int i = 0; i < repl->vertices(); ++i) 605 for (int i = 0; i < repl->vertices(); ++i)
606 repl->setVertex (i, getVertex (i)); 606 repl->setVertex (i, getVertex (i));
607 607
608 repl->setColor (color()); 608 repl->setColor (color());
621 return null; 621 return null;
622 } 622 }
623 623
624 // ============================================================================= 624 // =============================================================================
625 // ----------------------------------------------------------------------------- 625 // -----------------------------------------------------------------------------
626 str LDOverlayObject::raw() { 626 str LDOverlay::raw() {
627 return fmt ("0 !LDFORGE OVERLAY %1 %2 %3 %4 %5 %6", 627 return fmt ("0 !LDFORGE OVERLAY %1 %2 %3 %4 %5 %6",
628 filename(), camera(), x(), y(), width(), height()); 628 filename(), camera(), x(), y(), width(), height());
629 } 629 }
630 630
631 void LDOverlayObject::move (vertex vect) { 631 void LDOverlay::move (vertex vect) {
632 Q_UNUSED (vect) 632 Q_UNUSED (vect)
633 } 633 }
634 634
635 void LDOverlayObject::invert() {} 635 void LDOverlay::invert() {}
636 636
637 // ============================================================================= 637 // =============================================================================
638 // Hook the set accessors of certain properties to this changeProperty function. 638 // Hook the set accessors of certain properties to this changeProperty function.
639 // It takes care of history management so we can capture low-level changes, this 639 // It takes care of history management so we can capture low-level changes, this
640 // makes history stuff work out of the box. 640 // makes history stuff work out of the box.

mercurial