ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/calfunc.c
(Generate patch)

Comparing ray/src/common/calfunc.c (file contents):
Revision 2.6 by greg, Sun Nov 22 12:11:48 1992 UTC vs.
Revision 2.14 by schorsch, Fri Nov 14 17:22:06 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 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   *  calfunc.c - routines for calcomp using functions.
6   *
7 < *      The define BIGLIB pulls in a large number of the
11 < *  available math routines.
12 < *
13 < *      If VARIABLE is not defined, only library functions
7 > *      If VARIABLE is not set, only library functions
8   *  can be accessed.
9   *
10 < *     4/2/86
10 > *  2/19/03     Eliminated conditional compiles in favor of esupport extern.
11   */
12  
13 < #include  <stdio.h>
13 > #include "copyright.h"
14  
15 + #include  <stdio.h>
16 + #include  <string.h>
17   #include  <errno.h>
22
18   #include  <math.h>
19  
20 + #include  "rterror.h"
21   #include  "calcomp.h"
22  
23                                  /* bits in argument flag (better be right!) */
# Line 38 | Line 34 | typedef struct activation {
34  
35   static ACTIVATION  *curact = NULL;
36  
37 < static double  libfunc();
37 > static double  libfunc(char *fname, VARDEF *vp);
38  
39 + #ifndef  MAXLIB
40   #define  MAXLIB         64      /* maximum number of library functions */
44
45 static double  l_if(), l_select(), l_rand();
46 static double  l_floor(), l_ceil();
47 #ifdef  BIGLIB
48 static double  l_sqrt();
49 static double  l_sin(), l_cos(), l_tan();
50 static double  l_asin(), l_acos(), l_atan(), l_atan2();
51 static double  l_exp(), l_log(), l_log10();
41   #endif
42  
43 < #ifdef  BIGLIB
43 > static double  l_if(char *), l_select(char *), l_rand(char *);
44 > static double  l_floor(char *), l_ceil(char *);
45 > static double  l_sqrt(char *);
46 > static double  l_sin(char *), l_cos(char *), l_tan(char *);
47 > static double  l_asin(char *), l_acos(char *), l_atan(char *), l_atan2(char *);
48 > static double  l_exp(char *), l_log(char *), l_log10(char *);
49 >
50                          /* functions must be listed alphabetically */
51   static LIBR  library[MAXLIB] = {
52      { "acos", 1, ':', l_acos },
# Line 74 | Line 69 | static LIBR  library[MAXLIB] = {
69  
70   static int  libsize = 16;
71  
77 #else
78                        /* functions must be listed alphabetically */
79 static LIBR  library[MAXLIB] = {
80    { "ceil", 1, ':', l_ceil },
81    { "floor", 1, ':', l_floor },
82    { "if", 3, ':', l_if },
83    { "rand", 1, ':', l_rand },
84    { "select", 1, ':', l_select },
85 };
86
87 static int  libsize = 5;
88
89 #endif
90
91 extern char  *savestr(), *emalloc();
92
93 extern VARDEF  *argf();
94
95 #ifdef  VARIABLE
72   #define  resolve(ep)    ((ep)->type==VAR?(ep)->v.ln:argf((ep)->v.chan))
97 #else
98 #define  resolve(ep)    ((ep)->v.ln)
99 #define varlookup(name) NULL
100 #endif
73  
74  
75   int
76   fundefined(fname)               /* return # of arguments for function */
77   char  *fname;
78   {
79 <    LIBR  *lp;
79 >    register LIBR  *lp;
80      register VARDEF  *vp;
81  
82 <    if ((vp = varlookup(fname)) == NULL || vp->def == NULL
83 <                || vp->def->v.kid->type != FUNC)
112 <        if ((lp = liblookup(fname)) == NULL)
113 <            return(0);
114 <        else
115 <            return(lp->nargs);
116 <    else
82 >    if ((vp = varlookup(fname)) != NULL && vp->def != NULL
83 >                && vp->def->v.kid->type == FUNC)
84          return(nekids(vp->def->v.kid) - 1);
85 +    lp = vp != NULL ? vp->lib : liblookup(fname);
86 +    if (lp == NULL)
87 +        return(0);
88 +    return(lp->nargs);
89   }
90  
91  
# Line 149 | Line 120 | double  *a;
120   }
121  
122  
123 + void
124   funset(fname, nargs, assign, fptr)      /* set a library function */
125   char  *fname;
126   int  nargs;
127   int  assign;
128 < double  (*fptr)();
128 > double  (*fptr)(char *);
129   {
130      int  oldlibsize = libsize;
131 +    char *cp;
132      register LIBR  *lp;
133 <
133 >                                                /* check for context */
134 >    for (cp = fname; *cp; cp++)
135 >        ;
136 >    if (cp == fname)
137 >        return;
138 >    if (cp[-1] == CNTXMARK)
139 >        *--cp = '\0';
140      if ((lp = liblookup(fname)) == NULL) {      /* insert */
141          if (libsize >= MAXLIB) {
142              eputs("Too many library functons!\n");
# Line 214 | Line 193 | argument(n)                    /* return nth argument for active functi
193   register int  n;
194   {
195      register ACTIVATION  *actp = curact;
196 <    register EPNODE  *ep;
196 >    register EPNODE  *ep = NULL;
197      double  aval;
198  
199      if (actp == NULL || --n < 0) {
# Line 241 | Line 220 | register int  n;
220   }
221  
222  
244 #ifdef  VARIABLE
223   VARDEF *
224   argf(n)                         /* return function def for nth argument */
225   int  n;
# Line 277 | Line 255 | badarg:
255      eputs(actp->name);
256      eputs(": argument not a function\n");
257      quit(1);
258 +        return NULL; /* pro forma return */
259   }
260  
261  
# Line 286 | Line 265 | int  n;
265   {
266      return(argf(n)->name);
267   }
289 #endif
268  
269  
270   double
# Line 340 | Line 318 | char  *fname;
318   }
319  
320  
343 #ifndef  VARIABLE
344 static VARDEF  *varlist = NULL;         /* our list of dummy variables */
345
346
347 VARDEF *
348 varinsert(vname)                /* dummy variable insert */
349 char  *vname;
350 {
351    register VARDEF  *vp;
352
353    vp = (VARDEF *)emalloc(sizeof(VARDEF));
354    vp->name = savestr(vname);
355    vp->nlinks = 1;
356    vp->def = NULL;
357    vp->lib = liblookup(vname);
358    vp->next = varlist;
359    varlist = vp;
360    return(vp);
361 }
362
363
364 varfree(vp)                     /* free dummy variable */
365 register VARDEF  *vp;
366 {
367    register VARDEF  *vp2;
368
369    if (vp == varlist)
370        varlist = vp->next;
371    else {
372        for (vp2 = varlist; vp2->next != vp; vp2 = vp2->next)
373                ;
374        vp2->next = vp->next;
375    }
376    freestr(vp->name);
377    efree((char *)vp);
378 }
379
380
381 libupdate(nm)                   /* update library */
382 char  *nm;
383 {
384    register VARDEF  *vp;
385
386    for (vp = varlist; vp != NULL; vp = vp->next)
387        vp->lib = liblookup(vp->name);
388 }
389 #endif
390
391
392
321   /*
322   *  The following routines are for internal use:
323   */
# Line 423 | Line 351 | VARDEF  *vp;
351          else if (isinf(d))
352              errno = ERANGE;
353   #endif
354 <    if (errno) {
354 >    if (errno == EDOM || errno == ERANGE) {
355          wputs(fname);
356          if (errno == EDOM)
357                  wputs(": domain error\n");
# Line 444 | Line 372 | VARDEF  *vp;
372  
373  
374   static double
375 < l_if()                  /* if(cond, then, else) conditional expression */
375 > l_if(char *nm)          /* if(cond, then, else) conditional expression */
376                          /* cond evaluates true if greater than zero */
377   {
378      if (argument(1) > 0.0)
# Line 455 | Line 383 | l_if()                 /* if(cond, then, else) conditional expressio
383  
384  
385   static double
386 < l_select()              /* return argument #(A1+1) */
386 > l_select(char *nm)      /* return argument #(A1+1) */
387   {
388          register int  n;
389  
390 <        n = argument(1) + .5;
390 >        n = (int)(argument(1) + .5);
391          if (n == 0)
392                  return(nargum()-1);
393          if (n < 1 || n > nargum()-1) {
# Line 471 | Line 399 | l_select()             /* return argument #(A1+1) */
399  
400  
401   static double
402 < l_rand()                /* random function between 0 and 1 */
402 > l_rand(char *nm)                /* random function between 0 and 1 */
403   {
404      double  x;
405  
# Line 484 | Line 412 | l_rand()               /* random function between 0 and 1 */
412  
413  
414   static double
415 < l_floor()               /* return largest integer not greater than arg1 */
415 > l_floor(char *nm)               /* return largest integer not greater than arg1 */
416   {
417      return(floor(argument(1)));
418   }
419  
420  
421   static double
422 < l_ceil()                /* return smallest integer not less than arg1 */
422 > l_ceil(char *nm)                /* return smallest integer not less than arg1 */
423   {
424      return(ceil(argument(1)));
425   }
426  
427  
500 #ifdef  BIGLIB
428   static double
429 < l_sqrt()
429 > l_sqrt(char *nm)
430   {
431      return(sqrt(argument(1)));
432   }
433  
434  
435   static double
436 < l_sin()
436 > l_sin(char *nm)
437   {
438      return(sin(argument(1)));
439   }
440  
441  
442   static double
443 < l_cos()
443 > l_cos(char *nm)
444   {
445      return(cos(argument(1)));
446   }
447  
448  
449   static double
450 < l_tan()
450 > l_tan(char *nm)
451   {
452      return(tan(argument(1)));
453   }
454  
455  
456   static double
457 < l_asin()
457 > l_asin(char *nm)
458   {
459      return(asin(argument(1)));
460   }
461  
462  
463   static double
464 < l_acos()
464 > l_acos(char *nm)
465   {
466      return(acos(argument(1)));
467   }
468  
469  
470   static double
471 < l_atan()
471 > l_atan(char *nm)
472   {
473      return(atan(argument(1)));
474   }
475  
476  
477   static double
478 < l_atan2()
478 > l_atan2(char *nm)
479   {
480      return(atan2(argument(1), argument(2)));
481   }
482  
483  
484   static double
485 < l_exp()
485 > l_exp(char *nm)
486   {
487      return(exp(argument(1)));
488   }
489  
490  
491   static double
492 < l_log()
492 > l_log(char *nm)
493   {
494      return(log(argument(1)));
495   }
496  
497  
498   static double
499 < l_log10()
499 > l_log10(char *nm)
500   {
501      return(log10(argument(1)));
502   }
576 #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines