Tue, 28 Sep 2021 22:17:52 +0300
Fix handling of "BFC INVERTNEXT" with multiple inner whitespaces
3 | 1 | #include "subfilereference.h" |
21 | 2 | #include "documentmanager.h" |
26 | 3 | #include "invert.h" |
3 | 4 | |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
5 | ldraw::SubfileReference::SubfileReference |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
6 | ( |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
7 | const glm::mat4& transformation, |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
8 | const QString& referenceName, |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
9 | const Color color |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
10 | ) : |
13 | 11 | ColoredObject{color}, |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
12 | transformation{transformation}, |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
13 | referenceName{referenceName} |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
14 | { |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
15 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
16 | |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
17 | QVariant ldraw::SubfileReference::getProperty(Property property) const |
3 | 18 | { |
19 | switch (property) | |
20 | { | |
21 | case Property::Transformation: | |
22 | return QVariant::fromValue(this->transformation); | |
23 | case Property::ReferenceName: | |
24 | return this->referenceName; | |
25 | default: | |
13 | 26 | return ColoredObject::getProperty(property); |
3 | 27 | } |
28 | } | |
6 | 29 | |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
30 | void ldraw::SubfileReference::setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
31 | { |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
32 | LDRAW_OBJECT_HANDLE_SET_PROPERTY(Transformation, {this->transformation = value;}); |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
33 | LDRAW_OBJECT_HANDLE_SET_PROPERTY(ReferenceName, {this->referenceName = value;}); |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
34 | ldraw::ColoredObject::setProperty(result, pair); |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
35 | } |
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
6
diff
changeset
|
36 | |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
37 | QString ldraw::SubfileReference::textRepresentation() const |
6 | 38 | { |
141 | 39 | return this->referenceName + " " + utility::vertexToStringParens(this->position()); |
6 | 40 | } |
21 | 41 | |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
42 | void ldraw::SubfileReference::getPolygons |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
43 | ( |
21 | 44 | std::vector<gl::Polygon>& polygons, |
86
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
45 | GetPolygonsContext* context |
4bec0525ef1b
PolygonObjectEditor can now modify the object properly
Teemu Piippo <teemu@hecknology.net>
parents:
81
diff
changeset
|
46 | ) const |
21 | 47 | { |
48 | Model* model = this->resolve(context->documents); | |
49 | if (model != nullptr) | |
50 | { | |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
51 | const bool needInverting = glm::determinant(this->transformation) < 0; |
21 | 52 | const std::vector<gl::Polygon> modelPolygons = model->getPolygons(context->documents); |
53 | polygons.reserve(polygons.size() + modelPolygons.size()); | |
54 | for (gl::Polygon polygon : modelPolygons) | |
55 | { | |
23
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
56 | for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1) |
21 | 57 | { |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
58 | glm::vec4 vertex {polygon.vertices[i], 1}; |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
59 | vertex = this->transformation * vertex; |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
60 | polygon.vertices[i] = vertex; |
21 | 61 | } |
26 | 62 | if (needInverting != this->isInverted) |
63 | { | |
64 | gl::invert(polygon); | |
65 | } | |
139
72098474d362
Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents:
134
diff
changeset
|
66 | if (polygon.color == ldraw::MAIN_COLOR) |
21 | 67 | { |
68 | polygon.color = this->colorIndex; | |
69 | } | |
70 | polygon.id = this->id; | |
71 | polygons.push_back(polygon); | |
72 | } | |
73 | } | |
74 | } | |
75 | ||
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
76 | glm::vec3 ldraw::SubfileReference::position() const |
21 | 77 | { |
81
62373840e33a
object editor widgets start to form up
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
78 | return this->transformation[3]; |
21 | 79 | } |
80 | ||
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
81 | void ldraw::SubfileReference::invert() |
26 | 82 | { |
83 | this->isInverted = not this->isInverted; | |
84 | } | |
85 | ||
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
86 | Model* ldraw::SubfileReference::resolve(DocumentManager* documents) const |
21 | 87 | { |
88 | return documents->findModelByName(this->referenceName); | |
89 | } | |
132
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
86
diff
changeset
|
90 | |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
86
diff
changeset
|
91 | ldraw::Object::Type ldraw::SubfileReference::typeIdentifier() const |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
86
diff
changeset
|
92 | { |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
86
diff
changeset
|
93 | return Type::SubfileReference; |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
86
diff
changeset
|
94 | } |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
86
diff
changeset
|
95 | |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
86
diff
changeset
|
96 | QDataStream& ldraw::SubfileReference::serialize(QDataStream &stream) const |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
86
diff
changeset
|
97 | { |
134
f77d2230e87c
Add remaining serialize methods
Teemu Piippo <teemu@hecknology.net>
parents:
132
diff
changeset
|
98 | return ColoredObject::serialize(stream) << this->transformation << this->referenceName << this->isInverted; |
132
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
86
diff
changeset
|
99 | } |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
86
diff
changeset
|
100 | |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
86
diff
changeset
|
101 | QDataStream& ldraw::SubfileReference::deserialize(QDataStream &stream) |
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
86
diff
changeset
|
102 | { |
134
f77d2230e87c
Add remaining serialize methods
Teemu Piippo <teemu@hecknology.net>
parents:
132
diff
changeset
|
103 | return ColoredObject::deserialize(stream) >> this->transformation >> this->referenceName >> this->isInverted; |
132
488d0ba6070b
Begin work with serialization
Teemu Piippo <teemu@hecknology.net>
parents:
86
diff
changeset
|
104 | } |
141 | 105 | |
106 | QString ldraw::SubfileReference::toLDrawCode() const | |
107 | { | |
108 | QString result; | |
109 | if (this->isInverted) | |
110 | { | |
111 | result += "0 BFC INVERTNEXT\r\n"; | |
112 | } | |
113 | result += utility::format( | |
114 | "1 %1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11 %12 %13 %14", | |
115 | this->colorIndex.index, | |
116 | this->transformation[3][0], | |
117 | this->transformation[3][1], | |
118 | this->transformation[3][2], | |
119 | this->transformation[0][0], | |
120 | this->transformation[1][0], | |
121 | this->transformation[2][0], | |
122 | this->transformation[0][1], | |
123 | this->transformation[1][1], | |
124 | this->transformation[2][1], | |
125 | this->transformation[0][2], | |
126 | this->transformation[1][2], | |
127 | this->transformation[2][2], | |
128 | this->referenceName); | |
129 | return result; | |
130 | } |