0
我是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;
}
代碼應停止執行時可變下一個等於n_triang,但代碼保持執行無限期,我不知道爲什麼(即使一步一步調試)。下面是輸出:
正如你所看到的,當它達到n_triang的價值,應立即停止,而是繼續下去。
感謝任何人都可以提供幫助。
這是用於調試'while'環路一些普遍性的建議。你的停止條件是'next
found/not found/etc消息沒有幫助 - 爲你的printf()添加變量值。特別是你的返回值,因爲它被分配給'next'和'n_triang' – KevinDTimm
我建議你用'TRUE'或'FALSE'將任何和所有變量的比較轉換爲如下形式:if(variable){'或'if(!variable){'。特別是,C認爲*任何非零值爲「真」。所以如果你用'TRUE'(可能#define定義爲1)並且某個函數返回7,那麼行爲就不會是你所期望的。 –