前言
本节主要实现CGAL中RANSAC方法的平面分割功能 |
一、效果展示
二、代码
拷贝直接运行 |
#include <fstream>
#include <iostream>
#include <CGAL/property_map.h>
#include <CGAL/IO/read_points.h>
#include <CGAL/Point_set_3.h>
#include <CGAL/Point_with_normal_3.h>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Shape_detection/Efficient_RANSAC.h>
// Type declarations.
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::Point_3 Point;
typedef std::pair<Kernel::Point_3, Kernel::Vector_3> Point_with_normal;
typedef std::vector<Point_with_normal> Pwn_vector;
typedef CGAL::First_of_pair_property_map<Point_with_normal> Point_map;
typedef CGAL::Second_of_pair_property_map<Point_with_normal> Normal_map;
typedef CGAL::Shape_detection::Efficient_RANSAC_traits
<Kernel, Pwn_vector, Point_map, Normal_map> Traits;
typedef CGAL::Shape_detection::Efficient_RANSAC<Traits> Efficient_ransac;
typedef CGAL::Shape_detection::Plane<Traits> Plane;
typedef CGAL::Point_set_3<Point> Point_set;
int main(int argc, char** argv) {
std::cout << "Efficient RANSAC" << std::endl;
const std::string filename = (argc > 1) ? argv[1] : CGAL::data_file_path("data/cube.xyz");
Pwn_vector points;
if (!CGAL::IO::read_points(filename,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>();
ransac.detect();
std::cout << ransac.shapes().end() - ransac.shapes().begin() << " shapes detected." << std::endl;
Efficient_ransac::Plane_range planes = ransac.planes();
Efficient_ransac::Plane_range::iterator it = planes.begin();
int count = 0;
while (it != planes.end()) {
int num = (*it)->indices_of_assigned_points().size();
Point_set cloud;
for (int i = 0; i < num; i++)
{
Point temp=points[(*it)->indices_of_assigned_points()[i]].first;
cloud.insert(temp);
}
CGAL::IO::write_OFF(std::to_string(count)+".off",cloud);
cloud.clear();
it++;
count++;
}
return EXIT_SUCCESS;
}
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容