src/miscallenous.cpp

Wed, 08 Feb 2017 17:07:19 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 08 Feb 2017 17:07:19 +0200
changeset 1096
7cc929d8fc4d
parent 1072
9ce9496427f2
child 1116
3b1d2cc6603e
permissions
-rw-r--r--

Simplified GLRenderer::convert3dTo2d. The multiplication with the modelview matrix is not necessary, it's identity all the time... with that, I think the function makes much more sense now.

/*
 *  LDForge: LDraw parts authoring CAD
 *  Copyright (C) 2013 - 2017 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 <math.h>
#include <locale.h>
#include <QColor>
#include "main.h"
#include "miscallenous.h"
#include "mainwindow.h"
#include "dialogs.h"
#include "ldDocument.h"


int gcd (int a, int b)
{
	while (b != 0)
	{
		int temp = a;
		a = b;
		b = temp % b;
	}

	return a;
}


void simplify (int& numerator, int& denominator)
{
	int factor = gcd(numerator, denominator);
	numerator /= factor;
	denominator /= factor;
}


QString joinStrings (QList<StringFormatArg> vals, QString delim)
{
	QStringList list;

	for (const StringFormatArg& arg : vals)
		list << arg.text();

	return list.join (delim);
}


void roundToDecimals(double& value, int decimals)
{
	if (decimals == 0)
	{
		value = round(value);
	}
	else if (decimals > 0)
	{
		qreal coefficient = pow(10, decimals);
		value = round(value * coefficient) / coefficient;
	}
}


void applyToMatrix (Matrix& a, ApplyToMatrixFunction func)
{
	for (int i = 0; i < 9; ++i)
		func(i, a.value(i));
}

void applyToMatrix (const Matrix& a, ApplyToMatrixConstFunction func)
{
	for (int i = 0; i < 9; ++i)
		func(i, a.value(i));
}


QString formatFileSize (qint64 size)
{
	static const QString suffixes[] = {" bytes", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
	int magnitude = magnitude = (size > 0) ? floor(log(size) / log(1000) + 1e-10) : 0;
	magnitude = qBound(0, magnitude, countof(suffixes) - 1);
	return QString::number(size / pow(1000, magnitude)) + suffixes[magnitude];
}

mercurial