src/vertexmap.cpp

Sun, 29 Aug 2021 20:39:55 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sun, 29 Aug 2021 20:39:55 +0300
changeset 125
f127982d3412
parent 120
8c9fff699241
child 151
e628fc2e0c72
permissions
-rw-r--r--

Move tools under Document instead of MainWindow

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
120
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
4 unsigned int hashVertex(const glm::vec3& vec)
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
5 {
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
6 return qHash(glm::ivec3{
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
7 int(vec.x * 10000),
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
8 int(vec.y * 10000),
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
9 int(vec.z * 10000),
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
10 });
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
11 }
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
12
117
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 VertexMap::VertexMap(const Model *model) :
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 model{model}
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 {
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 connect(
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 model,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18 &Model::dataChanged,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 this,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 &VertexMap::build
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 );
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 connect(
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 model,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 &Model::rowsInserted,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 this,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 &VertexMap::build
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 connect(
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 model,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 &Model::rowsRemoved,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 this,
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 &VertexMap::build
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 this->build();
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
120
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
37 struct Edge
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
38 {
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
39 const glm::vec3& a;
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
40 const glm::vec3& b;
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
41 };
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
42
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
43 inline void edges(const ldraw::Object* object, std::function<void(Edge&&)> fn)
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
44 {
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
45 for (int i = 0; i < object->numPoints() - 1; i += 1)
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
46 {
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
47 const glm::vec3 p1 = object->getPoint(i);
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
48 const glm::vec3 p2 = object->getPoint((i + 1) % object->numPoints());
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
49 fn(Edge{p1, p2});
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
50 }
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
51 }
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
52
117
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 void VertexMap::build()
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 {
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 this->map.clear();
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
56 this->vertices.clear();
119
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
57 this->vertexHashes.clear();
117
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 this->model->apply<ldraw::Object>([&](const ldraw::Object* object)
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 {
120
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
60 glm::mat4 matrix;
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
61 if (object->numPoints() > 2)
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
62 {
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
63 const auto& p0 = object->getPoint(0);
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
64 const auto& p1 = object->getPoint(1);
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
65 const auto& p2 = object->getPoint(2);
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
66 const glm::vec3 a = glm::normalize(p1 - p0);
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
67 const glm::vec3 b = glm::normalize(p2 - p0);
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
68 const glm::vec3 c = glm::normalize(glm::cross(a, b));
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
69 const glm::vec3 d = glm::normalize(glm::cross(a, c));
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
70 matrix = glm::mat4{{a, 0}, {-c, 0}, {d, 0}, {}};
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
71 }
117
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 for (int i = 0; i < object->numPoints(); i += 1)
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 {
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 const glm::vec3& point = object->getPoint(i);
120
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
75 const unsigned int hash = hashVertex(point);
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
76 VertexInfo& info = this->map[hash];
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
77 info.point = point;
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
78 info.objects.insert(object->id);
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
79 if (object->numPoints() > 2 and not info.transformSet)
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
80 {
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
81 info.transform = matrix;
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
82 info.transform[3] = {point, 1};
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
83 info.transformSet = true;
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
84 }
119
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
85 if (not this->vertexHashes.contains(hash))
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
86 {
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
87 this->vertexHashes.insert(hash);
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
88 this->vertices.push_back(point);
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
89 }
117
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 }
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 });
120
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
92 for (auto& pair : this->map)
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
93 {
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
94 auto& info = pair.second;
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
95 // If there's no transformation calculated yet, make up a simple one that at least
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
96 // contains translation.
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
97 if (not info.transformSet)
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
98 {
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
99 info.transform = glm::translate(glm::identity<glm::mat4>(), info.point);
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
100 info.transformSet = true;
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
101 }
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
102 float scale = 1.0f;
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
103 for (const ldraw::id_t objectId : info.objects)
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
104 {
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
105 edges(model->get(objectId), [&](Edge&& edge)
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
106 {
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
107 if (hashVertex(edge.a) == pair.first or hashVertex(edge.b) == pair.first)
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
108 {
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
109 scale = std::min(scale, glm::length(edge.b - edge.a) / 3);
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
110 }
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
111 });
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
112 }
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
113 info.transform = glm::scale(info.transform, glm::vec3{scale, scale, scale});
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
114 }
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
115 Q_EMIT this->verticesChanged();
117
121a40d5e34c Add vertex map
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
116 }
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
117
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
118 /**
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
119 * @brief Apply \c fn for all vertices in the map.
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
120 * @param fn
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
121 */
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
122 void VertexMap::apply(ApplyFunction fn) const
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
123 {
119
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
124 for (unsigned int i = 0; i < this->vertices.size(); i += 1)
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
125 {
119
Teemu Piippo <teemu@hecknology.net>
parents: 118
diff changeset
126 const glm::vec3& point = this->vertices[i];
120
8c9fff699241 rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
127 fn(point, this->map.at(hashVertex(point)));
118
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
128 }
8e1c9f18ae15 Add vertex rendering
Teemu Piippo <teemu@hecknology.net>
parents: 117
diff changeset
129 }

mercurial