src/basics.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
parent 931
85080f7a1c20
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 <QObject>
#include <QStringList>
#include <QTextStream>
#include <QFile>
#include "main.h"
#include "basics.h"
#include "miscallenous.h"
#include "ldObject.h"
#include "ldDocument.h"

// =============================================================================
//
Matrix::Matrix (double vals[])
{
	for (int i = 0; i < 9; ++i)
		m_vals[i] = vals[i];
}

// =============================================================================
//
Matrix::Matrix (double fillval)
{
	for (int i = 0; i < 9; ++i)
		m_vals[i] = fillval;
}

// =============================================================================
//
Matrix::Matrix (const std::initializer_list<double>& vals)
{
	if (vals.size() == 9)
		memcpy (&m_vals[0], vals.begin(), sizeof m_vals);
}

// =============================================================================
//
void Matrix::dump() const
{
	for (int i = 0; i < 3; ++i)
	{
		for (int j = 0; j < 3; ++j)
			print ("%1\t", m_vals[i * 3 + j]);

		print ("\n");
	}
}

// =============================================================================
//
QString Matrix::toString() const
{
	QString val;

	for (int i = 0; i < 9; ++i)
	{
		if (i > 0)
			val += ' ';

		val += QString::number (m_vals[i]);
	}

	return val;
}

// =============================================================================
//
void Matrix::zero()
{
	memset (&m_vals[0], 0, sizeof m_vals);
}

// =============================================================================
//
Matrix Matrix::mult (const Matrix& other) const
{
	Matrix val;
	val.zero();

	for (int i = 0; i < 3; ++i)
	for (int j = 0; j < 3; ++j)
	for (int k = 0; k < 3; ++k)
		val[(i * 3) + j] += m_vals[(i * 3) + k] * other[(k * 3) + j];

	return val;
}

// =============================================================================
//
Matrix& Matrix::operator= (const Matrix& other)
{
	memcpy (&m_vals[0], &other.m_vals[0], sizeof m_vals);
	return *this;
}

// =============================================================================
//
double Matrix::getDeterminant() const
{
	return (value (0) * value (4) * value (8)) +
		   (value (1) * value (5) * value (6)) +
		   (value (2) * value (3) * value (7)) -
		   (value (2) * value (4) * value (6)) -
		   (value (1) * value (3) * value (8)) -
		   (value (0) * value (5) * value (7));
}

// =============================================================================
//
bool Matrix::operator== (const Matrix& other) const
{
	for (int i = 0; i < 9; ++i)
	{
		if (value (i) != other[i])
			return false;
	}

	return true;
}

mercurial