src/polygoncache.cpp

Wed, 09 Mar 2022 13:01:50 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 09 Mar 2022 13:01:50 +0200
changeset 173
8a3047468994
parent 152
03f8e6d42e13
child 183
97b591813c8b
permissions
-rw-r--r--

Fix performance issues in Model::find

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

PolygonCache::PolygonCache(Model *model) :
	model{model}
{
	const auto mark = [this](){ this->needRecache = true; };
	connect(model, &Model::dataChanged, mark);
	connect(model, &Model::rowsInserted, mark);
	connect(model, &Model::rowsRemoved, mark);
}

/**
 * @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
 */
std::vector<gl::Polygon> PolygonCache::getPolygons(DocumentManager* documents)
{
	if (this->needRecache)
	{
		this->cachedPolygons.clear();
		const std::optional<ModelId> modelId = documents->findIdForModel(this->model);
		if (modelId.has_value())
		{
			ldraw::GetPolygonsContext context{modelId.value(), documents};
			for (int i = 0; i < this->model->size(); i += 1)
			{
				this->getObjectPolygons(i, this->cachedPolygons, &context);
			}
		}
		this->needRecache = false;
	}
	return this->cachedPolygons;
}

/**
 * @brief Gets the GL polygons of the object at the specified position in the model
 * @param index Index of object in the model
 * @param polygons_out Vector to add polygons into
 * @param context Context to use to resolve subfile references
 */
void PolygonCache::getObjectPolygons(
	const int index,
	std::vector<gl::Polygon>& polygons_out,
	ldraw::GetPolygonsContext* context) const
{
	const ldraw::Object* object = (*this->model)[unsigned_cast(index)];
	object->getPolygons(polygons_out, context);
}

mercurial