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 1.17 by greg, Mon Jun 17 08:51:33 1991 UTC vs.
Revision 2.34 by greg, Sat May 13 00:26:27 2017 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  func.c - interface to calcomp functions.
9 *
10 *     4/7/86
6   */
7  
8 < #include  "ray.h"
8 > #include "copyright.h"
9  
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"
18 + #define  CALSUF         ".cal"
19 + #define  LCALSUF        4
20 + char  REFVNAME[] = "`FILE_REFCNT";
21 +
22   XF  unitxf = {                  /* identity transform */
23 <        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,
23 >        {{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          1.0
28   };
29  
# Line 27 | 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 < setmap(m, r, bx)                /* set channels for function call */
37 < OBJREC  *m;
38 < register RAY  *r;
39 < XF  *bx;
36 > static double  l_erf(char *), l_erfc(char *), l_arg(char *);
37 >
38 >
39 > void
40 > initfunc(void)  /* initialize function evaluation */
41   {
42 <        extern double  l_arg();
43 <        extern long  eclock;
44 <        static char  *initfile = "rayinit.cal";
45 <        static long  lastrno = -1;
46 <                                        /* check to see if already set */
47 <        if (m == fobj && r->rno == lastrno)
48 <                return(0);
49 <                                        /* initialize if first call */
50 <        if (initfile != NULL) {
51 <                loadfunc(initfile);
52 <                scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
53 <                scompile("Nx=$4;Ny=$5;Nz=$6;", NULL, 0);
54 <                scompile("Px=$7;Py=$8;Pz=$9;", NULL, 0);
55 <                scompile("T=$10;Rdot=$11;", NULL, 0);
56 <                scompile("S=$12;Tx=$13;Ty=$14;Tz=$15;", NULL, 0);
57 <                scompile("Ix=$16;Iy=$17;Iz=$18;", NULL, 0);
58 <                scompile("Jx=$19;Jy=$20;Jz=$21;", NULL, 0);
59 <                scompile("Kx=$22;Ky=$23;Kz=$24;", NULL, 0);
60 <                funset("arg", 1, '=', l_arg);
61 <                setnoisefuncs();
62 <                initfile = NULL;
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 +                *cpd = '\0';
107 +                while (isspace(*prms)) prms++;
108 +                if (*prms++ != '=')
109 +                        goto bad_params;
110 +                value = atof(prms);
111 +                if ((prms = fskip(prms)) == NULL)
112 +                        goto bad_params;
113 +                while (isspace(*prms)) prms++;
114 +                prms += (*prms == ',') | (*prms == ';');
115 +                varset(vname, '=', value);
116 +        }
117 +        eclock++;               /* notify expression evaluator */
118 +        return;
119 + bad_params:
120 +        sprintf(errmsg, "bad parameter list '%s'", last_params);
121 +        error(USER, errmsg);
122 + }
123 +
124 +
125 + MFUNC *
126 + getfunc(        /* get function for this modifier */
127 +        OBJREC  *m,
128 +        int  ff,
129 +        unsigned int  ef,
130 +        int  dofwd
131 + )
132 + {
133 +        char  sbuf[MAXSTR];
134 +        char  **arg;
135 +        MFUNC  *f;
136 +        int  ne, na;
137 +        int  i;
138 +                                        /* check to see if done already */
139 +        if ((f = (MFUNC *)m->os) != NULL)
140 +                return(f);
141 +        fobj = NULL; fray = NULL;
142 +        if (rayinitcal[0])              /* initialize on first call */
143 +                initfunc();
144 +        if ((na = m->oargs.nsargs) <= ff)
145 +                goto toofew;
146 +        arg = m->oargs.sarg;
147 +        if ((f = (MFUNC *)calloc(1, sizeof(MFUNC))) == NULL)
148 +                goto memerr;
149 +        i = strlen(arg[ff]);                    /* set up context */
150 +        if (i == 1 && arg[ff][0] == '.') {
151 +                setcontext(f->ctx = "");        /* "." means no file */
152 +        } else {
153 +                strcpy(sbuf,arg[ff]);           /* file name is context */
154 +                if (i > LCALSUF && !strcmp(sbuf+i-LCALSUF, CALSUF))
155 +                        sbuf[i-LCALSUF] = '\0'; /* remove suffix */
156 +                setcontext(f->ctx = savestr(sbuf));
157 +                if (!vardefined(REFVNAME)) {    /* file loaded? */
158 +                        loadfunc(arg[ff]);
159 +                        varset(REFVNAME, '=', 1.0);
160 +                } else                          /* reference_count++ */
161 +                        varset(REFVNAME, '=', varvalue(REFVNAME)+1.0);
162 +        }
163 +        curfunc = NULL;                 /* parse expressions */
164 +        sprintf(sbuf, "%s \"%s\"", ofun[m->otype].funame, m->oname);
165 +        for (i=0, ne=0; ef && i < na; i++, ef>>=1)
166 +                if (ef & 1) {                   /* flagged as an expression? */
167 +                        if (ne >= MAXEXPR)
168 +                                objerror(m, INTERNAL, "too many expressions");
169 +                        initstr(arg[i], sbuf, 0);
170 +                        f->ep[ne++] = getE1();
171 +                        if (nextc != EOF)
172 +                                syntax("unexpected character");
173 +                }
174 +        if (ef)
175 +                goto toofew;
176 +        if (i <= ff)                    /* find transform args */
177 +                i = ff+1;
178 +        while (i < na && arg[i][0] != '-')
179 +                i++;
180 +        if (i == na) {                  /* no transform */
181 +                f->fxp = f->bxp = &unitxf;
182 +        } else {                        /* get transform */
183 +                if ((f->bxp = (XF *)malloc(sizeof(XF))) == NULL)
184 +                        goto memerr;
185 +                if (invxf(f->bxp, na-i, arg+i) != na-i)
186 +                        objerror(m, USER, "bad transform");
187 +                if (f->bxp->sca < 0.0)
188 +                        f->bxp->sca = -f->bxp->sca;
189 +                if (dofwd) {                    /* do both transforms */
190 +                        if ((f->fxp = (XF *)malloc(sizeof(XF))) == NULL)
191 +                                goto memerr;
192 +                        xf(f->fxp, na-i, arg+i);
193 +                        if (f->fxp->sca < 0.0)
194 +                                f->fxp->sca = -f->fxp->sca;
195 +                }
196 +        }
197 +        m->os = (char *)f;
198 +        return(f);
199 + toofew:
200 +        objerror(m, USER, "too few string arguments");
201 + memerr:
202 +        error(SYSTEM, "out of memory in getfunc");
203 +        return NULL; /* pro forma return */
204 + }
205 +
206 +
207 + void
208 + freefunc(                       /* free memory associated with modifier */
209 +        OBJREC  *m
210 + )
211 + {
212 +        MFUNC  *f;
213 +        int  i;
214 +
215 +        if ((f = (MFUNC *)m->os) == NULL)
216 +                return;
217 +        for (i = 0; f->ep[i] != NULL; i++)
218 +                epfree(f->ep[i]);
219 +        if (f->ctx[0]) {                        /* done with definitions */
220 +                setcontext(f->ctx);
221 +                i = varvalue(REFVNAME)-.5;      /* reference_count-- */
222 +                if (i > 0)
223 +                        varset(REFVNAME, '=', (double)i);
224 +                else
225 +                        dcleanup(2);            /* remove definitions */
226 +                freestr(f->ctx);
227 +        }
228 +        if (f->bxp != &unitxf)
229 +                free((void *)f->bxp);
230 +        if ((f->fxp != NULL) & (f->fxp != &unitxf))
231 +                free((void *)f->fxp);
232 +        free((void *)f);
233 +        m->os = NULL;
234 + }
235 +
236 +
237 + int
238 + setfunc(                        /* set channels for function call */
239 +        OBJREC  *m,
240 +        RAY     *r
241 + )
242 + {
243 +        static RNUMBER  lastrno = ~0;
244 +        MFUNC           *f;
245 +                                        /* get function if any */
246 +        if ((f = (MFUNC *)m->os) == NULL)
247 +                objerror(m, CONSISTENCY, "setfunc called before getfunc");
248 +                
249 +        setcontext(f->ctx);             /* set evaluator context */
250 +                                        /* check to see if matrix set */
251 +        if ((m == fobj) & (r->rno == lastrno))
252 +                return(0);
253          fobj = m;
254          fray = r;
255 <        if (r->rox != NULL)
256 <                if (bx != &unitxf) {
257 <                        funcxf.sca = r->rox->b.sca * bx->sca;
258 <                        multmat4(funcxf.xfm, r->rox->b.xfm, bx->xfm);
255 >        if (r->rox != NULL) {
256 >                if (f->bxp != &unitxf) {
257 >                        funcxf.sca = r->rox->b.sca * f->bxp->sca;
258 >                        multmat4(funcxf.xfm, r->rox->b.xfm, f->bxp->xfm);
259                  } else
260 <                        copystruct(&funcxf, &r->rox->b);
261 <        else
262 <                copystruct(&funcxf, bx);
260 >                        funcxf = r->rox->b;
261 >        } else
262 >                funcxf = *f->bxp;
263          lastrno = r->rno;
264          eclock++;               /* notify expression evaluator */
265          return(1);
266   }
267  
268  
269 < setfunc(m, r)                           /* simplified interface to setmap */
270 < register OBJREC  *m;
271 < RAY  *r;
269 > int
270 > worldfunc(                      /* special function context sans object */
271 >        char    *ctx,
272 >        RAY     *r
273 > )
274   {
275 <        register XF  *mxf;
275 >        static RNUMBER  lastrno = ~0;
276  
277 <        if ((mxf = (XF *)m->os) == NULL) {
278 <                register int  n;
279 <                register char  **sa;
280 <
281 <                for (n = m->oargs.nsargs, sa = m->oargs.sarg;
282 <                                n > 0 && **sa != '-'; n--, sa++)
283 <                        ;
284 <                if (n == 0)
285 <                        mxf = &unitxf;
286 <                else {
287 <                        mxf = (XF *)malloc(sizeof(XF));
288 <                        if (mxf == NULL)
289 <                                goto memerr;
93 <                        if (invxf(mxf, n, sa) != n)
94 <                                objerror(m, USER, "bad transform");
95 <                        if (mxf->sca < 0.0)
96 <                                mxf->sca = -mxf->sca;
97 <                }
98 <                m->os = (char *)mxf;
99 <        }
100 <        return(setmap(m, r, mxf));
101 < memerr:
102 <        error(SYSTEM, "out of memory in setfunc");
277 >        if (rayinitcal[0])              /* initialize on first call */
278 >                initfunc();
279 >                                        /* set evaluator context */
280 >        setcontext(ctx);
281 >                                        /* check if ray already set */
282 >        if ((fobj == NULL) & (r->rno == lastrno))
283 >                return(0);
284 >        fobj = NULL;
285 >        fray = r;
286 >        funcxf = unitxf;
287 >        lastrno = r->rno;
288 >        eclock++;               /* notify expression evaluator */
289 >        return(1);
290   }
291  
292  
293 < loadfunc(fname)                 /* load definition file */
294 < char  *fname;
293 > void
294 > loadfunc(                       /* load definition file */
295 >        char  *fname
296 > )
297   {
109        extern char  *libpath;          /* library search path */
298          char  *ffname;
299  
300 <        if ((ffname = getpath(fname, libpath, R_OK)) == NULL) {
300 >        if ((ffname = getpath(fname, getrlibpath(), R_OK)) == NULL) {
301                  sprintf(errmsg, "cannot find function file \"%s\"", fname);
302 <                error(USER, errmsg);
302 >                error(SYSTEM, errmsg);
303          }
304          fcompile(ffname);
305   }
306  
307  
308 < double
309 < l_arg()                         /* return nth real argument */
308 > static double
309 > l_arg(char *nm)                 /* return nth real argument */
310   {
311 <        extern double  argument();
124 <        register int  n;
311 >        int  n;
312  
313 +        if (fobj == NULL)
314 +                error(INTERNAL, "arg(n) called without a modifier context");
315 +
316          n = argument(1) + .5;           /* round to integer */
317  
318          if (n < 1)
# Line 136 | Line 326 | l_arg()                                /* return nth real argument */
326   }
327  
328  
329 + static double
330 + l_erf(char *nm)                 /* error function */
331 + {
332 +        return(erf(argument(1)));
333 + }
334 +
335 +
336 + static double
337 + l_erfc(char *nm)                /* cumulative error function */
338 + {
339 +        return(erfc(argument(1)));
340 + }
341 +
342 +
343   double
344 < chanvalue(n)                    /* return channel n to calcomp */
345 < register int  n;
344 > chanvalue(                      /* return channel n to calcomp */
345 >        int  n
346 > )
347   {
348 <        double  sum;
349 <        register RAY  *r;
348 >        if (fray == NULL)
349 >                syntax("ray parameter used in constant expression");
350  
351          if (--n < 0)
352                  goto badchan;
353  
354 <        if (n < 3)                      /* ray direction */
354 >        if (n <= 2)                     /* ray direction */
355  
356                  return( (       fray->rdir[0]*funcxf.xfm[0][n] +
357                                  fray->rdir[1]*funcxf.xfm[1][n] +
358                                  fray->rdir[2]*funcxf.xfm[2][n]  )
359                           / funcxf.sca );
360  
361 <        if (n < 6)                      /* surface normal */
361 >        if (n <= 5)                     /* surface normal */
362  
363                  return( (       fray->ron[0]*funcxf.xfm[0][n-3] +
364                                  fray->ron[1]*funcxf.xfm[1][n-3] +
365                                  fray->ron[2]*funcxf.xfm[2][n-3] )
366                           / funcxf.sca );
367  
368 <        if (n < 9)                      /* intersection */
368 >        if (n <= 8) {                   /* intersection point */
369 >                if (fray->rot >= FHUGE)
370 >                        return(0.0);    /* XXX should be runtime error? */
371  
372                  return( fray->rop[0]*funcxf.xfm[0][n-6] +
373                                  fray->rop[1]*funcxf.xfm[1][n-6] +
374                                  fray->rop[2]*funcxf.xfm[2][n-6] +
375                                               funcxf.xfm[3][n-6] );
376 +        }
377  
378 <        if (n == 9) {                   /* distance */
378 >        if (n == 9)                     /* total distance */
379 >                return(raydist(fray,PRIMARY) * funcxf.sca);
380  
381 <                sum = fray->rot;
382 <                for (r = fray->parent; r != NULL; r = r->parent)
383 <                        sum += r->rot;
384 <                return(sum * funcxf.sca);
381 >        if (n == 10)                    /* dot product (range [-1,1]) */
382 >                return( fray->rod <= -1.0 ? -1.0 :
383 >                        fray->rod >= 1.0 ? 1.0 :
384 >                        fray->rod );
385  
177        }
178        if (n == 10)                    /* dot product */
179                return(fray->rod);
180
386          if (n == 11)                    /* scale */
387                  return(funcxf.sca);
388  
389 <        if (n < 15)                     /* origin */
389 >        if (n <= 14)                    /* origin */
390                  return(funcxf.xfm[3][n-12]);
391  
392 <        if (n < 18)                     /* i unit vector */
392 >        if (n <= 17)                    /* i unit vector */
393                  return(funcxf.xfm[0][n-15] / funcxf.sca);
394  
395 <        if (n < 21)                     /* j unit vector */
396 <                return(funcxf.xfm[1][n-15] / funcxf.sca);
395 >        if (n <= 20)                    /* j unit vector */
396 >                return(funcxf.xfm[1][n-18] / funcxf.sca);
397  
398 <        if (n < 24)                     /* k unit vector */
398 >        if (n <= 23)                    /* k unit vector */
399                  return(funcxf.xfm[2][n-21] / funcxf.sca);
400 +
401 +        if (n == 24)                    /* single ray (shadow) distance */
402 +                return((fray->rot+raydist(fray->parent,SHADOW)) * funcxf.sca);
403 +
404 +        if (n <= 26)                    /* local (u,v) coordinates */
405 +                return(fray->uv[n-25]);
406   badchan:
407          error(USER, "illegal channel number");
408 +        return(0.0);
409   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines