Skip to content

Instantly share code, notes, and snippets.

@sloriot
Created July 13, 2018 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sloriot/12f6ba2c5d593b1b0e2e82a07fc82df7 to your computer and use it in GitHub Desktop.
Save sloriot/12f6ba2c5d593b1b0e2e82a07fc82df7 to your computer and use it in GitHub Desktop.
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/IO/OFF_reader.h>
#include <CGAL/Polygon_mesh_processing/orient_polygon_soup.h>
#include <CGAL/Polygon_mesh_processing/polygon_soup_to_polygon_mesh.h>
#include <CGAL/Polygon_mesh_processing/orientation.h>
#include <vector>
#include <fstream>
#include <iostream>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Surface_mesh<K::Point_3> Mesh;
int main(int argc, char* argv[])
{;
std::ifstream input(argv[1]);
if (!input)
{
std::cerr << "Cannot open file " << std::endl;
return 1;
}
std::vector<K::Point_3> points;
std::vector< std::vector<std::size_t> > polygons;
if (!CGAL::read_OFF(input, points, polygons))
{
std::cerr << "Error parsing the OFF file " << std::endl;
return 1;
}
// merge duplicated points (useful to also stitch uncorrectly oriented patches)
const std::size_t nb_points = points.size();
std::vector<std::size_t> point_index(nb_points, 0);
std::map<K::Point_3, std::size_t> point_to_id;
std::vector<K::Point_3> unique_points;
unique_points.reserve(nb_points);
for (std::size_t i=0; i<nb_points; ++i)
{
std::size_t id =
point_to_id.insert( std::make_pair(points[i], unique_points.size()) ).first->second;
if ( id == unique_points.size() )
unique_points.push_back( points[i] );
point_index[i]=id;
}
if (unique_points.size() != nb_points)
{
typedef std::vector<std::size_t> Polygon;
BOOST_FOREACH(Polygon& p, polygons)
{
BOOST_FOREACH(std::size_t& id, p)
id = point_index[id];
}
points.swap(unique_points);
}
CGAL::Polygon_mesh_processing::orient_polygon_soup(points, polygons);
Mesh mesh;
CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh(points, polygons, mesh);
if (CGAL::is_closed(mesh) && (!CGAL::Polygon_mesh_processing::is_outward_oriented(mesh)))
CGAL::Polygon_mesh_processing::reverse_face_orientations(mesh);
std::ofstream out("oriented.off");
out << std::setprecision(17) << mesh;
out.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment