Tiny example improvements

This commit is contained in:
Mael Rouxel-Labbé 2021-06-29 15:34:39 +02:00
parent 0e6cdc24f8
commit 21bf16d4eb
1 changed files with 31 additions and 38 deletions

View File

@ -7,6 +7,9 @@
#include <CGAL/Surface_mesh_simplification/Policies/Edge_collapse/GarlandHeckbert_probabilistic_policies.h>
#include <CGAL/Surface_mesh_simplification/Policies/Edge_collapse/GarlandHeckbert_policies.h>
#include <CGAL/boost/graph/IO/polygon_mesh_io.h>
#include <CGAL/Polygon_mesh_processing/bbox.h>
#include <chrono>
#include <iostream>
#include <fstream>
@ -20,31 +23,27 @@ typedef Kernel::Point_3 Point_3;
typedef CGAL::Surface_mesh<Point_3> Surface_mesh;
typedef CGAL::Bbox_3 Bbox_3;
namespace PMP = CGAL::Polygon_mesh_processing;
namespace SMS = CGAL::Surface_mesh_simplification;
template<typename Cost, typename Placement>
Surface_mesh collapse(Surface_mesh surface_mesh, std::string outname,
FT ratio, Cost cost, Placement placement)
void collapse(Surface_mesh surface_mesh, std::string outname,
FT ratio, Cost cost, Placement placement)
{
if(!CGAL::is_triangle_mesh(surface_mesh))
{
std::cerr << "Input geometry is not triangulated." << std::endl;
return { } ;
}
std::cout << "Input mesh has " << num_vertices(surface_mesh) << " nv "
<< num_edges(surface_mesh) << " ne "
<< num_faces(surface_mesh) << " nf" << std::endl;
std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();
std::cout << "Aiming for " << 100 * ratio << "% of the input edges..." << std::endl;
SMS::Count_ratio_stop_predicate<Surface_mesh> stop(ratio);
int r = SMS::edge_collapse(surface_mesh, stop,
CGAL::parameters::get_cost(cost).get_placement(placement));
CGAL::parameters::get_cost(cost)
.get_placement(placement));
std::chrono::steady_clock::time_point end_time = std::chrono::steady_clock::now();
@ -52,27 +51,20 @@ Surface_mesh collapse(Surface_mesh surface_mesh, std::string outname,
<< std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count()
<< "ms" << std::endl;
std::cout << "\nFinished!\n" << r
<< " edges removed.\n" << surface_mesh.number_of_edges()
std::cout << "\nFinished!\n" << r
<< " edges removed.\n" << surface_mesh.number_of_edges()
<< " final edges.\n";
CGAL::IO::write_polygon_mesh(outname, surface_mesh, CGAL::parameters::stream_precision(17));
return surface_mesh;
}
void normalize(Surface_mesh& mesh)
void normalize(Surface_mesh& mesh)
{
Bbox_3 bbox { };
for (auto v : mesh.vertices())
{
bbox += mesh.point(v).bbox();
}
const Bbox_3 bbox = PMP::bbox(mesh);
FT max = std::max(std::max(bbox.xmax(), bbox.ymax()), bbox.zmax());
for (auto v : mesh.vertices())
for (auto v : vertices(mesh))
{
Point_3& p = mesh.point(v);
p = {p.x() / max, p.y() / max, p.z() / max};
@ -87,47 +79,48 @@ void normalize(Surface_mesh& mesh)
int main(int argc, char** argv)
{
Surface_mesh surface_mesh;
// command line arguments
const char* filename = (argc > 1) ? argv[1] : "data/cube-meshed.off";
const double ratio = (argc > 2) ? std::stod(argv[2]) : 0.2;
const std::string outname = (argc > 3) ? argv[3] : "test";
FT variance = (argc > 4) ? std::stod(argv[4]) : 0.05;
std::ifstream is(filename);
if(!is || !(is >> surface_mesh))
if(!CGAL::IO::read_polygon_mesh(filename, surface_mesh) || !CGAL::is_triangle_mesh(surface_mesh))
{
std::cerr << "Failed to read input mesh: " << filename << std::endl;
std::cerr << "Failed to read valid input mesh: " << filename << std::endl;
return EXIT_FAILURE;
}
// Garland&Heckbert simplification policies
typedef typename SMS::GarlandHeckbert_policies<Surface_mesh, Kernel> Classic_policies;
typedef typename SMS::GarlandHeckbert_probabilistic_policies<Surface_mesh, Kernel> Prob_policies;
typedef typename Classic_policies::Get_cost Classic_cost;
typedef typename Classic_policies::Get_placement Classic_placement_unbounded;
typedef typename CGAL::Surface_mesh_simplification::Bounded_normal_change_placement<
Classic_placement_unbounded> Classic_placement;
typedef typename Prob_policies::Get_cost Prob_cost;
typedef typename Prob_policies::Get_placement Prob_placement;
Classic_policies classic_policies(surface_mesh, 100);
const Classic_cost& classic_cost = classic_policies.get_cost();
const Classic_placement& classic_placement { classic_policies.get_placement() };
normalize(surface_mesh);
// Classic
collapse(surface_mesh, outname + "_classic.off", ratio, classic_cost, classic_placement);
// Probabilistic
for (int i = 0; i < 5; ++i)
{
Prob_policies prob_policies(surface_mesh, 100, variance, variance);
const Prob_cost& prob_cost = prob_policies.get_cost();
const Prob_placement& prob_placement = prob_policies.get_placement();
std::string filename = outname + "_prob_" + std::to_string(i) + ".off";
std::cout << "Using variance of " << variance << " for both parameters\n";
@ -135,6 +128,6 @@ int main(int argc, char** argv)
variance = variance / 2;
}
return EXIT_SUCCESS;
}