-
Notifications
You must be signed in to change notification settings - Fork 0
/
triangulationoptimizer.cpp
319 lines (278 loc) · 12.4 KB
/
triangulationoptimizer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "triangulationoptimizer.h"
#include <QOpenGLShader>
#include <QOpenGLContext>
#include <QOpenGLExtraFunctions>
#include <cmath>
#include <algorithm>
struct TriangulationOptimizer::ErrorData
{
GLuint error_total = 0;
GLuint count = 0;
};
namespace
{
template<typename T>
bool sens(const Triangle& t, const T& tri)
{
Vec2 a = tri[t.a];
Vec2 l = tri[t.b];
Vec2 r = tri[t.c];
Vec2 al{l.x - a.x, l.y - a.y};
Vec2 ar{r.x - a.x, r.y - a.y};
return al.x * ar.y - ar.x * al.y > 0;
}
float angle(Vec2 l, Vec2 a, Vec2 r)
{
using std::sqrt, std::acos;
Vec2 al{l.x - a.x, l.y - a.y};
Vec2 ar{r.x - a.x, r.y - a.y};
return acos((al.x * ar.x + al.y * ar.y) / sqrt(sqrLen(al)) / sqrt(sqrLen(ar)));
}
struct TriangleData
{
GLuint color_total_r = 0;
GLuint color_total_g = 0;
GLuint color_total_b = 0;
GLuint count = 0;
};
auto computeRegularisationGradients(const Triangulation& triangulation, const std::vector<std::vector<std::pair<unsigned,unsigned char>>>& triangles_per_vertex, float pixel_width, float pixel_height)
{
std::vector<Vec2> result(triangulation.size());
for(VertexIndice i = 0; i < result.size(); ++i)
{
auto& neighbor_tris = triangles_per_vertex[i];
Vec2 point = triangulation[i];
float energies[4]{};
Vec2 offsets[4]{{pixel_width,0},{0,pixel_height},{-pixel_width,0},{0,-pixel_height}};
for(int i = 0; i < 4; ++i)
{
Vec2 p{point.x + offsets[i].x, point.y + offsets[i].y};
for(auto [tri_id, pos] : neighbor_tris)
{
if(pos != 0) energies[i] += sqrDist(p, triangulation[triangulation.triangles()[tri_id].a]);
if(pos != 1) energies[i] += sqrDist(p, triangulation[triangulation.triangles()[tri_id].b]);
if(pos != 2) energies[i] += sqrDist(p, triangulation[triangulation.triangles()[tri_id].c]);
}
}
auto& [right_energy, up_energy, left_energy, down_energy] = energies;
result[i] = {(right_energy - left_energy) / neighbor_tris.size(), (up_energy - down_energy) / neighbor_tris.size()};
}
return result;
}
auto computeGradients(const Triangulation& triangulation, const std::vector<std::vector<std::pair<unsigned,unsigned char>>>& triangles_per_vertex, const std::vector<TriangulationOptimizer::ErrorData>& error_data, float pixel_width, float pixel_height)
{
std::vector<Vec2> result(triangulation.size());
for(VertexIndice i = 0; i < result.size(); ++i)
{
Vec2& gradient = result[i];
auto& neighbor_tris = triangles_per_vertex[i];
for(auto [tri_id, pos] : neighbor_tris)
{
float errors[4];
for(int i = 0; i < 4; ++i)
{
auto data = error_data[tri_id * 13 + 1 + pos * 4 + i];
errors[i] = data.count ? data.error_total / 255.f / data.count / 2 : 0.f;
}
auto& [right_error, up_error, left_error, down_error] = errors;
gradient.x += (right_error - left_error) / (2*pixel_width) / 3;
gradient.y += (up_error - down_error) / (2*pixel_height) / 3;
}
}
return result;
}
void checkForFlip(unsigned t, Triangulation& triangulation, const std::vector<std::vector<std::pair<unsigned,unsigned char>>>& triangles_per_vertex)
{
Triangle tri = triangulation.triangles()[t];
VertexIndice indices[]{tri.a, tri.b, tri.c};
for(int i = 0; i < 3; ++i)
{
VertexIndice v1 = indices[i];
VertexIndice v2 = indices[(i+1)%3];
VertexIndice v3 = indices[(i+2)%3];
float local_angle = angle(triangulation[v1], triangulation[v3], triangulation[v2]);
for(auto [other_t,l] : triangles_per_vertex[v1])
{
if(other_t==t) continue;
Triangle other_tri = triangulation.triangles()[other_t];
VertexIndice other_indices[]{other_tri.a, other_tri.b, other_tri.c};
bool has_edge = false;
VertexIndice opposite=v3;
for(VertexIndice v : other_indices)
{
if(v != v1 && v != v2 && v != v3) opposite = v;
if(v == v2) has_edge = true;
}
if(has_edge && local_angle + angle(triangulation[v1], triangulation[opposite], triangulation[v2]) >= M_PI)
{
triangulation.flipCommonEdge(t, other_t);
return;
}
}
}
}
}
TriangulationOptimizer::TriangulationOptimizer()
:_gl{QOpenGLContext::currentContext()->extraFunctions()}
{
QOpenGLShader vertex_shader{QOpenGLShader::Vertex};
QOpenGLShader geometry_shader{QOpenGLShader::Geometry};
QOpenGLShader fragment_shaders[2] = {QOpenGLShader{QOpenGLShader::Fragment}, QOpenGLShader{QOpenGLShader::Fragment}};
vertex_shader.compileSourceFile(":/Shaders/TriangulationOptimizer/vertex.glsl");
geometry_shader.compileSourceFile(":/Shaders/TriangulationOptimizer/geometry.glsl");
fragment_shaders[0].compileSourceFile(":/Shaders/TriangulationOptimizer/fragment1.glsl");
fragment_shaders[1].compileSourceFile(":/Shaders/TriangulationOptimizer/fragment2.glsl");
for(int i = 0; i < 2; ++i)
{
_programs[i].addShader(&vertex_shader);
_programs[i].addShader(&geometry_shader);
_programs[i].addShader(&fragment_shaders[i]);
_programs[i].link();
_image_locations[i] = _programs[i].uniformLocation("image");
_programs[i].removeAllShaders();
}
}
TriangulationOptimizer::~TriangulationOptimizer()
{
}
std::vector<TriangulationOptimizer::ErrorData> TriangulationOptimizer::computeErrors(const Triangulation& triangulation, int w, int h)
{
GLuint vao;
_gl->glGenVertexArrays(1,&vao);
_gl->glBindVertexArray(vao);
GLuint rbo;
_gl->glGenRenderbuffers(1, &rbo);
_gl->glBindRenderbuffer(GL_RENDERBUFFER, rbo);
_gl->glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, w, h);
GLuint fbo;
_gl->glGenFramebuffers(1, &fbo);
_gl->glBindFramebuffer(GL_FRAMEBUFFER, fbo);
_gl->glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo);
_gl->glViewport(0,0,w,h);
_gl->glDisable(GL_DEPTH_TEST);
_gl->glDisable(GL_CULL_FACE);
GLuint buffers[4];
_gl->glGenBuffers(4, buffers);
auto& [vertices_buffer, element_buffer, triangle_data_buffer, triangle_error_buffer] = buffers;
_gl->glBindBuffer(GL_ARRAY_BUFFER, vertices_buffer);
_gl->glBufferData(GL_ARRAY_BUFFER, triangulation.size() * sizeof(Vec2), triangulation.vertices().data(), GL_STATIC_DRAW);
_gl->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, element_buffer);
static_assert (sizeof(Triangle) == 3*sizeof(VertexIndice));
_gl->glBufferData(GL_ELEMENT_ARRAY_BUFFER, triangulation.triangles().size() * sizeof(Triangle), triangulation.triangles().data(), GL_STATIC_DRAW);
size_t total_triangles = triangulation.triangles().size() * 13;
std::vector<TriangleData> triangle_data(total_triangles);
_gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, triangle_data_buffer);
_gl->glBufferData(GL_SHADER_STORAGE_BUFFER, total_triangles * sizeof(TriangleData), triangle_data.data(), GL_DYNAMIC_COPY);
std::vector<ErrorData> error_data(total_triangles);
_gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, triangle_error_buffer);
_gl->glBufferData(GL_SHADER_STORAGE_BUFFER, total_triangles * sizeof(ErrorData), /*error_data.data()*/nullptr, GL_DYNAMIC_READ);
_gl->glEnableVertexAttribArray(0);
_gl->glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
_gl->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, triangle_data_buffer);
_gl->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, triangle_error_buffer);
for(int i = 0; i < 2; ++i)
{
_programs[i].bind();
_programs[i].setUniformValue(_image_locations[i], 0);
_gl->glDrawElements(GL_TRIANGLES, triangulation.triangles().size() * 3, GL_UNSIGNED_INT, nullptr);
_gl->glMemoryBarrier(GL_ALL_BARRIER_BITS /*GL_SHADER_STORAGE_BARRIER_BIT | GL_CLIENT_MAPPED_BUFFER_BARRIER_BIT | GL_BUFFER_UPDATE_BARRIER_BIT*/);
}
auto s = _gl->glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE,0);
_gl->glClientWaitSync(s,0,-1);
_gl->glDeleteSync(s);
for(auto& e : error_data) e.error_total = 1;
_gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, triangle_error_buffer);
GLvoid* p = _gl->glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, total_triangles * sizeof(ErrorData), GL_MAP_READ_BIT);
memcpy(error_data.data(), p, total_triangles * sizeof(ErrorData));
_gl->glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
_gl->glDeleteBuffers(4, buffers);
_gl->glDeleteFramebuffers(1, &fbo);
_gl->glDeleteRenderbuffers(1, &rbo);
_gl->glDeleteVertexArrays(1, &vao);
return error_data;
}
bool tri_eq(const Triangle& l, const Triangle& r)
{
VertexIndice la[]{l.a,l.b,l.c};
VertexIndice ra[]{r.a,r.b,r.c};
for(auto s : la)
{
if(std::find(std::begin(ra), std::end(ra), s) == std::end(ra)) return false;
}
return true;
}
void TriangulationOptimizer::optimize(Triangulation& triangulation, unsigned texture_handle, bool split)
{
int w, h;
_gl->glActiveTexture(GL_TEXTURE0);
_gl->glBindTexture(GL_TEXTURE_2D, texture_handle);
_gl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
_gl->glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h);
float pixel_width = 1.f / w;
float pixel_height = 1.f / h;
auto error_data = computeErrors(triangulation, w, h);
const auto& triangles_per_vertex = triangulation.trianglesPerVertex();
std::vector<Vec2> regularisation_gradients = computeRegularisationGradients(triangulation, triangles_per_vertex, pixel_width, pixel_height);
std::vector<Vec2> gradients = computeGradients(triangulation, triangles_per_vertex, error_data, pixel_width, pixel_height);
std::vector<Vec2> new_vertices = triangulation.vertices();
for(VertexIndice i = 0; i < new_vertices.size(); ++i)
{
Vec2 gradient{ gradients[i].x * _step + regularisation_gradients[i].x * _regularisation,
gradients[i].y * _step + regularisation_gradients[i].y * _regularisation };
gradient.x = std::clamp(gradient.x, -pixel_width * _step_clamp_pixel, pixel_width * _step_clamp_pixel);
gradient.y = std::clamp(gradient.y, -pixel_height * _step_clamp_pixel, pixel_height * _step_clamp_pixel);
if(new_vertices[i].x != 0 && new_vertices[i].x != 1 && new_vertices[i].y != 0 && new_vertices[i].y != 1)
{
new_vertices[i].x -= gradient.x;
new_vertices[i].y -= gradient.y;
}
for(auto [t,unused] : triangles_per_vertex[i])
{
Triangle tri = triangulation.triangles()[t];
if(sens(tri, triangulation) != sens(tri, new_vertices))
{
new_vertices[i] = triangulation[i];
break;
}
}
}
std::copy(begin(new_vertices), end(new_vertices), begin(triangulation));
unsigned tri_count = triangulation.triangles().size();
unsigned long long total_count = 0;
for(unsigned t = 0; t < tri_count; ++t)
{
total_count += error_data[t*13].count;
}
for(unsigned t = 0; t < tri_count; ++t)
{
if(split && (float)error_data[t*13].error_total / total_count >= _energy_split_treshold) triangulation.splitTriangle(t);
}
for(unsigned t = 0; t < triangulation.triangles().size(); ++t)
{
if(triangulation.triangleArea(t) < _min_triangle_area) triangulation.deleteTriangle(t);
}
for(unsigned t = 0; t < triangulation.triangles().size(); ++t)
{
checkForFlip(t, triangulation, triangles_per_vertex);
}
}
void TriangulationOptimizer::optimizeSplit(Triangulation& triangulation, unsigned texture_handle)
{
optimize(triangulation, texture_handle, true);
}
float TriangulationOptimizer::energySplitThreshold() const
{
return _energy_split_treshold;
}
void TriangulationOptimizer::energySplitThreshold(float val)
{
_energy_split_treshold = val;
}
float TriangulationOptimizer::minTriangleArea() const
{
return _min_triangle_area;
}
void TriangulationOptimizer::minTriangleArea(float val)
{
_min_triangle_area = val;
}