/* * rdm_util.c - Reading utilities for reading mtv-format data */ #include #include #include #include #include #include "CNplot.h" static void reset_maxmin(); /* * Check to see if the number of points has been defined */ int CNmtv_check_points(rnpts,npts,type,format,lineno) CNdouble *rnpts; int *npts; char *type; char *format; int lineno; { int error = CN_FALSE; /* Set the number of grid points */ *npts = (int) rnpts->val; if (!rnpts->def) { (void) fprintf(stderr," warning (line %3d) %8s: ",lineno,format); (void) fprintf(stderr, "\"%s\" has not been defined!\n",type); error = CN_TRUE; } else if ((*npts) <= 0) { (void) fprintf(stderr," warning (line %3d) %8s: ",lineno,format); (void) fprintf(stderr, "No of array points is less than or equal to 0 : "); (void) fprintf(stderr,"%s=%d\n",type,npts); error = CN_TRUE; } /* Return */ return(error); } /* * Read an int array in binary format */ int *CNmtv_read_binary_int_array(fp,npts) FILE *fp; int npts; { int *xarr=NULL; /* Error checking */ if (fp==NULL) { (void) fprintf(stderr,"Error - Cannot read from NULL file/pipe!\n"); return(NULL); } if (npts<=0) { (void) fprintf(stderr,"Error - Invalid size (npts=%d)\n",npts); return(NULL); } /* allocate space for the data in a 1D array */ xarr = CNcreate_1D_int_array(npts); /* * read the intensity values and save in a 1D array */ if (fread((char *)xarr,sizeof(int),npts,fp) != npts) { (void) fprintf(stderr," ***Binary read error of data array!\n"); CNfree_1D_int_array(xarr); return(NULL); } /* * Return the array pointer - it is up to the calling routine to * free this array */ return(xarr); } /* * Read an double array in binary format */ double *CNmtv_read_binary_dbl_array(fp,npts) FILE *fp; int npts; { double *xarr=NULL; /* Error checking */ if (fp==NULL) { (void) fprintf(stderr,"Error - Cannot read from NULL file/pipe!\n"); return(NULL); } if (npts<=0) { (void) fprintf(stderr,"Error - Invalid size (npts=%d)\n",npts); return(NULL); } /* allocate space for the data in a 1D array */ xarr = CNcreate_1D_double_array(npts); /* * read the intensity values and save in a 1D array */ if (fread((char *)xarr,sizeof(double),npts,fp) != npts) { (void) fprintf(stderr," ***Binary read error of data array!\n"); CNfree_1D_double_array(xarr); return(NULL); } /* * Return the array pointer - it is up to the calling routine to * free this array */ return(xarr); } /* * Read a line and get the numbers off the line */ void CNmtv_read_line(line,arrptr,lim,npts) char *line; double *arrptr; int lim, *npts; { char *word[CN_MAXWORDS]; int nw=0, i; double z; /* Parse line into lots of words */ if ((nw = CNgetwords(line,word, CN_MAXWORDS)) >= 0) { /* Go thru each word */ for (i=0; i (*nvals)) ? (*nvals) : needed; /* Do the actual transfer */ for (i=0; i0 */ static void reset_maxmin(xmin, xmax, dy) double *xmin, *xmax, dy; { if (fabs((*xmax)/dy) > 1.0e5) { /* Vastly different order of magnitude */ *xmax = *xmax * 10.0; *xmin = *xmin * 0.10; } else { /* Same order of magnitude */ *xmax = *xmax + 0.5*dy; *xmin = *xmin - 0.5*dy; } } /* * Triangulate a polygon */ void CNtriangulate_polygon(opointhead, opointtail, pointhead, pointtail, nodehead, nodetail, triahead, triatail, trID) CNpointptr opointhead, opointtail; CNpointptr *pointhead, *pointtail; CNnodeptr *nodehead, *nodetail; CNtriaptr *triahead, *triatail; int *trID; { int idtang(); double *xarr, *yarr; double xmin, xmax, ymin, ymax, ratio; CNnodeptr *narr; CNpointptr P, pt; int npoints; int *ipt, *ipl, nt, nl, i; int p1, p2, p3; /* Check the no of points */ npoints = CNcount_points(opointhead, opointtail); if (npoints < 4) { (void) fprintf(stderr, "Error - triangulation requires 4 or more points!\n"); return; } /* Allocate arrays based on no of points in point list */ xarr = (double *)malloc((unsigned int)(npoints*sizeof(double))); yarr = (double *)malloc((unsigned int)(npoints*sizeof(double))); narr = (CNnodeptr *)malloc((unsigned int)(npoints*sizeof(CNnodeptr))); if (!xarr || !yarr || !narr) { (void) fprintf(stderr,"Error - insufficent space for work arrays!\n"); if (xarr) free((char *)xarr); if (yarr) free((char *)yarr); if (narr) free((char *)narr); return; } /* Fill the arrays */ npoints = 0; for (P=opointhead; P!=NULL; P=P->next) { if (P==opointhead) { xmin = P->x; xmax = P->x; ymin = P->y; ymax = P->y; } if (P->x < xmin) xmin = P->x; if (P->x > xmax) xmax = P->x; if (P->y < ymin) ymin = P->y; if (P->y > ymax) ymax = P->y; xarr[npoints] = P->x; yarr[npoints] = P->y; npoints++; } /* Triangulation fails if the x-bounds are greater than y-bounds * by several orders of magnitude (or vice versa). Scale the * x,y arrays arrordingly */ if ((xmin == xmax) || (ymin == ymax)) { (void) fprintf(stderr,"***Error! All collinear points!\n"); (void) fprintf(stderr,"***Error! Triangulation failed!\n"); if (xarr) free((char *)xarr); if (yarr) free((char *)yarr); if (narr) free((char *)narr); return; } ratio = fabs((xmax-xmin)/(ymax-ymin)); if ((ratio > 1.0e3) || (ratio < 1.0e-3)) { for (i=0; inext) { pt = CNinsert_point(pointhead,pointtail,P->x,P->y,P->z,P->ID); narr[npoints] = CNinsert_tailnode(nodehead,nodetail,pt,P->z,P->ID); npoints++; } /* Create the triangles */ for (i=1; i<=nt; i++) { p1 = ipt[i*3-2]; p2 = ipt[i*3-1]; p3 = ipt[i*3]; if ((p1>=0 && p1=0 && p2=0 && p3