-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathmain.cpp
1910 lines (1681 loc) · 68.6 KB
/
main.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
/*
* Copyright (c) 2000-2022 Inria
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the ALICE Project-Team nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Contact: Bruno Levy
*
* https://www.inria.fr/fr/bruno-levy
*
* Inria,
* Domaine de Voluceau,
* 78150 Le Chesnay - Rocquencourt
* FRANCE
*
*/
#include <geogram_gfx/gui/simple_mesh_application.h>
#include <geogram/mesh/mesh_repair.h>
#include <geogram/mesh/mesh_fill_holes.h>
#include <geogram/mesh/mesh_degree3_vertices.h>
#include <geogram/mesh/mesh_surface_intersection.h>
#include <geogram/mesh/mesh_geometry.h>
#include <geogram/mesh/mesh_preprocessing.h>
#include <geogram/mesh/mesh_remesh.h>
#include <geogram/mesh/mesh_decimate.h>
#include <geogram/mesh/mesh_tetrahedralize.h>
#include <geogram/mesh/mesh_topology.h>
#include <geogram/mesh/mesh_AABB.h>
#include <geogram/mesh/mesh_baking.h>
#include <geogram/mesh/mesh_manifold_harmonics.h>
#include <geogram/mesh/mesh_io.h>
#include <geogram/parameterization/mesh_atlas_maker.h>
#include <geogram/image/image.h>
#include <geogram/image/image_library.h>
#include <geogram/image/morpho_math.h>
#include <geogram/delaunay/LFS.h>
#include <geogram/points/co3ne.h>
#include <geogram/points/kd_tree.h>
#include <geogram/third_party/PoissonRecon/poisson_geogram.h>
#include <geogram/basic/stopwatch.h>
#include <geogram/basic/command_line.h>
#include <geogram/basic/command_line_args.h>
#include <geogram/basic/file_system.h>
#include <geogram/basic/line_stream.h>
#include <stack>
/**********************************************************************/
namespace {
using namespace GEO;
/**
* \brief A class to read raw scanner input from
* http://graphics.stanford.edu/data/3Dscanrep/.
*/
class StanfordScannerReader : public MeshIOHandler {
public:
/**
* \brief Reads a .conf file from the Standord scanning repository.
* \details Inherits MeshIOHandler, declared to Mesh IO system
* in GeoBoxApplication::geogram_initialize();
*/
bool load(
const std::string& filename, Mesh& mesh,
const MeshIOFlags& ioflags = MeshIOFlags()
) override {
geo_argused(ioflags);
mesh.clear();
LineInput in(filename);
if(!in.OK()) {
Logger::err("geobox")
<< "Could not open file " << filename << std::endl;
return false;
}
Attribute<int> chart(mesh.vertices.attributes(),"chart");
int current_chart = 0;
while(!in.eof() && in.get_line()) {
in.get_fields();
if(in.nb_fields() == 0) { continue ; }
if(in.field_matches(0,"bmesh")) {
std::string part_filename = in.field(1);
part_filename =
FileSystem::dir_name(filename) + "/" + part_filename;
// Translation vector
double Tx = in.field_as_double(2);
double Ty = in.field_as_double(3);
double Tz = in.field_as_double(4);
/// Quaternion
double Qx = in.field_as_double(5);
double Qy = in.field_as_double(6);
double Qz = in.field_as_double(7);
double Qw = in.field_as_double(8);
setup_transform_from_translation_and_quaternion(
Tx,Ty,Tz,Qx,Qy,Qz,Qw
);
Mesh part;
if(!mesh_load(part_filename,part)) {
return false;
}
index_t v_offset = mesh.vertices.nb();
mesh.vertices.create_vertices(part.vertices.nb());
for(index_t v: part.vertices) {
// Transform the vertex in place
transform(part.vertices.point_ptr(v));
// Copy the vertex to our mesh.
if(mesh.vertices.single_precision()) {
for(index_t c=0; c<3; ++c) {
mesh.vertices.single_precision_point_ptr(
v + v_offset
)[c] = float(part.vertices.point_ptr(v)[c]);
}
} else {
for(index_t c=0; c<3; ++c) {
mesh.vertices.point_ptr(v + v_offset)[c] =
double(part.vertices.point_ptr(v)[c]);
}
}
if(chart.is_bound()) {
chart[v + v_offset] = current_chart;
}
}
++current_chart;
}
}
return true;
}
bool save(
const Mesh& mesh, const std::string& filename,
const MeshIOFlags& ioflags
) override {
geo_argused(mesh);
geo_argused(filename);
geo_argused(ioflags);
return false;
}
protected:
void setup_transform_from_translation_and_quaternion(
double Tx, double Ty, double Tz,
double Qx, double Qy, double Qz, double Qw
) {
/* for unit q, just set s = 2 or set xs = Qx + Qx, etc. */
double s = 2.0 / (Qx*Qx + Qy*Qy + Qz*Qz + Qw*Qw);
double xs = Qx * s;
double ys = Qy * s;
double zs = Qz * s;
double wx = Qw * xs;
double wy = Qw * ys;
double wz = Qw * zs;
double xx = Qx * xs;
double xy = Qx * ys;
double xz = Qx * zs;
double yy = Qy * ys;
double yz = Qy * zs;
double zz = Qz * zs;
M[0][0] = 1.0 - (yy + zz);
M[0][1] = xy - wz;
M[0][2] = xz + wy;
M[0][3] = 0.0;
M[1][0] = xy + wz;
M[1][1] = 1 - (xx + zz);
M[1][2] = yz - wx;
M[1][3] = 0.0;
M[2][0] = xz - wy;
M[2][1] = yz + wx;
M[2][2] = 1 - (xx + yy);
M[2][3] = 0.0;
M[3][0] = Tx;
M[3][1] = Ty;
M[3][2] = Tz;
M[3][3] = 1.0;
}
void transform(double* xyz) {
double xyzw[4] ;
for(unsigned int c=0; c<4; c++) {
xyzw[c] = M[3][c] ;
}
for(unsigned int j=0; j<4; j++) {
for(unsigned int i=0; i<3; i++) {
xyzw[j] += M[i][j] * xyz[i] ;
}
}
for(unsigned int c=0; c<3; c++) {
xyz[c] = xyzw[c] / xyzw[3] ;
}
}
private:
double M[4][4] ;
};
}
/**********************************************************************/
namespace {
using namespace GEO;
/**
* \brief Graphic interface to geometry processing functionalities
* of geogram.
*/
class GeoBoxApplication : public SimpleMeshApplication {
public:
enum TextureMode {
NO_TEXTURE=0,
UV_GRID=1,
RGB_TEXTURE=2,
NORMAL_MAP=3
};
/**
* \brief GeoBoxApplication constructor.
*/
GeoBoxApplication() : SimpleMeshApplication("GeoBox") {
texture_ = 0;
checker_texture_ = 0;
texture_mode_ = NO_TEXTURE;
}
void geogram_initialize(int argc, char** argv) override {
GEO::initialize(GEO::GEOGRAM_INSTALL_ALL);
geo_register_MeshIOHandler_creator(StanfordScannerReader,"conf");
GEO::CmdLine::import_arg_group("co3ne");
GEO::CmdLine::import_arg_group("pre");
GEO::CmdLine::import_arg_group("post");
GEO::CmdLine::import_arg_group("remesh");
GEO::CmdLine::import_arg_group("opt");
GEO::CmdLine::import_arg_group("tet");
SimpleMeshApplication::geogram_initialize(argc, argv);
}
void show_attributes() override {
SimpleMeshApplication::show_attributes();
texture_mode_ = NO_TEXTURE;
}
bool save(const std::string& filename) override {
bool result = true;
MeshIOFlags flags;
if(!texture_image_.is_null()) {
std::string tex_filename = "";
if(texture_mode_ == RGB_TEXTURE) {
tex_filename =
FileSystem::base_name(filename) + "_texture.png";
} else if(texture_mode_ == NORMAL_MAP) {
tex_filename =
FileSystem::base_name(filename) + "_normals.png";
}
if(tex_filename != "") {
// When saving in ".obj" file format, this will
// declare a material with the right texture.
flags.set_texture_filename(tex_filename);
tex_filename =
FileSystem::dir_name(filename) + "/" + tex_filename;
Logger::out("geobox") << "Saving texture to "
<< tex_filename
<< std::endl;
ImageLibrary::instance()->save_image(
tex_filename, texture_image_
);
}
}
if(CmdLine::get_arg_bool("attributes")) {
flags.set_attribute(MESH_FACET_REGION);
flags.set_attribute(MESH_CELL_REGION);
}
if(FileSystem::extension(filename) == "geogram") {
begin();
}
if(mesh_save(mesh_, filename, flags)) {
current_file_ = filename;
} else {
result = false;
}
if(FileSystem::extension(filename) == "geogram") {
end();
}
return result;
}
void draw_about() override {
ImGui::Separator();
if(ImGui::BeginMenu(icon_UTF8("info") + " About...")) {
ImGui::Text(
" GEObox\n"
" The geometry processing toolbox\n"
"\n"
);
float sz = float(280.0 * std::min(scaling(), 2.0));
if(phone_screen_) {
sz /= 4.0f;
}
ImGui::Image(
convert_to_ImTextureID(geogram_logo_texture_),
ImVec2(sz, sz)
);
ImGui::Text(
"\n"
"With algorithms from:\n"
"* ERC StG GOODSHAPE (StG-205693)\n"
"* ERC PoC VORPALINE (PoC-334829)\n"
" ...as well as new ones.\n"
);
ImGui::Separator();
ImGui::Text(
#ifdef GEO_OS_EMSCRIPTEN
"This version runs in your webbrowser\n"
"using Emscripten.\n"
"To get a (faster!) native executable\n"
"and the sources, see:"
#endif
" (C)opyright 2006-2023\n"
" Inria\n"
);
// ImGui::Text("\n");
ImGui::Separator();
ImGui::Text(
"%s",
(
"GEOGRAM version:" +
Environment::instance()->get_value("version")
).c_str()
);
ImGui::EndMenu();
}
}
/**
* \brief Draws and manages the menus and the commands.
* \details Overloads Application::draw_application_menus()
*/
void draw_application_menus() override {
if(ImGui::BeginMenu("Points")) {
if(ImGui::MenuItem("smooth point set")) {
GEO::Command::set_current(
"void smooth( "
" index_t nb_iterations=2 [number of iterations], "
" index_t nb_neighbors=30 [number of nearest neigh.]"
") [smoothes a pointset]",
this, &GeoBoxApplication::smooth_point_set
);
}
if(ImGui::MenuItem("filter outliers")) {
GEO::Command::set_current(
"void filter_outliers( "
" index_t N=70 [number of neighbors],"
" double R=0.01 [distance threshold]"
") [removes outliers from pointset]",
this, &GeoBoxApplication::filter_outliers
);
}
if(ImGui::MenuItem("reconstruct Co3Ne")) {
GEO::Command::set_current(
"void reconstruct_Co3Ne( "
" double radius=5.0 [search radius (in % bbox. diag.)],"
" index_t nb_smth_iter=2 [number of smoothing iterations], "
" index_t nb_neighbors=30 [number of nearest neighbors] "
") [reconstructs a surface from a pointset]",
this, &GeoBoxApplication::reconstruct_Co3Ne
);
}
if(ImGui::MenuItem("reconstruct Poisson")) {
GEO::Command::set_current(
"void reconstruct_Poisson( "
" index_t depth=8 [octree depth] "
") [reconstructs a surface from a pointset]",
this, &GeoBoxApplication::reconstruct_Poisson
);
}
ImGui::EndMenu();
}
if(ImGui::BeginMenu("Surface")) {
ImGui::MenuItem(" Repair", nullptr, false, false);
if(ImGui::MenuItem("repair surface")) {
GEO::Command::set_current(
" void repair( "
" double epsilon = 1e-6 [point merging tol. (% bbox. diag.)],"
" double min_comp_area = 0.03 "
" [for removing small cnx (% total area)], "
" double max_hole_area = 1e-3 "
" [for filling holes (% total area)], "
" index_t max_hole_edges = 2000 "
" [max. nb. edges in filled hole], "
" double max_degree3_dist = 0.0 "
" [for removing deg3 vrtx (% bbox. diag.)],"
" bool remove_isect = false "
" [remove intersecting triangles] "
" ) [repairs a surfacic mesh]",
this, &GeoBoxApplication::repair_surface
);
}
if(ImGui::MenuItem("merge vertices")) {
GEO::Command::set_current(
"void merge_vertices( "
" double epsilon=1e-6 "
" [tolerance for merging vertices (in % bbox diagonal)],"
") [merges the vertices that are within tolerance] ",
this, &GeoBoxApplication::merge_vertices
);
}
if(ImGui::MenuItem("intersect")) {
GEO::Command::set_current(
"void intersect("
"bool neighbors = true"
" [test neighboring triangles for intersections],"
"bool union = true"
" [removes internal shells],"
"bool coplanar = true"
" [re-triangulate sets of coplanar facets],"
"double max_angle = 0.0"
" [angle tolerance in degrees for coplanar facets],"
"bool verbose = false"
" [diplay lots of information messages]"
") [removes surface mesh intersections]",
this, &GeoBoxApplication::intersect
);
}
if(ImGui::MenuItem("keep largest part")) {
GEO::Command::set_current(
"void keep_largest_component( "
" bool are_you_sure=true "
") [keeps only the largest connected component]",
this, &GeoBoxApplication::keep_largest_component
);
}
ImGui::Separator();
ImGui::MenuItem(" Remesh", nullptr, false, false);
if(ImGui::MenuItem("remesh smooth")) {
GEO::Command::set_current(
"void remesh_smooth( "
#ifdef GEO_OS_EMSCRIPTEN
" index_t nb_points = 5000 [number of points in remesh],"
#else
" index_t nb_points = 30000 [number of points in remesh],"
#endif
" double tri_shape_adapt = 1.0 "
" [triangles shape adaptation], "
" double tri_size_adapt = 0.0 "
" [triangles size adaptation], "
" index_t normal_iter = 3 [nb normal smoothing iter.], "
" index_t Lloyd_iter = 5 [nb Lloyd iter.], "
" index_t Newton_iter = 30 [nb Newton iter.], "
" index_t Newton_m = 7 [nb Newton eval. per step], "
" index_t LFS_samples = 10000 "
" [nb samples (used if size adapt != 0)] "
")",
this, &GeoBoxApplication::remesh_smooth
);
}
if(ImGui::MenuItem("decimate")) {
GEO::Command::set_current(
"void decimate( "
" index_t nb_bins = 100 [the higher-the more precise], "
" bool remove_deg3_vrtx = true [remove degree3 vertices],"
" bool keep_borders = true, "
" bool repair = true "
") [quick and dirty mesh decimator (vertex clustering)]",
this, &GeoBoxApplication::decimate
);
}
ImGui::Separator();
ImGui::MenuItem(" Texture mapping...", nullptr,false,false);
if(ImGui::MenuItem("make texture atlas")) {
Command::set_current(
"void make_texture_atlas("
" bool detect_sharp_edges=false [detect sharp edges],"
" bool use_ABF=false [use angle-based flattening],"
" bool use_XATLAS=true [use XATLAS packer]"
") [generates UV coordinates]",
this, &GeoBoxApplication::make_texture_atlas
);
}
if(ImGui::MenuItem("remesh + normal map")) {
Command::set_current(
"void remesh_with_normal_map("
" index_t nb_vertices=5000 [desired number of vertices],"
" double anisotropy=0.2 [curvature adaptation],"
" bool hires=false [use highres normal map]"
") [remesh and generate normal-mapped mesh]",
this, &GeoBoxApplication::remesh_with_normal_map
);
}
ImGui::Separator();
ImGui::MenuItem(" Create...", nullptr, false, false);
if(ImGui::MenuItem("create cube")) {
GEO::Command::set_current(
"void create_cube("
" double x1=0, double y1=0, double z1=0,"
" double x2=1, double y2=1, double z2=1"
")",
this, &GeoBoxApplication::create_cube
);
}
if(ImGui::MenuItem("create icosahedron")) {
create_icosahedron();
}
ImGui::EndMenu();
}
if(ImGui::BeginMenu("Volume")) {
if(ImGui::MenuItem("tet meshing")) {
Command::set_current(
"void tet_meshing("
" bool preprocess=true [preprocesses the surface], "
" bool refine=true [insert points to improve quality],"
" double quality=1.0 [the smaller - the higher quality],"
" bool verbose=false [enable tetgen debug messages] "
") [Fills-in a closed mesh with tets, using tetgen]",
this,&GeoBoxApplication::tet_meshing
);
}
ImGui::EndMenu();
}
if(ImGui::BeginMenu("Mesh")) {
ImGui::MenuItem(" Stats", nullptr, false, false);
if(ImGui::MenuItem("show mesh stats")) {
show_statistics();
}
if(ImGui::MenuItem("show mesh topo")) {
show_topology();
}
ImGui::Separator();
ImGui::MenuItem(" Edit", nullptr, false, false);
if(ImGui::MenuItem("clear")) {
Command::set_current(
"void clear(bool yes_I_am_sure=false) "
"[removes all elements from the mesh]",
this, &GeoBoxApplication::clear
);
}
if(ImGui::MenuItem("remove elements")) {
Command::set_current(
"void remove_elements( "
" bool vertices=false [removes everyting], "
" bool edges=false [removes mesh edges], "
" bool facets=false [removes the surfacic part], "
" bool cells=false [removes the volumetric part],"
" bool kill_isolated_vx=false [kill isolated vertices]"
") [removes mesh elements]",
this, &GeoBoxApplication::remove_elements
);
}
if(ImGui::MenuItem("remove isolated vrtx")) {
Command::set_current(
"void remove_isolated_vertices(bool yes_I_am_sure=false) "
"[removes vertices that are not connected to any element]",
this, &GeoBoxApplication::remove_isolated_vertices
);
}
ImGui::Separator();
ImGui::MenuItem(" Selection", nullptr, false, false);
if(ImGui::MenuItem("select all vrtx")) {
select_all_vertices();
}
if(ImGui::MenuItem("unselect all vrtx")) {
unselect_all_vertices();
}
if(ImGui::MenuItem("invert vrtx sel")) {
invert_vertices_selection();
}
if(ImGui::MenuItem(
"sel vrtx on surf brdr")
) {
select_vertices_on_surface_border();
}
if(ImGui::MenuItem("unsel vrtx on surf brdr")) {
unselect_vertices_on_surface_border();
}
if(ImGui::MenuItem("delete sel vrtx")) {
delete_selected_vertices();
}
ImGui::EndMenu();
}
if(ImGui::BeginMenu("Attributes")) {
if(ImGui::MenuItem("local feature size")) {
Command::set_current(
"compute_local_feature_size( "
" std::string attribute_name=\"LFS\""
")",
this, &GeoBoxApplication::compute_local_feature_size
);
}
if(ImGui::MenuItem("dist. to border")) {
Command::set_current(
"compute_distance_to_border( "
" std::string attribute_name=\"distance\""
")",
this, &GeoBoxApplication::compute_distance_to_border
);
}
if(ImGui::MenuItem("ambient occlusion")) {
Command::set_current(
"compute_ambient_occlusion( "
" std::string attribute_name=\"AO\","
" index_t nb_rays_per_vertex=400,"
" index_t nb_smoothing_iter=2"
") [per-vertex ambient occlusion]",
this, &GeoBoxApplication::compute_ambient_occlusion
);
}
if(ImGui::MenuItem("manifold harmonics")) {
Command::set_current(
"compute_manifold_harmonics( "
" std::string attribute_name=\"MH\","
" index_t nb_harmonics=30"
")",
this, &GeoBoxApplication::compute_manifold_harmonics
);
}
ImGui::EndMenu();
}
}
void clear(bool are_you_sure = false) {
if(are_you_sure) {
mesh()->clear();
mesh()->vertices.set_single_precision();
mesh_gfx()->set_mesh(mesh());
}
}
bool load(const std::string& filename) override {
if(locked_) {
return false;
}
texture_filename_ = "";
bool result = SimpleMeshApplication::load(filename);
if(result && FileSystem::extension(filename) == "stl") {
locked_ = true;
begin();
mesh_repair(mesh_);
end();
locked_ = false;
}
std::string tex_file_name =
FileSystem::dir_name(filename) + "/" +
FileSystem::base_name(filename) + "_texture.png";
if(!FileSystem::is_file(tex_file_name)) {
tex_file_name =
FileSystem::dir_name(filename) + "/" +
FileSystem::base_name(filename) + "_normals.png";
}
if(FileSystem::is_file(tex_file_name)) {
texture_filename_ = tex_file_name;
}
return result;
}
void remove_elements(
bool vertices=false,
bool edges=false,
bool facets=false,
bool cells=false,
bool kill_isolated_vx=false
) {
if(vertices) {
mesh()->clear();
} else {
if(facets) {
mesh()->facets.clear();
}
if(edges) {
mesh()->edges.clear();
}
if(cells) {
mesh()->cells.clear();
}
if(kill_isolated_vx) {
mesh()->vertices.remove_isolated();
}
}
if(mesh()->facets.nb() == 0 && mesh()->cells.nb() == 0) {
show_vertices();
}
mesh_gfx()->set_mesh(mesh());
}
void remove_isolated_vertices(bool yes_I_am_sure = false) {
if(yes_I_am_sure) {
mesh()->vertices.remove_isolated();
mesh_gfx()->set_mesh(mesh());
}
}
void show_statistics() {
show_console();
mesh()->show_stats("Mesh");
}
void show_topology() {
show_console();
Logger::out("MeshTopology/surface")
<< "Nb components = "
<< mesh_nb_connected_components(*mesh())
<< std::endl;
Logger::out("MeshTopology/surface")
<< "Nb borders = "
<< mesh_nb_borders(*mesh())
<< std::endl;
Logger::out("MeshTopology/surface")
<< "Xi = "
<< mesh_Xi(*mesh())
<< std::endl;
}
void smooth_point_set(
index_t nb_iterations=2, index_t nb_neighbors=30
) {
begin();
if(nb_iterations != 0) {
Co3Ne_smooth(*mesh(), nb_neighbors, nb_iterations);
}
end();
}
/**
* \brief Removes the outliers from a pointset.
* \details Outliers are detected as points that have their
* N-th nearest neighbor further away than a given distance
* threshold.
* \param[in] N the number of nearest neighbors
* \param[in] R if N-th neighbor is further away than R
* then point will be discarded
*/
void filter_outliers(index_t N=70, double R=0.01) {
begin();
// Remove duplicated vertices
mesh_repair(mesh_, GEO::MESH_REPAIR_COLOCATE, 0.0);
// Compute nearest neighbors using a KdTree.
NearestNeighborSearch_var NN = new BalancedKdTree(3); // 3 is for 3D
NN->set_points(mesh_.vertices.nb(), mesh_.vertices.point_ptr(0));
// point_ptr(0) is a pointer to the first point
// (it is also a pointer to the whole points array since
// points are contiguous in a Mesh).
// Now let's remove the points that have their furthest
// nearest neighbor further away than R.
// A vector of integers. remove_point[v]=1 if v should be removed.
vector<index_t> remove_point(mesh_.vertices.nb(), 0);
double R2 = R*R; // squared threshold
// (KD-tree returns squared distances)
// Process all the points in parallel.
// The point sequence [0..mesh_.vertices.nb()-1] is split
// into intervals [from,to[ processed by the lambda
// function below. There is one interval per processor core,
// all processed in parallel.
parallel_for_slice(
0,mesh_.vertices.nb(),
[this,N,&NN,R2,&remove_point](index_t from, index_t to) {
vector<index_t> neigh(N);
vector<double> neigh_sq_dist(N);
for(index_t v=from; v<to; ++v) {
NN->get_nearest_neighbors(
N, mesh_.vertices.point_ptr(v),
neigh.data(), neigh_sq_dist.data()
);
remove_point[v] = (neigh_sq_dist[N-1] > R2);
}
}
);
// Now remove the points that should be removed.
mesh_.vertices.delete_elements(remove_point);
end();
}
/**
* \brief Keeps the largest connected components of a mesh,
* and deletes all the other ones.
* \param[in] are_you_sure confirmation
*/
void keep_largest_component(bool are_you_sure=true) {
if(!are_you_sure) {
return;
}
begin();
// component[f] will correspond to the component id of facet f
vector<index_t> component(mesh_.facets.nb(),index_t(-1));
index_t nb_comp=0;
// Iterates on all the facets of M
// (equivalent to for(index_t f = 0; f < M.facets.nb(); ++f))
for(index_t f: mesh_.facets) {
if(component[f] == index_t(-1)) {
// recursive traversal of the connected component
// incident to facet f (if it was not already traversed)
component[f] = nb_comp;
std::stack<index_t> S;
S.push(f);
while(!S.empty()) {
index_t top_f = S.top();
S.pop();
// Push the neighbors of facet top_f onto the stack if
// they were not already visited
for(
index_t le=0;
le<mesh_.facets.nb_vertices(top_f); ++le
) {
index_t adj_f = mesh_.facets.adjacent(top_f,le);
if(adj_f != index_t(-1) &&
component[adj_f] == index_t(-1)) {
component[adj_f] = nb_comp;
S.push(adj_f);
}
}
}
++nb_comp;
}
}
// Now compute the number of facets in each connected component
vector<index_t> comp_size(nb_comp,0);
for(index_t f: mesh_.facets) {
++comp_size[component[f]];
}
// Determine the id of the largest component
index_t largest_comp = 0;
index_t largest_comp_size = 0;
for(index_t comp=0; comp<nb_comp; ++comp) {
if(comp_size[comp] >= largest_comp_size) {
largest_comp_size = comp_size[comp];
largest_comp = comp;
}
}
// Remove all the facets that are not in the largest component
// component[] is now used as follows:
// component[f] = 0 if f should be kept
// component[f] = 1 if f should be deleted
// See GEO::MeshElements::delete_elements() documentation.
for(index_t f: mesh_.facets) {
component[f] = (component[f] != largest_comp) ? 1 : 0;
}
mesh_.facets.delete_elements(component);
end();
}
/**
* \brief Reconstructs the triangles from a poinset in the current
* mesh, using the Concurrent Co-Cones algorithm.
* \param[in] radius search radius for neighborhoods
* \param[in] nb_iterations number of smoothing iterations
* \param[in] nb_neighbors number of neighbors in graph
*/
void reconstruct_Co3Ne(
double radius=5.0,
index_t nb_iterations=0, index_t nb_neighbors=30
) {
hide_surface();
begin();
double R = bbox_diagonal(*mesh());
mesh_repair(*mesh(), MESH_REPAIR_COLOCATE, 1e-6*R);
radius *= 0.01 * R;
if(nb_iterations != 0) {
Co3Ne_smooth(*mesh(), nb_neighbors, nb_iterations);
}
Co3Ne_reconstruct(*mesh(), radius);
end();
hide_vertices();
show_surface();
}
/**
* \brief Reconstructs the triangles from a poinset in the current
* mesh, using the Poisson Reconstruction method.
* \param[in] depth octree depth
* \details Reference: http://hhoppe.com/poissonrecon.pdf
* Based on Misha Kahzdan's PoissonRecon code
* (see geogram/thirdparty/PoissonRecon) with custom adaptations.
*/
void reconstruct_Poisson(index_t depth=8) {
hide_surface();
begin();
mesh_.facets.clear();
// Poisson reconstruction needs oriented normals. Use
// Concurrent Co-Cones (Co3Ne) to compute them.
Mesh points;
points.copy(mesh_,false,MESH_VERTICES);
Co3Ne_compute_normals(points, 30, true);
mesh_.clear();
PoissonReconstruction poisson;
poisson.set_depth(depth);
poisson.reconstruct(&points, &mesh_);