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