src/main.h

Wed, 25 May 2022 17:25:24 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 25 May 2022 17:25:24 +0300
changeset 192
e6faeffed1d1
parent 191
d355d4c52d51
child 196
6bcb284679d4
permissions
-rw-r--r--

remove dead code

/*
 *  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 <QString>
#include <QVector>
#include <QSet>
#include <QDebug>
#include <compare>
#include <memory>
#include "basics.h"
#include "utility.h"
#include "maths.h"
#include "geometry.h"
#include "functional.h"

namespace settingGroups
{
	// List of setting groups
	constexpr char mainwindow[] = "mainwindow";
}

namespace ldraw
{
	class Object;

	// Uniquely identifies a model body object
	template<typename T>
	struct Id
	{
		std::int32_t value;
		template<typename A, typename B>
		static constexpr bool is_base_or_base_of = std::disjunction_v<std::is_base_of<A, B>, std::is_base_of<B, A>>;
		template<typename R, typename = std::enable_if_t<is_base_or_base_of<T, R>>>
		constexpr bool operator<(ldraw::Id<R> other) const
		{
			return this->value < other.value;
		}
		friend constexpr unsigned int qHash(ldraw::Id<T> id)
		{
			return qHash(id.value);
		}
		// Allow comparing ids as long as they are related
		template<typename R, typename = std::enable_if_t<is_base_or_base_of<T, R>>>
		friend bool operator==(ldraw::Id<T> one, ldraw::Id<R> other)
		{
			return one.value == other.value;
		}
		// Allow upcasting
		template<typename R, typename = std::enable_if_t<std::is_base_of_v<R, T>>>
		constexpr operator Id<R>() const
		{
			return Id<R>{this->value};
		}
	};

	using id_t = Id<Object>;
	using triangleid_t = Id<class Triangle>;
	using quadrilateralid_t = Id<class Quadrilateral>;
	using edgeid_t = Id<class EdgeLine>;
	using conditionaledgeid_t = Id<class ConditionalEdge>;
	using subfileid_t = Id<class SubfileReference>;
	using commentid_t = Id<class Comment>;
	using metacommandid_t = Id<class MetaCommand>;

	constexpr struct NullId
	{
		template<typename T>
		constexpr operator Id<T>() const
		{
			return Id<T>{0};
		}
		static constexpr decltype(ldraw::id_t::value) value = 0;
	} NULL_ID = {};

	template<typename T>
	inline bool operator==(Id<T> one, decltype(NULL_ID))
	{
		return one.value == 0;
	}

	template<typename T>
	inline bool operator!=(Id<T> one, decltype(NULL_ID))
	{
		return one.value != 0;
	}

	template<typename T>
	inline bool operator<(Id<T> one, decltype(NULL_ID))
	{
		return one.value < 0;
	}
}

constexpr std::size_t operator""_z(const unsigned long long int x)
{
	return static_cast<std::size_t>(x);
}

inline QString operator""_q(const char* string, const unsigned long int length)
{
	Q_UNUSED(length)
	return QString{string};
}

inline QPointF pointToPointF(const QPoint& point)
{
	return {static_cast<qreal>(point.x()), static_cast<qreal>(point.y())};
}

inline QPoint pointFToPoint(const QPointF& point)
{
	return {static_cast<int>(std::round(point.x())), static_cast<int>(std::round(point.y()))};
}

inline QSizeF sizeToSizeF(const QSize& size)
{
	return {static_cast<qreal>(size.width()), static_cast<qreal>(size.height())};
}

/**
 * \brief Hints to the specified vector that a certain amount of new elements are going to be added.
 * \param vector vector to consider
 * \param amount amount of new elements to expect
 */
template<typename T>
void reserveMore(std::vector<T>& vector, std::size_t amount)
{
	vector.reserve(vector.size() + amount);
}

inline QString vectorToString(const glm::vec2& vec)
{
	return "(%1, %2)"_q
		.arg(toDouble(vec.x))
		.arg(toDouble(vec.y));
}

inline QString vectorToString(const glm::vec3& vec)
{
	return "(%1, %2, %3)"_q
		.arg(toDouble(vec.x))
		.arg(toDouble(vec.y))
		.arg(toDouble(vec.z));
}

inline QString vectorToString(const glm::vec4& vec)
{
	return "(%1, %2, %3, %4)"_q
		.arg(toDouble(vec.x))
		.arg(toDouble(vec.y))
		.arg(toDouble(vec.z))
		.arg(toDouble(vec.w));
}

template<typename K, typename V>
struct KeyValuePair
{
	K key;
	V value;
};

template<typename K, typename V, typename IteratorType>
struct MapItemsIterator : IteratorType
{
	template<typename... Ts>
	MapItemsIterator(Ts&&... args) : IteratorType{args...} {}
	auto operator*() const
	{
		return KeyValuePair<const K&, V&>{this->key(), this->value()};
	}
};

template<typename K, typename V, typename MapType, typename IteratorType>
struct MapItems
{
	MapType& map;
	IteratorType begin()
	{
		return IteratorType(this->map.begin());
	}

	IteratorType end()
	{
		return IteratorType(this->map.end());
	}
};

/*
 * Python's dict.items for QMap: use in a for loop to iterate a map to
 * get both keys and values. Iteration yields KeyValuePairs.
 */
template<typename K, typename V>
auto items(const QMap<K, V>& map)
{
	return MapItems<
		const K&,
		const V&,
		const QMap<K, V>,
		MapItemsIterator<K, const V, typename QMap<K, V>::const_iterator>
	>{map};
}

template<typename K, typename V>
auto items(QMap<K, V>& map)
{
	return MapItems<
		const K&,
		V&,
		QMap<K, V>,
		MapItemsIterator<K, const V, typename QMap<K, V>::iterator>
	>{map};
}

template<typename T, typename IdentifierType>
struct TypeValue
{
	T value;
	bool operator==(TypeValue<T, IdentifierType> other) const
	{
		return value == other.value;
	}
	bool operator!=(TypeValue<T, IdentifierType> other) const
	{
		return value != other.value;
	}
	bool operator<(TypeValue<T, IdentifierType> other) const
	{
		return value < other.value;
	}
	bool operator>(TypeValue<T, IdentifierType> other) const
	{
		return value > other.value;
	}
	bool operator<=(TypeValue<T, IdentifierType> other) const
	{
		return value <= other.value;
	}
	bool operator>=(TypeValue<T, IdentifierType> other) const
	{
		return value >= other.value;
	}
};

template<typename T, typename R>
int qHash(TypeValue<T, R> value)
{
	return qHash(value.value);
}

using ModelId = TypeValue<int, struct TypeValueModelId>;

/**
 * Iterates a @c glm::mat
 */
template<int X, int Y, typename T, glm::qualifier Q, typename Fn>
void iter_matrix(const glm::mat<X, Y, T, Q>& matrix, Fn&& fn)
{
	for (int i = 0; i < X; ++i)
	{
		for (int j = 0; j < Y; ++j)
		{
			fn(i, j, matrix[i][j]);
		}
	}
}

QDataStream& operator<<(QDataStream&, const glm::vec3&);
QDataStream& operator>>(QDataStream&, glm::vec3&);

template<int X, int Y, typename T, glm::qualifier Q>
QDataStream& operator<<(QDataStream& stream, const glm::mat<X, Y, T, Q>& mat)
{
	iter_matrix(mat, [&stream](int, int, float x)
	{
		stream << x;
	});
	return stream;
}

template<int X, int Y, typename T, glm::qualifier Q>
QDataStream& operator>>(QDataStream& stream, glm::mat<X, Y, T, Q>& mat)
{
	iter_matrix(mat, [&stream](int, int, float x)
	{
		stream >> x;
	});
	return stream;
}

template<std::size_t N, typename T>
std::array<T, N> vectorToArray(const std::vector<T>& x)
{
	std::array<T, N> result;
	for (std::size_t i = 0; i < x.size() and i < N; i += 1)
	{
		result[i] = x[i];
	}
	return result;
}

mercurial