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 2.3 by greg, Tue Dec 24 17:43:43 1991 UTC vs.
Revision 2.27 by greg, Wed Jun 27 16:29:26 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"
16
13   #include  "func.h"
14  
15  
16   #define  INITFILE       "rayinit.cal"
21 #define  REFVNAME       "`FILE_REFCNT"
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 34 | 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 double  l_erf(), l_erfc(), l_arg();
33 > static char  rayinitcal[] = INITFILE;
34  
35 + static double  l_erf(char *), l_erfc(char *), l_arg(char *);
36  
37 +
38 + void
39 + initfunc()      /* initialize function evaluation */
40 + {
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(m, ff, ef, dofwd)       /* get function for this modifier */
67 < OBJREC  *m;
68 < int  ff;
69 < unsigned  ef;
70 < int  dofwd;
66 > getfunc(        /* get function for this modifier */
67 >        OBJREC  *m,
68 >        int  ff,
69 >        unsigned int  ef,
70 >        int  dofwd
71 > )
72   {
47        extern EPNODE  *curfunc;
48        static char  initfile[] = INITFILE;
73          char  sbuf[MAXSTR];
74 <        register char  **arg;
75 <        register MFUNC  *f;
74 >        char  **arg;
75 >        MFUNC  *f;
76          int  ne, na;
77 <        register int  i;
77 >        int  i;
78                                          /* check to see if done already */
79          if ((f = (MFUNC *)m->os) != NULL)
80                  return(f);
81 <        if (initfile[0]) {              /* initialize on first call */
82 <                setcontext("");
83 <                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 <                initfile[0] = '\0';
73 <        }
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] == '.')
90 >        if (i == 1 && arg[ff][0] == '.') {
91                  setcontext(f->ctx = "");        /* "." means no file */
92 <        else {
93 <                strcpy(sbuf,m->oargs.sarg[ff]); /* file name is context */
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(m->oargs.sarg[ff]);
98 >                        loadfunc(arg[ff]);
99                          varset(REFVNAME, '=', 1.0);
100                  } else                          /* reference_count++ */
101                          varset(REFVNAME, '=', varvalue(REFVNAME)+1.0);
# Line 107 | Line 117 | int  dofwd;
117                  i = ff+1;
118          while (i < na && arg[i][0] != '-')
119                  i++;
120 <        if (i == na)                    /* no transform */
121 <                f->f = f->b = &unitxf;
122 <        else {                          /* get transform */
123 <                if ((f->b = (XF *)malloc(sizeof(XF))) == NULL)
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->b, na-i, arg+i) != na-i)
125 >                if (invxf(f->bxp, na-i, arg+i) != na-i)
126                          objerror(m, USER, "bad transform");
127 <                if (f->b->sca < 0.0)
128 <                        f->b->sca = -f->b->sca;
127 >                if (f->bxp->sca < 0.0)
128 >                        f->bxp->sca = -f->bxp->sca;
129                  if (dofwd) {                    /* do both transforms */
130 <                        if ((f->f = (XF *)malloc(sizeof(XF))) == NULL)
130 >                        if ((f->fxp = (XF *)malloc(sizeof(XF))) == NULL)
131                                  goto memerr;
132 <                        xf(f->f, na-i, arg+i);
133 <                        if (f->f->sca < 0.0)
134 <                                f->f->sca = -f->f->sca;
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 arguments");
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 < freefunc(m)                     /* free memory associated with modifier */
148 < OBJREC  *m;
147 > void
148 > freefunc(                       /* free memory associated with modifier */
149 >        OBJREC  *m
150 > )
151   {
152 <        register MFUNC  *f;
153 <        register int  i;
152 >        MFUNC  *f;
153 >        int  i;
154  
155          if ((f = (MFUNC *)m->os) == NULL)
156                  return;
# Line 152 | Line 165 | OBJREC  *m;
165                          dcleanup(2);            /* remove definitions */
166                  freestr(f->ctx);
167          }
168 <        if (f->b != &unitxf)
169 <                free((char *)f->b);
170 <        if (f->f != NULL && f->f != &unitxf)
171 <                free((char *)f->f);
172 <        free((char *)f);
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 < setfunc(m, r)                   /* set channels for function call */
178 < OBJREC  *m;
179 < register RAY  *r;
177 > int
178 > setfunc(                        /* set channels for function call */
179 >        OBJREC  *m,
180 >        RAY     *r
181 > )
182   {
183 <        static long  lastrno = -1;
184 <        register MFUNC  *f;
185 <                                        /* get function */
183 >        static RNUMBER  lastrno = ~0;
184 >        MFUNC           *f;
185 >                                        /* get function if any */
186          if ((f = (MFUNC *)m->os) == NULL)
187 <                error(m, CONSISTENCY, "setfunc called before getfunc");
188 <                                        /* set evaluator context */
189 <        setcontext(f->ctx);
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)
191 >        if ((m == fobj) & (r->rno == lastrno))
192                  return(0);
193          fobj = m;
194          fray = r;
195 <        if (r->rox != NULL)
196 <                if (f->b != &unitxf) {
197 <                        funcxf.sca = r->rox->b.sca * f->b->sca;
198 <                        multmat4(funcxf.xfm, r->rox->b.xfm, f->b->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, f->b);
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 < loadfunc(fname)                 /* load definition file */
210 < char  *fname;
209 > int
210 > worldfunc(                      /* special function context sans object */
211 >        char    *ctx,
212 >        RAY     *r
213 > )
214   {
215 <        extern char  *libpath;          /* library search path */
215 >        static RNUMBER  lastrno = ~0;
216 >
217 >        if (rayinitcal[0])              /* initialize on first call */
218 >                initfunc();
219 >                                        /* set evaluator context */
220 >        setcontext(ctx);
221 >                                        /* check if ray already set */
222 >        if ((fobj == NULL) & (r->rno == lastrno))
223 >                return(0);
224 >        fobj = NULL;
225 >        fray = r;
226 >        funcxf = unitxf;
227 >        lastrno = r->rno;
228 >        eclock++;               /* notify expression evaluator */
229 >        return(1);
230 > }
231 >
232 >
233 > void
234 > loadfunc(                       /* load definition file */
235 >        char  *fname
236 > )
237 > {
238          char  *ffname;
239  
240 <        if ((ffname = getpath(fname, libpath, R_OK)) == NULL) {
240 >        if ((ffname = getpath(fname, getrlibpath(), R_OK)) == NULL) {
241                  sprintf(errmsg, "cannot find function file \"%s\"", fname);
242                  error(USER, errmsg);
243          }
# Line 206 | Line 246 | char  *fname;
246  
247  
248   static double
249 < l_arg()                         /* return nth real argument */
249 > l_arg(char *nm)                 /* return nth real argument */
250   {
251 <        extern double  argument();
212 <        register int  n;
251 >        int  n;
252  
253 +        if (fobj == NULL)
254 +                error(USER, "arg(n) called without a context");
255 +
256          n = argument(1) + .5;           /* round to integer */
257  
258          if (n < 1)
# Line 225 | Line 267 | l_arg()                                /* return nth real argument */
267  
268  
269   static double
270 < l_erf()                         /* error function */
270 > l_erf(char *nm)                 /* error function */
271   {
230        extern double  erf();
231
272          return(erf(argument(1)));
273   }
274  
275  
276   static double
277 < l_erfc()                        /* cumulative error function */
277 > l_erfc(char *nm)                /* cumulative error function */
278   {
239        extern double  erfc();
240
279          return(erfc(argument(1)));
280   }
281  
282  
283   double
284 < chanvalue(n)                    /* return channel n to calcomp */
285 < register int  n;
284 > chanvalue(                      /* return channel n to calcomp */
285 >        int  n
286 > )
287   {
288 <        double  sum;
289 <        register RAY  *r;
288 >        if (fray == NULL)
289 >                syntax("ray parameter used in constant expression");
290  
291          if (--n < 0)
292                  goto badchan;
293  
294 <        if (n < 3)                      /* ray direction */
294 >        if (n <= 2)                     /* ray direction */
295  
296                  return( (       fray->rdir[0]*funcxf.xfm[0][n] +
297                                  fray->rdir[1]*funcxf.xfm[1][n] +
298                                  fray->rdir[2]*funcxf.xfm[2][n]  )
299                           / funcxf.sca );
300  
301 <        if (n < 6)                      /* surface normal */
301 >        if (n <= 5)                     /* surface normal */
302  
303                  return( (       fray->ron[0]*funcxf.xfm[0][n-3] +
304                                  fray->ron[1]*funcxf.xfm[1][n-3] +
305                                  fray->ron[2]*funcxf.xfm[2][n-3] )
306                           / funcxf.sca );
307  
308 <        if (n < 9)                      /* intersection */
308 >        if (n <= 8)                     /* intersection */
309  
310                  return( fray->rop[0]*funcxf.xfm[0][n-6] +
311                                  fray->rop[1]*funcxf.xfm[1][n-6] +
312                                  fray->rop[2]*funcxf.xfm[2][n-6] +
313                                               funcxf.xfm[3][n-6] );
314  
315 <        if (n == 9) {                   /* distance */
315 >        if (n == 9)                     /* total distance */
316 >                return(raydist(fray,PRIMARY) * funcxf.sca);
317  
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        }
318          if (n == 10)                    /* dot product (range [-1,1]) */
319                  return( fray->rod <= -1.0 ? -1.0 :
320                          fray->rod >= 1.0 ? 1.0 :
# Line 289 | Line 323 | register int  n;
323          if (n == 11)                    /* scale */
324                  return(funcxf.sca);
325  
326 <        if (n < 15)                     /* origin */
326 >        if (n <= 14)                    /* origin */
327                  return(funcxf.xfm[3][n-12]);
328  
329 <        if (n < 18)                     /* i unit vector */
329 >        if (n <= 17)                    /* i unit vector */
330                  return(funcxf.xfm[0][n-15] / funcxf.sca);
331  
332 <        if (n < 21)                     /* j unit vector */
333 <                return(funcxf.xfm[1][n-15] / funcxf.sca);
332 >        if (n <= 20)                    /* j unit vector */
333 >                return(funcxf.xfm[1][n-18] / funcxf.sca);
334  
335 <        if (n < 24)                     /* k unit vector */
335 >        if (n <= 23)                    /* k unit vector */
336                  return(funcxf.xfm[2][n-21] / funcxf.sca);
337 +
338 +        if (n == 24)                    /* single ray (shadow) distance */
339 +                return((fray->rot+raydist(fray->parent,SHADOW)) * funcxf.sca);
340 +
341 +        if (n <= 26)                    /* local (u,v) coordinates */
342 +                return(fray->uv[n-25]);
343   badchan:
344          error(USER, "illegal channel number");
345 +        return(0.0);
346   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines