src/ldrawalgorithm.cpp

Wed, 09 Mar 2022 12:42:45 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 09 Mar 2022 12:42:45 +0200
changeset 172
50f055543ff6
parent 154
47cb50cfa9ad
child 183
97b591813c8b
permissions
-rw-r--r--

Render vertices as spheres

#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<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