src/linetypes/compoundobject.cpp

Mon, 16 May 2022 01:40:49 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Mon, 16 May 2022 01:40:49 +0300
changeset 187
30204975694a
parent 186
922662adb72a
permissions
-rw-r--r--

work on circle tool

183
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #include "compoundobject.h"
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 #include "documentmanager.h"
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 #include "invert.h"
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 #include "polygoncache.h"
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 ldraw::CompoundObject::CompoundObject
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 (
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 const glm::mat4& transformation,
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 const Color color
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 ) :
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 ColoredObject{color},
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 transformation{transformation}
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 QVariant ldraw::CompoundObject::getProperty(Property property) const
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 switch (property)
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 case Property::Transformation:
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 return QVariant::fromValue(this->transformation);
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 case Property::IsInverted:
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 return this->isInverted;
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 default:
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 return ColoredObject::getProperty(property);
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 void ldraw::CompoundObject::setProperty(SetPropertyResult* result, const PropertyKeyValue& pair)
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 LDRAW_OBJECT_HANDLE_SET_PROPERTY(Transformation, {this->transformation = value;});
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 LDRAW_OBJECT_HANDLE_SET_PROPERTY(IsInverted, {this->isInverted = value;});
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 ldraw::ColoredObject::setProperty(result, pair);
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 glm::vec3 ldraw::CompoundObject::position() const
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 return this->transformation[3];
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 void ldraw::CompoundObject::invert(GetPolygonsContext *context)
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 const std::optional<Axis> flatDimension = context ? this->flatDimension(context) : std::optional<Axis>{};
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 if (flatDimension.has_value())
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 glm::mat4 matrix = glm::identity<glm::mat4>();
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 matrix[*flatDimension][*flatDimension] = -1.0f;
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 this->transformation *= matrix;
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 else
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 this->isInverted = not this->isInverted;
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 QDataStream& ldraw::CompoundObject::serialize(QDataStream &stream) const
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 return ColoredObject::serialize(stream) << this->transformation << this->isInverted;
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 QDataStream& ldraw::CompoundObject::deserialize(QDataStream &stream)
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 return ColoredObject::deserialize(stream) >> this->transformation >> this->isInverted;
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 /**
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 * @brief Finds out in which dimension the object is flat in. In that dimension all vertices have a value of 0.
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 * If the object is not flat, this does not return a value.
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 * @param model Model to find out flatness of
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 * @param documents Where to look for subfiles
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 * @returns dimension the model is flat in, if such dimension exists, no value otherwise.
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 */
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 std::optional<Axis> ldraw::CompoundObject::flatDimension(GetPolygonsContext *context) const
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 // The dimensions that this model is potentially flat in.
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 QVector<Axis> dimensions = {X, Y, Z};
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 std::vector<gl::Polygon> polygons;
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 this->getPolygons(polygons, context);
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 for (const gl::Polygon& polygon : polygons)
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1)
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 const glm::vec3& v_i = polygon.vertices[i];
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 if (not qFuzzyCompare(v_i.x, 0.0f))
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 dimensions.removeOne(X);
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 if (not qFuzzyCompare(v_i.y, 0.0f))
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 dimensions.removeOne(Y);
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 if (not qFuzzyCompare(v_i.z, 0.0f))
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 dimensions.removeOne(Z);
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 // If there are no more dimensions left, we can exit the loop.
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 if (dimensions.isEmpty())
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 break;
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
103 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 if (dimensions.size() == 1)
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 // The model is flat in one dimension, return that.
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 // If the model is flat in two or three dimensions, it's not really a valid model.
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
108 return dimensions[0];
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
110 else
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
111 {
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
112 // The model is not flat.
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113 return {};
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
114 }
97b591813c8b - Add editors for string and bool properties
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
115 }
186
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
116
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
117 QString ldraw::CompoundObject::transformToBareString() const
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
118 {
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
119 return utility::format(
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
120 "%1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11 %12",
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
121 this->transformation[3][0],
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
122 this->transformation[3][1],
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
123 this->transformation[3][2],
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
124 this->transformation[0][0],
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
125 this->transformation[1][0],
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
126 this->transformation[2][0],
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
127 this->transformation[0][1],
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
128 this->transformation[1][1],
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
129 this->transformation[2][1],
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
130 this->transformation[0][2],
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
131 this->transformation[1][2],
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
132 this->transformation[2][2]);
922662adb72a work on circle tool
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
133 }

mercurial