Sat, 08 Apr 2023 16:12:12 +0300
Use explicit captures in main
264
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
1 | #include "src/vertexmap.h" |
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
2 | #include "src/gl/common.h" |
117 | 3 | |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
250
diff
changeset
|
4 | hash_t hashVertex(const glm::vec3& vec) |
120
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 | |
338 | 13 | VertexMap::VertexMap(const QTextDocument *model) : |
117 | 14 | model{model} |
15 | { | |
16 | this->build(); | |
17 | } | |
18 | ||
120
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
19 | struct Edge |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
20 | { |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
21 | const glm::vec3& a; |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
22 | const glm::vec3& b; |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
23 | }; |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
24 | |
200 | 25 | inline void edges(const ModelElement& element, std::function<void(Edge&&)> fn) |
26 | { | |
27 | std::visit<void>(overloaded{ | |
28 | [fn](const Colored<LineSegment>& edge) { | |
29 | fn(Edge{edge.p1, edge.p2}); | |
30 | }, | |
31 | [fn](const Colored<Triangle>& triangle) { | |
32 | fn(Edge{triangle.p1, triangle.p2}); | |
33 | fn(Edge{triangle.p2, triangle.p3}); | |
34 | fn(Edge{triangle.p3, triangle.p1}); | |
35 | }, | |
36 | [fn](const Colored<Quadrilateral>& quad) { | |
37 | fn(Edge{quad.p1, quad.p2}); | |
38 | fn(Edge{quad.p2, quad.p3}); | |
39 | fn(Edge{quad.p3, quad.p4}); | |
40 | fn(Edge{quad.p4, quad.p1}); | |
41 | }, | |
42 | [fn](const Colored<ConditionalEdge>& cedge) { | |
43 | fn(Edge{cedge.p1, cedge.p2}); | |
44 | }, | |
45 | [](const ModelElement&&){} | |
46 | }, element); | |
47 | } | |
48 | ||
49 | inline void points( | |
50 | const ModelElement& element, | |
51 | std::function<void(const glm::vec3&)> fn) | |
120
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
52 | { |
200 | 53 | std::visit<void>(overloaded{ |
54 | [fn](const Colored<LineSegment>& edge) { | |
55 | fn(edge.p1); | |
56 | fn(edge.p2); | |
57 | }, | |
58 | [fn](const Colored<Triangle>& triangle) { | |
59 | fn(triangle.p1); | |
60 | fn(triangle.p2); | |
61 | fn(triangle.p3); | |
62 | }, | |
63 | [fn](const Colored<Quadrilateral>& quad) { | |
64 | fn(quad.p1); | |
65 | fn(quad.p2); | |
66 | fn(quad.p3); | |
67 | fn(quad.p4); | |
68 | }, | |
69 | [fn](const Colored<ConditionalEdge>& cedge) { | |
70 | fn(cedge.p1); | |
71 | fn(cedge.p2); | |
72 | fn(cedge.c1); | |
73 | fn(cedge.c2); | |
74 | }, | |
75 | [](const ModelElement&&){} | |
76 | }, element); | |
77 | } | |
78 | ||
79 | template<typename R> | |
80 | auto ifplanar( | |
81 | const ModelElement& element, | |
82 | std::function<R(const glm::vec3&, const glm::vec3&, const glm::vec3&)> fn | |
83 | ) | |
84 | { | |
85 | return std::visit(overloaded{ | |
86 | [fn](const Triangle& triangle) -> std::optional<R> { | |
87 | return fn(triangle.p1, triangle.p2, triangle.p3); | |
88 | }, | |
89 | [fn](const Quadrilateral quad) -> std::optional<R> { | |
90 | return fn(quad.p1, quad.p2, quad.p3); | |
91 | }, | |
92 | [](const ModelElement&&) -> std::optional<R> {return {};} | |
93 | }, element); | |
120
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
94 | } |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
95 | |
117 | 96 | void VertexMap::build() |
97 | { | |
98 | this->map.clear(); | |
118 | 99 | this->vertices.clear(); |
119 | 100 | this->vertexHashes.clear(); |
333
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
309
diff
changeset
|
101 | #if 0 |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
200
diff
changeset
|
102 | for (std::size_t i = 0; i < this->model->size(); ++i) |
117 | 103 | { |
200 | 104 | const ModelElement& element = this->model->at(i); |
105 | std::optional<glm::mat4> matrix = ifplanar<glm::mat4>( | |
106 | element, | |
107 | [](const glm::vec3& p1, const glm::vec3& p2, const glm::vec3& p3) | |
108 | { | |
109 | const glm::vec3 a = glm::normalize(p2 - p1); | |
110 | const glm::vec3 b = glm::normalize(p3 - p1); | |
111 | const glm::vec3 c = glm::normalize(glm::cross(a, b)); | |
112 | const glm::vec3 d = glm::normalize(glm::cross(a, c)); | |
113 | return glm::mat4{{a, 0}, {-c, 0}, {d, 0}, {}}; | |
114 | }); | |
115 | points(element, [&](const glm::vec3 point) { | |
259
c27612f0eac0
- Made it build under Qt6
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
250
diff
changeset
|
116 | const hash_t hash = hashVertex(point); |
120
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
117 | VertexInfo& info = this->map[hash]; |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
118 | info.point = point; |
200 | 119 | info.objects.insert(this->model->idAt(i)); |
120 | if (matrix.has_value() and not info.transformSet) | |
120
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
121 | { |
200 | 122 | info.transform = matrix.value(); |
120
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
123 | info.transform[3] = {point, 1}; |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
124 | info.transformSet = true; |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
125 | } |
119 | 126 | if (not this->vertexHashes.contains(hash)) |
127 | { | |
128 | this->vertexHashes.insert(hash); | |
129 | this->vertices.push_back(point); | |
130 | } | |
200 | 131 | }); |
132 | } | |
120
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
133 | for (auto& pair : this->map) |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
134 | { |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
135 | auto& info = pair.second; |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
136 | // 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
|
137 | // contains translation. |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
138 | if (not info.transformSet) |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
139 | { |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
140 | info.transform = glm::translate(glm::identity<glm::mat4>(), info.point); |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
141 | info.transformSet = true; |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
142 | } |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
143 | float scale = 1.0f; |
309
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
144 | for (const ElementId objectId : info.objects) |
120
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
145 | { |
200 | 146 | const std::optional<int> index = model->find(objectId); |
147 | if (index.has_value()) { | |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
200
diff
changeset
|
148 | edges(this->model->at(unsigned_cast(index.value())), [&](Edge&& edge) |
120
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
149 | { |
200 | 150 | if (hashVertex(edge.a) == pair.first or hashVertex(edge.b) == pair.first) |
151 | { | |
152 | scale = std::min(scale, glm::length(edge.b - edge.a) / 3); | |
153 | } | |
154 | }); | |
155 | } | |
120
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
156 | } |
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
157 | 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
|
158 | } |
333
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
309
diff
changeset
|
159 | #endif |
118 | 160 | Q_EMIT this->verticesChanged(); |
117 | 161 | } |
118 | 162 | |
163 | /** | |
164 | * @brief Apply \c fn for all vertices in the map. | |
165 | * @param fn | |
166 | */ | |
167 | void VertexMap::apply(ApplyFunction fn) const | |
168 | { | |
119 | 169 | for (unsigned int i = 0; i < this->vertices.size(); i += 1) |
118 | 170 | { |
119 | 171 | const glm::vec3& point = this->vertices[i]; |
120
8c9fff699241
rework rendering of vertices
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
172 | fn(point, this->map.at(hashVertex(point))); |
118 | 173 | } |
174 | } |