forked from swayfreeda/ImageBasedModellingEduV1.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_texture_patches.cpp
executable file
·544 lines (461 loc) · 23 KB
/
generate_texture_patches.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
* Copyright (C) 2015, Nils Moehrle
* TU Darmstadt - Graphics, Capture and Massively Parallel Computing
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD 3-Clause license. See the LICENSE.txt file for details.
*/
#include <set>
#include <list>
#include <util/timer.h>
#include <core/image_tools.h>
#include <Eigen/SparseCore>
#include <Eigen/SparseLU>
#include "texturing.h"
TEX_NAMESPACE_BEGIN
void merge_vertex_projection_infos(std::vector<std::vector<VertexProjectionInfo> > * vertex_projection_infos) {
/* Merge vertex infos within the same texture patch. */
#pragma omp parallel for
for (std::size_t i = 0; i < vertex_projection_infos->size(); ++i) {
// vertex projection infomation of each vertex
std::vector<VertexProjectionInfo> & infos = vertex_projection_infos->at(i);
std::map<std::size_t, VertexProjectionInfo> info_map;
std::map<std::size_t, VertexProjectionInfo>::iterator it;
for (VertexProjectionInfo const & info : infos) {
std::size_t texture_patch_id = info.texture_patch_id;
if((it = info_map.find(texture_patch_id)) == info_map.end()) {
info_map[texture_patch_id] = info;
} else {
it->second.faces.insert(it->second.faces.end(),
info.faces.begin(), info.faces.end());
}
}
infos.clear();
infos.reserve(info_map.size());
for (it = info_map.begin(); it != info_map.end(); ++it) {
infos.push_back(it->second);
}
}
}
/** Struct representing a TexturePatch candidate - final texture patches are obtained by merging candiates. */
struct TexturePatchCandidate {
Rect<int> bounding_box;
TexturePatch::Ptr texture_patch;
};
/** Create a TexturePatchCandidate by calculating
* the faces' bounding box projected into the view,
* relative texture coordinates
* and extracting the texture views relevant part
*/
TexturePatchCandidate
generate_candidate(int label, TextureView const & texture_view,
std::vector<std::size_t> const & faces, core::TriangleMesh::ConstPtr mesh) {
// view image
core::ByteImage::Ptr view_image = texture_view.get_image();
int min_x = view_image->width(), min_y = view_image->height();
int max_x = 0, max_y = 0;
// all the faces in the mesh
core::TriangleMesh::FaceList const & mesh_faces = mesh->get_faces();
// all the vertices in the mesh
core::TriangleMesh::VertexList const & vertices = mesh->get_vertices();
// compute texture coordinates
std::vector<math::Vec2f> texcoords;
for (std::size_t i = 0; i < faces.size(); ++i) {
for (std::size_t j = 0; j < 3; ++j) {
math::Vec3f vertex = vertices[mesh_faces[faces[i] * 3 + j]];
math::Vec2f pixel = texture_view.get_pixel_coords(vertex);
texcoords.push_back(pixel);
min_x = std::min(static_cast<int>(std::floor(pixel[0])) - texture_patch_border, min_x);
min_y = std::min(static_cast<int>(std::floor(pixel[1])) - texture_patch_border, min_y);
max_x = std::max(static_cast<int>(std::ceil(pixel[0])) + texture_patch_border, max_x);
max_y = std::max(static_cast<int>(std::ceil(pixel[1])) + texture_patch_border, max_y);
}
}
/* Calculate the relative texcoords. */
math::Vec2f min(min_x, min_y);
for (std::size_t i = 0; i < texcoords.size(); ++i) {
texcoords[i] = texcoords[i] - min;
}
int const width = max_x - min_x;
int const height = max_y - min_y;
/* Check for valid projections/erroneous labeling files. */
assert(min_x >= 0 - texture_patch_border);
assert(min_y >= 0 - texture_patch_border);
assert(max_x < view_image->width() + texture_patch_border);
assert(max_y < view_image->height() + texture_patch_border);
core::ByteImage::Ptr image;
image = core::image::crop(view_image, width, height, min_x, min_y, *math::Vec3uc(255, 0, 255));
// gamma corrections of an image
core::image::gamma_correct(image, 2.2f);
TexturePatchCandidate texture_patch_candidate =
{Rect<int>(min_x, min_y, max_x, max_y),
TexturePatch::create(label, faces, texcoords, image)};
return texture_patch_candidate;
}
// generate texture patches
void
generate_texture_patches(UniGraph const & graph, core::TriangleMesh::ConstPtr mesh,
core::VertexInfoList::ConstPtr vertex_infos,
std::vector<TextureView> * texture_views,
std::vector<std::vector<VertexProjectionInfo> > * vertex_projection_infos,
std::vector<TexturePatch::Ptr> * texture_patches) {
util::WallTimer timer;
// Face List
core::TriangleMesh::FaceList const & mesh_faces = mesh->get_faces();
// Vertices
core::TriangleMesh::VertexList const & vertices = mesh->get_vertices();
// Projection Infomation
vertex_projection_infos->resize(vertices.size());
//==============================generate patches ================================================//
std::size_t num_patches = 0;
std::cout << "\tRunning... " << std::flush;
#pragma omp parallel for schedule(dynamic)
for (std::size_t i = 0; i < texture_views->size(); ++i) {
// get all the patches that are seen from view i+1
std::vector<std::vector<std::size_t> > subgraphs;
int const label = i + 1;
graph.get_subgraphs(label, &subgraphs);
// get the texture view and related image
TextureView * texture_view = &texture_views->at(i);
texture_view->load_image();
// each subgraph is a patch candidate
std::list<TexturePatchCandidate> candidates;
for (std::size_t j = 0; j < subgraphs.size(); ++j) {
candidates.push_back(generate_candidate(label, *texture_view, subgraphs[j], mesh));
}
texture_view->release_image();
/* Merge candidates which contain the same image content. */
std::list<TexturePatchCandidate>::iterator it, sit;
for (it = candidates.begin(); it != candidates.end(); ++it) {
for (sit = candidates.begin(); sit != candidates.end();) {
Rect<int> bounding_box = sit->bounding_box;
if (it != sit && bounding_box.is_inside(&it->bounding_box)) {
TexturePatch::Faces & faces = it->texture_patch->get_faces();
TexturePatch::Faces & ofaces = sit->texture_patch->get_faces();
faces.insert(faces.end(), ofaces.begin(), ofaces.end());
TexturePatch::Texcoords & texcoords = it->texture_patch->get_texcoords();
TexturePatch::Texcoords & otexcoords = sit->texture_patch->get_texcoords();
math::Vec2f offset;
offset[0] = sit->bounding_box.min_x - it->bounding_box.min_x;
offset[1] = sit->bounding_box.min_y - it->bounding_box.min_y;
for (std::size_t i = 0; i < otexcoords.size(); ++i) {
texcoords.push_back(otexcoords[i] + offset);
}
sit = candidates.erase(sit);
} else {
++sit;
}
}
}
it = candidates.begin();
for (; it != candidates.end(); ++it) {
std::size_t texture_patch_id;
#pragma omp critical
{
texture_patches->push_back(it->texture_patch);
texture_patch_id = num_patches++;
}
std::vector<std::size_t> const & faces = it->texture_patch->get_faces();
std::vector<math::Vec2f> const & texcoords = it->texture_patch->get_texcoords();
for (std::size_t i = 0; i < faces.size(); ++i) {
std::size_t const face_id = faces[i];
std::size_t const face_pos = face_id * 3;
for (std::size_t j = 0; j < 3; ++j) {
std::size_t const vertex_id = mesh_faces[face_pos + j];
math::Vec2f const projection = texcoords[i * 3 + j];
VertexProjectionInfo info = {texture_patch_id, projection, {face_id}};
#pragma omp critical
vertex_projection_infos->at(vertex_id).push_back(info);
}
}
} // for each candidata
} // for each texture view
// merge vertex projection information
merge_vertex_projection_infos(vertex_projection_infos);
//================================= process (fill) holes ============================================//
std::size_t num_holes = 0;
std::size_t num_hole_faces = 0;
//if (!settings.skip_hole_filling) {
{
std::vector<std::vector<std::size_t> > subgraphs;
graph.get_subgraphs(0, &subgraphs);
#pragma omp parallel for schedule(dynamic)
for (std::size_t i = 0; i < subgraphs.size(); ++i) {
// for each subgraph // each subgraph is a hole????
std::vector<std::size_t> const & subgraph = subgraphs[i];
// neighboring facet of each vertex
std::map<std::size_t, std::set<std::size_t> > tmp;
for (std::size_t const face_id : subgraph) {
std::size_t const v0 = mesh_faces[face_id * 3];
std::size_t const v1 = mesh_faces[face_id * 3 + 1];
std::size_t const v2 = mesh_faces[face_id * 3 + 2];
tmp[v0].insert(face_id);
tmp[v1].insert(face_id);
tmp[v2].insert(face_id);
}
std::size_t const num_vertices = tmp.size();
/* Only fill small holes. */
if (num_vertices > 100) {
//std::cerr << "Hole to large" << std::endl;
continue;
}
/* Calculate 2D parameterization using the technique from libremesh/patch2d,
* which was published as sourcecode accompanying the following paper:
*
* Isotropic Surface Remeshing
* Simon Fuhrmann, Jens Ackermann, Thomas Kalbe, Michael Goesele
*/
std::size_t seed = -1;
std::vector<bool> is_border(num_vertices, false);
std::vector<std::vector<std::size_t> > adj_verts_via_border(num_vertices);
/* Index structures to map from local <-> global vertex id. */
std::map<std::size_t, std::size_t> g2l;
std::vector<std::size_t> l2g(num_vertices);
/* Index structure to determine column in matrix/vector. */
std::vector<std::size_t> idx(num_vertices);
std::size_t num_border_vertices = 0;
bool disk_topology = true;
std::map<std::size_t, std::set<std::size_t> >::iterator it = tmp.begin();
for (std::size_t j = 0; j < num_vertices; ++j, ++it) {
// index of the vertex
std::size_t vertex_id = it->first;
g2l[vertex_id] = j;
l2g[j] = vertex_id;
/* Check topology in original mesh. */
if (vertex_infos->at(vertex_id).vclass != core::VERTEX_CLASS_SIMPLE) {
//std::cerr << "Complex/Border vertex in original mesh" << std::endl;
disk_topology = false;
break;
}
/* Check new topology and determine if vertex is now at the border. */
std::vector<std::size_t> const & adj_faces = vertex_infos->at(vertex_id).faces;
std::set<std::size_t> const & adj_hole_faces = it->second;
std::vector<std::pair<std::size_t, std::size_t> > fan;
for (std::size_t k = 0; k < adj_faces.size(); ++k) {
std::size_t adj_face = adj_faces[k];
if (graph.get_label(adj_faces[k]) == 0 && adj_hole_faces.find(adj_face) != adj_hole_faces.end()) {
std::size_t curr = adj_faces[k];
std::size_t next = adj_faces[(k + 1) % adj_faces.size()];
std::pair<std::size_t, std::size_t> pair(curr, next);
fan.push_back(pair);
}
}
std::size_t gaps = 0;
for (std::size_t k = 0; k < fan.size(); k++) {
std::size_t curr = fan[k].first;
std::size_t next = fan[(k + 1) % fan.size()].first;
if (fan[k].second != next) {
++gaps;
for (std::size_t l = 0; l < 3; ++l) {
if(mesh_faces[curr * 3 + l] == vertex_id) {
std::size_t second = mesh_faces[curr * 3 + (l + 2) % 3];
adj_verts_via_border[j].push_back(second);
}
if(mesh_faces[next * 3 + l] == vertex_id) {
std::size_t first = mesh_faces[next * 3 + (l + 1) % 3];
adj_verts_via_border[j].push_back(first);
}
}
}
}
is_border[j] = gaps == 1;
/* Check if vertex is now complex. */
if (gaps > 1) {
//std::cerr << "Complex vertex in hole" << std::endl;
disk_topology = false;
break;
}
if (is_border[j]) {
idx[j] = num_border_vertices++;
seed = vertex_id;
} else {
idx[j] = j - num_border_vertices;
}
}
tmp.clear();
if (!disk_topology) continue;
if (num_border_vertices == 0) {
//std::cerr << "Genus zero topology" << std::endl;
continue;
}
//===================== get the border of this subgraph =====================================//
std::vector<std::size_t> border;
border.reserve(num_border_vertices);
std::size_t prev = seed;
std::size_t curr = seed;
while (prev == seed || curr != seed) {
std::size_t next = std::numeric_limits<std::size_t>::max();
std::vector<std::size_t> const & adj_verts = adj_verts_via_border[g2l[curr]];
for (std::size_t adj_vert : adj_verts) {
assert(is_border[g2l[adj_vert]]);
if (adj_vert != prev && adj_vert != curr) {
next = adj_vert;
break;
}
}
if (next != std::numeric_limits<std::size_t>::max()) {
prev = curr;
curr = next;
border.push_back(next);
} else {
//std::cerr << "No new border vertex" << std::endl;
border.clear();
break;
}
if (border.size() > num_border_vertices) {
//std::cerr << "Loop within border" << std::endl;
break;
}
}
if (border.size() != num_border_vertices) {
continue;
}
//============================compute the coordinates of the vertices of holes=========================//
// compute total length and total projection length
float total_length = 0.0f;
float total_projection_length = 0.0f;
for (std::size_t j = 0; j < border.size(); ++j) {
std::size_t vi0 = border[j];
std::size_t vi1 = border[(j + 1) % border.size()];
std::vector<VertexProjectionInfo> const & vpi0 = vertex_projection_infos->at(vi0);
std::vector<VertexProjectionInfo> const & vpi1 = vertex_projection_infos->at(vi0);
/* According to the previous checks (vertex class within the origial
* mesh and boundary) there already has to be at least one projection
* of each border vertex. */
assert(!vpi0.empty() && !vpi1.empty());
math::Vec2f vp0(0.0f), vp1(0.0f);
for (VertexProjectionInfo const & info0 : vpi0) {
for (VertexProjectionInfo const & info1 : vpi1) {
if (info0.texture_patch_id == info1.texture_patch_id) {
vp0 = info0.projection;
vp1 = info1.projection;
break;
}
}
}
total_projection_length += (vp0 - vp1).norm();
math::Vec3f const & v0 = vertices[vi0];
math::Vec3f const & v1 = vertices[vi1];
total_length += (v0 - v1).norm();
}
float radius = total_projection_length / (2.0f * MATH_PI);
float length = 0.0f;
std::vector<math::Vec2f> projections(num_vertices);
for (std::size_t j = 0; j < border.size(); ++j) {
float angle = 2.0f * MATH_PI * (length / total_length);
projections[g2l[border[j]]] = math::Vec2f(std::cos(angle), std::sin(angle));
math::Vec3f const & v0 = vertices[border[j]];
math::Vec3f const & v1 = vertices[border[(j + 1) % border.size()]];
length += (v0 - v1).norm();
}
typedef Eigen::Triplet<float, int> Triplet;
std::vector<Triplet> coeff;
std::size_t matrix_size = num_vertices - border.size();
Eigen::VectorXf xx(matrix_size), xy(matrix_size);
if (matrix_size != 0) {
Eigen::VectorXf bx(matrix_size);
Eigen::VectorXf by(matrix_size);
for (std::size_t j = 0; j < num_vertices; ++j) {
if (is_border[j]) continue;
std::size_t const vertex_id = l2g[j];
/* Calculate "Mean Value Coordinates" as proposed by Michael S. Floater */
std::map<std::size_t, float> weights;
// adjacent facets of each vertex
std::vector<std::size_t> const & adj_faces = vertex_infos->at(vertex_id).faces;
for (std::size_t adj_face : adj_faces) {
std::size_t v0 = mesh_faces[adj_face * 3];
std::size_t v1 = mesh_faces[adj_face * 3 + 1];
std::size_t v2 = mesh_faces[adj_face * 3 + 2];
if (v1 == vertex_id) std::swap(v1, v0);
if (v2 == vertex_id) std::swap(v2, v0);
math::Vec3f v01 = vertices[v1] - vertices[v0];
float v01n = v01.norm();
math::Vec3f v02 = vertices[v2] - vertices[v0];
float v02n = v02.norm();
float alpha = std::acos(v01.dot(v02) / (v01n * v02n));
weights[g2l[v1]] += std::tan(alpha / 2.0f) / v01n;
weights[g2l[v2]] += std::tan(alpha / 2.0f) / v02n;
}
// normalization of the weights
std::map<std::size_t, float>::iterator it;
float sum = 0.0f;
for (it = weights.begin(); it != weights.end(); ++it)
sum += it->second;
for (it = weights.begin(); it != weights.end(); ++it)
it->second /= sum;
bx[idx[j]] = 0.0f;
by[idx[j]] = 0.0f;
for (it = weights.begin(); it != weights.end(); ++it) {
if (is_border[it->first]) {
std::size_t border_vertex_id = border[idx[it->first]];
bx[idx[j]] += projections[g2l[border_vertex_id]][0] * it->second;
by[idx[j]] += projections[g2l[border_vertex_id]][1] * it->second;
} else {
coeff.push_back(Triplet(idx[j], idx[it->first], -it->second));
}
}
}
for (std::size_t j = 0; j < matrix_size; ++j) {
coeff.push_back(Triplet(j, j, 1.0f));
}
typedef Eigen::SparseMatrix<float> SpMat;
SpMat A(matrix_size, matrix_size);
A.setFromTriplets(coeff.begin(), coeff.end());
Eigen::SparseLU<SpMat> solver;
solver.analyzePattern(A);
solver.factorize(A);
xx = solver.solve(bx);
xy = solver.solve(by);
}
int image_size = std::floor(radius * 1.1f) * 2 + 4;
int scale = image_size / 2 - texture_patch_border;
for (std::size_t j = 0, k = 0; j < num_vertices; ++j) {
if (!is_border[j]) {
projections[j] = math::Vec2f(xx[k], xy[k]) * scale + image_size / 2;
++k;
} else {
projections[j] = projections[j] * scale + image_size / 2;
}
}
core::ByteImage::Ptr image = core::ByteImage::create(image_size, image_size, 3);
//DEBUG image->fill_color(*math::Vec4uc(0, 255, 0, 255));
std::vector<math::Vec2f> texcoords; texcoords.reserve(subgraph.size());
for (std::size_t const face_id : subgraph) {
for (std::size_t j = 0; j < 3; ++j) {
std::size_t const vertex_id = mesh_faces[face_id * 3 + j];
math::Vec2f const & projection = projections[g2l[vertex_id]];
texcoords.push_back(projection);
}
}
TexturePatch::Ptr texture_patch = TexturePatch::create(0, subgraph, texcoords, image);
std::size_t texture_patch_id;
#pragma omp critical
{
texture_patches->push_back(texture_patch);
texture_patch_id = num_patches++;
num_hole_faces += subgraph.size();
++num_holes;
}
for (std::size_t j = 0; j < num_vertices; ++j) {
std::size_t const vertex_id = l2g[j];
std::vector<std::size_t> const & adj_faces = vertex_infos->at(vertex_id).faces;
std::vector<std::size_t> faces;
faces.reserve(adj_faces.size());
for (std::size_t adj_face : adj_faces) {
if (graph.get_label(adj_face) == 0) {
faces.push_back(adj_face);
}
}
VertexProjectionInfo info = {texture_patch_id, projections[j], faces};
#pragma omp critical
vertex_projection_infos->at(vertex_id).push_back(info);
}
}
}
merge_vertex_projection_infos(vertex_projection_infos);
std::cout << "done. (Took " << timer.get_elapsed_sec() << "s)" << std::endl;
std::cout << "\t" << num_patches << " texture patches." << std::endl;
std::cout << "\t" << num_holes << " holes (" << num_hole_faces << " faces)." << std::endl;
}
TEX_NAMESPACE_END