1 #include "subfilereference.h" |
|
2 #include "documentmanager.h" |
|
3 #include "invert.h" |
|
4 #include "polygoncache.h" |
|
5 |
|
6 ldraw::SubfileReference::SubfileReference |
|
7 ( |
|
8 const glm::mat4& transformation, |
|
9 const QString& referenceName, |
|
10 const Color color |
|
11 ) : |
|
12 CompoundObject{transformation, color}, |
|
13 referenceName{referenceName} |
|
14 { |
|
15 } |
|
16 |
|
17 QVariant ldraw::SubfileReference::getProperty(Property property) const |
|
18 { |
|
19 switch (property) |
|
20 { |
|
21 case Property::ReferenceName: |
|
22 return this->referenceName; |
|
23 default: |
|
24 return CompoundObject::getProperty(property); |
|
25 } |
|
26 } |
|
27 |
|
28 void ldraw::SubfileReference::setProperty(SetPropertyResult* result, const PropertyKeyValue& pair) |
|
29 { |
|
30 LDRAW_OBJECT_HANDLE_SET_PROPERTY(ReferenceName, {this->referenceName = value;}); |
|
31 ldraw::CompoundObject::setProperty(result, pair); |
|
32 } |
|
33 |
|
34 QString ldraw::SubfileReference::textRepresentation() const |
|
35 { |
|
36 return this->referenceName + " " + utility::vertexToStringParens(this->position()); |
|
37 } |
|
38 |
|
39 void ldraw::SubfileReference::getPolygons |
|
40 ( |
|
41 std::vector<gl::Polygon>& polygons, |
|
42 GetPolygonsContext* context |
|
43 ) const |
|
44 { |
|
45 Model* dependency = this->resolve(context->modelId, context->documents); |
|
46 PolygonCache* cache = nullptr; |
|
47 if (dependency != nullptr) |
|
48 { |
|
49 const auto dependencyModelId = context->documents->findIdForModel(dependency); |
|
50 if (dependencyModelId.has_value()) |
|
51 { |
|
52 cache = context->documents->getPolygonCacheForModel(dependencyModelId.value()); |
|
53 } |
|
54 } |
|
55 if (cache != nullptr) |
|
56 { |
|
57 const bool needInverting = glm::determinant(this->transformation) < 0; |
|
58 const std::vector<gl::Polygon> modelPolygons = getCachedPolygons( |
|
59 cache, |
|
60 dependency, |
|
61 context->documents); |
|
62 polygons.reserve(polygons.size() + modelPolygons.size()); |
|
63 for (gl::Polygon polygon : modelPolygons) |
|
64 { |
|
65 for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1) |
|
66 { |
|
67 glm::vec4 vertex {polygon.vertices[i], 1}; |
|
68 vertex = this->transformation * vertex; |
|
69 polygon.vertices[i] = vertex; |
|
70 } |
|
71 if (needInverting != this->isInverted) |
|
72 { |
|
73 gl::invert(polygon); |
|
74 } |
|
75 if (polygon.color == ldraw::MAIN_COLOR) |
|
76 { |
|
77 polygon.color = this->colorIndex; |
|
78 } |
|
79 polygon.id = this->id; |
|
80 polygons.push_back(polygon); |
|
81 } |
|
82 } |
|
83 } |
|
84 |
|
85 Model* ldraw::SubfileReference::resolve(const ModelId callingModelId, DocumentManager* documents) const |
|
86 { |
|
87 return documents->findDependencyByName(callingModelId, this->referenceName); |
|
88 } |
|
89 |
|
90 ldraw::Object::Type ldraw::SubfileReference::typeIdentifier() const |
|
91 { |
|
92 return Type::SubfileReference; |
|
93 } |
|
94 |
|
95 QDataStream& ldraw::SubfileReference::serialize(QDataStream &stream) const |
|
96 { |
|
97 return CompoundObject::serialize(stream) << this->referenceName; |
|
98 } |
|
99 |
|
100 QDataStream& ldraw::SubfileReference::deserialize(QDataStream &stream) |
|
101 { |
|
102 return CompoundObject::deserialize(stream) >> this->referenceName; |
|
103 } |
|
104 |
|
105 QString ldraw::SubfileReference::toLDrawCode() const |
|
106 { |
|
107 QString result; |
|
108 if (this->isInverted) |
|
109 { |
|
110 result += "0 BFC INVERTNEXT\r\n"; |
|
111 } |
|
112 result += utility::format( |
|
113 "1 %1 %2 %3", |
|
114 this->colorIndex.index, |
|
115 this->transformToBareString(), |
|
116 this->referenceName); |
|
117 return result; |
|
118 } |
|
119 |
|
120 QString ldraw::SubfileReference::iconName() const |
|
121 { |
|
122 return ":/icons/linetype-subfile.png"; |
|
123 } |
|
124 |
|
125 QString ldraw::SubfileReference::typeName() const |
|
126 { |
|
127 return QObject::tr("subfile reference"); |
|
128 } |
|