src/matrix.cpp

Sun, 26 Jan 2020 01:06:27 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 26 Jan 2020 01:06:27 +0200
changeset 32
767592024ec5
parent 26
3a9e761e4faa
permissions
-rw-r--r--

fix default angle

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

Matrix4x4 combine(const Matrix3x3& topLeft, const Point3D& translation)
{
	return {{
		{topLeft(0, 0), topLeft(0, 1), topLeft(0, 2), translation.x},
		{topLeft(1, 0), topLeft(1, 1), topLeft(1, 2), translation.y},
		{topLeft(2, 0), topLeft(2, 1), topLeft(2, 2), translation.z},
		{0, 0, 0, 1}
	}};
}

/*
 * Computes the determinant of a 3×3 matrix with each variable passed in row-major order.
 */
qreal math::det(qreal a, qreal b, qreal c, qreal d, qreal e, qreal f, qreal g, qreal h, qreal i)
{
	return a*e*i + b*f*g + c*d*h - a*f*h - b*d*i - c*e*g;
}

/*
 * Computes the determinant of a 2×2 matrix.
 */
qreal math::det(const Matrix<2, 2>& matrix)
{
	return matrix(0, 0) * matrix(1, 1) - matrix(0, 1) * matrix(1, 0);
}

/*
 * Computes the determinant of a 3×3 matrix.
 */
qreal math::det(const Matrix3x3& matrix)
{
	return math::sum(
		+matrix(0, 0) * matrix(1, 1) * matrix(2, 2),
		-matrix(0, 0) * matrix(1, 2) * matrix(2, 1),
		-matrix(0, 1) * matrix(1, 0) * matrix(2, 2),
		+matrix(0, 1) * matrix(1, 2) * matrix(2, 0),
		+matrix(0, 2) * matrix(1, 0) * matrix(2, 1),
		-matrix(0, 2) * matrix(1, 1) * matrix(2, 0));
}

/*
 * Computes the determinant of a 4×4 matrix.
 */
qreal math::det(const Matrix4x4& matrix)
{
	qreal sum = 0;

	for (int column : {0, 1, 2, 3})
	{
		int column_1 = (column >= 1) ? 0 : 1;
		int column_2 = (column >= 2) ? 1 : 2;
		int column_3 = (column >= 3) ? 2 : 3;
		sum += ((column % 1) ? -1 : 1) * math::det(
			matrix(1, column_1), matrix(1, column_2), matrix(1, column_3),
			matrix(2, column_1), matrix(2, column_2), matrix(2, column_3),
			matrix(3, column_1), matrix(3, column_2), matrix(3, column_3));
	}

	return sum;
}

mercurial