我正在尝试.vtk
使用 C 编写要读入 VisIt 的旧文件。不幸的是,我安装的 VisIt 程序拒绝呈现我正在编写的 VTK 文件,阅读:“本地主机失败”
下面是用于从一个文件中读取数据并将其转换为旧版 VTK 文件的代码。我使用宏XPIX
、YPIX
和ZPIX
来描述像素网格的尺寸。每个像素包含一个标量密度值。我已经使用行主要排序在“网格文件”中列出了像素:即
int list_index(x,y,z) = YPIX * ZPIX * x + ZPIX * y + z;
此像素列表中的每个条目都被读入一个名为 double 类型的数组grid[]
,并写入outfile
旧版 VTK 标头数据下方:
/*Write vtk header */
fprintf(outfile,"# vtk DataFile Version 3.0\n");
fprintf(outfile,"Galaxy density grid\nASCII\nDATASET STRUCTURED_POINTS\n");
fprintf(outfile,"DIMENSIONS %d %d %d \n", (XPIX+1), (YPIX+1), (ZPIX+1));
fprintf(outfile,"ORIGIN 0 0 0\n");
fprintf(outfile,"SPACING 1 1 1\n");//or ASPECT_RATIO
fprintf(outfile,"CELL_DATA %d\n", totalpix);
fprintf(outfile,"SCALARS cell_density float 1\n");
fprintf(outfile, "LOOKUP_TABLE default\n");
/*Create Memory Space to store Pixel Grid*/
double *grid;
grid = malloc(XPIX * YPIX * ZPIX * sizeof(double));
if (grid == NULL ){
fprintf(stderr, "Pixel grid of type double failed to initialize\n");
exit(EXIT_FAILURE);
}
fprintf(stderr,"Pixel grid has been initialized.\n Now reading infile\n");
/*Read infile contents into double grid[], using Row-Major Indexing*/
double rho;
char newline;
int i, j, k;
for(i = 0; i < XPIX; i++){
for(j = 0; j < YPIX; j++){
for(k = 0; k < ZPIX; k++){
fscanf(infile, "%lf", &rho);
grid[getindex(i,j,k)] = rho;
}
}
fprintf(stderr,"%d\n", i);
}
fprintf(stderr,"Finished reading\n");
#if !DEBUG
/*Write out grid contents in Row major order*/
fprintf(stderr,"Now writing vtk file");
for(i = 0; i < XPIX; i++){
for(j = 0; j < YPIX; j++){
for(k = 0; k < ZPIX; k++){
fprintf(outfile, "%lf ", grid[getindex(i,j,k)]);
}
fprintf(outfile,"\n");
}
}
fprintf(stderr,"Finished Writing to outfile\n");
#endif
通过此例程运行网格数据列表后,我XPIX*YPIX
在 lookup_table 中有行,每行都有ZPIX
条目。这是不正确的格式吗?VisIt 继续无法读取输入文件。我知道structured_points
可能使用列主索引的事实,但我的第一个目标当然是从 VisIt 获得某种结果。我想最终使用标量 cell_density 绘制轮廓。我的数据集是否太大?