diff -r 2f79053c2e9a -r 47cb50cfa9ad src/ldrawalgorithm.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ldrawalgorithm.cpp Fri Mar 04 23:00:06 2022 +0200 @@ -0,0 +1,74 @@ +#include "ldrawalgorithm.h" +#include "linetypes/quadrilateral.h" +#include "linetypes/triangle.h" + +void ldraw::invert(ModelEditor& editor, ldraw::id_t id) +{ + editor.modifyObject(id, [](ldraw::Object* object){ + object->invert(); + }); +} + +static std::array splitTriangles(ldraw::Diagonal diagonal, const std::array& points) +{ + std::array result; + switch (diagonal) + { + case ldraw::Diagonal::Diagonal_13: + result = {geom::Triangle{points[0], points[1], points[2]}, {points[0], points[2], points[3]}}; + break; + case ldraw::Diagonal::Diagonal_24: + result = {geom::Triangle{points[0], points[1], points[3]}, {points[1], points[2], points[3]}}; + break; + } + return result; +} + +auto ldraw::splitQuadrilateral( + ModelEditor& editor, + ldraw::quadrilateralid_t quadrilateral_id, + ldraw::Diagonal splitType +) -> std::optional> +{ + std::optional> result; + const auto resolved = editor.model().get2(quadrilateral_id); + if (resolved.object != nullptr) + { + const ldraw::Color color = resolved.object->colorIndex; + const std::array split = splitTriangles(splitType, resolved.object->points); + const int position = resolved.index.row(); + editor.remove(position); + result = std::make_pair( + editor.insert(position, split[0].points, color), + editor.insert(position, split[1].points, color)); + } + return result; +} + +/** + * @brief Modifies the !LDRAW_ORG line so that it becomes unofficial + */ +void ldraw::makeUnofficial(ModelEditor& editor) +{ + if (editor.model().size() >= 4) + { + const ldraw::Object* ldrawOrgLine = editor.model()[3]; + if (isA(ldrawOrgLine)) + { + const QString& body = ldrawOrgLine->getProperty(); + if (body.startsWith("!LDRAW_ORG ") and not body.startsWith("!LDRAW_ORG Unofficial_")) + { + // Add Unofficial_ to part type + QStringList tokens = body.split(" "); + tokens[1] = "Unofficial_" + tokens[1]; + // Remove the UPDATE tag if it's there + if (tokens.size() >= 4 && tokens[2] == "UPDATE") + { + tokens.removeAt(3); + tokens.removeAt(2); + } + editor.setObjectProperty(ldrawOrgLine->id, tokens.join(" ")); + } + } + } +} \ No newline at end of file