-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.cpp
1882 lines (1690 loc) · 53.6 KB
/
helper.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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "helper.h"
#include <algorithm>
#include <queue>
#include <map>
#include <igl/gaussian_curvature.h>
#include <igl/massmatrix.h>
#include <igl/cotmatrix.h>
#include <igl/invert_diag.h>
#include <igl/readOFF.h>
#include "MaxFlow.h"
#define DENOMINATOR_EPS 1e-6
typedef TinyVector<double, 3> vec3d;
void greedy_graph_coloring(const size_t num_vertices, const std::vector<std::set<size_t>>& edges, std::vector<std::vector<size_t>>& colored_vertices)
{
std::vector<int> result(num_vertices, -1);
result[0] = 0;
std::vector<bool> available(num_vertices, false);
int max_color = 0;
for (size_t u = 1; u < num_vertices; u++)
{
for (typename std::set<size_t>::iterator iter = edges[u].begin(); iter != edges[u].end(); iter++)
{
if (result[*iter] != -1)
available[result[*iter]] = true;
}
int cr;
for (cr = 0; cr < (int)num_vertices; cr++)
{
if (available[cr] == false)
break;
}
result[u] = cr;
max_color = std::max(cr, max_color);
for (typename std::set<size_t>::iterator iter = edges[u].begin(); iter != edges[u].end(); iter++)
{
if (result[*iter] != -1)
available[result[*iter]] = false;
}
}
colored_vertices.resize(max_color + 1);
for (size_t i = 0; i < num_vertices; i++)
{
colored_vertices[result[i]].push_back(i);
}
}
void get_graph_component(const std::vector<std::set<size_t>>& edges, std::vector<std::set<size_t>>& components)
{
//edges id starting from 0
components.clear();
std::vector<int> vert2color(edges.size(), -1);
int cid = 0;
int startid = -1;
for (size_t i = 0; i < edges.size(); i++)
{
if (!edges[i].empty())
{
startid = i;
break;
}
}
assert(startid != -1);
while (startid != -1)
{
std::set<size_t> onecluser;
std::queue<size_t> q;
q.push(startid);
vert2color[startid] = cid;
while (!q.empty())
{
size_t front = q.front();
q.pop();
onecluser.insert(front);
for (auto v : edges[front])
{
if (vert2color[v] == -1)
{
q.push(v);
vert2color[v] = cid;
}
}
}
components.push_back(onecluser);
cid++;
startid = -1;
for (size_t i = 0; i < edges.size(); i++)
{
if (!edges[i].empty() && vert2color[i] == -1)
{
startid = i;
break;
}
}
}
}
void compute_vert_mean_curvature(const std::vector<TinyVector<double, 3>>& pos, const std::vector<TinyVector<size_t, 3>>& faces, std::vector<double>& curvature)
{
using namespace Eigen;
using namespace std;
MatrixXd V;
MatrixXi F;
curvature.clear();
curvature.resize(pos.size(), 0.0);
//igl::readOFF(TUTORIAL_SHARED_PATH "/bumpy.off",V,F);
//igl::readOFF("E:\\code\\PaperCode\\VolumeMeshProcessing\\x64\\Release\\AllModels\\fandisk\\fandisk.off", V, F);
//remove dup verts
std::vector<size_t> o2n(pos.size(), (size_t)-1), n2o;
size_t count = 0;
for (size_t i = 0; i < faces.size(); i++)
{
for (size_t j = 0; j < 3; j++)
{
size_t id = faces[i][j];
if (o2n[id] == (size_t)-1)
{
o2n[id] = count;
count = count + 1;
n2o.push_back(id);
}
}
}
std::vector<std::vector<double>> vV(n2o.size(), std::vector<double>(3));
std::vector<std::vector<int>> vF(faces.size(), std::vector<int>(3));
for (size_t i = 0; i < n2o.size(); i++)
{
//V(3 *i, 1) = pos[i][0];
/*V(3 * i + 1) = pos[i][1];
V(3 * i + 2) = pos[i][2];*/
for (size_t j = 0; j < 3; j++)
{
vV[i][j] = pos[n2o[i]][j];
}
}
for (size_t i = 0; i < faces.size(); i++)
{
/*F(3 * i) = faces[i][0];
F(3 * i + 1) = faces[i][1];
F(3 * i + 2) = faces[i][2];*/
for (size_t j = 0; j < 3; j++)
{
vF[i][j] = o2n[faces[i][j]];
}
}
igl::list_to_matrix(vV, V);
igl::list_to_matrix(vF, F);
std::cout << "V size: " << V.size() << std::endl;
std::cout << "F size: " << F.size() << std::endl;
std::cout << "pos & faces size: " << pos.size() << " " << faces.size() << std::endl;
SparseMatrix<double> M, Minv;
MatrixXd HN;
SparseMatrix<double> L;
igl::cotmatrix(V, F, L);
igl::massmatrix(V, F, igl::MASSMATRIX_TYPE_VORONOI, M);
igl::invert_diag(M, Minv);
HN = -Minv * (L * V);
VectorXd H = HN.rowwise().norm(); //up to sign
std::cout << "H min max: " << H.minCoeff() << " " << H.maxCoeff() << std::endl;
for (size_t i = 0; i < n2o.size(); i++)
{
curvature[n2o[i]] = H[i];
}
}
void update_tree_color(const std::vector<size_t>& new_color, TreeNode<size_t>* t)
{
std::set<size_t> new_keys;
for (auto v : t->keys)
{
new_keys.insert(new_color[v]);
}
t->keys = new_keys;
for (size_t i = 0; i < t->children.size(); i++)
{
update_tree_color(new_color, t->children[i]);
}
}
bool get_tree_from_convex_graph(const std::vector<std::set<size_t>> &graph , const std::map<std::pair<size_t, size_t>, int>& flag_fpconvex, bool flag_convex_bool, TreeNode<size_t>* tn, int layer, std::vector<size_t> &invalid_cluster)
{
//set tn
int target_convex = 2 - (int)flag_convex_bool;
std::set<size_t> counter_nodes;
for (size_t i = 0; i < graph.size(); i++)
{
for (auto v : graph[i])
{
if (v > i)
{
auto it = flag_fpconvex.find(std::pair<size_t, size_t>(i, v));
assert(it != flag_fpconvex.end());
if (it->second != 0 && it->second != target_convex)
{
counter_nodes.insert(i);
counter_nodes.insert(v);
}
}
}
}
if (!counter_nodes.empty())
{
std::vector<std::set<size_t>> counter_graph(graph.size()); //graph made of counter nodes
for (auto cv : counter_nodes)
{
for (auto cvn : graph[cv])
{
if (std::find(counter_nodes.begin(), counter_nodes.end(), cvn) != counter_nodes.end())
{
auto it = flag_fpconvex.find(std::pair<size_t, size_t>(std::min(cv, cvn), std::max(cv, cvn)));
assert(it != flag_fpconvex.end());
if (it->second != target_convex) //including smooth one
counter_graph[cv].insert(cvn);
}
}
}
std::vector<std::set<size_t>> counter_clusters;
get_graph_component(counter_graph, counter_clusters);
//set tn.children
//set nodes that are not counter nodes to tn.keys
for (size_t i = 0; i < graph.size(); i++)
{
if (!graph[i].empty() && std::find(counter_nodes.begin(), counter_nodes.end(), i) == counter_nodes.end())
{
tn->keys.insert(i);
}
}
//one more criteria: cluster == 1 and no other component
if (tn->keys.size() == 0 && counter_clusters.size() == 1)
{
//assume only one cluster is invalid
std::cout << "tree construction failed" << std::endl;
//exit(EXIT_FAILURE);
invalid_cluster = std::vector<size_t>(counter_clusters[0].begin(), counter_clusters[0].end());
return false;
}
for (size_t i = 0; i < counter_clusters.size(); i++)
{
std::vector<std::set<size_t>> subgraph(graph.size());
for (auto v : counter_clusters[i])
{
for (auto vn : graph[v])
{
if (std::find(counter_clusters[i].begin(), counter_clusters[i].end(), vn) != counter_clusters[i].end())
{
subgraph[v].insert(vn);
}
}
}
TreeNode<size_t>* child = new TreeNode<size_t>;
//if (layer == 10)
//{
// std::cout << "Layers over 10!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
// //exit(EXIT_FAILURE);
// return false;
//}
bool tmp_flag = get_tree_from_convex_graph(subgraph, flag_fpconvex, !flag_convex_bool, child, layer + 1, invalid_cluster);
if (!tmp_flag)
{
return false;
}
tn->children.push_back(child);
}
}
else
{
//set nodes that are not counter nodes to tn.keys
for (size_t i = 0; i < graph.size(); i++)
{
if (!graph[i].empty() && std::find(counter_nodes.begin(), counter_nodes.end(), i) == counter_nodes.end())
{
tn->keys.insert(i);
}
}
}
return true;
}
using namespace MeshLib;
void get_mesh_vert_faces(Mesh3d& mesh, std::vector<std::array<double, 3>>& pos, std::vector<std::vector<size_t>>& faces)
{
pos.clear();
faces.clear();
for (size_t i = 0; i < mesh.get_faces_list()->size(); i++)
{
HE_edge<double>* begin_edge = mesh.get_faces_list()->at(i)->edge;
HE_edge<double>* edge = mesh.get_faces_list()->at(i)->edge;
std::vector<size_t> oneface;
do
{
//tri_verts[i][local_id++] = edge->pair->vert->id;
oneface.push_back(edge->pair->vert->id);
edge = edge->next;
} while (edge != begin_edge);
//mesh.get_faces_list()->at(fid)->normal;
faces.push_back(oneface);
}
//std::vector<TinyVector<double, 3>> vert_pos;
for (size_t i = 0; i < mesh.get_vertices_list()->size(); i++)
{
//vert_pos.push_back(mesh.get_vertices_list()->at(i)->pos);
vec3d one_pt = mesh.get_vertices_list()->at(i)->pos;
pos.push_back(std::array<double, 3>({ one_pt[0], one_pt[1], one_pt[2] }));
}
}
int get_mesh_edge_type(Mesh3d& m, int heid) //0: smooth, 1: convex, 2: concave
{
HE_edge<double>* e1 = m.get_edges_list()->at(heid);
HE_edge<double>* e2 = e1->pair;
if (e1->face == NULL || e2->face == NULL)
{
return 0;
}
double product = e1->face->normal.Dot(e2->next->vert->pos - e1->next->vert->pos);
double tmp_cos = e1->face->normal.Dot(e2->face->normal);
int res = 0;
if (product < 0.0)
{
if (tmp_cos < th_smooth_cos_value)
res = 1;
}
else
{
if (tmp_cos < th_smooth_cos_value)
{
res = 2;
}
}
return res;
}
void get_all_tri_area(Mesh3d& mesh, std::vector<double>& tri_area)
{
tri_area.clear();
tri_area.resize(mesh.get_num_of_faces(), 0.0);
std::vector<TinyVector<size_t, 3>> tri_verts(mesh.get_faces_list()->size());
//std::vector<TinyVector<double, 3>> tri_normals;
//get tri list
for (size_t i = 0; i < mesh.get_faces_list()->size(); i++)
{
HE_edge<double>* begin_edge = mesh.get_faces_list()->at(i)->edge;
HE_edge<double>* edge = mesh.get_faces_list()->at(i)->edge;
int local_id = 0;
do
{
tri_verts[i][local_id++] = edge->pair->vert->id;
edge = edge->next;
} while (edge != begin_edge);
//mesh.get_faces_list()->at(fid)->normal;
//tri_normals.push_back(mesh.get_faces_list()->at(i)->normal);
}
std::vector<TinyVector<double, 3>> vert_pos;
for (size_t i = 0; i < mesh.get_vertices_list()->size(); i++)
{
vert_pos.push_back(mesh.get_vertices_list()->at(i)->pos);
}
for (size_t i = 0; i < tri_area.size(); i++)
{
//tri_area[i] = std::abs(compute_tri_area<double>(tri_verts[tri_faces[i][0]], tri_verts[tri_faces[i][1]], tri_verts[tri_faces[i][2]]));
//total_tri_area += tri_area[i];
tri_area[i] = std::abs(compute_tri_area<double>(vert_pos[tri_verts[i][0]], vert_pos[tri_verts[i][1]], vert_pos[tri_verts[i][2]]));
}
}
bool dijkstra_mesh(Mesh3d* m, size_t start_vert, std::vector<size_t>& prev_map, std::vector<double>& dist)
{
prev_map.clear();
dist.clear();
dist.resize(m->get_num_of_vertices(), DBL_MAX);
prev_map.resize(m->get_num_of_vertices(), 0);
dist[start_vert] = 0.0;
std::set<std::pair<double, HE_vert<double>*>> vqueue;
for (size_t i = 0; i < m->get_num_of_vertices(); i++)
{
HE_vert<double>* hv = m->get_vertex(i);
vqueue.insert(std::pair<double, HE_vert<double>*>(dist[hv->id], hv));
}
while (!vqueue.empty())
{
std::set<std::pair<double, HE_vert<double>*>>::iterator iter = vqueue.begin();
HE_vert<double>* u = iter->second;
vqueue.erase(iter);
if (u->edge == NULL)
continue;
HE_edge<double>* he = u->edge;
do
{
double alt = dist[u->id] + (u->pos - he->vert->pos).Length();
if (alt < dist[he->vert->id])
{
vqueue.erase(std::pair<double, HE_vert<double>*>(dist[he->vert->id], he->vert));
dist[he->vert->id] = alt;
prev_map[he->vert->id] = u->id;
vqueue.insert(std::pair<double, HE_vert<double>*>(dist[he->vert->id], he->vert));
}
he = he->pair->next;
} while (he != u->edge);
}
return true;
}
void get_he_list_dijkstra(Mesh3d& m, size_t start_vert, size_t end_vert, const std::vector<size_t>& prev_map, std::vector<size_t>& he_list)
{
//get he list for path start_vert -> end_vert
he_list.clear();
size_t cur = end_vert;
while (cur != start_vert)
{
size_t prev = prev_map[cur];
HE_edge<double>* start_edge = m.get_vertex(cur)->edge;
HE_edge<double>* edge = m.get_vertex(cur)->edge;
do
{
if (edge->vert->id == prev)
{
he_list.push_back(edge->id);
he_list.push_back(edge->pair->id);
break;
}
edge = edge->pair->next;
} while (edge != start_edge);
cur = prev;
}
}
void repair_tree_features(Mesh3d& m, const std::vector<int>& face_color, const std::vector<size_t>& invalid_colors, std::vector<std::pair<int, int>>& ungrouped_features)
{
//get sub mesh firstly
std::vector<bool> invalid_color_map(face_color.size(), false);
for (auto cid : invalid_colors)
//invalid_color_set.insert(cid);
invalid_color_map[cid] = true;
std::vector<std::array<double, 3>> all_pts;
std::vector<std::vector<size_t>> all_faces, invalid_faces;
get_mesh_vert_faces(m, all_pts, all_faces);
for (size_t i = 0; i < face_color.size(); i++)
{
if (invalid_color_map[face_color[i] - 1] == true)
{
invalid_faces.push_back(all_faces[i]);
}
}
Mesh3d sub_mesh;
sub_mesh.load_mesh(all_pts, invalid_faces);
sub_mesh.write_obj("invalid.obj");
//to change: ungrouped features
//step1: load feature, step2: select convex-concavefeature, step3: add shorted path to other corner points or boundary
std::vector<std::vector<int>> feature_v2he(sub_mesh.get_num_of_vertices()); //id of hes ematating from each vertex
std::vector<std::pair<int, int>> ungrouped_features_new;
std::vector<bool> flag_he_feature(sub_mesh.get_num_of_edges(), false);
for (size_t i = 0; i < ungrouped_features.size(); i++)
{
int id0 = ungrouped_features[i].first;
int id1 = ungrouped_features[i].second;
//feature_degree_v[id0]++;
//feature_degree_v[id1]++;
HE_edge<double>* begin_edge = sub_mesh.get_vertices_list()->at(id0)->edge;
HE_edge<double>* edge = sub_mesh.get_vertices_list()->at(id0)->edge;
bool flag_found = false;
//if (edge != NULL && !sub_mesh.is_on_boundary(edge)) //not boundary
if (edge != NULL)
{
do
{
if (id1 == edge->vert->id)
{
feature_v2he[id0].push_back(edge->id);
feature_v2he[id1].push_back(edge->pair->id);
flag_he_feature[edge->id] = true;
flag_he_feature[edge->pair->id] = true;
flag_found = true;
break;
}
edge = edge->pair->next;
} while (edge != begin_edge);
}
//assert(flag_found == true);
if (flag_found == true)
{
ungrouped_features_new.push_back(ungrouped_features[i]);
}
}
//for debugging: visualize flag_he_feature
//auto flag_he_feature_debug = flag_he_feature;
//std::vector<std::pair<int, int>> debug_feas;
//std::ofstream ofs("debug.fea");
//for (size_t i = 0; i < flag_he_feature.size(); i++)
//{
// if (flag_he_feature_debug[i])
// {
// flag_he_feature_debug[sub_mesh.get_edge(i)->pair->id] = false;
// //ofs << sub_mesh.get_edge(i)->vert->id << " " << sub_mesh.get_edge(i)->pair->vert->id << std::endl;
// debug_feas.push_back(std::pair<int, int>(sub_mesh.get_edge(i)->vert->id, sub_mesh.get_edge(i)->pair->vert->id));
// }
//}
//ofs << debug_feas.size() << std::endl;
//for (auto& pp : debug_feas)
//{
// ofs << pp.first << " " << pp.second << std::endl;
//}
//ofs.close();
//int max_v_degree = 0;
std::vector<int> feature_vtype(sub_mesh.get_num_of_vertices(), 0); //0: normal case, 1: corner, 2: turn vertex
std::vector<int> feature_vcolor(sub_mesh.get_num_of_vertices(), false);
for (size_t i = 0; i < feature_v2he.size(); i++)
{
/*if (max_v_degree < feature_v2he[i].size())
max_v_degree = feature_v2he[i].size();*/
if (feature_v2he[i].size() == 1)
{
feature_vtype[i] = 1;
//feature_vcolor[i] = true;
}
else if (feature_v2he[i].size() >= 3)
{
//normal or turn
int convex_count = 0, concave_count = 0;
for (auto heid : feature_v2he[i])
{
int hetype = get_mesh_edge_type(sub_mesh, heid);
if (hetype == 1)
convex_count += 1;
if (hetype == 2)
concave_count += 1;
}
if (convex_count > 0 && concave_count > 0)
{
feature_vtype[i] = 2; //turn
//std::cout << "turn vertex id: " << i << std::endl;
}
else
{
feature_vtype[i] = 1;
}
}
}
std::set<int> ep_cand; //corner or boundary
for (size_t i = 0; i < feature_v2he.size(); i++)
{
if (feature_vtype[i] > 0)
ep_cand.insert(i);
}
/*for (size_t i = 0; i < sub_mesh.get_num_of_vertices(); i++)
{
if (sub_mesh.is_on_boundary(sub_mesh.get_vertex(i)))
{
ep_cand.insert(i);
}
}*/
for (size_t i = 0; i < sub_mesh.get_num_of_edges(); i++)
{
if (sub_mesh.is_on_boundary(sub_mesh.get_edge(i)))
{
ep_cand.insert(sub_mesh.get_edge(i)->vert->id);
ep_cand.insert(sub_mesh.get_edge(i)->pair->vert->id);
}
}
//add features for each turn vertex
//std::vector<bool> flag_he_feature_add = flag_he_feature;
std::vector<bool> flag_he_feature_add(flag_he_feature.size(), false);
for (size_t i = 0; i < feature_v2he.size(); i++)
{
if (feature_vtype[i] == 2 && !feature_vcolor[i])
{
std::vector<size_t> prev_map;
std::vector<double> dist;
dijkstra_mesh(&sub_mesh, i, prev_map, dist);
int target = -1;
std::vector<std::pair<double, int>> dist_ep;
for (auto ep : ep_cand)
{
if (ep != i)
dist_ep.push_back(std::pair<double, int>(dist[ep], ep));
}
std::sort(dist_ep.begin(), dist_ep.end());
for (size_t j = 0; j < dist_ep.size(); j++)
{
int cur_vert = dist_ep[j].second;
bool path_valid = false; //if one of the edge in the path is not feature, set as true
std::vector<size_t> he_list;
get_he_list_dijkstra(sub_mesh, i, cur_vert, prev_map, he_list);
for (auto he : he_list)
{
if (flag_he_feature[he] == false)
path_valid = true;
}
if (path_valid)
{
//update flag_he_feature_add
for (auto he : he_list)
{
flag_he_feature_add[he] = true;
}
target = cur_vert;
break;
}
}
assert(target != -1);
feature_vcolor[i] = true;
//target also set as true
feature_vcolor[target] = true;
}
}
//update ungrouped_features
std::set<std::pair<int, int>> ungrouped_features_set;
for (auto& pp : ungrouped_features)
{
ungrouped_features_set.insert(std::pair<int, int>(std::min(pp.first, pp.second), std::max(pp.first, pp.second)));
}
//ungrouped_features.clear();
for (size_t i = 0; i < flag_he_feature_add.size(); i++)
{
if (flag_he_feature_add[i])
{
//set opposite as true
flag_he_feature_add[sub_mesh.get_edge(i)->pair->id] = false;
int v1 = sub_mesh.get_edge(i)->vert->id, v2 = sub_mesh.get_edge(i)->pair->vert->id;
ungrouped_features_set.insert(std::pair<int, int>(std::min(v1, v2), std::max(v1, v2)));
//ungrouped_features.push_back(std::pair<int, int>(sub_mesh.get_edge(i)->vert->id, sub_mesh.get_edge(i)->pair->vert->id));
}
}
//ungrouped_features.clear();
ungrouped_features = std::vector<std::pair<int, int>>(ungrouped_features_set.begin(), ungrouped_features_set.end());
}
void repair_tree_features_maxflow(Mesh3d& m, const std::vector<int>& face_color, const std::vector<size_t>& invalid_colors, std::vector<std::pair<int, int>>& ungrouped_features)
{
//bool flag_debug = true;
bool flag_debug = false;
std::set<std::pair<int, int>> ungrouped_features_set;
for (auto& pp : ungrouped_features)
{
ungrouped_features_set.insert(std::pair<int, int>(std::min(pp.first, pp.second), std::max(pp.first, pp.second)));
}
std::vector<std::array<double, 3>> all_pts;
std::vector<std::vector<size_t>> all_faces, all_invalid_faces;
get_mesh_vert_faces(m, all_pts, all_faces);
std::vector<bool> invalid_color_map(face_color.size(), false);
for (auto cid : invalid_colors)
//invalid_color_set.insert(cid);
invalid_color_map[cid] = true;
for (size_t i = 0; i < face_color.size(); i++)
{
if (invalid_color_map[face_color[i] - 1])
{
all_invalid_faces.push_back(all_faces[i]);
}
}
Mesh3d invalid_mesh;
invalid_mesh.load_mesh(all_pts, all_invalid_faces);
if (flag_debug)
{
invalid_mesh.write_obj("invalid_all.obj");
}
std::map<std::pair<int, int>, int> edgepair2type;
for (size_t i = 0; i < ungrouped_features.size(); i++)
{
int id0 = ungrouped_features[i].first;
int id1 = ungrouped_features[i].second;
HE_edge<double>* begin_edge = invalid_mesh.get_vertices_list()->at(id0)->edge;
HE_edge<double>* edge = invalid_mesh.get_vertices_list()->at(id0)->edge;
//if (edge != NULL && !invalid_mesh.is_on_boundary(edge)) //not boundary
if (edge != NULL)
{
do
{
if (id1 == edge->vert->id)
{
int edge_type = get_mesh_edge_type(invalid_mesh, edge->id); //if boundary, type is 0
edgepair2type[ungrouped_features[i]] = edge_type;
break;
}
edge = edge->pair->next;
} while (edge != begin_edge);
}
}
bool flag_patchbypatch = true;
//flip edge
std::vector<std::pair<int, int>> flipped_edges;
std::vector<int> flipped_corner;
//repair patch by patch
if (flag_patchbypatch)
{
for (auto cid : invalid_colors)
{
std::vector<std::vector<size_t>> cur_faces;
for (size_t i = 0; i < face_color.size(); i++)
{
if (face_color[i] - 1 == cid)
{
cur_faces.push_back(all_faces[i]);
}
}
Mesh3d sub_mesh;
sub_mesh.load_mesh(all_pts, cur_faces);
if (flag_debug)
sub_mesh.write_obj("curpatch.obj");
//submesh might need to be adjusted
std::vector<bool> flag_he_feature(sub_mesh.get_num_of_edges(), false);
std::vector<int> flag_he_type(sub_mesh.get_num_of_edges(), 0);
for (size_t i = 0; i < ungrouped_features.size(); i++)
{
int id0 = ungrouped_features[i].first;
int id1 = ungrouped_features[i].second;
HE_edge<double>* begin_edge = sub_mesh.get_vertices_list()->at(id0)->edge;
HE_edge<double>* edge = sub_mesh.get_vertices_list()->at(id0)->edge;
bool flag_found = false;
//if (edge != NULL && !sub_mesh.is_on_boundary(edge)) //not boundary
if (edge != NULL)
{
do
{
if (id1 == edge->vert->id)
{
flag_he_feature[edge->id] = true;
flag_he_feature[edge->pair->id] = true;
//int edge_type = get_mesh_edge_type(sub_mesh, edge->id);
int edge_type = edgepair2type[ungrouped_features[i]];
flag_he_type[edge->id] = edge_type;
flag_he_type[edge->pair->id] = edge_type;
flag_found = true;
break;
}
edge = edge->pair->next;
} while (edge != begin_edge);
}
}
//check mesh validness: a triangle should not have two nb edge with different convexity
bool flag_mesh_valid = true;
for (size_t i = 0; i < sub_mesh.get_num_of_faces(); i++)
{
if (sub_mesh.is_on_boundary(sub_mesh.get_face(i)))
{
int convex_count = 0, concave_count = 0;
HE_edge<double>* edge = sub_mesh.get_face(i)->edge;
do
{
if (flag_he_feature[edge->id])
{
if (flag_he_type[edge->id] == 1)
{
convex_count += 1;
}
else if (flag_he_type[edge->id] == 2)
{
concave_count += 1;
}
}
edge = edge->next;
} while (edge != sub_mesh.get_face(i)->edge);
if (convex_count > 0 && concave_count > 0)
{
flag_mesh_valid = false;
//edge flip
edge = sub_mesh.get_face(i)->edge; //find no feature curve
do
{
if (!flag_he_feature[edge->id])
{
break;
}
edge = edge->next;
} while (edge != sub_mesh.get_face(i)->edge);
if (sub_mesh.is_on_boundary(edge))
{
std::cout << "all 3 edge of triangle " << i << " are features" << std::endl;
}
else
{
int v0 = edge->vert->id, v1 = edge->pair->vert->id;
flipped_edges.push_back(std::pair<int, int>(std::min(v0, v1), std::max(v0, v1)));
flipped_corner.push_back(edge->next->vert->id);
int oppo_face_id = edge->pair->face->id;
std::vector<size_t> newf1, newf2;
//edge flip
newf1.push_back(edge->vert->id);
newf1.push_back(edge->next->vert->id);
newf1.push_back(edge->pair->next->vert->id);
edge = edge->pair;
newf2.push_back(edge->vert->id);
newf2.push_back(edge->next->vert->id);
newf2.push_back(edge->pair->next->vert->id);
cur_faces[i] = newf1;
cur_faces[oppo_face_id] = newf2;
}
}
}
}
if (!flag_mesh_valid)
{
std::cout << "input mesh for patch " << cid << " is invalid" << std::endl;
/*Mesh3d new_mesh;
new_mesh.load_mesh(all_pts, cur_faces);
std::swap(sub_mesh, new_mesh);*/
sub_mesh.load_mesh(all_pts, cur_faces);
if (flag_debug)
sub_mesh.write_obj("invalidpatch_repair.obj");
//submesh might need to be adjusted
flag_he_feature.clear();
flag_he_type.clear();
flag_he_feature.resize(sub_mesh.get_num_of_edges(), false);
flag_he_type.resize(sub_mesh.get_num_of_edges(), 0);
for (size_t i = 0; i < ungrouped_features.size(); i++)
{
int id0 = ungrouped_features[i].first;
int id1 = ungrouped_features[i].second;
//feature_degree_v[id0]++;
//feature_degree_v[id1]++;
HE_edge<double>* begin_edge = sub_mesh.get_vertices_list()->at(id0)->edge;
HE_edge<double>* edge = sub_mesh.get_vertices_list()->at(id0)->edge;
bool flag_found = false;
//if (edge != NULL && !sub_mesh.is_on_boundary(edge)) //not boundary
if (edge != NULL)
{
do
{
if (id1 == edge->vert->id)
{
flag_he_feature[edge->id] = true;
flag_he_feature[edge->pair->id] = true;
//int edge_type = get_mesh_edge_type(sub_mesh, edge->id);
int edge_type = edgepair2type[ungrouped_features[i]];
flag_he_type[edge->id] = edge_type;
flag_he_type[edge->pair->id] = edge_type;
flag_found = true;
break;
}
edge = edge->pair->next;
} while (edge != begin_edge);
}
}
}
//mesh constructed, build max cut algorithm
int n_node = sub_mesh.get_num_of_faces() + 2;
std::set<int> convex_node, concave_node;
for (size_t i = 0; i < sub_mesh.get_num_of_edges(); i++)
{
if (!flag_he_feature[i]) continue;
if (flag_he_type[i] == 1)
{
//convex
if (sub_mesh.get_edge(i)->face != NULL)
{
convex_node.insert(sub_mesh.get_edge(i)->face->id);
}
}
else if (flag_he_type[i] == 2)
{
//concave
if (sub_mesh.get_edge(i)->face != NULL)
{
concave_node.insert(sub_mesh.get_edge(i)->face->id);
}
}
}
assert(!convex_node.empty() && !concave_node.empty());
if (!convex_node.empty() && !concave_node.empty())
{
int s = sub_mesh.get_num_of_faces();
int t = s + 1;
double max_cap = 1.0 * sub_mesh.get_num_of_faces();
std::vector<std::pair<int, int>> graph_edges;
std::vector<double> graph_edgecap;
//s to convex
for (auto node : convex_node)
{
graph_edges.push_back(std::pair<int, int>(s, node));
graph_edgecap.push_back(max_cap);
}
//concave to t
for (auto node : concave_node)
{
graph_edges.push_back(std::pair<int, int>(node, t));
graph_edgecap.push_back(max_cap);
}
if (flag_debug)
{
std::vector<std::vector<size_t>> convex_faces, concave_faces;
for (auto node : convex_node)
{
convex_faces.push_back(cur_faces[node]);
}
for (auto node : concave_node)
{
concave_faces.push_back(cur_faces[node]);
}
Mesh3d convexmesh, concavemesh;
convexmesh.load_mesh(all_pts, convex_faces);
concavemesh.load_mesh(all_pts, concave_faces);
convexmesh.write_obj("convex.obj");
concavemesh.write_obj("concave.obj");
}
//other parts
for (size_t i = 0; i < sub_mesh.get_num_of_edges(); i++)
{
if (!sub_mesh.is_on_boundary(sub_mesh.get_edge(i)) && flag_he_type[i] == 0)
{
//not boundary, not convex or concave
graph_edges.push_back(std::pair<int, int>(sub_mesh.get_edge(i)->face->id, sub_mesh.get_edge(i)->pair->face->id));
graph_edgecap.push_back(1.0);
}
}