ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/func.c
Revision: 2.32
Committed: Wed Apr 27 21:11:32 2016 UTC (8 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.31: +20 -6 lines
Log Message:
Future bug-proofing, should have no effect on behavior or performance

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.32 static const char RCSid[] = "$Id: func.c,v 2.31 2016/03/22 03:56:17 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * func.c - interface to calcomp functions.
6 greg 2.14 */
7    
8 greg 2.15 #include "copyright.h"
9 greg 1.1
10     #include "ray.h"
11 greg 2.21 #include "paths.h"
12 greg 1.1 #include "otypes.h"
13 greg 2.2 #include "func.h"
14 greg 2.30 #include <ctype.h>
15 greg 1.1
16 greg 2.2
17 greg 1.21 #define INITFILE "rayinit.cal"
18 greg 2.2 #define CALSUF ".cal"
19     #define LCALSUF 4
20 greg 2.10 char REFVNAME[] = "`FILE_REFCNT";
21 greg 1.21
22 greg 1.13 XF unitxf = { /* identity transform */
23 greg 2.8 {{1.0, 0.0, 0.0, 0.0},
24     {0.0, 1.0, 0.0, 0.0},
25     {0.0, 0.0, 1.0, 0.0},
26     {0.0, 0.0, 0.0, 1.0}},
27 greg 1.13 1.0
28     };
29    
30 greg 1.12 XF funcxf; /* current transformation */
31     static OBJREC *fobj = NULL; /* current function object */
32     static RAY *fray = NULL; /* current function ray */
33 greg 1.1
34 greg 2.26 static char rayinitcal[] = INITFILE;
35    
36 greg 2.20 static double l_erf(char *), l_erfc(char *), l_arg(char *);
37 greg 1.1
38 greg 2.2
39 greg 2.26 void
40 greg 2.32 initfunc(void) /* initialize function evaluation */
41 greg 2.26 {
42     if (!rayinitcal[0]) /* already done? */
43     return;
44     esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW;
45     esupport &= ~(E_OUTCHAN);
46     setcontext("");
47     scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
48     scompile("Nx=$4;Ny=$5;Nz=$6;", NULL, 0);
49     scompile("Px=$7;Py=$8;Pz=$9;", NULL, 0);
50     scompile("T=$10;Ts=$25;Rdot=$11;", NULL, 0);
51     scompile("S=$12;Tx=$13;Ty=$14;Tz=$15;", NULL, 0);
52     scompile("Ix=$16;Iy=$17;Iz=$18;", NULL, 0);
53     scompile("Jx=$19;Jy=$20;Jz=$21;", NULL, 0);
54     scompile("Kx=$22;Ky=$23;Kz=$24;", NULL, 0);
55     scompile("Lu=$26;Lv=$27;", NULL, 0);
56     funset("arg", 1, '=', l_arg);
57     funset("erf", 1, ':', l_erf);
58     funset("erfc", 1, ':', l_erfc);
59     setnoisefuncs();
60     setprismfuncs();
61     loadfunc(rayinitcal);
62     rayinitcal[0] = '\0';
63     }
64    
65    
66 greg 2.29 /* Set parameters for current evaluation */
67     void
68     set_eparams(char *prms)
69     {
70 greg 2.32 static char *last_params;
71     static int lplen = 0;
72     int len;
73 greg 2.29 char vname[RMAXWORD];
74     double value;
75     char *cpd;
76     /* check if already set */
77 greg 2.32 if (prms == NULL || !*prms)
78 greg 2.29 return;
79 greg 2.32 if (lplen && !strcmp(prms, last_params))
80 greg 2.29 return;
81 greg 2.32 len = strlen(prms); /* record new settings */
82     if ((lplen != 0) & (lplen <= len)) {
83     free(last_params);
84     lplen = 0;
85     }
86     if (!lplen) {
87     lplen = len + 100;
88     last_params = (char *)malloc(lplen);
89     if (last_params == NULL)
90     error(SYSTEM, "out of memory in set_eparams()");
91     }
92     strcpy(last_params, prms);
93     /* assign each variable */
94 greg 2.29 while (*prms) {
95     if (isspace(*prms)) {
96     ++prms; continue;
97     }
98     if (!isalpha(*prms))
99     goto bad_params;
100     cpd = vname;
101     while (*prms && (*prms != '=') & !isspace(*prms)) {
102 greg 2.31 if (!isid(*prms) | (cpd-vname >= RMAXWORD-1))
103 greg 2.29 goto bad_params;
104     *cpd++ = *prms++;
105     }
106     if (cpd == vname)
107     goto bad_params;
108     *cpd = '\0';
109     while (isspace(*prms)) prms++;
110     if (*prms++ != '=')
111     goto bad_params;
112     value = atof(prms);
113     if ((prms = fskip(prms)) == NULL)
114     goto bad_params;
115     while (isspace(*prms)) prms++;
116     prms += (*prms == ',') | (*prms == ';');
117     varset(vname, '=', value);
118     }
119     return;
120     bad_params:
121     sprintf(errmsg, "bad parameter list '%s'", last_params);
122     error(USER, errmsg);
123     }
124    
125    
126 greg 2.25 MFUNC *
127 schorsch 2.22 getfunc( /* get function for this modifier */
128     OBJREC *m,
129     int ff,
130     unsigned int ef,
131     int dofwd
132     )
133 greg 2.2 {
134     char sbuf[MAXSTR];
135 greg 2.25 char **arg;
136     MFUNC *f;
137 greg 2.2 int ne, na;
138 greg 2.25 int i;
139 greg 2.2 /* check to see if done already */
140     if ((f = (MFUNC *)m->os) != NULL)
141     return(f);
142 greg 2.7 fobj = NULL; fray = NULL;
143 greg 2.26 if (rayinitcal[0]) /* initialize on first call */
144     initfunc();
145 greg 2.2 if ((na = m->oargs.nsargs) <= ff)
146     goto toofew;
147     arg = m->oargs.sarg;
148     if ((f = (MFUNC *)calloc(1, sizeof(MFUNC))) == NULL)
149     goto memerr;
150     i = strlen(arg[ff]); /* set up context */
151 greg 2.26 if (i == 1 && arg[ff][0] == '.') {
152 greg 2.2 setcontext(f->ctx = ""); /* "." means no file */
153 greg 2.26 } else {
154 greg 2.14 strcpy(sbuf,arg[ff]); /* file name is context */
155 greg 2.2 if (i > LCALSUF && !strcmp(sbuf+i-LCALSUF, CALSUF))
156     sbuf[i-LCALSUF] = '\0'; /* remove suffix */
157     setcontext(f->ctx = savestr(sbuf));
158     if (!vardefined(REFVNAME)) { /* file loaded? */
159 greg 2.5 loadfunc(arg[ff]);
160 greg 2.2 varset(REFVNAME, '=', 1.0);
161     } else /* reference_count++ */
162     varset(REFVNAME, '=', varvalue(REFVNAME)+1.0);
163     }
164     curfunc = NULL; /* parse expressions */
165     sprintf(sbuf, "%s \"%s\"", ofun[m->otype].funame, m->oname);
166     for (i=0, ne=0; ef && i < na; i++, ef>>=1)
167     if (ef & 1) { /* flagged as an expression? */
168     if (ne >= MAXEXPR)
169     objerror(m, INTERNAL, "too many expressions");
170     initstr(arg[i], sbuf, 0);
171     f->ep[ne++] = getE1();
172     if (nextc != EOF)
173     syntax("unexpected character");
174     }
175     if (ef)
176     goto toofew;
177     if (i <= ff) /* find transform args */
178     i = ff+1;
179     while (i < na && arg[i][0] != '-')
180     i++;
181 greg 2.26 if (i == na) { /* no transform */
182     f->fxp = f->bxp = &unitxf;
183     } else { /* get transform */
184     if ((f->bxp = (XF *)malloc(sizeof(XF))) == NULL)
185 greg 2.3 goto memerr;
186 greg 2.26 if (invxf(f->bxp, na-i, arg+i) != na-i)
187 greg 2.2 objerror(m, USER, "bad transform");
188 greg 2.26 if (f->bxp->sca < 0.0)
189     f->bxp->sca = -f->bxp->sca;
190 greg 2.2 if (dofwd) { /* do both transforms */
191 greg 2.26 if ((f->fxp = (XF *)malloc(sizeof(XF))) == NULL)
192 greg 2.3 goto memerr;
193 greg 2.26 xf(f->fxp, na-i, arg+i);
194     if (f->fxp->sca < 0.0)
195     f->fxp->sca = -f->fxp->sca;
196 greg 2.2 }
197     }
198     m->os = (char *)f;
199     return(f);
200     toofew:
201 greg 2.4 objerror(m, USER, "too few string arguments");
202 greg 2.2 memerr:
203     error(SYSTEM, "out of memory in getfunc");
204 schorsch 2.22 return NULL; /* pro forma return */
205 greg 2.2 }
206    
207    
208 greg 2.25 void
209 schorsch 2.22 freefunc( /* free memory associated with modifier */
210     OBJREC *m
211     )
212 greg 2.2 {
213 greg 2.25 MFUNC *f;
214     int i;
215 greg 2.2
216     if ((f = (MFUNC *)m->os) == NULL)
217     return;
218     for (i = 0; f->ep[i] != NULL; i++)
219     epfree(f->ep[i]);
220     if (f->ctx[0]) { /* done with definitions */
221     setcontext(f->ctx);
222     i = varvalue(REFVNAME)-.5; /* reference_count-- */
223     if (i > 0)
224     varset(REFVNAME, '=', (double)i);
225     else
226     dcleanup(2); /* remove definitions */
227     freestr(f->ctx);
228     }
229 greg 2.26 if (f->bxp != &unitxf)
230     free((void *)f->bxp);
231     if ((f->fxp != NULL) & (f->fxp != &unitxf))
232     free((void *)f->fxp);
233 greg 2.14 free((void *)f);
234 greg 2.2 m->os = NULL;
235     }
236    
237    
238 greg 2.25 int
239 schorsch 2.22 setfunc( /* set channels for function call */
240 greg 2.26 OBJREC *m,
241     RAY *r
242 schorsch 2.22 )
243 greg 1.1 {
244 greg 2.26 static RNUMBER lastrno = ~0;
245     MFUNC *f;
246 greg 2.25 /* get function if any */
247 greg 2.26 if ((f = (MFUNC *)m->os) == NULL)
248     objerror(m, CONSISTENCY, "setfunc called before getfunc");
249    
250     setcontext(f->ctx); /* set evaluator context */
251 greg 2.2 /* check to see if matrix set */
252 greg 2.25 if ((m == fobj) & (r->rno == lastrno))
253 greg 1.17 return(0);
254 greg 1.1 fobj = m;
255     fray = r;
256 greg 2.26 if (r->rox != NULL) {
257     if (f->bxp != &unitxf) {
258     funcxf.sca = r->rox->b.sca * f->bxp->sca;
259     multmat4(funcxf.xfm, r->rox->b.xfm, f->bxp->xfm);
260 greg 1.13 } else
261 schorsch 2.19 funcxf = r->rox->b;
262 greg 2.26 } else
263     funcxf = *f->bxp;
264     lastrno = r->rno;
265     eclock++; /* notify expression evaluator */
266     return(1);
267     }
268    
269    
270     int
271     worldfunc( /* special function context sans object */
272     char *ctx,
273     RAY *r
274     )
275     {
276     static RNUMBER lastrno = ~0;
277 greg 2.27
278     if (rayinitcal[0]) /* initialize on first call */
279     initfunc();
280 greg 2.26 /* set evaluator context */
281     setcontext(ctx);
282     /* check if ray already set */
283     if ((fobj == NULL) & (r->rno == lastrno))
284     return(0);
285     fobj = NULL;
286     fray = r;
287     funcxf = unitxf;
288 greg 1.16 lastrno = r->rno;
289 greg 1.1 eclock++; /* notify expression evaluator */
290 greg 1.17 return(1);
291 greg 1.1 }
292    
293    
294 greg 2.25 void
295 schorsch 2.22 loadfunc( /* load definition file */
296     char *fname
297     )
298 greg 1.1 {
299     char *ffname;
300    
301 greg 2.18 if ((ffname = getpath(fname, getrlibpath(), R_OK)) == NULL) {
302 greg 1.1 sprintf(errmsg, "cannot find function file \"%s\"", fname);
303 greg 2.31 error(SYSTEM, errmsg);
304 greg 1.1 }
305     fcompile(ffname);
306     }
307    
308    
309 greg 2.2 static double
310 greg 2.20 l_arg(char *nm) /* return nth real argument */
311 greg 1.1 {
312 greg 2.25 int n;
313 greg 1.1
314 greg 2.7 if (fobj == NULL)
315 greg 2.31 error(INTERNAL, "arg(n) called without a modifier context");
316 greg 2.7
317 greg 1.1 n = argument(1) + .5; /* round to integer */
318    
319     if (n < 1)
320     return(fobj->oargs.nfargs);
321    
322     if (n > fobj->oargs.nfargs) {
323     sprintf(errmsg, "missing real argument %d", n);
324     objerror(fobj, USER, errmsg);
325     }
326     return(fobj->oargs.farg[n-1]);
327 greg 1.19 }
328    
329    
330 greg 2.2 static double
331 greg 2.20 l_erf(char *nm) /* error function */
332 greg 1.19 {
333     return(erf(argument(1)));
334     }
335    
336    
337 greg 2.2 static double
338 greg 2.20 l_erfc(char *nm) /* cumulative error function */
339 greg 1.19 {
340     return(erfc(argument(1)));
341 greg 1.1 }
342    
343    
344 greg 2.25 double
345 schorsch 2.22 chanvalue( /* return channel n to calcomp */
346 greg 2.25 int n
347 schorsch 2.22 )
348 greg 1.1 {
349 greg 2.7 if (fray == NULL)
350     syntax("ray parameter used in constant expression");
351 greg 1.1
352 greg 1.10 if (--n < 0)
353     goto badchan;
354 greg 1.1
355 greg 2.17 if (n <= 2) /* ray direction */
356 greg 1.1
357 greg 1.12 return( ( fray->rdir[0]*funcxf.xfm[0][n] +
358     fray->rdir[1]*funcxf.xfm[1][n] +
359     fray->rdir[2]*funcxf.xfm[2][n] )
360     / funcxf.sca );
361 greg 1.1
362 greg 2.17 if (n <= 5) /* surface normal */
363 greg 1.8
364 greg 1.12 return( ( fray->ron[0]*funcxf.xfm[0][n-3] +
365     fray->ron[1]*funcxf.xfm[1][n-3] +
366     fray->ron[2]*funcxf.xfm[2][n-3] )
367     / funcxf.sca );
368 greg 1.8
369 greg 2.28 if (n <= 8) { /* intersection point */
370     if (fray->rot >= FHUGE)
371     return(0.0); /* XXX should be runtime error? */
372 greg 1.8
373 greg 1.12 return( fray->rop[0]*funcxf.xfm[0][n-6] +
374     fray->rop[1]*funcxf.xfm[1][n-6] +
375     fray->rop[2]*funcxf.xfm[2][n-6] +
376     funcxf.xfm[3][n-6] );
377 greg 2.28 }
378 greg 1.8
379 greg 2.13 if (n == 9) /* total distance */
380     return(raydist(fray,PRIMARY) * funcxf.sca);
381 greg 1.10
382 greg 1.18 if (n == 10) /* dot product (range [-1,1]) */
383     return( fray->rod <= -1.0 ? -1.0 :
384     fray->rod >= 1.0 ? 1.0 :
385     fray->rod );
386 greg 1.10
387 greg 1.8 if (n == 11) /* scale */
388 greg 1.12 return(funcxf.sca);
389 greg 1.8
390 greg 2.17 if (n <= 14) /* origin */
391 greg 1.12 return(funcxf.xfm[3][n-12]);
392 greg 1.8
393 greg 2.17 if (n <= 17) /* i unit vector */
394 greg 1.12 return(funcxf.xfm[0][n-15] / funcxf.sca);
395 greg 1.8
396 greg 2.17 if (n <= 20) /* j unit vector */
397     return(funcxf.xfm[1][n-18] / funcxf.sca);
398 greg 1.8
399 greg 2.17 if (n <= 23) /* k unit vector */
400 greg 1.12 return(funcxf.xfm[2][n-21] / funcxf.sca);
401 greg 2.6
402 greg 2.17 if (n == 24) /* single ray (shadow) distance */
403 greg 2.13 return((fray->rot+raydist(fray->parent,SHADOW)) * funcxf.sca);
404 greg 2.16
405 greg 2.17 if (n <= 26) /* local (u,v) coordinates */
406     return(fray->uv[n-25]);
407 greg 1.10 badchan:
408     error(USER, "illegal channel number");
409 greg 2.16 return(0.0);
410 greg 1.1 }