src/LDObject.cc

changeset 655
b376645315ab
parent 654
a74f2ff353b8
child 656
2a1c204df14d
child 706
d79083b9f74d
equal deleted inserted replaced
654:a74f2ff353b8 655:b376645315ab
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 "Main.h"
20 #include "LDObject.h"
21 #include "Document.h"
22 #include "Misc.h"
23 #include "MainWindow.h"
24 #include "EditHistory.h"
25 #include "GLRenderer.h"
26 #include "Colors.h"
27
28 cfg (String, ld_defaultname, "");
29 cfg (String, ld_defaultuser, "");
30 cfg (Int, ld_defaultlicense, 0);
31
32 // List of all LDObjects
33 static LDObjectList g_LDObjects;
34
35 // =============================================================================
36 // LDObject constructors
37 //
38 LDObject::LDObject() :
39 m_isHidden (false),
40 m_isSelected (false),
41 m_parent (null),
42 m_document (null),
43 m_isGLInit (false),
44 qObjListEntry (null)
45 {
46 memset (m_coords, 0, sizeof m_coords);
47 chooseID();
48 g_LDObjects << this;
49 }
50
51 // =============================================================================
52 //
53 void LDObject::chooseID()
54 {
55 int32 id = 1; // 0 shalt be null
56
57 for (LDObject* obj : g_LDObjects)
58 {
59 assert (obj != this);
60
61 if (obj->id() >= id)
62 id = obj->id() + 1;
63 }
64
65 setID (id);
66 }
67
68 // =============================================================================
69 //
70 void LDObject::setVertexCoord (int i, Axis ax, double value)
71 {
72 Vertex v = vertex (i);
73 v[ax] = value;
74 setVertex (i, v);
75 }
76
77 LDError::LDError() {}
78
79 // =============================================================================
80 //
81 QString LDComment::asText() const
82 {
83 return format ("0 %1", text());
84 }
85
86 // =============================================================================
87 //
88 QString LDSubfile::asText() const
89 {
90 QString val = format ("1 %1 %2 ", color(), position());
91 val += transform().toString();
92 val += ' ';
93 val += fileInfo()->name();
94 return val;
95 }
96
97 // =============================================================================
98 //
99 QString LDLine::asText() const
100 {
101 QString val = format ("2 %1", color());
102
103 for (int i = 0; i < 2; ++i)
104 val += format (" %1", vertex (i));
105
106 return val;
107 }
108
109 // =============================================================================
110 //
111 QString LDTriangle::asText() const
112 {
113 QString val = format ("3 %1", color());
114
115 for (int i = 0; i < 3; ++i)
116 val += format (" %1", vertex (i));
117
118 return val;
119 }
120
121 // =============================================================================
122 //
123 QString LDQuad::asText() const
124 {
125 QString val = format ("4 %1", color());
126
127 for (int i = 0; i < 4; ++i)
128 val += format (" %1", vertex (i));
129
130 return val;
131 }
132
133 // =============================================================================
134 //
135 QString LDCondLine::asText() const
136 {
137 QString val = format ("5 %1", color());
138
139 // Add the coordinates
140 for (int i = 0; i < 4; ++i)
141 val += format (" %1", vertex (i));
142
143 return val;
144 }
145
146 // =============================================================================
147 //
148 QString LDError::asText() const
149 {
150 return contents();
151 }
152
153 // =============================================================================
154 //
155 QString LDVertex::asText() const
156 {
157 return format ("0 !LDFORGE VERTEX %1 %2", color(), pos);
158 }
159
160 // =============================================================================
161 //
162 QString LDEmpty::asText() const
163 {
164 return "";
165 }
166
167 // =============================================================================
168 //
169 const char* LDBFC::k_statementStrings[] =
170 {
171 "CERTIFY CCW",
172 "CCW",
173 "CERTIFY CW",
174 "CW",
175 "NOCERTIFY",
176 "INVERTNEXT",
177 "CLIP",
178 "CLIP CCW",
179 "CLIP CW",
180 "NOCLIP",
181 };
182
183 QString LDBFC::asText() const
184 {
185 return format ("0 BFC %1", LDBFC::k_statementStrings[m_statement]);
186 }
187
188 // =============================================================================
189 //
190 QList<LDTriangle*> LDQuad::splitToTriangles()
191 {
192 // Create the two triangles based on this quadrilateral:
193 // 0---3 0---3 3
194 // | | | / /|
195 // | | ==> | / / |
196 // | | |/ / |
197 // 1---2 1 1---2
198 LDTriangle* tri1 = new LDTriangle (vertex (0), vertex (1), vertex (3));
199 LDTriangle* tri2 = new LDTriangle (vertex (1), vertex (2), vertex (3));
200
201 // The triangles also inherit the quad's color
202 tri1->setColor (color());
203 tri2->setColor (color());
204
205 QList<LDTriangle*> triangles;
206 triangles << tri1;
207 triangles << tri2;
208 return triangles;
209 }
210
211 // =============================================================================
212 //
213 void LDObject::replace (LDObject* other)
214 {
215 long idx = lineNumber();
216 assert (idx != -1);
217
218 // Replace the instance of the old object with the new object
219 document()->setObject (idx, other);
220
221 // Remove the old object
222 destroy();
223 }
224
225 // =============================================================================
226 //
227 void LDObject::swap (LDObject* other)
228 {
229 assert (document() == other->document());
230 document()->swapObjects (this, other);
231 }
232
233 // =============================================================================
234 //
235 LDLine::LDLine (Vertex v1, Vertex v2)
236 {
237 setVertex (0, v1);
238 setVertex (1, v2);
239 }
240
241 // =============================================================================
242 //
243 LDQuad::LDQuad (const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3)
244 {
245 setVertex (0, v0);
246 setVertex (1, v1);
247 setVertex (2, v2);
248 setVertex (3, v3);
249 }
250
251 // =============================================================================
252 //
253 LDObject::~LDObject() {}
254
255 // =============================================================================
256 //
257 LDSubfile::~LDSubfile() {}
258
259 // =============================================================================
260 //
261 void LDObject::destroy()
262 {
263 // If this object was selected, unselect it now
264 if (isSelected())
265 unselect();
266
267 // If this object was associated to a file, remove it off it now
268 if (document())
269 document()->forgetObject (this);
270
271 // Delete the GL lists
272 GL::deleteLists (this);
273
274 // Remove this object from the list of LDObjects
275 g_LDObjects.removeOne (this);
276
277 delete this;
278 }
279
280 // =============================================================================
281 //
282 static void transformObject (LDObject* obj, Matrix transform, Vertex pos, int parentcolor)
283 {
284 switch (obj->type())
285 {
286 case LDObject::ELine:
287 case LDObject::ECondLine:
288 case LDObject::ETriangle:
289 case LDObject::EQuad:
290
291 for (int i = 0; i < obj->vertices(); ++i)
292 {
293 Vertex v = obj->vertex (i);
294 v.transform (transform, pos);
295 obj->setVertex (i, v);
296 }
297
298 break;
299
300 case LDObject::ESubfile:
301 {
302 LDSubfile* ref = static_cast<LDSubfile*> (obj);
303 Matrix newMatrix = transform * ref->transform();
304 Vertex newpos = ref->position();
305
306 newpos.transform (transform, pos);
307 ref->setPosition (newpos);
308 ref->setTransform (newMatrix);
309 }
310 break;
311
312 default:
313 break;
314 }
315
316 if (obj->color() == maincolor)
317 obj->setColor (parentcolor);
318 }
319
320 // =============================================================================
321 //
322 LDObjectList LDSubfile::inlineContents (InlineFlags flags)
323 {
324 LDObjectList objs = fileInfo()->inlineContents (flags);
325
326 // Transform the objects
327 for (LDObject* obj : objs)
328 {
329 // Set the parent now so we know what inlined the object.
330 obj->setParent (this);
331 transformObject (obj, transform(), position(), color());
332 }
333
334 return objs;
335 }
336
337 // =============================================================================
338 //
339 long LDObject::lineNumber() const
340 {
341 assert (document() != null);
342
343 for (int i = 0; i < document()->getObjectCount(); ++i)
344 if (document()->getObject (i) == this)
345 return i;
346
347 return -1;
348 }
349
350 // =============================================================================
351 //
352 void LDObject::moveObjects (LDObjectList objs, const bool up)
353 {
354 if (objs.isEmpty())
355 return;
356
357 // If we move down, we need to iterate the array in reverse order.
358 const long start = up ? 0 : (objs.size() - 1);
359 const long end = up ? objs.size() : -1;
360 const long incr = up ? 1 : -1;
361 LDObjectList objsToCompile;
362 LDDocument* file = objs[0]->document();
363
364 for (long i = start; i != end; i += incr)
365 {
366 LDObject* obj = objs[i];
367
368 const long idx = obj->lineNumber(),
369 target = idx + (up ? -1 : 1);
370
371 if ((up && idx == 0) || (!up && idx == (long) (file->objects().size() - 1)))
372 {
373 // One of the objects hit the extrema. If this happens, this should be the first
374 // object to be iterated on. Thus, nothing has changed yet and it's safe to just
375 // abort the entire operation.
376 assert (i == start);
377 return;
378 }
379
380 objsToCompile << obj;
381 objsToCompile << file->getObject (target);
382
383 obj->swap (file->getObject (target));
384 }
385
386 removeDuplicates (objsToCompile);
387
388 // The objects need to be recompiled, otherwise their pick lists are left with
389 // the wrong index colors which messes up selection.
390 for (LDObject* obj : objsToCompile)
391 g_win->R()->compileObject (obj);
392 }
393
394 // =============================================================================
395 //
396 QString LDObject::typeName (LDObject::Type type)
397 {
398 LDObject* obj = LDObject::getDefault (type);
399 QString name = obj->typeName();
400 obj->destroy();
401 return name;
402 }
403
404 // =============================================================================
405 //
406 QString LDObject::describeObjects (const LDObjectList& objs)
407 {
408 bool firstDetails = true;
409 QString text = "";
410
411 if (objs.isEmpty())
412 return "nothing"; // :)
413
414 for (long i = 0; i < ENumTypes; ++i)
415 {
416 Type objType = (Type) i;
417 int count = 0;
418
419 for (LDObject * obj : objs)
420 if (obj->type() == objType)
421 count++;
422
423 if (count == 0)
424 continue;
425
426 if (!firstDetails)
427 text += ", ";
428
429 QString noun = format ("%1%2", typeName (objType), plural (count));
430
431 // Plural of "vertex" is "vertices", correct that
432 if (objType == EVertex && count != 1)
433 noun = "vertices";
434
435 text += format ("%1 %2", count, noun);
436 firstDetails = false;
437 }
438
439 return text;
440 }
441
442 // =============================================================================
443 //
444 LDObject* LDObject::topLevelParent()
445 {
446 if (parent() == null)
447 return this;
448
449 LDObject* it = this;
450
451 while (it->parent() != null)
452 it = it->parent();
453
454 return it;
455 }
456
457 // =============================================================================
458 //
459 LDObject* LDObject::next() const
460 {
461 long idx = lineNumber();
462 assert (idx != -1);
463
464 if (idx == (long) document()->getObjectCount() - 1)
465 return null;
466
467 return document()->getObject (idx + 1);
468 }
469
470 // =============================================================================
471 //
472 LDObject* LDObject::previous() const
473 {
474 long idx = lineNumber();
475 assert (idx != -1);
476
477 if (idx == 0)
478 return null;
479
480 return document()->getObject (idx - 1);
481 }
482
483 // =============================================================================
484 //
485 void LDObject::move (Vertex vect)
486 {
487 if (hasMatrix())
488 {
489 LDMatrixObject* mo = dynamic_cast<LDMatrixObject*> (this);
490 mo->setPosition (mo->position() + vect);
491 }
492 elif (type() == LDObject::EVertex)
493 {
494 // ugh
495 static_cast<LDVertex*> (this)->pos += vect;
496 }
497 else
498 {
499 for (int i = 0; i < vertices(); ++i)
500 setVertex (i, vertex (i) + vect);
501 }
502 }
503
504 // =============================================================================
505 //
506 #define CHECK_FOR_OBJ(N) \
507 if (type == LDObject::E##N) \
508 return new LD##N;
509
510 LDObject* LDObject::getDefault (const LDObject::Type type)
511 {
512 CHECK_FOR_OBJ (Comment)
513 CHECK_FOR_OBJ (BFC)
514 CHECK_FOR_OBJ (Line)
515 CHECK_FOR_OBJ (CondLine)
516 CHECK_FOR_OBJ (Subfile)
517 CHECK_FOR_OBJ (Triangle)
518 CHECK_FOR_OBJ (Quad)
519 CHECK_FOR_OBJ (Empty)
520 CHECK_FOR_OBJ (BFC)
521 CHECK_FOR_OBJ (Error)
522 CHECK_FOR_OBJ (Vertex)
523 CHECK_FOR_OBJ (Overlay)
524 return null;
525 }
526
527 // =============================================================================
528 //
529 void LDObject::invert() {}
530 void LDBFC::invert() {}
531 void LDEmpty::invert() {}
532 void LDComment::invert() {}
533 void LDError::invert() {}
534
535 // =============================================================================
536 //
537 void LDTriangle::invert()
538 {
539 // Triangle goes 0 -> 1 -> 2, reversed: 0 -> 2 -> 1.
540 // Thus, we swap 1 and 2.
541 Vertex tmp = vertex (1);
542 setVertex (1, vertex (2));
543 setVertex (2, tmp);
544
545 return;
546 }
547
548 // =============================================================================
549 //
550 void LDQuad::invert()
551 {
552 // Quad: 0 -> 1 -> 2 -> 3
553 // rev: 0 -> 3 -> 2 -> 1
554 // Thus, we swap 1 and 3.
555 Vertex tmp = vertex (1);
556 setVertex (1, vertex (3));
557 setVertex (3, tmp);
558 }
559
560 // =============================================================================
561 //
562 void LDSubfile::invert()
563 {
564 // Subfiles are inverted when they're prefixed with
565 // a BFC INVERTNEXT statement. Thus we need to toggle this status.
566 // For flat primitives it's sufficient that the determinant is
567 // flipped but I don't have a method for checking flatness yet.
568 // Food for thought...
569
570 int idx = lineNumber();
571
572 if (idx > 0)
573 {
574 LDBFC* bfc = dynamic_cast<LDBFC*> (previous());
575
576 if (bfc && bfc->statement() == LDBFC::InvertNext)
577 {
578 // This is prefixed with an invertnext, thus remove it.
579 bfc->destroy();
580 return;
581 }
582 }
583
584 // Not inverted, thus prefix it with a new invertnext.
585 LDBFC* bfc = new LDBFC (LDBFC::InvertNext);
586 document()->insertObj (idx, bfc);
587 }
588
589 // =============================================================================
590 //
591 static void invertLine (LDObject* line)
592 {
593 // For lines, we swap the vertices. I don't think that a
594 // cond-line's control points need to be swapped, do they?
595 Vertex tmp = line->vertex (0);
596 line->setVertex (0, line->vertex (1));
597 line->setVertex (1, tmp);
598 }
599
600 void LDLine::invert()
601 {
602 invertLine (this);
603 }
604
605 void LDCondLine::invert()
606 {
607 invertLine (this);
608 }
609
610 void LDVertex::invert() {}
611
612 // =============================================================================
613 //
614 LDLine* LDCondLine::demote()
615 {
616 LDLine* repl = new LDLine;
617
618 for (int i = 0; i < repl->vertices(); ++i)
619 repl->setVertex (i, vertex (i));
620
621 repl->setColor (color());
622
623 replace (repl);
624 return repl;
625 }
626
627 // =============================================================================
628 //
629 LDObject* LDObject::fromID (int id)
630 {
631 for (LDObject* obj : g_LDObjects)
632 if (obj->id() == id)
633 return obj;
634
635 return null;
636 }
637
638 // =============================================================================
639 //
640 QString LDOverlay::asText() const
641 {
642 return format ("0 !LDFORGE OVERLAY %1 %2 %3 %4 %5 %6",
643 fileName(), camera(), x(), y(), width(), height());
644 }
645
646 void LDOverlay::invert() {}
647
648 // =============================================================================
649 // Hook the set accessors of certain properties to this changeProperty function.
650 // It takes care of history management so we can capture low-level changes, this
651 // makes history stuff work out of the box.
652 //
653 template<class T> static void changeProperty (LDObject* obj, T* ptr, const T& val)
654 {
655 long idx;
656
657 if (*ptr == val)
658 return;
659
660 if (obj->document() && (idx = obj->lineNumber()) != -1)
661 {
662 QString before = obj->asText();
663 *ptr = val;
664 QString after = obj->asText();
665
666 if (before != after)
667 obj->document()->addToHistory (new EditHistory (idx, before, after));
668 }
669 else
670 *ptr = val;
671 }
672
673 // =============================================================================
674 //
675 void LDObject::setColor (const int& val)
676 {
677 changeProperty (this, &m_color, val);
678 }
679
680 // =============================================================================
681 //
682 const Vertex& LDObject::vertex (int i) const
683 {
684 return m_coords[i]->data();
685 }
686
687 // =============================================================================
688 //
689 void LDObject::setVertex (int i, const Vertex& vert)
690 {
691 changeProperty (this, &m_coords[i], LDSharedVertex::getSharedVertex (vert));
692 }
693
694 // =============================================================================
695 //
696 void LDMatrixObject::setPosition (const Vertex& a)
697 {
698 changeProperty (linkPointer(), &m_position, LDSharedVertex::getSharedVertex (a));
699 }
700
701 // =============================================================================
702 //
703 void LDMatrixObject::setTransform (const Matrix& val)
704 {
705 changeProperty (linkPointer(), &m_transform, val);
706 }
707
708 // =============================================================================
709 //
710 static QMap<Vertex, LDSharedVertex*> g_sharedVerts;
711
712 LDSharedVertex* LDSharedVertex::getSharedVertex (const Vertex& a)
713 {
714 auto it = g_sharedVerts.find (a);
715
716 if (it == g_sharedVerts.end())
717 {
718 LDSharedVertex* v = new LDSharedVertex (a);
719 g_sharedVerts[a] = v;
720 return v;
721 }
722
723 return *it;
724 }
725
726 // =============================================================================
727 //
728 void LDSharedVertex::addRef (LDObject* a)
729 {
730 m_refs << a;
731 }
732
733 // =============================================================================
734 //
735 void LDSharedVertex::delRef (LDObject* a)
736 {
737 m_refs.removeOne (a);
738
739 if (m_refs.empty())
740 {
741 g_sharedVerts.remove (m_data);
742 delete this;
743 }
744 }
745
746 // =============================================================================
747 //
748 void LDObject::select()
749 {
750 assert (document() != null);
751 document()->addToSelection (this);
752 }
753
754 // =============================================================================
755 //
756 void LDObject::unselect()
757 {
758 assert (document() != null);
759 document()->removeFromSelection (this);
760 }
761
762 // =============================================================================
763 //
764 QString getLicenseText (int id)
765 {
766 switch (id)
767 {
768 case 0:
769 return g_CALicense;
770
771 case 1:
772 return g_nonCALicense;
773
774 case 2:
775 return "";
776 }
777
778 assert (false);
779 return "";
780 }
781
782 // =============================================================================
783 //
784 LDObject* LDObject::createCopy() const
785 {
786 /*
787 LDObject* copy = clone();
788 copy->setFile (null);
789 copy->setGLInit (false);
790 copy->chooseID();
791 copy->setSelected (false);
792 */
793
794 /*
795 LDObject* copy = getDefault (getType());
796 copy->setColor (color());
797
798 if (hasMatrix())
799 {
800 LDMatrixObject* copyMo = static_cast<LDMatrixObject*> (copy);
801 const LDMatrixObject* mo = static_cast<const LDMatrixObject*> (this);
802 copyMo->setPosition (mo->getPosition());
803 copyMo->setTransform (mo->transform());
804 }
805 else
806 {
807 for (int i = 0; i < vertices(); ++i)
808 copy->setVertex (getVertex (i));
809 }
810
811 switch (getType())
812 {
813 case Subfile:
814 {
815 LDSubfile* copyRef = static_cast<LDSubfile*> (copy);
816 const LDSubfile* ref = static_cast<const LDSubfile*> (this);
817
818 copyRef->setFileInfo (ref->fileInfo());
819 }
820 }
821 */
822
823 LDObject* copy = parseLine (asText());
824 return copy;
825 }

mercurial