src/ldrawalgorithm.cpp

Sat, 05 Mar 2022 13:18:40 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 05 Mar 2022 13:18:40 +0200
changeset 161
6fe485a84d06
parent 154
47cb50cfa9ad
child 183
97b591813c8b
permissions
-rw-r--r--

Icons update

154
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #include "ldrawalgorithm.h"
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 #include "linetypes/quadrilateral.h"
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 #include "linetypes/triangle.h"
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 void ldraw::invert(ModelEditor& editor, ldraw::id_t id)
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 {
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 editor.modifyObject(id, [](ldraw::Object* object){
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 object->invert();
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 });
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 }
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 static std::array<geom::Triangle, 2> splitTriangles(ldraw::Diagonal diagonal, const std::array<glm::vec3, 4>& points)
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 {
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 std::array<geom::Triangle, 2> result;
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 switch (diagonal)
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 {
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 case ldraw::Diagonal::Diagonal_13:
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 result = {geom::Triangle{points[0], points[1], points[2]}, {points[0], points[2], points[3]}};
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 break;
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 case ldraw::Diagonal::Diagonal_24:
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 result = {geom::Triangle{points[0], points[1], points[3]}, {points[1], points[2], points[3]}};
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 break;
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 }
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 return result;
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 }
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 auto ldraw::splitQuadrilateral(
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 ModelEditor& editor,
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 ldraw::quadrilateralid_t quadrilateral_id,
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 ldraw::Diagonal splitType
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 ) -> std::optional<std::pair<ldraw::triangleid_t, ldraw::triangleid_t>>
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 {
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 std::optional<std::pair<ldraw::triangleid_t, ldraw::triangleid_t>> result;
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 const auto resolved = editor.model().get2(quadrilateral_id);
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 if (resolved.object != nullptr)
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 {
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 const ldraw::Color color = resolved.object->colorIndex;
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 const std::array<geom::Triangle, 2> split = splitTriangles(splitType, resolved.object->points);
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 const int position = resolved.index.row();
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 editor.remove(position);
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 result = std::make_pair(
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 editor.insert<ldraw::Triangle>(position, split[0].points, color),
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 editor.insert<ldraw::Triangle>(position, split[1].points, color));
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 }
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 return result;
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 }
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 /**
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 * @brief Modifies the !LDRAW_ORG line so that it becomes unofficial
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 */
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 void ldraw::makeUnofficial(ModelEditor& editor)
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 {
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 if (editor.model().size() >= 4)
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 {
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 const ldraw::Object* ldrawOrgLine = editor.model()[3];
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 if (isA<ldraw::MetaCommand>(ldrawOrgLine))
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 {
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 const QString& body = ldrawOrgLine->getProperty<ldraw::Property::Text>();
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 if (body.startsWith("!LDRAW_ORG ") and not body.startsWith("!LDRAW_ORG Unofficial_"))
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 {
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 // Add Unofficial_ to part type
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 QStringList tokens = body.split(" ");
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 tokens[1] = "Unofficial_" + tokens[1];
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 // Remove the UPDATE tag if it's there
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 if (tokens.size() >= 4 && tokens[2] == "UPDATE")
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 {
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 tokens.removeAt(3);
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 tokens.removeAt(2);
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 }
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 editor.setObjectProperty<ldraw::Property::Text>(ldrawOrgLine->id, tokens.join(" "));
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 }
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 }
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 }
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 }

mercurial