【CGAL】自动生成带颜色的点并保存为PLY文件

说明

该代码为CGAL官方库原代码,但是在库中的位置不太好找,这里做一下收录。
代码功能为自动生成带颜色的点,并保存为PLY文件
可以通过这篇代码了解CGAL中关于PLY文件读写相关的结构体定义与操作。

代码

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/property_map.h>
#include <CGAL/IO/write_ply_points.h>
#include <utility>
#include <vector>
#include <fstream>
// types
typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
typedef Kernel::FT FT;
typedef Kernel::Point_3 Point;
typedef Kernel::Vector_3 Vector;
typedef std::array<unsigned char, 4> Color;
// Point with normal, color
typedef std::tuple<Point, Color> PC;
typedef CGAL::Nth_of_tuple_property_map<0, PC> Point_map;
typedef CGAL::Nth_of_tuple_property_map<1, PC> Color_map;
// Define how a color should be stored
namespace CGAL {
    template< class F >
    struct Output_rep< ::Color, F > {
        const ::Color& c;
        static const bool is_specialized = true;
        Output_rep(const ::Color& c) : c(c)
        { }
        std::ostream& operator() (std::ostream& out) const
        {
            if (IO::is_ascii(out))
                out << int(c[0]) << " " << int(c[1]) << " " << int(c[2]) << " " << int(c[3]);
            else
                out.write(reinterpret_cast<const char*>(&c), sizeof(c));
            return out;
        }
    };
} // namespace CGAL
int main(int, char**)
{
    std::vector<PC> points; // store points
    for (int i = 0; i < 1000; ++i)
        points.push_back(std::make_tuple(Point(i / 10., i / 20., i / 30.),
            CGAL::make_array(
                static_cast<unsigned char>(214),
                static_cast<unsigned char>(40),
                static_cast<unsigned char>(3),
                static_cast<unsigned char>(255)
            )));
    std::ofstream f("E:\\out.ply", std::ios::binary);
    CGAL::IO::set_binary_mode(f); // The PLY file will be written in the binary format
    CGAL::IO::write_PLY_with_properties(f, points,
        CGAL::make_ply_point_writer(Point_map()),
        std::make_tuple(Color_map(),
            CGAL::IO::PLY_property<unsigned char>("red"),
            CGAL::IO::PLY_property<unsigned char>("green"),
            CGAL::IO::PLY_property<unsigned char>("blue"),
            CGAL::IO::PLY_property<unsigned char>("alpha")));
    return EXIT_SUCCESS;
}

结果

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值