src/ldtypes.cc

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

mercurial