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

117
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #include "vertexmap.h"
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 #include "linetypes/polygonobject.h"
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 VertexMap::VertexMap(const Model *model) :
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 model{model}
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 {
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 connect(
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 model,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 &Model::dataChanged,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 this,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 &VertexMap::build
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 );
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 connect(
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 model,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 &Model::rowsInserted,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 this,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 &VertexMap::build
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 );
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 connect(
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 model,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 &Model::rowsRemoved,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 this,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 &VertexMap::build
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 );
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 this->build();
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 }
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 void VertexMap::build()
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 {
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 this->map.clear();
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
31 this->vertices.clear();
117
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 this->model->apply<ldraw::Object>([&](const ldraw::Object* object)
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 {
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 for (int i = 0; i < object->numPoints(); i += 1)
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 {
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 const glm::vec3& point = object->getPoint(i);
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
37 const unsigned int hash = qHash(point);
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
38 this->map[hash].insert(object->id);
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
39 this->vertices[hash] = point;
117
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 }
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 });
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
42 Q_EMIT this->verticesChanged();
117
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 }
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
44
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
45 /**
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
46 * @brief Apply \c fn for all vertices in the map.
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
47 * @param fn
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
48 */
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
49 void VertexMap::apply(ApplyFunction fn) const
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
50 {
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
51 for (auto it = this->map.cbegin(); it != this->map.cend(); ++it)
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
52 {
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
53 const glm::vec3& point = this->vertices.at(it->first);
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
54 fn(point, it->second);
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
55 }
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
56 }

mercurial