michel@es.ele.tue.nl (& Berkelaar) (10/13/89)
Here is part two: --------------- #! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # If this archive is complete, you will see the following message at the end: # "End of archive 2 (of 3)." # # Contents: # Makefile direct-tek.c find-tek.c hat.c hills.c pswrap.c # read_matrix.c showfloat.c stdoutpipe.c surface.c read_matrix.h # showfloat.h # # Wrapped by tap@cs.utoronto.ca on Wed Oct 11 23:48:18 1989 # PATH=/bin:/usr/bin:/usr/ucb ; export PATH if test -f Makefile -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"Makefile\" else echo shar: Extracting \"Makefile\" \(890 characters\) sed "s/^X//" >Makefile <<'END_OF_Makefile' X# Compile with floating point flag if your machine has it! X X# If your make does not define LINK.c, uncomment the following line X#LINK.c= $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) $(LDFLAGS) $(LDFLAGS) X XCFLAGS = -O2 XCLIBS = -lm X Xdefault: all X Xall: hills graph3d surface graph3d-tek hat pswrap X Xpswrap: pswrap.o X $(LINK.c) -o pswrap pswrap.o -lm X Xhat: hat.o X $(LINK.c) -o hat hat.o -lm X Xhat2: hat2.o X $(LINK.c) -o hat2 hat2.o -lm X Xhills: hills.o X $(LINK.c) -o hills hills.o -lm X Xgraph3d: graph3d.o direct-tek.o stdoutpipe.o X $(LINK.c) -o graph3d graph3d.o direct-tek.o stdoutpipe.o -lm -lplot X Xgraph3d-tek: graph3d.o direct-tek.o stdoutpipe.o X $(LINK.c) -o graph3d-tek graph3d.o direct-tek.o stdoutpipe.o -lm -l4014 X Xsurface: surface.o read_matrix.o showfloat.o X $(LINK.c) -o surface surface.o read_matrix.o showfloat.o -lm X Xshowfloat: showfloat.o X $(LINK.c) -DMAIN showfloat.c -o showfloat END_OF_Makefile if test 890 -ne `wc -c <Makefile`; then echo shar: \"Makefile\" unpacked with wrong size! fi # end of overwriting check fi if test -f direct-tek.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"direct-tek.c\" else echo shar: Extracting \"direct-tek.c\" \(4489 characters\) sed "s/^X//" >direct-tek.c <<'END_OF_direct-tek.c' X#include <stdio.h> X#include <sys/param.h> X#include <sys/types.h> X#include <sys/stat.h> X X/* X * direct-tek - change `out' to be a stream for writing directly to a tek X * terminal, under certain conditions. X * The conditions are: X * (1) my_name ends in "-tek" X * (2) the integer 'dont_change_out' is zero X * (3) the print mode is X * There are three ways of getting the name of the tek terminal X * (1) the char * variable 'use_tty' contains the name X * (2) the environment variable 'TEK_TTY' contains the name X * (3) the file '$HOME/.tek-tty' contains the name X */ X X/* X * MAXHOSTNAMELEN is defined in <sys/param.h>, but only since sunOS 4.O X */ X#ifndef MAXHOSTNAMELEN X#define MAXHOSTNAMELEN 64 X#endif X Xextern char *use_tty; Xextern int dont_change_out; Xextern char *my_name; Xextern FILE *out; Xextern int force_tty; Xextern int errno; Xextern char *sys_errlist[]; X XCheckTekTerminal() X{ X FILE *f; X char *home,*getenv(),*machine; X int error; X char tektty[MAXHOSTNAMELEN+MAXPATHLEN]; X char tekhost[MAXHOSTNAMELEN]; X char host[MAXHOSTNAMELEN]; X char file[MAXPATHLEN]; X char command[200]; X char *comargv[4]; X struct stat statbuf; X /* X * don't do it if we don't end in "-tek" or have been prohibited X */ X if (dont_change_out || strcmp(my_name+strlen(my_name)-4,"-tek")) { X if (use_tty != NULL) X fprintf(stderr,"%s: tty option ignored\n",my_name); X return; X } X /* X * get the tty name from the environment or from a file X */ X if (use_tty==NULL) { X if (!(use_tty=getenv("TEK_TTY"))) { X home = getenv("HOME"); X if (!home) {error=1;goto cant_find_tty;} X strcpy(file,home); X strcat(file,"/.tek-tty"); X if (!(f=fopen(file,"r"))) {error=2;goto cant_find_tty;} X use_tty=fgets(tektty,MAXPATHLEN+MAXHOSTNAMELEN,f); X fclose(f); X } X } X X /* X * check that we found something (and strip the \n) X */ X if (use_tty==NULL || use_tty[0]==0) {error=3;goto cant_find_tty;} X if (use_tty[strlen(use_tty)-1]=='\n') use_tty[strlen(use_tty)-1]=0; X X /* X * split it into machine and filename X */ X if (index(use_tty,':')) { X if (sscanf(use_tty,"%[^:]:%s",tekhost,tektty)!=2) { X fprintf(stderr,"%s: bad tty name format \"%s\"\n",my_name,use_tty); X } else { X machine = tekhost; X use_tty = tektty; X } X } else { X machine = NULL; X } X X /* X * if the tekhost is the same as our host, don't bother with machine X */ X gethostname(host,MAXHOSTNAMELEN); X if (machine!=NULL && !strcmp(machine,host)) machine=NULL; X X /* X * Put the tty file name in file[] X * if the ttyname does not being with a . or a / assume it is of the X * form X or ttyX, and convert it to /dev/ttyX X */ X file[0]=0; X if (use_tty[0]!='.' && use_tty[0]!='/') { X if (strncmp("tty",use_tty,3)) X strcpy(file,"/dev/tty"); X else X strcpy(file,"/dev/"); X strcat(file,use_tty); X } else { X strcpy(file,use_tty); X } X X /* X * Now, do the dirty work X * If we have a remote machine, use popen & rsh cat, X * otherwise just open a file. X * Close stdout first, so that the fd 1 gets associated with our new stream. X */ X if (machine==NULL) { X /* fprintf(stderr,"machine==NULL, openning \"%s\"\n",file); */ X if (stat(file,&statbuf)) {error=4;goto cant_find_tty;} X if (!force_tty && statbuf.st_uid!=getuid()) {error=6; goto cant_find_tty;} X close(1); X if (!(out=fopen(file,"w"))) {error=5;goto cant_find_tty;} X } else { X comargv[0]="/usr/ucb/rsh"; X comargv[1]=machine; X if (!force_tty) X sprintf(command,"if (-o %s) then \nexec cat > %s \nelse \necho not owner of %s \nexit 1 \nendif",file,file,file); X else X sprintf(command,"exec cat > %s",file); X comargv[2]=command; X comargv[3]=0; X stdoutpipe(comargv,1); X } X return; X X cant_find_tty: X fprintf(stderr,"%s: Can't find location of Tektronics window, reason:\n",my_name); X switch (error) { X case 1: X fprintf(stderr,"HOME environment variable is not set: can't look up tek tty\n"); X break; X case 2: X fprintf(stderr,"Can't open file \"%s\"\n",file); X break; X case 3: X fprintf(stderr,"NULL tektronics device name, check $TEK_TTY and ~/.tek-tty\n"); X break; X case 4: X fprintf(stderr,"Can't find tektronics device: \"%s\": %s\n",file,sys_errlist[errno]); X break; X case 5: X fprintf(stderr,"Can't open tektronics device for writing: \"%s\": %s\n",file,sys_errlist[errno]); X break; X case 6: X fprintf(stderr,"Not owner of \"%s\" - use `-forcetty` to write\n",file); X break; X } X exit(1); X} END_OF_direct-tek.c if test 4489 -ne `wc -c <direct-tek.c`; then echo shar: \"direct-tek.c\" unpacked with wrong size! fi # end of overwriting check fi if test -f find-tek.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"find-tek.c\" else echo shar: Extracting \"find-tek.c\" \(1124 characters\) sed "s/^X//" >find-tek.c <<'END_OF_find-tek.c' X/* X * If (1) my_name ends in `-tek' X * (2) stdout is a terminal, X * (3) the flag `to_stdout' is not set X * it trys to set stdout to be tek-window. X * X * It looks first in ./.tek-tty then in $HOME/.tek-tty X * for the name of a tty device, X * and if it finds one, it checks that that tty belongs to X * the user running this process. This check can be overridden X * by the flag `force-tek-tty' X */ X X#include <stdio.h> X#include <sys/params.h> X Xint find_tek_tty(to_stdout,force) Xint to_stdout; Xint force; X{ X char *getenv(); X char *home,*tek,buffer[MAXPATHLEN]; X int found_tek=0; X path = getenv("HOME"); X if ( to_stdout X || !isatty(1) X || strcmp("-tek",my_name+length(my_name)-4)) return; X X tek = getenv("TEKTTY"); X home = getenv("HOME"); X X if (tek) { X if (found_tek(tek,force)) return; X strcpy(buffer,home); X strcat(buffer,"/"); X strcat(buffer,tek); X if (found_tek(buffer,force)) return; X } X if (found_tek(".tek-tty",force)) return; X strcpy(buffer,home); X strcat(buffer,"/.tek-tty"); X if (found_tek(buffer,force)) return; X} X Xfound_tek(name,force) Xchar *name; Xint force; X{ X if X} END_OF_find-tek.c if test 1124 -ne `wc -c <find-tek.c`; then echo shar: \"find-tek.c\" unpacked with wrong size! fi # end of overwriting check fi if test -f hat.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"hat.c\" else echo shar: Extracting \"hat.c\" \(990 characters\) sed "s/^X//" >hat.c <<'END_OF_hat.c' X/* X * generates a mexican hat X * usage: hat grid-size radius X * the radius should be given in radians X * the grid size is the number of points along each axis X */ X X#include <math.h> X#include <stdio.h> X Xdouble sigmoid(x) Xdouble x; X{ X return 1/(1 + exp(x)); X} X Xmain(argc,argv) Xint argc; Xchar *argv[]; X{ X double radius=20; X double x,y,inc,z; X int i,j,points; X if (argc<2) { X fprintf(stderr,"usage: %s grid-points [radius]\n\"%s 60 20\" is good\n",argv[0],argv[0]); X exit(1); X } X points = atoi(argv[1]); X if (argc>2) X radius = atof(argv[2]); X if (radius==0 || points<2) { X fprintf(stderr,"%s: radius is 0 or number of points is < 2\n",argv[0]); X exit(1); X } X inc = radius/points; X x = -radius/2; X printf("%d %d\n",points,points); X for (i=0;i<points;i++) { X y = -radius/2; X for (j=0;j<points;j++) { X z = sqrt(x*x + y*y); X printf("%g ",cos(z) * sigmoid(z*8/radius) * sigmoid(-z*8/radius)); X y += inc; X } X printf("\n"); X x += inc; X } X} END_OF_hat.c if test 990 -ne `wc -c <hat.c`; then echo shar: \"hat.c\" unpacked with wrong size! fi # end of overwriting check fi if test -f hills.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"hills.c\" else echo shar: Extracting \"hills.c\" \(2517 characters\) sed "s/^X//" >hills.c <<'END_OF_hills.c' X/*******************************************************\ X* * X* @(#)hills.c 1/13/89 * X* Author: Tony Plate * X* Copyright (C) 1989 Tony Plate * X* This program may be freely distributed and * X* used, provided that this copyright notice is * X* retained intact. There is no warranty with * X* this software. * X* * X\*******************************************************/ X#include <stdio.h> X#define DB(X) X/* X * hills - toy program - generate a matrix with hills - randomly X * two arguments, edge of matrix & number of hills X */ X Xint max(x,y) int x,y; { if (x > y) return x; else return y; } Xint min(x,y) int x,y; { if (x < y) return x; else return y; } Xint iab(x) int x; { if (x >= 0) return x; else return -x; } X Xint inbounds(array,d,x,y) Xint *array,d,x,y; X{ X DB( printf("[%2d %2d ",x,y); ) X if ((x<0) || (x>=d) || (y<0) || (y>=d)) { X DB( printf("--] "); ) X return 0; X } else { X DB( printf("%2d] ",array[x*d + y]); ) X return array[x*d + y]; X } X} X Xmain(argc,argv) Xint argc; Xchar *argv[]; X{ X int d,n_hills,*array,height,i,j,x,y,corrected,new; X height = 5; X if (argc < 3) { X fprintf(stderr,"usage: %s size n-hills [height [seed]]\n",argv[0]); X exit(1); X } X d = atoi(argv[1]); X n_hills = atoi(argv[2]); X if (argc > 3) height = atoi(argv[3]); X srandom(getpid()); X if (argc > 3) srandom(atoi(argv[4])); X array = (int*)calloc(sizeof(int),d * d); X for (i=0;i<d;i++) X for (j=0;j<d;j++) X array[i*d+j]=0; X for (;n_hills > 0;n_hills--) { X x = random() % d; X y = random() % d; X array[x*d+y]=height; X /* X for (i=max(x-height,0);i<=min(x+height,d);i++) X for (j=max(y-height,0);j<=min(x+height,d);j++) { X array[i*d+j] = max(array[i*d+j],min(height-iab(i-x),height-iab(j-y))); X } X */ X } X do { X corrected = 0; X for (i=0;i<d;i++) X for (j=0;j<d;j++) { X DB( printf("Looking at [%2d %2d %2d]: ",i,j,array[i*d+j]); ) X new = max(inbounds(array,d,i,j), X max(inbounds(array,d,i-1,j-1), X max(inbounds(array,d,i-1,j ), X max(inbounds(array,d,i-1,j+1), X max(inbounds(array,d,i ,j-1), X max(inbounds(array,d,i ,j+1), X max(inbounds(array,d,i+1,j-1), X max(inbounds(array,d,i+1,j ), X inbounds(array,d,i+1,j+1))))))))-1); X DB( printf("new = %d\n",new); ) X if (array[i*d+j]!=new) { X corrected++; X array[i*d+j]=new; X } X } X } while (corrected); X array[0]=height+1; X printf("%d %d\n",d,d); X for (i=0;i<d;i++) { X for (j=0;j<d;j++) X printf(" %3d",array[i*d+j]); X printf("\n"); X } X} END_OF_hills.c if test 2517 -ne `wc -c <hills.c`; then echo shar: \"hills.c\" unpacked with wrong size! fi # end of overwriting check fi if test -f pswrap.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"pswrap.c\" else echo shar: Extracting \"pswrap.c\" \(6751 characters\) sed "s/^X//" >pswrap.c <<'END_OF_pswrap.c' X/* X * pswrap - put a prologue and epilogue around a postscript file X */ X#include <stdio.h> X#include <math.h> X#include <sys/types.h> X#include <sys/time.h> X Xint ps_box_size = 10000; /* the diagram is assumed to be in a 10000x10000 box */ Xchar *title = "standard input"; Xint font_size = 200; Xint font_size_set = 0; Xfloat x_pos = 1; /* prfloating size and position, in inches */ Xfloat y_pos = 1; Xfloat x_size = 6; Xfloat y_scale = 1; Xint x_pack; Xint y_pack; Xfloat page_bottom = 1; Xfloat page_height = 9; /* from bottom, the date will go just above this */ Xfloat page_margin = 1; Xfloat page_width = 6; /* from margin */ Xint n_diagrams = 0; Xint do_date = 1; Xchar *diag[100]; Xchar *my_name; Xchar *user; Xchar *date; X Xchar *options_description[] = { X"-nodate : don't display the date", X"-fontsize N : set the fontsize used in the diagrams", X"-y_scale S : set the ratio height:width", X NULL X}; X Xmain(argc,argv) Xint argc; Xchar *argv[]; X{ X struct timeval tv; X struct timezone tz; X gettimeofday(&tv,&tz); X date = ctime(&tv.tv_sec); X my_name = argv[0]; X process_options(argv); X find_best_packing(); X print_diagrams(); X} X Xprint_diagrams() X{ X char **p = diag; X float x,y; X int i,j; X y = page_bottom + page_height - x_size*y_scale; X x = page_margin; X /* font size gets smaller as drawing boxes get smaller, but not unreadable */ X if (!font_size_set) font_size = (sqrt(x_size)*ps_box_size)/81.65; X header(); X i = j = 0; X while (*p) { X printf("%% file: %s of %d, position %g %g, scale %g %g\n",*p,n_diagrams,x,y,x_size,x_size*y_scale); X printf("save 72 72 scale %g %g translate\n",x,y); X printf("%g %g scale save\n",x_size,x_size*y_scale); X printf("1 %d div 1 %d div scale\n",ps_box_size,ps_box_size); X printf("[] 0 setdash 1 setlinewidth\n"); X cat(*p); X printf("restore restore\n"); X /* fprintf(stderr,"i=%d x_pack=%d j=%d y_pack=%d\n",i,x_pack,j,y_pack); */ X if (++i<x_pack) { X x += x_size; X } else { X j++; X i = 0; X y -= y_scale*x_size; X x = page_margin; X } X p++; X } X trailer(); X} X Xcat(file) Xchar *file; X{ X FILE *f; X int c; X extern char *sys_errlist[]; X extern int errno; X if (!strcmp("-",file)) X f=stdin; X else X f=fopen(file,"r"); X if (f==NULL) { X fprintf(stderr,"%s: can't open \"%s\": %s\n",my_name,file,sys_errlist[errno]); X } else { X while ((c=getc(f))!=EOF) putchar(c); X } X} X Xheader() X{ X printf("%%!PS-Adobe-1.0\n"); X printf("%%%%Creator: pswrap\n"); X printf("%%%%For: %s\n",getenv("USER")); X printf("%%%%CreationDate: %s",date); X printf("%%%%Title: diagrams\n"); X printf("%%%%Pages: (atend)\n"); X printf("%%%%DocumentFonts: (atend)\n"); X printf("/inches { 72 mul } def\n"); X printf("/cm { 28.34646 mul } def\n"); X printf("%% xy xy xy xy\n"); X printf("/sqfill {\n"); X printf(" newpath\n"); X printf(" moveto\n"); X printf(" lineto\n"); X printf(" lineto\n"); X printf(" lineto\n"); X printf(" closepath\n"); X printf(" gsave\n"); X printf(" 1 setgray\n"); X printf(" fill\n"); X printf(" grestore\n"); X printf(" 0 setgray\n"); X printf(" stroke\n"); X printf("} def\n"); X printf("%% xy xy xy\n"); X printf("/trfill {\n"); X printf(" newpath\n"); X printf(" moveto\n"); X printf(" lineto\n"); X printf(" lineto\n"); X printf(" gsave\n"); X printf(" 1 setgray\n"); X printf(" closepath\n"); X printf(" fill\n"); X printf(" grestore\n"); X printf(" 0 setgray\n"); X printf(" stroke\n"); X printf("} def \n"); X printf("/sq {\n"); X printf(" newpath\n"); X printf(" moveto\n"); X printf(" lineto\n"); X printf(" lineto\n"); X printf(" lineto\n"); X printf(" closepath\n"); X printf(" 0 setgray\n"); X printf(" stroke\n"); X printf("} def\n"); X printf("/tr {\n"); X printf(" newpath\n"); X printf(" moveto\n"); X printf(" lineto\n"); X printf(" lineto\n"); X printf(" closepath\n"); X printf(" 0 setgray\n"); X printf(" stroke\n"); X printf("} def\n"); X printf("/label {\n"); X printf(" %d mul\n",font_size); X printf(" dup\n"); X printf(" 0 lt { %d add } if\n",ps_box_size); X printf(" exch\n"); X printf(" %d mul\n",font_size); X printf(" dup\n"); X printf(" 0 lt { %d add } if\n",ps_box_size); X printf(" exch\n"); X printf(" moveto\n"); X printf(" show\n"); X printf("} def\n"); X printf("/text {\n"); X printf(" moveto\n"); X printf(" show\n"); X printf("} def\n"); X printf("%%%%EndProlog\n"); X printf("gsave\n"); X printf("/Courier findfont\n"); X printf("%d scalefont setfont\n",font_size); X} X Xtrailer() X{ X printf("showpage\n"); X printf("grestore\n"); X printf("%%%%Trailer\n"); X printf("%%%%DocumentFonts: Courier\n"); X printf("%%%%Pages: 1\n"); X} X Xprocess_options(argv) Xchar *argv[]; X{ X char *p; X while (p = *(++argv)) { X if (*p == '-') { X p++; X if (prefix(p,"ps_scale")) { X if (!*(++argv)) error("Expected box size after '-%s'",p); X ps_box_size = atoi(*argv); X } else if (prefix(p,"nodate")) { X do_date = 0; X } else if (prefix(p,"fontsize")) { X if (!*(++argv)) error("Expected number after '-%s'",p); X font_size = atoi(*argv); X font_size_set = 1; X } else if (prefix(p,"yscale")) { X if (!*(++argv)) error("Expected scale factor after '-%s'",p); X y_scale = atof(*argv); X } else { X error("Bad argument: '%s'",p); X } X } else { X diag[n_diagrams++] = *argv; X } X } X if (n_diagrams==0) { X diag[n_diagrams++] = "-"; X } X} X Xfind_best_packing() X{ X int n = n_diagrams; X float max_size=0; X float y_size; X int max_size_x_pack=0; X int max_size_y_pack=0; X for (x_pack=1;x_pack<=n;x_pack++) { X x_size = page_width/x_pack; X y_pack = n/x_pack; X if (n % x_pack) y_pack++; X y_size = page_height/y_pack; X if (y_size<x_size*y_scale) x_size = y_size/y_scale; X if (x_size > max_size) { X max_size = x_size; X max_size_x_pack = x_pack; X max_size_y_pack = y_pack; X } X } X x_pack = max_size_x_pack; X y_pack = max_size_y_pack; X x_size = max_size; X if ((x_pack+1)*x_size <= page_width) x_pack++; X} X X/* X * return 1 if prefix is an unambigous prefix of the option 'word' X */ X Xprefix(prefix,word) Xchar *prefix,*word; X{ X int matches,len; X char **option; X len = strlen(prefix); X if (strncmp(prefix,word,len)) return 0; X if (strlen(word) == len) return 1; X /* we've got a prefix match, check for ambiguity */ X matches = 0; X option = options_description; X while (*option) { X if (option[0][0]=='-' && !strncmp(prefix,option[0]+1,len)) X matches++; X option++; X } X if (matches==1) return 1; X else return 0; X} X Xprint_options() X{ X char **p = options_description; X while (*p) printf("%s\n",*p++); X} X Xerror(s,p) X{ X char str[200]; X fprintf(stderr,sprintf(str,"%s: %s\n",my_name,s),p); X exit(1); X} END_OF_pswrap.c if test 6751 -ne `wc -c <pswrap.c`; then echo shar: \"pswrap.c\" unpacked with wrong size! fi # end of overwriting check fi if test -f read_matrix.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"read_matrix.c\" else echo shar: Extracting \"read_matrix.c\" \(2372 characters\) sed "s/^X//" >read_matrix.c <<'END_OF_read_matrix.c' X/*******************************************************\ X* * X* @(#)read_matrix.c 1/2/89 * X* Author: Tony Plate * X* Copyright (C) 1989 Tony Plate * X* This program may be freely distributed, * X* provided that this copyright notice is * X* retained intact. There is no warranty * X* with this software. * X* * X\*******************************************************/ X X#include <stdio.h> X/* X * one function that will read in a matrix, (and allocate space for it) X */ X Xfloat *read_matrix(x_dim,y_dim) Xint *x_dim,*y_dim; X{ X float v,*matrix; X int x,y,r,stop; X int long_matrix = 0; /* true if we don't know the # of y lines */ X if (scanf(" %d",x_dim)!=1) { X fprintf(stderr,"read_matrix: couldn't read x dimensions\n"); X return NULL; X } X if (scanf(" *%c",&x)==1) { X long_matrix = 1; X } else if (scanf(" %d",y_dim)!=1) { X fprintf(stderr,"read_matrix: couldn't read y dimension\n"); X return NULL; X } X if (!long_matrix) { X if (*x_dim<=0 || *y_dim<=0) { X fprintf(stderr,"read_matrix: bad dimensions %d %d\n",*x_dim,*y_dim); X return NULL; X } X matrix = (float *) calloc(*x_dim * *y_dim , sizeof(float)); X if (matrix==NULL) { X fprintf(stderr,"read_matrix: can't allocate space\n"); X return NULL; X } X for (y=0;y<*y_dim;y++) X for (x=0;x<*x_dim;x++) X if (scanf(" %f",&v)!=1) { X fprintf(stderr,"read_matrix: can't read value row=%d,col=%d\n",y,x); X return NULL; X } else matrix[y*(*x_dim) + x] = v; X } else { X if (*x_dim<=0) { X fprintf(stderr,"read_matrix: bad x dimensions %d\n",*x_dim); X return NULL; X } X *y_dim = 1; X matrix = (float *) calloc(*x_dim * *y_dim , sizeof(float)); X if (matrix==NULL) { X fprintf(stderr,"read_matrix: can't allocate space\n"); X return NULL; X } X stop = 0; X for (y=0;!stop;y++) { X for (x=0;x<*x_dim;x++) { X if (scanf(" %f",&v)!=1) { X if (x!=0) { X fprintf(stderr,"read_matrix: can't read value row=%d,col=%d\n",y,x); X return NULL; X } else { X stop = 1; X break; X } X } else { X if (y==*y_dim) { X (*y_dim)++; X matrix = (float *) realloc(matrix,*x_dim * *y_dim * sizeof(float)); X if (matrix==NULL) { X fprintf(stderr,"read_matrix: can't reallocate space\n"); X return NULL; X } X } X matrix[y*(*x_dim) + x] = v; X } X } X } X } X return matrix; X} END_OF_read_matrix.c if test 2372 -ne `wc -c <read_matrix.c`; then echo shar: \"read_matrix.c\" unpacked with wrong size! fi # end of overwriting check fi if test -f showfloat.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"showfloat.c\" else echo shar: Extracting \"showfloat.c\" \(1479 characters\) sed "s/^X//" >showfloat.c <<'END_OF_showfloat.c' X#include <math.h> X#include "showfloat.h" X X/* X * sn(d) returns a three character representation of the floating point X * number 'd'. It just represents values between -1.0 & +1.0, it was X * designed so that probability matrices could be printed compactly. X * If a number is less than 0.1, but greater than or equal to 0.01, it is X * represented as :n (double decimal point) X * output d X * ^^ > 1.0 X * ++ = 1.0 100 % X * nn = 0.nn nn% X * .n = 0.00n n/10 of % X * :n = 0.000n n/100 of % X */ Xint sign(d) Xdouble d; X{ X if (d<0) return -1; X else return 1; X} X Xchar *sn(d) Xdouble d; X{ X char *res,c,p; X int i; X static char str[40]; X if (d < -1.0) res = " __"; X else if (d > 1.0) res = " ^^"; X else { X res = str; X i = 100 * (d + sign(d)*0.005); X if ((i != 0) && (fabs(d) >= 0.0095)) { X sprintf(str,"%3d",i); X if (i == 100) res = " =1"; X if (i == -100) res = "=-1"; X } X else { X i = 1000 * (d + sign(d)*0.0005); X p = '.'; X if (i==0 || fabs(d) < 0.00095) {p=':'; i = 10000 * (d+sign(d)*0.00005);} X c = ' '; X if (i < 0) { i = -i; c = '-'; } X sprintf(str,"%c%c%1d",c,p,i); X if (i == 0) res = " 0"; X } X } X return res; X} X Xchar *tn(x) Xdouble x; X{ X static char str[40]; X sprintf(str," %g",x); X return str; X} X X#ifdef MAIN Xmain(argc,argv) Xint argc; Xchar *argv[]; X{ X double x,atof(); X x = atof(argv[1]); X printf("%s == %s\n",sn(x),tn(x)); X} X#endif END_OF_showfloat.c if test 1479 -ne `wc -c <showfloat.c`; then echo shar: \"showfloat.c\" unpacked with wrong size! fi # end of overwriting check fi if test -f stdoutpipe.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"stdoutpipe.c\" else echo shar: Extracting \"stdoutpipe.c\" \(868 characters\) sed "s/^X//" >stdoutpipe.c <<'END_OF_stdoutpipe.c' X/* X * like popen(command,"w"), except that stdout is out end of the pipe X */ X#include <stdio.h> Xextern char *my_name; X Xstdoutpipe(argv,killme) Xchar *argv[]; Xint killme; X{ X int pipes[2]; X int r; X extern int errno; X extern char *sys_errlist[]; X int parent_pid = getpid(); X pipe(pipes); X if (pipe(pipes)!=0) { X fprintf(stderr,"%s: couldn't create pipe: %s\n",my_name,sys_errlist[errno]); X exit(1); X } X if (r=fork()) { X /* parent */ X if (r<0) { X fprintf(stderr,"%s: fork failed: %s\n",my_name,sys_errlist[errno]); X exit(1); X } X /* make stdout the write to the pipe */ X dup2(pipes[1],1); X } else { X /* child - make stdin come from the pipe */ X dup2(pipes[0],0); X execv(argv[0],argv); X fprintf(stderr,"%s: exec failed: %s: %s\n",my_name,argv[0],sys_errlist[errno]); X if (killme) kill(parent_pid,9); X exit(1); X } X} END_OF_stdoutpipe.c if test 868 -ne `wc -c <stdoutpipe.c`; then echo shar: \"stdoutpipe.c\" unpacked with wrong size! fi # end of overwriting check fi if test -f surface.c -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"surface.c\" else echo shar: Extracting \"surface.c\" \(11790 characters\) sed "s/^X//" >surface.c <<'END_OF_surface.c' X/*******************************************************\ X* * X* @(#)surface.c 1/2/89 * X* Author: Tony Plate * X* Copyright (C) 1989 Tony Plate * X* This program may be freely distributed, * X* provided that this copyright notice is * X* retained intact. There is no warranty * X* with this software. * X* * X\*******************************************************/ X X/* X * read a matrix, and output it in one of 4 formats: X * The X and Y directions are used for the positions of values, the Z direction X * is used to represent the value. X */ X X#include <math.h> X#include <stdio.h> X#include <ctype.h> X#include "read_matrix.h" X Xchar *options_description[] = { X"Options: (an unambiguous substring is sufficient)", X"-array : display as a matrix (same format as input)", X"-ascii : ascii format", X"-binary : binary output (default)", X"-brief : display as a matrix, using brief format numbers", X"-delimit <DELIMITER> : only take material from inside the", X" two lines containing the string <DELIMITER>", X"-grid : draw 3d grid lines (default)", X"-help : display this information", X"-histogram : histogram style", X"-polygon : sqare polygons", X"-squaregrid : change the y upper bound so that the grid is square", X"-suppresstext : suppress text following the array", X"-transpose : transpose the x and y-axis", X"-triangle : triangular polygons", X"-unit : the input surface is in the XY unit square", X"-x <low> <high>: specify the X bounds of the input surface", X"-xlines : just draw the lines in the x-direction", X"-y <low> <high>: specify the Y bounds of the input surface", X"-ylines : just draw the lines in the y-direction", XNULL X}; X X X#define DB(X) Xenum {ARRAY,GRID,X_GRID,Y_GRID,BRIEF,TRIANGLES,SQUARES} output_format; Xint transpose=0; Xint histogram=0; Xint squaregrid=0; Xint text_follow=1; Xfloat xlow=0; Xfloat xhigh=0; Xfloat ylow=0; Xfloat yhigh=0; Xint x_dim,y_dim; X Xenum {ASCII,BINARY} number_format=BINARY; X#define LEN 16384 Xchar str[LEN]; X Xprint_options() X{ X char **p; X p = options_description; X while (*p) printf("%s\n",*p++); X} X X Xmain(argc,argv) Xint argc; Xchar *argv[]; X{ X int x,y,label_found; X char *p,*label,*my_name; X float *matrix; X my_name = argv[0]; X output_format = GRID; X label = NULL; X while (*(++argv)) { X p = argv[0]; X if (*p == '-') { X p++; X if (prefix(p,"array")) X output_format = ARRAY; X else if (prefix(p,"ascii")) X number_format = ASCII; X else if (prefix(p,"binary")) X number_format = BINARY; X else if (prefix(p,"brief")) X output_format = BRIEF; X else if (prefix(p,"delimit")) X label = *(++argv); X else if (prefix(p,"grid")) X output_format = GRID; X else if (prefix(p,"help")) { X print_options(); X exit(0); X } else if (prefix(p,"histogram")) X histogram = 1; X else if (prefix(p,"polygon")) X output_format = SQUARES; X else if (prefix(p,"transpose")) X transpose = 1; X else if (prefix(p,"triangle")) X output_format = TRIANGLES; X else if (prefix(p,"x")) { X if (argv[1]==NULL || argv[2]==NULL) { X fprintf(stderr,"%s: expected two numbers after -x\n",my_name); X exit(1); X } X xlow = atof(argv[1]); X xhigh = atof(argv[2]); X argv++; X argv++; X } else if (prefix(p,"y")) { X if (argv[1]==NULL || argv[2]==NULL) { X fprintf(stderr,"%s: expected two numbers after -y\n",my_name); X exit(1); X } X ylow = atof(argv[1]); X yhigh = atof(argv[2]); X argv++; X argv++; X } else if (prefix(p,"unit")) { X xlow = 0; xhigh = 1; X ylow = 0; yhigh = 1; X } else if (prefix(p,"xlines")) X output_format = X_GRID; X else if (prefix(p,"ylines")) X output_format = Y_GRID; X else if (prefix(p,"squaregrid")) X squaregrid = 1; X else if (prefix(p,"suppresstext")) X text_follow = 0; X else goto bad_option; X } else { X X bad_option: X fprintf(stderr,"%s: bad or ambiguous option: %s\nType \"%s -help\" to see list of options\n",my_name,argv[0],my_name); X exit(1); X } X } X if (label!=NULL) { X /* X * find the label string in the file X */ X label_found=0; X while (!label_found && fgets(str,LEN,stdin)!=NULL) X if (substring(label,str)) label_found=1; X if (!label_found) { X fprintf(stderr,"%s: label %s not in input\n",my_name,label); X exit(1); X } X } X if ((matrix = read_matrix(&x_dim,&y_dim))==NULL) exit(1); X if (xlow==0 && xhigh==0) xhigh = x_dim; X if (ylow==0 && yhigh==0) yhigh = y_dim; X if (squaregrid) { X yhigh = y_dim*(xhigh - xlow)/x_dim + ylow; X } X if (transpose) { X int old_x,old_y,i,start; X float tmp,new_tmp; X char *done; X done = (char*)calloc(x_dim,y_dim); X /* X * this is a difficult transpose to do in place, X * because the array may not be square. X * we do it by a permutation - work out where 0,1 goes, etc. X */ X start = 0; X while (start < x_dim*y_dim) { X i = start; X old_y = i / x_dim; X old_x = i % x_dim; X tmp = matrix[i]; X /* printf("(%d",i); */ X do { X done[i]++; X i = old_x * y_dim + old_y; X /* printf(",%d",i); */ X new_tmp = matrix[i]; X matrix[i] = tmp; X tmp = new_tmp; X old_y = i / x_dim; X old_x = i % x_dim; X } while (i!=start); X /* printf(")\n"); */ X while (done[start] && start<x_dim*y_dim) start++; X } X i = y_dim; X y_dim = x_dim; X x_dim = i; X tmp = xlow; xlow = ylow; ylow = xlow; X tmp = xhigh; xhigh = yhigh; yhigh = xhigh; X } X if (number_format==BINARY && output_format!=BRIEF && output_format!=ARRAY) X printf("binary format float4\n"); X /* X * horizontal lines, non-histogram X */ X if (!histogram && (output_format==GRID || output_format==X_GRID)) X for (y=0;y<y_dim;y++) X for (x=0;x<x_dim;x++) X point((x==0 ? 'm' : 'n'),x,y,matrix[y*x_dim+x]); X /* X * vertical lines, non-histogram X */ X if (!histogram && (output_format==GRID || output_format==Y_GRID)) X for (x=0;x<x_dim;x++) X for (y=0;y<y_dim;y++) X point((y==0 ? 'm' : 'n'),x,y,matrix[y*x_dim+x]); X /* X * square panels, non-histogram X */ X if (!histogram && output_format==SQUARES) { X for (x=0;x<x_dim-1;x++) { X for (y=0;y<y_dim-1;y++) { X point('m',x,y,matrix[y*x_dim+x]); X point('n',x+1,y,matrix[y*x_dim+x+1]); X point('n',x+1,y+1,matrix[(y+1)*x_dim+x+1]); X point('n',x,y+1,matrix[(y+1)*x_dim+x]); X point('j',x,y,matrix[y*x_dim+x]); X } X } X } X /* X * triangular panels, non-histogram X */ X if (!histogram && output_format==TRIANGLES) { X fprintf(stderr,"Can't do triangles yet!\n"); X exit(1); X } X /* X * square panels, histogram X */ X if (histogram && (output_format==SQUARES || output_format==TRIANGLES)) { X int n_square=0; X for (x=0;x<x_dim;x++) { X for (y=0;y<y_dim;y++) { X DB( fprintf(stderr,"Doing %d %d (%g)\n",x,y,matrix[y*x_dim+x]); ) X /* a flat square */ X point('m',x,y,matrix[y*x_dim+x]); X point('n',x+1,y,matrix[y*x_dim+x]); X point('n',x+1,y+1,matrix[y*x_dim+x]); X point('n',x,y+1,matrix[y*x_dim+x]); X point('j',x,y,matrix[y*x_dim+x]); X DB( sprintf(str," sq %d",n_square); point_text(str); ) X /* the vertical square in the x direction */ X if (x<x_dim-1 && matrix[y*x_dim+x]!=matrix[y*x_dim+x+1]) { X point('m',x+1,y,matrix[y*x_dim+x]); X point('n',x+1,y,matrix[y*x_dim+x+1]); X point('n',x+1,y+1,matrix[y*x_dim+x+1]); X point('n',x+1,y+1,matrix[y*x_dim+x]); X point('j',x+1,y,matrix[y*x_dim+x]); X } X /* the vertical square in the y direction */ X if (y<y_dim-1 && matrix[y*x_dim+x]!=matrix[(y+1)*x_dim+x]) { X point('m',x,y+1,matrix[y*x_dim+x]); X point('n',x,y+1,matrix[(y+1)*x_dim+x]); X point('n',x+1,y+1,matrix[(y+1)*x_dim+x]); X point('n',x+1,y+1,matrix[y*x_dim+x]); X point('j',x,y+1,matrix[y*x_dim+x]); X } X n_square++; X } X } X } X if (output_format==BRIEF) { X printf("%d %d\n",x_dim,y_dim); X for (y=0;y<y_dim;y++) { X for (x=0;x<x_dim;x++) X printf("%s",sn(matrix[y*x_dim + x])); X printf("\n"); X } X } X if (output_format==ARRAY) { X printf("%d %d\n",x_dim,y_dim); X for (y=0;y<y_dim;y++) { X for (x=0;x<x_dim;x++) X printf(" %11g",matrix[y*x_dim + x]); X printf("\n"); X } X } X /* X * horizontal lines, histogram X */ X if (histogram && (output_format==GRID || output_format==Y_GRID)) { X for (y=0;y<=y_dim;y++) { X if (y>0) { X point_next('m'); X for (x=0;x<=x_dim;x++) { X if (x>0) X point('n',x,y,matrix[(y-1)*x_dim + x-1]); X if (x<x_dim && X (x==0 || (matrix[(y-1)*x_dim + x-1]!=matrix[(y-1)*x_dim + x]))) X point('n',x,y,matrix[(y-1)*x_dim + x]); X } X } X if (y<y_dim) { X point_next('m'); X for (x=0;x<=x_dim;x++) { X if (x>0) point('n',x,y,matrix[y*x_dim + x-1]); X if (x<x_dim X && (x==0 || (matrix[y*x_dim + x-1]!=matrix[y*x_dim + x]))) X point('n',x,y,matrix[y*x_dim + x]); X } X } X } X } X /* X * vertical lines, histogram X */ X if (histogram && (output_format==GRID || output_format==X_GRID)) { X for (x=0;x<=x_dim;x++) { X if (x>0) { X point_next('m'); X for (y=0;y<=y_dim;y++) { X if (y>0) point('n',x,y,matrix[(y-1)*x_dim + x-1]); X if (y<y_dim X && (y==0 || (matrix[(y-1)*x_dim + x-1]!=matrix[y*x_dim + x-1]))) X point('n',x,y,matrix[y*x_dim + x-1]); X } X } X if (x<x_dim) { X point_next('m'); X for (y=0;y<=y_dim;y++) { X if (y>0) point('n',x,y,matrix[(y-1)*x_dim + x]); X if (y<y_dim X && (y==0 || (matrix[(y-1)*x_dim + x]!=matrix[y*x_dim + x]))) X point('n',x,y,matrix[y*x_dim + x]); X } X } X } X } X point_end(); X label_found=0; X if (text_follow) X while (fgets(str,LEN,stdin)!=NULL && !label_found) X if (!isblank(str)) { X if (label!=NULL && substring(label,str)) label_found=1; X else { X /* if (number_format==BINARY) putc('t',stdout); */ X fputs(str,stdout); X } X } X} X Xstatic prev_type=0; Xstatic use_type=0; X Xpoint_end() X{ X if (number_format==ASCII) { X if (prev_type=='m' || prev_type=='n') X printf("\" \n"); X } else if (number_format==BINARY) { X /* X if (prev_type=='m' || prev_type=='n') X putc('e',stdout); X */ X } X prev_type = 0; X} X Xpoint_text(text) Xchar *text; X{ X if (number_format==ASCII) { X printf("\"%s\n",text); X } else if (number_format==ASCII) { X printf("t\"%s\n",text); X } X prev_type = 0; X} X Xpoint_next(type) Xchar type; X{ X use_type = type; X} X Xpoint(type,x,y,z) Xchar type; Xint x,y; Xdouble z; X{ X float yf,xf; X float zf = z; X if (histogram) { X yf = (y-0.5)*(yhigh-ylow)/(y_dim-1) + ylow; X xf = (x-0.5)*(xhigh-xlow)/(x_dim-1) + xlow; X } else { X yf = y*(yhigh-ylow)/(y_dim-1) + ylow; X xf = x*(xhigh-xlow)/(x_dim-1) + xlow; X } X if (use_type) { type = use_type; use_type = 0; } X if (number_format==ASCII) { X if (type=='j') type='n'; X if (type=='m' && (prev_type=='m' || prev_type=='n')) printf("\" \n"); X printf("%g %g %g\n",xf,yf,zf); X } else if (number_format==BINARY) { X if (type=='j') { X putc('j',stdout); /* join up */ X type='n'; X } else { X putc(type,stdout); X fwrite(&xf,sizeof(xf),1,stdout); X fwrite(&yf,sizeof(yf),1,stdout); X fwrite(&zf,sizeof(zf),1,stdout); X /* printf("%c %g %g %g\n",type,xf,yf,zf); */ X } X } X prev_type = type; X} X Xsubstring(sub,str) Xchar *sub,*str; X{ X register char *p,*q; X while (*str) { X p = str; X q = sub; X while (*p && *p == *q) {p++; q++;} X if (*q==NULL) return 1; X str++; X } X return 0; X} X Xisblank(str) Xchar *str; X{ X if (str!=NULL) X while (*str) X if (!isspace(*str++)) return 0; X return 1; X} X X/* X * return 1 if prefix is an unambigous prefix of the option 'word' X */ X Xprefix(prefix,word) Xchar *prefix,*word; X{ X int matches,len; X char **option; X len = strlen(prefix); X if (strncmp(prefix,word,len)) return 0; X /* we've got a match, check for ambiguity */ X matches = 0; X option = options_description; X while (*option) { X if (option[0][0]=='-' && !strncmp(prefix,option[0]+1,len)) X matches++; X option++; X } X if (matches==1) return 1; X else return 0; X} END_OF_surface.c if test 11790 -ne `wc -c <surface.c`; then echo shar: \"surface.c\" unpacked with wrong size! fi # end of overwriting check fi if test -f read_matrix.h -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"read_matrix.h\" else echo shar: Extracting \"read_matrix.h\" \(430 characters\) sed "s/^X//" >read_matrix.h <<'END_OF_read_matrix.h' X/*******************************************************\ X* * X* @(#)read_matrix.h 1/2/89 * X* Author: Tony Plate * X* Copyright (C) 1989 Tony Plate * X* This program may be freely distributed, * X* provided that this copyright notice is * X* retained intact. There is no warranty * X* with this software. * X* * X\*******************************************************/ Xfloat *read_matrix(); END_OF_read_matrix.h if test 430 -ne `wc -c <read_matrix.h`; then echo shar: \"read_matrix.h\" unpacked with wrong size! fi # end of overwriting check fi if test -f showfloat.h -a "${1}" != "-c" ; then echo shar: Will not over-write existing file \"showfloat.h\" else echo shar: Extracting \"showfloat.h\" \(193 characters\) sed "s/^X//" >showfloat.h <<'END_OF_showfloat.h' Xchar *sn(); /* sn(double) - returns a 3 char string, to represent X a number in the range -1.0 to +1.0. */ Xchar *tn(); /* returns and ordinary %g representation */ X END_OF_showfloat.h if test 193 -ne `wc -c <showfloat.h`; then echo shar: \"showfloat.h\" unpacked with wrong size! fi # end of overwriting check fi echo shar: End of archive 2 \(of 3\). cp /dev/null ark2isdone MISSING="" for I in 1 2 3 ; do if test ! -f ark${I}isdone ; then MISSING="${MISSING} ${I}" fi done if test "${MISSING}" = "" ; then echo You have unpacked all 3 archives. rm -f ark[1-9]isdone else echo You still need to unpack the following archives: echo " " ${MISSING} fi ## End of shell archive. exit 0 -- ------------------------------------------------------------------------------- Michel Berkelaar | Email: michel@ele.tue.nl (prefered) Eindhoven University of Technology | ..!mcvax!euteal!michel (old) Dept. of Electrical Engineering | Design Automation Section | P.O. Box 513 | Phone: ... - 31 - 40 - 473345 NL-5600 MB Eindhoven | Fax: ... - 31 - 40 - 448375 The Netherlands | -------------------------------------------------------------------------------