src/basics.h

Fri, 17 Sep 2021 22:43:22 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 17 Sep 2021 22:43:22 +0300
changeset 131
e2080ac44039
parent 96
165777a20dc7
child 148
e1ced2523cad
permissions
-rw-r--r--

add missing change to document.cpp

/*
 *  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 <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <optional>
#include <type_traits>
#include <QMatrix4x4>
#include <QObject>
#include <QPointF>
#include <QSet>
#include <QString>
#include <QStringList>
#include <QVariant>
#include <QVector>
#include <QVector3D>
#include <glm/glm.hpp>

using GLRotationMatrix = QMatrix4x4;

enum Axis
{
	X = 0,
	Y = 1,
	Z = 2
};

enum Result
{
	Success = 0,
	PartialSuccess,
	Failure
};

constexpr bool failed(Result r)
{
	return r == Failure;
}

enum Winding
{
	NoWinding,
	Anticlockwise,
	Clockwise,
};

/*
 * Special operator definition that implements the XOR operator for windings.
 * However, if either winding is NoWinding, then this function returns NoWinding.
 */
inline Winding operator^(Winding one, Winding other)
{
	if (one == NoWinding or other == NoWinding)
		return NoWinding;
	else
		return static_cast<Winding>(static_cast<int>(one) ^ static_cast<int>(other));
}

inline Winding& operator^=(Winding& one, Winding other)
{
	one = one ^ other;
	return one;
}

template<typename T, int N>
constexpr int countof(T const (&)[N])
{
	return N;
}

/**
 * @brief casts @c x to a suitable unsigned integer
 */
template<typename T>
constexpr auto unsigned_cast(T x)
	-> std::enable_if_t<std::is_integral_v<T>, std::make_unsigned_t<T>>
{
	return static_cast<std::make_unsigned_t<T>>(x);
}

/**
 * @brief casts @c x to a suitable signed integer
 */
template<typename T>
constexpr auto signed_cast(T x)
	-> std::enable_if_t<std::is_integral_v<T>, std::make_signed_t<T>>
{
	return static_cast<std::make_signed_t<T>>(x);
}


/**
* @brief casts floating point values to float, converting non-floating point values causes an error
* @param[in] x floating point value to cast
* @returns float
*/
template<typename T>
auto toFloat(T x) -> std::enable_if_t<std::is_floating_point_v<T>, float>
{
	return static_cast<float>(x);
}

/**
* @brief casts floating point values to double, converting non-floating point values causes an error
* @param[in] x floating point value to cast
* @returns double
*/
template<typename T>
auto toDouble(T x) -> std::enable_if_t<std::is_floating_point_v<T>, double>
{
	return static_cast<double>(x);
}

/**
* @brief casts floating point values to qreal, converting non-floating point values causes an error
* @param[in] x floating point value to cast
* @returns qreal
*/
template<typename T>
auto toQreal(T x) -> std::enable_if_t<std::is_floating_point_v<T>, qreal>
{
	return static_cast<qreal>(x);
}

template<int N, typename T, glm::qualifier Q>
inline QPoint toQPoint(const glm::vec<N, T, Q>& vec)
{
	return {static_cast<int>(vec.x), static_cast<int>(vec.y)};
}

template<int N, typename T, glm::qualifier Q>
inline QPointF toQPointF(const glm::vec<N, T, Q>& vec)
{
	return {toDouble(vec.x), toDouble(vec.y)};
}

inline glm::vec2 toVec2(const QPoint& point)
{
	return {point.x(), point.y()};
}

inline glm::vec2 toVec2(const QPointF& point)
{
	return {point.x(), point.y()};
}

/*
 * coalesce(arg1, arg2, ..., argn)
 * Returns the first of the given arguments that evaluates to true.
 */
template<typename T>
T coalesce(T&& arg)
{
	// recursion base: 1 argument
	return arg;
}

template<typename T, typename... Rest>
std::common_type_t<T, Rest...> coalesce(T&& arg, Rest&&... rest)
{
	// general case: n arguments
	return arg ? arg : coalesce(rest...);
}

template<typename T = double>
constexpr std::enable_if_t<std::is_floating_point_v<T>, T> PI = static_cast<T>(M_PIl);

Q_DECLARE_METATYPE(glm::vec3)
Q_DECLARE_METATYPE(glm::mat4)

mercurial