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.1 by greg, Thu Feb 2 10:41:23 1989 UTC vs.
Revision 2.5 by greg, Wed Feb 5 09:23:38 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  sca;                    /* scalefactor */
20        double  xfm[4][4];              /* transform matrix */
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, sca, xfm)          /* 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  sca;
45 < double  xfm[4][4];
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 >        if (initfile[0]) {              /* initialize on first call */
58 >                setcontext("");
59 >                scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
60 >                scompile("Nx=$4;Ny=$5;Nz=$6;", NULL, 0);
61 >                scompile("Px=$7;Py=$8;Pz=$9;", NULL, 0);
62 >                scompile("T=$10;Rdot=$11;", NULL, 0);
63 >                scompile("S=$12;Tx=$13;Ty=$14;Tz=$15;", NULL, 0);
64 >                scompile("Ix=$16;Iy=$17;Iz=$18;", NULL, 0);
65 >                scompile("Jx=$19;Jy=$20;Jz=$21;", NULL, 0);
66 >                scompile("Kx=$22;Ky=$23;Kz=$24;", NULL, 0);
67 >                funset("arg", 1, '=', l_arg);
68 >                funset("erf", 1, ':', l_erf);
69 >                funset("erfc", 1, ':', l_erfc);
70 >                setnoisefuncs();
71                  loadfunc(initfile);
72 <                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;
72 >                initfile[0] = '\0';
73          }
74 <        fobj = m;
75 <        fray = r;
76 <        fxf.sca = r->ros * sca;
77 <        multmat4(fxf.xfm, r->rox, xfm);
78 <        eclock++;               /* notify expression evaluator */
74 >        if ((na = m->oargs.nsargs) <= ff)
75 >                goto toofew;
76 >        arg = m->oargs.sarg;
77 >        if ((f = (MFUNC *)calloc(1, sizeof(MFUNC))) == NULL)
78 >                goto memerr;
79 >        i = strlen(arg[ff]);                    /* set up context */
80 >        if (i == 1 && arg[ff][0] == '.')
81 >                setcontext(f->ctx = "");        /* "." means no file */
82 >        else {
83 >                strcpy(sbuf,arg[ff]);   /* file name is context */
84 >                if (i > LCALSUF && !strcmp(sbuf+i-LCALSUF, CALSUF))
85 >                        sbuf[i-LCALSUF] = '\0'; /* remove suffix */
86 >                setcontext(f->ctx = savestr(sbuf));
87 >                if (!vardefined(REFVNAME)) {    /* file loaded? */
88 >                        loadfunc(arg[ff]);
89 >                        varset(REFVNAME, '=', 1.0);
90 >                } else                          /* reference_count++ */
91 >                        varset(REFVNAME, '=', varvalue(REFVNAME)+1.0);
92 >        }
93 >        curfunc = NULL;                 /* parse expressions */
94 >        sprintf(sbuf, "%s \"%s\"", ofun[m->otype].funame, m->oname);
95 >        for (i=0, ne=0; ef && i < na; i++, ef>>=1)
96 >                if (ef & 1) {                   /* flagged as an expression? */
97 >                        if (ne >= MAXEXPR)
98 >                                objerror(m, INTERNAL, "too many expressions");
99 >                        initstr(arg[i], sbuf, 0);
100 >                        f->ep[ne++] = getE1();
101 >                        if (nextc != EOF)
102 >                                syntax("unexpected character");
103 >                }
104 >        if (ef)
105 >                goto toofew;
106 >        if (i <= ff)                    /* find transform args */
107 >                i = ff+1;
108 >        while (i < na && arg[i][0] != '-')
109 >                i++;
110 >        if (i == na)                    /* no transform */
111 >                f->f = f->b = &unitxf;
112 >        else {                          /* get transform */
113 >                if ((f->b = (XF *)malloc(sizeof(XF))) == NULL)
114 >                        goto memerr;
115 >                if (invxf(f->b, na-i, arg+i) != na-i)
116 >                        objerror(m, USER, "bad transform");
117 >                if (f->b->sca < 0.0)
118 >                        f->b->sca = -f->b->sca;
119 >                if (dofwd) {                    /* do both transforms */
120 >                        if ((f->f = (XF *)malloc(sizeof(XF))) == NULL)
121 >                                goto memerr;
122 >                        xf(f->f, na-i, arg+i);
123 >                        if (f->f->sca < 0.0)
124 >                                f->f->sca = -f->f->sca;
125 >                }
126 >        }
127 >        m->os = (char *)f;
128 >        return(f);
129 > toofew:
130 >        objerror(m, USER, "too few string arguments");
131 > memerr:
132 >        error(SYSTEM, "out of memory in getfunc");
133   }
134  
135  
136 < setfunc(m, r)                           /* simplified interface to setmap */
137 < register OBJREC  *m;
64 < RAY  *r;
136 > freefunc(m)                     /* free memory associated with modifier */
137 > OBJREC  *m;
138   {
139 < #define  mxf    ((XF *)m->os)
139 >        register MFUNC  *f;
140 >        register int  i;
141  
142 <        if (mxf == NULL) {
143 <                register int  n = m->oargs.nsargs;
144 <                register char  **sa = m->oargs.sarg;
145 <
146 <                while (n > 0 && **sa != '-') {
147 <                        n--;
148 <                        sa++;
149 <                }
150 <                mxf = (XF *)malloc(sizeof(XF));
151 <                if (mxf == NULL)
152 <                        goto memerr;
153 <                mxf->sca = 1.0;
80 <                setident4(mxf->xfm);
81 <                if (invxf(mxf->xfm, &mxf->sca, n, sa) != n)
82 <                        objerror(m, USER, "bad transform");
83 <                if (mxf->sca < 0.0)
84 <                        mxf->sca = -mxf->sca;
142 >        if ((f = (MFUNC *)m->os) == NULL)
143 >                return;
144 >        for (i = 0; f->ep[i] != NULL; i++)
145 >                epfree(f->ep[i]);
146 >        if (f->ctx[0]) {                        /* done with definitions */
147 >                setcontext(f->ctx);
148 >                i = varvalue(REFVNAME)-.5;      /* reference_count-- */
149 >                if (i > 0)
150 >                        varset(REFVNAME, '=', (double)i);
151 >                else
152 >                        dcleanup(2);            /* remove definitions */
153 >                freestr(f->ctx);
154          }
155 <        setmap(m, r, mxf->sca, mxf->xfm);
156 <        return;
157 < memerr:
158 <        error(SYSTEM, "out of memory in setfunc");
159 < #undef  mxf
155 >        if (f->b != &unitxf)
156 >                free((char *)f->b);
157 >        if (f->f != NULL && f->f != &unitxf)
158 >                free((char *)f->f);
159 >        free((char *)f);
160 >        m->os = NULL;
161   }
162  
163  
164 + setfunc(m, r)                   /* set channels for function call */
165 + OBJREC  *m;
166 + register RAY  *r;
167 + {
168 +        static long  lastrno = -1;
169 +        register MFUNC  *f;
170 +                                        /* get function */
171 +        if ((f = (MFUNC *)m->os) == NULL)
172 +                error(m, CONSISTENCY, "setfunc called before getfunc");
173 +                                        /* set evaluator context */
174 +        setcontext(f->ctx);
175 +                                        /* check to see if matrix set */
176 +        if (m == fobj && r->rno == lastrno)
177 +                return(0);
178 +        fobj = m;
179 +        fray = r;
180 +        if (r->rox != NULL)
181 +                if (f->b != &unitxf) {
182 +                        funcxf.sca = r->rox->b.sca * f->b->sca;
183 +                        multmat4(funcxf.xfm, r->rox->b.xfm, f->b->xfm);
184 +                } else
185 +                        copystruct(&funcxf, &r->rox->b);
186 +        else
187 +                copystruct(&funcxf, f->b);
188 +        lastrno = r->rno;
189 +        eclock++;               /* notify expression evaluator */
190 +        return(1);
191 + }
192 +
193 +
194   loadfunc(fname)                 /* load definition file */
195   char  *fname;
196   {
197          extern char  *libpath;          /* library search path */
198          char  *ffname;
199  
200 <        if ((ffname = getpath(fname, libpath)) == NULL) {
200 >        if ((ffname = getpath(fname, libpath, R_OK)) == NULL) {
201                  sprintf(errmsg, "cannot find function file \"%s\"", fname);
202                  error(USER, errmsg);
203          }
# Line 105 | Line 205 | char  *fname;
205   }
206  
207  
208 < double
208 > static double
209   l_arg()                         /* return nth real argument */
210   {
211          extern double  argument();
# Line 124 | Line 224 | l_arg()                                /* return nth real argument */
224   }
225  
226  
227 + static double
228 + l_erf()                         /* error function */
229 + {
230 +        extern double  erf();
231 +
232 +        return(erf(argument(1)));
233 + }
234 +
235 +
236 + static double
237 + l_erfc()                        /* cumulative error function */
238 + {
239 +        extern double  erfc();
240 +
241 +        return(erfc(argument(1)));
242 + }
243 +
244 +
245   double
246   chanvalue(n)                    /* return channel n to calcomp */
247   register int  n;
248   {
249 <        double  res;
249 >        double  sum;
250          register RAY  *r;
251  
252 <        n--;                                    /* for convenience */
252 >        if (--n < 0)
253 >                goto badchan;
254  
255 <        if (n < 0 || n > 10)
137 <                error(USER, "illegal channel number");
255 >        if (n < 3)                      /* ray direction */
256  
257 <        if (n == 9) {                           /* distance */
257 >                return( (       fray->rdir[0]*funcxf.xfm[0][n] +
258 >                                fray->rdir[1]*funcxf.xfm[1][n] +
259 >                                fray->rdir[2]*funcxf.xfm[2][n]  )
260 >                         / funcxf.sca );
261  
262 <                res = fray->rot;
142 <                for (r = fray->parent; r != NULL; r = r->parent)
143 <                        res += r->rot;
144 <                res *= fxf.sca;
262 >        if (n < 6)                      /* surface normal */
263  
264 <        } else if (n == 10) {                   /* dot product */
264 >                return( (       fray->ron[0]*funcxf.xfm[0][n-3] +
265 >                                fray->ron[1]*funcxf.xfm[1][n-3] +
266 >                                fray->ron[2]*funcxf.xfm[2][n-3] )
267 >                         / funcxf.sca );
268  
269 <                res = fray->rod;
269 >        if (n < 9)                      /* intersection */
270  
271 <        } else if (n < 3) {                     /* ray direction */
272 <                        res = ( fray->rdir[0]*fxf.xfm[0][n] +
273 <                                        fray->rdir[1]*fxf.xfm[1][n] +
274 <                                        fray->rdir[2]*fxf.xfm[2][n]     )
275 <                                 / fxf.sca ;
276 <        } else if (n < 6) {                     /* surface normal */
277 <                        res = ( fray->ron[0]*fxf.xfm[0][n-3] +
278 <                                        fray->ron[1]*fxf.xfm[1][n-3] +
279 <                                        fray->ron[2]*fxf.xfm[2][n-3]    )
280 <                                 / fxf.sca ;
281 <        } else {                                /* intersection */
282 <                        res =   fray->rop[0]*fxf.xfm[0][n-6] +
162 <                                        fray->rop[1]*fxf.xfm[1][n-6] +
163 <                                        fray->rop[2]*fxf.xfm[2][n-6] +
164 <                                                     fxf.xfm[3][n-6] ;
271 >                return( fray->rop[0]*funcxf.xfm[0][n-6] +
272 >                                fray->rop[1]*funcxf.xfm[1][n-6] +
273 >                                fray->rop[2]*funcxf.xfm[2][n-6] +
274 >                                             funcxf.xfm[3][n-6] );
275 >
276 >        if (n == 9) {                   /* distance */
277 >
278 >                sum = fray->rot;
279 >                for (r = fray->parent; r != NULL; r = r->parent)
280 >                        sum += r->rot;
281 >                return(sum * funcxf.sca);
282 >
283          }
284 +        if (n == 10)                    /* dot product (range [-1,1]) */
285 +                return( fray->rod <= -1.0 ? -1.0 :
286 +                        fray->rod >= 1.0 ? 1.0 :
287 +                        fray->rod );
288  
289 <        return(res);
289 >        if (n == 11)                    /* scale */
290 >                return(funcxf.sca);
291 >
292 >        if (n < 15)                     /* origin */
293 >                return(funcxf.xfm[3][n-12]);
294 >
295 >        if (n < 18)                     /* i unit vector */
296 >                return(funcxf.xfm[0][n-15] / funcxf.sca);
297 >
298 >        if (n < 21)                     /* j unit vector */
299 >                return(funcxf.xfm[1][n-15] / funcxf.sca);
300 >
301 >        if (n < 24)                     /* k unit vector */
302 >                return(funcxf.xfm[2][n-21] / funcxf.sca);
303 > badchan:
304 >        error(USER, "illegal channel number");
305   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines