ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cal/tabfunc.c
Revision: 1.6
Committed: Sat Nov 12 06:14:51 2005 UTC (18 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R2, rad4R2P2, rad5R0, rad5R1, rad4R2, rad4R1, rad4R0, rad3R8, rad3R9, rad4R2P1
Changes since 1.5: +4 -2 lines
Log Message:
Improved default ordinal color scale in falsecolor

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 1.6 static const char RCSid[] = "$Id: tabfunc.c,v 1.5 2003/07/27 22:12:01 schorsch Exp $";
3 greg 1.1 #endif
4     /*
5     * Put tabular data into functions suitable for cal programs.
6     *
7     * 2/2/95 Greg Ward
8     */
9    
10     #include <stdio.h>
11     #include <stdlib.h>
12 schorsch 1.2 #include <string.h>
13 greg 1.1 #include <math.h>
14     #include <ctype.h>
15 schorsch 1.4 #include <sys/types.h>
16 schorsch 1.2
17 schorsch 1.4 #include "rtprocess.h" /* getpid() */
18     #include "rtmath.h"
19     #include "rtio.h"
20 greg 1.1
21     #define isdelim(c) (isspace(c) || (c)==',')
22    
23     #define MAXTAB 1024 /* maximum number of data rows */
24     #define MAXLINE 4096 /* maximum line width (characters) */
25     #define OUTFMT "%.7g" /* output format conversion string */
26    
27     int interpolate = 0;
28     char *progname;
29     char **func;
30     int nfuncs;
31    
32 schorsch 1.3 RREAL abscissa[MAXTAB]; /* independent values (first column) */
33     RREAL (*ordinate)[MAXTAB]; /* dependent values (other columns) */
34 greg 1.1 int tabsize = 0; /* final table size (number of rows) */
35     char locID[16]; /* local identifier (for uniqueness) */
36    
37 schorsch 1.2 /*extern char *fgets(), *fskip(), *absc_exp();*/
38 greg 1.1
39 schorsch 1.2 static void load_data(FILE *fp);
40     static void print_funcs(char *xe);
41 schorsch 1.3 static void putlist(register RREAL *av, int al, register int pos);
42 schorsch 1.2 static char * absc_exp(void);
43    
44     int
45     main(
46     int argc,
47     char **argv
48     )
49 greg 1.1 {
50     progname = argv[0];
51     argv++;
52     argc--;
53     if (argc && !strcmp(argv[0], "-i")) {
54     interpolate++;
55     puts("interp_arr2`(i,x,f):(i+1-x)*f(i)+(x-i)*f(i+1);");
56     puts("interp_arr`(x,f):if(x-1,if(f(0)-x,interp_arr2`(floor(x),x,f),f(f(0))),f(1));");
57     argv++;
58     argc--;
59     }
60     if (!argc || argv[0][0] == '-') {
61     fprintf(stderr, "Usage: %s [-i] func1 [func2 ..]\n", progname);
62     exit(1);
63     }
64     func = argv;
65     nfuncs = argc;
66 schorsch 1.3 ordinate = (RREAL (*)[MAXTAB])malloc(nfuncs*MAXTAB*sizeof(RREAL));
67 greg 1.1 if (ordinate == NULL) {
68     fprintf(stderr, "%s: not enough memory\n", progname);
69     exit(1);
70     }
71     sprintf(locID, "p%d", getpid());
72     load_data(stdin);
73     print_funcs(absc_exp());
74     exit(0);
75     }
76    
77    
78 schorsch 1.2 static void
79     load_data( /* load tabular data from fp */
80     FILE *fp
81     )
82 greg 1.1 {
83     int lineno;
84     char *err;
85     char inpbuf[MAXLINE];
86     register char *cp;
87     register int i;
88    
89     tabsize = lineno = 0;
90     inpbuf[MAXLINE-2] = '\n';
91     while (fgets(inpbuf, MAXLINE, fp) != NULL) {
92     lineno++;
93     if (inpbuf[MAXLINE-2] != '\n') {
94     err = "line too long";
95     goto fatal;
96     }
97     if (tabsize >= MAXTAB-1) {
98     err = "too many rows";
99     goto fatal;
100     }
101     if ((cp = fskip(inpbuf)) == NULL)
102     continue; /* skip non-data lines */
103     abscissa[tabsize] = atof(inpbuf);
104     for (i = 0; i < nfuncs; i++) {
105     while (isdelim(*cp))
106     cp++;
107     if (!*cp) {
108     err = "too few columns";
109     goto fatal;
110     }
111     ordinate[i][tabsize] = atof(cp);
112     if ((cp = fskip(cp)) == NULL) {
113     err = "bad floating-point format";
114     goto fatal;
115     }
116     }
117     tabsize++;
118     }
119     return;
120     fatal:
121     fprintf(stderr, "%s: input line %d: %s\n", progname, lineno, err);
122     exit(1);
123     }
124    
125    
126 schorsch 1.2 static char *
127     absc_exp(void) /* produce expression for abscissa */
128 greg 1.1 {
129     static char ourexp[64];
130     double step, eps;
131     int uniform, increasing;
132     register int i;
133    
134     if (tabsize < 2)
135     return("1");
136     step = abscissa[1] - abscissa[0];
137     eps = ((increasing = (step > 0)) ? 1e-3 : -1e-3) * step;
138     uniform = 1;
139     for (i = 2; i < tabsize; i++) {
140     if (uniform && fabs((abscissa[i]-abscissa[i-1]) - step) > eps)
141     uniform = 0;
142     if (!uniform && (abscissa[i-1] < abscissa[i]) != increasing) {
143     fprintf(stderr, "%s: input not a function\n",
144     progname);
145     exit(1);
146     }
147     }
148     if (uniform) {
149     if (increasing && fabs(step - 1) < eps) {
150     if (fabs(abscissa[0] - 1) < eps)
151     strcpy(ourexp, "x");
152     else
153     sprintf(ourexp, "x-%g", abscissa[0]-1);
154 greg 1.6 } else if (fabs(abscissa[0]) < eps)
155     sprintf(ourexp, "x/%g+1", step);
156     else
157 greg 1.1 sprintf(ourexp, "(x-%g)/%g+1", abscissa[0], step);
158     } else {
159     printf("X`%s(i):select(i,", locID);
160     putlist(abscissa, tabsize, 20);
161     puts(");");
162     if (increasing) {
163     printf("fx`%s(x):if(x-%g,if(%g-x,fx2`%s(x,%d),%d),1);\n",
164     locID, abscissa[0], abscissa[tabsize-1],
165     locID, tabsize, tabsize);
166     printf("fx2`%s(x,i):if(x-X`%s(i),\n", locID, locID);
167     } else {
168     printf("fx`%s(x):if(%g-x,if(x-%g,fx2`%s(x,%d),%d),1);\n",
169     locID, abscissa[0], abscissa[tabsize-1],
170     locID, tabsize, tabsize);
171     printf("fx2`%s(x,i):if(X`%s(i)-x,\n", locID, locID);
172     }
173     printf("\ti+(x-X`%s(i))/(X`%s(i+1)-X`%s(i)),\n",
174     locID, locID, locID);
175     printf("\tfx2`%s(x,i-1));\n", locID);
176     sprintf(ourexp, "fx`%s(x)", locID);
177     }
178     return(ourexp);
179     }
180    
181    
182 schorsch 1.2 static void
183     print_funcs( /* print functions */
184     char *xe
185     )
186 greg 1.1 {
187     int xelen;
188     register int i;
189    
190     xelen = strlen(xe);
191     for (i = 0; i < nfuncs; i++) {
192 schorsch 1.5 if ((func[i][0] == '\0') | (func[i][0] == '0'))
193 greg 1.1 continue;
194     if (interpolate) {
195     printf("%s`%s(i):select(i,", func[i], locID);
196     putlist(ordinate[i], tabsize,
197     27+strlen(func[i]));
198     puts(");");
199     printf("%s(x):interp_arr`(%s,%s`%s);\n",
200     func[i], xe, func[i], locID);
201     } else {
202     printf("%s(x):select(%s,", func[i], xe);
203     putlist(ordinate[i], tabsize, strlen(func[i])+xelen+12);
204     puts(");");
205     }
206     }
207     }
208    
209    
210 schorsch 1.2 static void
211     putlist( /* put out array of values */
212 schorsch 1.3 register RREAL *av,
213 schorsch 1.2 int al,
214     register int pos
215     )
216 greg 1.1 {
217     char obuf[32];
218     int len;
219    
220     while (al--) {
221     sprintf(obuf, OUTFMT, *av++);
222     pos += (len = strlen(obuf)+1);
223     if (pos >= 64) {
224     putchar('\n');
225     pos = len;
226     }
227     fputs(obuf, stdout);
228     if (al)
229     putchar(',');
230     }
231     }