src/linetypes/circularprimitive.cpp

Wed, 25 May 2022 18:33:38 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 25 May 2022 18:33:38 +0300
changeset 198
eb9d900dc79a
parent 196
6bcb284679d4
permissions
-rw-r--r--

fix up things and remove unnecessary code

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

mercurial