src/maths.h

Tue, 28 Sep 2021 23:07:23 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 28 Sep 2021 23:07:23 +0300
changeset 145
4dea24d3eda0
parent 122
b54b350dff5d
permissions
-rw-r--r--

Use QSaveFile to save the file more safely

/*
 *  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/>.
 */

#pragma once
#include <cmath>
#include "utility.h"

namespace math
{
	using std::abs;
	using std::sqrt;
	using std::sin;
	using std::cos;
	using std::tan;
	using std::atan;
	using std::atan2;
	using std::acos;
	using std::asin;
	using std::exp;
	using std::log;
	using std::log10;
	using std::hypot;
	using std::floor;
	using std::ceil;
	using std::trunc;
	using std::round;
	template<typename T, typename... Rest>
	inline auto hypot(T&& x, Rest&&... rest)
	{
		return math::hypot(x, math::hypot(rest...));
	}
	template<typename T, typename... Rest>
	const T& max(const T& x, const T& y)
	{
		if (x > y)
			return x;
		else
			return y;
	}
	template<typename T, typename... Rest>
	const T& max(const T& x, const T& y, Rest&&... rest)
	{
		return math::max(x, math::max(y, rest...));
	}
	template<typename T, typename... Rest>
	const T& min(const T& x, const T& y)
	{
		if (x < y)
			return x;
		else
			return y;
	}
	template<typename T, typename... Rest>
	const T& min(const T& x, const T& y, Rest&&... rest)
	{
		return math::min(x, math::min(y, rest...));
	}
	constexpr double infinity = std::numeric_limits<double>::infinity();
	constexpr long double pi = M_PIl;
	// Returns the minimum value of a container
	template<typename T>
	inline auto nmin(T&& values)
	{
		auto it = std::begin(values);
		auto result_p = it;
		for (++it; it != std::end(values); ++it)
		{
			if (*it < *result_p)
				result_p = it;
		}
		return *result_p;
	}
	// Returns the maximum value of a container
	template<typename T>
	inline auto nmax(T&& values)
	{
		auto it = std::begin(values);
		auto result_p = it;
		for (++it; it != std::end(values); ++it)
		{
			if (*it > *result_p)
				result_p = it;
		}
		return *result_p;
	}
	/*
	 * Returns the empty sum. (recursion base)
	 */
	template<typename T>
	constexpr T sum()
	{
		return {};
	}

	/*
	 * Returns the sum of n arguments.
	 */
	template<typename T, typename... Rest>
	constexpr auto sum(const T& arg, Rest&&... rest)
	{
		return arg + sum<T>(rest...);
	}

	std::optional<glm::vec3> linePlaneIntersection();
}

template<typename T, glm::qualifier Q>
inline unsigned int qHash(const glm::vec<3, T, Q>& key)
{
	return qHash(key.x) ^ utility::rotl10(qHash(key.y)) ^ utility::rotl20(qHash(key.z));
}

mercurial