#include <fstream>
#include <iostream>
#include <CGAL/Timer.h>
#include <CGAL/number_utils.h>
#include <CGAL/property_map.h>
#include <CGAL/IO/read_xyz_points.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Shape_detection/Efficient_RANSAC.h>
typedef std::pair<Kernel::Point_3, Kernel::Vector_3> Point_with_normal;
typedef std::vector<Point_with_normal> Pwn_vector;
<
Kernel, Pwn_vector, Point_map, Normal_map> Traits;
int main(int argc, char** argv) {
Pwn_vector points;
std::ifstream stream((argc > 1) ? argv[1] : "data/cube.pwn");
if (!stream ||
stream,
std::back_inserter(points),
CGAL::parameters::point_map(Point_map()).
normal_map(Normal_map()))) {
std::cerr << "Error: cannot read file cube.pwn!" << std::endl;
return EXIT_FAILURE;
}
Efficient_ransac ransac;
ransac.set_input(points);
ransac.add_shape_factory<Plane>();
CGAL::Timer time;
time.start();
ransac.preprocess();
time.stop();
std::cout << "preprocessing took: " << time.time() * 1000 << "ms" << std::endl;
Efficient_ransac::Shape_range shapes = ransac.shapes();
FT best_coverage = 0;
for (std::size_t i = 0; i < 3; ++i) {
time.reset();
time.start();
ransac.detect();
time.stop();
FT coverage =
FT(points.size() - ransac.number_of_unassigned_points()) / FT(points.size());
std::cout << "time: " << time.time() * 1000 << "ms" << std::endl;
std::cout << ransac.shapes().end() - ransac.shapes().begin()
<< " primitives, " << coverage << " coverage" << std::endl;
if (coverage > best_coverage) {
best_coverage = coverage;
shapes = ransac.shapes();
}
}
Efficient_ransac::Shape_range::iterator it = shapes.begin();
while (it != shapes.end()) {
boost::shared_ptr<Efficient_ransac::Shape> shape = *it;
std::cout << (*it)->info();
FT sum_distances = 0;
std::vector<std::size_t>::const_iterator
index_it = (*it)->indices_of_assigned_points().begin();
while (index_it != (*it)->indices_of_assigned_points().end()) {
const Point_with_normal& p = *(points.begin() + (*index_it));
sum_distances +=
CGAL::sqrt((*it)->squared_distance(p.first));
index_it++;
}
FT average_distance = sum_distances / shape->indices_of_assigned_points().size();
std::cout << " average distance: " << average_distance << std::endl;
it++;
}
return EXIT_SUCCESS;
}