#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/IO/read_xyz_points.h>
#include <CGAL/Point_with_normal_3.h>
#include <CGAL/property_map.h>
#include <CGAL/Timer.h>
#include <CGAL/number_utils.h>
#include <CGAL/Shape_detection_3.h>
#include <iostream>
#include <fstream>
typedef std::pair<Kernel::Point_3, Kernel::Vector_3> Point_with_normal;
typedef std::vector<Point_with_normal> Pwn_vector;
Pwn_vector, Point_map, Normal_map> Traits;
int main()
{
Pwn_vector points;
std::ifstream stream("data/cube.pwn");
if (!stream ||
std::back_inserter(points),
Point_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 (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;
}