gpt4 book ai didi

c++如何以二进制格式输出ply文件

转载 作者:行者123 更新时间:2023-11-30 03:57:32 40 4
gpt4 key购买 nike

我正在尝试创建一个包含以下 header 的二进制 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;
};

这不起作用并生成无效的层文件。但是,如果我使用相同的代码(更改 header )来生成 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';

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

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

最佳答案

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

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

关于c++如何以二进制格式输出ply文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27906681/

40 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com