-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmesh_grob_surface_commands.cpp
1222 lines (1069 loc) · 34.3 KB
/
mesh_grob_surface_commands.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
/*
* OGF/Graphite: Geometry and Graphics Programming Library + Utilities
* Copyright (C) 2000-2009 INRIA - Project ALICE
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* If you modify this software, you should include a notice giving the
* name of the person performing the modification, the date of modification,
* and the reason for such modification.
*
* Contact: Bruno Levy - [email protected]
*
* Project ALICE
* LORIA, INRIA Lorraine,
* Campus Scientifique, BP 239
* 54506 VANDOEUVRE LES NANCY CEDEX
* FRANCE
*
* Note that the GNU General Public License does not permit incorporating
* the Software into proprietary programs.
*
* As an exception to the GPL,
* Graphite can be linked with the following (non-GPL) libraries:
* Qt, SuperLU, WildMagic and CGAL
*/
#include <OGF/mesh/commands/mesh_grob_surface_commands.h>
#include <geogram/mesh/mesh_baking.h>
#include <geogram/mesh/mesh_local_operations.h>
#include <geogram/image/image_library.h>
#include <geogram/image/morpho_math.h>
#ifdef GEOGRAM_WITH_VORPALINE
#include <vorpalib/mesh/mesh_remesh.h>
#include <vorpalib/mesh/mesh_quaddom.h>
#endif
#include <geogram/mesh/mesh_preprocessing.h>
#include <geogram/mesh/mesh_repair.h>
#include <geogram/mesh/mesh_fill_holes.h>
#include <geogram/mesh/mesh_remesh.h>
#include <geogram/mesh/mesh_geometry.h>
#include <geogram/mesh/mesh_surface_intersection.h>
#include <geogram/mesh/mesh_decimate.h>
#include <geogram/mesh/mesh_AABB.h>
#include <geogram/mesh/mesh_subdivision.h>
#include <geogram/mesh/mesh_smoothing.h>
#include <geogram/parameterization/mesh_atlas_maker.h>
#include <geogram/parameterization/mesh_param_packer.h>
#include <geogram/parameterization/mesh_LSCM.h>
#include <geogram/parameterization/mesh_ABF.h>
#include <geogram/NL/nl.h>
#include <geogram/basic/command_line.h>
#include <OGF/scene_graph/types/scene_graph.h>
namespace {
using namespace OGF;
/**
* \brief Pre or post-processes the result of a boolean operation.
* \details Triangulates the facets, collapses the small edges
* and removes self-intersections.
*/
void fix_mesh_for_boolean_ops(Mesh* M) {
GEO::mesh_repair(
*M,
GEO::MeshRepairMode(
GEO::MESH_REPAIR_COLOCATE | GEO::MESH_REPAIR_DUP_F
),
1e-3*GEO::surface_average_edge_length(*M)
);
GEO::tessellate_facets(*M,3);
GEO::mesh_remove_intersections(*M);
}
}
namespace OGF {
MeshGrobSurfaceCommands::MeshGrobSurfaceCommands() {
}
MeshGrobSurfaceCommands::~MeshGrobSurfaceCommands() {
}
void MeshGrobSurfaceCommands::merge_vertices(
double epsilon
) {
epsilon *= (0.01 * bbox_diagonal(*mesh_grob()));
mesh_repair(
*mesh_grob(),
GEO::MeshRepairMode(
GEO::MESH_REPAIR_COLOCATE | GEO::MESH_REPAIR_DUP_F
),
epsilon
);
mesh_grob()->update();
}
void MeshGrobSurfaceCommands::repair_surface(
double epsilon,
double min_comp_area,
double max_hole_area,
double max_hole_edges,
double max_degree3_dist,
bool remove_intersections
) {
double bbox_diagonal = GEO::bbox_diagonal(*mesh_grob());
epsilon *= (0.01 * bbox_diagonal);
double area = GEO::Geom::mesh_area(*mesh_grob(),3);
min_comp_area *= area;
max_hole_area *= area;
mesh_repair(*mesh_grob(), GEO::MESH_REPAIR_DEFAULT, epsilon);
if(min_comp_area != 0.0) {
double nb_f_removed = mesh_grob()->facets.nb();
GEO::remove_small_connected_components(*mesh_grob(), min_comp_area);
nb_f_removed -= mesh_grob()->facets.nb();
if(nb_f_removed != 0) {
GEO::mesh_repair(
*mesh_grob(), GEO::MESH_REPAIR_DEFAULT, epsilon
);
}
}
if(max_hole_area != 0.0 && max_hole_edges != 0) {
GEO::fill_holes(
*mesh_grob(), max_hole_area, index_t(max_hole_edges)
);
}
if(max_degree3_dist > 0.0) {
max_degree3_dist *= (0.01 * bbox_diagonal);
GEO::remove_degree3_vertices(*mesh_grob(), max_degree3_dist);
}
if(remove_intersections) {
Logger::out("Mesh") << "Removing intersections" << std::endl;
GEO::mesh_remove_intersections(*mesh_grob());
Logger::out("Mesh") << "Removed intersections" << std::endl;
}
mesh_grob()->update();
}
void MeshGrobSurfaceCommands::fill_holes(index_t max_nb_vertices) {
GEO::mesh_repair(
*mesh_grob(),
GEO::MESH_REPAIR_TOPOLOGY,
0.0
);
GEO::fill_holes(*mesh_grob(), 1e30, max_nb_vertices, false);
GEO::mesh_repair(
*mesh_grob(),
GEO::MeshRepairMode(
GEO::MESH_REPAIR_COLOCATE | GEO::MESH_REPAIR_DUP_F
),
0.0
);
GEO::mesh_repair(
*mesh_grob(),
GEO::MESH_REPAIR_TOPOLOGY,
0.0
);
mesh_grob()->update();
}
void MeshGrobSurfaceCommands::expand_border(double margin) {
margin *= (GEO::bbox_diagonal(*mesh_grob()) * 0.001);
GEO::expand_border(*mesh_grob(),margin);
mesh_grob()->update();
}
void MeshGrobSurfaceCommands::fix_facets_orientation() {
Attribute<double> attribute;
attribute.bind_if_is_defined(mesh_grob()->facets.attributes(), "visibility");
if(!attribute.is_bound()) {
Logger::err("Attributes") << "visibility" << ": no such facet attribute of type double"
<< std::endl;
Logger::err("Attributes")
<< "use Attributes->Facets->compute facets visibility"
<< std::endl;
return;
}
for(index_t f: mesh_grob()->facets) {
if(attribute[f] < 0.0) {
mesh_grob()->facets.flip(f);
attribute[f] = -attribute[f];
}
}
mesh_grob()->facets.connect();
mesh_grob()->update();
}
void MeshGrobSurfaceCommands::remove_invisible_facets(
double min_visibility
) {
Attribute<double> attribute;
attribute.bind_if_is_defined(
mesh_grob()->facets.attributes(), "visibility"
);
if(!attribute.is_bound()) {
Logger::err("Attributes")
<< "visibility" << ": no such facet attribute of type double"
<< std::endl;
Logger::err("Attributes")
<< "use Attributes->Facets->compute facets visibility"
<< std::endl;
return;
}
vector<index_t> to_kill(mesh_grob()->facets.nb(),0);
for(index_t f: mesh_grob()->facets) {
to_kill[f] = (::fabs(attribute[f]) > min_visibility) ? 0 : 1;
}
mesh_grob()->facets.delete_elements(to_kill);
mesh_grob()->update();
}
MeshGrob* MeshGrobSurfaceCommands::compute_union(
const MeshGrobName& other_name,
const NewMeshGrobName& result_name,
bool pre_process,
bool post_process
) {
return compute_boolean_operation(
other_name, result_name,
"A+B",
pre_process, post_process
);
}
MeshGrob* MeshGrobSurfaceCommands::compute_intersection(
const MeshGrobName& other_name,
const NewMeshGrobName& result_name,
bool pre_process,
bool post_process
) {
return compute_boolean_operation(
other_name, result_name,
"A*B",
pre_process, post_process
);
}
MeshGrob* MeshGrobSurfaceCommands::compute_difference(
const MeshGrobName& other_name,
const NewMeshGrobName& result_name,
bool pre_process,
bool post_process
) {
return compute_boolean_operation(
other_name, result_name,
"A-B",
pre_process, post_process
);
}
MeshGrob* MeshGrobSurfaceCommands::compute_boolean_operation(
const MeshGrobName& other_name,
const NewMeshGrobName& result_name,
const std::string& operation,
bool pre_process,
bool post_process
) {
MeshGrob* other = MeshGrob::find(scene_graph(), other_name);
if(other == nullptr) {
Logger::err("Booleans") << other_name << ": no such MeshGrob"
<< std::endl;
return nullptr;
}
if(other == mesh_grob()) {
Logger::err("Booleans") << "Mesh and operand are the same"
<< std::endl;
return nullptr;
}
if(!mesh_grob()->facets.are_simplices()) {
Logger::err("Booleans") << "Mesh is not triangulated" << std::endl;
return nullptr;
}
if(!other->facets.are_simplices()) {
Logger::err("Booleans") << other_name << " is not triangulated"
<< std::endl;
return nullptr;
}
MeshGrob* result = MeshGrob::find_or_create(scene_graph(), result_name);
if(pre_process) {
fix_mesh_for_boolean_ops(mesh_grob());
mesh_grob()->update();
fix_mesh_for_boolean_ops(other);
other->update();
}
mesh_boolean_operation(*result, *mesh_grob(), *other, operation);
if(post_process) {
fix_mesh_for_boolean_ops(result);
}
result->update();
return result;
}
void MeshGrobSurfaceCommands::intersect(
bool remove_internal_shells,
bool simplify_coplanar_facets,
double coplanar_angle_tolerance,
bool interpolate_attributes,
bool verbose,
const NewMeshGrobName& skeleton
) {
MeshSurfaceIntersection intersection(*mesh_grob());
intersection.set_delaunay(true);
intersection.set_detect_intersecting_neighbors(true);
intersection.set_radial_sort(remove_internal_shells);
intersection.set_interpolate_attributes(interpolate_attributes);
intersection.set_verbose(verbose);
MeshGrob* skl = nullptr;
if(skeleton != "") {
skl = MeshGrob::find_or_create(scene_graph(),skeleton);
skl->clear();
intersection.set_build_skeleton(skl);
}
intersection.intersect();
if(remove_internal_shells) {
intersection.remove_internal_shells();
}
if(simplify_coplanar_facets) {
intersection.simplify_coplanar_facets(coplanar_angle_tolerance);
}
show_mesh();
mesh_grob()->update();
if(skl != nullptr) {
skl->update();
}
}
MeshGrob* MeshGrobSurfaceCommands::remesh_smooth(
const NewMeshGrobName& surface_name_in,
unsigned int nb_points,
double tri_shape_adaptation,
double tri_size_adaptation,
bool adjust,
double adjust_max_edge_distance,
unsigned int nb_normal_iter,
unsigned int nb_Lloyd_iter,
unsigned int nb_Newton_iter,
unsigned int Newton_m,
unsigned int LFS_samples
) {
std::string surface_name = surface_name_in;
if(surface_name == mesh_grob()->name()) {
Logger::err("Remesh")
<< "remesh should not be the same as mesh"
<< std::endl;
return nullptr;
}
if(mesh_grob()->facets.nb() == 0) {
Logger::err("Remesh")
<< "mesh has no facet" << std::endl;
return nullptr;
}
if(!mesh_grob()->facets.are_simplices()) {
Logger::err("Remesh")
<< "mesh need to be simplicial, use repair"
<< std::endl;
return nullptr;
}
index_t dimension = mesh_grob()->vertices.dimension();
MeshGrob* remesh = MeshGrob::find_or_create(
scene_graph(), surface_name
);
remesh->clear();
remesh->lock_graphics();
if(tri_shape_adaptation != 0.0) {
tri_shape_adaptation *= 0.02;
GEO::compute_normals(*mesh_grob());
mesh_grob()->update();
if(nb_normal_iter != 0) {
GEO::Logger::out("Nsmooth") << "Smoothing normals, "
<< nb_normal_iter
<< " iteration(s)" << std::endl;
GEO::simple_Laplacian_smooth(
*mesh_grob(), nb_normal_iter, true
);
}
GEO::set_anisotropy(*mesh_grob(), tri_shape_adaptation);
mesh_grob()->update();
} else {
mesh_grob()->vertices.set_dimension(3);
mesh_grob()->update();
}
if(tri_size_adaptation != 0.0) {
GEO::compute_sizing_field(
*mesh_grob(), tri_size_adaptation, LFS_samples
);
mesh_grob()->update();
} else {
AttributesManager& attributes = mesh_grob()->vertices.attributes();
if(attributes.is_defined("weight")) {
attributes.delete_attribute_store("weight");
mesh_grob()->update();
}
}
GEO::remesh_smooth(
*mesh_grob(), *remesh,
nb_points, 0,
nb_Lloyd_iter, nb_Newton_iter, Newton_m,
adjust, adjust_max_edge_distance
);
show_mesh(remesh);
remesh->unlock_graphics();
remesh->update();
// If anisotropic remeshing was used, then this lifts
// the mesh into 6D space. Here we restore the initial
// 3D setting.
if(mesh_grob()->vertices.dimension() != dimension) {
mesh_grob()->vertices.set_dimension(dimension);
}
// Need to update this one as well, since vertices
// order may have changed
mesh_grob()->update();
return remesh;
}
MeshGrob* MeshGrobSurfaceCommands::remesh_feature_sensitive(
const NewMeshGrobName& surface_name,
unsigned int nb_points,
bool refine,
double max_dist,
double normal_anisotropy,
unsigned int nb_Lloyd_iter,
unsigned int nb_Newton_iter,
unsigned int nb_LpCVT_iter,
unsigned int Newton_m,
bool RVC_centroids
) {
if(surface_name == mesh_grob()->name()) {
Logger::err("Remesh")
<< "remesh should not be the same as mesh"
<< std::endl;
return nullptr;
}
if(mesh_grob()->facets.nb() == 0) {
Logger::err("Remesh")
<< "mesh has no facet" << std::endl;
return nullptr;
}
if(!mesh_grob()->facets.are_simplices()) {
Logger::err("Remesh")
<< "mesh need to be simplicial, use repair"
<< std::endl;
return nullptr;
}
#ifdef GEOGRAM_WITH_VORPALINE
MeshGrob* remesh = MeshGrob::find_or_create(
scene_graph(), surface_name
);
remesh->clear();
bool RVC_centroids_bkp =
GEO::CmdLine::get_arg_bool("remesh:RVC_centroids");
GEO::CmdLine::set_arg("remesh:RVC_centroids",RVC_centroids);
GEO::remesh_feature_sensitive(
*mesh_grob(), *remesh,
nb_points,
refine, max_dist,
normal_anisotropy,
nb_Lloyd_iter, nb_Newton_iter, nb_LpCVT_iter, Newton_m
);
GEO::CmdLine::set_arg("remesh:RVC_centroids",RVC_centroids_bkp);
show_mesh(remesh);
remesh->update();
// Need to update this one as well,
// since vertices order may have changed
mesh_grob()->update();
return remesh;
#else
geo_argused(surface_name);
geo_argused(nb_points);
geo_argused(normal_anisotropy);
geo_argused(nb_Lloyd_iter);
geo_argused(nb_Newton_iter);
geo_argused(nb_LpCVT_iter);
geo_argused(Newton_m);
geo_argused(RVC_centroids);
geo_argused(refine);
geo_argused(max_dist);
Logger::err("Remesh") << "Needs Vorpaline/Vorpalib"
<< std::endl;
Logger::err("Remesh") << "Vorpaline is marked by Tessael"
<< std::endl;
Logger::err("Remesh") << "See https://www.tessael.com"
<< std::endl;
Logger::err("Remesh") << "or [email protected]"
<< std::endl;
return nullptr;
#endif
}
/***********************************************************/
MeshGrob* MeshGrobSurfaceCommands::remesh_quad_dominant(
const NewMeshGrobName& surface_name,
double rel_edge_len,
bool sharp_features,
bool optimize_parity,
double max_scaling_corr
) {
if(surface_name == mesh_grob()->name()) {
Logger::err("Remesh")
<< "remesh should not be the same as mesh"
<< std::endl;
return nullptr;
}
if(mesh_grob()->facets.nb() == 0) {
Logger::err("Remesh")
<< "mesh has no facet" << std::endl;
return nullptr;
}
if(!mesh_grob()->facets.are_simplices()) {
Logger::err("Remesh")
<< "mesh need to be simplicial, use repair"
<< std::endl;
return nullptr;
}
#ifdef GEOGRAM_WITH_VORPALINE
MeshGrob* remesh = MeshGrob::find_or_create(
scene_graph(), surface_name
);
remesh->clear();
mesh_quad_dominant(
*mesh_grob(), *remesh,
rel_edge_len,
sharp_features,
optimize_parity,
max_scaling_corr
);
show_mesh(remesh);
remesh->update();
// Need to update this one as well,
// since vertices order may have changed
mesh_grob()->update();
return remesh;
#else
geo_argused(surface_name);
geo_argused(rel_edge_len);
geo_argused(optimize_parity);
geo_argused(max_scaling_corr);
geo_argused(sharp_features);
Logger::err("Remesh") << "Needs Vorpaline/Vorpalib"
<< std::endl;
Logger::err("Remesh") << "Vorpaline is marked by Tessael"
<< std::endl;
Logger::err("Remesh") << "See https://www.tessael.com"
<< std::endl;
Logger::err("Remesh") << "or [email protected]"
<< std::endl;
return nullptr;
#endif
}
/***********************************************************/
void MeshGrobSurfaceCommands::decimate(
index_t nb_bins,
bool remove_deg3_vrtx,
bool keep_borders,
bool repair
) {
MeshDecimateMode mode = MESH_DECIMATE_DUP_F;
if(remove_deg3_vrtx) {
mode = MeshDecimateMode(mode | MESH_DECIMATE_DEG_3);
}
if(keep_borders) {
mode = MeshDecimateMode(mode | MESH_DECIMATE_KEEP_B);
}
mesh_decimate_vertex_clustering(*mesh_grob(), nb_bins, mode);
if(repair) {
repair_surface();
}
show_mesh();
mesh_grob()->update();
}
/***********************************************************/
void MeshGrobSurfaceCommands::split_triangles(index_t nb_times) {
if(!mesh_grob()->facets.are_simplices()) {
Logger::err("Split") << "Mesh is not simplicial, cannot split."
<< std::endl;
return;
}
for(index_t i=0; i<nb_times; ++i) {
mesh_split_triangles(*mesh_grob());
}
show_mesh();
mesh_grob()->update();
}
void MeshGrobSurfaceCommands::split_quads(index_t nb_times) {
for(index_t i=0; i<nb_times; ++i) {
mesh_split_quads(*mesh_grob());
}
show_mesh();
mesh_grob()->update();
}
void MeshGrobSurfaceCommands::split_catmull_clark(index_t nb_times) {
for(index_t i=0; i<nb_times; ++i) {
mesh_split_catmull_clark(*mesh_grob());
}
show_mesh();
mesh_grob()->update();
}
void MeshGrobSurfaceCommands::tessellate_facets(
index_t max_vertices_per_facet
) {
GEO::tessellate_facets(*mesh_grob(),max_vertices_per_facet);
if(mesh_grob()->get_shader() != nullptr) {
mesh_grob()->get_shader()->set_property(
"mesh_style", "true;0 0 0 1;1"
);
}
show_mesh();
mesh_grob()->update();
}
void MeshGrobSurfaceCommands::triangulate_center_vertex() {
GEO::mesh_triangulate_center_vertex(*mesh_grob());
if(mesh_grob()->get_shader() != nullptr) {
mesh_grob()->get_shader()->set_property(
"mesh_style", "true;0 0 0 1;1"
);
}
show_mesh();
mesh_grob()->update();
}
void MeshGrobSurfaceCommands::smooth() {
Attribute<bool> v_is_locked(
mesh_grob()->vertices.attributes(),"selection"
);
bool has_locked_v = false;
for(index_t v: mesh_grob()->vertices) {
if(v_is_locked[v]) {
has_locked_v = true;
break;
}
}
if(!has_locked_v) {
Logger::err("Smooth") << "Mesh has no locked vertex"
<< std::endl;
return;
}
mesh_smooth(*mesh_grob());
mesh_grob()->update();
}
void MeshGrobSurfaceCommands::make_texture_atlas(
bool detect_sharp_edges,
double sharp_edges_threshold,
ChartParameterizer param,
ChartPacker pack,
bool verbose
) {
if(!mesh_grob()->facets.are_simplices()) {
Logger::err("MAM")
<< "For now, only simplicial meshes are supported"
<< std::endl;
return;
}
if(!detect_sharp_edges) {
sharp_edges_threshold = 360.0;
}
mesh_make_atlas(
*mesh_grob(),
sharp_edges_threshold,
param,
pack,
verbose
);
show_UV();
mesh_grob()->update();
}
void MeshGrobSurfaceCommands::pack_texture_space(
ChartPacker pack
) {
switch(pack) {
case PACK_NONE:
break;
case PACK_TETRIS:
pack_atlas_using_tetris_packer(*mesh_grob());
break;
case PACK_XATLAS:
pack_atlas_only_normalize_charts(*mesh_grob());
pack_atlas_using_xatlas(*mesh_grob());
break;
}
show_UV();
mesh_grob()->update();
}
void MeshGrobSurfaceCommands::parameterize_chart(
const std::string& attribute, ChartParameterizer algo, bool verbose
) {
switch(algo) {
case PARAM_PROJECTION:
Logger::out("Param") << "Not implemented for single chart"
<< std::endl;
return;
break;
case PARAM_LSCM:
mesh_compute_LSCM(*mesh_grob(), attribute, false, "", verbose);
break;
case PARAM_SPECTRAL_LSCM:
mesh_compute_LSCM(*mesh_grob(), attribute, true, "", verbose);
break;
case PARAM_ABF:
if(!mesh_grob()->facets.are_simplices()) {
Logger::err("ABF")
<< "Mesh facets need to be triangles" << std::endl;
return ;
}
mesh_compute_ABF_plus_plus(*mesh_grob(), attribute, verbose);
break;
}
show_UV("vertices." + attribute);
mesh_grob()->update();
}
void MeshGrobSurfaceCommands::bake_normals(
const MeshGrobName& surface,
index_t size,
const NewImageFileName& image,
index_t nb_dilate,
const std::string& attribute
) {
Attribute<double> tex_coord;
tex_coord.bind_if_is_defined(
mesh_grob()->facet_corners.attributes(), attribute
);
if(!tex_coord.is_bound()) {
Logger::err("baking")
<< attribute << ": no such facet corner attribute" << std::endl;
return;
}
if(tex_coord.dimension() != 2) {
Logger::err("baking")
<< attribute << ": wrong dimension" << std::endl;
return;
}
Image_var normal_map = new Image(Image::RGB, Image::BYTE, size, size);
MeshGrob* highres = MeshGrob::find(scene_graph(),surface);
if(highres == mesh_grob()) {
// Case 1: bake normals from the parameterized surface.
bake_mesh_facet_normals(mesh_grob(), normal_map);
} else {
// Case 2: bake normals from a different highres surface.
// Step 1: create a "geometry image" from the parameterized
// mesh.
Logger::out("baking") << "Creating geometry image"
<< std::endl;
Image_var geometry_image = new Image(
Image::RGB, Image::FLOAT64, size, size
);
bake_mesh_geometry(mesh_grob(),geometry_image);
// Step 2: create the normal map by looking up the high res
// facet nearest to each point from the geometry image.
if(highres->facets.nb() != 0) {
Logger::out("baking")
<< "Transferring highres surface normals to geometry image"
<< std::endl;
bake_mesh_facet_normals_indirect(
geometry_image, normal_map, highres
);
} else {
Logger::out("baking")
<< "Transferring highres pointset normals to geometry image"
<< std::endl;
Attribute<double> normal;
normal.bind_if_is_defined(
highres->vertices.attributes(), "normal"
);
if(!normal.is_bound()) {
Logger::out("baking")
<< "\'normal\': no such vertex attribute"
<< std::endl;
return;
}
bake_mesh_points_attribute_indirect(
geometry_image, normal_map, highres, normal,
1.0, 0.5 // scale and bias to map [-1,1] to [0,1]
);
}
}
MorphoMath mm(normal_map);
mm.dilate(nb_dilate);
ImageLibrary::instance()->save_image(image, normal_map);
Object* shader = mesh_grob()->get_shader();
if(shader != nullptr) {
shader->set_property("painting","TEXTURE");
shader->set_property("tex_image",image);
shader->set_property("tex_coords","facet_corners." + attribute);
shader->set_property("tex_repeat","1");
shader->set_property("normal_map","true");
shader->set_property("lighting","true");
}
mesh_grob()->update();
}
void MeshGrobSurfaceCommands::bake_colors(
const MeshGrobName& surface,
const std::string& color_attr_name,
index_t size,
const NewImageFileName& image,
index_t nb_dilate,
const std::string& attribute
) {
Attribute<double> tex_coord;
tex_coord.bind_if_is_defined(
mesh_grob()->facet_corners.attributes(), attribute
);
if(!tex_coord.is_bound()) {
Logger::err("baking")
<< attribute << ": no such facet corner attribute" << std::endl;
return;
}
if(tex_coord.dimension() != 2) {
Logger::err("baking")
<< attribute << ": wrong dimension" << std::endl;
return;
}
MeshGrob* highres = MeshGrob::find(scene_graph(),surface);
if(highres == nullptr) {
Logger::err("baking") << surface << ": no such MeshGrob"
<< std::endl;
return;
}
Attribute<double> color;
color.bind_if_is_defined(
highres->vertices.attributes(), color_attr_name
);
if(!color.is_bound()) {
Logger::err("baking") << color_attr_name
<< ": no such vertex attribute"
<< std::endl;
return;
}
if(color.dimension() < 3) {
Logger::err("baking") << color_attr_name
<< ": wront dimension" << std::endl;
return;
}
Image_var color_map = new Image(Image::RGB, Image::BYTE, size, size);
if(highres == mesh_grob()) {
// Case 1: bake colors from the parameterized surface.
bake_mesh_attribute(mesh_grob(), color_map, color);
} else {
// Case 2: bake normals from a different highres surface.
// Step 1: create a "geometry image" from the parameterized
// mesh.
Logger::out("baking") << "Creating geometry image"
<< std::endl;
Image_var geometry_image = new Image(
Image::RGB, Image::FLOAT64, size, size
);
bake_mesh_geometry(mesh_grob(),geometry_image);
// Step 2: create the color map by looking up the high res
// point nearest to each point from the geometry image.
bake_mesh_points_attribute_indirect(
geometry_image, color_map, highres, color
);
}
MorphoMath mm(color_map);
mm.dilate(nb_dilate);
ImageLibrary::instance()->save_image(image, color_map);
Object* shader = mesh_grob()->get_shader();
if(shader != nullptr) {
shader->set_property("painting","TEXTURE");
shader->set_property("tex_image",image);
shader->set_property("tex_coords","facet_corners." + attribute);
shader->set_property("tex_repeat","1");
shader->set_property("normal_map","false");
}
mesh_grob()->update();
}
void MeshGrobSurfaceCommands::bake_texture(
const MeshGrobName& src_surface_name,
const ImageFileName& src_texture_name,
const std::string& src_tex_coord_name,
index_t size,
const NewImageFileName& image_name,
index_t nb_dilate,
const std::string& tex_coord_name
) {
Attribute<double> tex_coord;
tex_coord.bind_if_is_defined(
mesh_grob()->facet_corners.attributes(), tex_coord_name
);
if(!tex_coord.is_bound()) {
Logger::err("baking")
<< tex_coord_name
<< ": no such facet corner attribute" << std::endl;
return;
}
if(tex_coord.dimension() != 2) {
Logger::err("baking")
<< tex_coord_name << ": wrong dimension" << std::endl;
return;
}
MeshGrob* src_surface = MeshGrob::find(scene_graph(),src_surface_name);
if(src_surface == nullptr) {
Logger::err("baking") << src_surface_name << ": no such MeshGrob"
<< std::endl;
return;
}