src/LDObject.h

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 #pragma once
20 #include "Main.h"
21 #include "Types.h"
22 #include "misc/DocumentPointer.h"
23
24 #define LDOBJ(T) \
25 protected: \
26 virtual LD##T* clone() override \
27 { \
28 return new LD##T (*this); \
29 } \
30 \
31 public: \
32 virtual LDObject::Type type() const override \
33 { \
34 return LDObject::E##T; \
35 } \
36 virtual QString asText() const override; \
37 virtual void invert() override;
38
39 #define LDOBJ_NAME(N) virtual QString typeName() const override { return #N; }
40 #define LDOBJ_VERTICES(V) virtual int vertices() const override { return V; }
41 #define LDOBJ_SETCOLORED(V) virtual bool isColored() const override { return V; }
42 #define LDOBJ_COLORED LDOBJ_SETCOLORED (true)
43 #define LDOBJ_UNCOLORED LDOBJ_SETCOLORED (false)
44
45 #define LDOBJ_CUSTOM_SCEMANTIC virtual bool isScemantic() const override
46 #define LDOBJ_SCEMANTIC LDOBJ_CUSTOM_SCEMANTIC { return true; }
47 #define LDOBJ_NON_SCEMANTIC LDOBJ_CUSTOM_SCEMANTIC { return false; }
48
49 #define LDOBJ_SETMATRIX(V) virtual bool hasMatrix() const override { return V; }
50 #define LDOBJ_HAS_MATRIX LDOBJ_SETMATRIX (true)
51 #define LDOBJ_NO_MATRIX LDOBJ_SETMATRIX (false)
52
53 class QListWidgetItem;
54 class LDSubfile;
55 class LDDocument;
56 class LDSharedVertex;
57
58 // =============================================================================
59 // LDObject
60 //
61 // Base class object for all object types. Each LDObject represents a single line
62 // in the LDraw code file. The virtual method getType returns an enumerator
63 // which is a token of the object's type. The object can be casted into
64 // sub-classes based on this enumerator.
65 // =============================================================================
66 class LDObject
67 {
68 PROPERTY (public, bool, isHidden, setHidden, STOCK_WRITE)
69 PROPERTY (public, bool, isSelected, setSelected, STOCK_WRITE)
70 PROPERTY (public, LDObject*, parent, setParent, STOCK_WRITE)
71 PROPERTY (public, LDDocument*, document, setDocument, STOCK_WRITE)
72 PROPERTY (private, int, id, setID, STOCK_WRITE)
73 PROPERTY (public, int, color, setColor, CUSTOM_WRITE)
74 PROPERTY (public, bool, isGLInit, setGLInit, STOCK_WRITE)
75
76 public:
77 // Object type codes.
78 enum Type
79 {
80 ESubfile, // Object represents a sub-file reference
81 EQuad, // Object represents a quadrilateral
82 ETriangle, // Object represents a triangle
83 ELine, // Object represents a line
84 ECondLine, // Object represents a conditional line
85 EVertex, // Object is a vertex, LDForge extension object
86 EBFC, // Object represents a BFC statement
87 EOverlay, // Object contains meta-info about an overlay image.
88 EComment, // Object represents a comment
89 EError, // Object is the result of failed parsing
90 EEmpty, // Object represents an empty line
91 EUnidentified, // Unknown object type (some functions return this; TODO: they probably should not)
92 ENumTypes // Amount of object types
93 };
94
95 LDObject();
96
97 // Makes a copy of this object
98 LDObject* createCopy() const;
99
100 // Deletes this object
101 void destroy();
102
103 // Index (i.e. line number) of this object
104 long lineNumber() const;
105
106 // Type enumerator of this object
107 virtual Type type() const = 0;
108
109 // Get a vertex by index
110 const Vertex& vertex (int i) const;
111
112 // Type name of this object
113 virtual QString typeName() const = 0;
114
115 // Does this object have a matrix and position? (see LDMatrixObject)
116 virtual bool hasMatrix() const = 0;
117
118 // Inverts this object (winding is reversed)
119 virtual void invert() = 0;
120
121 // Is this object colored?
122 virtual bool isColored() const = 0;
123
124 // Does this object have meaning in the part model?
125 virtual bool isScemantic() const = 0;
126
127 // Moves this object using the given vertex as a movement List
128 void move (Vertex vect);
129
130 // Object after this in the current file
131 LDObject* next() const;
132
133 // Object prior to this in the current file
134 LDObject* previous() const;
135
136 // This object as LDraw code
137 virtual QString asText() const = 0;
138
139 // Replace this LDObject with another LDObject. Object is deleted in the process.
140 void replace (LDObject* other);
141
142 // Selects this object.
143 void select();
144
145 // Set a vertex to the given value
146 void setVertex (int i, const Vertex& vert);
147
148 // Set a single coordinate of a vertex
149 void setVertexCoord (int i, Axis ax, double value);
150
151 // Swap this object with another.
152 void swap (LDObject* other);
153
154 // What object in the current file ultimately references this?
155 LDObject* topLevelParent();
156
157 // Removes this object from selection // TODO: rename to deselect?
158 void unselect();
159
160 // Number of vertices this object has // TODO: rename to getNumVertices
161 virtual int vertices() const = 0;
162
163 // Get type name by enumerator
164 static QString typeName (LDObject::Type type);
165
166 // Returns a default-constructed LDObject by the given type
167 static LDObject* getDefault (const LDObject::Type type);
168
169 // TODO: move this to LDDocument?
170 static void moveObjects (LDObjectList objs, const bool up);
171
172 // Get a description of a list of LDObjects
173 static QString describeObjects (const LDObjectList& objs);
174 static LDObject* fromID (int id);
175
176 // TODO: make these private!
177 // OpenGL list for this object
178 uint glLists[4];
179
180 // Object list entry for this object
181 QListWidgetItem* qObjListEntry;
182
183 protected:
184 // LDObjects are to be deleted with the deleteSelf() method, not with
185 // operator delete. This is because it seems virtual functions cannot
186 // be properly called from the destructor, thus a normal method must
187 // be used instead. The destructor also doesn't seem to be able to
188 // be private without causing a truckload of problems so it's protected
189 // instead.
190 virtual ~LDObject();
191 void chooseID();
192
193 private:
194 virtual LDObject* clone() = 0;
195 LDSharedVertex* m_coords[4];
196 };
197
198 // =============================================================================
199 // LDSharedVertex
200 //
201 // For use as coordinates of LDObjects. Keeps count of references.
202 // =============================================================================
203 class LDSharedVertex
204 {
205 public:
206 inline const Vertex& data() const
207 {
208 return m_data;
209 }
210
211 inline operator const Vertex&() const
212 {
213 return m_data;
214 }
215
216 void addRef (LDObject* a);
217 void delRef (LDObject* a);
218
219 static LDSharedVertex* getSharedVertex (const Vertex& a);
220
221 protected:
222 LDSharedVertex (const Vertex& a) : m_data (a) {}
223
224 private:
225 LDObjectList m_refs;
226 Vertex m_data;
227 };
228
229 // =============================================================================
230 //
231 // Common code for objects with matrices. This class is multiple-derived in
232 // and thus not used directly other than as a common storage point for matrices
233 // and vertices.
234 //
235 // The link pointer is a pointer to this object's LDObject self - since this is
236 // multiple-derived in, static_cast or dynamic_cast won't budge here.
237 //
238 // In 0.1-alpha, there was a separate 'radial' type which had a position and
239 // matrix as well. Even though right now only LDSubfile uses this, I'm keeping
240 // this class distinct in case I get new extension ideas. :)
241 //
242 class LDMatrixObject
243 {
244 PROPERTY (public, LDObject*, linkPointer, setLinkPointer, STOCK_WRITE)
245 PROPERTY (public, Matrix, transform, setTransform, CUSTOM_WRITE)
246
247 public:
248 LDMatrixObject() :
249 m_position (LDSharedVertex::getSharedVertex (g_origin)) {}
250
251 LDMatrixObject (const Matrix& transform, const Vertex& pos) :
252 m_transform (transform),
253 m_position (LDSharedVertex::getSharedVertex (pos)) {}
254
255 inline const Vertex& position() const
256 {
257 return m_position->data();
258 }
259
260 void setCoordinate (const Axis ax, double value)
261 {
262 Vertex v = position();
263 v[ax] = value;
264 setPosition (v);
265 }
266
267 void setPosition (const Vertex& a);
268
269 private:
270 LDSharedVertex* m_position;
271 };
272
273 // =============================================================================
274 //
275 // Represents a line in the LDraw file that could not be properly parsed. It is
276 // represented by a (!) ERROR in the code view. It exists for the purpose of
277 // allowing garbage lines be debugged and corrected within LDForge.
278 //
279 class LDError : public LDObject
280 {
281 LDOBJ (Error)
282 LDOBJ_NAME (error)
283 LDOBJ_VERTICES (0)
284 LDOBJ_UNCOLORED
285 LDOBJ_SCEMANTIC
286 LDOBJ_NO_MATRIX
287 PROPERTY (public, QString, fileReferenced, setFileReferenced, STOCK_WRITE)
288 PROPERTY (private, QString, contents, setContents, STOCK_WRITE)
289 PROPERTY (private, QString, reason, setReason, STOCK_WRITE)
290
291 public:
292 LDError();
293 LDError (QString contents, QString reason) :
294 m_contents (contents),
295 m_reason (reason) {}
296 };
297
298 // =============================================================================
299 //
300 // Represents an empty line in the LDraw code file.
301 //
302 class LDEmpty : public LDObject
303 {
304 LDOBJ (Empty)
305 LDOBJ_NAME (empty)
306 LDOBJ_VERTICES (0)
307 LDOBJ_UNCOLORED
308 LDOBJ_NON_SCEMANTIC
309 LDOBJ_NO_MATRIX
310 };
311
312 // =============================================================================
313 //
314 // Represents a code-0 comment in the LDraw code file.
315 //
316 class LDComment : public LDObject
317 {
318 PROPERTY (public, QString, text, setText, STOCK_WRITE)
319 LDOBJ (Comment)
320 LDOBJ_NAME (comment)
321 LDOBJ_VERTICES (0)
322 LDOBJ_UNCOLORED
323 LDOBJ_NON_SCEMANTIC
324 LDOBJ_NO_MATRIX
325
326 public:
327 LDComment() {}
328 LDComment (QString text) : m_text (text) {}
329 };
330
331 // =============================================================================
332 //
333 // Represents a 0 BFC statement in the LDraw code. eStatement contains the type
334 // of this statement.
335 //
336 class LDBFC : public LDObject
337 {
338 public:
339 enum Statement
340 {
341 CertifyCCW,
342 CCW,
343 CertifyCW,
344 CW,
345 NoCertify,
346 InvertNext,
347 Clip,
348 ClipCCW,
349 ClipCW,
350 NoClip,
351 NumStatements
352 };
353
354 LDOBJ (BFC)
355 LDOBJ_NAME (bfc)
356 LDOBJ_VERTICES (0)
357 LDOBJ_UNCOLORED
358 LDOBJ_CUSTOM_SCEMANTIC { return (statement() == InvertNext); }
359 LDOBJ_NO_MATRIX
360 PROPERTY (public, Statement, statement, setStatement, STOCK_WRITE)
361
362 public:
363 LDBFC() {}
364 LDBFC (const LDBFC::Statement type) :
365 m_statement (type) {}
366
367 // Statement strings
368 static const char* k_statementStrings[];
369 };
370
371 // =============================================================================
372 // LDSubfile
373 //
374 // Represents a single code-1 subfile reference.
375 // =============================================================================
376 class LDSubfile : public LDObject, public LDMatrixObject
377 {
378 LDOBJ (Subfile)
379 LDOBJ_NAME (subfile)
380 LDOBJ_VERTICES (0)
381 LDOBJ_COLORED
382 LDOBJ_SCEMANTIC
383 LDOBJ_HAS_MATRIX
384 PROPERTY (public, LDDocumentPointer, fileInfo, setFileInfo, STOCK_WRITE)
385
386 public:
387 enum InlineFlag
388 {
389 DeepInline = (1 << 0),
390 CacheInline = (1 << 1),
391 RendererInline = (1 << 2),
392 DeepCacheInline = (DeepInline | CacheInline),
393 };
394
395 Q_DECLARE_FLAGS (InlineFlags, InlineFlag)
396
397 LDSubfile()
398 {
399 setLinkPointer (this);
400 }
401
402 // Inlines this subfile. Note that return type is an array of heap-allocated
403 // LDObject copies, they must be deleted manually.
404 LDObjectList inlineContents (InlineFlags flags);
405
406 protected:
407 ~LDSubfile();
408 };
409
410 Q_DECLARE_OPERATORS_FOR_FLAGS (LDSubfile::InlineFlags)
411
412 // =============================================================================
413 // LDLine
414 //
415 // Represents a single code-2 line in the LDraw code file. v0 and v1 are the end
416 // points of the line. The line is colored with dColor unless uncolored mode is
417 // set.
418 // =============================================================================
419 class LDLine : public LDObject
420 {
421 LDOBJ (Line)
422 LDOBJ_NAME (line)
423 LDOBJ_VERTICES (2)
424 LDOBJ_COLORED
425 LDOBJ_SCEMANTIC
426 LDOBJ_NO_MATRIX
427
428 public:
429 LDLine() {}
430 LDLine (Vertex v1, Vertex v2);
431 };
432
433 // =============================================================================
434 // LDCondLine
435 //
436 // Represents a single code-5 conditional line. The end-points v0 and v1 are
437 // inherited from LDLine, c0 and c1 are the control points of this line.
438 // =============================================================================
439 class LDCondLine : public LDLine
440 {
441 LDOBJ (CondLine)
442 LDOBJ_NAME (condline)
443 LDOBJ_VERTICES (4)
444 LDOBJ_COLORED
445 LDOBJ_SCEMANTIC
446 LDOBJ_NO_MATRIX
447
448 public:
449 LDCondLine() {}
450 LDLine* demote();
451 };
452
453 // =============================================================================
454 // LDTriangle
455 //
456 // Represents a single code-3 triangle in the LDraw code file. Vertices v0, v1
457 // and v2 contain the end-points of this triangle. dColor is the color the
458 // triangle is colored with.
459 // =============================================================================
460 class LDTriangle : public LDObject
461 {
462 LDOBJ (Triangle)
463 LDOBJ_NAME (triangle)
464 LDOBJ_VERTICES (3)
465 LDOBJ_COLORED
466 LDOBJ_SCEMANTIC
467 LDOBJ_NO_MATRIX
468
469 public:
470 LDTriangle() {}
471 LDTriangle (Vertex v0, Vertex v1, Vertex v2)
472 {
473 setVertex (0, v0);
474 setVertex (1, v1);
475 setVertex (2, v2);
476 }
477 };
478
479 // =============================================================================
480 // LDQuad
481 //
482 // Represents a single code-4 quadrilateral. v0, v1, v2 and v3 are the end points
483 // of the quad, dColor is the color used for the quad.
484 // =============================================================================
485 class LDQuad : public LDObject
486 {
487 LDOBJ (Quad)
488 LDOBJ_NAME (quad)
489 LDOBJ_VERTICES (4)
490 LDOBJ_COLORED
491 LDOBJ_SCEMANTIC
492 LDOBJ_NO_MATRIX
493
494 public:
495 LDQuad() {}
496 LDQuad (const Vertex& v0, const Vertex& v1, const Vertex& v2, const Vertex& v3);
497
498 // Split this quad into two triangles (note: heap-allocated)
499 QList<LDTriangle*> splitToTriangles();
500 };
501
502 // =============================================================================
503 // LDVertex
504 //
505 // The vertex is an LDForce-specific extension which represents a single
506 // vertex which can be used as a parameter to tools or to store coordinates
507 // with. Vertices are a part authoring tool and they should not appear in
508 // finished parts.
509 // =============================================================================
510 class LDVertex : public LDObject
511 {
512 LDOBJ (Vertex)
513 LDOBJ_NAME (vertex)
514 LDOBJ_VERTICES (0) // TODO: move pos to vaCoords[0]
515 LDOBJ_COLORED
516 LDOBJ_NON_SCEMANTIC
517 LDOBJ_NO_MATRIX
518
519 public:
520 LDVertex() {}
521
522 Vertex pos;
523 };
524
525 // =============================================================================
526 // LDOverlay
527 //
528 // Overlay image meta, stored in the header of parts so as to preserve overlay
529 // information.
530 // =============================================================================
531 class LDOverlay : public LDObject
532 {
533 LDOBJ (Overlay)
534 LDOBJ_NAME (overlay)
535 LDOBJ_VERTICES (0)
536 LDOBJ_UNCOLORED
537 LDOBJ_NON_SCEMANTIC
538 LDOBJ_NO_MATRIX
539 PROPERTY (public, int, camera, setCamera, STOCK_WRITE)
540 PROPERTY (public, int, x, setX, STOCK_WRITE)
541 PROPERTY (public, int, y, setY, STOCK_WRITE)
542 PROPERTY (public, int, width, setWidth, STOCK_WRITE)
543 PROPERTY (public, int, height, setHeight, STOCK_WRITE)
544 PROPERTY (public, QString, fileName, setFileName, STOCK_WRITE)
545 };
546
547 // Other common LDraw stuff
548 static const QString g_CALicense ("!LICENSE Redistributable under CCAL version 2.0 : see CAreadme.txt");
549 static const QString g_nonCALicense ("!LICENSE Not redistributable : see NonCAreadme.txt");
550 static const int g_lores = 16;
551 static const int g_hires = 48;
552
553 QString getLicenseText (int id);

mercurial