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.5 by greg, Wed Apr 19 21:36:11 1989 UTC vs.
Revision 2.7 by greg, Sat Sep 26 08:10:41 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1992 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14  
15   #include  "otypes.h"
16  
17 + #include  "func.h"
18  
18 typedef struct {
19        double  xfm[4][4];              /* transform matrix */
20        double  sca;                    /* scalefactor */
21 }  XF;
19  
20 < static OBJREC  *fobj;           /* current function object */
21 < static RAY  *fray;              /* current function ray */
22 < static XF  fxf;                 /* current transformation */
20 > #define  INITFILE       "rayinit.cal"
21 > #define  REFVNAME       "`FILE_REFCNT"
22 > #define  CALSUF         ".cal"
23 > #define  LCALSUF        4
24  
25 + XF  unitxf = {                  /* identity transform */
26 +        1.0, 0.0, 0.0, 0.0,
27 +        0.0, 1.0, 0.0, 0.0,
28 +        0.0, 0.0, 1.0, 0.0,
29 +        0.0, 0.0, 0.0, 1.0,
30 +        1.0
31 + };
32  
33 < setmap(m, r, xfm, sca)          /* set channels for function call */
33 > XF  funcxf;                     /* current transformation */
34 > static OBJREC  *fobj = NULL;    /* current function object */
35 > static RAY  *fray = NULL;       /* current function ray */
36 >
37 > static double  l_erf(), l_erfc(), l_arg();
38 >
39 >
40 > MFUNC *
41 > getfunc(m, ff, ef, dofwd)       /* get function for this modifier */
42   OBJREC  *m;
43 < register RAY  *r;
44 < double  xfm[4][4];
45 < double  sca;
43 > int  ff;
44 > unsigned  ef;
45 > int  dofwd;
46   {
47 <        extern double  l_noise3(), l_noise3a(), l_noise3b(), l_noise3c();
48 <        extern double  l_hermite(), l_fnoise3(), l_arg();
49 <        extern long  eclock;
50 <        static char  *initfile = "rayinit.cal";
51 <
52 <        if (initfile != NULL) {
47 >        extern EPNODE  *curfunc;
48 >        static char  initfile[] = INITFILE;
49 >        char  sbuf[MAXSTR];
50 >        register char  **arg;
51 >        register MFUNC  *f;
52 >        int  ne, na;
53 >        register int  i;
54 >                                        /* check to see if done already */
55 >        if ((f = (MFUNC *)m->os) != NULL)
56 >                return(f);
57 >        fobj = NULL; fray = NULL;
58 >        if (initfile[0]) {              /* initialize on first call */
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 >                funset("arg", 1, '=', l_arg);
69 >                funset("erf", 1, ':', l_erf);
70 >                funset("erfc", 1, ':', l_erfc);
71 >                setnoisefuncs();
72                  loadfunc(initfile);
73 <                scompile(NULL, "Dx=$1;Dy=$2;Dz=$3;");
42 <                scompile(NULL, "Nx=$4;Ny=$5;Nz=$6;");
43 <                scompile(NULL, "Px=$7;Py=$8;Pz=$9;");
44 <                scompile(NULL, "T=$10;Rdot=$11;");
45 <                funset("arg", 1, l_arg);
46 <                funset("noise3", 3, l_noise3);
47 <                funset("noise3a", 3, l_noise3a);
48 <                funset("noise3b", 3, l_noise3b);
49 <                funset("noise3c", 3, l_noise3c);
50 <                funset("hermite", 5, l_hermite);
51 <                funset("fnoise3", 3, l_fnoise3);
52 <                initfile = NULL;
73 >                initfile[0] = '\0';
74          }
75 <        fobj = m;
76 <        fray = r;
77 <        fxf.sca = r->robs * sca;
78 <        multmat4(fxf.xfm, r->robx, xfm);
79 <        eclock++;               /* notify expression evaluator */
75 >        if ((na = m->oargs.nsargs) <= ff)
76 >                goto toofew;
77 >        arg = m->oargs.sarg;
78 >        if ((f = (MFUNC *)calloc(1, sizeof(MFUNC))) == NULL)
79 >                goto memerr;
80 >        i = strlen(arg[ff]);                    /* set up context */
81 >        if (i == 1 && arg[ff][0] == '.')
82 >                setcontext(f->ctx = "");        /* "." means no file */
83 >        else {
84 >                strcpy(sbuf,arg[ff]);   /* file name is context */
85 >                if (i > LCALSUF && !strcmp(sbuf+i-LCALSUF, CALSUF))
86 >                        sbuf[i-LCALSUF] = '\0'; /* remove suffix */
87 >                setcontext(f->ctx = savestr(sbuf));
88 >                if (!vardefined(REFVNAME)) {    /* file loaded? */
89 >                        loadfunc(arg[ff]);
90 >                        varset(REFVNAME, '=', 1.0);
91 >                } else                          /* reference_count++ */
92 >                        varset(REFVNAME, '=', varvalue(REFVNAME)+1.0);
93 >        }
94 >        curfunc = NULL;                 /* parse expressions */
95 >        sprintf(sbuf, "%s \"%s\"", ofun[m->otype].funame, m->oname);
96 >        for (i=0, ne=0; ef && i < na; i++, ef>>=1)
97 >                if (ef & 1) {                   /* flagged as an expression? */
98 >                        if (ne >= MAXEXPR)
99 >                                objerror(m, INTERNAL, "too many expressions");
100 >                        initstr(arg[i], sbuf, 0);
101 >                        f->ep[ne++] = getE1();
102 >                        if (nextc != EOF)
103 >                                syntax("unexpected character");
104 >                }
105 >        if (ef)
106 >                goto toofew;
107 >        if (i <= ff)                    /* find transform args */
108 >                i = ff+1;
109 >        while (i < na && arg[i][0] != '-')
110 >                i++;
111 >        if (i == na)                    /* no transform */
112 >                f->f = f->b = &unitxf;
113 >        else {                          /* get transform */
114 >                if ((f->b = (XF *)malloc(sizeof(XF))) == NULL)
115 >                        goto memerr;
116 >                if (invxf(f->b, na-i, arg+i) != na-i)
117 >                        objerror(m, USER, "bad transform");
118 >                if (f->b->sca < 0.0)
119 >                        f->b->sca = -f->b->sca;
120 >                if (dofwd) {                    /* do both transforms */
121 >                        if ((f->f = (XF *)malloc(sizeof(XF))) == NULL)
122 >                                goto memerr;
123 >                        xf(f->f, na-i, arg+i);
124 >                        if (f->f->sca < 0.0)
125 >                                f->f->sca = -f->f->sca;
126 >                }
127 >        }
128 >        m->os = (char *)f;
129 >        return(f);
130 > toofew:
131 >        objerror(m, USER, "too few string arguments");
132 > memerr:
133 >        error(SYSTEM, "out of memory in getfunc");
134   }
135  
136  
137 < setfunc(m, r)                           /* simplified interface to setmap */
138 < register OBJREC  *m;
64 < RAY  *r;
137 > freefunc(m)                     /* free memory associated with modifier */
138 > OBJREC  *m;
139   {
140 <        register XF  *mxf;
140 >        register MFUNC  *f;
141 >        register int  i;
142  
143 <        if ((mxf = (XF *)m->os) == NULL) {
144 <                register int  n;
145 <                register char  **sa;
146 <
147 <                for (n = m->oargs.nsargs, sa = m->oargs.sarg;
148 <                                n > 0 && **sa != '-'; n--, sa++)
149 <                        ;
150 <                mxf = (XF *)malloc(sizeof(XF));
151 <                if (mxf == NULL)
152 <                        goto memerr;
153 <                mxf->sca = 1.0;
154 <                setident4(mxf->xfm);
80 <                if (invxf(mxf->xfm, &mxf->sca, n, sa) != n)
81 <                        objerror(m, USER, "bad transform");
82 <                if (mxf->sca < 0.0)
83 <                        mxf->sca = -mxf->sca;
84 <                m->os = (char *)mxf;
143 >        if ((f = (MFUNC *)m->os) == NULL)
144 >                return;
145 >        for (i = 0; f->ep[i] != NULL; i++)
146 >                epfree(f->ep[i]);
147 >        if (f->ctx[0]) {                        /* done with definitions */
148 >                setcontext(f->ctx);
149 >                i = varvalue(REFVNAME)-.5;      /* reference_count-- */
150 >                if (i > 0)
151 >                        varset(REFVNAME, '=', (double)i);
152 >                else
153 >                        dcleanup(2);            /* remove definitions */
154 >                freestr(f->ctx);
155          }
156 <        setmap(m, r, mxf->xfm, mxf->sca);
157 <        return;
158 < memerr:
159 <        error(SYSTEM, "out of memory in setfunc");
160 < #undef  mxf
156 >        if (f->b != &unitxf)
157 >                free((char *)f->b);
158 >        if (f->f != NULL && f->f != &unitxf)
159 >                free((char *)f->f);
160 >        free((char *)f);
161 >        m->os = NULL;
162   }
163  
164  
165 + setfunc(m, r)                   /* set channels for function call */
166 + OBJREC  *m;
167 + register RAY  *r;
168 + {
169 +        static long  lastrno = -1;
170 +        register MFUNC  *f;
171 +                                        /* get function */
172 +        if ((f = (MFUNC *)m->os) == NULL)
173 +                error(m, CONSISTENCY, "setfunc called before getfunc");
174 +                                        /* set evaluator context */
175 +        setcontext(f->ctx);
176 +                                        /* check to see if matrix set */
177 +        if (m == fobj && r->rno == lastrno)
178 +                return(0);
179 +        fobj = m;
180 +        fray = r;
181 +        if (r->rox != NULL)
182 +                if (f->b != &unitxf) {
183 +                        funcxf.sca = r->rox->b.sca * f->b->sca;
184 +                        multmat4(funcxf.xfm, r->rox->b.xfm, f->b->xfm);
185 +                } else
186 +                        copystruct(&funcxf, &r->rox->b);
187 +        else
188 +                copystruct(&funcxf, f->b);
189 +        lastrno = r->rno;
190 +        eclock++;               /* notify expression evaluator */
191 +        return(1);
192 + }
193 +
194 +
195   loadfunc(fname)                 /* load definition file */
196   char  *fname;
197   {
198          extern char  *libpath;          /* library search path */
199          char  *ffname;
200  
201 <        if ((ffname = getpath(fname, libpath)) == NULL) {
201 >        if ((ffname = getpath(fname, libpath, R_OK)) == NULL) {
202                  sprintf(errmsg, "cannot find function file \"%s\"", fname);
203                  error(USER, errmsg);
204          }
# Line 105 | Line 206 | char  *fname;
206   }
207  
208  
209 < double
209 > static double
210   l_arg()                         /* return nth real argument */
211   {
212          extern double  argument();
213          register int  n;
214  
215 +        if (fobj == NULL)
216 +                syntax("arg(n) used in constant expression");
217 +
218          n = argument(1) + .5;           /* round to integer */
219  
220          if (n < 1)
# Line 124 | Line 228 | l_arg()                                /* return nth real argument */
228   }
229  
230  
231 + static double
232 + l_erf()                         /* error function */
233 + {
234 +        extern double  erf();
235 +
236 +        return(erf(argument(1)));
237 + }
238 +
239 +
240 + static double
241 + l_erfc()                        /* cumulative error function */
242 + {
243 +        extern double  erfc();
244 +
245 +        return(erfc(argument(1)));
246 + }
247 +
248 +
249   double
250   chanvalue(n)                    /* return channel n to calcomp */
251   register int  n;
252   {
253 <        double  res;
253 >        double  sum;
254          register RAY  *r;
255  
256 <        n--;                                    /* for convenience */
256 >        if (fray == NULL)
257 >                syntax("ray parameter used in constant expression");
258  
259 <        if (n < 0 || n > 10)
260 <                error(USER, "illegal channel number");
259 >        if (--n < 0)
260 >                goto badchan;
261  
262 <        if (n == 9) {                           /* distance */
262 >        if (n < 3)                      /* ray direction */
263  
264 <                res = fray->rot;
265 <                for (r = fray->parent; r != NULL; r = r->parent)
266 <                        res += r->rot;
267 <                res *= fxf.sca;
264 >                return( (       fray->rdir[0]*funcxf.xfm[0][n] +
265 >                                fray->rdir[1]*funcxf.xfm[1][n] +
266 >                                fray->rdir[2]*funcxf.xfm[2][n]  )
267 >                         / funcxf.sca );
268  
269 <        } else if (n == 10) {                   /* dot product */
269 >        if (n < 6)                      /* surface normal */
270  
271 <                res = fray->rod;
271 >                return( (       fray->ron[0]*funcxf.xfm[0][n-3] +
272 >                                fray->ron[1]*funcxf.xfm[1][n-3] +
273 >                                fray->ron[2]*funcxf.xfm[2][n-3] )
274 >                         / funcxf.sca );
275  
276 <        } else if (n < 3) {                     /* ray direction */
277 <                        res = ( fray->rdir[0]*fxf.xfm[0][n] +
278 <                                        fray->rdir[1]*fxf.xfm[1][n] +
279 <                                        fray->rdir[2]*fxf.xfm[2][n]     )
280 <                                 / fxf.sca ;
281 <        } else if (n < 6) {                     /* surface normal */
282 <                        res = ( fray->ron[0]*fxf.xfm[0][n-3] +
283 <                                        fray->ron[1]*fxf.xfm[1][n-3] +
284 <                                        fray->ron[2]*fxf.xfm[2][n-3]    )
285 <                                 / fxf.sca ;
286 <        } else {                                /* intersection */
287 <                        res =   fray->rop[0]*fxf.xfm[0][n-6] +
288 <                                        fray->rop[1]*fxf.xfm[1][n-6] +
163 <                                        fray->rop[2]*fxf.xfm[2][n-6] +
164 <                                                     fxf.xfm[3][n-6] ;
276 >        if (n < 9)                      /* intersection */
277 >
278 >                return( fray->rop[0]*funcxf.xfm[0][n-6] +
279 >                                fray->rop[1]*funcxf.xfm[1][n-6] +
280 >                                fray->rop[2]*funcxf.xfm[2][n-6] +
281 >                                             funcxf.xfm[3][n-6] );
282 >
283 >        if (n == 9) {                   /* total distance */
284 >                sum = fray->rot;
285 >                for (r = fray->parent; r != NULL; r = r->parent)
286 >                        sum += r->rot;
287 >                return(sum * funcxf.sca);
288 >
289          }
290  
291 <        return(res);
291 >        if (n == 10)                    /* dot product (range [-1,1]) */
292 >                return( fray->rod <= -1.0 ? -1.0 :
293 >                        fray->rod >= 1.0 ? 1.0 :
294 >                        fray->rod );
295 >
296 >        if (n == 11)                    /* scale */
297 >                return(funcxf.sca);
298 >
299 >        if (n < 15)                     /* origin */
300 >                return(funcxf.xfm[3][n-12]);
301 >
302 >        if (n < 18)                     /* i unit vector */
303 >                return(funcxf.xfm[0][n-15] / funcxf.sca);
304 >
305 >        if (n < 21)                     /* j unit vector */
306 >                return(funcxf.xfm[1][n-15] / funcxf.sca);
307 >
308 >        if (n < 24)                     /* k unit vector */
309 >                return(funcxf.xfm[2][n-21] / funcxf.sca);
310 >
311 >        if (n == 24) {                  /* single ray (shadow) distance */
312 >                sum = fray->rot;
313 >                for (r = fray->parent; r != NULL && r->crtype&SHADOW;
314 >                                r = r->parent)
315 >                        sum += r->rot;
316 >                return(sum * funcxf.sca);
317 >        }
318 > badchan:
319 >        error(USER, "illegal channel number");
320   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines