Skip to content

Instantly share code, notes, and snippets.

@afabri
Last active March 17, 2016 16:46
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 afabri/1bbf46c6fd4f742bd982 to your computer and use it in GitHub Desktop.
Save afabri/1bbf46c6fd4f742bd982 to your computer and use it in GitHub Desktop.
Seeding the mesh generator
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Mesh_triangulation_3.h>
#include <CGAL/Mesh_complex_3_in_triangulation_3.h>
#include <CGAL/Mesh_criteria_3.h>
#include <CGAL/Polyhedral_mesh_domain_3.h>
#include <CGAL/make_mesh_3.h>
#include <CGAL/refine_mesh_3.h>
#include <CGAL/grid_simplify_point_set.h>
// IO
#include <CGAL/IO/Polyhedron_iostream.h>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_3 Point_3;
typedef CGAL::Polyhedron_3<K> Polyhedron;
typedef CGAL::Polyhedral_mesh_domain_3<Polyhedron, K> CGAL_Mesh_domain;
// My own domain class that basically overwrites the initialization
struct Mesh_domain : public CGAL_Mesh_domain {
std::vector<Point_3> seeds;
Mesh_domain(const Polyhedron& polyhedron, double cell_size)
: CGAL_Mesh_domain(polyhedron),
seeds(polyhedron.points_begin(), polyhedron.points_end())
{
// Select a subset of the points on the surface
std::cerr << seeds.size() << " vertices" << std::endl;
seeds.erase(CGAL::grid_simplify_point_set(seeds.begin(), seeds.end(), cell_size),
seeds.end());
std::cerr << seeds.size() << " seed points" << std::endl;
}
struct Construct_initial_points
{
Construct_initial_points(const std::vector<Point_3>& seeds)
: seeds(seeds)
{}
template<class OutputIterator>
OutputIterator operator()(OutputIterator pts, const int n = 8) const
{
BOOST_FOREACH(Point_3 p, seeds){
*pts++ = std::make_pair(p,0);
}
return pts;
}
private:
const std::vector<Point_3>& seeds;
};
Construct_initial_points construct_initial_points_object() const
{
return Construct_initial_points(seeds);
}
};
// Triangulation
#ifdef CGAL_CONCURRENT_MESH_3
typedef CGAL::Mesh_triangulation_3<
Mesh_domain,
CGAL::Kernel_traits<Mesh_domain>::Kernel, // Same as sequential
CGAL::Parallel_tag // Tag to activate parallelism
>::type Tr;
#else
typedef CGAL::Mesh_triangulation_3<Mesh_domain>::type Tr;
#endif
typedef CGAL::Mesh_complex_3_in_triangulation_3<Tr> C3t3;
// Criteria
typedef CGAL::Mesh_criteria_3<Tr> Mesh_criteria;
// To avoid verbose function and named parameters call
using namespace CGAL::parameters;
int main(int argc, char*argv[])
{
const char* fname = (argc>1)?argv[1]:"data/elephant.off";
double cell_size = (argc>2)?boost::lexical_cast<double>(argv[2]) : 1.0;
// Create input polyhedron
Polyhedron polyhedron;
std::ifstream input(fname);
input >> polyhedron;
if(input.bad()){
std::cerr << "Error: Cannot read file " << fname << std::endl;
return EXIT_FAILURE;
}
input.close();
// Create domain
Mesh_domain domain(polyhedron, cell_size);
// Mesh criteria (no cell_size set)
Mesh_criteria criteria(facet_angle=25, facet_size=0.15, facet_distance=0.008,
cell_radius_edge_ratio=3);
// Mesh generation
C3t3 c3t3 = CGAL::make_mesh_3<C3t3>(domain, criteria, no_perturb(), no_exude());
// Output
std::ofstream medit_file("out_1.mesh");
c3t3.output_to_medit(medit_file);
medit_file.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment