home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / TARFILE.GZ / tarfile / ch_4.3 / xcp / proc_pol.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  3.3 KB  |  140 lines

  1. /* 
  2.  * proc_pol.c
  3.  * 
  4.  * Practical Algorithms for Image Analysis
  5.  * 
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /*
  10.  * PROC(ess)_POLY(gon)
  11.  *
  12.  * polygon analysis:
  13.  * -----------------
  14.  *   evaluate curvature points
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <math.h>
  20. #include "ip.h"
  21.  
  22. #define    ON_MODE        1
  23. #define    OFF_MODE    0
  24. #define    C_HEIGHT    10
  25. #define    C_WIDTH        10
  26. #define    C_COLOR        255
  27.  
  28.  
  29. #define N2_LIMIT    64L
  30. #define    MAX_ZRFD    15L
  31. #define N_MOMENTS    15
  32. #define N_MODE_PARMS    2
  33.  
  34. #define ENLARGE        1.0
  35. #define NOLOOP         0
  36. #define MAX_ROW     2048
  37. #define LOOP_COMPLETE    8.0
  38.  
  39. #define SQ2        1.414213562
  40. #define ZERO        0.0
  41. #define FABS(a)        ((a) > ZERO ? (a) : - (a))
  42.  
  43. #define ON        1
  44. #define OFF        0
  45. #define SHIFT_CENTROID    ON
  46. #define    SAVE_SHAPE    OFF
  47.  
  48. #define    EVAL_CURV_EN    OFF        /* evaluate curv. energy */
  49. #define    POLY_FIT    ON             /* invoke Wall-Danielsson fit */
  50. #define    DRAW_BDY_ONLY    OFF       /* draws reconstructed polygonal contour */
  51.  
  52. #define DISPLAY_DATA
  53. #define FILE_EXT ".zdt"
  54.  
  55.  
  56. /* globals */
  57. //extern int page;
  58. extern char *in_filename;
  59.  
  60.  
  61. struct polygon *
  62. select_poly (struct polygon *poly_head, Image * imgIO, int value)
  63. {
  64.   struct polygon *current_poly;
  65.  
  66.   int c, k;
  67.  
  68.   int rad_mode = OFF, tan_mode = OFF;
  69.  
  70.   float *a_n = NULL, *b_n = NULL;
  71.  
  72.   int n_mom = N_MOMENTS;
  73.   int n_mp = N_MODE_PARMS;
  74.  
  75.   double av_dirn = 0.0;
  76.  
  77.   struct Bdy bd, *bdp = &bd;
  78.   struct spoint cursor, *pc = &cursor;
  79.   FILE *fOut;
  80.   char in_buf[IN_BUF_LEN];
  81.   char *strptr;
  82.  
  83. /*
  84.  * cycle through list of boundary polygons
  85.  */
  86.   printf ("...enter n to cycle, y to select poly...\n");
  87.   current_poly = poly_head;
  88.   for (;;) {
  89.     pc->x = current_poly->first_x;
  90.     pc->y = current_poly->first_y;
  91.     printf ("poly starting at (%d, %d) select?(y/n) ", pc->x, pc->y);
  92.     if ((c = readlin (in_buf)) == 'y')
  93.       break;
  94.     if ((current_poly = current_poly->next_poly) == NULL) {
  95.       printf ("At end of polygon list!!  Return to first polygon?(y/n) ");
  96.       if ((c = readlin (in_buf)) == 'n')
  97.         return (current_poly);
  98.       else
  99.         current_poly = poly_head;
  100.     }
  101.   }
  102.  
  103.   printf ("...curvature points delta_phik[] and delta_lk[]:\n");
  104.  
  105.   printf (" delta_phik:\n");
  106.   for (k = 0; k < current_poly->poly_points; k++) {
  107.     printf ("%7.2f  ", *(current_poly->d_phi_ptr + k));
  108.     if ((k + 1) % 8 == 0)
  109.       printf ("\n");
  110.   }
  111.   printf ("\n   delta_lk:\n");
  112.   for (k = 0; k < current_poly->poly_points; k++) {
  113.     printf ("%7.2f  ", *(current_poly->d_l_ptr + k));
  114.     if ((k + 1) % 8 == 0)
  115.       printf ("\n");
  116.   }
  117.  
  118.   c = 0;
  119.   printf ("\n...write curvature points to file file (%s)?(y/n)", FILE_EXT);
  120.   while ((c != 'y') && (c != 'n'))
  121.     c = readlin (in_buf);
  122.   if (c == 'y') {
  123.     printf ("...enter filename: ");
  124.     readlin (in_buf);
  125.     if (((strptr = strstr (in_buf, FILE_EXT)) && (strptr != (in_buf + strlen (in_buf) - strlen (FILE_EXT)))) || !strptr)
  126.       strcat (in_buf, FILE_EXT);
  127.     if ((fOut = fopen (in_buf, "w")) == NULL) {
  128.       puts ("\n...cannot open output file");
  129.       exit (1);
  130.     }
  131.     fprintf (fOut, "#Curvature points for image file %s\n", in_filename);
  132.     fprintf (fOut, "%d\n", current_poly->poly_points);
  133.     for (k = 0; k < current_poly->poly_points; k++) {
  134.       fprintf (fOut, "%d  %f\n", (int) *(current_poly->d_phi_ptr + k), *(current_poly->d_l_ptr + k));
  135.     }
  136.     fclose (fOut);
  137.   }
  138.   return (current_poly);
  139. }
  140.