ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/func.c
Revision: 2.27
Committed: Wed Jun 27 16:29:26 2012 UTC (11 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad4R2, rad4R2P1
Changes since 2.26: +4 -1 lines
Log Message:
Added missing initialization call

File Contents

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