-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrepair_mesh.cc
108 lines (88 loc) · 3.5 KB
/
repair_mesh.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
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Polygon_mesh_processing/repair_polygon_soup.h>
#include <CGAL/Polygon_mesh_processing/orient_polygon_soup.h>
#include <CGAL/Polygon_mesh_processing/polygon_soup_to_polygon_mesh.h>
#include <CGAL/IO/PLY_reader.h>
#include <CGAL/IO/PLY_writer.h>
#include <algorithm>
#include <array>
#include <iostream>
#include <vector>
#include <fstream>
#include <gflags/gflags.h>
// A little example to repair a polygon mesh
DEFINE_string(input_mesh, "",
"The input mesh file.");
DEFINE_string(output_mesh, "",
"The output mesh file.");
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Simple_cartesian<double> Kernel;
typedef K::FT FT;
typedef K::Point_3 Point_3;
typedef CGAL::Surface_mesh<Point_3> Mesh;
typedef std::array<FT, 3> Custom_point;
typedef std::vector<std::size_t> CGAL_Polygon;
namespace PMP = CGAL::Polygon_mesh_processing;
struct Array_traits {
struct Equal_3
{
bool operator()(const Custom_point& p, const Custom_point& q) const {
return (p == q);
}
};
struct Less_xyz_3
{
bool operator()(const Custom_point& p, const Custom_point& q) const {
return std::lexicographical_compare(p.begin(), p.end(), q.begin(), q.end());
}
};
Equal_3 equal_3_object() const { return Equal_3(); }
Less_xyz_3 less_xyz_3_object() const { return Less_xyz_3(); }
};
int main(int argc, char* argv[]) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
// Sanity checks
if (FLAGS_input_mesh == "") {
std::cout << "The input mesh file is not specified.\n";
return 1;
}
if (FLAGS_output_mesh == "") {
std::cout << "The output mesh file is not specified.\n";
return 1;
}
std::cout << "Reading: " << FLAGS_input_mesh << std::endl;
std::ifstream in(FLAGS_input_mesh);
std::vector<Kernel::Point_3> points;
std::vector< std::vector<std::size_t> > polygons;
std::vector<CGAL::Color> fcolors;
std::vector<CGAL::Color> vcolors;
if (!in || !CGAL::read_PLY(in, points, polygons, fcolors, vcolors) || points.empty()) {
std::cerr << "Cannot open file: " << FLAGS_input_mesh << std::endl;
return EXIT_FAILURE;
}
std::vector<CGAL_Polygon> polygons2;
for (size_t it = 0; it < polygons.size(); it++) {
CGAL_Polygon p;
for (size_t it2 = 0; it2 < polygons[it].size(); it2++) {
p.push_back(polygons[it][it2]);
}
polygons2.push_back(p);
}
std::vector<std::array<FT, 3> > points2;
for (size_t it = 0; it < points.size(); it++) {
Kernel::Point_3 point = points[it];
points2.push_back(CGAL::make_array<FT>(point[0], point[1], point[2]));
}
PMP::repair_polygon_soup(points2, polygons2, CGAL::parameters::geom_traits(Array_traits()));
std::cout << "After reparation, the soup has " << points2.size() << " vertices and " << polygons2.size() << " faces" << std::endl;
Mesh mesh;
PMP::orient_polygon_soup(points2, polygons2);
PMP::polygon_soup_to_polygon_mesh(points2, polygons2, mesh);
std::cout << "Mesh has " << num_vertices(mesh) << " vertices and " << num_faces(mesh) << " faces" << std::endl;
std::cout << "Writing: " << FLAGS_output_mesh << std::endl;
std::ofstream out(FLAGS_output_mesh);
out.precision(17);
CGAL::write_PLY(out, mesh);
return 0;
}