简体   繁体   English

C-在1D字节数组中索引x,y,z坐标

[英]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. 我想评估曲面的值来实现行进四面体算法,但我不明白如何使用.raw无格式数据。

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? 将带有卷数据集的.raw文件加载到1D字节数组后,应该应用什么算术转换来获取与X,Y,Z相关的值? This is the only way I know to load .raw files, could I create a 3D byte array instead of this? 这是我知道加载.raw文件的唯一方法,我可以创建一个3D字节数组而不是这个吗? 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: 在(x,y,z)索引数据的方式是:

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

Behind the scenes, this is what the C compiler does for you if you write: 在幕后,这就是C编译器为您编写的内容:

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

array[x][y][z]

It only works that simply because C indexes from 0; 它的作用只是因为C索引从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. 如果语言从1开始索引,则必须修改计算以获得通过在进行索引之前从x,y和z中的每一个中减去一个而获得的净结果。


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: 1D数组pData中(x,y,z)的数据位于:

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 DIMz的值主要用于验证: 0 <= z < DIMz (使用数学而不是C表示法),并行0 <= x < DIMx; 0 <= y <= DIMy 0 <= x < DIMx; 0 <= y <= DIMy . 0 <= x < DIMx; 0 <= y <= DIMy The C notation for z is 0 <= z && z < DIMz ; z的C表示法是0 <= z && z < DIMz ; repeat mutatis mutandis for x and y . 重复比照 xy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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