如何在构建半边数据结构时找到对边

时间:2012-09-17 17:01:10

标签: data-structures cg

我找不到一种有效的方法来生成给定边的相对边。我的想法只是做迭代:

//construct the opposite half edges
for(int j=0;j<edge_num;j++)
        for(int m=0;m<edge_num;m++)
if(edge[j].vert_end->v_index==edge[m].vert_start->v_index  && 
   edge[j].vert_start->v_index==edge[m].vert_end->v_index )
                {
                    edge[j].pair = &edge[m];
                    edge[m].pair = &edge[j];
                }

从加载.M文件的过程生成有关半边的其他信息。 我的结构是:

class HE_vert{
public:
    GLfloat x, y, z;
    int v_index;
    HE_edge *edge;
};

class HE_face{
public:
    int v1, v2, v3;
    int f_index;
    HE_edge* edge;
};

class HE_edge{
public:
    HE_edge(){ pair = NULL; }
public:
    HE_vert* vert_start;   // vertex at the start of the half-edge
    HE_vert* vert_end;   // vertex at the end of the half-edge
    HE_edge* pair;   // oppositely oriented adjacent half-edge
     HE_face* face;   // face the half-edge borders
    HE_edge* next;   // next half-edge around the face
    int e_index;
};

我检查了所有输出信息并且它是正确的,但是花了很长的计算时间,特别是在加载bunny.M时。我怎样才能以更有效的方式做到这一点?你能给我一些提示吗?

1 个答案:

答案 0 :(得分:0)

// grid[i + vert_num*j] = edge from i to j
int grid[vert_num*vert_num]; // malloc()?

// memset()?
for (int i = vert_num*vert_num - 1; i >= 0; i--)
{
    grid[i] = -1;
}

for (int i = 0; i < edge_num; i++)
{
    int i_from = edge[i]->vert_start->v_index;
    int i_to = edge[i]->vert_end->v_index;
    int pair_index = grid[i_to + vert_num*i_from];

    if (pair_index >= 0)
    {
        edge[i]->pair = edge[pair_index];
        edge[pair_index]->pair = edge[i];
        grid[i_to + vert_num*i_from] = -1;
    }
    else
    {
        grid[i_from + vert_num*i_to] = i;
    }
}

可能的优化:使用链表而不是大数组。每行/每列只有大约1-4个条目。