ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/func.c
Revision: 2.43
Committed: Tue Apr 22 17:12:25 2025 UTC (9 days, 22 hours ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.42: +1 -6 lines
Log Message:
feat(rpict,rtrace,rvu,rxpict,rxtrace,rxpiece): Added -e expr and -f file.cal options

File Contents

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