1
我試圖創建二進制PLY文件,用下面的標頭輸出簾布層的文件:C++如何以二進制格式
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'後面是否有行尾? – dkg
是的,二進制和ASCII二進制 – manatttta
根據http://www.cs.virginia.edu/~gfx/Courses/2001/Advanced.spring.01/plylib/Ply.txt你的數據是否有正確的字節數?你可以使用固定長度類型作爲uint8,等等......以確保 – dkg