Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#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;
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] );
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];
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)))
std::ofstream out("");
out << std::setprecision(17) << mesh;
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.