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.21 by greg, Thu Aug 8 13:08:01 1991 UTC vs.
Revision 2.26 by greg, Sat Jun 9 07:16:47 2012 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  
15  
16   #define  INITFILE       "rayinit.cal"
17 < #define  DEFVNAME       "FILE_LOADED`"
17 > #define  CALSUF         ".cal"
18 > #define  LCALSUF        4
19 > char  REFVNAME[] = "`FILE_REFCNT";
20  
21   XF  unitxf = {                  /* identity transform */
22 <        1.0, 0.0, 0.0, 0.0,
23 <        0.0, 1.0, 0.0, 0.0,
24 <        0.0, 0.0, 1.0, 0.0,
25 <        0.0, 0.0, 0.0, 1.0,
22 >        {{1.0, 0.0, 0.0, 0.0},
23 >        {0.0, 1.0, 0.0, 0.0},
24 >        {0.0, 0.0, 1.0, 0.0},
25 >        {0.0, 0.0, 0.0, 1.0}},
26          1.0
27   };
28  
# Line 30 | Line 30 | XF  funcxf;                    /* current transformation */
30   static OBJREC  *fobj = NULL;    /* current function object */
31   static RAY  *fray = NULL;       /* current function ray */
32  
33 + static char  rayinitcal[] = INITFILE;
34  
35 < setmap(m, r, bx)                /* set channels for function call */
36 < OBJREC  *m;
37 < register RAY  *r;
38 < XF  *bx;
35 > static double  l_erf(char *), l_erfc(char *), l_arg(char *);
36 >
37 >
38 > void
39 > initfunc()      /* initialize function evaluation */
40   {
41 <        extern long  eclock;
42 <        static long  lastrno = -1;
43 <                                        /* check to see if already set */
44 <        if (m == fobj && r->rno == lastrno)
41 >        if (!rayinitcal[0])     /* already done? */
42 >                return;
43 >        esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW;
44 >        esupport &= ~(E_OUTCHAN);
45 >        setcontext("");
46 >        scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
47 >        scompile("Nx=$4;Ny=$5;Nz=$6;", NULL, 0);
48 >        scompile("Px=$7;Py=$8;Pz=$9;", NULL, 0);
49 >        scompile("T=$10;Ts=$25;Rdot=$11;", NULL, 0);
50 >        scompile("S=$12;Tx=$13;Ty=$14;Tz=$15;", NULL, 0);
51 >        scompile("Ix=$16;Iy=$17;Iz=$18;", NULL, 0);
52 >        scompile("Jx=$19;Jy=$20;Jz=$21;", NULL, 0);
53 >        scompile("Kx=$22;Ky=$23;Kz=$24;", NULL, 0);
54 >        scompile("Lu=$26;Lv=$27;", NULL, 0);
55 >        funset("arg", 1, '=', l_arg);
56 >        funset("erf", 1, ':', l_erf);
57 >        funset("erfc", 1, ':', l_erfc);
58 >        setnoisefuncs();
59 >        setprismfuncs();
60 >        loadfunc(rayinitcal);
61 >        rayinitcal[0] = '\0';
62 > }
63 >
64 >
65 > MFUNC *
66 > getfunc(        /* get function for this modifier */
67 >        OBJREC  *m,
68 >        int  ff,
69 >        unsigned int  ef,
70 >        int  dofwd
71 > )
72 > {
73 >        char  sbuf[MAXSTR];
74 >        char  **arg;
75 >        MFUNC  *f;
76 >        int  ne, na;
77 >        int  i;
78 >                                        /* check to see if done already */
79 >        if ((f = (MFUNC *)m->os) != NULL)
80 >                return(f);
81 >        fobj = NULL; fray = NULL;
82 >        if (rayinitcal[0])              /* initialize on first call */
83 >                initfunc();
84 >        if ((na = m->oargs.nsargs) <= ff)
85 >                goto toofew;
86 >        arg = m->oargs.sarg;
87 >        if ((f = (MFUNC *)calloc(1, sizeof(MFUNC))) == NULL)
88 >                goto memerr;
89 >        i = strlen(arg[ff]);                    /* set up context */
90 >        if (i == 1 && arg[ff][0] == '.') {
91 >                setcontext(f->ctx = "");        /* "." means no file */
92 >        } else {
93 >                strcpy(sbuf,arg[ff]);           /* file name is context */
94 >                if (i > LCALSUF && !strcmp(sbuf+i-LCALSUF, CALSUF))
95 >                        sbuf[i-LCALSUF] = '\0'; /* remove suffix */
96 >                setcontext(f->ctx = savestr(sbuf));
97 >                if (!vardefined(REFVNAME)) {    /* file loaded? */
98 >                        loadfunc(arg[ff]);
99 >                        varset(REFVNAME, '=', 1.0);
100 >                } else                          /* reference_count++ */
101 >                        varset(REFVNAME, '=', varvalue(REFVNAME)+1.0);
102 >        }
103 >        curfunc = NULL;                 /* parse expressions */
104 >        sprintf(sbuf, "%s \"%s\"", ofun[m->otype].funame, m->oname);
105 >        for (i=0, ne=0; ef && i < na; i++, ef>>=1)
106 >                if (ef & 1) {                   /* flagged as an expression? */
107 >                        if (ne >= MAXEXPR)
108 >                                objerror(m, INTERNAL, "too many expressions");
109 >                        initstr(arg[i], sbuf, 0);
110 >                        f->ep[ne++] = getE1();
111 >                        if (nextc != EOF)
112 >                                syntax("unexpected character");
113 >                }
114 >        if (ef)
115 >                goto toofew;
116 >        if (i <= ff)                    /* find transform args */
117 >                i = ff+1;
118 >        while (i < na && arg[i][0] != '-')
119 >                i++;
120 >        if (i == na) {                  /* no transform */
121 >                f->fxp = f->bxp = &unitxf;
122 >        } else {                        /* get transform */
123 >                if ((f->bxp = (XF *)malloc(sizeof(XF))) == NULL)
124 >                        goto memerr;
125 >                if (invxf(f->bxp, na-i, arg+i) != na-i)
126 >                        objerror(m, USER, "bad transform");
127 >                if (f->bxp->sca < 0.0)
128 >                        f->bxp->sca = -f->bxp->sca;
129 >                if (dofwd) {                    /* do both transforms */
130 >                        if ((f->fxp = (XF *)malloc(sizeof(XF))) == NULL)
131 >                                goto memerr;
132 >                        xf(f->fxp, na-i, arg+i);
133 >                        if (f->fxp->sca < 0.0)
134 >                                f->fxp->sca = -f->fxp->sca;
135 >                }
136 >        }
137 >        m->os = (char *)f;
138 >        return(f);
139 > toofew:
140 >        objerror(m, USER, "too few string arguments");
141 > memerr:
142 >        error(SYSTEM, "out of memory in getfunc");
143 >        return NULL; /* pro forma return */
144 > }
145 >
146 >
147 > void
148 > freefunc(                       /* free memory associated with modifier */
149 >        OBJREC  *m
150 > )
151 > {
152 >        MFUNC  *f;
153 >        int  i;
154 >
155 >        if ((f = (MFUNC *)m->os) == NULL)
156 >                return;
157 >        for (i = 0; f->ep[i] != NULL; i++)
158 >                epfree(f->ep[i]);
159 >        if (f->ctx[0]) {                        /* done with definitions */
160 >                setcontext(f->ctx);
161 >                i = varvalue(REFVNAME)-.5;      /* reference_count-- */
162 >                if (i > 0)
163 >                        varset(REFVNAME, '=', (double)i);
164 >                else
165 >                        dcleanup(2);            /* remove definitions */
166 >                freestr(f->ctx);
167 >        }
168 >        if (f->bxp != &unitxf)
169 >                free((void *)f->bxp);
170 >        if ((f->fxp != NULL) & (f->fxp != &unitxf))
171 >                free((void *)f->fxp);
172 >        free((void *)f);
173 >        m->os = NULL;
174 > }
175 >
176 >
177 > int
178 > setfunc(                        /* set channels for function call */
179 >        OBJREC  *m,
180 >        RAY     *r
181 > )
182 > {
183 >        static RNUMBER  lastrno = ~0;
184 >        MFUNC           *f;
185 >                                        /* get function if any */
186 >        if ((f = (MFUNC *)m->os) == NULL)
187 >                objerror(m, CONSISTENCY, "setfunc called before getfunc");
188 >                
189 >        setcontext(f->ctx);             /* set evaluator context */
190 >                                        /* check to see if matrix set */
191 >        if ((m == fobj) & (r->rno == lastrno))
192                  return(0);
193          fobj = m;
194          fray = r;
195 <        if (r->rox != NULL)
196 <                if (bx != &unitxf) {
197 <                        funcxf.sca = r->rox->b.sca * bx->sca;
198 <                        multmat4(funcxf.xfm, r->rox->b.xfm, bx->xfm);
195 >        if (r->rox != NULL) {
196 >                if (f->bxp != &unitxf) {
197 >                        funcxf.sca = r->rox->b.sca * f->bxp->sca;
198 >                        multmat4(funcxf.xfm, r->rox->b.xfm, f->bxp->xfm);
199                  } else
200 <                        copystruct(&funcxf, &r->rox->b);
201 <        else
202 <                copystruct(&funcxf, bx);
200 >                        funcxf = r->rox->b;
201 >        } else
202 >                funcxf = *f->bxp;
203          lastrno = r->rno;
204          eclock++;               /* notify expression evaluator */
205          return(1);
206   }
207  
208  
209 < setfunc(m, r)                           /* simplified interface to setmap */
210 < register OBJREC  *m;
211 < RAY  *r;
209 > int
210 > worldfunc(                      /* special function context sans object */
211 >        char    *ctx,
212 >        RAY     *r
213 > )
214   {
215 <        register XF  *mxf;
216 <
217 <        if ((mxf = (XF *)m->os) == NULL) {
218 <                register int  n;
219 <                register char  **sa;
220 <
221 <                for (n = m->oargs.nsargs, sa = m->oargs.sarg;
222 <                                n > 0 && **sa != '-'; n--, sa++)
223 <                        ;
224 <                if (n == 0)
225 <                        mxf = &unitxf;
226 <                else {
76 <                        mxf = (XF *)malloc(sizeof(XF));
77 <                        if (mxf == NULL)
78 <                                goto memerr;
79 <                        if (invxf(mxf, n, sa) != n)
80 <                                objerror(m, USER, "bad transform");
81 <                        if (mxf->sca < 0.0)
82 <                                mxf->sca = -mxf->sca;
83 <                }
84 <                m->os = (char *)mxf;
85 <        }
86 <        return(setmap(m, r, mxf));
87 < memerr:
88 <        error(SYSTEM, "out of memory in setfunc");
215 >        static RNUMBER  lastrno = ~0;
216 >                                        /* set evaluator context */
217 >        setcontext(ctx);
218 >                                        /* check if ray already set */
219 >        if ((fobj == NULL) & (r->rno == lastrno))
220 >                return(0);
221 >        fobj = NULL;
222 >        fray = r;
223 >        funcxf = unitxf;
224 >        lastrno = r->rno;
225 >        eclock++;               /* notify expression evaluator */
226 >        return(1);
227   }
228  
229  
230 < loadfunc(fname)                 /* load definition file */
231 < char  *fname;
230 > void
231 > loadfunc(                       /* load definition file */
232 >        char  *fname
233 > )
234   {
95        extern char  *libpath;          /* library search path */
235          char  *ffname;
236  
237 <        if ((ffname = getpath(fname, libpath, R_OK)) == NULL) {
237 >        if ((ffname = getpath(fname, getrlibpath(), R_OK)) == NULL) {
238                  sprintf(errmsg, "cannot find function file \"%s\"", fname);
239                  error(USER, errmsg);
240          }
# Line 103 | Line 242 | char  *fname;
242   }
243  
244  
245 < double
246 < l_arg()                         /* return nth real argument */
245 > static double
246 > l_arg(char *nm)                 /* return nth real argument */
247   {
248 <        extern double  argument();
110 <        register int  n;
248 >        int  n;
249  
250 +        if (fobj == NULL)
251 +                error(USER, "arg(n) called without a context");
252 +
253          n = argument(1) + .5;           /* round to integer */
254  
255          if (n < 1)
# Line 122 | Line 263 | l_arg()                                /* return nth real argument */
263   }
264  
265  
266 < double
267 < l_erf()                         /* error function */
266 > static double
267 > l_erf(char *nm)                 /* error function */
268   {
128        extern double  erf();
129
269          return(erf(argument(1)));
270   }
271  
272  
273 < double
274 < l_erfc()                        /* cumulative error function */
273 > static double
274 > l_erfc(char *nm)                /* cumulative error function */
275   {
137        extern double  erfc();
138
276          return(erfc(argument(1)));
277   }
278  
279  
143 funcfile(fname)                 /* set context, load file if necessary */
144 char  *fname;
145 {
146        extern char  *setcontext();
147        static char  initfile[] = INITFILE;
148
149        if (initfile[0]) {              /* initialize on first call */
150                setcontext("");
151                scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
152                scompile("Nx=$4;Ny=$5;Nz=$6;", NULL, 0);
153                scompile("Px=$7;Py=$8;Pz=$9;", NULL, 0);
154                scompile("T=$10;Rdot=$11;", NULL, 0);
155                scompile("S=$12;Tx=$13;Ty=$14;Tz=$15;", NULL, 0);
156                scompile("Ix=$16;Iy=$17;Iz=$18;", NULL, 0);
157                scompile("Jx=$19;Jy=$20;Jz=$21;", NULL, 0);
158                scompile("Kx=$22;Ky=$23;Kz=$24;", NULL, 0);
159                funset("arg", 1, '=', l_arg);
160                funset("erf", 1, ':', l_erf);
161                funset("erfc", 1, ':', l_erfc);
162                setnoisefuncs();
163                loadfunc(initfile);
164                initfile[0] = '\0';
165        }
166        setcontext(fname);
167        if (!vardefined(DEFVNAME)) {
168                loadfunc(fname);
169                varset(DEFVNAME, ':', 1.0);
170        }
171 }
172
173
280   double
281 < chanvalue(n)                    /* return channel n to calcomp */
282 < register int  n;
281 > chanvalue(                      /* return channel n to calcomp */
282 >        int  n
283 > )
284   {
285 <        double  sum;
286 <        register RAY  *r;
285 >        if (fray == NULL)
286 >                syntax("ray parameter used in constant expression");
287  
288          if (--n < 0)
289                  goto badchan;
290  
291 <        if (n < 3)                      /* ray direction */
291 >        if (n <= 2)                     /* ray direction */
292  
293                  return( (       fray->rdir[0]*funcxf.xfm[0][n] +
294                                  fray->rdir[1]*funcxf.xfm[1][n] +
295                                  fray->rdir[2]*funcxf.xfm[2][n]  )
296                           / funcxf.sca );
297  
298 <        if (n < 6)                      /* surface normal */
298 >        if (n <= 5)                     /* surface normal */
299  
300                  return( (       fray->ron[0]*funcxf.xfm[0][n-3] +
301                                  fray->ron[1]*funcxf.xfm[1][n-3] +
302                                  fray->ron[2]*funcxf.xfm[2][n-3] )
303                           / funcxf.sca );
304  
305 <        if (n < 9)                      /* intersection */
305 >        if (n <= 8)                     /* intersection */
306  
307                  return( fray->rop[0]*funcxf.xfm[0][n-6] +
308                                  fray->rop[1]*funcxf.xfm[1][n-6] +
309                                  fray->rop[2]*funcxf.xfm[2][n-6] +
310                                               funcxf.xfm[3][n-6] );
311  
312 <        if (n == 9) {                   /* distance */
312 >        if (n == 9)                     /* total distance */
313 >                return(raydist(fray,PRIMARY) * funcxf.sca);
314  
207                sum = fray->rot;
208                for (r = fray->parent; r != NULL; r = r->parent)
209                        sum += r->rot;
210                return(sum * funcxf.sca);
211
212        }
315          if (n == 10)                    /* dot product (range [-1,1]) */
316                  return( fray->rod <= -1.0 ? -1.0 :
317                          fray->rod >= 1.0 ? 1.0 :
# Line 218 | Line 320 | register int  n;
320          if (n == 11)                    /* scale */
321                  return(funcxf.sca);
322  
323 <        if (n < 15)                     /* origin */
323 >        if (n <= 14)                    /* origin */
324                  return(funcxf.xfm[3][n-12]);
325  
326 <        if (n < 18)                     /* i unit vector */
326 >        if (n <= 17)                    /* i unit vector */
327                  return(funcxf.xfm[0][n-15] / funcxf.sca);
328  
329 <        if (n < 21)                     /* j unit vector */
330 <                return(funcxf.xfm[1][n-15] / funcxf.sca);
329 >        if (n <= 20)                    /* j unit vector */
330 >                return(funcxf.xfm[1][n-18] / funcxf.sca);
331  
332 <        if (n < 24)                     /* k unit vector */
332 >        if (n <= 23)                    /* k unit vector */
333                  return(funcxf.xfm[2][n-21] / funcxf.sca);
334 +
335 +        if (n == 24)                    /* single ray (shadow) distance */
336 +                return((fray->rot+raydist(fray->parent,SHADOW)) * funcxf.sca);
337 +
338 +        if (n <= 26)                    /* local (u,v) coordinates */
339 +                return(fray->uv[n-25]);
340   badchan:
341          error(USER, "illegal channel number");
342 +        return(0.0);
343   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines