0

我正在尝试编写一个程序来读取包含四面体格式的文件:

    T 109 20 10 19
   [T X1 X2 X3 X4 ]
    T 123 34 24 33
    T 182 83 82 92

其中每行中的每个数字 X 表示数据文件第 X 行的三元组(x、y 和 z 坐标):

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;
}
4

1 回答 1

0

格式类似于 .obj 格式,解析起来并不难。你过得很好;在这一点上,您的错误大多只是拼写错误。

只需就地使用数据而不是指针:

Vertex *vert[4];

变成:

Vertex vert[4];

您还需要将所有 '->' 符号更改为点 (.) 符号,例如:

    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] ];

并且您的测试代码应该将 n 降低一个(当前是 lastLine+1):

    // printing 13 variables in total... 
n--; // n must be last entry

最后,我在你的分配上得到了一个编译错误,并且不得不对它们进行类型转换:

Vertex *vert_dt = (Vertex*) malloc( MAX * sizeof (Vertex) ) ;

Vertex *vert_std = (Vertex*) malloc( MAX * sizeof (Vertex) ) ;

Tetra *tetrahedron = (Tetra*) malloc(MAX * sizeof (Tetra) );

请注意, fscanf() 非常严格,因此带有额外前导空格的输入文件可能会使程序感到困惑。我在虚构文件上测试了这些更改,所以不能保证。

希望这可以帮助!这是我的完整文件:

#define FILEPATHtri "grid1DT.txt"
#define FILEPATHorg "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 = (Vertex*) malloc( MAX * sizeof (Vertex) ) ;

    Vertex *vert_std = (Vertex*) malloc( MAX * sizeof (Vertex) ) ;

    Tetra *tetrahedron = (Tetra*) 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... 
        n--; // rewind to last tetrahedron
        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;
}
于 2013-02-10T21:47:33.597 回答