home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / getobj3d / fill3d.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-17  |  3.0 KB  |  138 lines

  1.  
  2. /*  fill3d.c:  routines to perform a 3d flood fill using  a stack
  3.                to simulate recursion.
  4.  
  5.    Brian Tierney,   LBL
  6.  
  7. */
  8.  
  9. #include "getobj.h"
  10. /***************************************************************/
  11. int
  12. object_fill(c, r, f)        /* label all points within the item 1 */
  13.     int       c, r, f;
  14.  
  15. /* this routine uses a stack to do a 3D recursive flood fill starting
  16.    at location (c,r,f)  */
  17. {
  18.     void      sum_pixel();
  19.  
  20.     int       cnt = 0, skip = 0;/* count number of points in object */
  21.     sp = 0;            /* initialize stack pointer */
  22.  
  23.     push(-1, -1, -1);        /* null stack */
  24.     do {
  25.  
  26. start:
  27.     grid[f][r][c] = 3;
  28.     cnt++;
  29.     if (stats)
  30.         sum_pixel(c, r, f);
  31.     if (bridges > 0 && cnt > 1) {    /* check strength of connecting
  32.                      * bridges */
  33.  
  34.         if (bridges == 1) {    /* 2D bridges */
  35.         cnt += mark_edge_neighbors(c, r, f, 0);
  36.         if (count_diag_neighbor_edges(c, r, f, 0) >= 2) {
  37.             skip++;
  38.             goto w_stop;
  39.         }
  40.         }
  41.         if (bridges == 2) {    /* 3D weak bridges */
  42.         cnt += mark_edge_neighbors(c, r, f, 1);
  43.         if (count_diag_neighbor_edges(c, r, f, 1) >= 4) {
  44.             skip++;
  45.             goto w_stop;
  46.         }
  47.         }
  48.         if (bridges == 3) {    /* 3D strong bridges */
  49.         cnt += mark_edge_neighbors(c, r, f, 1);
  50.         if (count_diag_neighbor_edges(c, r, f, 1) >= 2) {
  51.             skip++;
  52.             goto w_stop;
  53.         }
  54.         }
  55.     } else {
  56.         cnt += mark_edge_neighbors(c, r, f, 0);    /* include edges */
  57.     }
  58.  
  59.     if ((f < nframe - 1) && (grid[f + 1][r][c] == 1)) {
  60.         push(c, r, f);
  61.         f++;
  62.         goto start;
  63.     }
  64.     if ((f > 0) && (grid[f - 1][r][c] == 1)) {
  65.         push(c, r, f);
  66.         f--;
  67.         goto start;
  68.     }
  69.     if ((r < nrow - 1) && (grid[f][r + 1][c] == 1)) {
  70.         push(c, r, f);
  71.         r++;
  72.         goto start;
  73.     }
  74.     if ((r > 0) && (grid[f][r - 1][c] == 1)) {
  75.         push(c, r, f);
  76.         r--;
  77.         goto start;
  78.     }
  79.     if ((c < ncol - 1) && (grid[f][r][c + 1] == 1)) {
  80.         push(c, r, f);
  81.         c++;
  82.         goto start;
  83.     }
  84.     if ((c > 0) && (grid[f][r][c - 1] == 1)) {
  85.         push(c, r, f);
  86.         c--;
  87.         goto start;
  88.     }
  89. w_stop:
  90.     pop(&c, &r, &f);
  91.  
  92.     } while (f >= 0);        /* neg i indicates empty stack */
  93.  
  94.     if (sp != 0)
  95.     fprintf(stderr, "Error: stack not empty \n");
  96.  
  97.     if (verbose)
  98.     if (bridges)
  99.         fprintf(stderr, "   fill stopped %d times due to weak bridges \n",
  100.             skip);
  101.  
  102.     return (cnt);
  103. }
  104.  
  105. /***************************************************************/
  106. push(i, j, k)            /* add location to stack */
  107.     int       i, j, k;
  108. {
  109.     sp++;
  110.     if (sp >= stack_size) {
  111.     fprintf(stderr, "recursive stack overflow!! ");
  112.     fprintf(stderr, " stack was allocated %d slots \n", stack_size);
  113.     exit(-1);
  114.     }
  115.     stack[sp].i = i;
  116.     stack[sp].j = j;
  117.     stack[sp].k = k;
  118. }
  119.  
  120. /***************************************************************/
  121. pop(i, j, k)            /* remove item from stack */
  122.     int      *i, *j, *k;
  123. {
  124.     *i = stack[sp].i;
  125.     *j = stack[sp].j;
  126.     *k = stack[sp].k;
  127.     sp--;
  128. }
  129.  
  130. /***************************************************************/
  131. alloc_stack(st_size)        /* allocate stack for non-recursive
  132.                  * flood-fill alg */
  133.     int       st_size;
  134. {
  135.     if ((stack = Calloc(st_size, STACK_ITEM)) == NULL)
  136.     perror("calloc: stack");
  137. }
  138.