Reading in cloud compare sbf binary files using C++, does not give me the expected output

Feel free to ask any question here
Post Reply
DanMenzz
Posts: 4
Joined: Wed Jun 17, 2020 6:42 pm

Reading in cloud compare sbf binary files using C++, does not give me the expected output

Post by DanMenzz »

I ran into a problem and was hoping to get some suggestions. I am working with a binary file from a software called Cloud Compare. The binary file has the contents described here, https://www.cloudcompare.org/doc/wiki/i ... ?title=SBF. The goal is to read in this file; correctly display the content onto the terminal; and then write the content onto a new file thus creating a duplicate. However, the issue is that the content displayed on the terminal is not correct ( ex. point count (NP) does not give the correct number ). I have tried reading in the contents of the input binary file in two ways.


Method 1:

// Read Header Data, takes up 64 bytes
char* dbfbuff = new char[2];
unsigned long long int np;
unsigned short int ns;
std::vector<double> xyz_shift; xyz_shift.resize(3);
char* unused_buffer = new char[28];

readFile.read(dbfbuff, 2);
std::cout << dbfbuff << std::endl;
std::cout << "After reading in header flag: " << readFile.tellg() << " bytes" << std::endl;

readFile.read((char*) &np, sizeof(unsigned long long int));
std::cout << "NP: " << np << std::endl;
std::cout << "After reading in NP: " << readFile.tellg() << " bytes" << std::endl;



Method 2:

std::stringstream ss;
ss << readFile.rdbuf();

long readSize = 0;
uint16_t dbfbuff;
uint64_t t2;
size_t np;
uint16_t ns;

ss >> dbfbuff;
readSize += sizeof(uint16_t);
std::cout << dbfbuff << std::endl;
std::cout << "After reading in header flag: " << readSize << " bytes" << std::endl;

ss >> t2;
np = static_cast<size_t>(t2);
readSize += sizeof(uint64_t);
std::cout << t2 << std::endl;
std::cout << "After reading in NP: " << readSize << " bytes" << std::endl;




An example of the issue Is that for method 1, the output for point count (NP) is a very very large positive number whereas for method 2 the output is a very small positive number, but the actual value should be in the millions. Do you have any suggestions for reading in the data from the binary file? Thank you for your time!


Cheers,
Daniel Mendez
daniel
Site Admin
Posts: 7712
Joined: Wed Oct 13, 2010 7:34 am
Location: Grenoble, France
Contact:

Re: Reading in cloud compare sbf binary files using C++, does not give me the expected output

Post by daniel »

Have you looked at the code in CloudCompare? (https://github.com/CloudCompare/CloudCo ... Filter.cpp). That's definitely the source of truth for how the SBF format is organized.The wiki may be wrong?!

And otherwise, a classical issue is with the big endian / little endian representation (it depends on which platform you are running your own tool).
WargodHernandez
Posts: 187
Joined: Tue Mar 05, 2019 3:59 pm

Re: Reading in cloud compare sbf binary files using C++, does not give me the expected output

Post by WargodHernandez »

The wiki is correct, I verified both the code and a file I saved locally with a hex editor. Without knowledge of the readFile class your using I suspect the issue is somewhere in this region. If you post what your using I can probably help debug.
DanMenzz
Posts: 4
Joined: Wed Jun 17, 2020 6:42 pm

Re: Reading in cloud compare sbf binary files using C++, does not give me the expected output

Post by DanMenzz »

daniel wrote: Thu Jun 18, 2020 1:39 pm Have you looked at the code in CloudCompare? (https://github.com/CloudCompare/CloudCo ... Filter.cpp). That's definitely the source of truth for how the SBF format is organized.The wiki may be wrong?!

And otherwise, a classical issue is with the big endian / little endian representation (it depends on which platform you are running your own tool).

Thank you for the suggestions! I am currently looking into the endian representation on my machine.
DanMenzz
Posts: 4
Joined: Wed Jun 17, 2020 6:42 pm

Re: Reading in cloud compare sbf binary files using C++, does not give me the expected output

Post by DanMenzz »

WargodHernandez wrote: Thu Jun 18, 2020 5:13 pm The wiki is correct, I verified both the code and a file I saved locally with a hex editor. Without knowledge of the readFile class your using I suspect the issue is somewhere in this region. If you post what your using I can probably help debug.
Thank you for the update! I am using the C++ std library. Here is the link:
http://www.cplusplus.com/reference/istr ... ream/read/

Thanks!
WargodHernandez
Posts: 187
Joined: Tue Mar 05, 2019 3:59 pm

Re: Reading in cloud compare sbf binary files using C++, does not give me the expected output

Post by WargodHernandez »

Code: Select all

// read a file into memory
#include <iostream>     // std::cout
#include <fstream>      // std::ifstream
#include <vector>

int main() {

    std::ifstream readFile("C:\\Users\\af836c\\Desktop\\ME_Sensor_11_scanCONTROL29xx_100_NORMAL.sbf.data", std::ifstream::binary);
    char* dbfbuff = new char[2];
    unsigned long long int np;
    unsigned short int ns;
    std::vector<double> xyz_shift; xyz_shift.resize(3);
    char* unused_buffer = new char[28];

    readFile.read(dbfbuff, 2);
    std::cout << (int)dbfbuff[0] << (int)dbfbuff[1] <<  std::endl;
    std::cout << "After reading in header flag: " << readFile.tellg() << " bytes" << std::endl;

    readFile.read((char*)&np, sizeof(unsigned long long int));
    std::cout << "NP: " << np << std::endl;
    std::cout << "After reading in NP: " << readFile.tellg() << " bytes" << std::endl;

    readFile.close();
    return 0;
}
Results in the following output on an x64 Intel Windows 10 system:

Code: Select all

4242
After reading in header flag: 2 bytes
NP: 1494655216077766656
After reading in NP: 10 bytes

C:\Users\af836c\source\repos\SBFTest\x64\Debug\SBFTest.exe (process 19436) exited with code 0.
Press any key to close this window . . .
That is the correct output for this file.
Just making sure you are loading the .sbf.data file and not the .sbf file correct?

Otherwise it is the endian issue, if you are on an ARM device this would definitely be an issue
DanMenzz
Posts: 4
Joined: Wed Jun 17, 2020 6:42 pm

Re: Reading in cloud compare sbf binary files using C++, does not give me the expected output

Post by DanMenzz »

WargodHernandez wrote: Thu Jun 18, 2020 9:34 pm 4242
After reading in header flag: 2 bytes
NP: 1494655216077766656
After reading in NP: 10 bytes

C:\Users\af836c\source\repos\SBFTest\x64\Debug\SBFTest.exe (process 19436) exited with code 0.
Press any key to close this window . . .[/code]

That is the correct output for this file.
Just making sure you are loading the .sbf.data file and not the .sbf file correct?

Otherwise it is the endian issue, if you are on an ARM device this would definitely be an issue
That looks right to me and yes I was using the .sbf.data file. I definitely have an Endian issue and am currently trying to go from little endian to big endian. Any recommendations for doing so?
Post Reply