-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.cpp
1549 lines (1373 loc) · 51.3 KB
/
Source.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 <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <set>
#include <random>
#include <algorithm>
#include <direct.h>
#include "cxxopts.hpp"
#include "Mesh3D.h"
#include "helper.h"
#include "happly.h"
#include "Tree.h"
using namespace MeshLib;
#define MIN_PATCH_AREA 1e-4
std::string GetFileExtension(const std::string& FileName)
{
if (FileName.find_last_of(".") != std::string::npos)
return FileName.substr(FileName.find_last_of(".") + 1);
return "";
}
bool load_feature_file(const char* filename, std::vector<std::pair<int, int>>& ungrouped_feature)
{
ungrouped_feature.clear();
std::ifstream ifs(filename);
if (!ifs.is_open())
{
std::cout << "Cannot Open Input Feature File" << std::endl;
return false;
}
//fea file: first part
int pair_size = 0;
ifs >> pair_size;
std::pair<int, int> tmp_pair(-1, -1);
for (size_t i = 0; i < pair_size; i++)
{
ifs >> tmp_pair.first >> tmp_pair.second;
ungrouped_feature.push_back(tmp_pair);
}
//not consider grouped feature
return true;
}
void save_feature_file(const char* filename, const std::vector<std::pair<int, int>>& ungrouped_feature)
{
std::ofstream ofs(filename);
ofs << ungrouped_feature.size() << std::endl;
for (size_t i = 0; i < ungrouped_feature.size(); i++)
{
ofs << ungrouped_feature[i].first << " " << ungrouped_feature[i].second << std::endl;
}
ofs.close();
}
void save_conf_file(const char* filename, const std::string str, bool flag_convex = true)
{
std::ofstream ofs(filename);
ofs << "csg{\n list = ";
ofs << str << std::endl;
ofs << " flag_convex = " << int(flag_convex) << "," << std::endl;
ofs << "}";
ofs.close();
}
int check_mesh_edge_convex(Mesh3d* m, HE_edge<double>* he)
{
//return 0: smooth, 1: convex, 2: concave
int hetri_vertidsum = 0, hepairtri_vertidsum = 0;
HE_edge<double>* begin_edge = he;
HE_edge<double>* edge = he;
do
{
hetri_vertidsum += edge->vert->id;
edge = edge->next;
} while (edge != begin_edge);
begin_edge = he->pair;
edge = he->pair;
do
{
hepairtri_vertidsum += edge->vert->id;
edge = edge->next;
} while (edge != begin_edge);
int hetri_otherid = hetri_vertidsum - he->vert->id - he->pair->vert->id;
int hepairtri_otherid = hepairtri_vertidsum - he->vert->id - he->pair->vert->id;
double product = he->face->normal.Dot(m->get_vertices_list()->at(hetri_otherid)->pos - m->get_vertices_list()->at(hepairtri_otherid)->pos);
double face_cos_value = he->face->normal.Dot(he->pair->face->normal);
int res = 0;
if (product > 0.0)
{
if (face_cos_value < th_smooth_cos_value)
res = 1;
//return true;
}
else
{
if (face_cos_value < th_smooth_cos_value)
res = 2;
//return false;
}
return res;
}
int main(int argc, char** argv)
{
//select model firstly: 0 for normalization and 1 for sampling
try
{
cxxopts::Options options("FeaturedModelPointSample", "Point Sampling program for featured CAD models (author: Haoxiang Guo, Email: guohaoxiangxiang@gmail.com)");
options
.positional_help("[optional args]")
.show_positional_help()
.allow_unrecognised_options()
.add_options()
("i,input", "input mesh (obj/off format)", cxxopts::value<std::string>())
("f,feature", "input feature file (fea format)", cxxopts::value<std::string>())
("p,pointcloud", "input pointcloud", cxxopts::value<std::string>())
("pf", "face of input pointcloud", cxxopts::value<std::string>())
("o,output", "output mesh/points (obj/off/points/xyz format)", cxxopts::value<std::string>())
("k,mask", "output mask file (txt format)", cxxopts::value<std::string>())
("fs", "number of samples on feature edges(default: 10000)", cxxopts::value<int>())
("ns", "number of samples on non-feature faces(default: 40000)", cxxopts::value<int>())
("m,mode", "processing mode: 0 for normalization and 1 for feature sample", cxxopts::value<int>())
("c,color", "whether coloring is used, 0: not used, 1: used, default: 0", cxxopts::value<int>())
("mp", "maximum number of patches in each colored cluster, only work for csg, default -1(no upper bound)", cxxopts::value<int>())
("cot", "whether cotangent weight is used for sampling, 0: not used, 1: used, default: 0", cxxopts::value<int>())
("s,sigma", "sigma for noisy points position, default 0.0", cxxopts::value<double>())
("sn", "sigma for noisy points normal in degrees, default 0.0", cxxopts::value<double>())
("csg", "whether generating csg tree for model, default: 0", cxxopts::value<int>())
("convex", "whether the first layer is convex, default: 0", cxxopts::value<int>())
("r,repair", "whether the turn vertex are repaired, default: 1", cxxopts::value<int>())
("verbose", "verbose setting, default: 0", cxxopts::value<int>())
("strict", "treat all edges as either strictly convex or concave")
("repairtree", "repair tree feature")
("h,help", "print help");
auto result = options.parse(argc, argv);
if (result.count("help"))
{
std::cout << options.help({ "", "Group" }) << std::endl;
exit(0);
}
int n_nonfeature_sample = 50000;
int n_feature_sample = 0;
int min_sample_perpatch = 50;
double sigma = -1.0;
double sigma_n = -1.0;
assert(result.count("m"));
int processing_mode = result["m"].as<int>();
assert(result.count("i") && result.count("o"));
auto& inputfile = result["i"].as<std::string>();
auto& outputfile = result["o"].as<std::string>();
//output pts by colors
int last_dot = (int)outputfile.find_last_of(".");
auto output_prefix = outputfile.substr(0, last_dot);
int flag_csg = 0;
bool flag_sample_pts = true; //simply generate mask and csg tree of a given point cloud
if (result.count("p"))
{
flag_sample_pts = false;
}
std::string inputext = GetFileExtension(inputfile);
Mesh3d mesh;
if (inputext == "obj")
mesh.load_obj(inputfile.c_str());
else if (inputext == "off")
mesh.load_off(inputfile.c_str());
std::cout << "verts: " << mesh.get_vertices_list()->size() << " face: " << mesh.get_faces_list()->size() << std::endl;
bool flag_verbose = false;
if (result.count("verbose"))
{
flag_verbose = (bool)result["verbose"].as<int>();
}
bool flag_strict = false;
if (result.count("strict"))
{
flag_strict = true;
}
if (processing_mode == 0)
{
//normalization part begin
//[-0.9, 9]^3
std::vector<TinyVector<double, 3>> pts_nl(mesh.get_vertices_list()->size());
double max_range = mesh.xmax - mesh.xmin;
max_range = max_range < (mesh.ymax - mesh.ymin) ? (mesh.ymax - mesh.ymin) : max_range;
max_range = max_range < (mesh.zmax - mesh.zmin) ? (mesh.zmax - mesh.zmin) : max_range;
double xcenter = (mesh.xmin + mesh.xmax) / 2;
double ycenter = (mesh.ymin + mesh.ymax) / 2;
double zcenter = (mesh.zmin + mesh.zmax) / 2;
std::cout << "center " << xcenter << " " << ycenter << " " << zcenter << std::endl;
for (size_t i = 0; i < mesh.get_vertices_list()->size(); i++)
{
mesh.get_vertices_list()->at(i)->pos[0] = (mesh.get_vertices_list()->at(i)->pos[0] - xcenter) / max_range * 1.8;
mesh.get_vertices_list()->at(i)->pos[1] = (mesh.get_vertices_list()->at(i)->pos[1] - ycenter) / max_range * 1.8;
mesh.get_vertices_list()->at(i)->pos[2] = (mesh.get_vertices_list()->at(i)->pos[2] - zcenter) / max_range * 1.8;
}
//output mesh
std::string outputext = GetFileExtension(outputfile);
if (outputext == "obj")
{
mesh.write_obj(outputfile.c_str());
}
else if (outputext == "off")
{
mesh.write_off(outputfile.c_str());
}
return 1;
}
else if (processing_mode == 1)
{
//first sample feature parts then non-feature parts
//mask:
//feature: 0
//non feature: 1,2,3...indicating coloring
assert(result.count("f") && result.count("k"));
auto& inputfeaturefile = result["f"].as<std::string>();
auto& outputmaskfile = result["k"].as<std::string>();
if (result.count("fs"))
n_feature_sample = result["fs"].as<int>();
if (result.count("ns"))
n_nonfeature_sample = result["ns"].as<int>();
if (result.count("s"))
sigma = result["s"].as<double>();
if (result.count("sn"))
sigma_n = result["sn"].as<double>();
bool flag_repair_turn_features = true;
if (result.count("r"))
flag_repair_turn_features = result["r"].as<int>();
bool flag_repair_tree_features = false;
if (result.count("repairtree"))
{
flag_repair_tree_features = true;
}
if (result.count("csg"))
flag_csg = result["csg"].as<int>();
bool flag_skip_hanging_features = false; //not skipping hanging features
//at least sample points with the same shape as face
/*if (n_nonfeature_sample < mesh.get_num_of_faces())
{
n_nonfeature_sample = mesh.get_num_of_faces();
}*/
//std::vector<int> sample_mask(n_feature_sample + n_nonfeature_sample, 0);
std::vector<int> sample_mask;
std::vector<std::pair<int, int>> ungrouped_features;
load_feature_file(inputfeaturefile.c_str(), ungrouped_features);
std::vector<TinyVector<double, 3>> sample_pts, sample_pt_normals;
std::vector<size_t> sample_pts_tris; //used for assign labels of sample pts
if (!flag_sample_pts)
{
auto& inputpcfile = result["p"].as<std::string>();
load_xyz_file(inputpcfile.c_str(), sample_pts, sample_pt_normals);
auto& inputpffile = result["pf"].as<std::string>();
sample_pts_tris.resize(sample_pts.size(), 0);
std::ifstream ifs(inputpffile);
for (size_t ii = 0; ii < sample_pts.size(); ii++)
{
ifs >> sample_pts_tris[ii];
}
ifs.close();
//for testing
std::ofstream outputsamples("test.xyz");
for (size_t i = 0; i < sample_pts.size(); i++)
{
outputsamples << sample_pts[i] << " " << sample_pt_normals[i] << std::endl;
}
outputsamples.close();
}
//skip elements with no features
if (flag_csg && ungrouped_features.empty())
{
std::cout << "empty feature file: " << inputfeaturefile << std::endl;
return 1;
}
//feature check: no hanging feature
//std::vector<size_t> feature_degree_v(mesh.get_num_of_vertices(), 0);
std::vector<std::vector<int>> feature_v2he(mesh.get_num_of_vertices()); //id of hes ematating from each vertex
std::vector<std::pair<int, int>> ungrouped_features_new;
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 = mesh.get_vertices_list()->at(id0)->edge;
HE_edge<double>* edge = mesh.get_vertices_list()->at(id0)->edge;
bool flag_found = false;
do
{
if (id1 == edge->vert->id)
{
feature_v2he[id0].push_back(edge->id);
feature_v2he[id1].push_back(edge->pair->id);
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]);
}
}
if (ungrouped_features.size() != ungrouped_features_new.size())
ungrouped_features = ungrouped_features_new;
////for debugging, save all smooth feature
//std::vector<std::pair<int, int>> smooth_fea;
//for (size_t i = 0; i < mesh.get_num_of_vertices(); i++)
//{
//
// if (feature_v2he[i].size() == 2)
// {
// //check edge convex status
// int flag_convex_edge0 = check_mesh_edge_convex(&mesh, mesh.get_edges_list()->at(feature_v2he[i][0]));
// int flag_convex_edge1 = check_mesh_edge_convex(&mesh, mesh.get_edges_list()->at(feature_v2he[i][1]));
// if (flag_convex_edge0 == 0)
// {
// smooth_fea.push_back(std::pair<int, int>(mesh.get_edges_list()->at(feature_v2he[i][0])->vert->id, mesh.get_edges_list()->at(feature_v2he[i][0])->pair->vert->id));
// }
// if (flag_convex_edge1 == 0)
// {
// smooth_fea.push_back(std::pair<int, int>(mesh.get_edges_list()->at(feature_v2he[i][1])->vert->id, mesh.get_edges_list()->at(feature_v2he[i][1])->pair->vert->id));
// }
// }
//}
//std::ofstream ofs("smooth.fea");
//ofs << smooth_fea.size() << std::endl;
//for (size_t i = 0; i < smooth_fea.size(); i++)
//{
// ofs << smooth_fea[i].first << " " << smooth_fea[i].second << std::endl;
//}
//
//ofs.close();
std::vector<size_t> turn_verts, hanging_verts;
for (size_t i = 0; i < mesh.get_num_of_vertices(); i++)
{
//assert(feature_degree_v[i] == feature_v2he[i].size());
if (feature_v2he[i].size() == 1)
{
std::cout << "input file: " << inputfile << std::endl;
std::cout << "hanging vertex exists: " << i << std::endl;
std::ofstream ofs(inputfile + "hanging");
ofs.close();
//turn_verts.push_back(i);
hanging_verts.push_back(i);
//return 0;
}
else if (feature_v2he[i].size() == 2)
{
//check edge convex status
int flag_convex_edge0 = check_mesh_edge_convex(&mesh, mesh.get_edges_list()->at(feature_v2he[i][0]));
int flag_convex_edge1 = check_mesh_edge_convex(&mesh, mesh.get_edges_list()->at(feature_v2he[i][1]));
if (flag_convex_edge0 * flag_convex_edge1 != 0 && flag_convex_edge0 != flag_convex_edge1)
{
std::ofstream ofs(inputfile + "turn");
ofs.close();
std::cout << "input file: " << inputfile << std::endl;
std::cout << "turn vertex exists: " << i << std::endl;
turn_verts.push_back(i);
}
}
}
//do not handle hanging vertex
if (flag_skip_hanging_features && !hanging_verts.empty())
{
return 0;
}
/*if (!flag_repair_turn_features && !(turn_verts.size() + hanging_verts.size() == 0))
{
return 0;
}*/
//feature parts first
std::random_device rd;
std::mt19937 e2(rd());
std::uniform_real_distribution<double> unif_dist(0, 1);
//std::normal_distribution<double> normal_dist(0, sigma);
/*std::uniform_real_distribution<double> normal_dist(-sigma, sigma);
std::uniform_real_distribution<double> angle_unif_dist(-sigma_n, sigma_n);*/
std::vector<double> feature_length(ungrouped_features.size(), 0.0);
double total_feature_length = 0.0;
for (size_t i = 0; i < ungrouped_features.size(); i++)
{
int id0 = ungrouped_features[i].first;
int id1 = ungrouped_features[i].second;
feature_length[i] = (mesh.get_vertices_list()->at(id0)->pos - mesh.get_vertices_list()->at(id1)->pos).Length();
total_feature_length += feature_length[i];
}
std::vector<double> line_bound(ungrouped_features.size() + 1, 0.0);
for (size_t i = 0; i < ungrouped_features.size(); i++)
{
line_bound[i + 1] = line_bound[i] + feature_length[i] / total_feature_length;
}
//sampling
if (flag_sample_pts)
{
for (size_t i = 0; i < n_feature_sample; i++)
{
double u = unif_dist(e2);
auto iter = std::upper_bound(line_bound.begin(), line_bound.end(), u);
int fid = (int)std::distance(line_bound.begin(), iter);
assert(fid != ungrouped_features.size() + 1);
fid = std::max(0, fid - 1);
//sample
int id0 = ungrouped_features[fid].first;
int id1 = ungrouped_features[fid].second;
double s = unif_dist(e2);
sample_pts.push_back(s * mesh.get_vertices_list()->at(id0)->pos + (1.0 - s) * mesh.get_vertices_list()->at(id1)->pos);
sample_pt_normals.push_back(TinyVector<double, 3>(1.0, 0.0, 0.0));
sample_mask.push_back(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);
}
//cluster faces: assuming the features are all close
std::vector<bool> he_feature_flag(mesh.get_edges_list()->size(), false);
for (size_t i = 0; i < ungrouped_features.size(); i++)
{
int id0 = ungrouped_features[i].first;
int id1 = ungrouped_features[i].second;
//iterate over all verts emanating from id0
HE_edge<double>* edge = mesh.get_vertices_list()->at(id0)->edge;
do
{
if (edge->vert->id == id1)
{
break;
}
edge = edge->pair->next;
} while (edge != mesh.get_vertices_list()->at(id0)->edge);
assert(edge->vert->id == id1);
he_feature_flag[edge->id] = true;
he_feature_flag[edge->pair->id] = true;
}
//repairing conducted here, he_feature_flag, ungrouped_features should be updated
std::vector<std::vector<int>> grouped_features; //grouped features: only one he of a pair is stored
std::vector<int> he2gid;
get_grouped_edges(mesh, he_feature_flag, feature_v2he, grouped_features, he2gid);
if (flag_repair_turn_features && !turn_verts.empty())
//if (true)
{
//group features first
std::cout << "feature group size: " << grouped_features.size() << std::endl;
//repair features
std::vector<bool> flag_feature_points(mesh.get_vertices_list()->size(), false);
for (size_t i = 0; i < he_feature_flag.size(); i++)
{
if (he_feature_flag[i])
{
flag_feature_points[mesh.get_edges_list()->at(i)->vert->id] = true;
}
}
//repair turn vertex
for (size_t i = 0; i < turn_verts.size(); i++)
{
size_t cur_vert = turn_verts[i];
std::vector<bool> flag_feature_points_tmp = flag_feature_points;
size_t cur_group = he2gid[feature_v2he[cur_vert][0]];
for (auto heid : grouped_features[cur_group])
{
flag_feature_points_tmp[mesh.get_edges_list()->at(heid)->vert->id] = false;
flag_feature_points_tmp[mesh.get_edges_list()->at(heid)->pair->vert->id] = false;
}
//distance from cur_vert to all other verts
std::vector<size_t> prev_map;
std::vector<double> dist;
dijkstra_mesh(&mesh, cur_vert, prev_map, dist);
/*size_t nnvid = 0;
double shortest_dist = DBL_MAX;*/
std::set<std::pair<double, size_t>> dist_id_set;
for (size_t j = 0; j < flag_feature_points_tmp.size(); j++)
{
if (flag_feature_points_tmp[j])
{
/*if (dist[j] < shortest_dist)
{
shortest_dist = dist[j];
nnvid = j;
}*/
dist_id_set.insert(std::pair<double, size_t>(dist[j], j));
}
}
for (auto& dist_id : dist_id_set)
{
size_t nnvid = dist_id.second;
bool flag_usable = true;
std::vector<HE_edge<double>*> tmp_hes;
//add path from nnvid to cur_vert
while (nnvid != cur_vert)
{
size_t prev_vert = prev_map[nnvid];
//ungrouped_features.push_back(std::pair<int, int>((int)nnvid, (int)prev_vert));
HE_edge<double>* begin_edge = mesh.get_vertices_list()->at(nnvid)->edge;
HE_edge<double>* edge = begin_edge;
do
{
if (edge->vert->id == prev_vert)
{
break;
}
edge = edge->pair->next;
} while (edge != begin_edge);
assert(edge->vert->id == prev_vert);
/*he_feature_flag[edge->id] = true;
he_feature_flag[edge->pair->id] = true;*/
if (he_feature_flag[edge->id])
{
flag_usable = false;
break;
}
else
{
tmp_hes.push_back(edge);
}
nnvid = prev_vert;
}
if (flag_usable)
{
for (auto he : tmp_hes)
{
ungrouped_features.push_back(std::pair<int, int>(he->vert->id, he->pair->vert->id));
he_feature_flag[he->id] = true;
he_feature_flag[he->pair->id] = true;
feature_v2he[he->vert->id].push_back(he->pair->id);
feature_v2he[he->pair->vert->id].push_back(he->id);
}
break;
}
}
}
//save repaired features
save_feature_file((output_prefix + "_repairturn.fea").c_str(), ungrouped_features);
}
std::vector<int> face_color_init(mesh.get_faces_list()->size(), -1); //starting from 1
std::vector<std::vector<int>> face_clusters;
std::vector<std::pair<int, int>> feature_twoface_colors;//color of faces on two sides of the feature, *.first < *.second
//new version
//int cluster_start_id = 1;
////set face_color_init by propagation
//get_grouped_edges(mesh, he_feature_flag, feature_v2he, grouped_features, he2gid);
//int cluster_id = cluster_mesh_faces(&mesh, he_feature_flag, grouped_features, cluster_start_id, face_color_init, feature_twoface_colors);
//std::cout << "cluster number before merging: " << cluster_id - 1 << std::endl;
////get face_clusters
////get_cluster_from_coloring(face_color_init, cluster_start_id, face_clusters);
////merging: udpate face_color_init, cluster_id, face_clusters
//cluster_id = merge_clusters(&mesh, he_feature_flag, cluster_start_id, cluster_id - 1,feature_twoface_colors, face_color_init);
//std::cout << "cluster number after merging: " << cluster_id - 1 << std::endl;
//new version end
#if 1
//traditional version
int start_id = 0;
int cluster_id = 1;
while (start_id != -1)
{
std::vector<int> onecluster;
std::queue<int> q;
q.push(start_id);
face_color_init[start_id] = cluster_id;
while (!q.empty())
{
int front = q.front();
q.pop();
onecluster.push_back(front);
HE_edge<double>* edge = mesh.get_faces_list()->at(front)->edge;
do
{
if (he_feature_flag[edge->id] == false)
{
//not feature
int pair_fid = edge->pair->face->id;
if (face_color_init[pair_fid] == -1)
{
q.push(pair_fid);
face_color_init[pair_fid] = cluster_id;
}
}
edge = edge->next;
} while (edge != mesh.get_faces_list()->at(front)->edge);
}
face_clusters.push_back(onecluster);
start_id = -1;
//find next start_id
for (size_t i = 0; i < face_color_init.size(); i++)
{
if (face_color_init[i] == -1)
{
start_id = i;
break;
}
}
cluster_id++;
}
#endif
std::vector<int> face_color = face_color_init; //starting from 1
//check face area
std::vector<double> tri_areas;
get_all_tri_area(mesh, tri_areas);
//std::cout << "min tri area: " << *std::min_element(tri_areas.begin(), tri_areas.end()) << std::endl;
std::vector<double> patch_areas(cluster_id, 0.0);
for (size_t i = 0; i < face_color.size(); i++)
{
patch_areas[face_color[i]] += tri_areas[i];
}
if (*std::min_element(patch_areas.begin() + 1, patch_areas.end()) < MIN_PATCH_AREA)
{
std::cout << "too small patch area : " << *std::min_element(patch_areas.begin() + 1, patch_areas.end()) << std::endl;
std::ofstream ofs(output_prefix + "smallpatch");
ofs.close();
//return 1;
}
int n_color = cluster_id;
//coloring
bool flag_coloring = false;
if (result.count("c"))
flag_coloring = result["c"].as<int>();
bool flag_first_convex = false;
if (result.count("convex"))
flag_first_convex = result["convex"].as<int>();
if (flag_coloring && !flag_csg)
{
std::vector<std::set<size_t>> connectivity(cluster_id - 1);
//color - 1
//here assume faces on both sides of a feature belongs to df patches
for (size_t i = 0; i < he_feature_flag.size(); i++)
{
if (he_feature_flag[i])
{
HE_edge<double>* e1 = mesh.get_edges_list()->at(i);
HE_edge<double>* e2 = e1->pair;
if (face_color_init[e1->face->id] == face_color_init[e2->face->id]) continue;
connectivity[face_color_init[e1->face->id] - 1].insert(face_color_init[e2->face->id] - 1);
}
}
//print graph
std::cout << "graph:" << std::endl;
for (size_t i = 0; i < connectivity.size(); i++)
{
std::cout << i + 1 << ": ";
for (auto v : connectivity[i])
{
std::cout << v + 1 << " ";
}
std::cout << std::endl;
}
std::vector<std::vector<size_t>> colored_vertices;
greedy_graph_coloring(cluster_id - 1, connectivity, colored_vertices);
std::cout << "number of colors: " << colored_vertices.size() << std::endl;
n_color = colored_vertices.size() + 1;
//update face_color
for (size_t i = 0; i < colored_vertices.size(); i++)
{
for (size_t j = 0; j < colored_vertices[i].size(); j++)
{
size_t local_id = colored_vertices[i][j];
for (size_t k = 0; k < face_clusters[local_id].size(); k++)
{
face_color[face_clusters[local_id][k]] = i + 1;
}
}
}
}
if (flag_csg)
{
//coloring is based on vertices
std::vector<std::set<size_t>> connectivity(cluster_id - 1);
std::vector<std::set<size_t>> connectivity_v(cluster_id - 1); //connectivity based on vertices
std::map<std::pair<size_t, size_t>, double> fp2product;
//update 1203, construct csg tree by voting
//std::map<std::pair<size_t, size_t>, std::array<int, 3>> fp2count;
std::map<std::pair<size_t, size_t>, int> flag_fpconvex; //update 1203, 0: smooth, 1: convex, 2:concave
//color - 1
//update 0104, face pair convexity is determined by grouped edges
std::vector<std::array<int, 3>> ge2count(grouped_features.size(), std::array<int, 3>{0, 0, 0});
std::map<std::pair<size_t, size_t>, std::set<int>> fp2ge; //face pair to grouped edges
for (size_t i = 0; i < he_feature_flag.size(); i++)
{
if (he_feature_flag[i])
{
HE_edge<double>* e1 = mesh.get_edges_list()->at(i);
HE_edge<double>* e2 = e1->pair;
if (face_color_init[e1->face->id] != face_color_init[e2->face->id])
connectivity[face_color_init[e1->face->id] - 1].insert(face_color_init[e2->face->id] - 1);
else
continue;
size_t fid1 = face_color_init[e1->face->id], fid2 = face_color_init[e2->face->id]; //starting from zero
size_t minfid = std::min(fid1, fid2);
size_t maxfid = std::max(fid1, fid2);
//triangle face
size_t tfid1 = e1->face->id, tfid2 = e2->face->id;
size_t ev1 = e1->vert->id, ev2 = e2->vert->id;
size_t tv1 = tri_verts[tfid1][0] + tri_verts[tfid1][1] + tri_verts[tfid1][2] - ev1 - ev2;
size_t tv2 = tri_verts[tfid2][0] + tri_verts[tfid2][1] + tri_verts[tfid2][2] - ev1 - ev2;
double product = e1->face->normal.Dot(vert_pos[tv2] - vert_pos[tv1]);
std::pair<size_t, size_t> tmp_pair(minfid - 1, maxfid - 1);
auto it = fp2product.find(tmp_pair);
if (it == fp2product.end())
{
fp2product[tmp_pair] = product;
}
else
{
fp2product[tmp_pair] += product;
}
double tmp_cos = e1->face->normal.Dot(e2->face->normal);
//if (fp2count.find(tmp_pair) == fp2count.end())
/*if (ge2count.find(tmp_pair) == ge2count.end())
{
fp2count[tmp_pair] = std::array<int, 3>({ 0, 0, 0 });
}*/
int gid = he2gid[i];
if (fp2ge.find(tmp_pair) == fp2ge.end())
{
fp2ge[tmp_pair] = std::set<int>();
}
fp2ge[tmp_pair].insert(gid);
if (product < 0.0)
{
//convex
if (tmp_cos < th_smooth_cos_value)
{
//fp2count[tmp_pair][1]++;
ge2count[gid][1]++;
}
else
{
//fp2count[tmp_pair][0]++;
ge2count[gid][0]++;
}
}
else
{
//concave
if (tmp_cos < th_smooth_cos_value)
{
ge2count[gid][2]++;
}
else
{
ge2count[gid][0]++;
}
}
}
}
//init connectivity_v
connectivity_v = connectivity;
for (size_t i = 0; i < feature_v2he.size(); i++)
{
if (feature_v2he[i].size() > 2)
{
//vertex with degree larger than 3
HE_edge<double>* ve_begin = mesh.get_vertices_list()->at(i)->edge;
assert(ve_begin->pair->vert->id == i);
HE_edge<double>* ve_iter = ve_begin;
std::set<size_t> surounding_cs;
do
{
/*int next_id = ve_iter->vert->id;
if (next_id == id1)
{
te = ve_iter;
break;
}
ve_iter = ve_iter->pair->next;*/
surounding_cs.insert(face_color_init[ve_iter->face->id] -1);
ve_iter = ve_iter->pair->next;
} while (ve_iter != ve_begin);
//if (surounding_cs.size() != feature_v2he[i].size())
//{
// //update 1201, no longer big deal, because the generix might exists
// std::cout << "input file: " << inputfile << std::endl;
// std::cout << "feature wrong near: " << i << std::endl;
//}
//assert(surounding_cs.size() == feature_v2he[i].size());
std::vector<size_t> surounding_cs_vector(surounding_cs.begin(), surounding_cs.end());
for (size_t it = 0; it < surounding_cs_vector.size() - 1; it++)
{
size_t cur_fid = surounding_cs_vector[it];
for (size_t it1 = it + 1; it1 < surounding_cs_vector.size(); it1++)
{
size_t n_fid = surounding_cs_vector[it1];
connectivity_v[cur_fid].insert(n_fid);
connectivity_v[n_fid].insert(cur_fid);
}
}
}
}
if (flag_strict)
{
for (auto& p : fp2product)
{
if (p.second < 0.0)
{
//flag_fpconvex[p.first] = true;
flag_fpconvex[p.first] = 1;
}
else
{
//flag_fpconvex[p.first] = false;
flag_fpconvex[p.first] = 2;
}
}
}
else
{
//not struct version
/*for (auto& p : fp2count)
{
int maxid = -1, max_num = -1;
for (size_t ii = 0; ii < 3; ii++)
{
if (p.second[ii] > max_num)
{
max_num = p.second[ii];
maxid = ii;
}
}
flag_fpconvex[p.first] = maxid;
}*/
//update 0104, get face pair connectivity by grouped edges
bool flag_valid_seg = true;
std::set<size_t> invalid_seg_patches;
std::vector<int> ge2convex(grouped_features.size(), 0);
for (size_t i = 0; i < grouped_features.size(); i++)
{
int maxid = -1, max_num = -1;
for (size_t ii = 0; ii < 3; ii++)
{
if (ge2count[i][ii] > max_num)
{
max_num = ge2count[i][ii];
maxid = ii;
}
}
ge2convex[i] = maxid;
}
for (auto& p : fp2ge)
{
std::set<int> cur_strict_convex_types;
for (auto gid : p.second)
{
if (ge2convex[gid] != 0)
cur_strict_convex_types.insert(ge2convex[gid]);
}
if (cur_strict_convex_types.size() >= 2)
{
flag_valid_seg = false;
invalid_seg_patches.insert(p.first.first);
invalid_seg_patches.insert(p.first.second);
}
else if (cur_strict_convex_types.size() == 0)
{
flag_fpconvex[p.first] = 0; //smooth
}
else
{
//only one left
flag_fpconvex[p.first] = *cur_strict_convex_types.begin();
}
}
if (!flag_valid_seg)
{
std::ofstream ofs(inputfile + "treefail");
ofs.close();
std::cout << "invalid segmentation " << output_prefix << std::endl;
repair_tree_features_maxflow(mesh, face_color, std::vector<size_t>(invalid_seg_patches.begin(), invalid_seg_patches.end()), ungrouped_features);
ofs.open(output_prefix + "_fixtree.fea");
ofs << ungrouped_features.size() << std::endl;
for (auto& pp : ungrouped_features)
{
ofs << pp.first << " " << pp.second << std::endl;
}
ofs.close();
mesh.write_obj((output_prefix + "_fixtree.obj").c_str());
return 1;
}