Back to index

radiance  4R0+20100331
Defines | Functions | Variables
tabfunc.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <sys/types.h>
#include "rtprocess.h"
#include "rtmath.h"
#include "rtio.h"

Go to the source code of this file.

Defines

#define isdelim(c)   (isspace(c) || (c)==',')
#define MAXTAB   1024 /* maximum number of data rows */
#define MAXLINE   4096 /* maximum line width (characters) */
#define OUTFMT   "%.7g" /* output format conversion string */

Functions

static void load_data (FILE *fp)
static void print_funcs (char *xe)
static void putlist (register RREAL *av, int al, register int pos)
static char * absc_exp (void)
int main (int argc, char **argv)

Variables

static const char RCSid [] = "$Id: tabfunc.c,v 1.6 2005/11/12 06:14:51 greg Exp $"
int interpolate = 0
char * progname
char ** func
int nfuncs
RREAL abscissa [MAXTAB]
RREAL(* ordinate )[MAXTAB]
int tabsize = 0
char locID [16]

Define Documentation

#define isdelim (   c)    (isspace(c) || (c)==',')

Definition at line 21 of file tabfunc.c.

#define MAXLINE   4096 /* maximum line width (characters) */

Definition at line 24 of file tabfunc.c.

#define MAXTAB   1024 /* maximum number of data rows */

Definition at line 23 of file tabfunc.c.

#define OUTFMT   "%.7g" /* output format conversion string */

Definition at line 25 of file tabfunc.c.


Function Documentation

static char * absc_exp ( void  ) [static]

Definition at line 127 of file tabfunc.c.

{
       static char   ourexp[64];
       double step, eps;
       int    uniform, increasing;
       register int  i;

       if (tabsize < 2)
              return("1");
       step = abscissa[1] - abscissa[0];
       eps = ((increasing = (step > 0)) ? 1e-3 : -1e-3) * step;
       uniform = 1;
       for (i = 2; i < tabsize; i++) {
              if (uniform && fabs((abscissa[i]-abscissa[i-1]) - step) > eps)
                     uniform = 0;
              if (!uniform && (abscissa[i-1] < abscissa[i]) != increasing) {
                     fprintf(stderr, "%s: input not a function\n",
                                   progname);
                     exit(1);
              }
       }
       if (uniform) {
              if (increasing && fabs(step - 1) < eps) {
                     if (fabs(abscissa[0] - 1) < eps)
                            strcpy(ourexp, "x");
                     else
                            sprintf(ourexp, "x-%g", abscissa[0]-1);
              } else if (fabs(abscissa[0]) < eps)
                     sprintf(ourexp, "x/%g+1", step);
              else
                     sprintf(ourexp, "(x-%g)/%g+1", abscissa[0], step);
       } else {
              printf("X`%s(i):select(i,", locID);
              putlist(abscissa, tabsize, 20);
              puts(");");
              if (increasing) {
                     printf("fx`%s(x):if(x-%g,if(%g-x,fx2`%s(x,%d),%d),1);\n",
                                   locID, abscissa[0], abscissa[tabsize-1],
                                   locID, tabsize, tabsize);
                     printf("fx2`%s(x,i):if(x-X`%s(i),\n", locID, locID);
              } else {
                     printf("fx`%s(x):if(%g-x,if(x-%g,fx2`%s(x,%d),%d),1);\n",
                                   locID, abscissa[0], abscissa[tabsize-1],
                                   locID, tabsize, tabsize);
                     printf("fx2`%s(x,i):if(X`%s(i)-x,\n", locID, locID);
              }
              printf("\ti+(x-X`%s(i))/(X`%s(i+1)-X`%s(i)),\n",
                            locID, locID, locID);
              printf("\tfx2`%s(x,i-1));\n", locID);
              sprintf(ourexp, "fx`%s(x)", locID);
       }
       return(ourexp);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void load_data ( FILE *  fp) [static]

Definition at line 79 of file tabfunc.c.

{
       int    lineno;
       char   *err;
       char   inpbuf[MAXLINE];
       register char *cp;
       register int  i;

       tabsize = lineno = 0;
       inpbuf[MAXLINE-2] = '\n';
       while (fgets(inpbuf, MAXLINE, fp) != NULL) {
              lineno++;
              if (inpbuf[MAXLINE-2] != '\n') {
                     err = "line too long";
                     goto fatal;
              }
              if (tabsize >= MAXTAB-1) {
                     err = "too many rows";
                     goto fatal;
              }
              if ((cp = fskip(inpbuf)) == NULL)
                     continue;            /* skip non-data lines */
              abscissa[tabsize] = atof(inpbuf);
              for (i = 0; i < nfuncs; i++) {
                     while (isdelim(*cp))
                            cp++;
                     if (!*cp) {
                            err = "too few columns";
                            goto fatal;
                     }
                     ordinate[i][tabsize] = atof(cp);
                     if ((cp = fskip(cp)) == NULL) {
                            err = "bad floating-point format";
                            goto fatal;
                     }
              }
              tabsize++;
       }
       return;
fatal:
       fprintf(stderr, "%s: input line %d: %s\n", progname, lineno, err);
       exit(1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char **  argv 
)

Definition at line 45 of file tabfunc.c.

{
       progname = argv[0];
       argv++;
       argc--;
       if (argc && !strcmp(argv[0], "-i")) {
              interpolate++;
              puts("interp_arr2`(i,x,f):(i+1-x)*f(i)+(x-i)*f(i+1);");
              puts("interp_arr`(x,f):if(x-1,if(f(0)-x,interp_arr2`(floor(x),x,f),f(f(0))),f(1));");
              argv++;
              argc--;
       }
       if (!argc || argv[0][0] == '-') {
              fprintf(stderr, "Usage: %s [-i] func1 [func2 ..]\n", progname);
              exit(1);
       }
       func = argv;
       nfuncs = argc;
       ordinate = (RREAL (*)[MAXTAB])malloc(nfuncs*MAXTAB*sizeof(RREAL));
       if (ordinate == NULL) {
              fprintf(stderr, "%s: not enough memory\n", progname);
              exit(1);
       }
       sprintf(locID, "p%d", getpid());
       load_data(stdin);
       print_funcs(absc_exp());
       exit(0);
}

Here is the call graph for this function:

static void print_funcs ( char *  xe) [static]

Definition at line 183 of file tabfunc.c.

{
       int    xelen;
       register int  i;

       xelen = strlen(xe);
       for (i = 0; i < nfuncs; i++) {
              if ((func[i][0] == '\0') | (func[i][0] == '0'))
                     continue;
              if (interpolate) {
                     printf("%s`%s(i):select(i,", func[i], locID);
                     putlist(ordinate[i], tabsize,
                                   27+strlen(func[i]));
                     puts(");");
                     printf("%s(x):interp_arr`(%s,%s`%s);\n",
                                   func[i], xe, func[i], locID);
              } else {
                     printf("%s(x):select(%s,", func[i], xe);
                     putlist(ordinate[i], tabsize, strlen(func[i])+xelen+12);
                     puts(");");
              }
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void putlist ( register RREAL av,
int  al,
register int  pos 
) [static]

Definition at line 211 of file tabfunc.c.

{
       char   obuf[32];
       int    len;

       while (al--) {
              sprintf(obuf, OUTFMT, *av++);
              pos += (len = strlen(obuf)+1);
              if (pos >= 64) {
                     putchar('\n');
                     pos = len;
              }
              fputs(obuf, stdout);
              if (al)
                     putchar(',');
       }
}

Here is the caller graph for this function:


Variable Documentation

Definition at line 32 of file tabfunc.c.

char** func

Definition at line 29 of file tabfunc.c.

int interpolate = 0

Definition at line 27 of file tabfunc.c.

char locID[16]

Definition at line 35 of file tabfunc.c.

int nfuncs

Definition at line 30 of file tabfunc.c.

Definition at line 33 of file tabfunc.c.

char* progname

Definition at line 28 of file tabfunc.c.

const char RCSid[] = "$Id: tabfunc.c,v 1.6 2005/11/12 06:14:51 greg Exp $" [static]

Definition at line 2 of file tabfunc.c.

int tabsize = 0

Definition at line 34 of file tabfunc.c.