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 1.22 by greg, Thu Aug 8 15:41:14 1991 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1991 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 15 | Line 15 | static char SCCSid[] = "$SunId$ LBL";
15   #include  "otypes.h"
16  
17  
18 < typedef struct {
19 <        double  sca;                    /* scalefactor */
20 <        double  xfm[4][4];              /* transform matrix */
21 < }  XF;
18 > #define  INITFILE       "rayinit.cal"
19 > #define  DEFVNAME       "FILE_LOADED`"
20  
21 < static OBJREC  *fobj;           /* current function object */
22 < static RAY  *fray;              /* current function ray */
23 < static XF  fxf;                 /* current transformation */
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,
26 >        1.0
27 > };
28  
29 + XF  funcxf;                     /* current transformation */
30 + static OBJREC  *fobj = NULL;    /* current function object */
31 + static RAY  *fray = NULL;       /* current function ray */
32  
33 < setmap(m, r, sca, xfm)          /* set channels for function call */
33 >
34 > setmap(m, r, bx)                /* set channels for function call */
35   OBJREC  *m;
36   register RAY  *r;
37 < double  sca;
32 < double  xfm[4][4];
37 > XF  *bx;
38   {
34        extern double  l_noise3(), l_noise3a(), l_noise3b(), l_noise3c();
35        extern double  l_hermite(), l_fnoise3(), l_arg();
39          extern long  eclock;
40 <        static char  *initfile = "rayinit.cal";
41 <
42 <        if (initfile != NULL) {
43 <                loadfunc(initfile);
41 <                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;
53 <        }
40 >        static long  lastrno = -1;
41 >                                        /* check to see if already set */
42 >        if (m == fobj && r->rno == lastrno)
43 >                return(0);
44          fobj = m;
45          fray = r;
46 <        fxf.sca = r->ros * sca;
47 <        multmat4(fxf.xfm, r->rox, xfm);
46 >        if (r->rox != NULL)
47 >                if (bx != &unitxf) {
48 >                        funcxf.sca = r->rox->b.sca * bx->sca;
49 >                        multmat4(funcxf.xfm, r->rox->b.xfm, bx->xfm);
50 >                } else
51 >                        copystruct(&funcxf, &r->rox->b);
52 >        else
53 >                copystruct(&funcxf, bx);
54 >        lastrno = r->rno;
55          eclock++;               /* notify expression evaluator */
56 +        return(1);
57   }
58  
59  
# Line 63 | Line 61 | setfunc(m, r)                          /* simplified interface to setmap */
61   register OBJREC  *m;
62   RAY  *r;
63   {
64 < #define  mxf    ((XF *)m->os)
64 >        register XF  *mxf;
65  
66 <        if (mxf == NULL) {
67 <                register int  n = m->oargs.nsargs;
68 <                register char  **sa = m->oargs.sarg;
66 >        if ((mxf = (XF *)m->os) == NULL) {
67 >                register int  n;
68 >                register char  **sa;
69  
70 <                while (n > 0 && **sa != '-') {
71 <                        n--;
72 <                        sa++;
70 >                for (n = m->oargs.nsargs, sa = m->oargs.sarg;
71 >                                n > 0 && **sa != '-'; n--, sa++)
72 >                        ;
73 >                if (n == 0)
74 >                        mxf = &unitxf;
75 >                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 <                mxf = (XF *)malloc(sizeof(XF));
77 <                if (mxf == NULL)
78 <                        goto memerr;
79 <                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;
84 >                m->os = (char *)mxf;
85          }
86 <        setmap(m, r, mxf->sca, mxf->xfm);
87 <        return;
86 >        return(setmap(m, r, mxf));
87   memerr:
88          error(SYSTEM, "out of memory in setfunc");
90 #undef  mxf
89   }
90  
91  
# Line 97 | Line 95 | char  *fname;
95          extern char  *libpath;          /* library search path */
96          char  *ffname;
97  
98 <        if ((ffname = getpath(fname, libpath)) == NULL) {
98 >        if ((ffname = getpath(fname, libpath, R_OK)) == NULL) {
99                  sprintf(errmsg, "cannot find function file \"%s\"", fname);
100                  error(USER, errmsg);
101          }
# Line 125 | Line 123 | l_arg()                                /* return nth real argument */
123  
124  
125   double
126 + l_erf()                         /* error function */
127 + {
128 +        extern double  erf();
129 +
130 +        return(erf(argument(1)));
131 + }
132 +
133 +
134 + double
135 + l_erfc()                        /* cumulative error function */
136 + {
137 +        extern double  erfc();
138 +
139 +        return(erfc(argument(1)));
140 + }
141 +
142 +
143 + funcfile(fname)                 /* set context, load file if necessary */
144 + register 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 +        if (fname[0] == '.' && fname[1] == '\0')
167 +                setcontext("");                 /* "." means no file */
168 +        else {
169 +                setcontext(fname);
170 +                if (!vardefined(DEFVNAME)) {
171 +                        loadfunc(fname);
172 +                        varset(DEFVNAME, ':', 1.0);
173 +                }
174 +        }
175 + }
176 +
177 +
178 + double
179   chanvalue(n)                    /* return channel n to calcomp */
180   register int  n;
181   {
182 <        double  res;
182 >        double  sum;
183          register RAY  *r;
184  
185 <        n--;                                    /* for convenience */
185 >        if (--n < 0)
186 >                goto badchan;
187  
188 <        if (n < 0 || n > 10)
137 <                error(USER, "illegal channel number");
188 >        if (n < 3)                      /* ray direction */
189  
190 <        if (n == 9) {                           /* distance */
190 >                return( (       fray->rdir[0]*funcxf.xfm[0][n] +
191 >                                fray->rdir[1]*funcxf.xfm[1][n] +
192 >                                fray->rdir[2]*funcxf.xfm[2][n]  )
193 >                         / funcxf.sca );
194  
195 <                res = fray->rot;
142 <                for (r = fray->parent; r != NULL; r = r->parent)
143 <                        res += r->rot;
144 <                res *= fxf.sca;
195 >        if (n < 6)                      /* surface normal */
196  
197 <        } else if (n == 10) {                   /* dot product */
197 >                return( (       fray->ron[0]*funcxf.xfm[0][n-3] +
198 >                                fray->ron[1]*funcxf.xfm[1][n-3] +
199 >                                fray->ron[2]*funcxf.xfm[2][n-3] )
200 >                         / funcxf.sca );
201  
202 <                res = fray->rod;
202 >        if (n < 9)                      /* intersection */
203  
204 <        } else if (n < 3) {                     /* ray direction */
205 <                        res = ( fray->rdir[0]*fxf.xfm[0][n] +
206 <                                        fray->rdir[1]*fxf.xfm[1][n] +
207 <                                        fray->rdir[2]*fxf.xfm[2][n]     )
208 <                                 / fxf.sca ;
209 <        } else if (n < 6) {                     /* surface normal */
210 <                        res = ( fray->ron[0]*fxf.xfm[0][n-3] +
211 <                                        fray->ron[1]*fxf.xfm[1][n-3] +
212 <                                        fray->ron[2]*fxf.xfm[2][n-3]    )
213 <                                 / fxf.sca ;
214 <        } else {                                /* intersection */
215 <                        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] ;
204 >                return( fray->rop[0]*funcxf.xfm[0][n-6] +
205 >                                fray->rop[1]*funcxf.xfm[1][n-6] +
206 >                                fray->rop[2]*funcxf.xfm[2][n-6] +
207 >                                             funcxf.xfm[3][n-6] );
208 >
209 >        if (n == 9) {                   /* distance */
210 >
211 >                sum = fray->rot;
212 >                for (r = fray->parent; r != NULL; r = r->parent)
213 >                        sum += r->rot;
214 >                return(sum * funcxf.sca);
215 >
216          }
217 +        if (n == 10)                    /* dot product (range [-1,1]) */
218 +                return( fray->rod <= -1.0 ? -1.0 :
219 +                        fray->rod >= 1.0 ? 1.0 :
220 +                        fray->rod );
221  
222 <        return(res);
222 >        if (n == 11)                    /* scale */
223 >                return(funcxf.sca);
224 >
225 >        if (n < 15)                     /* origin */
226 >                return(funcxf.xfm[3][n-12]);
227 >
228 >        if (n < 18)                     /* i unit vector */
229 >                return(funcxf.xfm[0][n-15] / funcxf.sca);
230 >
231 >        if (n < 21)                     /* j unit vector */
232 >                return(funcxf.xfm[1][n-15] / funcxf.sca);
233 >
234 >        if (n < 24)                     /* k unit vector */
235 >                return(funcxf.xfm[2][n-21] / funcxf.sca);
236 > badchan:
237 >        error(USER, "illegal channel number");
238   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines