C ++如何以二进制格式输出Ply文件
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';
这非常有效。可能出了什么问题?
也许我需要以二进制格式在每个顶点的末尾引入换行符?
当向
/从文件写入/读取似乎与预期格式不匹配的二进制数据时,很可能在操作系统级别存在字符转换,将某些字节组合替换为其他字节组合(0x0C变得0x0C 0x0A)。
您很可能在文本模式(C++流的默认值)中打开了文件,而该文件应该是二进制的,以使操作系统行为中立。
相关文章:
- 十进制到八进制递归C++.以标准::字符串格式输出
- 如何将非空终止字符串输出到 iostream,但保持格式
- 保证最大输出长度为双精度,并设置默认输出格式的精度
- 输出使用 cout 转换为 ASCII 的文本时输出格式不正确,C++
- 将 Tcl 中的命令输出重定向到文件时,文件以 DOS 格式生成
- 如何将 Openh264 解码帧无延迟输出格式转换为 OpenCV 矩阵
- 输出类似于 FORTRAN 的无格式C++
- 以一定格式输出
- 为什么int8_t的输出格式使用 4 个字节?
- qDebug - 如何以位(二进制格式)输出数据
- 音频缓冲区列表,用于输出流格式已压缩的 AUHAL 单元
- 使用setW和setFill的输出格式
- 二次方程求解器输出以未知格式输出
- 格式输出C 带有setW
- 如何以C++格式输出已保存.html文件中<a href>标签中的所有链接?
- C++:更改单个输出操作的格式输出
- 使用特定格式输出从 Main 间接调用函数
- C ++如何以二进制格式输出Ply文件
- 以C++格式输出格式
- 流操作,用于以不同格式输出对象数据