0

As in my previous question, I'm interested in loading a .raw file of a volume dataset into a byte array. I think using a 3D byte array would make things easier when indexing the X,Y,Z coordinates, but I'm not sure about the read size that I should use to load the volume. Would this size declaration allow me to index the volume data correctly?

int XDIM=256, YDIM=256, ZDIM=256;
const int size = XDIM*YDIM*ZDIM;
bool LoadVolumeFromFile(const char* fileName) {

    FILE *pFile = fopen(fileName,"rb");
   if(NULL == pFile) {
    return false;
   }

   GLubyte* pVolume=new GLubyte[XDIM][YDIM][ZDIM]; 
   fread(pVolume,sizeof(GLubyte),size,pFile); // <-is this size ok? 
   fclose(pFile);
Community
  • 1
  • 1
andandandand
  • 21,946
  • 60
  • 170
  • 271
  • 1
    Strictly speaking, your code example is C++, not C (since it uses `new`). That size will read enough bytes from the file to fill *pVolume, assuming there is enough data in the file. But I might declare it as `size = sizeof( GLubyte[XDIM][YDIM][ZDIM])` just in case you ever change to a 16, 32, n-bit data type. – Brian McFarland Feb 21 '12 at 19:51

1 Answers1

0

From the code you posted the fread() call appears to be safe, but consider if a 3D byte array is the best choice of a data structure.

I assume you are doing some kind of rendering as you are using GLubyte. And of course to do any rendering you need to access a vertex defined in 3D space. That will lead to:

pVolume[vertIndex][vertIndex][vertIndex]  

This will constantly cause your cahce to be thrashed. The memory will be laid out will all the xs first, then all the ys, and then all the zs. Thus, each time you jump from an x to y to z you may hit a cache miss and really slow perf.

Unknown1987
  • 1,671
  • 13
  • 30