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