I have a binary data file. There are over 4 millions LIDAR data records. Every record is stored as four numbers - three floats (coordinates x, y, z) and one integer (unimportant now). I should implement a function which gets minimal and maximal coordinate in every axis. I wrote the code below, the algorithm looks to be very straightforward and simple, but it's not working (it returns every minimum as 0.0f and every maximum as 1.0f). Do I have anything wrong?
void get_min_max(const char *filename, float *a_min_x, float *a_max_x, float *a_min_y, float *a_max_y, float *a_min_z, float *a_max_z) {
FILE *f = NULL;
float x, y, z;
float min_x, min_y, min_z, max_x, max_y, max_z;
int l_type;
f = fopen(filename, "rb");
if (!f) {
printf("No binary file read!\n");
exit(-1);
}
min_x = min_y = min_z = std::numeric_limits<float>::max();
max_x = max_y = max_z = -std::numeric_limits<float>::max();
while (true) {
x = fread((void*)(&x), sizeof(x), 1, f);
y = fread((void*)(&y), sizeof(y), 1, f);
z = fread((void*)(&z), sizeof(z), 1, f);
min_x = fminf(x, min_x);
min_y = fminf(y, min_y);
min_z = fminf(z, min_z);
max_x = fmaxf(x, max_x);
max_y = fmaxf(y, max_y);
max_z = fmaxf(z, max_z);
l_type = fread((void*)(&l_type), sizeof(l_type), 1, f);
if (feof(f)) {
break;
}
}
fclose(f);
*a_min_x = min_x; // = 0.0f ???
*a_min_y = min_y; // = 0.0f ???
*a_min_z = min_z; // = 0.0f ???
*a_max_x = max_x; // = 1.0f ???
*a_max_y = max_y; // = 1.0f ???
*a_max_z = max_z; // = 1.0f ???
}
Your problem is assigning fread
s result into your variable:
x = fread(&x, ...)
First fread
reads the data into x, then it returns the number of elements read (1), and then x becomes 1.0.
You should really learn how to use a debugger, you would have seen this very quickly.