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

Comparing ray/src/rt/func.c (file contents):
Revision 2.27 by greg, Wed Jun 27 16:29:26 2012 UTC vs.
Revision 2.46 by greg, Sat Dec 6 04:13:14 2025 UTC

# Line 11 | Line 11 | static const char      RCSid[] = "$Id$";
11   #include  "paths.h"
12   #include  "otypes.h"
13   #include  "func.h"
14 + #include <ctype.h>
15  
16  
17   #define  INITFILE       "rayinit.cal"
# Line 36 | Line 37 | static double  l_erf(char *), l_erfc(char *), l_arg(ch
37  
38  
39   void
40 < initfunc()      /* initialize function evaluation */
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("");
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);
# Line 59 | Line 60 | initfunc()     /* initialize function evaluation */
60          setprismfuncs();
61          loadfunc(rayinitcal);
62          rayinitcal[0] = '\0';
63 +        doptimize(1);           /* optimize definitions */
64   }
65  
66  
67 + /* Set parameters for current evaluation */
68 + void
69 + set_eparams(const char *prms)
70 + {
71 +        static const char       *last_params = NULL;
72 +        char                    vname[RMAXWORD];
73 +        double                  value;
74 +        char                    *cpd;
75 +                                        /* check if already set */
76 +        if (prms == NULL || !*prms)
77 +                return;
78 +        if (prms == last_params || (last_params != NULL &&
79 +                                        !strcmp(prms, last_params)))
80 +                return;
81 +        last_params = prms;             /* XXX assumes static string */
82 +                                        /* assign each variable */
83 +        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 +                        if (!isid(*prms) | (cpd-vname >= RMAXWORD-1))
92 +                                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 +                if ((prms = fskip((char *)prms)) == NULL)
101 +                        goto bad_params;
102 +                while (isspace(*prms)) prms++;
103 +                prms += (*prms == ',') | (*prms == ';') | (*prms == ':');
104 +                varset(vname, '=', value);
105 +        }
106 +        eclock++;               /* notify expression evaluator */
107 +        return;
108 + bad_params:
109 +        sprintf(errmsg, "bad parameter list '%s'", last_params);
110 +        error(USER, errmsg);
111 + }
112 +
113 +
114   MFUNC *
115   getfunc(        /* get function for this modifier */
116          OBJREC  *m,
# Line 79 | Line 128 | getfunc(       /* get function for this modifier */
128          if ((f = (MFUNC *)m->os) != NULL)
129                  return(f);
130          fobj = NULL; fray = NULL;
82        if (rayinitcal[0])              /* initialize on first call */
83                initfunc();
131          if ((na = m->oargs.nsargs) <= ff)
132                  goto toofew;
133          arg = m->oargs.sarg;
# Line 88 | Line 135 | getfunc(       /* get function for this modifier */
135                  goto memerr;
136          i = strlen(arg[ff]);                    /* set up context */
137          if (i == 1 && arg[ff][0] == '.') {
138 <                setcontext(f->ctx = "");        /* "." means no file */
138 >                calcontext(f->ctx = "");        /* "." means no file */
139          } else {
140                  strcpy(sbuf,arg[ff]);           /* file name is context */
141                  if (i > LCALSUF && !strcmp(sbuf+i-LCALSUF, CALSUF))
142                          sbuf[i-LCALSUF] = '\0'; /* remove suffix */
143 <                setcontext(f->ctx = savestr(sbuf));
143 >                calcontext(f->ctx = savestr(sbuf));
144                  if (!vardefined(REFVNAME)) {    /* file loaded? */
145                          loadfunc(arg[ff]);
146                          varset(REFVNAME, '=', 1.0);
147                  } else                          /* reference_count++ */
148                          varset(REFVNAME, '=', varvalue(REFVNAME)+1.0);
149          }
150 <        curfunc = NULL;                 /* parse expressions */
150 >        ecurfunc = NULL;                        /* parse expressions */
151          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? */
# Line 109 | Line 156 | getfunc(       /* get function for this modifier */
156                          initstr(arg[i], sbuf, 0);
157                          f->ep[ne++] = getE1();
158                          if (nextc != EOF)
159 <                                syntax("unexpected character");
159 >                                esyntax("unexpected character");
160                  }
161          if (ef)
162                  goto toofew;
163          if (i <= ff)                    /* find transform args */
164                  i = ff+1;
165 <        while (i < na && arg[i][0] != '-')
165 >        while (i < na && !isxfopt(arg[i]))
166                  i++;
167          if (i == na) {                  /* no transform */
168                  f->fxp = f->bxp = &unitxf;
# Line 155 | Line 202 | freefunc(                      /* free memory associated with modifier */
202          if ((f = (MFUNC *)m->os) == NULL)
203                  return;
204          for (i = 0; f->ep[i] != NULL; i++)
205 <                epfree(f->ep[i]);
205 >                epfree(f->ep[i],1);
206          if (f->ctx[0]) {                        /* done with definitions */
207 <                setcontext(f->ctx);
207 >                calcontext(f->ctx);
208                  i = varvalue(REFVNAME)-.5;      /* reference_count-- */
209                  if (i > 0)
210                          varset(REFVNAME, '=', (double)i);
# Line 186 | Line 233 | setfunc(                       /* set channels for function call */
233          if ((f = (MFUNC *)m->os) == NULL)
234                  objerror(m, CONSISTENCY, "setfunc called before getfunc");
235                  
236 <        setcontext(f->ctx);             /* set evaluator context */
236 >        calcontext(f->ctx);             /* set evaluator context */
237                                          /* check to see if matrix set */
238          if ((m == fobj) & (r->rno == lastrno))
239                  return(0);
# Line 208 | Line 255 | setfunc(                       /* set channels for function call */
255  
256   int
257   worldfunc(                      /* special function context sans object */
258 <        char    *ctx,
258 >        const char      *ctx,
259          RAY     *r
260   )
261   {
262          static RNUMBER  lastrno = ~0;
216
217        if (rayinitcal[0])              /* initialize on first call */
218                initfunc();
263                                          /* set evaluator context */
264 <        setcontext(ctx);
264 >        calcontext((char *)ctx);
265                                          /* check if ray already set */
266          if ((fobj == NULL) & (r->rno == lastrno))
267                  return(0);
# Line 239 | Line 283 | loadfunc(                      /* load definition file */
283  
284          if ((ffname = getpath(fname, getrlibpath(), R_OK)) == NULL) {
285                  sprintf(errmsg, "cannot find function file \"%s\"", fname);
286 <                error(USER, errmsg);
286 >                error(SYSTEM, errmsg);
287          }
288          fcompile(ffname);
289   }
# Line 251 | Line 295 | l_arg(char *nm)                        /* return nth real argument */
295          int  n;
296  
297          if (fobj == NULL)
298 <                error(USER, "arg(n) called without a context");
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 286 | Line 331 | chanvalue(                     /* return channel n to calcomp */
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 <= 2)                     /* ray direction */
339 >        if (n < 3)                      /* 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 <= 5)                     /* surface normal */
346 >        if (n < 6)                      /* 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 <= 8)                     /* intersection */
353 >        if (n < 9)                      /* intersection point */
354  
355                  return( fray->rop[0]*funcxf.xfm[0][n-6] +
356                                  fray->rop[1]*funcxf.xfm[1][n-6] +
# Line 323 | Line 368 | chanvalue(                     /* return channel n to calcomp */
368          if (n == 11)                    /* scale */
369                  return(funcxf.sca);
370  
371 <        if (n <= 14)                    /* origin */
371 >        if (n < 15)                     /* origin */
372                  return(funcxf.xfm[3][n-12]);
373  
374 <        if (n <= 17)                    /* i unit vector */
374 >        if (n < 18)                     /* i unit vector */
375                  return(funcxf.xfm[0][n-15] / funcxf.sca);
376  
377 <        if (n <= 20)                    /* j unit vector */
377 >        if (n < 21)                     /* j unit vector */
378                  return(funcxf.xfm[1][n-18] / funcxf.sca);
379  
380 <        if (n <= 23)                    /* k unit vector */
380 >        if (n < 24)                     /* k unit vector */
381                  return(funcxf.xfm[2][n-21] / funcxf.sca);
382  
383          if (n == 24)                    /* single ray (shadow) distance */
384                  return((fray->rot+raydist(fray->parent,SHADOW)) * funcxf.sca);
385  
386 <        if (n <= 26)                    /* local (u,v) coordinates */
386 >        if (n < 27)                     /* local (u,v) coordinates */
387                  return(fray->uv[n-25]);
388   badchan:
389          error(USER, "illegal channel number");

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)