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

后端 未结 1 1320
清歌不尽
清歌不尽 2021-01-14 06:32

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 lo

相关标签:
1条回答
  • 2021-01-14 06:44

    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. The C notation for z is 0 <= z && z < DIMz; repeat mutatis mutandis for x and y.

    0 讨论(0)
提交回复
热议问题