src/polygoncache.cpp

Tue, 28 Jun 2022 11:51:17 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Tue, 28 Jun 2022 11:51:17 +0300
changeset 281
afed72b544f0
parent 264
76a025db4948
child 305
d891da20abca
permissions
-rw-r--r--

- Add CMake build type to about page and window title
- Use locale-dependant short date format in window title and long format in about page
- Move version definition into project() call
- Renamed the c++ macros to match CMake variable names

#include "src/circularprimitive.h"
#include "src/documentmanager.h"
#include "src/invert.h"
#include "src/polygoncache.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([&transform](glm::vec3& p) {
		p = transform * glm::vec4{p, 1};
	}, element);
	return element;
}

PolygonCache* findPolygonCacheForModel(Model* model, DocumentManager* context)
{
	std::optional<ModelId> modelId = context->findIdForModel(model);
	if (modelId.has_value()) {
		return context->getPolygonCacheForModel(modelId.value());
	}
	else {
		return nullptr;
	}
}

static std::vector<WithId<PolygonElement>> getPolygonsAt(
	const Model* model,
	GetPolygonsContext* context)
{
	std::vector<WithId<PolygonElement>> result;
	for (std::size_t 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});
			},
			[&result, &id, context](const Colored<SubfileReference>& ref) {
				Model* dependency = context->documents->findDependencyByName(context->modelId, ref.name);
				PolygonCache* cache = nullptr;
				if (dependency != nullptr) {
					cache = findPolygonCacheForModel(dependency, context->documents);
				}
				if (cache != nullptr) {
					const bool needInverting = glm::determinant(ref.transformation) < 0;
					const PolygonCache::vector_type* modelPolygons = getCachedPolygons(
						cache,
						dependency,
						context->documents);
					reserveMore(result, 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);
					}
				}
			},
			[&result, id](const Colored<CircularPrimitive>& circ) {
				rasterize(circ, [&](const ModelElement& element){
					std::visit<void>(overloaded{
						// TODO: :-(
						[&](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 auto&){},
					}, element);
				});
			},
			[](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 PolygonCache::vector_type* 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