Search code examples
c++point-cloudsbin

Reading Point Cloud .bin file using C++


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?


Solution

  • 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?

    • you did not search the internet or did not mention here what actually .pcd.bin format is. I found the definition of the true binary pcd format here: https://pointclouds.org/documentation/tutorials/pcd_file_format.html , but this is NOT the format you deal with
    • well, so you did not understand the format, hence your problem :-)
    • the guys who "invented" pcd.bin evidently started from ASCII format, removed the header, and wrote everything else as binary, in groups of five floats.
    • So there's no header in the input.
    • Please note how I organize the while loop. It's far better than your testing for the end-of-file condition, which is usually incorrect.
    • For this reason you printed out the last item twice