문제

As in my previous question, I'm working loading a 1D array with volumetric data of a .raw file. The answer by Jonathan Leffler proved helpful, but now I'm working with a volume dataset of different dimensions (X,Y,Z aren't the same). How would the formula be generalized?

pVolume[((x * 256) + y) * 256 + z] // works when all dims are 256


int XDIM=256, YDIM=256, ZDIM=256; // I want this sizes to be arbitrary
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);
도움이 되었습니까?

해결책

Access in strides follows a simple principle:

A[i][j][k] = B[k + j * Dim3 + i * Dim3 * Dim2];

// k = 1..Dim3,  (or 0 <= k < Dim3, as one does in C)
// j = 1..Dim2,
// i = 1..Dim1.

Here B is a 1D array of size Dim1 * Dim2 * Dim3. The formula obviously generalizes to arbitrarily many dimensions. If you want a mnemonic, start the sum with the fasted index, and in each summand you multiply further by the extent of the previous dimension.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top