Wed, 25 May 2022 17:42:02 +0300
Simplify PolygonCache
186 | 1 | #include "circularprimitive.h" |
2 | ||
3 | ldraw::CircularPrimitive::CircularPrimitive(CircularPrimitiveType type, int segments, int divisions) : | |
4 | type{type}, | |
5 | segments{segments}, | |
6 | divisions{divisions} | |
7 | { | |
8 | } | |
9 | ||
10 | QVariant ldraw::CircularPrimitive::getProperty(Property property) const | |
11 | { | |
12 | switch (property) | |
13 | { | |
14 | case Property::Segments: | |
15 | return this->segments; | |
16 | case Property::Divisions: | |
17 | return this->divisions; | |
18 | case Property::CircularPrimitiveType: | |
19 | return this->type; | |
20 | default: | |
21 | return BaseClass::getProperty(property); | |
22 | } | |
23 | } | |
24 | ||
25 | QString ldraw::CircularPrimitive::textRepresentation() const | |
26 | { | |
27 | return circularPrimitiveTypeName(this->type) + " " + QString::number(this->fraction()); | |
28 | } | |
29 | ||
30 | QString ldraw::CircularPrimitive::circularPrimitiveTypeName(CircularPrimitiveType type) | |
31 | { | |
32 | switch (type) | |
33 | { | |
34 | case Circle: | |
35 | return QObject::tr("Circle"); | |
36 | case Disc: | |
37 | return QObject::tr("Disc"); | |
38 | } | |
39 | } | |
40 | ||
41 | ldraw::Object::Type ldraw::CircularPrimitive::typeIdentifier() const | |
42 | { | |
43 | return ldraw::Object::Type::CircularPrimitive; | |
44 | } | |
45 | ||
46 | QDataStream &ldraw::CircularPrimitive::serialize(QDataStream &stream) const | |
47 | { | |
48 | return BaseClass::serialize(stream) << this->type << this->segments << this->divisions; | |
49 | } | |
50 | ||
51 | QDataStream &ldraw::CircularPrimitive::deserialize(QDataStream &stream) | |
52 | { | |
53 | return BaseClass::deserialize(stream) >> this->type >> this->segments >> this->divisions; | |
54 | } | |
55 | ||
56 | QString ldraw::CircularPrimitive::toLDrawCode() const | |
57 | { | |
58 | return utility::format( | |
59 | "0 !LDFORGE CIRCULAR_PRIMITIVE %1 %2 %3 %4", | |
60 | static_cast<int>(this->type), | |
61 | this->segments, | |
62 | this->divisions, | |
63 | this->transformToBareString()); | |
64 | } | |
65 | ||
66 | QString ldraw::CircularPrimitive::iconName() const | |
67 | { | |
68 | return ":/icons/linetype-circularprimitive.png"; | |
69 | } | |
70 | ||
71 | QString ldraw::CircularPrimitive::typeName() const | |
72 | { | |
73 | return QObject::tr("circular primitive"); | |
74 | } | |
75 | ||
76 | void ldraw::CircularPrimitive::getPolygons(std::vector<gl::Polygon> &polygons, GetPolygonsContext *) const | |
77 | { | |
78 | for (int i = 0; i < this->segments; i += 1) | |
79 | { | |
80 | const float ang_1 = (2 * math::pi * i) / this->divisions; | |
81 | const float ang_2 = (2 * math::pi * (i + 1)) / this->divisions; | |
82 | const glm::vec3 p_1 = {std::sin(ang_1), 0, std::cos(ang_1)}; | |
83 | const glm::vec3 p_2 = {std::sin(ang_2), 0, std::cos(ang_2)}; | |
84 | switch (this->type) | |
85 | { | |
86 | case Circle: | |
87 | polygons.push_back(gl::edgeLine( | |
88 | p_1, | |
89 | p_2, | |
90 | this->colorIndex, | |
91 | this->id)); | |
92 | break; | |
93 | case Disc: | |
94 | polygons.push_back(gl::triangle( | |
95 | {0, 0, 0}, | |
96 | p_1, | |
97 | p_2, | |
98 | this->colorIndex, | |
99 | this->id)); | |
100 | break; | |
101 | } | |
102 | } | |
103 | } | |
104 | ||
105 | float ldraw::CircularPrimitive::fraction() const | |
106 | { | |
107 | return static_cast<float>(this->segments) / static_cast<float>(this->divisions); | |
108 | } |