-
Notifications
You must be signed in to change notification settings - Fork 0
/
topology.cc
1377 lines (1149 loc) · 49.2 KB
/
topology.cc
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 <stdlib.h>
// -------------------- OpenMesh
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
#include <OpenMesh/Core/Utils/PropertyManager.hh>
//#include <OpenMesh/Tools/SmartTagger/SmartTaggerT.hh>
//#include <OpenMesh/Tools/Smoother/JacobiLaplaceSmootherT.hh>
#include <OpenMesh/Core/Mesh/Status.hh>
#include <OpenMesh/Apps/Assembly/topology.hh>
//need custom_mesh_props for face type filtering
#include <OpenMesh/Apps/Assembly/custom_mesh_props.hh>
//typedef OpenMesh::TriMesh_ArrayKernelT<> MyMesh;
/*
Find edge connecting v1 and v2
*/
MyMesh::EdgeHandle find_edge(MyMesh & mesh, MyMesh::VertexHandle & v1, MyMesh::VertexHandle & v2 ){
MyMesh::HalfedgeHandle heh = mesh.find_halfedge(v1, v2);
if (heh.is_valid()) {
return mesh.edge_handle(heh);
}
else {
return MyMesh::InvalidEdgeHandle;
}
}
/*
Find an outgoing boundary halfedge of v. v should only have maximum one outgoing boundary halfedge.
*/
MyMesh::HalfedgeHandle find_outgoing_boundary_he(MyMesh & mesh, MyMesh::VertexHandle & v){
MyMesh::HalfedgeHandle heh;
for(MyMesh::VertexOHalfedgeIter oh = mesh.voh_iter(v); oh.is_valid(); ++oh) {
if (mesh.is_boundary(*oh)){
return *oh;
}
}
return MyMesh::InvalidHalfedgeHandle;
}
/*
Find an incoming boundary halfedge of v. v should only have maximum one incoming boundary halfedge.
*/
MyMesh::HalfedgeHandle find_incoming_boundary_he(MyMesh & mesh, MyMesh::VertexHandle & v){
MyMesh::HalfedgeHandle heh;
for(MyMesh::VertexIHalfedgeIter ih = mesh.vih_iter(v); ih.is_valid(); ++ih) {
if (mesh.is_boundary(*ih)){
return *ih;
}
}
return MyMesh::InvalidHalfedgeHandle;
}
//find all (undeleted) boundary halfedges
std::vector<MyMesh::HalfedgeHandle> find_boundary_halfedges(MyMesh & mesh){
std::vector<MyMesh::HalfedgeHandle> surface_halfedges;
for (MyMesh::HalfedgeIter h_it = mesh.halfedges_sbegin();h_it != mesh.halfedges_end(); ++h_it ){
if (mesh.is_boundary(*h_it)){
surface_halfedges.push_back(*h_it);
}
}
return surface_halfedges;
}
/*
Returns a vector with the common neighbors of v1 and v2, i.e. vertices that are directly connected both to v1 and v2
*/
std::vector<MyMesh::VertexHandle> find_common_neighbors(MyMesh & mesh, MyMesh::VertexHandle & v1, MyMesh::VertexHandle & v2){
std::vector<MyMesh::VertexHandle> common_neighbors;
//find common neighbors of v1 and v2
//iterate over neighbors of v1
for(MyMesh::VertexVertexIter it1 = mesh.vv_iter(v1); it1.is_valid(); ++it1) {
//neighbors of v2
for(MyMesh::VertexVertexIter it2 = mesh.vv_iter(v2); it2.is_valid(); ++it2) {
if ((*it1)==(*it2)){
common_neighbors.push_back(*it1);
}
}
}
return common_neighbors;
}
/*
Returns nearest surface neighbors of v1
*/
std::vector<MyMesh::VertexHandle> find_surface_neighbors(MyMesh & mesh, MyMesh::VertexHandle & v1){
std::vector<MyMesh::VertexHandle> surface_neighbors;
MyMesh::HalfedgeHandle he;
MyMesh::VertexHandle v_from, v_to;
//find the surface edges out of all edges of v1
/* for(MyMesh::VertexEdgeIter edge = mesh.ve_iter(v1); edge.is_valid(); ++edge) {
if (mesh.is_boundary(*edge)){
he = (*edge).h0();
v_from = mesh.from_vertex_handle(he);
v_to = mesh.to_vertex_handle(he);
if (v_from != v1){
surface_neighbors.push_back(v_from);
}
if (v_to != v1){
surface_neighbors.push_back(v_to);
}
}
}
*/
he = find_incoming_boundary_he(mesh, v1);
surface_neighbors.push_back(mesh.from_vertex_handle(he));
he = find_outgoing_boundary_he(mesh, v1);
surface_neighbors.push_back(mesh.to_vertex_handle(he));
if (surface_neighbors.size()!=2){
std::cout<<"something's wrong, v1 should have 2 surface neighbor vertices; "<<surface_neighbors.size()<<std::endl;
}
return surface_neighbors;
}
/*Does the merge by inserting a dummy face (or two dummy faces if crack) between (v1, v2, v0) and collapses the half edge connecting v1 and v2
returns the common neighbor of v1, v2 for wedge, and the first common neighbor for crack
*/
MyMesh::VertexHandle merge_vertices(MyMesh & mesh, MyMesh::VertexHandle & v1, MyMesh::VertexHandle & v2){
std::vector<MyMesh::VertexHandle> common_neighbors = find_common_neighbors(mesh, v1, v2);
if (common_neighbors.size()==0){
std::cout<<"no common neighbors, vertices can't be merged"<<std::endl;
}
if (common_neighbors.size()>2){
std::cout<<"more than two common neighbors, vertices can't be merged"<<std::endl;
}
if (mesh.is_valid_handle (mesh.find_halfedge(v1, v2))){
std::cout<<"v1 and v2 are connected!!"<<std::endl;
}
std::vector<MyMesh::VertexHandle> dummy_face_vhandles;
// std::vector<MyMesh::VertexHandle*> handle_tracking_v;
// std::vector<MyMesh::FaceHandle*> handle_tracking_f;
// std::vector<MyMesh::HalfedgeHandle*> handle_tracking_h;
MyMesh::FaceHandle dummy_face;
MyMesh::VertexHandle remaining_vertex;
MyMesh::HalfedgeHandle he, he10_surface, he10_nonsurface, he20_surface, he20_nonsurface, he_tmp;
//need to copy edge properties
for (auto& v0: common_neighbors){
he_tmp = mesh.find_halfedge(v0, v1);
if (mesh.is_boundary(he_tmp)){
he10_surface = he_tmp;
he10_nonsurface = mesh.opposite_halfedge_handle(he_tmp);
}
else{
he10_nonsurface = he_tmp;
he10_surface = mesh.opposite_halfedge_handle(he_tmp);
}
he_tmp = mesh.find_halfedge(v0, v2);
if (mesh.is_boundary(he_tmp)){
he20_surface = he_tmp;
he20_nonsurface = mesh.opposite_halfedge_handle(he_tmp);
}
else{
he20_nonsurface = he_tmp;
he20_surface = mesh.opposite_halfedge_handle(he_tmp);
}
mesh.copy_all_properties(he10_nonsurface, he20_surface);
mesh.copy_all_properties(he20_nonsurface, he10_surface);
}
//for all common neighbors v0 of v1 and v2, insert dummy faces (1 for wedge, 2 for crack);
//auto v0 = common_neighbors[0];
for (auto& v0: common_neighbors){
he = mesh.find_halfedge(v0, v1);
//need to get the orientation of dummy face right
if (mesh.is_boundary(he)){
he = mesh.opposite_halfedge_handle(he);
}
dummy_face_vhandles.clear();
dummy_face_vhandles.push_back( mesh.to_vertex_handle(he) );
dummy_face_vhandles.push_back( mesh.from_vertex_handle(he) );
dummy_face_vhandles.push_back(v2);
dummy_face = mesh.add_face(dummy_face_vhandles);
}
he = mesh.find_halfedge(v1, v2);
remaining_vertex = mesh.to_vertex_handle(he);
mesh.collapse(he);
//std::cout<<"@@@ "<<mesh.deleted(v1)<<" "<<mesh.deleted(v2)<<" "<<mesh.deleted(remaining_vertex)<<" he"<<he<<std::endl;
//handle_tracking_v.push_back(& (common_neighbors[0]) );
//handle_tracking_v.push_back(&v1);
//handle_tracking_v.push_back(&v2);
//mesh.garbage_collection<std::vector<MyMesh::VertexHandle> >(handle_tracking);
/* mesh.garbage_collection<std::vector<MyMesh::VertexHandle*>, std::vector<MyMesh::HalfedgeHandle*>, std::vector<MyMesh::FaceHandle*> >(handle_tracking_v, handle_tracking_h, handle_tracking_f);
//mesh.garbage_collection();
if (mesh.is_valid_handle(v1)){
remaining_vertex = v1;
}
if (mesh.is_valid_handle(v2)){
remaining_vertex = v2;
}
//std::cout<<"**** "<<mesh.halfedge_handle(v1)<<" "<<mesh.halfedge_handle(v2)<<" "<<mesh.halfedge_handle(remaining_vertex)<<" he"<<he<<std::endl;
//std::cout<<"### "<<mesh.is_valid_handle(v1)<<" "<<mesh.is_valid_handle(v2)<<" "<<mesh.is_valid_handle(remaining_vertex)<<" he"<<he<<std::endl;
*/
return remaining_vertex;//common_neighbors[0];
}
/*Does the wedge/crack closure by inserting a dummy face (or two dummy faces if crack) between (w1, w3, v0) and collapses the half edge connecting w1 and w3
returns the vertices w13p = w1 or w3 (whichever is left) and w2 after the merge
Note: w1-w2-w3 must be ordered wedge triplets!!
*/
/*std::tuple<MyMesh::VertexHandle, MyMesh::VertexHandle> close_wedge(MyMesh & mesh, MyMesh::VertexHandle & w1, MyMesh::VertexHandle & w2, MyMesh::VertexHandle & w3){
//one of the common neighbors should be w2; if crack there are two common neighbors
std::vector<MyMesh::VertexHandle> common_neighbors = find_common_neighbors(mesh, w1, w3);
std::vector<MyMesh::VertexHandle> dummy_face_vhandles;
std::vector<MyMesh::VertexHandle*> handle_tracking_v;
std::vector<MyMesh::FaceHandle*> handle_tracking_f;
std::vector<MyMesh::HalfedgeHandle*> handle_tracking_h;
MyMesh::FaceHandle dummy_face;
MyMesh::VertexHandle w13p;
MyMesh::HalfedgeHandle he, he10_surface, he10_nonsurface, he20_surface, he20_nonsurface, he_tmp;
//need to copy edge properties
for (auto& v0: common_neighbors){
he_tmp = mesh.find_halfedge(v0, w1);
if (mesh.is_boundary(he_tmp)){
he10_surface = he_tmp;
he10_nonsurface = mesh.opposite_halfedge_handle(he_tmp);
}
else{
he10_nonsurface = he_tmp;
he10_surface = mesh.opposite_halfedge_handle(he_tmp);
}
he_tmp = mesh.find_halfedge(v0, w3);
if (mesh.is_boundary(he_tmp)){
he20_surface = he_tmp;
he20_nonsurface = mesh.opposite_halfedge_handle(he_tmp);
}
else{
he20_nonsurface = he_tmp;
he20_surface = mesh.opposite_halfedge_handle(he_tmp);
}
mesh.copy_all_properties(he10_nonsurface, he20_surface);
mesh.copy_all_properties(he20_nonsurface, he10_surface);
}
//for all common neighbors v0 of v1 and v2, insert dummy faces (1 for wedge, 2 for crack);
//auto v0 = common_neighbors[0];
for (auto& v0: common_neighbors){
he = mesh.find_halfedge(v0, w1);
//need to get the orientation of dummy face right
if (mesh.is_boundary(he)){
he = mesh.opposite_halfedge_handle(he);
}
dummy_face_vhandles.clear();
dummy_face_vhandles.push_back( mesh.to_vertex_handle(he) );
dummy_face_vhandles.push_back( mesh.from_vertex_handle(he) );
dummy_face_vhandles.push_back(w3);
dummy_face = mesh.add_face(dummy_face_vhandles);
}
he = mesh.find_halfedge(w1, w3);
//remaining_vertex = mesh.to_vertex_handle(he);
mesh.collapse(he);
//std::cout<<"@@@ "<<mesh.deleted(v1)<<" "<<mesh.deleted(v2)<<" "<<mesh.deleted(remaining_vertex)<<" he"<<he<<std::endl;
handle_tracking_v.push_back(&w1);
handle_tracking_v.push_back(&w2);
handle_tracking_v.push_back(&w3);
//mesh.garbage_collection<std::vector<MyMesh::VertexHandle> >(handle_tracking);
mesh.garbage_collection<std::vector<MyMesh::VertexHandle*>, std::vector<MyMesh::HalfedgeHandle*>, std::vector<MyMesh::FaceHandle*> >(handle_tracking_v, handle_tracking_h, handle_tracking_f);
//mesh.garbage_collection();
if (mesh.is_valid_handle(w1)){
w13p = w1;
}
if (mesh.is_valid_handle(w3)){
w13p = w3;
}
//std::cout<<"**** "<<mesh.halfedge_handle(v1)<<" "<<mesh.halfedge_handle(v2)<<" "<<mesh.halfedge_handle(remaining_vertex)<<" he"<<he<<std::endl;
std::cout<<"### "<<mesh.is_valid_handle(w13p)<<" "<<mesh.is_valid_handle(w2)<<" "<<mesh.is_valid_handle(w1)<<" he"<<he<<std::endl;
return std::make_tuple(w13p, w2);
}
*/
/*
Creates a crack along v2 - v1 - v3. Can only be used for cracks, i.e. v1 has to be non-boundary
Returns the two new vertices v1--> (v0, v1')
*/
MyMesh::VertexHandle split_vertices(MyMesh & mesh, MyMesh::VertexHandle & v1, MyMesh::VertexHandle & v2, MyMesh::VertexHandle & v3){
if (mesh.is_boundary(v1)){
std::cout<<"This is not a crack! Try split_vertices(mesh, v1, v2) for wedge openings"<<std::endl;
}
//std::vector<MyMesh::VertexHandle*> handle_tracking_v;
//std::vector<MyMesh::FaceHandle*> handle_tracking_f;
//std::vector<MyMesh::HalfedgeHandle*> handle_tracking_h;
//vertex_split returns a halfedge; should be able to use to get the newly created vertex
MyMesh::HalfedgeHandle new_halfedge = mesh.vertex_split(MyMesh::Point(mesh.point(v1)), v1, v3, v2);
//v0 will be the newly inserted vertex
MyMesh::VertexHandle v0 = mesh.from_vertex_handle(new_halfedge);
mesh.delete_edge( find_edge(mesh, v0, v1) );
//remaining edge's surface halfedge (hold_surface) properties should be copied to the
//newly created edge's non-surface halfedge (hnew_nonsurface)
MyMesh::HalfedgeHandle hold_surface;
MyMesh::HalfedgeHandle hnew_nonsurface;
MyMesh::HalfedgeHandle he;
//assuming halfedge v2-v1 stays
he = mesh.find_halfedge(v2, v1);
if (mesh.is_boundary(he)){
hold_surface = he;
}
else{
hold_surface = mesh.opposite_halfedge_handle(he);
}
he = mesh.find_halfedge(v2, v0);
if (mesh.is_boundary(he)){
hnew_nonsurface = mesh.opposite_halfedge_handle(he);
}
else{
hnew_nonsurface = he;
}
mesh.copy_all_properties(hold_surface, hnew_nonsurface);
//assuming halfedge v3-v1 stays
he = mesh.find_halfedge(v3, v1);
if (mesh.is_boundary(he)){
hold_surface = he;
}
else{
hold_surface = mesh.opposite_halfedge_handle(he);
}
he = mesh.find_halfedge(v3, v0);
if (mesh.is_boundary(he)){
hnew_nonsurface = mesh.opposite_halfedge_handle(he);
}
else{
hnew_nonsurface = he;
}
mesh.copy_all_properties(hold_surface, hnew_nonsurface);
//mesh.garbage_collection();
//mesh.garbage_collection<std::vector<MyMesh::VertexHandle*>, std::vector<MyMesh::HalfedgeHandle*>, std::vector<MyMesh::FaceHandle*> >(handle_tracking_v, handle_tracking_h, handle_tracking_f);
//std::cout<<"OH3 "<<mesh.halfedge_handle(v0)<<std::endl;
return v0;
}
//---------
/*
MyMesh::VertexHandle open_crack(MyMesh & mesh, MyMesh::VertexHandle & v2, MyMesh::VertexHandle & v1, MyMesh::VertexHandle & v3){
if (mesh.is_boundary(v1)){
std::cout<<"This is not a crack! Try split_vertices(mesh, v1, v2) for wedge openings"<<std::endl;
}
//vertex_split returns a halfedge; should be able to use to get the newly created vertex
MyMesh::HalfedgeHandle new_halfedge = mesh.vertex_split(MyMesh::Point(mesh.point(v1)), v1, v3, v2);
//v0 will be the newly inserted vertex
MyMesh::VertexHandle v0 = mesh.from_vertex_handle(new_halfedge);
mesh.delete_edge( find_edge(mesh, v0, v1) );
//remaining edge's surface halfedge (hold_surface) properties should be copied to the
//newly created edge's non-surface halfedge (hnew_nonsurface)
MyMesh::HalfedgeHandle hold_surface;
MyMesh::HalfedgeHandle hnew_nonsurface;
MyMesh::HalfedgeHandle he;
//assuming halfedge v2-v1 stays
he = mesh.find_halfedge(v2, v1);
if (mesh.is_boundary(he)){
hold_surface = he;
}
else{
hold_surface = mesh.opposite_halfedge_handle(he);
}
he = mesh.find_halfedge(v2, v0);
if (mesh.is_boundary(he)){
hnew_nonsurface = mesh.opposite_halfedge_handle(he);
}
else{
hnew_nonsurface = he;
}
mesh.copy_all_properties(hold_surface, hnew_nonsurface);
//assuming halfedge v3-v1 stays
he = mesh.find_halfedge(v3, v1);
if (mesh.is_boundary(he)){
hold_surface = he;
}
else{
hold_surface = mesh.opposite_halfedge_handle(he);
}
he = mesh.find_halfedge(v3, v0);
if (mesh.is_boundary(he)){
hnew_nonsurface = mesh.opposite_halfedge_handle(he);
}
else{
hnew_nonsurface = he;
}
mesh.copy_all_properties(hold_surface, hnew_nonsurface);
mesh.garbage_collection();
std::cout<<"OH3 "<<mesh.halfedge_handle(v0)<<std::endl;
return v0;
}
*/
/*
Creates a wedge crack along v2 - v1. Can only be used for wedges, i.e. v1 has to be boundary
Returns the newly created vertex
*/
MyMesh::VertexHandle split_vertices(MyMesh & mesh, MyMesh::VertexHandle & v1, MyMesh::VertexHandle & v2){
/*if (!mesh.is_boundary(v1) || mesh.is_boundary(v2)){
std::cout<<"v1 has to be boundary, v2 non-boundary"<<std::endl;
}*/
//insert dummy face
std::vector<MyMesh::VertexHandle> surface_neighbors = find_surface_neighbors(mesh, v1);
MyMesh::VertexHandle vA, vB, vC, v0, vD;
MyMesh::HalfedgeHandle he;
std::vector<MyMesh::VertexHandle> dummy_face_vhandles;
MyMesh::FaceHandle dummy_face1, dummy_face2, dummy_face3, dummy_face4;
vA = surface_neighbors[0];
vB = surface_neighbors[1];
vC = mesh.add_vertex(0.5*(mesh.point(vA) + mesh.point(vB)) +MyMesh::Point(0.0,0.0,1.5) );
//vD = mesh.add_vertex( mesh.point(vC) - 3*(mesh.point(vC) - mesh.point(v1) ) +MyMesh::Point(0.0,0.0,1.5));
//std::cout<<"v1 "<<v1<<" v2 "<<v2<<" vA "<<vA<<" vB "<<vB<<" vC "<<vC<<std::endl;
dummy_face_vhandles.clear();
he = mesh.find_halfedge(vA, v1);
//need to get the orientation of dummy face right
//this could be done probably by the order of surface neighbors returned by find_surface_neighbors()
if (mesh.is_boundary(he)){
dummy_face_vhandles.push_back( vA);
dummy_face_vhandles.push_back( v1);
dummy_face_vhandles.push_back(vC);
dummy_face1 = mesh.add_face(dummy_face_vhandles);
}
else{
dummy_face_vhandles.push_back( v1);
dummy_face_vhandles.push_back( vA);
dummy_face_vhandles.push_back(vC);
dummy_face1 = mesh.add_face(dummy_face_vhandles);
}
dummy_face_vhandles.clear();
he = mesh.find_halfedge(v1, vB);
//need to get the orientation of dummy face right
if (mesh.is_boundary(he)){
dummy_face_vhandles.push_back( vC);
dummy_face_vhandles.push_back( v1);
dummy_face_vhandles.push_back(vB);
dummy_face2 = mesh.add_face(dummy_face_vhandles);
}
else{
dummy_face_vhandles.push_back( v1);
dummy_face_vhandles.push_back( vC);
dummy_face_vhandles.push_back(vB);
dummy_face2 = mesh.add_face(dummy_face_vhandles);
}
/*
dummy_face_vhandles.clear();
he = mesh.find_halfedge(vC, vB);
//need to get the orientation of dummy face right
if (mesh.is_boundary(he)){
dummy_face_vhandles.push_back( vD);
dummy_face_vhandles.push_back( vC);
dummy_face_vhandles.push_back(vB);
dummy_face3 = mesh.add_face(dummy_face_vhandles);
}
else{
dummy_face_vhandles.push_back( vC);
dummy_face_vhandles.push_back( vD);
dummy_face_vhandles.push_back(vB);
dummy_face3 = mesh.add_face(dummy_face_vhandles);
}
dummy_face_vhandles.clear();
he = mesh.find_halfedge(vD, vA);
//need to get the orientation of dummy face right
if (mesh.is_boundary(he)){
dummy_face_vhandles.push_back( vA);
dummy_face_vhandles.push_back( vC);
dummy_face_vhandles.push_back(vD);
dummy_face4 = mesh.add_face(dummy_face_vhandles);
}
else{
dummy_face_vhandles.push_back( vC);
dummy_face_vhandles.push_back( vA);
dummy_face_vhandles.push_back(vD);
dummy_face4 = mesh.add_face(dummy_face_vhandles);
}
*/
//crack it
v0 = split_vertices(mesh, v1, v2, vC);
//std::cout<<"OH2 "<<mesh.halfedge_handle(v0)<<std::endl;
//remove dummy face
mesh.delete_face(dummy_face1);
//mesh.garbage_collection();
mesh.delete_face(dummy_face2);
/*mesh.delete_edge( find_edge(mesh, v0, vC), false );
mesh.garbage_collection();
mesh.delete_edge( find_edge(mesh, v1, vC), false );
*/
//mesh.delete_vertex(vC);
// mesh.garbage_collection();
// std::cout<<"OH "<<mesh.halfedge_handle(v0)<<std::endl;
return v0;
/* surface_neighbors = find_surface_neighbors(mesh, v2);
if (surface_neighbors[0]==v1){
return surface_neighbors[1];
}
else{
return surface_neighbors[0];
}
*/
}
/*Returns wedge crack fission pair vertices
returns a tuple of two vectors
first vector contains surface vertices
second vector contains corresponding inner vertices
wedge understood to be open along v_pair_surface[i] -- v_pair_not_surface[i] edge
*/
std::tuple< std::vector<MyMesh::VertexHandle>, std::vector<MyMesh::VertexHandle> > get_type1_fission_pairs(MyMesh & mesh){
std::vector<MyMesh::VertexHandle> v_pair_surface;
std::vector<MyMesh::VertexHandle> v_pair_not_surface;
std::vector<MyMesh::VertexHandle> surface_vertices;
//find boundary vertices
for(MyMesh::VertexIter it = mesh.vertices_sbegin(); it != mesh.vertices_end(); ++it) {
if(mesh.is_boundary(*it)){
surface_vertices.push_back(*it);
}
}
//iterate over all surface vertices
for (auto& v1: surface_vertices){
//iterate over all neighbors v2 of surface vertex v1
for(MyMesh::VertexVertexIter v2 = mesh.vv_iter(v1); v2.is_valid(); ++v2){
//if v2 is not a boundary, v2 - v1 can be wedge-open
if (!mesh.is_boundary(*v2)){
v_pair_surface.push_back(v1);
v_pair_not_surface.push_back(*v2);
}
}
}
return std::make_pair(v_pair_surface,v_pair_not_surface);
}
/* type2 fission is crack opening
all 3 vertices of the prospective crack have to be non-boundary
returns each triplet TWICE
could have been combined with type1_fission_pairs, but here all triplets appear twice; if duplicates are removed, it could be combined with type1 fission
*/
std::tuple< std::vector<MyMesh::VertexHandle>, std::vector<MyMesh::VertexHandle>, std::vector<MyMesh::VertexHandle> > get_type2_fission_triplets(MyMesh & mesh){
std::vector <MyMesh::VertexHandle> fission1;
std::vector <MyMesh::VertexHandle> fission2;
std::vector <MyMesh::VertexHandle> fission3;
//iterate over all vertices
for (MyMesh::VertexIter v1 = mesh.vertices_sbegin(); v1 != mesh.vertices_end(); ++v1){
if (mesh.is_boundary(*v1)){
continue;
}
//iterate over v2 neighbors of v1
for (MyMesh::VertexVertexIter v2 = mesh.vv_iter(*v1); v2.is_valid(); ++v2){
if (mesh.is_boundary(*v2)){
continue;
}
//iterate over v3 neighbors of v2
for (MyMesh::VertexVertexIter v3 = mesh.vv_iter(*v2); v3.is_valid(); ++v3){
if (mesh.is_boundary(*v3) || (*v3==*v1)){
continue;
}
else{
fission1.push_back(*v1);
fission2.push_back(*v2);
fission3.push_back(*v3);
}
}
}
}
return std::make_tuple(fission1, fission2, fission3);
}
/* returns wedges v1--v2--v3
three vectors containing v1[i], v2[i], v3[i]
also returns the number of common neighbor vertices of v1 and v2 to be used for wedge closure test
!! includes crack wedges as well, separately
*/
std::tuple< std::vector<MyMesh::VertexHandle>, std::vector<MyMesh::VertexHandle>, std::vector<MyMesh::VertexHandle> , std::vector<int>> get_open_wedge_triplets(MyMesh & mesh){
std::vector <MyMesh::VertexHandle> wedge1, wedge2, wedge3, surface_vertices, common_neighbors;
std::vector <int> number_of_common_neighbor_vertices;
MyMesh::VertexHandle w2, w3;
MyMesh::HalfedgeHandle he;
//find surface vertices
for (MyMesh::VertexIter it = mesh.vertices_sbegin(); it != mesh.vertices_end(); ++it){
if (mesh.is_boundary(*it)){
surface_vertices.push_back(*it);
}
}
//loop over surface vertices
for (auto &w1 : surface_vertices){
he = find_outgoing_boundary_he(mesh, w1);
w2 = mesh.to_vertex_handle(he);
he = mesh.next_halfedge_handle(he);//find_outgoing_boundary_he(mesh, w2);
w3 = mesh.to_vertex_handle(he);
//only if w1 and w3 are not connected; this solves the triangle hole problem as well (which is not a wedge)
if (!mesh.is_valid_handle( mesh.find_halfedge(w1, w3) )){
wedge1.push_back(w1);
wedge2.push_back(w2);
wedge3.push_back(w3);
common_neighbors = find_common_neighbors(mesh, w1, w3);
number_of_common_neighbor_vertices.push_back(common_neighbors.size());
}
}
return std::make_tuple(wedge1, wedge2, wedge3, number_of_common_neighbor_vertices);
}
std::tuple< std::vector<MyMesh::VertexHandle>, std::vector<MyMesh::VertexHandle>, std::vector<MyMesh::VertexHandle> , std::vector<int>> old_get_open_wedge_triplets(MyMesh & mesh){
std::vector <MyMesh::VertexHandle> wedge1, wedge2, wedge3;
std::vector <int> number_of_common_neighbor_vertices;
std::vector <MyMesh::VertexHandle> surface_vertices;
bool qualifies;
std::vector<MyMesh::VertexHandle> common_neighbors;
MyMesh::VertexHandle vertex1, vertex2;
MyMesh::HalfedgeHandle he1, he2;
//find surface vertices
for (MyMesh::VertexIter it = mesh.vertices_sbegin(); it != mesh.vertices_end(); ++it){
if (mesh.is_boundary(*it)){
surface_vertices.push_back(*it);
}
}
//loop over surface vertex pairs, check if they have common neighbors attached by surface edges
for (unsigned int ix1 =0; ix1<surface_vertices.size(); ++ix1){
//this could possibly be made faster by iterating over the neighbors of surface_vertices[ix1]
//but then wedges may appear multiple times
for (unsigned int ix2 =ix1+1; ix2<surface_vertices.size(); ++ix2){
vertex1 = surface_vertices[ix1];
vertex2 = surface_vertices[ix2];
//only if vertex1 and vertex2 are not connected
if (!mesh.is_valid_handle( mesh.find_halfedge(vertex1, vertex2) )) {
common_neighbors = find_common_neighbors(mesh, vertex1, vertex2);
//have to check if vertex1--cn and vertex2--cn are both surface edges
for (auto &cn : common_neighbors){
qualifies=true;
he1 = mesh.find_halfedge(cn, vertex1);
he2 = mesh.opposite_halfedge_handle(he1);
//if neither halfedges of vertex1--cn are boundary
if ( (!mesh.is_boundary(he1)) && !(mesh.is_boundary(he2))){
qualifies=false;
}
he1 = mesh.find_halfedge(cn, vertex2);
he2 = mesh.opposite_halfedge_handle(he1);
//if neither halfedges of vertex2--cn are boundary
if ( (!mesh.is_boundary(he1)) && !(mesh.is_boundary(he2))){
qualifies=false;
}
if (qualifies){
wedge1.push_back(vertex1);
wedge2.push_back(cn);
wedge3.push_back(vertex2);
number_of_common_neighbor_vertices.push_back(common_neighbors.size());
}
}
}
}
}
return std::make_tuple(wedge1, wedge2, wedge3, number_of_common_neighbor_vertices);
}
/* type1 fusion is wedge closure; open_wedge_triplets could be passed as parameters; might be faster if they're reused for more moves
returns fusion1, fusion2, fusion3 vertices where fusion2 is the common neighbor and fusion1 and fusion2 can be fused
returning fusion2 is merely convenience, fusion1 and fusion3 only have one common neighbor
*/
std::tuple< std::vector<MyMesh::VertexHandle>, std::vector<MyMesh::VertexHandle>, std::vector<MyMesh::VertexHandle> > get_type1_fusion_triplets(MyMesh & mesh){
//if two triangles only, should return
//get open wedges
std::vector <MyMesh::VertexHandle> wedge1;
std::vector <MyMesh::VertexHandle> wedge2;
std::vector <MyMesh::VertexHandle> wedge3;
std::vector <MyMesh::VertexHandle> fusion1;
std::vector <MyMesh::VertexHandle> fusion2;
std::vector <MyMesh::VertexHandle> fusion3;
bool qualifies;
std::vector<int> number_of_common_neighbor_vertices;
auto wedge = get_open_wedge_triplets(mesh);
wedge1 = std::get<0>(wedge);
wedge2 = std::get<1>(wedge);
wedge3 = std::get<2>(wedge);
number_of_common_neighbor_vertices = std::get<3>(wedge);
MyMesh::HalfedgeHandle crack_halfedge;
MyMesh::VertexHandle next_vertex, start_vertex;
//not all open wedges qualify for type1 fusion; need to filter them:
//1. neighboring faces can't fold onto each other
//2. has to be wedge closure, not crack closure
//3. single triangle with dangling others should not be able to close, i.e. w1 - w4 can't be connected
// looks like all these are actually satisfied if the number of common neighbor vertices of w1 and w3 is precisely 1
//loop over all open wedge pairs
for (unsigned int i=0; i<wedge1.size(); ++i){
qualifies=true;
//3. avoid single triangle holes; the problem is that large structures can be attached to such holes via a single edge only;
//those still identified as open wedges, but fusion should not be allowed
if (number_of_common_neighbor_vertices[i] !=1 ){
qualifies=false;
}
if (qualifies){
fusion1.push_back(wedge1[i]);
fusion2.push_back(wedge2[i]);
fusion3.push_back(wedge3[i]);
}
}
return std::make_tuple(fusion1, fusion2, fusion3);
}
/*
Overloads get_type1_fusion_triplets(mesh); Filters for fusion pairs within l_fuse distance
*/
std::tuple< std::vector<MyMesh::VertexHandle>, std::vector<MyMesh::VertexHandle>, std::vector<MyMesh::VertexHandle> > get_type1_fusion_triplets(MyMesh & mesh, double l_fuse){
double l_fuse2 = l_fuse*l_fuse;
std::vector<MyMesh::VertexHandle> v1, v2, v3, v1f, v2f, v3f;
//get topologically fusable triplets
auto fusion_vectors = get_type1_fusion_triplets(mesh);
v1 = std::get<0>(fusion_vectors);
v2 = std::get<1>(fusion_vectors);
v3 = std::get<2>(fusion_vectors);
//filter them for within l_fuse distance
for (unsigned int i=0; i<v1.size(); i++){
if ( (mesh.point(v1[i]) - mesh.point(v3[i])).sqrnorm() < l_fuse2){
v1f.push_back(v1[i]);
v3f.push_back(v3[i]);
v2f.push_back(v2[i]);
}
}
return std::make_tuple(v1f, v2f, v3f);
}
/* type2 fusion is crack closure
this is done by filtering open_wedge_triplets for cracks
returns each fusion triplet TWICE - the reason is that if v1-v2-v3-v4 is a crack, v1-v2-v3 and v3-v4-v1 are the same fusion move but they are detected twice
*/
std::tuple< std::vector<MyMesh::VertexHandle>, std::vector<MyMesh::VertexHandle>, std::vector<MyMesh::VertexHandle> > get_type2_fusion_triplets(MyMesh & mesh){
//need to test if mesh is composed of 2 triangles only
//then there is no type2 fusion
std::vector <MyMesh::VertexHandle> wedge1, wedge2, wedge3, fusion1, fusion2, fusion3;
std::vector<int> number_of_common_neighbor_vertices;
auto wedge = get_open_wedge_triplets(mesh);
wedge1 = std::get<0>(wedge);
wedge2 = std::get<1>(wedge);
wedge3 = std::get<2>(wedge);
number_of_common_neighbor_vertices = std::get<3>(wedge);
bool qualifies;
MyMesh::HalfedgeHandle crack_halfedge;
MyMesh::VertexHandle next_vertex, start_vertex;
//loop over all open wedge pairs
for (unsigned int i=0; i<wedge1.size(); ++i){
qualifies=true;
//crack fusion requires two common neighbors; but not all with 2 common neighbors are crack fusion
if (number_of_common_neighbor_vertices[i] !=2 ){
qualifies=false;
}
//if 2 common neighbors, have to do the crack loop
if (qualifies){
//find a boundary halfedge incident to wedge2
for (MyMesh::VertexEdgeIter edge = mesh.ve_iter(wedge2[i]); edge.is_valid(); ++edge ){
if (mesh.is_boundary((*edge).h0())){
crack_halfedge = (*edge).h0();
}
if (mesh.is_boundary((*edge).h1())){
crack_halfedge = (*edge).h1();
}
}
//do 4 steps and check if can get back to surface_vertex
start_vertex = mesh.from_vertex_handle(crack_halfedge);
//loop_vertices.push_back( start_vertex );
for (int j=0; j<4; ++j){
crack_halfedge = mesh.next_halfedge_handle(crack_halfedge);
next_vertex = mesh.from_vertex_handle(crack_halfedge);
}
//if back to starting vertex after 4 steps
if (next_vertex == start_vertex){
//will need to filter for duplicates; each crack appears 2 times meaning 2 different fusions
//each fusion will be counted twice;
fusion1.push_back(wedge1[i]);
fusion2.push_back(wedge2[i]);
fusion3.push_back(wedge3[i]);
}
}
}
return std::make_tuple(fusion1, fusion2, fusion3);
}
std::tuple< std::vector<MyMesh::VertexHandle>, std::vector<MyMesh::VertexHandle>, std::vector<MyMesh::VertexHandle> > get_type2_fusion_triplets(MyMesh & mesh, double l_fuse){
double l_fuse2 = l_fuse*l_fuse;
std::vector<MyMesh::VertexHandle> v1, v2, v3, v1f, v2f, v3f;
auto fusion_vectors = get_type2_fusion_triplets(mesh);
v1 = std::get<0>(fusion_vectors);
v2 = std::get<1>(fusion_vectors);
v3 = std::get<2>(fusion_vectors);
for (unsigned int i=0; i<v1.size(); i++){
if ( (mesh.point(v1[i]) - mesh.point(v3[i])).sqrnorm() < l_fuse2){
v1f.push_back(v1[i]);
v3f.push_back(v3[i]);
v2f.push_back(v2[i]);
}
}
return std::make_tuple(v1f, v2f, v3f);
}
std::vector<MyMesh::FaceHandle> get_simply_removable_faces(MyMesh & mesh){
std::vector<MyMesh::FaceHandle> removable_faces;
int n_surface_edges;
for (MyMesh::FaceIter fit = mesh.faces_sbegin(); fit!=mesh.faces_end(); ++fit){
n_surface_edges = 0;
for (MyMesh::FaceEdgeIter fe = mesh.fe_iter(*fit); fe.is_valid(); ++fe ){
if (mesh.is_boundary(*fe)){
n_surface_edges++;
}
}
if (n_surface_edges==2){
removable_faces.push_back(*fit);
}
}
return removable_faces;
}
std::vector<MyMesh::FaceHandle> get_simply_removable_faces(MyMesh & mesh, int face_type){
auto face_props = OpenMesh::FProp<FaceProp>(mesh, "face_props");
//find removable subunits topologically
std::vector<MyMesh::FaceHandle> removable_faces_geom = get_simply_removable_faces(mesh);
//filter them for the current subunit type only
std::vector<MyMesh::FaceHandle> removable_faces;
for (auto & rface : removable_faces_geom){
if (face_props[rface].face_type == face_type){
removable_faces.push_back(rface);
}
}
return removable_faces;
}
std::vector<MyMesh::FaceHandle> get_wedge_removable_faces(MyMesh & mesh){
std::vector<MyMesh::FaceHandle> removable_faces;
int n_surface_edges, n_surface_vertices;
for (MyMesh::FaceIter fit = mesh.faces_sbegin(); fit!=mesh.faces_end(); ++fit){
n_surface_edges = 0;
n_surface_vertices = 0;
for (MyMesh::FaceEdgeIter fe = mesh.fe_iter(*fit); fe.is_valid(); ++fe ){
if (mesh.is_boundary(*fe)){
n_surface_edges++;
}
}
for (MyMesh::FaceVertexIter fv = mesh.fv_iter(*fit); fv.is_valid(); ++fv ){
if (mesh.is_boundary(*fv)){
n_surface_vertices++;
}
}
//have to test to make sure structure doesn't fall apart by removing the wedge, eg. /\/\/\/\ zigzag stripe
//looks like this is only possible if wedge hub is a non-surface hub
if ( (n_surface_edges==1) && (n_surface_vertices==2)){
removable_faces.push_back(*fit);
}
}
return removable_faces;
}
std::vector<MyMesh::FaceHandle> get_wedge_removable_faces(MyMesh & mesh, int face_type){
auto face_props = OpenMesh::FProp<FaceProp>(mesh, "face_props");
//find removable subunits geometrically
std::vector<MyMesh::FaceHandle> removable_faces_geom = get_wedge_removable_faces(mesh);
//filter them for the current subunit type only
std::vector<MyMesh::FaceHandle> removable_faces;
for (auto & rface : removable_faces_geom){
if (face_props[rface].face_type == face_type){
removable_faces.push_back(rface);
}
}
return removable_faces;
}
/* (Half)Edge fusion.
Barely any consistency check, will fuse any two boundary halfedges;
Will fuse hedge1.from() to hedge2.to() and hedge1.to() fo hedge2.from()
Returns tuple of two vertices,
1st vertex: hedge1 from_vertex
2nd vertex: hedge2 from_vertex
*/
std::tuple< MyMesh::VertexHandle, MyMesh::VertexHandle > merge_halfedges(MyMesh & mesh, MyMesh::HalfedgeHandle & hedge1, MyMesh::HalfedgeHandle & hedge2){
//both edge1 and edge2 have to be surface edges
if (!(mesh.is_boundary(hedge1) && mesh.is_boundary(hedge2))){
std::cout<<"one of the edges is not boundary, cannot merge"<<std::endl;
}
//they can't be neighboring edges either; then it would be a crack/wedge closure
//may test it here
std::vector<MyMesh::VertexHandle> dummy_face_vhandles;
MyMesh::FaceHandle dummy_face1, dummy_face2;
MyMesh::HalfedgeHandle nonboundary_hedge1, nonboundary_hedge2, dummy_edge1, dummy_edge2;
MyMesh::VertexHandle vA1, vA2, vB1, vB2, vremain1, vremain2;
std::vector<MyMesh::VertexHandle> vh;
//identify the boundary and non-boundary halfedges
nonboundary_hedge1 = mesh.opposite_halfedge_handle(hedge1);
nonboundary_hedge2 = mesh.opposite_halfedge_handle(hedge2);