I'm trying to read .bin point cloud files. I found this link suggesting a python code that I can convert to C++. I came up with the following code, but the precision of the floating point numbers are different compared to the results that I got from running the python code in the above link. I noticed that some coordinate values in the middle are totally missing, or in other words, the count of the floating point values that resulted from python is more than that of the C++ code:
std::ifstream file("C:/Users/hedey/OneDrive/Documents/Research_papers/STDF/data_object_velodyne/training/velodyne/001984.bin", ios::in | std::ios::binary);
char header[4];
float item;
if (file.is_open())
{
file.read((char*)&header, 4);
cout << char(header) << endl;
while (!file.eof())
{
file.read((char*)&item, 4);
cout << float(item) << endl;
}
file.close();
}
Here is a link to the bin file I was trying to read: https://www.dropbox.com/s/m6gney49lmr5vg9/001984.zip?dl=0
Also, the header string is not showing up using the above C++ code. What might be wrong with my code? How can it be improved?
Here's a code that produces exactly the same output as the Python version:
#include <iostream>
#include <fstream>
#include <cmath>
int main()
{
std::ifstream file("1.pcd.bin", std::ios::in | std::ios::binary);
if (!file) return EXIT_FAILURE;
float item;
while (file.read((char*)&item, 4))
{
std::cout << "[" << item;
if (std::round(item) == item) std::cout << ".";
std::cout << "]\n";
}
}
Now, where did you go wrong?
while
loop. It's far better than your testing for the end-of-file condition, which is usually incorrect.