我試圖寫一個程序,讀取包含四面體文件格式化爲這樣:解析和嵌套指針
T 109 20 10 19
[T X1 X2 X3 X4 ]
T 123 34 24 33
T 182 83 82 92
其中每行中的每個數字X表示三重峯(x的,y和z座標)在數據文件的行X:
line 1 x y z
line 2 x y z
[.......]
line n x y z
我在這裏Parsing data from ASCII formatted file in C發佈了類似(但簡單)的問題。這一次,我的想法是讀取四面體文件,並將表示這些四面體頂點座標的4行數字存儲在數組vertid[]
(0到3)中,然後使用指向數組的指針數組將四面體鏈接到頂點Vertex
結構。我將總結:將行ID號存儲在四面體結構中,然後在正確的頂點處將指針設置爲「點」。
請記住,我不是程序員(我需要這個來產生Voronoi圖)。如果不是存儲化妝四面體的頂點,而是存儲邊緣(因爲我相信實際上需要邊緣數據而不是用於生成Voronoi D的頂點數據)? 我花了我的星期日在這工作,它不工作,我沒有理解爲什麼。 謝謝。
(不工作)例如:
#define FILEPATHtri "/pathto/grid1DT.txt"
#define FILEPATHorg "/pathto/grid1.txt"
#define MAX 10000
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "math.h"
typedef struct
{
float x;
float y;
float z;
char ID;
}Vertex;
/* NOT USED ATM
typedef struct {
edge_ref next[4]; // pointers to other edges (AQE data structure)
void *data[4]; // pointers to endpoints of edge
unsigned ID; // ID of edge
}Edge;
*/
typedef struct
{
Vertex *vert[4];
int vertid[4];
char t;
}Tetra;
void file_link (void)
{
Vertex *vert_dt = malloc(MAX * sizeof (Vertex)) ;
Vertex *vert_std = malloc(MAX * sizeof (Vertex)) ;
Tetra *tetrahedron = malloc(MAX * sizeof (Tetra));
FILE *fp1,*fp2 ;
fp1 = fopen(FILEPATHtri,"r");
int i = 0;
while(fscanf(fp1, "%c %i %i %i %i ", &tetrahedron[i].t, &tetrahedron[i].vertid[0], &tetrahedron[i].vertid[1],
&tetrahedron[i].vertid[2], &tetrahedron[i].vertid[3]) == 5) // Saving the line numbers into vertid[0..3]
{
i++;
}
fclose(fp1);
fp2 = fopen(FILEPATHorg,"r");
int j = 0;
while(fscanf(fp2, "%f %f %f ", &vert_std[j].x, &vert_std[j].y, &vert_std[j].z) == 3)
{
j++;
}
fclose(fp2);
int n;
for (n=0; n<i; n++) {
tetrahedron[n]->vert[0]=vert_std[ tetrahedron[n].vertid[0] ]; // retriving coords from the standard file and storing
tetrahedron[n]->vert[1]=vert_std[ tetrahedron[n].vertid[1] ]; // them in the vertex pointer array of tretrahedron
tetrahedron[n]->vert[2]=vert_std[ tetrahedron[n].vertid[2] ];
tetrahedron[n]->vert[3]=vert_std[ tetrahedron[n].vertid[3] ];
}
int m = 100; //used to retrieve m'th tetra, so that I can print the tetra that I want,just for checking purposes..
// printing 13 variables in total...
printf("These are the 4 coordinates of the vertices of the %i th tetrahedron from the DT file %f %f %f\n%f %f %f\n%f %f %f\n%f %f %f\n", n,
tetrahedron[n]->vert[0]->x,tetrahedron[n]->vert[0]->y,tetrahedron[n]->vert[0]->z,
tetrahedron[n]->vert[1]->x,tetrahedron[n]->vert[1]->y,tetrahedron[n]->vert[1]->z,
tetrahedron[n]->vert[2]->x,tetrahedron[n]->vert[2]->y,tetrahedron[n]->vert[2]->z,
tetrahedron[n]->vert[3]->x,tetrahedron[n]->vert[3]->y,tetrahedron[n]->vert[3]->z);
free(vert_dt);
free(vert_std);
free(tetrahedron);
}
int main(void) {
file_link();
return 0;
}