src/linetypes/circularprimitive.cpp

Wed, 25 May 2022 17:47:06 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 25 May 2022 17:47:06 +0300
changeset 194
be056e87c8ca
parent 186
922662adb72a
child 196
6bcb284679d4
permissions
-rw-r--r--

simplify further

#include "circularprimitive.h"

ldraw::CircularPrimitive::CircularPrimitive(CircularPrimitiveType type, int segments, int divisions) :
	type{type},
	segments{segments},
	divisions{divisions}
{
}

QVariant ldraw::CircularPrimitive::getProperty(Property property) const
{
	switch (property)
	{
	case Property::Segments:
		return this->segments;
	case Property::Divisions:
		return this->divisions;
	case Property::CircularPrimitiveType:
		return this->type;
	default:
		return BaseClass::getProperty(property);
	}
}

QString ldraw::CircularPrimitive::textRepresentation() const
{
	return circularPrimitiveTypeName(this->type) + " " + QString::number(this->fraction());
}

QString ldraw::CircularPrimitive::circularPrimitiveTypeName(CircularPrimitiveType type)
{
	switch (type)
	{
	case Circle:
		return QObject::tr("Circle");
	case Disc:
		return QObject::tr("Disc");
	}
}

ldraw::Object::Type ldraw::CircularPrimitive::typeIdentifier() const
{
	return ldraw::Object::Type::CircularPrimitive;
}

QDataStream &ldraw::CircularPrimitive::serialize(QDataStream &stream) const
{
	return BaseClass::serialize(stream) << this->type << this->segments << this->divisions;
}

QDataStream &ldraw::CircularPrimitive::deserialize(QDataStream &stream)
{
	return BaseClass::deserialize(stream) >> this->type >> this->segments >> this->divisions;
}

QString ldraw::CircularPrimitive::toLDrawCode() const
{
	return utility::format(
		"0 !LDFORGE CIRCULAR_PRIMITIVE %1 %2 %3 %4",
		static_cast<int>(this->type),
		this->segments,
		this->divisions,
		this->transformToBareString());
}

QString ldraw::CircularPrimitive::iconName() const
{
	return ":/icons/linetype-circularprimitive.png";
}

QString ldraw::CircularPrimitive::typeName() const
{
	return QObject::tr("circular primitive");
}

void ldraw::CircularPrimitive::getPolygons(std::vector<gl::Polygon> &polygons, GetPolygonsContext *) const
{
	for (int i = 0; i < this->segments; i += 1)
	{
		const float ang_1 = (2 * math::pi * i) / this->divisions;
		const float ang_2 = (2 * math::pi * (i + 1)) / this->divisions;
		const glm::vec3 p_1 = {std::sin(ang_1), 0, std::cos(ang_1)};
		const glm::vec3 p_2 = {std::sin(ang_2), 0, std::cos(ang_2)};
		switch (this->type)
		{
		case Circle:
			polygons.push_back(gl::edgeLine(
				p_1,
				p_2,
				this->colorIndex,
				this->id));
			break;
		case Disc:
			polygons.push_back(gl::triangle(
				{0, 0, 0},
				p_1,
				p_2,
				this->colorIndex,
				this->id));
			break;
		}
	}
}

float ldraw::CircularPrimitive::fraction() const
{
	return static_cast<float>(this->segments) / static_cast<float>(this->divisions);
}

mercurial