Skip to content

Instantly share code, notes, and snippets.

@afabri
Created April 6, 2022 10:45
Show Gist options
  • Save afabri/c8e18a83fc280074ac78b20e76f7f236 to your computer and use it in GitHub Desktop.
Save afabri/c8e18a83fc280074ac78b20e76f7f236 to your computer and use it in GitHub Desktop.
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/boost/graph/IO/polygon_mesh_io.h>
#include <CGAL/AABB_face_graph_triangle_primitive.h>
#include <CGAL/AABB_tree.h>
#include <CGAL/AABB_traits.h>
#include <CGAL/Polygon_mesh_processing/locate.h>
#include <CGAL/Polygon_mesh_processing/compute_normal.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/clip.h>
#include <CGAL/Polygon_mesh_processing/connected_components.h>
#include <fstream>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_3 Point_3;
typedef K::Vector_3 Vector_3;
typedef CGAL::Surface_mesh<Point_3> Mesh;
typedef typename boost::graph_traits<Mesh>::vertex_descriptor vertex_descriptor;
typedef typename boost::graph_traits<Mesh>::face_descriptor face_descriptor;
typedef CGAL::AABB_face_graph_triangle_primitive<Mesh> AABB_face_graph_primitive;
typedef CGAL::AABB_traits<K, AABB_face_graph_primitive> AABB_face_graph_traits;
typedef CGAL::AABB_tree<AABB_face_graph_traits> Tree;
namespace PMP = CGAL::Polygon_mesh_processing;
typedef PMP::Face_location<Mesh, double> Face_location;
int main(int argc, char* argv[])
{
Mesh tm;
CGAL::IO::read_polygon_mesh(argv[1], tm);
Tree tree;
PMP::build_AABB_tree(tm, tree);
// Read the polyline and create a mesh for the extruded polyline
// The polyline file must start with the number of points
// and the first and last point must be the same
const double extrusion_distance = 0.1;
std::ifstream ifs(argv[2]);
int n;
ifs >> n;
Point_3 p;
std::vector<Point_3> points;
points.reserve(2* (n-1));
for(int i = 0; i < n-1; ++i){
ifs >> p;
Face_location location = PMP::locate_with_AABB_tree(p, tree, tm);
face_descriptor fd = location.first;
Vector_3 v = PMP::compute_face_normal(fd, tm);
v /= sqrt(v.squared_length());
v *= extrusion_distance;
Point_3 q = PMP::construct_point(location, tm);
points.push_back(q+v);
points.push_back(q-v);
}
std::vector<std::array<int,3>> faces;
for(int i = 0; i < (2*(n-1)-2); ++i){
std::array<int,3> a = {i, i+1, i+2};
faces.push_back(a);
}
// We could have used a modulo for closing
{
std::array<int,3> a = {2*(n-1)-2, 2*(n-1)-1, 0};
faces.push_back(a);
}
{
std::array<int,3> a = {2*(n-1)-1, 0, 1};
faces.push_back(a);
}
// The extruded polyline may have problems
bool orientable = PMP::orient_polygon_soup(points,faces);
if(! orientable){
std::cout << "Provide better curve" << std::endl;
return 1;
}
Mesh splitter;
PMP::polygon_soup_to_polygon_mesh(points, faces, splitter);
CGAL::IO::write_polygon_mesh("splitter.off", splitter);
PMP::split(tm, splitter);
std::vector<Mesh> cctm;
PMP::split_connected_components(tm, cctm);
if(cctm.size() != 2){
std::cout << "We expected 2 components and not " << cctm.size() << std::endl;
return 1;
}
CGAL::IO::write_polygon_mesh("out0.off", cctm[0]);
CGAL::IO::write_polygon_mesh("out1.off", cctm[1]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment