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.23 by greg, Sat Dec 12 00:03:42 2009 UTC vs.
Revision 2.32 by greg, Wed Apr 27 21:11:32 2016 UTC

# Line 7 | Line 7 | static const char      RCSid[] = "$Id$";
7  
8   #include "copyright.h"
9  
10 #include <math.h>
11
10   #include  "ray.h"
11   #include  "paths.h"
12   #include  "otypes.h"
13   #include  "func.h"
14 + #include <ctype.h>
15  
16  
17   #define  INITFILE       "rayinit.cal"
# Line 32 | 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 char  rayinitcal[] = INITFILE;
35 +
36   static double  l_erf(char *), l_erfc(char *), l_arg(char *);
37  
38  
39 < extern MFUNC *
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 >        setcontext("");
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(char *prms)
69 > {
70 >        static char     *last_params;
71 >        static int      lplen = 0;
72 >        int             len;
73 >        char            vname[RMAXWORD];
74 >        double          value;
75 >        char            *cpd;
76 >                                        /* check if already set */
77 >        if (prms == NULL || !*prms)
78 >                return;
79 >        if (lplen && !strcmp(prms, last_params))
80 >                return;
81 >        len = strlen(prms);             /* record new settings */
82 >        if ((lplen != 0) & (lplen <= len)) {
83 >                free(last_params);
84 >                lplen = 0;
85 >        }
86 >        if (!lplen) {
87 >                lplen = len + 100;
88 >                last_params = (char *)malloc(lplen);
89 >                if (last_params == NULL)
90 >                        error(SYSTEM, "out of memory in set_eparams()");
91 >        }
92 >        strcpy(last_params, prms);
93 >                                        /* assign each variable */
94 >        while (*prms) {
95 >                if (isspace(*prms)) {
96 >                        ++prms; continue;
97 >                }
98 >                if (!isalpha(*prms))
99 >                        goto bad_params;
100 >                cpd = vname;
101 >                while (*prms && (*prms != '=') & !isspace(*prms)) {
102 >                        if (!isid(*prms) | (cpd-vname >= RMAXWORD-1))
103 >                                goto bad_params;
104 >                        *cpd++ = *prms++;
105 >                }
106 >                if (cpd == vname)
107 >                        goto bad_params;
108 >                *cpd = '\0';
109 >                while (isspace(*prms)) prms++;
110 >                if (*prms++ != '=')
111 >                        goto bad_params;
112 >                value = atof(prms);
113 >                if ((prms = fskip(prms)) == NULL)
114 >                        goto bad_params;
115 >                while (isspace(*prms)) prms++;
116 >                prms += (*prms == ',') | (*prms == ';');
117 >                varset(vname, '=', value);
118 >        }
119 >        return;
120 > bad_params:
121 >        sprintf(errmsg, "bad parameter list '%s'", last_params);
122 >        error(USER, errmsg);
123 > }
124 >
125 >
126 > MFUNC *
127   getfunc(        /* get function for this modifier */
128          OBJREC  *m,
129          int  ff,
# Line 43 | Line 131 | getfunc(       /* get function for this modifier */
131          int  dofwd
132   )
133   {
46        static char  initfile[] = INITFILE;
134          char  sbuf[MAXSTR];
135 <        register char  **arg;
136 <        register MFUNC  *f;
135 >        char  **arg;
136 >        MFUNC  *f;
137          int  ne, na;
138 <        register int  i;
138 >        int  i;
139                                          /* check to see if done already */
140          if ((f = (MFUNC *)m->os) != NULL)
141                  return(f);
142          fobj = NULL; fray = NULL;
143 <        if (initfile[0]) {              /* initialize on first call */
144 <                esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW;
58 <                esupport &= ~(E_OUTCHAN);
59 <                setcontext("");
60 <                scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
61 <                scompile("Nx=$4;Ny=$5;Nz=$6;", NULL, 0);
62 <                scompile("Px=$7;Py=$8;Pz=$9;", NULL, 0);
63 <                scompile("T=$10;Ts=$25;Rdot=$11;", NULL, 0);
64 <                scompile("S=$12;Tx=$13;Ty=$14;Tz=$15;", NULL, 0);
65 <                scompile("Ix=$16;Iy=$17;Iz=$18;", NULL, 0);
66 <                scompile("Jx=$19;Jy=$20;Jz=$21;", NULL, 0);
67 <                scompile("Kx=$22;Ky=$23;Kz=$24;", NULL, 0);
68 <                scompile("Lu=$26;Lv=$27;", NULL, 0);
69 <                funset("arg", 1, '=', l_arg);
70 <                funset("erf", 1, ':', l_erf);
71 <                funset("erfc", 1, ':', l_erfc);
72 <                setnoisefuncs();
73 <                setprismfuncs();
74 <                loadfunc(initfile);
75 <                initfile[0] = '\0';
76 <        }
143 >        if (rayinitcal[0])              /* initialize on first call */
144 >                initfunc();
145          if ((na = m->oargs.nsargs) <= ff)
146                  goto toofew;
147          arg = m->oargs.sarg;
148          if ((f = (MFUNC *)calloc(1, sizeof(MFUNC))) == NULL)
149                  goto memerr;
150          i = strlen(arg[ff]);                    /* set up context */
151 <        if (i == 1 && arg[ff][0] == '.')
151 >        if (i == 1 && arg[ff][0] == '.') {
152                  setcontext(f->ctx = "");        /* "." means no file */
153 <        else {
153 >        } else {
154                  strcpy(sbuf,arg[ff]);           /* file name is context */
155                  if (i > LCALSUF && !strcmp(sbuf+i-LCALSUF, CALSUF))
156                          sbuf[i-LCALSUF] = '\0'; /* remove suffix */
# Line 110 | Line 178 | getfunc(       /* get function for this modifier */
178                  i = ff+1;
179          while (i < na && arg[i][0] != '-')
180                  i++;
181 <        if (i == na)                    /* no transform */
182 <                f->f = f->b = &unitxf;
183 <        else {                          /* get transform */
184 <                if ((f->b = (XF *)malloc(sizeof(XF))) == NULL)
181 >        if (i == na) {                  /* no transform */
182 >                f->fxp = f->bxp = &unitxf;
183 >        } else {                        /* get transform */
184 >                if ((f->bxp = (XF *)malloc(sizeof(XF))) == NULL)
185                          goto memerr;
186 <                if (invxf(f->b, na-i, arg+i) != na-i)
186 >                if (invxf(f->bxp, na-i, arg+i) != na-i)
187                          objerror(m, USER, "bad transform");
188 <                if (f->b->sca < 0.0)
189 <                        f->b->sca = -f->b->sca;
188 >                if (f->bxp->sca < 0.0)
189 >                        f->bxp->sca = -f->bxp->sca;
190                  if (dofwd) {                    /* do both transforms */
191 <                        if ((f->f = (XF *)malloc(sizeof(XF))) == NULL)
191 >                        if ((f->fxp = (XF *)malloc(sizeof(XF))) == NULL)
192                                  goto memerr;
193 <                        xf(f->f, na-i, arg+i);
194 <                        if (f->f->sca < 0.0)
195 <                                f->f->sca = -f->f->sca;
193 >                        xf(f->fxp, na-i, arg+i);
194 >                        if (f->fxp->sca < 0.0)
195 >                                f->fxp->sca = -f->fxp->sca;
196                  }
197          }
198          m->os = (char *)f;
# Line 137 | Line 205 | memerr:
205   }
206  
207  
208 < extern void
208 > void
209   freefunc(                       /* free memory associated with modifier */
210          OBJREC  *m
211   )
212   {
213 <        register MFUNC  *f;
214 <        register int  i;
213 >        MFUNC  *f;
214 >        int  i;
215  
216          if ((f = (MFUNC *)m->os) == NULL)
217                  return;
# Line 158 | Line 226 | freefunc(                      /* free memory associated with modifier */
226                          dcleanup(2);            /* remove definitions */
227                  freestr(f->ctx);
228          }
229 <        if (f->b != &unitxf)
230 <                free((void *)f->b);
231 <        if (f->f != NULL && f->f != &unitxf)
232 <                free((void *)f->f);
229 >        if (f->bxp != &unitxf)
230 >                free((void *)f->bxp);
231 >        if ((f->fxp != NULL) & (f->fxp != &unitxf))
232 >                free((void *)f->fxp);
233          free((void *)f);
234          m->os = NULL;
235   }
236  
237  
238 < extern int
238 > int
239   setfunc(                        /* set channels for function call */
240 <        OBJREC  *m,
241 <        register RAY  *r
240 >        OBJREC  *m,
241 >        RAY     *r
242   )
243   {
244 <        static RNUMBER  lastrno = ~0;
245 <        register MFUNC  *f;
246 <                                        /* get function */
244 >        static RNUMBER  lastrno = ~0;
245 >        MFUNC           *f;
246 >                                        /* get function if any */
247          if ((f = (MFUNC *)m->os) == NULL)
248                  objerror(m, CONSISTENCY, "setfunc called before getfunc");
249 <                                        /* set evaluator context */
250 <        setcontext(f->ctx);
249 >                
250 >        setcontext(f->ctx);             /* set evaluator context */
251                                          /* check to see if matrix set */
252 <        if (m == fobj && r->rno == lastrno)
252 >        if ((m == fobj) & (r->rno == lastrno))
253                  return(0);
254          fobj = m;
255          fray = r;
256 <        if (r->rox != NULL)
257 <                if (f->b != &unitxf) {
258 <                        funcxf.sca = r->rox->b.sca * f->b->sca;
259 <                        multmat4(funcxf.xfm, r->rox->b.xfm, f->b->xfm);
256 >        if (r->rox != NULL) {
257 >                if (f->bxp != &unitxf) {
258 >                        funcxf.sca = r->rox->b.sca * f->bxp->sca;
259 >                        multmat4(funcxf.xfm, r->rox->b.xfm, f->bxp->xfm);
260                  } else
261                          funcxf = r->rox->b;
262 <        else
263 <                funcxf = *(f->b);
262 >        } else
263 >                funcxf = *f->bxp;
264          lastrno = r->rno;
265          eclock++;               /* notify expression evaluator */
266          return(1);
267   }
268  
269  
270 < extern void
270 > int
271 > worldfunc(                      /* special function context sans object */
272 >        char    *ctx,
273 >        RAY     *r
274 > )
275 > {
276 >        static RNUMBER  lastrno = ~0;
277 >
278 >        if (rayinitcal[0])              /* initialize on first call */
279 >                initfunc();
280 >                                        /* set evaluator context */
281 >        setcontext(ctx);
282 >                                        /* check if ray already set */
283 >        if ((fobj == NULL) & (r->rno == lastrno))
284 >                return(0);
285 >        fobj = NULL;
286 >        fray = r;
287 >        funcxf = unitxf;
288 >        lastrno = r->rno;
289 >        eclock++;               /* notify expression evaluator */
290 >        return(1);
291 > }
292 >
293 >
294 > void
295   loadfunc(                       /* load definition file */
296          char  *fname
297   )
# Line 208 | Line 300 | loadfunc(                      /* load definition file */
300  
301          if ((ffname = getpath(fname, getrlibpath(), R_OK)) == NULL) {
302                  sprintf(errmsg, "cannot find function file \"%s\"", fname);
303 <                error(USER, errmsg);
303 >                error(SYSTEM, errmsg);
304          }
305          fcompile(ffname);
306   }
# Line 217 | Line 309 | loadfunc(                      /* load definition file */
309   static double
310   l_arg(char *nm)                 /* return nth real argument */
311   {
312 <        register int  n;
312 >        int  n;
313  
314          if (fobj == NULL)
315 <                syntax("arg(n) used in constant expression");
315 >                error(INTERNAL, "arg(n) called without a modifier context");
316  
317          n = argument(1) + .5;           /* round to integer */
318  
# Line 249 | Line 341 | l_erfc(char *nm)               /* cumulative error function */
341   }
342  
343  
344 < extern double
344 > double
345   chanvalue(                      /* return channel n to calcomp */
346 <        register int  n
346 >        int  n
347   )
348   {
349          if (fray == NULL)
# Line 274 | Line 366 | chanvalue(                     /* return channel n to calcomp */
366                                  fray->ron[2]*funcxf.xfm[2][n-3] )
367                           / funcxf.sca );
368  
369 <        if (n <= 8)                     /* intersection */
369 >        if (n <= 8) {                   /* intersection point */
370 >                if (fray->rot >= FHUGE)
371 >                        return(0.0);    /* XXX should be runtime error? */
372  
373                  return( fray->rop[0]*funcxf.xfm[0][n-6] +
374                                  fray->rop[1]*funcxf.xfm[1][n-6] +
375                                  fray->rop[2]*funcxf.xfm[2][n-6] +
376                                               funcxf.xfm[3][n-6] );
377 +        }
378  
379          if (n == 9)                     /* total distance */
380                  return(raydist(fray,PRIMARY) * funcxf.sca);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines