【问题标题】:c++ how to output ply file in binary formatc ++如何以二进制格式输出ply文件
【发布时间】:2015-01-12 16:37:28
【问题描述】:

我正在尝试创建一个带有以下标头的二进制 PLY 文件:

ply
format binary_little_endian 1.0
element vertex 43000
property float x
property float y
property float z
property float nx
property float ny
property float nz
property uchar red
property uchar green
property uchar blue
end_header

我有以下重载函数可以写成二进制(用另一个例子验证):

inline void write_fbin(std::ofstream &out, float val) {
    out.write(reinterpret_cast<char*>(&val), sizeof(float));
}



inline void write_fbin(std::ofstream &out, unsigned char val) {
    out.write(reinterpret_cast<char*>(&val), sizeof(unsigned char));
}

我将顶点信息写如下:

write_fbin(ofstr, static_cast<float>(point.x));
write_fbin(ofstr, static_cast<float>(point.y));
write_fbin(ofstr, static_cast<float>(point.z));
write_fbin(ofstr, static_cast<float>(point.n_x));
write_fbin(ofstr, static_cast<float>(point.n_y));
write_fbin(ofstr, static_cast<float>(point.n_z));
write_fbin(ofstr, static_cast<unsigned char>(point.r));
write_fbin(ofstr, static_cast<unsigned char>(point.g));
write_fbin(ofstr, static_cast<unsigned char>(point.b));

point 是一个类型的结构

struct DensePoint {
    float x, y, z;
    float n_x, n_y, n_z;
    unsigned char r, g, b;
};

这不起作用并产生无效的层文件。但是,如果我使用相同的代码(更改标头)来生成 ASCII 版本,

ofstr
            << point.x << ' '
            << point.y << ' '
            << point.z << ' '
            << point.n_x << ' '
            << point.n_y << ' '
            << point.n_z << ' '
            << static_cast<int>(point.r) << ' '
            << static_cast<int>(point.g) << ' '
            << static_cast<int>(point.b) <<
            '\n';

这非常有效。有什么问题?

也许我需要在二进制格式的每个顶点的末尾引入一个换行符?

【问题讨论】:

  • end_header 后面有行尾吗?
  • 是的,二进制和 ascii 格式都可以
  • 根据cs.virginia.edu/~gfx/Courses/2001/Advanced.spring.01/plylib/…,您的数据的字节数是否正确?您可以使用固定长度类型作为 uint8 等...以确保
  • 请发布一个示例输出文件,或者至少是其中的一些sn-p。
  • @dkg my write_fbin() 函数保证大小计算正确

标签: c++ ply-file-format


【解决方案1】:

当向/从似乎与预期格式不匹配的文件写入/读取二进制数据时,很可能在操作系统级别存在字符转换,将某些字节组合替换为其他字节组合(es 0x0C 变为 0x0C 0x0A )。

您很可能已经以文本模式(C++ 流的默认设置)打开了该文件,而该文件应该是二进制文件,以使操作系统行为保持中立。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 2012-02-15
    • 2017-09-21
    • 1970-01-01
    相关资源
    最近更新 更多