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 <暗淡;0 <= y <= DIMy
.z
的 C 表示法是 0 <= z &&z<DIMz
;对 x
和 y
重复 mutatis mutandis.
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屋!