src/vertexmap.cpp

Tue, 27 Jul 2021 16:29:00 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 27 Jul 2021 16:29:00 +0300
changeset 118
8e1c9f18ae15
parent 117
121a40d5e34c
child 119
24275a4064f4
permissions
-rw-r--r--

Add vertex rendering

#include "vertexmap.h"
#include "linetypes/polygonobject.h"

VertexMap::VertexMap(const Model *model) :
	model{model}
{
	connect(
		model,
		&Model::dataChanged,
		this,
		&VertexMap::build
	);
	connect(
		model,
		&Model::rowsInserted,
		this,
		&VertexMap::build
	);
	connect(
		model,
		&Model::rowsRemoved,
		this,
		&VertexMap::build
	);
	this->build();
}

void VertexMap::build()
{
	this->map.clear();
	this->vertices.clear();
	this->model->apply<ldraw::Object>([&](const ldraw::Object* object)
	{
		for (int i = 0; i < object->numPoints(); i += 1)
		{
			const glm::vec3& point = object->getPoint(i);
			const unsigned int hash = qHash(point);
			this->map[hash].insert(object->id);
			this->vertices[hash] = point;
		}
	});
	Q_EMIT this->verticesChanged();
}

/**
 * @brief Apply \c fn for all vertices in the map.
 * @param fn
 */
void VertexMap::apply(ApplyFunction fn) const
{
	for (auto it = this->map.cbegin(); it != this->map.cend(); ++it)
	{
		const glm::vec3& point = this->vertices.at(it->first);
		fn(point, it->second);
	}
}

mercurial