#include <fstream>
#include <iostream>
#include <CGAL/Timer.h>
#include <CGAL/number_utils.h>
#include <CGAL/property_map.h>
#include <CGAL/IO/read_points.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
typedef Kernel::FT FT;
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;
((argc > 1) ? argv[1] : CGAL::data_file_path("points_3/cube.pwn")),
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;
}
A convenience header that includes all classes related to the efficient RANSAC algorithm.
Shape detection algorithm based on the RANSAC method.
Definition: Efficient_RANSAC.h:60
Plane implements Shape_base.
Definition: Plane.h:33
bool read_points(const std::string &fname, PointOutputIterator output, const NamedParameters &np=parameters::default_values())
Default traits class for the CGAL::Shape_detection::Efficient_RANSAC.
Definition: Efficient_RANSAC_traits.h:41