src/LDObject.h

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

mercurial