src/ldtypes.cpp

changeset 377
271d1da66b7e
parent 376
4a89dd47535f
child 379
f5f3faac60cd
equal deleted inserted replaced
376:4a89dd47535f 377:271d1da66b7e
81 vertex v = getVertex (i); 81 vertex v = getVertex (i);
82 v[ax] = value; 82 v[ax] = value;
83 setVertex (i, v); 83 setVertex (i, v);
84 } 84 }
85 85
86 LDGibberish::LDGibberish() {} 86 LDErrorObject::LDErrorObject() {}
87 LDGibberish::LDGibberish (str contents, str reason) : contents (contents), reason (reason) {} 87
88 88 // =============================================================================
89 // ============================================================================= 89 str LDCommentObject::raw() {
90 str LDComment::raw() {
91 return fmt ("0 %1", text); 90 return fmt ("0 %1", text);
92 } 91 }
93 92
94 str LDSubfile::raw() { 93 str LDSubfileObject::raw() {
95 str val = fmt ("1 %1 %2 ", color(), position()); 94 str val = fmt ("1 %1 %2 ", color(), position());
96 val += transform().stringRep(); 95 val += transform().stringRep();
97 val += ' '; 96 val += ' ';
98 val += fileInfo()->name(); 97 val += fileInfo()->name();
99 return val; 98 return val;
100 } 99 }
101 100
102 str LDLine::raw() { 101 str LDLineObject::raw() {
103 str val = fmt ("2 %1", color()); 102 str val = fmt ("2 %1", color());
104 103
105 for (ushort i = 0; i < 2; ++i) 104 for (ushort i = 0; i < 2; ++i)
106 val += fmt (" %1", getVertex (i)); 105 val += fmt (" %1", getVertex (i));
107 106
108 return val; 107 return val;
109 } 108 }
110 109
111 str LDTriangle::raw() { 110 str LDTriangleObject::raw() {
112 str val = fmt ("3 %1", color()); 111 str val = fmt ("3 %1", color());
113 112
114 for (ushort i = 0; i < 3; ++i) 113 for (ushort i = 0; i < 3; ++i)
115 val += fmt (" %1", getVertex (i)); 114 val += fmt (" %1", getVertex (i));
116 115
117 return val; 116 return val;
118 } 117 }
119 118
120 str LDQuad::raw() { 119 str LDQuadObject::raw() {
121 str val = fmt ("4 %1", color()); 120 str val = fmt ("4 %1", color());
122 121
123 for (ushort i = 0; i < 4; ++i) 122 for (ushort i = 0; i < 4; ++i)
124 val += fmt (" %1", getVertex (i)); 123 val += fmt (" %1", getVertex (i));
125 124
126 return val; 125 return val;
127 } 126 }
128 127
129 str LDCondLine::raw() { 128 str LDCondLineObject::raw() {
130 str val = fmt ("5 %1", color()); 129 str val = fmt ("5 %1", color());
131 130
132 // Add the coordinates 131 // Add the coordinates
133 for (ushort i = 0; i < 4; ++i) 132 for (ushort i = 0; i < 4; ++i)
134 val += fmt (" %1", getVertex (i)); 133 val += fmt (" %1", getVertex (i));
135 134
136 return val; 135 return val;
137 } 136 }
138 137
139 str LDGibberish::raw() { 138 str LDErrorObject::raw() {
140 return contents; 139 return contents;
141 } 140 }
142 141
143 str LDVertex::raw() { 142 str LDVertexObject::raw() {
144 return fmt ("0 !LDFORGE VERTEX %1 %2", color(), pos); 143 return fmt ("0 !LDFORGE VERTEX %1 %2", color(), pos);
145 } 144 }
146 145
147 str LDEmpty::raw() { 146 str LDEmptyObject::raw() {
148 return ""; 147 return "";
149 } 148 }
150 149
151 // ============================================================================= 150 // =============================================================================
152 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 151 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
153 // ============================================================================= 152 // =============================================================================
154 const char* LDBFC::statements[] = { 153 const char* LDBFCObject::statements[] = {
155 "CERTIFY CCW", 154 "CERTIFY CCW",
156 "CCW", 155 "CCW",
157 "CERTIFY CW", 156 "CERTIFY CW",
158 "CW", 157 "CW",
159 "NOCERTIFY", 158 "NOCERTIFY",
160 "INVERTNEXT", 159 "INVERTNEXT",
161 }; 160 };
162 161
163 str LDBFC::raw() { 162 str LDBFCObject::raw() {
164 return fmt ("0 BFC %1", LDBFC::statements[type]); 163 return fmt ("0 BFC %1", LDBFCObject::statements[type]);
165 } 164 }
166 165
167 // ============================================================================= 166 // =============================================================================
168 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 167 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
169 // ============================================================================= 168 // =============================================================================
170 vector<LDTriangle*> LDQuad::splitToTriangles() { 169 vector<LDTriangleObject*> LDQuadObject::splitToTriangles() {
171 // Create the two triangles based on this quadrilateral: 170 // Create the two triangles based on this quadrilateral:
172 // 0---3 0---3 3 171 // 0---3 0---3 3
173 // | | | / /| 172 // | | | / /|
174 // | | ==> | / / | 173 // | | ==> | / / |
175 // | | |/ / | 174 // | | |/ / |
176 // 1---2 1 1---2 175 // 1---2 1 1---2
177 LDTriangle* tri1 = new LDTriangle (getVertex (0), getVertex (1), getVertex (3)); 176 LDTriangleObject* tri1 = new LDTriangleObject (getVertex (0), getVertex (1), getVertex (3));
178 LDTriangle* tri2 = new LDTriangle (getVertex (1), getVertex (2), getVertex (3)); 177 LDTriangleObject* tri2 = new LDTriangleObject (getVertex (1), getVertex (2), getVertex (3));
179 178
180 // The triangles also inherit the quad's color 179 // The triangles also inherit the quad's color
181 tri1->setColor (color()); 180 tri1->setColor (color());
182 tri2->setColor (color()); 181 tri2->setColor (color());
183 182
184 vector<LDTriangle*> triangles; 183 vector<LDTriangleObject*> triangles;
185 triangles << tri1; 184 triangles << tri1;
186 triangles << tri2; 185 triangles << tri2;
187 return triangles; 186 return triangles;
188 } 187 }
189 188
213 } 212 }
214 213
215 g_curfile->addToHistory (new SwapHistory (id(), other->id())); 214 g_curfile->addToHistory (new SwapHistory (id(), other->id()));
216 } 215 }
217 216
218 LDLine::LDLine (vertex v1, vertex v2) { 217 LDLineObject::LDLineObject (vertex v1, vertex v2) {
219 setVertex (0, v1); 218 setVertex (0, v1);
220 setVertex (1, v2); 219 setVertex (1, v2);
221 } 220 }
222 221
223 LDObject::~LDObject() { 222 LDObject::~LDObject() {
253 252
254 break; 253 break;
255 254
256 case LDObject::Subfile: 255 case LDObject::Subfile:
257 { 256 {
258 LDSubfile* ref = static_cast<LDSubfile*> (obj); 257 LDSubfileObject* ref = static_cast<LDSubfileObject*> (obj);
259 matrix newMatrix = transform * ref->transform(); 258 matrix newMatrix = transform * ref->transform();
260 vertex newpos = ref->position(); 259 vertex newpos = ref->position();
261 260
262 newpos.transform (transform, pos); 261 newpos.transform (transform, pos);
263 ref->setPosition (newpos); 262 ref->setPosition (newpos);
274 } 273 }
275 274
276 // ============================================================================= 275 // =============================================================================
277 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 276 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
278 // ============================================================================= 277 // =============================================================================
279 vector<LDObject*> LDSubfile::inlineContents (bool deep, bool cache) { 278 vector<LDObject*> LDSubfileObject::inlineContents (bool deep, bool cache) {
280 vector<LDObject*> objs, objcache; 279 vector<LDObject*> objs, objcache;
281 280
282 // If we have this cached, just clone that 281 // If we have this cached, just clone that
283 if (deep && fileInfo()->cache().size()) { 282 if (deep && fileInfo()->cache().size()) {
284 for (LDObject* obj : fileInfo()->cache()) 283 for (LDObject* obj : fileInfo()->cache())
294 293
295 // Got another sub-file reference, inline it if we're deep-inlining. If not, 294 // Got another sub-file reference, inline it if we're deep-inlining. If not,
296 // just add it into the objects normally. Also, we only cache immediate 295 // just add it into the objects normally. Also, we only cache immediate
297 // subfiles and this is not one. Yay, recursion! 296 // subfiles and this is not one. Yay, recursion!
298 if (deep && obj->getType() == LDObject::Subfile) { 297 if (deep && obj->getType() == LDObject::Subfile) {
299 LDSubfile* ref = static_cast<LDSubfile*> (obj); 298 LDSubfileObject* ref = static_cast<LDSubfileObject*> (obj);
300 299
301 vector<LDObject*> otherobjs = ref->inlineContents (true, false); 300 vector<LDObject*> otherobjs = ref->inlineContents (true, false);
302 301
303 for (LDObject* otherobj : otherobjs) { 302 for (LDObject* otherobj : otherobjs) {
304 // Cache this object, if desired 303 // Cache this object, if desired
459 458
460 // ============================================================================= 459 // =============================================================================
461 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 460 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
462 // ============================================================================= 461 // =============================================================================
463 void LDObject::move (vertex vect) { (void) vect; } 462 void LDObject::move (vertex vect) { (void) vect; }
464 void LDEmpty::move (vertex vect) { (void) vect; } 463 void LDEmptyObject::move (vertex vect) { (void) vect; }
465 void LDBFC::move (vertex vect) { (void) vect; } 464 void LDBFCObject::move (vertex vect) { (void) vect; }
466 void LDComment::move (vertex vect) { (void) vect; } 465 void LDCommentObject::move (vertex vect) { (void) vect; }
467 void LDGibberish::move (vertex vect) { (void) vect; } 466 void LDErrorObject::move (vertex vect) { (void) vect; }
468 467
469 void LDVertex::move (vertex vect) { 468 void LDVertexObject::move (vertex vect) {
470 pos += vect; 469 pos += vect;
471 } 470 }
472 471
473 void LDSubfile::move (vertex vect) { 472 void LDSubfileObject::move (vertex vect) {
474 setPosition (position() + vect); 473 setPosition (position() + vect);
475 } 474 }
476 475
477 void LDLine::move (vertex vect) { 476 void LDLineObject::move (vertex vect) {
478 for (short i = 0; i < 2; ++i) 477 for (short i = 0; i < 2; ++i)
479 setVertex (i, getVertex (i) + vect); 478 setVertex (i, getVertex (i) + vect);
480 } 479 }
481 480
482 void LDTriangle::move (vertex vect) { 481 void LDTriangleObject::move (vertex vect) {
483 for (short i = 0; i < 3; ++i) 482 for (short i = 0; i < 3; ++i)
484 setVertex (i, getVertex (i) + vect); 483 setVertex (i, getVertex (i) + vect);
485 } 484 }
486 485
487 void LDQuad::move (vertex vect) { 486 void LDQuadObject::move (vertex vect) {
488 for (short i = 0; i < 4; ++i) 487 for (short i = 0; i < 4; ++i)
489 setVertex (i, getVertex (i) + vect); 488 setVertex (i, getVertex (i) + vect);
490 } 489 }
491 490
492 void LDCondLine::move (vertex vect) { 491 void LDCondLineObject::move (vertex vect) {
493 for (short i = 0; i < 4; ++i) 492 for (short i = 0; i < 4; ++i)
494 setVertex (i, getVertex (i) + vect); 493 setVertex (i, getVertex (i) + vect);
495 } 494 }
496 495
497 // ============================================================================= 496 // =============================================================================
498 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 497 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
499 // ============================================================================= 498 // =============================================================================
500 #define CHECK_FOR_OBJ(N) \ 499 #define CHECK_FOR_OBJ(N) \
501 if( type == LDObject::N ) \ 500 if( type == LDObject::N ) \
502 return new LD##N; 501 return new LD##N##Object;
503 502
504 LDObject* LDObject::getDefault (const LDObject::Type type) { 503 LDObject* LDObject::getDefault (const LDObject::Type type) {
505 CHECK_FOR_OBJ (Comment) 504 CHECK_FOR_OBJ (Comment)
506 CHECK_FOR_OBJ (BFC) 505 CHECK_FOR_OBJ (BFC)
507 CHECK_FOR_OBJ (Line) 506 CHECK_FOR_OBJ (Line)
509 CHECK_FOR_OBJ (Subfile) 508 CHECK_FOR_OBJ (Subfile)
510 CHECK_FOR_OBJ (Triangle) 509 CHECK_FOR_OBJ (Triangle)
511 CHECK_FOR_OBJ (Quad) 510 CHECK_FOR_OBJ (Quad)
512 CHECK_FOR_OBJ (Empty) 511 CHECK_FOR_OBJ (Empty)
513 CHECK_FOR_OBJ (BFC) 512 CHECK_FOR_OBJ (BFC)
514 CHECK_FOR_OBJ (Gibberish) 513 CHECK_FOR_OBJ (Error)
515 CHECK_FOR_OBJ (Vertex) 514 CHECK_FOR_OBJ (Vertex)
516 CHECK_FOR_OBJ (Overlay) 515 CHECK_FOR_OBJ (Overlay)
517 return null; 516 return null;
518 } 517 }
519 518
520 // ============================================================================= 519 // =============================================================================
521 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 520 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
522 // ============================================================================= 521 // =============================================================================
523 void LDObject::invert() {} 522 void LDObject::invert() {}
524 void LDBFC::invert() {} 523 void LDBFCObject::invert() {}
525 void LDEmpty::invert() {} 524 void LDEmptyObject::invert() {}
526 void LDComment::invert() {} 525 void LDCommentObject::invert() {}
527 void LDGibberish::invert() {} 526 void LDErrorObject::invert() {}
528 527
529 void LDTriangle::invert() { 528 void LDTriangleObject::invert() {
530 // Triangle goes 0 -> 1 -> 2, reversed: 0 -> 2 -> 1. 529 // Triangle goes 0 -> 1 -> 2, reversed: 0 -> 2 -> 1.
531 // Thus, we swap 1 and 2. 530 // Thus, we swap 1 and 2.
532 vertex tmp = getVertex (1); 531 vertex tmp = getVertex (1);
533 setVertex (1, getVertex (2)); 532 setVertex (1, getVertex (2));
534 setVertex (2, tmp); 533 setVertex (2, tmp);
535 534
536 return; 535 return;
537 } 536 }
538 537
539 void LDQuad::invert() { 538 void LDQuadObject::invert() {
540 // Quad: 0 -> 1 -> 2 -> 3 539 // Quad: 0 -> 1 -> 2 -> 3
541 // rev: 0 -> 3 -> 2 -> 1 540 // rev: 0 -> 3 -> 2 -> 1
542 // Thus, we swap 1 and 3. 541 // Thus, we swap 1 and 3.
543 vertex tmp = getVertex (1); 542 vertex tmp = getVertex (1);
544 setVertex (1, getVertex (3)); 543 setVertex (1, getVertex (3));
545 setVertex (3, tmp); 544 setVertex (3, tmp);
546 } 545 }
547 546
548 void LDSubfile::invert() { 547 void LDSubfileObject::invert() {
549 // Subfiles are inverted when they're prefixed with 548 // Subfiles are inverted when they're prefixed with
550 // a BFC INVERTNEXT statement. Thus we need to toggle this status. 549 // a BFC INVERTNEXT statement. Thus we need to toggle this status.
551 // For flat primitives it's sufficient that the determinant is 550 // For flat primitives it's sufficient that the determinant is
552 // flipped but I don't have a method for checking flatness yet. 551 // flipped but I don't have a method for checking flatness yet.
553 // Food for thought... 552 // Food for thought...
554 553
555 ulong idx = getIndex (g_curfile); 554 ulong idx = getIndex (g_curfile);
556 555
557 if (idx > 0) { 556 if (idx > 0) {
558 LDBFC* bfc = dynamic_cast<LDBFC*> (prev()); 557 LDBFCObject* bfc = dynamic_cast<LDBFCObject*> (prev());
559 558
560 if (bfc && bfc->type == LDBFC::InvertNext) { 559 if (bfc && bfc->type == LDBFCObject::InvertNext) {
561 // This is prefixed with an invertnext, thus remove it. 560 // This is prefixed with an invertnext, thus remove it.
562 g_curfile->forgetObject (bfc); 561 g_curfile->forgetObject (bfc);
563 delete bfc; 562 delete bfc;
564 return; 563 return;
565 } 564 }
566 } 565 }
567 566
568 // Not inverted, thus prefix it with a new invertnext. 567 // Not inverted, thus prefix it with a new invertnext.
569 LDBFC* bfc = new LDBFC (LDBFC::InvertNext); 568 LDBFCObject* bfc = new LDBFCObject (LDBFCObject::InvertNext);
570 g_curfile->insertObj (idx, bfc); 569 g_curfile->insertObj (idx, bfc);
571 } 570 }
572 571
573 static void invertLine (LDObject* line) { 572 static void invertLine (LDObject* line) {
574 // For lines, we swap the vertices. I don't think that a 573 // For lines, we swap the vertices. I don't think that a
576 vertex tmp = line->getVertex (0); 575 vertex tmp = line->getVertex (0);
577 line->setVertex (0, line->getVertex (1)); 576 line->setVertex (0, line->getVertex (1));
578 line->setVertex (1, tmp); 577 line->setVertex (1, tmp);
579 } 578 }
580 579
581 void LDLine::invert() { 580 void LDLineObject::invert() {
582 invertLine (this); 581 invertLine (this);
583 } 582 }
584 583
585 void LDCondLine::invert() { 584 void LDCondLineObject::invert() {
586 invertLine (this); 585 invertLine (this);
587 } 586 }
588 587
589 void LDVertex::invert() {} 588 void LDVertexObject::invert() {}
590 589
591 // ============================================================================= 590 // =============================================================================
592 LDLine* LDCondLine::demote() { 591 LDLineObject* LDCondLineObject::demote() {
593 LDLine* repl = new LDLine; 592 LDLineObject* repl = new LDLineObject;
594 593
595 for (int i = 0; i < repl->vertices(); ++i) 594 for (int i = 0; i < repl->vertices(); ++i)
596 repl->setVertex (i, getVertex (i)); 595 repl->setVertex (i, getVertex (i));
597 596
598 repl->setColor (color()); 597 repl->setColor (color());
608 607
609 return null; 608 return null;
610 } 609 }
611 610
612 // ============================================================================= 611 // =============================================================================
613 str LDOverlay::raw() { 612 str LDOverlayObject::raw() {
614 return fmt ("0 !LDFORGE OVERLAY %1 %2 %3 %4 %5 %6", 613 return fmt ("0 !LDFORGE OVERLAY %1 %2 %3 %4 %5 %6",
615 filename(), camera(), x(), y(), width(), height()); 614 filename(), camera(), x(), y(), width(), height());
616 } 615 }
617 616
618 void LDOverlay::move (vertex vect) { 617 void LDOverlayObject::move (vertex vect) {
619 Q_UNUSED (vect) 618 Q_UNUSED (vect)
620 } 619 }
621 620
622 void LDOverlay::invert() {} 621 void LDOverlayObject::invert() {}
623 622
624 // ============================================================================= 623 // =============================================================================
625 // Hook the set accessors of certain properties to this changeProperty function. 624 // Hook the set accessors of certain properties to this changeProperty function.
626 // It takes care of history management so we can capture low-level changes, this 625 // It takes care of history management so we can capture low-level changes, this
627 // makes history stuff work out of the box. 626 // makes history stuff work out of the box.

mercurial