src/linetypes/subfilereference.cpp

Mon, 24 Aug 2020 22:55:37 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Mon, 24 Aug 2020 22:55:37 +0300
changeset 91
9eb5e06f34c3
parent 35
98906a94732f
child 81
62373840e33a
permissions
-rw-r--r--

add comments

#include "subfilereference.h"
#include "documentmanager.h"
#include "invert.h"

ldraw::SubfileReference::SubfileReference(const glm::mat4& transformation,
	const QString& referenceName,
	const Color color) :
	ColoredObject{color},
	transformation{transformation},
	referenceName{referenceName}
{
}

QVariant ldraw::SubfileReference::getProperty(Property property) const
{
	switch (property)
	{
	case Property::Transformation:
		return QVariant::fromValue(this->transformation);
	case Property::ReferenceName:
		return this->referenceName;
	default:
		return ColoredObject::getProperty(property);
	}
}

auto ldraw::SubfileReference::setProperty(
	Property property,
	const QVariant& value)
	-> SetPropertyResult
{
	switch (property)
	{
	case Property::Transformation:
		this->transformation = value.value<glm::mat4>();
		return SetPropertyResult::Success;
	case Property::ReferenceName:
		this->referenceName = value.toString();
		return SetPropertyResult::Success;
	default:
		return ColoredObject::setProperty(property, value);
	}
}

QString ldraw::SubfileReference::textRepresentation() const
{
	return referenceName + " " + utility::vertexToStringParens(this->position());
}

void ldraw::SubfileReference::getPolygons(
	std::vector<gl::Polygon>& polygons,
	GetPolygonsContext* context) const
{
	Model* model = this->resolve(context->documents);
	if (model != nullptr)
	{
		const bool needInverting = glm::determinant(this->transformation) < 0;
		const std::vector<gl::Polygon> modelPolygons = model->getPolygons(context->documents);
		polygons.reserve(polygons.size() + modelPolygons.size());
		for (gl::Polygon polygon : modelPolygons)
		{
			for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1)
			{
				glm::vec4 vertex {polygon.vertices[i], 1};
				vertex = this->transformation * vertex;
				polygon.vertices[i] = vertex;
			}
			if (needInverting != this->isInverted)
			{
				gl::invert(polygon);
			}
			if (polygon.color == ldraw::mainColor)
			{
				polygon.color = this->colorIndex;
			}
			polygon.id = this->id;
			polygons.push_back(polygon);
		}
	}
}

glm::vec3 ldraw::SubfileReference::position() const
{
	return {this->transformation[3][0], this->transformation[3][1], this->transformation[3][2]};
}

void ldraw::SubfileReference::invert()
{
	this->isInverted = not this->isInverted;
}

Model* ldraw::SubfileReference::resolve(DocumentManager* documents) const
{
	return documents->findModelByName(this->referenceName);
}

mercurial