简体   繁体   中英

C- Indexing x,y,z coordinates in a 1D byte array

I want to evaluate the values of the surface to implement a marching tetrahedron algorithm, but I don't understand how to work with .raw unformatted data.

After loading a .raw file with a volume dataset into a 1D byte array, what arithmetic transformation should be applied to get the value associated to X,Y,Z from it? This is the only way I know to load .raw files, could I create a 3D byte array instead of this? How?

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[size]; //<- here pVolume is a 1D byte array 
   fread(pVolume,sizeof(GLubyte),size,pFile);
   fclose(pFile);

The way you'd index the datum at (x, y, z) is:

pVolume[((x * 256) + y) * 256 + z]

Behind the scenes, this is what the C compiler does for you if you write:

GLuByte array[256][256][256];

array[x][y][z]

It only works that simply because C indexes from 0; if the language indexed from 1, you'd have to revise the calculation to achieve the net result obtained by subtracting one from each of x, y and z before doing the indexing.


Auxilliary Question

Can you generalize the formula for arbitrary dimensions?

Given (where the numeric values don't really matter):

DIMx = 256
DIMy = 128
DIMz =  64

the datum at (x, y, z) in 1D array pData is found at:

pData[((x * DIMx) + y) * DIMy + z]

The value of DIMz serves primarily for validation: 0 <= z < DIMz (using mathematical rather than C notation), and in parallel 0 <= x < DIMx; 0 <= y <= DIMy 0 <= x < DIMx; 0 <= y <= DIMy . The C notation for z is 0 <= z && z < DIMz ; repeat mutatis mutandis for x and y .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM