2017-02-09 140 views
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, &current_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, &current_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, &current_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,但代碼保持執行無限期,我不知道爲什麼(即使一步一步調試)。下面是輸出:

enter image description here

正如你所看到的,當它達到n_triang的價值,應立即停止,而是繼續下去。

感謝任何人都可以提供幫助。

+1

這是用於調試'while'環路一些普遍性的建議。你的停止條件是'next

+1

found/not found/etc消息沒有幫助 - 爲你的printf()添加變量值。特別是你的返回值,因爲它被分配給'next'和'n_triang' – KevinDTimm

+0

我建議你用'TRUE'或'FALSE'將任何和所有變量的比較轉換爲如下形式:if(variable){'或'if(!variable){'。特別是,C認爲*任何非零值爲「真」。所以如果你用'TRUE'(可能#define定義爲1)並且某個函數返回7,那麼行爲就不會是你所期望的。 –

回答

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; 

您正在測試三角形,如果使用/未使用的,這表明你認爲三角形是某種形式的「記憶庫」。這可以。

但是,您不約束您的索引變量(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;