Skip to content

Instantly share code, notes, and snippets.

@ochafik
Created February 19, 2021 19:52
Show Gist options
  • Save ochafik/e55c52c50804bcb6f073968a57099e63 to your computer and use it in GitHub Desktop.
Save ochafik/e55c52c50804bcb6f073968a57099e63 to your computer and use it in GitHub Desktop.
CGAL::Polyhedron_3 repair code with CGAL::Polygon_mesh_processing::repair_polygon_soup
// Copyright 2021 Google LLC.
// SPDX-License-Identifier: Apache-2.0
//
#include <CGAL/Polygon_mesh_processing/repair_polygon_soup.h>
#include <CGAL/Polygon_mesh_processing/polygon_soup_to_polygon_mesh.h>
template <typename K>
std::shared_ptr<CGAL::Polyhedron_3<K>> repairPolyhedron(const CGAL::Polyhedron_3<K> &poly)
{
std::vector<typename CGAL::Point_3<K>> points;
std::vector<std::vector<std::size_t>> polygons;
for (auto fi = poly.facets_begin(); fi != poly.facets_end(); ++fi) {
auto hc = fi->facet_begin();
auto hc_end = hc;
std::vector<std::size_t> polygon;
do {
const auto &v = *((hc++)->vertex());
auto vertexIndex = points.size();
points.push_back(v.point());
polygon.push_back(vertexIndex);
} while (hc != hc_end);
polygons.emplace_back(polygon);
}
CGAL::Polygon_mesh_processing::repair_polygon_soup(
points, polygons); //, CGAL::parameters::geom_traits(Array_traits()));
auto out = std::make_shared<CGAL::Polyhedron_3<K>>();
CGAL::Polygon_mesh_processing::polygon_soup_to_polygon_mesh(points, polygons, *out);
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment