CGAL学习记录——ransac多平面分割

CGAL-RANSAC多平面分割

前言

本节主要实现CGAL中RANSAC方法的平面分割功能
已添加到QT+PCL中:QT+PCL+CGAL实现多平面分割

一、效果展示

在这里插入图片描述

二、代码

拷贝直接运行

#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;
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小修勾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值