繁体   English   中英

C ++如何以二进制格式输出层文件

[英]c++ how to output ply file in binary format

我正在尝试使用以下标头创建一个二进制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';

这很完美。 有什么事吗

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

在似乎与预期格式不匹配的文件中写入二进制数据或从中读取二进制数据时,很有可能在OS级别进行了字符转换,将某些字节组合替换为其他字节组合(es 0x0C变为0x0C 0x0A)。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM