ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/Development/ray/src/rt/func.c
Revision: 2.46
Committed: Sat Dec 6 04:13:14 2025 UTC (2 weeks, 1 day ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.45: +2 -2 lines
Log Message:
perf: Removed unnecessary call to doptimize(1) after every file load

File Contents

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