mirror of https://github.com/CGAL/cgal
improve example
This commit is contained in:
parent
c253a64361
commit
086476bc30
|
|
@ -8,8 +8,7 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <algorithm>
|
#include <cstdlib>
|
||||||
#include <random>
|
|
||||||
|
|
||||||
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
|
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
|
||||||
typedef Kernel::Point_3 Point;
|
typedef Kernel::Point_3 Point;
|
||||||
|
|
@ -19,14 +18,11 @@ typedef CGAL::Surface_mesh<Point> Mesh;
|
||||||
namespace PMP = CGAL::Polygon_mesh_processing;
|
namespace PMP = CGAL::Polygon_mesh_processing;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
// create a mesh with many connected connected components that should
|
// create a mesh with many connected connected components that should
|
||||||
// be reoriented to define a valid closed mesh
|
// be reoriented to define a valid closed mesh
|
||||||
|
void create_mesh_with_cc_to_orient(Mesh& mesh)
|
||||||
|
{
|
||||||
const std::string filename = CGAL::data_file_path("meshes/elephant.off");
|
const std::string filename = CGAL::data_file_path("meshes/elephant.off");
|
||||||
|
|
||||||
Mesh mesh;
|
|
||||||
CGAL::IO::read_polygon_mesh(filename, mesh);
|
CGAL::IO::read_polygon_mesh(filename, mesh);
|
||||||
|
|
||||||
// turn the mesh into a triangle soup, duplicating all the vertices and shuffling orientations
|
// turn the mesh into a triangle soup, duplicating all the vertices and shuffling orientations
|
||||||
|
|
@ -34,8 +30,6 @@ int main()
|
||||||
std::vector< std::array<std::size_t, 3> > triangles;
|
std::vector< std::array<std::size_t, 3> > triangles;
|
||||||
triangles.reserve(faces(mesh).size());
|
triangles.reserve(faces(mesh).size());
|
||||||
points.reserve(3*triangles.size());
|
points.reserve(3*triangles.size());
|
||||||
std::random_device rd;
|
|
||||||
std::mt19937 g(rd());
|
|
||||||
for (Mesh::Face_index f : mesh.faces())
|
for (Mesh::Face_index f : mesh.faces())
|
||||||
{
|
{
|
||||||
Mesh::Halfedge_index h = mesh.halfedge(f);
|
Mesh::Halfedge_index h = mesh.halfedge(f);
|
||||||
|
|
@ -44,31 +38,38 @@ int main()
|
||||||
points.push_back(mesh.point(target(h,mesh)));
|
points.push_back(mesh.point(target(h,mesh)));
|
||||||
points.push_back(mesh.point(target(mesh.next(h),mesh)));
|
points.push_back(mesh.point(target(mesh.next(h),mesh)));
|
||||||
triangles.push_back( {s, s+1, s+2} );
|
triangles.push_back( {s, s+1, s+2} );
|
||||||
std::shuffle(triangles.back().begin(), triangles.back().end(), g);
|
if (std::rand() % 2 == 0)
|
||||||
|
std::swap(triangles.back()[0], triangles.back()[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// load the soup into the mesh;
|
// load the soup into the mesh;
|
||||||
mesh.clear();
|
mesh.clear();
|
||||||
PMP::polygon_soup_to_polygon_mesh(points, triangles, mesh);
|
PMP::polygon_soup_to_polygon_mesh(points, triangles, mesh);
|
||||||
CGAL::IO::write_polygon_mesh("soup.off", mesh, CGAL::parameters::stream_precision(17));
|
}
|
||||||
|
|
||||||
// determine face orientations to be reversed to create compatibility
|
int main()
|
||||||
|
{
|
||||||
|
Mesh mesh;
|
||||||
|
create_mesh_with_cc_to_orient(mesh);
|
||||||
|
CGAL::IO::write_polygon_mesh("to_orient.off", mesh, CGAL::parameters::stream_precision(17));
|
||||||
|
|
||||||
|
// determine face orientations to be reversed to create compatibility
|
||||||
auto fbm = mesh.add_property_map<Mesh::Face_index, bool>("fbm", false).first;
|
auto fbm = mesh.add_property_map<Mesh::Face_index, bool>("fbm", false).first;
|
||||||
bool is_orientable = PMP::connected_components_compatible_orientations(mesh, fbm);
|
bool is_orientable = PMP::connected_components_compatible_orientations(mesh, fbm);
|
||||||
assert(is_orientable);
|
assert(is_orientable);
|
||||||
|
|
||||||
// reverse orientation of faces with bit 1
|
// reverse orientation of faces with bit 1
|
||||||
std::vector<Mesh::Face_index> faces_to_reverse;
|
std::vector<Mesh::Face_index> faces_to_reverse;
|
||||||
for (Mesh::Face_index f : mesh.faces())
|
for (Mesh::Face_index f : mesh.faces())
|
||||||
if (get(fbm, f))
|
if (get(fbm, f))
|
||||||
faces_to_reverse.push_back(f);
|
faces_to_reverse.push_back(f);
|
||||||
PMP::reverse_face_orientations(faces_to_reverse, mesh);
|
PMP::reverse_face_orientations(faces_to_reverse, mesh);
|
||||||
|
|
||||||
// there are still borders between previously incompatible faces: stitch to close the mesh
|
// there are still borders between previously incompatible faces: stitch to close the mesh
|
||||||
PMP::stitch_borders(mesh);
|
PMP::stitch_borders(mesh);
|
||||||
|
|
||||||
assert(CGAL::is_closed(mesh));
|
assert(CGAL::is_closed(mesh));
|
||||||
CGAL::IO::write_polygon_mesh("reoriented_and_stitched.off", mesh, CGAL::parameters::stream_precision(17));
|
CGAL::IO::write_polygon_mesh("oriented_and_stitched.off", mesh, CGAL::parameters::stream_precision(17));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue