我是C的新手,目前在C中进行BREP(边界表示)实现,其思路是:给定拓扑矩阵和顶点矩阵,以生成具有实体连通性信息的有序表。我遇到的问题是这两个循环:
主循环
while(next < n_triang)
{
/* extract the triangle to be used and the next in queue */
next = extract_next(grid_triang, n_triang, next, ¤t_triang);
/* set triang_vertex as the current triangle */
for(i = 0; i < VERTEX_COUNT; i++)
{
triang_vertex[i] = (int)grid_triang[current_triang][i];
}
/* find the orientation of the current triangle */
orientation_wrong = find_connection(triang_vertex, grid_edges, &n_edges, &partner_triang, &partner_edge);
/* if there is no partner triangle, set status to free */
if(partner_triang == FALSE)
{
grid_triang[current_triang][USE_INDEX] = TRIANG_FREE;
}
else
{
/* if orientation is wrong, change the order of vertex and add the triangle to the mesh */
if( orientation_wrong == TRUE)
{
temp = triang_vertex[Y_];
triang_vertex[Y_] = triang_vertex[Z_];
triang_vertex[Z_] = temp;
for(i=0; i<VERTEX_COUNT; i++)
{
grid_triang[current_triang][i] = (float)triang_vertex[i];
}
add_triangle(triang_vertex, next, grid_vertex, grid_edges, ¤t_triang, &n_edges);
/* add the current triangle to the faces grid */
for (i = 0; i < n_points; i++)
{
grid_faces[i][current_triang] = triang_vertex[i];
}
}
else
{
/* if orientation is correct, add the triangle to the mesh and to the faces grid */
add_triangle(triang_vertex, next, grid_vertex, grid_edges, ¤t_triang, &n_edges);
for (i = 0; i < VERTEX_COUNT; i++)
{
grid_faces[i][current_triang] = triang_vertex[i];
}
}
}
}
extract_next功能
int extract_next(float **grid_triang, int n_triang, int next, int *current_triang)
{
/* define variables */
int new_next = 0, visited_triang = 0, kill = 0;
int found_triang = FALSE;
/* if triangle is used throw error */
if(grid_triang[next][USE_INDEX] == TRIANG_USED )
{
perror("Current triangle not available");
exit(EXIT_FAILURE);
}
else *current_triang = next; /* set current triangle to output */
/* change status to used */
grid_triang[next][USE_INDEX] = TRIANG_USED;
next++;
/* start the iteration */
while(!found_triang && visited_triang < n_triang)
{
/* if triangle is used, go to next triangle and add a visited */
if(grid_triang[next][USE_INDEX] == TRIANG_USED)
{
next++;
visited_triang++;
printf("Used, moving");
}
else if(grid_triang[next][USE_INDEX] == TRIANG_FREE)
{
/* if triangle is free then break the loop */
found_triang = TRUE;
printf("found a free one!\n");
}
else printf("not valid");
}
/* if found triangle set the next triangle to output */
if (found_triang)
{
new_next = next;
}
printf("New triangle extracted\n");
return new_next;
}
当变量 next 等于 n_triang 时,代码应停止执行,但代码会无限期地执行,我无法理解原因(即使是逐步调试)。这是输出:
正如您所看到的,当它达到 n_triang 的值时,它应该停止但是会无限期地继续。
感谢任何可以提供帮助的人。
答案 0 :(得分:0)
我怀疑你的问题在于:
/* start the iteration */
while(!found_triang && visited_triang < n_triang)
{
/* if triangle is used, go to next triangle and add a visited */
if(grid_triang[next][USE_INDEX] == TRIANG_USED)
{
next++;
visited_triang++;
printf("Used, moving");
}
else if(grid_triang[next][USE_INDEX] == TRIANG_FREE)
{
/* if triangle is free then break the loop */
found_triang = TRUE;
printf("found a free one!\n");
}
else printf("not valid");
}
/* if found triangle set the next triangle to output */
if (found_triang)
{
new_next = next;
}
printf("New triangle extracted\n");
return new_next;
您正在测试是否使用了三角形,这表明您认为三角形是某种内存池。&#34;这很好。
但是,您不会将索引变量(next
)限制为保持在池的范围内。我相信你会得到一个相当高的起点,然后走出阵列的末端。
我建议您添加一些索引检查,并在需要时将索引包装回零:
#define TRIANGLE_USED(n) (grid_triang[(n)][USE_INDEX] == TRIANG_USED)
for (n_visited = 0; n_visited < n_triang; ++n_visited)
{
// The `next` variable is an index into a pool of triangles.
// Don't overrun the pool - reset to the bottom if needed.
if (next >= n_triang) {
next = 0;
}
if (TRIANGLE_USED(next)) {
printf("Triangle %d is used. Skipping.", next);
++next;
}
else {
printf("Triangle %d is free. Stopping.", next);
break;
}
/*NOTREACHED*/
}
return next;