Example implicit sphere

This commit is contained in:
Pierre Alliez 2023-12-23 18:59:54 +01:00
parent 6cacbee79f
commit 77649cdfc8
1 changed files with 17 additions and 20 deletions

View File

@ -1,46 +1,43 @@
#include <CGAL/Simple_cartesian.h>
#include <CGAL/Isosurfacing_3/Implicit_Cartesian_grid_domain_3.h>
#include <CGAL/Isosurfacing_3/marching_cubes_3.h>
#include <CGAL/Bbox_3.h>
#include <CGAL/boost/graph/IO/OFF.h>
#include <vector>
using Kernel = CGAL::Simple_cartesian<double>;
using FT = typename Kernel::FT;
using Vector = typename Kernel::Vector_3;
using Point = typename Kernel::Point_3;
using Vector = typename Kernel::Vector_3;
using Point_range = std::vector<Point>;
using Polygon_range = std::vector<std::vector<std::size_t> >;
using Triangle_range = std::vector<std::vector<std::size_t> >;
// Sphere = Euclidean distance function to the origin
auto sphere_function = [&](const Point& p) -> FT
{
return std::sqrt(p.x() * p.x() + p.y() * p.y() + p.z() * p.z());
};
int main(int, char**)
{
const CGAL::Bbox_3 bbox { -1.0, -1.0, -1.0, 1.0, 1.0, 1.0 };
// box domain and spacing vector
const CGAL::Bbox_3 bbox{ -1.0, -1.0, -1.0, 1.0, 1.0, 1.0 };
const FT spacing = 0.04;
const Vector vec_spacing(spacing, spacing, spacing);
// Euclidean distance function to the origin
auto sphere_function = [&](const Point& p) -> FT
{
return sqrt(p.x() * p.x() + p.y() * p.y() + p.z() * p.z());
};
// create a domain with given bounding box and grid spacing
// create domain with sphere function
auto domain = CGAL::Isosurfacing::create_implicit_Cartesian_grid_domain<Kernel>(bbox, vec_spacing, sphere_function);
// prepare collections for the output indexed mesh
// points and triangles for the output indexed mesh
Point_range points;
Polygon_range polygons;
Triangle_range triangles;
// execute marching cubes with an isovalue of 0.8
CGAL::Isosurfacing::marching_cubes(domain, 0.8, points, polygons);
// execute marching cubes with a given isovalue
const FT isovalue = 0.8;
CGAL::Isosurfacing::marching_cubes(domain, isovalue, points, triangles);
// save ouput indexed mesh to a file, in the OFF format
CGAL::IO::write_OFF("result.off", points, polygons);
CGAL::IO::write_OFF("output.off", points, triangles);
return EXIT_SUCCESS;
}