C-在一维字节数组中索引x,y,z坐标 [英] C- Indexing x,y,z coordinates in a 1D byte array
问题描述
我想评估曲面的值以实施行进的四面体算法,但我不了解如何使用.raw无格式数据。
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文件加载到一维字节数组中后,应应用哪种算术转换从中获取与X,Y,Z关联的值?这是我知道加载.raw文件的唯一方法,是否可以创建3D字节数组而不是此格式?怎么样?
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);
推荐答案
在(x ,y,z)是:
The way you'd index the datum at (x, y, z) is:
pVolume[((x * 256) + y) * 256 + z]
在幕后,这是C编译器为您编写的:
Behind the scenes, this is what the C compiler does for you if you write:
GLuByte array[256][256][256];
array[x][y][z]
仅适用那仅仅是因为C从0开始索引;如果从1开始对语言进行索引,则在进行索引之前,必须对计算进行修改以实现通过从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.
您能概括任意尺寸的公式吗?
Can you generalize the formula for arbitrary dimensions?
给出(其中数值并不重要):
Given (where the numeric values don't really matter):
DIMx = 256
DIMy = 128
DIMz = 64
一维数组 pData
中(x,y,z)处的基准位于:
the datum at (x, y, z) in 1D array pData
is found at:
pData[((x * DIMx) + y) * DIMy + z]
DIMz的值主要用于验证: 0< = z< DIMz
(使用数学表示法,而不是C表示法),并且并行 0< = x< DIMx; 0< = y< = DIMy
。 z
的C表示法是 0< = z& & DIMz
;对 x
和 y
重复必要修改。
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
.
这篇关于C-在一维字节数组中索引x,y,z坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!