src/vertex.cpp

Tue, 07 Jul 2015 21:35:20 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 07 Jul 2015 21:35:20 +0300
changeset 941
f895379d7fab
permissions
-rw-r--r--

Refactoring update.
Removed all asserts.

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2013 - 2015 Teemu Piippo
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "vertex.h"
#include "format.h"

Vertex::Vertex() :
	QVector3D() {}

Vertex::Vertex (const QVector3D& a) :
	QVector3D (a) {}

Vertex::Vertex (qreal xpos, qreal ypos, qreal zpos) :
	QVector3D(xpos, ypos, zpos) {}


void Vertex::transform (const Matrix& matr, const Vertex& pos)
{
	double x2 = (matr[0] * x()) + (matr[1] * y()) + (matr[2] * z()) + pos.x();
	double y2 = (matr[3] * x()) + (matr[4] * y()) + (matr[5] * z()) + pos.y();
	double z2 = (matr[6] * x()) + (matr[7] * y()) + (matr[8] * z()) + pos.z();
	setX (x2);
	setY (y2);
	setZ (z2);
}

void Vertex::apply (ApplyFunction func)
{
	double newX = x(), newY = y(), newZ = z();
	func (X, newX);
	func (Y, newY);
	func (Z, newZ);
	*this = Vertex (newX, newY, newZ);
}

void Vertex::apply (ApplyConstFunction func) const
{
	func (X, x());
	func (Y, y());
	func (Z, z());
}

double Vertex::operator[] (Axis ax) const
{
	switch (ax)
	{
	case X: return x();
	case Y: return y();
	case Z: return z();
	}

	return 0.0;
}

void Vertex::setCoordinate (Axis ax, qreal value)
{
	switch (ax)
	{
	case X: setX (value); break;
	case Y: setY (value); break;
	case Z: setZ (value); break;
	}
}

QString Vertex::toString (bool mangled) const
{
	if (mangled)
		return format ("(%1, %2, %3)", x(), y(), z());

	return format ("%1 %2 %3", x(), y(), z());
}

bool Vertex::operator< (const Vertex& other) const
{
	if (x() != other.x()) return x() < other.x();
	if (y() != other.y()) return y() < other.y();
	if (z() != other.z()) return z() < other.z();
	return false;
}

mercurial