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 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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: func.c,v 2.31 2016/03/22 03:56:17 greg Exp $";
3 #endif
4 /*
5 * func.c - interface to calcomp functions.
6 */
7
8 #include "copyright.h"
9
10 #include "ray.h"
11 #include "paths.h"
12 #include "otypes.h"
13 #include "func.h"
14 #include <ctype.h>
15
16
17 #define INITFILE "rayinit.cal"
18 #define CALSUF ".cal"
19 #define LCALSUF 4
20 char REFVNAME[] = "`FILE_REFCNT";
21
22 XF unitxf = { /* identity transform */
23 {{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 1.0
28 };
29
30 XF funcxf; /* current transformation */
31 static OBJREC *fobj = NULL; /* current function object */
32 static RAY *fray = NULL; /* current function ray */
33
34 static char rayinitcal[] = INITFILE;
35
36 static double l_erf(char *), l_erfc(char *), l_arg(char *);
37
38
39 void
40 initfunc(void) /* initialize function evaluation */
41 {
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 /* Set parameters for current evaluation */
67 void
68 set_eparams(char *prms)
69 {
70 static char *last_params;
71 static int lplen = 0;
72 int len;
73 char vname[RMAXWORD];
74 double value;
75 char *cpd;
76 /* check if already set */
77 if (prms == NULL || !*prms)
78 return;
79 if (lplen && !strcmp(prms, last_params))
80 return;
81 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 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 if (!isid(*prms) | (cpd-vname >= RMAXWORD-1))
103 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 MFUNC *
127 getfunc( /* get function for this modifier */
128 OBJREC *m,
129 int ff,
130 unsigned int ef,
131 int dofwd
132 )
133 {
134 char sbuf[MAXSTR];
135 char **arg;
136 MFUNC *f;
137 int ne, na;
138 int i;
139 /* check to see if done already */
140 if ((f = (MFUNC *)m->os) != NULL)
141 return(f);
142 fobj = NULL; fray = NULL;
143 if (rayinitcal[0]) /* initialize on first call */
144 initfunc();
145 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 if (i == 1 && arg[ff][0] == '.') {
152 setcontext(f->ctx = ""); /* "." means no file */
153 } else {
154 strcpy(sbuf,arg[ff]); /* file name is context */
155 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 loadfunc(arg[ff]);
160 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 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 goto memerr;
186 if (invxf(f->bxp, na-i, arg+i) != na-i)
187 objerror(m, USER, "bad transform");
188 if (f->bxp->sca < 0.0)
189 f->bxp->sca = -f->bxp->sca;
190 if (dofwd) { /* do both transforms */
191 if ((f->fxp = (XF *)malloc(sizeof(XF))) == NULL)
192 goto memerr;
193 xf(f->fxp, na-i, arg+i);
194 if (f->fxp->sca < 0.0)
195 f->fxp->sca = -f->fxp->sca;
196 }
197 }
198 m->os = (char *)f;
199 return(f);
200 toofew:
201 objerror(m, USER, "too few string arguments");
202 memerr:
203 error(SYSTEM, "out of memory in getfunc");
204 return NULL; /* pro forma return */
205 }
206
207
208 void
209 freefunc( /* free memory associated with modifier */
210 OBJREC *m
211 )
212 {
213 MFUNC *f;
214 int i;
215
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 if (f->bxp != &unitxf)
230 free((void *)f->bxp);
231 if ((f->fxp != NULL) & (f->fxp != &unitxf))
232 free((void *)f->fxp);
233 free((void *)f);
234 m->os = NULL;
235 }
236
237
238 int
239 setfunc( /* set channels for function call */
240 OBJREC *m,
241 RAY *r
242 )
243 {
244 static RNUMBER lastrno = ~0;
245 MFUNC *f;
246 /* get function if any */
247 if ((f = (MFUNC *)m->os) == NULL)
248 objerror(m, CONSISTENCY, "setfunc called before getfunc");
249
250 setcontext(f->ctx); /* set evaluator context */
251 /* check to see if matrix set */
252 if ((m == fobj) & (r->rno == lastrno))
253 return(0);
254 fobj = m;
255 fray = r;
256 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 } else
261 funcxf = r->rox->b;
262 } 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
278 if (rayinitcal[0]) /* initialize on first call */
279 initfunc();
280 /* 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 lastrno = r->rno;
289 eclock++; /* notify expression evaluator */
290 return(1);
291 }
292
293
294 void
295 loadfunc( /* load definition file */
296 char *fname
297 )
298 {
299 char *ffname;
300
301 if ((ffname = getpath(fname, getrlibpath(), R_OK)) == NULL) {
302 sprintf(errmsg, "cannot find function file \"%s\"", fname);
303 error(SYSTEM, errmsg);
304 }
305 fcompile(ffname);
306 }
307
308
309 static double
310 l_arg(char *nm) /* return nth real argument */
311 {
312 int n;
313
314 if (fobj == NULL)
315 error(INTERNAL, "arg(n) called without a modifier context");
316
317 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 }
328
329
330 static double
331 l_erf(char *nm) /* error function */
332 {
333 return(erf(argument(1)));
334 }
335
336
337 static double
338 l_erfc(char *nm) /* cumulative error function */
339 {
340 return(erfc(argument(1)));
341 }
342
343
344 double
345 chanvalue( /* return channel n to calcomp */
346 int n
347 )
348 {
349 if (fray == NULL)
350 syntax("ray parameter used in constant expression");
351
352 if (--n < 0)
353 goto badchan;
354
355 if (n <= 2) /* ray direction */
356
357 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
362 if (n <= 5) /* surface normal */
363
364 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
369 if (n <= 8) { /* intersection point */
370 if (fray->rot >= FHUGE)
371 return(0.0); /* XXX should be runtime error? */
372
373 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 }
378
379 if (n == 9) /* total distance */
380 return(raydist(fray,PRIMARY) * funcxf.sca);
381
382 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
387 if (n == 11) /* scale */
388 return(funcxf.sca);
389
390 if (n <= 14) /* origin */
391 return(funcxf.xfm[3][n-12]);
392
393 if (n <= 17) /* i unit vector */
394 return(funcxf.xfm[0][n-15] / funcxf.sca);
395
396 if (n <= 20) /* j unit vector */
397 return(funcxf.xfm[1][n-18] / funcxf.sca);
398
399 if (n <= 23) /* k unit vector */
400 return(funcxf.xfm[2][n-21] / funcxf.sca);
401
402 if (n == 24) /* single ray (shadow) distance */
403 return((fray->rot+raydist(fray->parent,SHADOW)) * funcxf.sca);
404
405 if (n <= 26) /* local (u,v) coordinates */
406 return(fray->uv[n-25]);
407 badchan:
408 error(USER, "illegal channel number");
409 return(0.0);
410 }