home *** CD-ROM | disk | FTP | other *** search
- /*
- * xvora.c
- *
- * Practical Algorithms for Image Analysis
- *
- * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
- */
-
- /*
- * XVOR(onoi)A(nalysis)
- *
- * routine to extract statistics of a given Voronoi diagram from
- * the representation stored in a file of type .vdt; a histogram
- * may be contructed from various quantities
- *
- * note: occasionally, very small V polygon edge lengths occur which
- * may not show on the display: currently, these are flagged in
- * site_gem() (vora.c), but are kept for statistics; this may have
- * to be dealt with more carefully;
- *
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <math.h>
- #include <string.h>
- #include <stdarg.h>
- #include "xvora.h"
-
- #define NX 511
- #define NY 511
- #define NXNY NX*NY /* total area */
-
- #define WHITE 255
-
- #define LINE_LEN 80 /* max length of line in data file */
- #define TITLE_LEN 30 /* max length of line title */
- #define FN_LEN 10 /* length of data file name */
-
- #define BIN_NUMBER 10 /* no bins in histogram */
-
- #define EDGE_NUMBER 0
- #define POLY_AREA 1
- #define NND 2
- #define EDGE_HIST "\nhistogram for polygon edges"
- #define AREA_HIST "\nhistogram for polygon areas"
- #define NND_HIST "\nhistogram for nn-distances"
-
- #define ND 20
-
-
- #define ON 1
- #define OFF 0
- #define DISPL_ALL ON /* when set, display all sites */
-
- /*
- * #define DISC_ONLY 1
- * #define HEXA_ONLY 2
- */
-
- #define FOUR 4
- #define FIVE 5 /* defect site coordination */
- #define SIX 6
- #define SEVEN 7
- #define EIGHT 8
-
- #define NSQ 2 /* limit distrib to NSQ*std_dev */
-
- #undef SHOW_HDATA
- #undef SHOW_HSORT
- #define SHOW_Q
- #define SHOW_P
-
- #undef FRMS_DEBUG
-
- /* global variables */
- extern char *optarg;
- extern int optind, opterr;
-
- int V_INP = 0; /* input file type flags */
- int D_INP = 0;
-
- int ACTIVE_AOI = 1; /* enforce ``active'' AOI bounds */
- /* the ``active'' AOI represents */
- /* a subset of the orig displ AOI */
-
-
- char *HIST_HEADER;
- int HIST_TYPE = -1;
- int N_BINS = BIN_NUMBER;
- int MIN_SET = 0;
- int MAX_SET = 0;
- int SHOW_INPUT = 0;
- int EVAL_KNN = 0;
- int SHOW_ALL = 1;
- int COMPRESS = 1; /* compr data set before eval histo */
-
- int LABEL_MODE = -1;
- int NBRS = 0;
- int CLRP = 1;
- int BORDER = 0;
- int MARK_AOI = 0;
-
- int P_STATS = 0;
- int Q_STATS = 0;
-
- int WRITE_FILE = 0;
-
- int SAVE_DATA = 0;
- int DISPL_SDATA = 0;
- int DISPL_PDATA = 0;
- int DUMP_VC = 1; /* dump poly vertex coords to file */
- int CPV = 1; /* if set, coll poly vertex coords */
-
- int KNN_MAX = 2; /* max nof NN shells to be constr */
-
-
-
- /*
- * error message
- */
- void
- exitmess (prompt, status)
- char *prompt;
- int status;
- {
- printf (prompt);
- printf ("\n");
-
- exit (status);
- }
-
-
- void
- fail_alloc (char *str, int code)
- {
- printf ("\n...memory alloc for %s failed\n", str);
- exit (code);
- }
-
-
-
- /*
- * gprintf() functions:
- * write to std output and, if option WRITE_FILE set, to file wbuf (stream)
- */
- void
- gprintf (FILE * fpOut, char *fmt,...)
- {
- va_list arg_ptr;
-
- va_start (arg_ptr, fmt);
- vprintf (fmt, arg_ptr);
- if (WRITE_FILE == 1)
- vfprintf (fpOut, fmt, arg_ptr);
- va_end (arg_ptr);
-
- }
- /*
- * usage of routine
- */
- void
- usage (char *progname)
- {
- progname = last_bs (progname);
- printf ("USAGE: %s infile outimg [-d] [-k S] [-p a] [-b] [-e] [-a] [-l]\n", progname);
- printf (" [-n nbins] [-i f] [-f f] [-s f] [-w f] [-L]\n");
- printf ("\n%s extracts statistics of a given Voronoi diagram from\n", progname);
- printf ("the representation stored in a file of type .vdt; a histogram\n");
- printf ("may be contructed from various quantities\n\n");
- printf ("ARGUMENTS:\n");
- printf (" infile: input filename (ASCII) produced by xvor;\n");
- printf (" .vdt (Voronoi data)\n");
- printf (" or .ddt (Delaunay data)\n");
- printf (" outimg: output image filename (TIF)\n\n");
- printf ("OPTIONS:\n");
- printf (" -d: show input data\n");
- printf (" -k S: construct k-NN shells, k<=S (default=%d)\n", KNN_MAX);
- printf (" -p a: enable labeling; a is a string, as follows:\n");
- printf (" a=nall: enable numbering of all sites\n");
- printf (" a=n57: enable numbering of 5-fold, 7-fold sites\n");
- printf (" a=n57a: ->in addition, mark active AOI\n");
- printf (" -b: no border strip\n\n");
- printf (" construct histogram for:\n");
- printf (" -e: edge number (per Voronoi polygon)\n");
- printf (" -a: polygon area\n");
- printf (" -l: nn-distance (edge bisector length)\n");
- printf (" -n n: set number of bins to n (default:=%d)\n", N_BINS);
- printf (" -i f: set initial value to f(float)\n");
- printf (" -f f: set final value to f(float)\n");
- printf (" -s: evaluate nn-distance statistics function Q = Q(t)\n\n");
- printf (" -w f: write data to file\n");
- printf (" f=h: hist data --> file of type .hdt\n");
- printf (" f=s: site topol data --> file of type .std\n");
- printf (" f=p: poly topol data --> file of type .ptd\n");
- printf (" -L: print Software License for this module\n");
- exit (1);
- }
-
-
- /*
- * sort sites on y
- */
- int
- sycomp (s1, s2)
- struct vSite *s1, *s2;
- {
- return ((int) (SIGN (s1->coord.y - s2->coord.y)));
- }
-
-
-
- /*
- * sort sites on x
- */
- int
- sxcomp (s1, s2)
- struct vSite *s1, *s2;
- {
- return ((int) (SIGN (s1->coord.x - s2->coord.x)));
- }
-
-
-
- /*
- * comparison function for qsort():
- * sort array of tuples in order of increassing x-values
- */
- int
- compare (t1, t2)
- const void *t1, *t2;
- {
- float f1, f2;
-
- f1 = *((float *) t1);
- f2 = *((float *) t2);
- return ((int) SIGN (f1 - f2));
- }
-
- /*
- * label site at (x, y) with numeral
- */
- void
- nbr_site (x, y, n, imgIO, value)
- double x, y;
- int n;
- Image *imgIO;
- int value;
- {
- static char *num[20] =
- {" 0", " 1", " 2", " 3", " 4",
- " 5", " 6", " 7", " 8", " 9",
- "10", "11", "12", "13", "14",
- "15", "16", "17", "18", "19"};
- int ix, iy;
-
- ix = (int) x - 16;
- iy = (int) y + 12;
- if (DISC_ONLY == ON) {
- if ((strcmp (*(num + n), " 5") == 0) ||
- (strcmp (*(num + n), " 7") == 0))
- gdImageString (gdFontSmall, (int) x, (int) y, *(num + n), imgIO, value);
- }
- else
- gdImageString (gdFontSmall, (int) x, (int) y, *(num + n), imgIO, value);
- }
-
-
- void
- main (int argc, char **argv)
- {
- int i, ich;
-
- int i_arg;
- static char whb[32];
- static char wsb[32];
- static char wpb[32];
-
- static char *inp_suffix =
- {".xxx"}; /* default inp file suffix */
- static char *vdt_type =
- {".vdt"}; /* suffix for .vdt inp file */
- static char *ddt_type =
- {".ddt"}; /* suffix for .ddt inp file */
- static char *whsuffix =
- {".hdt"}; /* suffix for output file name */
- static char *wssuffix =
- {".std"}; /* suffix for output file name */
- static char *wpsuffix =
- {".ptd"}; /* suffix for output file name */
- char *rbuf;
-
- char ln_buf[LINE_LEN];
-
- int ic, is, ie;
- int k;
- int inn, nnn_max;
- int ns, ne, nv;
- int nas, ncl = FOUR, ncu = EIGHT;
- int *nnna;
- double c_6, c_ic, mu_2;
- char rec;
-
- struct vSite *s; /* array of Voronoi sites */
- struct vSite *v; /* array of Voronoi vertices */
- struct vEdge *e; /* array of Voronoi edges */
- struct vPoly *p; /* array of Voronoi polygons */
- struct Tuple *pem; /* array of ptrs to Tuple: */
- /* midpoints of poly edges */
-
- unsigned int **a_n; /* ar of pntrs to area of n-fold coord bubs */
- unsigned int *a_nbar; /* mean area for bubs of coord no n */
- int nel = FOUR, neu = EIGHT;
- int ipn, *npna; /* array cont nof polygons with n edges */
- double ma_vp;
-
- int xmin, xmax, ymin, ymax;
-
- int ih, nh;
- float bw, min = (float) -1.0, max = (float) -1.0;
- float *hd, *ha;
- double del, mean, sdev;
- struct Histo histo, *h = &histo;
-
- float *foo;
- int np = 3;
-
- double r0;
- struct Tuple *pf, *qf, *dstats;
-
- /* list structs */
- struct kNNShell *sk; /* ary of pntrs to lists of kNN shells */
- struct linklist *sha; /* array of kNN shells (lists) */
- double frms;
- double *tctck;
-
- FILE *fpIn, *fpOut;
-
- Image *imgOut;
-
- /*
- * cmd line options:
- */
- static char *optstring = "dbp:ealn:i:f:sk:w:o:L";
-
-
- if (argc < 3)
- usage (argv[0]);
-
- /* get the filename for input */
- printf ("read data from file %s\n", rbuf = argv[1]);
- if ((fpIn = fopen (rbuf, "r")) == NULL) {
- printf ("\n...cannot open input file: %s\n", rbuf);
- exit (1);
- }
-
- /* construct output file name */
- ich = 0;
- while ((*(whb + ich) = *(wsb + ich) = *(wpb + ich) = *(rbuf + ich)) != '.')
- ich++;
- /* strip input file suffix */
- for (is = 0; is < 4; is++)
- *(inp_suffix + is) = *(rbuf + ich + is);
-
- if (strcmp (inp_suffix, vdt_type) == 0)
- V_INP = 1;
- else if (strcmp (inp_suffix, ddt_type) == 0)
- D_INP = 1;
- else {
- printf ("\n...unknown input file type encountered:");
- printf (" legal types: %s %s\n", vdt_type, ddt_type);
- fclose (fpIn);
- exit (1);
- }
-
- /* append suffix to output file names */
- for (is = 0; is < 4; is++) {
- *(whb + ich + is) = *(whsuffix + is);
- *(wsb + ich + is) = *(wssuffix + is);
- *(wpb + ich + is) = *(wpsuffix + is);
- }
-
- /*
- * parse command line
- */
- optind = 3;
- opterr = ON; /* give error messages */
-
-
- while ((i_arg = getopt (argc, argv, optstring)) != EOF) {
- switch (i_arg) {
- case 'd':
- printf ("...option %c: ", i_arg);
- printf ("display input data\n");
- SHOW_INPUT = ON;
- break;
- case 'k':
- printf ("...option %c: ", i_arg);
- printf ("construct %d-NN shells for all Sites\n", KNN_MAX = atoi (optarg));
- EVAL_KNN = ON;
- break;
- case 'p':
- printf ("...option %c: ", i_arg);
- if (strcmp (optarg, "nall") == 0) {
- printf ("number all V. sites\n");
- NBRS = ON;
- LABEL_MODE = DISC_ONLY;
- MARK_AOI = OFF;
- }
- else if (strcmp (optarg, "n57") == 0) {
- printf ("number V. disclination sites\n");
- NBRS = ON;
- LABEL_MODE = DISC_ONLY;
- MARK_AOI = OFF;
- }
- else if (strcmp (optarg, "n57a") == 0) {
- printf ("number V. disclination sites\n");
- NBRS = ON;
- LABEL_MODE = DISC_ONLY;
- MARK_AOI = ON;
- }
- else if (strcmp (optarg, "c57") == 0) {
- printf ("color V. penta- and heptagons\n");
- CLRP = ON;
- LABEL_MODE = DISC_ONLY;
- BORDER = ON;
- MARK_AOI = OFF;
- }
- else if (strcmp (optarg, "c57a") == 0) {
- printf ("color V. penta- and heptagons\n");
- CLRP = ON;
- LABEL_MODE = DISC_ONLY;
- BORDER = ON;
- MARK_AOI = ON;
- }
- else
- exitmess ("\n...unknown p option", 1);
- break;
- case 'b':
- printf ("...option %c: ", i_arg);
- printf ("no zero border strip\n");
- if (BORDER == ON)
- BORDER = OFF;
- break;
- case 'e':
- printf ("...option %c: ", i_arg);
- printf ("construct histogram for poly edges\n");
- HIST_TYPE = EDGE_NUMBER;
- HIST_HEADER = EDGE_HIST;
- break;
- case 'a':
- printf ("...option %c: ", i_arg);
- printf ("construct histogram for polygon area\n");
- HIST_TYPE = POLY_AREA;
- HIST_HEADER = AREA_HIST;
- break;
- case 'l':
- printf ("...option %c: ", i_arg);
- printf ("construct histogram for vertex nn-dist\n");
- HIST_TYPE = NND;
- HIST_HEADER = NND_HIST;
- break;
- case 'n':
- printf ("...option %c: ", i_arg);
- printf ("set number of bins to %d\n",
- N_BINS = atoi (optarg));
- break;
- case 'i':
- printf ("...option %c: ", i_arg);
- printf ("set min (init value) to %f\n",
- min = (float) atof (optarg));
- MIN_SET = 1;
- break;
- case 'f':
- printf ("...option %c: ", i_arg);
- printf ("set max (final value) to %f\n",
- max = (float) atof (optarg));
- MAX_SET = 1;
- break;
- case 's':
- printf ("...option %c: ", i_arg);
- printf ("distance statistics for q(t)\n");
- Q_STATS = 1;
- break;
- case 'w':
- printf ("...option %c: ", i_arg);
- if (strcmp (optarg, "h") == 0) {
- if ((fpOut = fopen (whb, "w")) == NULL) {
- printf ("\n...cannot open %s for writing\n",
- whb);
- exit (1);
- }
- printf ("write hist data to file %s\n", whb);
- WRITE_FILE = 1;
- }
- else if (strcmp (optarg, "s") == 0) {
- if ((fpOut = fopen (wsb, "w")) == NULL) {
- printf ("\n...cannot open %s for writing\n",
- wsb);
- exit (1);
- }
- printf ("log site topol data in file: %s\n", wsb);
- DISPL_SDATA = 1;
- WRITE_FILE = 1;
- }
- else if (strcmp (optarg, "p") == 0) {
- if ((fpOut = fopen (wpb, "w")) == NULL) {
- printf ("\n...cannot open %s for writing\n",
- wpb);
- exit (1);
- }
- printf ("log polygon topol data in file: %s\n", wpb);
- DISPL_PDATA = 1;
- WRITE_FILE = 1;
- }
- else
- exitmess ("\n...unknown -w option", 1);
-
- break;
- case 'L':
- print_sos_lic ();
- exit (0);
- default:
- printf ("\n...unknown cmd line argument\n");
- exit (1);
- break;
- }
- }
-
- if ((Q_STATS == 1) && (HIST_TYPE != NND)) {
- printf ("\n...before evaluating q-function:\n");
- printf ("-->evaluate NND histogram (opt -l) with %d bins (opt -nND)\n", ND + 1);
- exit (1);
- }
- if (D_INP == 1) {
- printf ("\n...Delaunay input (.ddt) currently not handled\n");
- exit (1);
- }
-
- /*
- * first pass through .vdt file:
- * -->determine number of sites (identical to the number of Voronoi polygons)
- * -->determine number of edges
- */
- ns = ne = nv = 0;
- do {
- while ((fgets (ln_buf, LINE_LEN, fpIn)) != (char *) NULL) {
- sscanf (ln_buf, "%c", &rec);
- switch (rec) {
- case 's':
- ns++;
- break;
- case 'v':
- nv++;
- break;
- case 'e':
- ne++;
- break;
- default:
- break;
- }
- }
- } while (feof (fpIn) == 0);
- fclose (fpIn);
- printf ("\n...first pass through data found:\n");
- printf (" %d sites, %d vertices, %d edges\n", ns, nv, ne);
-
- /*
- * allocate memory for input data
- */
- if ((s = (struct vSite *) calloc (ns, sizeof (struct vSite))) == NULL)
- fail_alloc ("struct vSite *s", 1);
- if ((v = (struct vSite *) calloc (nv, sizeof (struct vSite))) == NULL)
- fail_alloc ("struct vSite *v", 1);
- if ((e = (struct vEdge *) calloc (ne, sizeof (struct vEdge))) == NULL)
- fail_alloc ("struct vEdge *e", 1);
- if ((p = (struct vPoly *) calloc (ns, sizeof (struct vPoly))) == NULL)
- fail_alloc ("struct vPoly *p", 1);
-
- /*
- * scan input file
- */
- if ((fpIn = fopen (rbuf, "r")) == NULL)
- exitmess ("can't open input file", 1);
-
- printf ("\n...read and classify input data\n");
- ns = nv = ne = 0;
- do {
- while ((fgets (ln_buf, LINE_LEN, fpIn)) != (char *) NULL) {
- sscanf (ln_buf, "%c", &rec);
- switch (rec) {
- case 's':
- sscanf (ln_buf, "%c %f %f",
- &rec, &((s + ns)->coord.x), &((s + ns)->coord.y));
- (s + ns)->sitenbr = (p + ns)->sitenbr = ns;
- ns++;
- break;
- case 'v':
- sscanf (ln_buf, "%c %f %f",
- &rec, &((v + nv)->coord.x), &((v + nv)->coord.y));
- (v + nv)->sitenbr = nv;
- nv++;
- break;
- case 'e':
- sscanf (ln_buf, "%c %d %d %d %d %d",
- &rec, &((e + ne)->edgenbr),
- &((e + ne)->vO), &((e + ne)->vF),
- &((e + ne)->sO), &((e + ne)->sF));
- ne++;
- break;
- default:
- break;
- }
- }
- } while (feof (fpIn) == 0);
- fclose (fpIn);
-
- if (SHOW_INPUT == ON) {
- printf ("\n...second pass through data:\n");
-
- printf ("...sites:\n");
- for (i = 0; i < ns; i++)
- printf (" %3d: x: %f y: %f\n",
- (s + i)->sitenbr, (s + i)->coord.x, (s + i)->coord.y);
-
- printf ("...vertices:\n");
- for (i = 0; i < nv; i++)
- printf (" %3d: x: %f y: %f\n",
- (v + i)->sitenbr, (v + i)->coord.x, (v + i)->coord.y);
-
- printf ("...edges:\n");
- for (i = 0; i < ne; i++)
- printf (" e %3d: vO: %3d vF: %3d sO: %3d sF: %3d\n",
- (e + i)->edgenbr, (e + i)->vO, (e + i)->vF, (e + i)->sO, (e + i)->sF);
- }
-
-
- /*
- * check whether Site falls outside of the ``active'' AOI
- * as defined in vora.h: if out of bounds, set aoiFlag Active;
- */
- mark_eSites (s, ns);
-
- /*
- * enforce ``active'' AOI bounds: mark all edges which cross
- * AOI boundaries and flag corresponding sites which lie out-of-bounds
- */
- if (ACTIVE_AOI == 1)
- mark_eEdges (v, e, ne);
-
- /*
- * determine site coordination,
- * given by the number of edges of the associated Voronoi polygon
- *
- * if any of the polygon edges associated with a given Site
- * end out-of-bounds, mark that Site by setting eFlag: this
- * flag indicates enforcement of stronger condition than that
- * involved in setting aoiFlag (in mark_eSite())
- *
- * (the offending vertex should already have been marked,
- * by setting aoiFlag, in mark_eEdges())
- */
-
- /*
- * initialize structure member
- */
- for (is = 0; is < ns; is++) {
- (s + is)->nnn = 0;
- (s + is)->eFlag = UnSet;
- }
- site_coordination (s, v, e, ns, ne);
-
-
- /*
- * for each site, of known coord. number:
- * -->determine the set of nearest neighbor distances;
- * -->construct Voronoi polygons associated with given site by grouping edges;
- *
- * flag ``edge'' polygons, by checking the status of
- *
- * set eFlag if the polygon does not lie entirely within the ``active'' AOI;
- * that is, enforce the criterion, more stringent than the one employed in
- * function mark_eSites(), that all polygon edges associated with Sites
- * considered to be part of the ``active'' AOI must lie entirely within that
- * (new) AOI;
- *
- * in addition, if any polygon edge length is smaller than a preset limit
- * (see EPS in vora.c), set lFlag;
- */
- for (i = 0; i < ns; i++) {
- if (((s + i)->nnd = (float *) calloc ((s + i)->nnn, sizeof (float))) == NULL)
- fail_alloc ("(s+i)->nnd", 1);
-
- if (((s + i)->nnsi = (int *) calloc ((s + i)->nnn, sizeof (int))) == NULL)
- fail_alloc ("(s+i)->nnsi", 1);
- }
-
- for (i = 0; i < ns; i++) {
- if (((p + i)->pei = (int *) calloc ((s + i)->nnn, sizeof (int))) == NULL)
- fail_alloc ("(p+i)->pei", 1);
-
- if (((p + i)->vel = (float *) calloc ((s + i)->nnn, sizeof (float))) == NULL)
- fail_alloc ("(s+i)->nnd", 1);
-
- if (((p + i)->sphi = (float *) calloc ((s + i)->nnn, sizeof (float))) == NULL)
- fail_alloc ("(p+i)->sphi", 1);
-
- if (CPV == 1) {
- if (((p + i)->pvi = (int *) calloc ((s + i)->nnn, sizeof (int))) == NULL)
- fail_alloc ("(p+i)->pvi", 1);
- }
- }
- printf ("...evaluate properties of sites\n");
- nnn_max = site_geom (s, v, e, p, ns, ne);
-
- /*
- * allocate memory for auxiliary array of edge midpoint coordinates,
- * employed in sorting of polygon edges
- */
- if ((pem = (struct Tuple *) calloc (nnn_max, sizeof (struct Tuple))) == NULL)
- fail_alloc ("struct Tuple *pem", 1);
-
- /*
- * during construction of the V. diagram (xvor.c) edge endpoints
- * which lie out-of-bounds (with respect to the full display AOI) are
- * flagged by setting the corresponding vertex array index (vO or vF)
- * to -1; that is, in the array e, indices (e+is)->vO, (e+is)->vF) of
- * vertices delimiting edges are set to -1 if corresponding coordinates
- * are out-of-bounds with respect to the original AOI
- */
- printf ("...evaluate properties of polygons\n");
- poly_geom (s, v, e, p, pem, ns, ne, CPV);
-
- if (DISPL_PDATA == 1) {
- log_pt_data (fpOut, wpb, ns, p, e, v, CPV);
- if (DUMP_VC == 1) {
- printf ("\n...dump polygon vertices to file\n");
- dump_poly_vc (fpOut, ns, p, v);
- }
- }
- else {
- printf ("\n...to display and log polygon data: option -wp\n");
- }
-
-
-
- /*
- * examine nof NNs as a function of V polygon domain area (->Lewis' law)
- * (see also: xsgt.c)
- */
- npna = pe_dist (p, ns, nel, neu);
- gprintf (fpOut, "\n...distribution of V poly edges:\n");
- for (ie = 0; ie <= nnn_max; ie++) {
- gprintf (fpOut, "\t%2d-sided V polygons : %3d\n", ie, *(npna + ie));
- }
-
- if ((a_n = (unsigned int **) calloc (nnn_max + 1, sizeof (unsigned int *))) == NULL)
- fail_alloc ("a_n", 1);
-
- for (inn = 0; inn <= nnn_max; inn++) {
- if ((a_n[inn] = (unsigned int *) calloc (npna[inn] + 1, sizeof (unsigned int))) == NULL)
- fail_alloc ("a_n[inn]", 1);
-
- }
-
- if ((a_nbar = (unsigned int *) calloc (nnn_max + 1, sizeof (unsigned int))) == NULL)
- fail_alloc ("a_nbar", 1);
-
- for (ie = 0; ie <= nnn_max; ie++)
- *(npna + ie) = 0;
- ma_vp = p_of_nVA (npna, a_n, ns, p);
- //for(inn=0; inn<nnn_max+10; inn++)
- // free(a_n[inn]);
- //free(a_n);
- //exit(0);
-
- gprintf (fpOut, "\nmean area of (active) Vor poly: %lf\n", ma_vp);
- gprintf (fpOut, "V poly grouped by edge no n=2(1)%d:\n", nnn_max);
- for (inn = 2; inn <= nnn_max; inn++) {
- for (ipn = 0; ipn < npna[inn]; ipn++) {
- if (SHOW_ALL == 1) {
- gprintf (fpOut, "...%5u ", *(a_n[inn] + ipn));
- if ((ipn + 1) % 8 == 0)
- gprintf (fpOut, "\n");
- }
- *(a_nbar + inn) += *(a_n[inn] + ipn);
- }
- if (ipn > 0)
- *(a_nbar + inn) /= ipn;
-
- gprintf (fpOut, "\n nof NN: %2d -- <A_n> (mean of %d): %6u ",
- inn, ipn, *(a_nbar + inn));
- gprintf (fpOut, " -- <A_n>/<A>: %lf\n\n",
- (double) (*(a_nbar + inn)) / ma_vp);
- }
-
-
-
- /* copy vPoly area into vSite array (to ease handling of output) */
- for (is = 0; is < ns; is++)
- (s + is)->v_area = (unsigned int) (p + is)->area;
-
- if (DISPL_SDATA == 1)
- log_st_data (fpOut, wsb, ns, s);
-
- else {
- printf ("\n...to display and log site data: option -ws\n");
- }
-
- /*
- * determine the number of topological defects:
- * this is given by the number of sites not coordinated with 6 NN;
- * restrict count to an ``active'' AOI excluding the ``edge'' of
- * the display AOI;
- */
- printf (" defect sites in active AOI (%3d, %3d), (%3d, %3d)\n",
- AULX, AULY, ALRX, ALRY);
-
- nnna = count_defects (s, ns, ncl, ncu);
- nas = 0;
- for (ic = ncl; ic <= ncu; ic++)
- nas += *(nnna + ic);
-
- gprintf (fpOut, "\n...distribution of NN:\n");
- mu_2 = 0.0;
- for (ic = ncl; ic <= ncu; ic++) {
- c_ic = (double) (*(nnna + ic)) / (double) nas;
- mu_2 += (ic - 6) * (ic - 6) * c_ic;
-
- gprintf (fpOut, "\t%2d-fold sites: %3d -- f_%2d: %lf\n",
- ic, *(nnna + ic), ic, c_ic);
- }
- c_6 = (double) (*(nnna + SIX)) / (double) nas;
- gprintf (fpOut, "...conc of 6-fold sites: ");
- gprintf (fpOut, "\tc_6 = (n_6 = %d)/%d = %lf\n", *(nnna + SIX), nas, c_6);
- gprintf (fpOut, "...second moment of NN distribution: %lf\n", mu_2);
- gprintf (fpOut, "\n");
-
-
- /*
- * kNN shells and fractal measure
- */
- if (EVAL_KNN == 1) {
- if ((sk = (struct kNNShell *) calloc (ns, sizeof (struct kNNShell))) == NULL)
- fail_alloc ("sk", 1);
-
-
- for (is = 0; is < ns; is++) {
- if (((sk + is)->aka = (double *) calloc (KNN_MAX, sizeof (double))) == NULL)
- fail_alloc ("sk->aka", 1);
- }
- for (is = 0; is < ns; is++) {
- if (((sk + is)->tctc = (double *) calloc (KNN_MAX, sizeof (double))) == NULL)
- fail_alloc ("sk->tctc", 1);
- }
-
-
- if ((sha = (struct linklist *) calloc (KNN_MAX, sizeof (struct linklist))) == NULL)
- fail_alloc ("sha", 1);
-
-
- gprintf (fpOut, "...constr kNN shells up to k=%d\n", KNN_MAX);
- frms = construct_kNNs (ns, s, sha, sk);
- gprintf (fpOut, "...avrg fractal meas of set: %lf\n", frms);
-
-
- /*
- * display arrays sk->tctc, then average values over sites
- */
- if ((tctck = (double *) calloc (KNN_MAX + 1, sizeof (double))) == NULL)
- fail_alloc ("tctck", 1);
-
-
- gprintf (fpOut, "\n...charge-charge correlations (active Sites only)\n");
-
- nas = 0;
- for (k = 0; k < KNN_MAX; k++)
- *(tctck + k) = 0.0;
- for (is = 0; is < ns; is++) {
- gprintf (fpOut, "...Site %3d ", (sk + is)->sitenbr);
- gprintf (fpOut, "charge: %d ", (sk + is)->tc);
-
- if (((s + is)->eFlag == UnSet) &&
- ((s + is)->aoiFlag == UnSet)) {
- nas++;
- for (k = 0; k < (sk + is)->kNN; k++) {
- gprintf (fpOut, "...%6.2lf ",
- (sk + is)->tctc[k]);
-
- *(tctck + k) += (sk + is)->tctc[k];
- }
- if ((k + 1) % 8 == 0)
- gprintf (fpOut, "\n");
- }
- gprintf (fpOut, "\n");
- }
- gprintf (fpOut, "\n...charge-charge correlation function:\n");
- gprintf (fpOut, "\taveraged over %d active Sites\n", nas);
- for (k = 0; k < KNN_MAX; k++) {
- gprintf (fpOut, "...%6.2lf", *(tctck + k) / (double) nas);
-
- if ((k + 1) % 8 == 0)
- gprintf (fpOut, "\n");
- }
- gprintf (fpOut, "\n");
-
- #ifdef FRMS_DEBUG
- KNN_MAX = 0;
- for (is = 0; is < ns; is++) {
- printf ("Site %3d: ", (s + is)->sitenbr);
- if (((s + is)->eFlag == UnSet) &&
- ((s + is)->aoiFlag == UnSet)) {
- printf ("kNN: %3d, frms: %6.3lf\n",
- (sk + is)->kNN, (sk + is)->frms);
-
- if ((sk + is)->kNN > KNN_MAX)
- KNN_MAX = (sk + is)->kNN;
- }
- else { /* Site flagged out-of-bounds */
- if ((sk + is)->kNN == 0)
- printf (" out of bounds: -->ignored\n");
- else
- printf (" something wrong\n");
- }
- printf ("\tactual KNN_MAX: %3d\n", KNN_MAX);
- }
- #endif
- }
-
-
-
- /*
- * histogram evaluation
- */
- if (HIST_TYPE != -1) {
- printf ("\n...initiate histogram evaluation\n");
- switch (HIST_TYPE) {
- case EDGE_NUMBER: /* site coordination */
- if ((hd = (float *) calloc (ns, sizeof (float))) == NULL)
- fail_alloc ("hd", 1);
- nh = 0;
- for (is = 0; is < ns; is++) {
- if (((s + is)->eFlag == UnSet) &&
- ((s + is)->aoiFlag == UnSet)) {
- *(hd + is) = (float) (s + is)->nnn;
- nh++;
- }
- }
- if ((hd = (float *) realloc (hd, nh * sizeof (float))) == NULL)
- fail_alloc ("hd (realloc)", 1);
- COMPRESS == 0;
- break;
- case POLY_AREA:
- nh = ns;
- if ((hd = (float *) calloc (nh, sizeof (float))) == NULL)
- fail_alloc ("hd", 1);
- for (i = 0; i < nh; i++) {
- if ((*(hd + i) = (float) (p + i)->area) == -1.0)
- *(hd + i) = (float) 0.0;
- }
- break;
- case NND: /* note: each nn bond counted twice !! */
- nh = 0;
- for (is = 0; is < ns; is++)
- nh += (s + is)->nnn;
- if ((hd = (float *) calloc (nh, sizeof (float))) == NULL)
- fail_alloc ("hd", 1);
- i = 0;
- for (is = 0; is < ns; is++) {
- for (ie = 0; ie < (s + is)->nnn; ie++) {
- *(hd + i) = (s + is)->nnd[ie];
- i++;
- }
- }
- break;
- }
-
- #ifdef SHOW_HDATA
- printf ("\n...data to construct %s:\n", HIST_HEADER);
- for (i = 0; i < nh; i++)
- printf (" data[%d] = %f\n", i, *(hd + i));
- printf ("\n");
- #endif
-
- /*
- * evaluate statistics
- */
- mean = find_mean (hd, nh);
- sdev = find_sigma (hd, nh, mean);
- printf ("...(raw) mean: %lf, std dev: %lf\n", mean, sdev);
-
- /*
- * limit values to be included in histogram to those within a spread of
- * NSQ*sdev of (raw) mean to eliminate artefacts
- */
- if (COMPRESS == 1) {
- ih = 0;
- for (i = 0; i < nh; i++) {
- del = (double) (*(hd + i)) - mean;
- if (fabs (del) <= NSQ * sdev) {
- *(hd + ih) = *(hd + i);
- ih++;
- }
- }
- nh = ih;
- printf ("...%d entries within %d*std dev of mean:\n", nh, NSQ);
-
- /*
- * realloc, to reduce memory requirement
- */
- hd = (float *) realloc (hd, nh * sizeof (float));
- }
-
- /*
- * prepare to evaluate histogram
- */
- qsort (hd, nh, sizeof (float), compare);
-
- #ifdef SHOW_HSORT
- printf ("\n...sorted data:\n");
- for (i = 0; i < nh; i++)
- printf (" data[%d] = %f\n", i, *(hd + i));
- #endif
-
- if (MIN_SET == 0)
- min = *(hd + 0);
- if (MAX_SET == 0)
- max = *(hd + nh - 1);
- if ((-1.0e-10 < (min - max)) && ((min - max) < 1.0e-10))
- min = max;
-
- if (HIST_TYPE == EDGE_NUMBER) { /* discrete variable */
- min = (float) 1.0;
- max = *(hd + nh - 1);
- N_BINS = (int) (max - min) + 1;
- }
- bw = (max - min) / ((float) (N_BINS - 1));
-
- if ((ha = (float *) calloc ((size_t) N_BINS, sizeof (float))) == NULL)
- fail_alloc ("hist\n", 1);
-
- if (bw != 0.0)
- construct_shist (nh, hd, ha, bw, min);
-
-
- printf ("%s\n", HIST_HEADER);
- printf ("......mean: %lf\n", mean);
- printf ("...std_dev: %lf\n", sdev);
- printf ("...min: %f, bw: %f, max: %f\n", min, bw, max);
- for (i = 0; i < N_BINS; i++) {
- printf (" %6.2f", *(ha + i));
- if ((i + 1) % 10 == 0)
- printf ("\n");
- }
- }
-
- #if defined(LINUX)
- qsort (s, (size_t) ns, sizeof (struct vSite), (__compar_fn_t) sycomp);
- #else
- qsort (s, (size_t) ns, sizeof (struct vSite), sycomp);
- #endif
- ymin = (int) (s + 0)->coord.y;
- ymax = (int) (s + ns - 1)->coord.y;
-
- #if defined(LINUX)
- qsort (s, ns, sizeof (struct vSite), (__compar_fn_t) sxcomp);
- #else
- qsort (s, ns, sizeof (struct vSite), sxcomp);
- #endif
- xmin = (int) (s + 0)->coord.x;
- xmax = (int) (s + ns - 1)->coord.x;
-
- printf ("...min: (%3d, %3d), max: (%3d, %3d)\n",
- xmin, ymin, xmax, ymax);
- /* allocate default image buffer for image output */
- imgOut = ImageAlloc ((long) ymax, (long) xmax, BPS);
-
- /*
- * label Voronoi diagram
- */
- if (NBRS == ON) {
- if (MARK_AOI == ON)
- draw_rect (AULX, AULY, ALRX, ALRY, imgOut, WHITE);
- for (is = 0; is < ns; is++) {
- if (DISPL_ALL == ON)
- nbr_site ((s + is)->coord.x, (s + is)->coord.y, (s + is)->nnn, imgOut, WHITE);
- else {
- if ((s + is)->eFlag != -1)
- nbr_site ((s + is)->coord.x, (s + is)->coord.y, (s + is)->nnn, imgOut, WHITE);
- }
- }
- }
- else if (CLRP == ON) {
- if (MARK_AOI == ON)
- draw_rect (AULX, AULY, ALRX, ALRY, imgOut, WHITE);
- for (is = 0; is < ns; is++) {
- if (DISPL_ALL == ON)
- //fill_poly( (s+is)->coord.x, (s+is)->coord.y, (s+is)->nnn, LABEL_MODE );
- fill_Voronoi ((s + is)->coord.x, (s + is)->coord.y, (s + is)->nnn, imgOut, LABEL_MODE);
- else {
- if ((s + is)->eFlag != -1);
- //fill_poly( (s+is)->coord.x, (s+is)->coord.y, (s+is)->nnn, LABEL_MODE );
- fill_Voronoi ((s + is)->coord.x, (s + is)->coord.y, (s + is)->nnn, imgOut, LABEL_MODE);
- }
- }
-
- /*
- * draw border in active frame buffer
- */
- if (BORDER == ON) {
- printf ("\n...generate border...\n");
-
- draw_border (xmin, ymin, xmax, ymax, imgOut, WHITE);
- }
- }
- else {
- if (MARK_AOI == ON)
- draw_rect (AULX, AULY, ALRX, ALRY, imgOut, WHITE);
-
- }
-
-
-
- /*
- * distance statistics:
- * --> single-sites: p-function
- * -->nn site-pairs: q-function
- */
-
- /*
- * p-function: evaluate ``numerically'', employing graphics fill/flood fcts
- */
- if (P_STATS == ON) {
- printf ("\n...distance statistics: -->p-function\n");
-
- if ((pf = (struct Tuple *) calloc (ND + 1, sizeof (struct Tuple))) == NULL)
- fail_alloc ("pf", 1);
-
- r0 = sqrt ((float) NXNY / ((float) ns));
- eval_p (s, ns, pf, ND, (double) r0, (double) NXNY);
-
- #ifdef SHOW_P
- printf ("\n...single-site distance statistics: p-function\n");
- for (i = 0; i <= ND; i++) {
- printf (" %6.2f", (pf + i)->y);
- if ((i + 1) % 10 == 0)
- printf ("\n");
- }
- #endif
- }
-
- /*
- * q-function: evaluate by computing partial sums of nnd histogram
- */
- if (Q_STATS == ON) {
- printf ("\n...distance statistics: -->q-function\n");
-
- if ((qf = (struct Tuple *) calloc (ND + 1, sizeof (struct Tuple))) == NULL)
- fail_alloc ("qf", 1);
-
- r0 = sqrt ((float) NXNY / ((float) ns));
- eval_q (ha, qf, ND, (double) r0);
-
- #ifdef SHOW_Q
- printf ("\n...nn distance statistics: q-function\n");
- for (i = 0; i <= ND; i++) {
- printf (" %6.2f", (qf + i)->y);
- if ((i + 1) % 10 == 0)
- printf ("\n");
- }
- #endif
- }
-
-
- /*
- * write output file of type .hdt (see also: .adt, .pdt)
- */
- if (WRITE_FILE == 1) {
-
- if ((foo = (float *) calloc (np, sizeof (float))) == NULL)
- fail_alloc ("foo", 1);
-
- for (i = 0; i < np; i++)
- *(foo + i) = (float) 1.0;
-
- if (HIST_TYPE != -1) {
- h->nh = nh;
- h->nb = N_BINS;
- h->bw = bw;
- h->amin = min;
- h->amax = max;
- h->mean = mean;
- h->sdev = sdev;
- h->phd = &hd[0];
- h->ph = &ha[0];
-
- if (P_STATS == ON)
- dstats = pf;
- if (Q_STATS == ON)
- dstats = qf;
- else
- dstats = (struct Tuple *) NULL;
-
- write_hdt_data (fpOut, np, foo, h, dstats, ND);
- }
- fclose (fpOut);
- }
-
- printf ("\n...writing output file %s\n", argv[2]);
- ImageOut (argv[2], imgOut);
-
- if (WRITE_FILE == 1)
- free (foo);
- if (HIST_TYPE != -1) {
- free (hd);
- free (ha);
- }
- free (nnna); /* alloc in vora.c */
- for (i = 0; i < ns; i++) {
- free ((s + i)->nnd);
- free ((s + i)->nnsi);
- }
- free (s);
- free (v);
- free (e);
- for (i = 0; i < ns; i++) {
- free ((p + i)->pei);
- free ((p + i)->vel);
- free ((p + i)->sphi);
- if (CPV == 1)
- free ((p + i)->pvi);
- }
- free (p);
- for (inn = 0; inn < nnn_max; inn++)
- free (a_n[inn]);
- free (a_n);
- free (a_nbar);
- free (npna);
-
- if (EVAL_KNN == 1) {
- for (is = 0; is <= ns; is++)
- free ((sk + is)->aka);
- for (is = 0; is <= ns; is++)
- free ((sk + is)->tctc);
- free (sk);
- free (sha);
- free (tctck);
- }
- free (pem);
- if (P_STATS == ON)
- free (pf);
- if (Q_STATS == ON)
- free (qf);
- }
-