src/ldrawalgorithm.cpp

Wed, 25 May 2022 17:42:02 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 25 May 2022 17:42:02 +0300
changeset 193
b4beff48bb7a
parent 183
97b591813c8b
child 200
ca23936b455b
permissions
-rw-r--r--

Simplify PolygonCache

#include "ldrawalgorithm.h"
#include "linetypes/quadrilateral.h"
#include "linetypes/triangle.h"

void ldraw::invert(ModelEditor& editor, ldraw::id_t id, GetPolygonsContext* context)
{
	editor.modifyObject(id, [context](ldraw::Object* object){
		object->invert(context);
	});
}

static std::array<geom::Triangle, 2> splitTriangles(ldraw::Diagonal diagonal, const std::array<glm::vec3, 4>& points)
{
	std::array<geom::Triangle, 2> 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::pair<ldraw::triangleid_t, ldraw::triangleid_t>>
{
	std::optional<std::pair<ldraw::triangleid_t, ldraw::triangleid_t>> result;
	const auto resolved = editor.model().get2(quadrilateral_id);
	if (resolved.object != nullptr)
	{
		const ldraw::Color color = resolved.object->colorIndex;
		const std::array<geom::Triangle, 2> split = splitTriangles(splitType, resolved.object->points);
		const int position = resolved.index.row();
		editor.remove(position);
		result = std::make_pair(
			editor.insert<ldraw::Triangle>(position, split[0].points, color),
			editor.insert<ldraw::Triangle>(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<ldraw::MetaCommand>(ldrawOrgLine))
		{
			const QString& body = ldrawOrgLine->getProperty<ldraw::Property::Text>();
			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<ldraw::Property::Text>(ldrawOrgLine->id, tokens.join(" "));
			}
		}
	}
}

mercurial