src/vertex.cpp

Sat, 14 Dec 2019 22:36:06 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 14 Dec 2019 22:36:06 +0200
changeset 19
ed9685f44ab3
parent 18
918b6c0f8b5b
child 20
cef43609a374
permissions
-rw-r--r--

added missing files

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright(C) 2013 - 2018 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 "main.h"
#include "vertex.h"

/*
void Vertex::transform(const Matrix& matrix, const Vertex& pos)
{
	double x2 = (matrix(0, 0) * x) + (matrix(0, 1) * y) + (matrix(0, 2) * z) + pos.x;
	double y2 = (matrix(1, 0) * x) + (matrix(1, 1) * y) + (matrix(1, 2) * z) + pos.y;
	double z2 = (matrix(2, 0) * x) + (matrix(2, 1) * y) + (matrix(2, 2) * z) + pos.z;
	this->x = x2;
	this->y = y2;
	this->z = z2;
}
*/

Point3D::CoordinateType& Point3D::get(Axis axis)
{
	switch (axis)
	{
	case X:
		return this->x;
	case Y:
		return this->y;
	case Z:
		return this->z;
	default:
		throw std::runtime_error("Non-axis given to Vertex::operator[]");
	}
}

Point3D::CoordinateType Point3D::get(Axis axis) const
{
	switch (axis)
	{
	case X:
		return this->x;
	case Y:
		return this->y;
	case Z:
		return this->z;
	default:
		return 0;
	}
}

void Point3D::assign(Axis axis, CoordinateType value)
{
	this->get(axis) = value;
}

Point3D VertexFromVector(const QVector3D& vector)
{
	return {vector.x(), vector.y(), vector.z()};
}

Point3D operator*(const Point3D& point, Point3D::CoordinateType scalar)
{
	return {point.x * scalar, point.y * scalar, point.z * scalar};
}

Point3D& operator+=(Point3D& point, const QVector3D& other)
{
	point.x += other.x();
	point.y += other.y();
	point.z += other.z();
	return point;
}

Point3D operator+(Point3D point, const QVector3D& other)
{
	point += other;
	return point;
}


QVector3D vertexToVector(const Point3D& vertex)
{
	return {
		static_cast<float>(vertex.x),
		static_cast<float>(vertex.y),
		static_cast<float>(vertex.z)
	};
}

Point3D operator-(Point3D point, const QVector3D& vector)
{
	point -= vector;
	return point;
}

Point3D& operator-=(Point3D& point, const QVector3D& vector)
{
	point.x -= vector.x();
	point.y -= vector.y();
	point.z -= vector.z();
	return point;
}

QVector3D operator-(const Point3D& point, const Point3D& other)
{
	return {
		static_cast<float>(point.x - other.x),
		static_cast<float>(point.y - other.y),
		static_cast<float>(point.z - other.z)
	};
}

Point3D& operator*=(Point3D& point, Point3D::CoordinateType scalar)
{
	point.x *= scalar;
	point.y *= scalar;
	point.z *= scalar;
	return point;
}

bool operator==(const Point3D& point, const Point3D& other)
{
	return point.x == other.x and point.y == other.y and point.z == other.z;
}

bool operator!=(const Point3D& point, const Point3D& other)
{
	return not (point == other);
}

bool operator<(const Point3D& point, const Point3D& other)
{
	if (not qFuzzyCompare(point.x, other.x))
		return point.x < other.x;
	else if (not qFuzzyCompare(point.y, other.y))
		return point.y < other.y;
	else
		return point.z < other.z;
}

/*
 * Transforms this vertex with a tranformation matrix and returns the result.
 */
Point3D math::transform(const Point3D point, const GLRotationMatrix& matrix)
{
	return {
		matrix(0, 0) * point.x
			+ matrix(0, 1) * point.y
			+ matrix(0, 2) * point.z,
		matrix(1, 0) * point.x
			+ matrix(1, 1) * point.y
			+ matrix(1, 2) * point.z,
		matrix(2, 0) * point.x
			+ matrix(2, 1) * point.y
			+ matrix(2, 2) * point.z,
	};
}

/*
 * Returns the distance from one vertex to another.
 */
qreal math::distance(const Point3D& one, const Point3D& other)
{
	return (one - other).length();
}

/*
 * Returns a vertex with all coordinates inverted.
 */
Point3D operator-(const Point3D& vertex)
{
	return {-vertex.x, -vertex.y, -vertex.z};
}

/*
 * Inserts this vertex into a data stream. This is needed for vertices to be
 * stored in QSettings.
 */
QDataStream& operator<<(QDataStream& out, const Point3D& vertex)
{
	return out << vertex.x << vertex.y << vertex.z;
}

/*
 * Takes a vertex from a data stream.
 */
QDataStream& operator>>(QDataStream& in, Point3D& vertex)
{
	return in >> vertex.x >> vertex.y >> vertex.z;
}

unsigned int qHash(const Point3D& key)
{
	return qHash(key.x) ^ utility::rotl10(qHash(key.y)) ^ utility::rotl20(qHash(key.z));
}

QString vertexToStringParens(const Point3D& vertex)
{
	return utility::format("(%1, %2, %3)", vertex.x, vertex.y, vertex.z);
}

mercurial