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.14 by greg, Sat Feb 22 02:07:28 2003 UTC vs.
Revision 2.29 by greg, Wed May 20 12:58:30 2015 UTC

# Line 5 | Line 5 | static const char      RCSid[] = "$Id$";
5   *  func.c - interface to calcomp functions.
6   */
7  
8 < /* ====================================================================
9 < * The Radiance Software License, Version 1.0
10 < *
11 < * Copyright (c) 1990 - 2002 The Regents of the University of California,
12 < * through Lawrence Berkeley National Laboratory.   All rights reserved.
13 < *
14 < * Redistribution and use in source and binary forms, with or without
15 < * modification, are permitted provided that the following conditions
16 < * are met:
17 < *
18 < * 1. Redistributions of source code must retain the above copyright
19 < *         notice, this list of conditions and the following disclaimer.
20 < *
21 < * 2. Redistributions in binary form must reproduce the above copyright
22 < *       notice, this list of conditions and the following disclaimer in
23 < *       the documentation and/or other materials provided with the
24 < *       distribution.
25 < *
26 < * 3. The end-user documentation included with the redistribution,
27 < *           if any, must include the following acknowledgment:
28 < *             "This product includes Radiance software
29 < *                 (http://radsite.lbl.gov/)
30 < *                 developed by the Lawrence Berkeley National Laboratory
31 < *               (http://www.lbl.gov/)."
32 < *       Alternately, this acknowledgment may appear in the software itself,
33 < *       if and wherever such third-party acknowledgments normally appear.
34 < *
35 < * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
36 < *       and "The Regents of the University of California" must
37 < *       not be used to endorse or promote products derived from this
38 < *       software without prior written permission. For written
39 < *       permission, please contact [email protected].
40 < *
41 < * 5. Products derived from this software may not be called "Radiance",
42 < *       nor may "Radiance" appear in their name, without prior written
43 < *       permission of Lawrence Berkeley National Laboratory.
44 < *
45 < * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
46 < * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 < * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 < * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
49 < * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 < * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 < * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52 < * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 < * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 < * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 < * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 < * SUCH DAMAGE.
57 < * ====================================================================
58 < *
59 < * This software consists of voluntary contributions made by many
60 < * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
61 < * information on Lawrence Berkeley National Laboratory, please see
62 < * <http://www.lbl.gov/>.
63 < */
8 > #include "copyright.h"
9  
10   #include  "ray.h"
11 <
11 > #include  "paths.h"
12   #include  "otypes.h"
68
13   #include  "func.h"
14  
15  
# Line 86 | 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 + /* Set parameters for current evaluation */
66 + void
67 + set_eparams(char *prms)
68 + {
69 +        static char     *last_params = NULL;
70 +        char            vname[RMAXWORD];
71 +        double          value;
72 +        char            *cpd;
73 +                                        /* check if already set */
74 +        if ((prms == NULL) | (prms == last_params))
75 +                return;
76 +        if (last_params != NULL && !strcmp(prms, last_params))
77 +                return;
78 +        last_params = prms;             /* assign each variable */
79 +        while (*prms) {
80 +                if (isspace(*prms)) {
81 +                        ++prms; continue;
82 +                }
83 +                if (!isalpha(*prms))
84 +                        goto bad_params;
85 +                cpd = vname;
86 +                while (*prms && (*prms != '=') & !isspace(*prms)) {
87 +                        if (!isid(*prms))
88 +                                goto bad_params;
89 +                        *cpd++ = *prms++;
90 +                }
91 +                if (cpd == vname)
92 +                        goto bad_params;
93 +                *cpd = '\0';
94 +                while (isspace(*prms)) prms++;
95 +                if (*prms++ != '=')
96 +                        goto bad_params;
97 +                value = atof(prms);
98 +                if ((prms = fskip(prms)) == NULL)
99 +                        goto bad_params;
100 +                while (isspace(*prms)) prms++;
101 +                prms += (*prms == ',') | (*prms == ';');
102 +                varset(vname, '=', value);
103 +        }
104 +        return;
105 + bad_params:
106 +        sprintf(errmsg, "bad parameter list '%s'", last_params);
107 +        error(USER, errmsg);
108 + }
109 +
110 +
111   MFUNC *
112 < getfunc(m, ff, ef, dofwd)       /* get function for this modifier */
113 < OBJREC  *m;
114 < int  ff;
115 < unsigned int  ef;
116 < int  dofwd;
112 > getfunc(        /* get function for this modifier */
113 >        OBJREC  *m,
114 >        int  ff,
115 >        unsigned int  ef,
116 >        int  dofwd
117 > )
118   {
99        static char  initfile[] = INITFILE;
119          char  sbuf[MAXSTR];
120 <        register char  **arg;
121 <        register MFUNC  *f;
120 >        char  **arg;
121 >        MFUNC  *f;
122          int  ne, na;
123 <        register int  i;
123 >        int  i;
124                                          /* check to see if done already */
125          if ((f = (MFUNC *)m->os) != NULL)
126                  return(f);
127          fobj = NULL; fray = NULL;
128 <        if (initfile[0]) {              /* initialize on first call */
129 <                esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW;
111 <                esupport &= ~(E_OUTCHAN);
112 <                setcontext("");
113 <                scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
114 <                scompile("Nx=$4;Ny=$5;Nz=$6;", NULL, 0);
115 <                scompile("Px=$7;Py=$8;Pz=$9;", NULL, 0);
116 <                scompile("T=$10;Ts=$25;Rdot=$11;", NULL, 0);
117 <                scompile("S=$12;Tx=$13;Ty=$14;Tz=$15;", NULL, 0);
118 <                scompile("Ix=$16;Iy=$17;Iz=$18;", NULL, 0);
119 <                scompile("Jx=$19;Jy=$20;Jz=$21;", NULL, 0);
120 <                scompile("Kx=$22;Ky=$23;Kz=$24;", NULL, 0);
121 <                funset("arg", 1, '=', l_arg);
122 <                funset("erf", 1, ':', l_erf);
123 <                funset("erfc", 1, ':', l_erfc);
124 <                setnoisefuncs();
125 <                setprismfuncs();
126 <                loadfunc(initfile);
127 <                initfile[0] = '\0';
128 <        }
128 >        if (rayinitcal[0])              /* initialize on first call */
129 >                initfunc();
130          if ((na = m->oargs.nsargs) <= ff)
131                  goto toofew;
132          arg = m->oargs.sarg;
133          if ((f = (MFUNC *)calloc(1, sizeof(MFUNC))) == NULL)
134                  goto memerr;
135          i = strlen(arg[ff]);                    /* set up context */
136 <        if (i == 1 && arg[ff][0] == '.')
136 >        if (i == 1 && arg[ff][0] == '.') {
137                  setcontext(f->ctx = "");        /* "." means no file */
138 <        else {
138 >        } else {
139                  strcpy(sbuf,arg[ff]);           /* file name is context */
140                  if (i > LCALSUF && !strcmp(sbuf+i-LCALSUF, CALSUF))
141                          sbuf[i-LCALSUF] = '\0'; /* remove suffix */
# Line 162 | Line 163 | int  dofwd;
163                  i = ff+1;
164          while (i < na && arg[i][0] != '-')
165                  i++;
166 <        if (i == na)                    /* no transform */
167 <                f->f = f->b = &unitxf;
168 <        else {                          /* get transform */
169 <                if ((f->b = (XF *)malloc(sizeof(XF))) == NULL)
166 >        if (i == na) {                  /* no transform */
167 >                f->fxp = f->bxp = &unitxf;
168 >        } else {                        /* get transform */
169 >                if ((f->bxp = (XF *)malloc(sizeof(XF))) == NULL)
170                          goto memerr;
171 <                if (invxf(f->b, na-i, arg+i) != na-i)
171 >                if (invxf(f->bxp, na-i, arg+i) != na-i)
172                          objerror(m, USER, "bad transform");
173 <                if (f->b->sca < 0.0)
174 <                        f->b->sca = -f->b->sca;
173 >                if (f->bxp->sca < 0.0)
174 >                        f->bxp->sca = -f->bxp->sca;
175                  if (dofwd) {                    /* do both transforms */
176 <                        if ((f->f = (XF *)malloc(sizeof(XF))) == NULL)
176 >                        if ((f->fxp = (XF *)malloc(sizeof(XF))) == NULL)
177                                  goto memerr;
178 <                        xf(f->f, na-i, arg+i);
179 <                        if (f->f->sca < 0.0)
180 <                                f->f->sca = -f->f->sca;
178 >                        xf(f->fxp, na-i, arg+i);
179 >                        if (f->fxp->sca < 0.0)
180 >                                f->fxp->sca = -f->fxp->sca;
181                  }
182          }
183          m->os = (char *)f;
# Line 185 | Line 186 | toofew:
186          objerror(m, USER, "too few string arguments");
187   memerr:
188          error(SYSTEM, "out of memory in getfunc");
189 +        return NULL; /* pro forma return */
190   }
191  
192  
193   void
194 < freefunc(m)                     /* free memory associated with modifier */
195 < OBJREC  *m;
194 > freefunc(                       /* free memory associated with modifier */
195 >        OBJREC  *m
196 > )
197   {
198 <        register MFUNC  *f;
199 <        register int  i;
198 >        MFUNC  *f;
199 >        int  i;
200  
201          if ((f = (MFUNC *)m->os) == NULL)
202                  return;
# Line 208 | Line 211 | OBJREC  *m;
211                          dcleanup(2);            /* remove definitions */
212                  freestr(f->ctx);
213          }
214 <        if (f->b != &unitxf)
215 <                free((void *)f->b);
216 <        if (f->f != NULL && f->f != &unitxf)
217 <                free((void *)f->f);
214 >        if (f->bxp != &unitxf)
215 >                free((void *)f->bxp);
216 >        if ((f->fxp != NULL) & (f->fxp != &unitxf))
217 >                free((void *)f->fxp);
218          free((void *)f);
219          m->os = NULL;
220   }
221  
222  
223   int
224 < setfunc(m, r)                   /* set channels for function call */
225 < OBJREC  *m;
226 < register RAY  *r;
224 > setfunc(                        /* set channels for function call */
225 >        OBJREC  *m,
226 >        RAY     *r
227 > )
228   {
229 <        static unsigned long  lastrno = ~0;
230 <        register MFUNC  *f;
231 <                                        /* get function */
229 >        static RNUMBER  lastrno = ~0;
230 >        MFUNC           *f;
231 >                                        /* get function if any */
232          if ((f = (MFUNC *)m->os) == NULL)
233                  objerror(m, CONSISTENCY, "setfunc called before getfunc");
234 <                                        /* set evaluator context */
235 <        setcontext(f->ctx);
234 >                
235 >        setcontext(f->ctx);             /* set evaluator context */
236                                          /* check to see if matrix set */
237 <        if (m == fobj && r->rno == lastrno)
237 >        if ((m == fobj) & (r->rno == lastrno))
238                  return(0);
239          fobj = m;
240          fray = r;
241 <        if (r->rox != NULL)
242 <                if (f->b != &unitxf) {
243 <                        funcxf.sca = r->rox->b.sca * f->b->sca;
244 <                        multmat4(funcxf.xfm, r->rox->b.xfm, f->b->xfm);
241 >        if (r->rox != NULL) {
242 >                if (f->bxp != &unitxf) {
243 >                        funcxf.sca = r->rox->b.sca * f->bxp->sca;
244 >                        multmat4(funcxf.xfm, r->rox->b.xfm, f->bxp->xfm);
245                  } else
246 <                        copystruct(&funcxf, &r->rox->b);
247 <        else
248 <                copystruct(&funcxf, f->b);
246 >                        funcxf = r->rox->b;
247 >        } else
248 >                funcxf = *f->bxp;
249          lastrno = r->rno;
250          eclock++;               /* notify expression evaluator */
251          return(1);
252   }
253  
254  
255 + int
256 + worldfunc(                      /* special function context sans object */
257 +        char    *ctx,
258 +        RAY     *r
259 + )
260 + {
261 +        static RNUMBER  lastrno = ~0;
262 +
263 +        if (rayinitcal[0])              /* initialize on first call */
264 +                initfunc();
265 +                                        /* set evaluator context */
266 +        setcontext(ctx);
267 +                                        /* check if ray already set */
268 +        if ((fobj == NULL) & (r->rno == lastrno))
269 +                return(0);
270 +        fobj = NULL;
271 +        fray = r;
272 +        funcxf = unitxf;
273 +        lastrno = r->rno;
274 +        eclock++;               /* notify expression evaluator */
275 +        return(1);
276 + }
277 +
278 +
279   void
280 < loadfunc(fname)                 /* load definition file */
281 < char  *fname;
280 > loadfunc(                       /* load definition file */
281 >        char  *fname
282 > )
283   {
284          char  *ffname;
285  
286 <        if ((ffname = getpath(fname, getlibpath(), R_OK)) == NULL) {
286 >        if ((ffname = getpath(fname, getrlibpath(), R_OK)) == NULL) {
287                  sprintf(errmsg, "cannot find function file \"%s\"", fname);
288                  error(USER, errmsg);
289          }
# Line 263 | Line 292 | char  *fname;
292  
293  
294   static double
295 < l_arg()                         /* return nth real argument */
295 > l_arg(char *nm)                 /* return nth real argument */
296   {
297 <        register int  n;
297 >        int  n;
298  
299          if (fobj == NULL)
300 <                syntax("arg(n) used in constant expression");
300 >                error(USER, "arg(n) called without a context");
301  
302          n = argument(1) + .5;           /* round to integer */
303  
# Line 284 | Line 313 | l_arg()                                /* return nth real argument */
313  
314  
315   static double
316 < l_erf()                         /* error function */
316 > l_erf(char *nm)                 /* error function */
317   {
289        extern double  erf();
290
318          return(erf(argument(1)));
319   }
320  
321  
322   static double
323 < l_erfc()                        /* cumulative error function */
323 > l_erfc(char *nm)                /* cumulative error function */
324   {
298        extern double  erfc();
299
325          return(erfc(argument(1)));
326   }
327  
328  
329   double
330 < chanvalue(n)                    /* return channel n to calcomp */
331 < register int  n;
330 > chanvalue(                      /* return channel n to calcomp */
331 >        int  n
332 > )
333   {
334          if (fray == NULL)
335                  syntax("ray parameter used in constant expression");
# Line 311 | Line 337 | register int  n;
337          if (--n < 0)
338                  goto badchan;
339  
340 <        if (n < 3)                      /* ray direction */
340 >        if (n <= 2)                     /* ray direction */
341  
342                  return( (       fray->rdir[0]*funcxf.xfm[0][n] +
343                                  fray->rdir[1]*funcxf.xfm[1][n] +
344                                  fray->rdir[2]*funcxf.xfm[2][n]  )
345                           / funcxf.sca );
346  
347 <        if (n < 6)                      /* surface normal */
347 >        if (n <= 5)                     /* surface normal */
348  
349                  return( (       fray->ron[0]*funcxf.xfm[0][n-3] +
350                                  fray->ron[1]*funcxf.xfm[1][n-3] +
351                                  fray->ron[2]*funcxf.xfm[2][n-3] )
352                           / funcxf.sca );
353  
354 <        if (n < 9)                      /* intersection */
354 >        if (n <= 8) {                   /* intersection point */
355 >                if (fray->rot >= FHUGE)
356 >                        return(0.0);    /* XXX should be runtime error? */
357  
358                  return( fray->rop[0]*funcxf.xfm[0][n-6] +
359                                  fray->rop[1]*funcxf.xfm[1][n-6] +
360                                  fray->rop[2]*funcxf.xfm[2][n-6] +
361                                               funcxf.xfm[3][n-6] );
362 +        }
363  
364          if (n == 9)                     /* total distance */
365                  return(raydist(fray,PRIMARY) * funcxf.sca);
# Line 343 | Line 372 | register int  n;
372          if (n == 11)                    /* scale */
373                  return(funcxf.sca);
374  
375 <        if (n < 15)                     /* origin */
375 >        if (n <= 14)                    /* origin */
376                  return(funcxf.xfm[3][n-12]);
377  
378 <        if (n < 18)                     /* i unit vector */
378 >        if (n <= 17)                    /* i unit vector */
379                  return(funcxf.xfm[0][n-15] / funcxf.sca);
380  
381 <        if (n < 21)                     /* j unit vector */
382 <                return(funcxf.xfm[1][n-15] / funcxf.sca);
381 >        if (n <= 20)                    /* j unit vector */
382 >                return(funcxf.xfm[1][n-18] / funcxf.sca);
383  
384 <        if (n < 24)                     /* k unit vector */
384 >        if (n <= 23)                    /* k unit vector */
385                  return(funcxf.xfm[2][n-21] / funcxf.sca);
386  
387          if (n == 24)                    /* single ray (shadow) distance */
388                  return((fray->rot+raydist(fray->parent,SHADOW)) * funcxf.sca);
389 +
390 +        if (n <= 26)                    /* local (u,v) coordinates */
391 +                return(fray->uv[n-25]);
392   badchan:
393          error(USER, "illegal channel number");
394 +        return(0.0);
395   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines