Wed, 28 Jul 2021 08:23:09 +0300
update
| 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(); |
| 119 | 32 | this->vertexHashes.clear(); |
| 117 | 33 | this->model->apply<ldraw::Object>([&](const ldraw::Object* object) |
| 34 | { | |
| 35 | for (int i = 0; i < object->numPoints(); i += 1) | |
| 36 | { | |
| 37 | const glm::vec3& point = object->getPoint(i); | |
| 118 | 38 | const unsigned int hash = qHash(point); |
| 39 | this->map[hash].insert(object->id); | |
| 119 | 40 | if (not this->vertexHashes.contains(hash)) |
| 41 | { | |
| 42 | this->vertexHashes.insert(hash); | |
| 43 | this->vertices.push_back(point); | |
| 44 | } | |
| 117 | 45 | } |
| 46 | }); | |
| 118 | 47 | Q_EMIT this->verticesChanged(); |
| 117 | 48 | } |
| 118 | 49 | |
| 50 | /** | |
| 51 | * @brief Apply \c fn for all vertices in the map. | |
| 52 | * @param fn | |
| 53 | */ | |
| 54 | void VertexMap::apply(ApplyFunction fn) const | |
| 55 | { | |
| 119 | 56 | for (unsigned int i = 0; i < this->vertices.size(); i += 1) |
| 118 | 57 | { |
| 119 | 58 | const glm::vec3& point = this->vertices[i]; |
| 59 | fn(point, this->map.at(qHash(point))); | |
| 118 | 60 | } |
| 61 | } |