src/polygoncache.cpp

Thu, 09 Jun 2022 13:32:55 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Thu, 09 Jun 2022 13:32:55 +0300
changeset 210
232e7634cc8a
parent 205
1a4342d80de7
child 211
b27b90fb993f
permissions
-rw-r--r--

more refactoring, dosn't build yet

#include "polygoncache.h"
#include "documentmanager.h"
#include "invert.h"

Model* resolve(const QString& name, const ModelId callingModelId, DocumentManager* documents)
{
	return documents->findDependencyByName(callingModelId, name);
}

static PolygonElement transformed(
	PolygonElement element,
	const glm::mat4& transform)
{
	visitPoints(element, [&transform](glm::vec3& p) {
		p = transform * glm::vec4{p, 1};
	});
	return element;
}

static std::vector<WithId<PolygonElement>> getPolygonsAt(
	const Model* model,
	GetPolygonsContext* context)
{
	std::vector<WithId<PolygonElement>> result;
	for (int i = 0; i < model->size(); i += 1)
	{
		const ModelElement& element = (*model)[i];
		const ModelId id = model->idAt(i);
		std::visit<void>(overloaded{
			[&](const Colored<LineSegment>& edge) {
				result.push_back({{edge, edge.color}, id});
			},
			[&](const Colored<Triangle>& triangle) {
				result.push_back({{triangle, triangle.color}, id});
			},
			[&](const Colored<Quadrilateral>& quad) {
				result.push_back({{quad, quad.color}, id});
			},
			[&](const Colored<ConditionalEdge>& cedge) {
				result.push_back({{cedge, cedge.color}, id});
			},
			[&](const Colored<SubfileReference>& ref) {
				Model* dependency = context->documents->findDependencyByName(context->modelId, ref.name);
				PolygonCache* cache = nullptr;
				if (dependency != nullptr)
				{
					const auto dependencyModelId = context->documents->findIdForModel(dependency);
					if (dependencyModelId.has_value()) {
						cache = context->documents->getPolygonCacheForModel(dependencyModelId.value());
					}
				}
				if (cache != nullptr) {
					const bool needInverting = glm::determinant(ref.transformation) < 0;
					const auto& modelPolygons = getCachedPolygons(
						cache,
						dependency,
						context->documents);
					result.reserve(result.size() + modelPolygons.size());
					for (WithId<PolygonElement> polygon : modelPolygons) {
						polygon = {transformed(polygon, ref.transformation), polygon.id};
						if (needInverting != ref.inverted) {
							gl::invert(polygon);
						}
						if (polygon.color == MAIN_COLOR) {
							polygon.color = ref.color;
						}
						polygon.id = id;
						result.push_back(polygon);
					}
				}
			},
			[](const ModelElement&) {}
		}, element);
	}
	return result;
}

/**
 * @brief Gets a list of GL polygons that are used to represent this model.
 * @details Will build polygons if polygons are outdated.
 * @param documents Documents to use to resolve subfile references
 * @return vector of GL polygons
 */
const std::vector<WithId<PolygonElement>> &getCachedPolygons(
	PolygonCache *cache,
	Model *model,
	DocumentManager *documents)
{
	if (cache->needRecache)
	{
		cache->cachedPolygons.clear();
		const std::optional<ModelId> modelId = documents->findIdForModel(model);
		if (modelId.has_value())
		{
			GetPolygonsContext context{modelId.value(), documents};
			cache->cachedPolygons = getPolygonsAt(model, &context);
		}
		cache->needRecache = false;
	}
	return cache->cachedPolygons;
}

mercurial