src/ldrawalgorithm.cpp

Wed, 15 Jun 2022 12:32:50 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Wed, 15 Jun 2022 12:32:50 +0300
changeset 225
551c136b459e
parent 200
ca23936b455b
child 248
29986dfd1750
permissions
-rw-r--r--

Fix crash involving polygon being too empty

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
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
3 std::pair<Triangle, Triangle> splitTriangles(
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
4 const Quadrilateral& q,
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
5 ldraw::Diagonal diagonal)
154
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 {
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
7 std::pair<Triangle, Triangle> result;
154
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 switch (diagonal)
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 case ldraw::Diagonal::Diagonal_13:
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
11 result = {Triangle{q.p1, q.p2, q.p3}, {q.p1, q.p3, q.p4}};
154
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 break;
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 case ldraw::Diagonal::Diagonal_24:
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
14 result = {Triangle{q.p1, q.p2, q.p3}, {q.p2, q.p3, q.p4}};
154
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 break;
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 return result;
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 }
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 /**
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 * @brief Modifies the !LDRAW_ORG line so that it becomes unofficial
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 */
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
23 /*
154
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 void ldraw::makeUnofficial(ModelEditor& editor)
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 if (editor.model().size() >= 4)
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 {
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 const ldraw::Object* ldrawOrgLine = editor.model()[3];
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 if (isA<ldraw::MetaCommand>(ldrawOrgLine))
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 {
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 const QString& body = ldrawOrgLine->getProperty<ldraw::Property::Text>();
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 if (body.startsWith("!LDRAW_ORG ") and not body.startsWith("!LDRAW_ORG Unofficial_"))
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 {
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 // Add Unofficial_ to part type
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 QStringList tokens = body.split(" ");
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 tokens[1] = "Unofficial_" + tokens[1];
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 // Remove the UPDATE tag if it's there
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 if (tokens.size() >= 4 && tokens[2] == "UPDATE")
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 {
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 tokens.removeAt(3);
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 tokens.removeAt(2);
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 }
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 editor.setObjectProperty<ldraw::Property::Text>(ldrawOrgLine->id, tokens.join(" "));
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 }
47cb50cfa9ad add missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 }
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
47 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
48 */
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
49
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
50 ModelElement inverted(const ModelElement& element)
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
51 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
52 return std::visit(overloaded{
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
53 [](Colored<SubfileReference> ref) -> ModelElement {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
54 ref.inverted = not ref.inverted;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
55 return ref;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
56 },
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
57 [](Colored<Triangle> triangle) -> ModelElement {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
58 std::swap(triangle.p1, triangle.p2);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
59 return triangle;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
60 },
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
61 [](Colored<Quadrilateral> quad) -> ModelElement {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
62 std::swap(quad.p2, quad.p4);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
63 return quad;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
64 },
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
65 [](const ModelElement& x) { return x; }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
66 }, element);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
67 }

mercurial