ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/func.c
(Generate patch)

Comparing ray/src/rt/func.c (file contents):
Revision 2.16 by greg, Tue Mar 11 17:08:55 2003 UTC vs.
Revision 2.43 by greg, Tue Apr 22 17:12:25 2025 UTC

# Line 8 | Line 8 | static const char      RCSid[] = "$Id$";
8   #include "copyright.h"
9  
10   #include  "ray.h"
11 <
11 > #include  "paths.h"
12   #include  "otypes.h"
13
13   #include  "func.h"
14 + #include <ctype.h>
15  
16  
17   #define  INITFILE       "rayinit.cal"
# Line 31 | Line 31 | XF  funcxf;                    /* current transformation */
31   static OBJREC  *fobj = NULL;    /* current function object */
32   static RAY  *fray = NULL;       /* current function ray */
33  
34 < static double  l_erf(), l_erfc(), l_arg();
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 +        calcontext("");
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(const char *prms)
69 + {
70 +        static const char       *last_params = NULL;
71 +        char                    vname[RMAXWORD];
72 +        double                  value;
73 +        char                    *cpd;
74 +                                        /* check if already set */
75 +        if (prms == NULL || !*prms)
76 +                return;
77 +        if (prms == last_params || (last_params != NULL &&
78 +                                        !strcmp(prms, last_params)))
79 +                return;
80 +        last_params = prms;             /* XXX assumes static string */
81 +                                        /* assign each variable */
82 +        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 +                        if (!isid(*prms) | (cpd-vname >= RMAXWORD-1))
91 +                                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 +                if ((prms = fskip((char *)prms)) == NULL)
100 +                        goto bad_params;
101 +                while (isspace(*prms)) prms++;
102 +                prms += (*prms == ',') | (*prms == ';') | (*prms == ':');
103 +                varset(vname, '=', value);
104 +        }
105 +        eclock++;               /* notify expression evaluator */
106 +        return;
107 + bad_params:
108 +        sprintf(errmsg, "bad parameter list '%s'", last_params);
109 +        error(USER, errmsg);
110 + }
111 +
112 +
113   MFUNC *
114 < getfunc(m, ff, ef, dofwd)       /* get function for this modifier */
115 < OBJREC  *m;
116 < int  ff;
117 < unsigned int  ef;
118 < int  dofwd;
114 > getfunc(        /* get function for this modifier */
115 >        OBJREC  *m,
116 >        int  ff,
117 >        unsigned int  ef,
118 >        int  dofwd
119 > )
120   {
44        static char  initfile[] = INITFILE;
121          char  sbuf[MAXSTR];
122 <        register char  **arg;
123 <        register MFUNC  *f;
122 >        char  **arg;
123 >        MFUNC  *f;
124          int  ne, na;
125 <        register int  i;
125 >        int  i;
126                                          /* check to see if done already */
127          if ((f = (MFUNC *)m->os) != NULL)
128                  return(f);
129          fobj = NULL; fray = NULL;
54        if (initfile[0]) {              /* initialize on first call */
55                esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW;
56                esupport &= ~(E_OUTCHAN);
57                setcontext("");
58                scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
59                scompile("Nx=$4;Ny=$5;Nz=$6;", NULL, 0);
60                scompile("Px=$7;Py=$8;Pz=$9;", NULL, 0);
61                scompile("T=$10;Ts=$25;Rdot=$11;", NULL, 0);
62                scompile("S=$12;Tx=$13;Ty=$14;Tz=$15;", NULL, 0);
63                scompile("Ix=$16;Iy=$17;Iz=$18;", NULL, 0);
64                scompile("Jx=$19;Jy=$20;Jz=$21;", NULL, 0);
65                scompile("Kx=$22;Ky=$23;Kz=$24;", NULL, 0);
66                scompile("Lu=$25;Lv=$26;", NULL, 0);
67                funset("arg", 1, '=', l_arg);
68                funset("erf", 1, ':', l_erf);
69                funset("erfc", 1, ':', l_erfc);
70                setnoisefuncs();
71                setprismfuncs();
72                loadfunc(initfile);
73                initfile[0] = '\0';
74        }
130          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 <        if (i == 1 && arg[ff][0] == '.')
137 <                setcontext(f->ctx = "");        /* "." means no file */
138 <        else {
136 >        if (i == 1 && arg[ff][0] == '.') {
137 >                calcontext(f->ctx = "");        /* "." means no file */
138 >        } else {
139                  strcpy(sbuf,arg[ff]);           /* file name is context */
140                  if (i > LCALSUF && !strcmp(sbuf+i-LCALSUF, CALSUF))
141                          sbuf[i-LCALSUF] = '\0'; /* remove suffix */
142 <                setcontext(f->ctx = savestr(sbuf));
142 >                calcontext(f->ctx = savestr(sbuf));
143                  if (!vardefined(REFVNAME)) {    /* file loaded? */
144                          loadfunc(arg[ff]);
145                          varset(REFVNAME, '=', 1.0);
146                  } else                          /* reference_count++ */
147                          varset(REFVNAME, '=', varvalue(REFVNAME)+1.0);
148          }
149 <        curfunc = NULL;                 /* parse expressions */
149 >        ecurfunc = NULL;                        /* parse expressions */
150          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? */
# Line 100 | Line 155 | int  dofwd;
155                          initstr(arg[i], sbuf, 0);
156                          f->ep[ne++] = getE1();
157                          if (nextc != EOF)
158 <                                syntax("unexpected character");
158 >                                esyntax("unexpected character");
159                  }
160          if (ef)
161                  goto toofew;
# Line 108 | Line 163 | int  dofwd;
163                  i = ff+1;
164          while (i < na && arg[i][0] != '-')
165                  i++;
166 <        if (i == na)                    /* no transform */
167 <                f->f = f->b = &unitxf;
168 <        else {                          /* get transform */
169 <                if ((f->b = (XF *)malloc(sizeof(XF))) == NULL)
166 >        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                          goto memerr;
171 <                if (invxf(f->b, na-i, arg+i) != na-i)
171 >                if (invxf(f->bxp, na-i, arg+i) != na-i)
172                          objerror(m, USER, "bad transform");
173 <                if (f->b->sca < 0.0)
174 <                        f->b->sca = -f->b->sca;
173 >                if (f->bxp->sca < 0.0)
174 >                        f->bxp->sca = -f->bxp->sca;
175                  if (dofwd) {                    /* do both transforms */
176 <                        if ((f->f = (XF *)malloc(sizeof(XF))) == NULL)
176 >                        if ((f->fxp = (XF *)malloc(sizeof(XF))) == NULL)
177                                  goto memerr;
178 <                        xf(f->f, na-i, arg+i);
179 <                        if (f->f->sca < 0.0)
180 <                                f->f->sca = -f->f->sca;
178 >                        xf(f->fxp, na-i, arg+i);
179 >                        if (f->fxp->sca < 0.0)
180 >                                f->fxp->sca = -f->fxp->sca;
181                  }
182          }
183          m->os = (char *)f;
# Line 131 | Line 186 | toofew:
186          objerror(m, USER, "too few string arguments");
187   memerr:
188          error(SYSTEM, "out of memory in getfunc");
189 +        return NULL; /* pro forma return */
190   }
191  
192  
193   void
194 < freefunc(m)                     /* free memory associated with modifier */
195 < OBJREC  *m;
194 > freefunc(                       /* free memory associated with modifier */
195 >        OBJREC  *m
196 > )
197   {
198 <        register MFUNC  *f;
199 <        register int  i;
198 >        MFUNC  *f;
199 >        int  i;
200  
201          if ((f = (MFUNC *)m->os) == NULL)
202                  return;
203          for (i = 0; f->ep[i] != NULL; i++)
204 <                epfree(f->ep[i]);
204 >                epfree(f->ep[i],1);
205          if (f->ctx[0]) {                        /* done with definitions */
206 <                setcontext(f->ctx);
206 >                calcontext(f->ctx);
207                  i = varvalue(REFVNAME)-.5;      /* reference_count-- */
208                  if (i > 0)
209                          varset(REFVNAME, '=', (double)i);
# Line 154 | Line 211 | OBJREC  *m;
211                          dcleanup(2);            /* remove definitions */
212                  freestr(f->ctx);
213          }
214 <        if (f->b != &unitxf)
215 <                free((void *)f->b);
216 <        if (f->f != NULL && f->f != &unitxf)
217 <                free((void *)f->f);
214 >        if (f->bxp != &unitxf)
215 >                free((void *)f->bxp);
216 >        if ((f->fxp != NULL) & (f->fxp != &unitxf))
217 >                free((void *)f->fxp);
218          free((void *)f);
219          m->os = NULL;
220   }
221  
222  
223   int
224 < setfunc(m, r)                   /* set channels for function call */
225 < OBJREC  *m;
226 < register RAY  *r;
224 > setfunc(                        /* set channels for function call */
225 >        OBJREC  *m,
226 >        RAY     *r
227 > )
228   {
229 <        static unsigned long  lastrno = ~0;
230 <        register MFUNC  *f;
231 <                                        /* get function */
229 >        static RNUMBER  lastrno = ~0;
230 >        MFUNC           *f;
231 >                                        /* get function if any */
232          if ((f = (MFUNC *)m->os) == NULL)
233                  objerror(m, CONSISTENCY, "setfunc called before getfunc");
234 <                                        /* set evaluator context */
235 <        setcontext(f->ctx);
234 >                
235 >        calcontext(f->ctx);             /* set evaluator context */
236                                          /* check to see if matrix set */
237 <        if (m == fobj && r->rno == lastrno)
237 >        if ((m == fobj) & (r->rno == lastrno))
238                  return(0);
239          fobj = m;
240          fray = r;
241 <        if (r->rox != NULL)
242 <                if (f->b != &unitxf) {
243 <                        funcxf.sca = r->rox->b.sca * f->b->sca;
244 <                        multmat4(funcxf.xfm, r->rox->b.xfm, f->b->xfm);
241 >        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                  } else
246 <                        copystruct(&funcxf, &r->rox->b);
247 <        else
248 <                copystruct(&funcxf, f->b);
246 >                        funcxf = r->rox->b;
247 >        } 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 +        const char      *ctx,
258 +        RAY     *r
259 + )
260 + {
261 +        static RNUMBER  lastrno = ~0;
262 +                                        /* set evaluator context */
263 +        calcontext((char *)ctx);
264 +                                        /* 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 +        lastrno = r->rno;
271 +        eclock++;               /* notify expression evaluator */
272 +        return(1);
273 + }
274 +
275 +
276   void
277 < loadfunc(fname)                 /* load definition file */
278 < char  *fname;
277 > loadfunc(                       /* load definition file */
278 >        char  *fname
279 > )
280   {
281          char  *ffname;
282  
283 <        if ((ffname = getpath(fname, getlibpath(), R_OK)) == NULL) {
283 >        if ((ffname = getpath(fname, getrlibpath(), R_OK)) == NULL) {
284                  sprintf(errmsg, "cannot find function file \"%s\"", fname);
285 <                error(USER, errmsg);
285 >                error(SYSTEM, errmsg);
286          }
287          fcompile(ffname);
288 +        doptimize(1);           /* optimize definitions */
289   }
290  
291  
292   static double
293 < l_arg()                         /* return nth real argument */
293 > l_arg(char *nm)                 /* return nth real argument */
294   {
295 <        register int  n;
295 >        int  n;
296  
297          if (fobj == NULL)
298 <                syntax("arg(n) used in constant expression");
298 >                error(USER,
299 >                        "bad call to arg(n) - illegal constant in .cal file?");
300  
301          n = argument(1) + .5;           /* round to integer */
302  
# Line 230 | Line 312 | l_arg()                                /* return nth real argument */
312  
313  
314   static double
315 < l_erf()                         /* error function */
315 > l_erf(char *nm)                 /* error function */
316   {
235        extern double  erf();
236
317          return(erf(argument(1)));
318   }
319  
320  
321   static double
322 < l_erfc()                        /* cumulative error function */
322 > l_erfc(char *nm)                /* cumulative error function */
323   {
244        extern double  erfc();
245
324          return(erfc(argument(1)));
325   }
326  
327  
328   double
329 < chanvalue(n)                    /* return channel n to calcomp */
330 < register int  n;
329 > chanvalue(                      /* return channel n to calcomp */
330 >        int  n
331 > )
332   {
333          if (fray == NULL)
334 <                syntax("ray parameter used in constant expression");
334 >                esyntax("ray parameter used in constant expression");
335  
336          if (--n < 0)
337                  goto badchan;
338  
339 <        if (n < 3)                      /* ray direction */
339 >        if (n <= 2)                     /* ray direction */
340  
341                  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  
346 <        if (n < 6)                      /* surface normal */
346 >        if (n <= 5)                     /* surface normal */
347  
348                  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  
353 <        if (n < 9)                      /* intersection */
353 >        if (n <= 8) {                   /* intersection point */
354 >                if (fray->rot >= FHUGE*.99)
355 >                        return(0.0);    /* XXX should be runtime error? */
356  
357                  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 +        }
362  
363          if (n == 9)                     /* total distance */
364                  return(raydist(fray,PRIMARY) * funcxf.sca);
# Line 289 | Line 371 | register int  n;
371          if (n == 11)                    /* scale */
372                  return(funcxf.sca);
373  
374 <        if (n < 15)                     /* origin */
374 >        if (n <= 14)                    /* origin */
375                  return(funcxf.xfm[3][n-12]);
376  
377 <        if (n < 18)                     /* i unit vector */
377 >        if (n <= 17)                    /* i unit vector */
378                  return(funcxf.xfm[0][n-15] / funcxf.sca);
379  
380 <        if (n < 21)                     /* j unit vector */
381 <                return(funcxf.xfm[1][n-15] / funcxf.sca);
380 >        if (n <= 20)                    /* j unit vector */
381 >                return(funcxf.xfm[1][n-18] / funcxf.sca);
382  
383 <        if (n < 24)                     /* k unit vector */
383 >        if (n <= 23)                    /* k unit vector */
384                  return(funcxf.xfm[2][n-21] / funcxf.sca);
385  
386 <        if (n < 25)                     /* single ray (shadow) distance */
386 >        if (n == 24)                    /* single ray (shadow) distance */
387                  return((fray->rot+raydist(fray->parent,SHADOW)) * funcxf.sca);
388  
389 <        if (n < 27)                     /* local (u,v) coordinates */
390 <                return(fray->uv[n-26]);
389 >        if (n <= 26)                    /* local (u,v) coordinates */
390 >                return(fray->uv[n-25]);
391   badchan:
392          error(USER, "illegal channel number");
393          return(0.0);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines