src/linetypes/circularprimitive.cpp

changeset 1418
503d4e7e27c9
parent 1412
f5eb947a2e7f
child 1425
5354313b9958
equal deleted inserted replaced
1417:ed39bfca7a67 1418:503d4e7e27c9
6 #include "quadrilateral.h" 6 #include "quadrilateral.h"
7 #include "primitives.h" 7 #include "primitives.h"
8 8
9 QString LDCircularPrimitive::buildFilename() const 9 QString LDCircularPrimitive::buildFilename() const
10 { 10 {
11 int numerator = this->m_segments; 11 int numerator = segments();
12 int denominator = this->m_divisions; 12 int denominator = divisions();
13 QString prefix; 13 QString prefix;
14 14
15 if (m_divisions != MediumResolution) 15 if (divisions() != MediumResolution)
16 prefix = QString::number(m_divisions) + '\\'; 16 prefix = QString::number(divisions()) + '\\';
17 17
18 simplify(numerator, denominator); 18 simplify(numerator, denominator);
19 19
20 // Ensure that the denominator is at least 4, expand if necessary 20 // Ensure that the denominator is at least 4, expand if necessary
21 if (denominator < 4) 21 if (denominator < 4)
31 int segments, 31 int segments,
32 int divisions, 32 int divisions,
33 const QMatrix4x4& matrix) : 33 const QMatrix4x4& matrix) :
34 LDMatrixObject {matrix}, 34 LDMatrixObject {matrix},
35 m_type {type}, 35 m_type {type},
36 m_segments {segments}, 36 m_section {segments, divisions} {}
37 m_divisions {divisions} {}
38 37
39 LDObjectType LDCircularPrimitive::type() const 38 LDObjectType LDCircularPrimitive::type() const
40 { 39 {
41 return SubclassType; 40 return SubclassType;
42 } 41 }
46 return LDSubfileReference(buildFilename(), transformationMatrix()).asText(); 45 return LDSubfileReference(buildFilename(), transformationMatrix()).asText();
47 } 46 }
48 47
49 void LDCircularPrimitive::getVertices(DocumentManager* /* context */, QSet<Vertex>& vertices) const 48 void LDCircularPrimitive::getVertices(DocumentManager* /* context */, QSet<Vertex>& vertices) const
50 { 49 {
51 int endSegment = (m_segments == m_divisions) ? m_segments : m_segments + 1; 50 int endSegment = (segments() == divisions()) ? segments() : segments() + 1;
52 51
53 for (int i = 0; i < endSegment; i += 1) 52 for (int i = 0; i < endSegment; i += 1)
54 { 53 {
55 QPointF point2d = pointOnLDrawCircumference(i, m_divisions); 54 QPointF point2d = pointOnLDrawCircumference(i, divisions());
56 55
57 for (double y_value : {0.0, 1.0}) 56 for (double y_value : {0.0, 1.0})
58 { 57 {
59 Vertex vertex {point2d.x(), y_value, point2d.y()}; 58 Vertex vertex {point2d.x(), y_value, point2d.y()};
60 vertex.transform(transformationMatrix()); 59 vertex.transform(transformationMatrix());
137 136
138 void LDCircularPrimitive::buildPrimitiveBody(Model& model, bool deep) const 137 void LDCircularPrimitive::buildPrimitiveBody(Model& model, bool deep) const
139 { 138 {
140 PrimitiveModel primitive; 139 PrimitiveModel primitive;
141 primitive.type = m_type; 140 primitive.type = m_type;
142 primitive.segments = m_segments; 141 primitive.segments = segments();
143 primitive.divisions = m_divisions; 142 primitive.divisions = divisions();
144 primitive.ringNumber = 0; 143 primitive.ringNumber = 0;
145 primitive.generateBody(model, deep); 144 primitive.generateBody(model, deep);
146 } 145 }
147 146
148 QString LDCircularPrimitive::stem() const 147 QString LDCircularPrimitive::stem() const
180 179
181 QString LDCircularPrimitive::objectListText() const 180 QString LDCircularPrimitive::objectListText() const
182 { 181 {
183 QString prefix; 182 QString prefix;
184 183
185 if (m_divisions == HighResolution) 184 if (divisions() == HighResolution)
186 prefix = "Hi-Res"; 185 prefix = "Hi-Res";
187 else if (m_divisions == LowResolution) 186 else if (divisions() == LowResolution)
188 prefix = "Lo-Res"; 187 prefix = "Lo-Res";
189 else if (m_divisions != MediumResolution) 188 else if (divisions() != MediumResolution)
190 prefix = format("%1-resolution", m_divisions); 189 prefix = format("%1-resolution", divisions());
191 190
192 QString result = format( 191 QString result = format(
193 "%1 %2 %3 %4, (", 192 "%1 %2 %3 %4, (",
194 prefix, 193 prefix,
195 PrimitiveModel::typeName(m_type), 194 PrimitiveModel::typeName(m_type),
196 m_segments / m_divisions, 195 double(segments()) / double(divisions()),
197 position().toString(true) 196 position().toString(true)
198 ).simplified(); 197 ).simplified();
199 198
200 for (int i = 0; i < 3; ++i) 199 for (int i = 0; i < 3; ++i)
201 for (int j = 0; j < 3; ++j) 200 for (int j = 0; j < 3; ++j)
215 changeProperty(&m_type, newType); 214 changeProperty(&m_type, newType);
216 } 215 }
217 216
218 int LDCircularPrimitive::segments() const 217 int LDCircularPrimitive::segments() const
219 { 218 {
220 return m_segments; 219 return m_section.segments;
221 } 220 }
222 221
223 void LDCircularPrimitive::setSegments(int newSegments) 222 void LDCircularPrimitive::setSegments(int newSegments)
224 { 223 {
225 changeProperty(&m_segments, newSegments); 224 changeProperty(&m_section.segments, newSegments);
226 } 225 }
227 226
228 int LDCircularPrimitive::divisions() const 227 int LDCircularPrimitive::divisions() const
229 { 228 {
230 return m_divisions; 229 return m_section.divisions;
231 } 230 }
232 231
233 void LDCircularPrimitive::setDivisions(int newDivisions) 232 void LDCircularPrimitive::setDivisions(int newDivisions)
234 { 233 {
235 changeProperty(&m_divisions, newDivisions); 234 changeProperty(&m_section.divisions, newDivisions);
235 }
236
237 const CircularSection&LDCircularPrimitive::section() const
238 {
239 return m_section;
240 }
241
242 void LDCircularPrimitive::setSection(const CircularSection& newSection)
243 {
244 changeProperty(&m_section, newSection);
236 } 245 }
237 246
238 int LDCircularPrimitive::triangleCount(DocumentManager*) const 247 int LDCircularPrimitive::triangleCount(DocumentManager*) const
239 { 248 {
240 switch (m_type) 249 switch (m_type)
242 case PrimitiveModel::Ring: 251 case PrimitiveModel::Ring:
243 case PrimitiveModel::Cone: 252 case PrimitiveModel::Cone:
244 throw std::logic_error("Bad primitive type to LDCircularPrimitive"); 253 throw std::logic_error("Bad primitive type to LDCircularPrimitive");
245 254
246 case PrimitiveModel::Cylinder: 255 case PrimitiveModel::Cylinder:
256 case PrimitiveModel::CylinderOpen:
257 return 2 * segments();
258
247 case PrimitiveModel::CylinderClosed: 259 case PrimitiveModel::CylinderClosed:
248 case PrimitiveModel::CylinderOpen: 260 return 3 * segments();
249 return 2 * m_segments;
250 261
251 case PrimitiveModel::Disc: 262 case PrimitiveModel::Disc:
252 case PrimitiveModel::DiscNegative: 263 case PrimitiveModel::DiscNegative:
253 return m_segments; 264 return segments();
254 265
255 case PrimitiveModel::Circle: 266 case PrimitiveModel::Circle:
256 return 0; 267 return 0;
257 268
258 case PrimitiveModel::Chord: 269 case PrimitiveModel::Chord:
259 return qBound(0, m_segments - 1, m_divisions - 2); 270 return qBound(0, segments() - 1, divisions() - 2);
260 } 271 }
261 272
262 return 0; 273 return 0;
263 } 274 }
264 275
296 } 307 }
297 308
298 void LDCircularPrimitive::serialize(class Serializer& serializer) 309 void LDCircularPrimitive::serialize(class Serializer& serializer)
299 { 310 {
300 LDMatrixObject::serialize(serializer); 311 LDMatrixObject::serialize(serializer);
301 serializer << m_segments << m_divisions << m_type; 312 serializer << m_section << m_type;
302 } 313 }

mercurial