ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/Development/ray/src/rt/func.c
(Generate patch)

Comparing ray/src/rt/func.c (file contents):
Revision 1.5 by greg, Wed Apr 19 21:36:11 1989 UTC vs.
Revision 2.46 by greg, Sat Dec 6 04:13:14 2025 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1986 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"
13 + #include  "func.h"
14 + #include <ctype.h>
15  
16  
17 < typedef struct {
18 <        double  xfm[4][4];              /* transform matrix */
19 <        double  sca;                    /* scalefactor */
20 < }  XF;
17 > #define  INITFILE       "rayinit.cal"
18 > #define  CALSUF         ".cal"
19 > #define  LCALSUF        4
20 > char  REFVNAME[] = "`FILE_REFCNT";
21  
22 < static OBJREC  *fobj;           /* current function object */
23 < static RAY  *fray;              /* current function ray */
24 < static XF  fxf;                 /* current transformation */
22 > XF  unitxf = {                  /* identity transform */
23 >        {{1.0, 0.0, 0.0, 0.0},
24 >        {0.0, 1.0, 0.0, 0.0},
25 >        {0.0, 0.0, 1.0, 0.0},
26 >        {0.0, 0.0, 0.0, 1.0}},
27 >        1.0
28 > };
29  
30 + XF  funcxf;                     /* current transformation */
31 + static OBJREC  *fobj = NULL;    /* current function object */
32 + static RAY  *fray = NULL;       /* current function ray */
33  
34 < setmap(m, r, xfm, sca)          /* set channels for function call */
35 < OBJREC  *m;
36 < register RAY  *r;
37 < double  xfm[4][4];
38 < double  sca;
34 > static char  rayinitcal[] = INITFILE;
35 >
36 > static double  l_erf(char *), l_erfc(char *), l_arg(char *);
37 >
38 >
39 > void
40 > initfunc(void)  /* initialize function evaluation */
41   {
42 <        extern double  l_noise3(), l_noise3a(), l_noise3b(), l_noise3c();
43 <        extern double  l_hermite(), l_fnoise3(), l_arg();
44 <        extern long  eclock;
45 <        static char  *initfile = "rayinit.cal";
42 >        if (!rayinitcal[0])     /* already done? */
43 >                return;
44 >        esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW;
45 >        esupport &= ~(E_OUTCHAN);
46 >        calcontext("");
47 >        scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
48 >        scompile("Nx=$4;Ny=$5;Nz=$6;", NULL, 0);
49 >        scompile("Px=$7;Py=$8;Pz=$9;", NULL, 0);
50 >        scompile("T=$10;Ts=$25;Rdot=$11;", NULL, 0);
51 >        scompile("S=$12;Tx=$13;Ty=$14;Tz=$15;", NULL, 0);
52 >        scompile("Ix=$16;Iy=$17;Iz=$18;", NULL, 0);
53 >        scompile("Jx=$19;Jy=$20;Jz=$21;", NULL, 0);
54 >        scompile("Kx=$22;Ky=$23;Kz=$24;", NULL, 0);
55 >        scompile("Lu=$26;Lv=$27;", NULL, 0);
56 >        funset("arg", 1, '=', l_arg);
57 >        funset("erf", 1, ':', l_erf);
58 >        funset("erfc", 1, ':', l_erfc);
59 >        setnoisefuncs();
60 >        setprismfuncs();
61 >        loadfunc(rayinitcal);
62 >        rayinitcal[0] = '\0';
63 >        doptimize(1);           /* optimize definitions */
64 > }
65  
66 <        if (initfile != NULL) {
67 <                loadfunc(initfile);
68 <                scompile(NULL, "Dx=$1;Dy=$2;Dz=$3;");
69 <                scompile(NULL, "Nx=$4;Ny=$5;Nz=$6;");
70 <                scompile(NULL, "Px=$7;Py=$8;Pz=$9;");
71 <                scompile(NULL, "T=$10;Rdot=$11;");
72 <                funset("arg", 1, l_arg);
73 <                funset("noise3", 3, l_noise3);
74 <                funset("noise3a", 3, l_noise3a);
75 <                funset("noise3b", 3, l_noise3b);
76 <                funset("noise3c", 3, l_noise3c);
77 <                funset("hermite", 5, l_hermite);
78 <                funset("fnoise3", 3, l_fnoise3);
79 <                initfile = NULL;
66 >
67 > /* Set parameters for current evaluation */
68 > void
69 > set_eparams(const char *prms)
70 > {
71 >        static const char       *last_params = NULL;
72 >        char                    vname[RMAXWORD];
73 >        double                  value;
74 >        char                    *cpd;
75 >                                        /* check if already set */
76 >        if (prms == NULL || !*prms)
77 >                return;
78 >        if (prms == last_params || (last_params != NULL &&
79 >                                        !strcmp(prms, last_params)))
80 >                return;
81 >        last_params = prms;             /* XXX assumes static string */
82 >                                        /* assign each variable */
83 >        while (*prms) {
84 >                if (isspace(*prms)) {
85 >                        ++prms; continue;
86 >                }
87 >                if (!isalpha(*prms))
88 >                        goto bad_params;
89 >                cpd = vname;
90 >                while (*prms && (*prms != '=') & !isspace(*prms)) {
91 >                        if (!isid(*prms) | (cpd-vname >= RMAXWORD-1))
92 >                                goto bad_params;
93 >                        *cpd++ = *prms++;
94 >                }
95 >                *cpd = '\0';
96 >                while (isspace(*prms)) prms++;
97 >                if (*prms++ != '=')
98 >                        goto bad_params;
99 >                value = atof(prms);
100 >                if ((prms = fskip((char *)prms)) == NULL)
101 >                        goto bad_params;
102 >                while (isspace(*prms)) prms++;
103 >                prms += (*prms == ',') | (*prms == ';') | (*prms == ':');
104 >                varset(vname, '=', value);
105          }
54        fobj = m;
55        fray = r;
56        fxf.sca = r->robs * sca;
57        multmat4(fxf.xfm, r->robx, xfm);
106          eclock++;               /* notify expression evaluator */
107 +        return;
108 + bad_params:
109 +        sprintf(errmsg, "bad parameter list '%s'", last_params);
110 +        error(USER, errmsg);
111   }
112  
113  
114 < setfunc(m, r)                           /* simplified interface to setmap */
115 < register OBJREC  *m;
116 < RAY  *r;
114 > MFUNC *
115 > getfunc(        /* get function for this modifier */
116 >        OBJREC  *m,
117 >        int  ff,
118 >        unsigned int  ef,
119 >        int  dofwd
120 > )
121   {
122 <        register XF  *mxf;
123 <
124 <        if ((mxf = (XF *)m->os) == NULL) {
125 <                register int  n;
126 <                register char  **sa;
127 <
128 <                for (n = m->oargs.nsargs, sa = m->oargs.sarg;
129 <                                n > 0 && **sa != '-'; n--, sa++)
130 <                        ;
131 <                mxf = (XF *)malloc(sizeof(XF));
132 <                if (mxf == NULL)
122 >        char  sbuf[MAXSTR];
123 >        char  **arg;
124 >        MFUNC  *f;
125 >        int  ne, na;
126 >        int  i;
127 >                                        /* check to see if done already */
128 >        if ((f = (MFUNC *)m->os) != NULL)
129 >                return(f);
130 >        fobj = NULL; fray = NULL;
131 >        if ((na = m->oargs.nsargs) <= ff)
132 >                goto toofew;
133 >        arg = m->oargs.sarg;
134 >        if ((f = (MFUNC *)calloc(1, sizeof(MFUNC))) == NULL)
135 >                goto memerr;
136 >        i = strlen(arg[ff]);                    /* set up context */
137 >        if (i == 1 && arg[ff][0] == '.') {
138 >                calcontext(f->ctx = "");        /* "." means no file */
139 >        } else {
140 >                strcpy(sbuf,arg[ff]);           /* file name is context */
141 >                if (i > LCALSUF && !strcmp(sbuf+i-LCALSUF, CALSUF))
142 >                        sbuf[i-LCALSUF] = '\0'; /* remove suffix */
143 >                calcontext(f->ctx = savestr(sbuf));
144 >                if (!vardefined(REFVNAME)) {    /* file loaded? */
145 >                        loadfunc(arg[ff]);
146 >                        varset(REFVNAME, '=', 1.0);
147 >                } else                          /* reference_count++ */
148 >                        varset(REFVNAME, '=', varvalue(REFVNAME)+1.0);
149 >        }
150 >        ecurfunc = NULL;                        /* parse expressions */
151 >        sprintf(sbuf, "%s \"%s\"", ofun[m->otype].funame, m->oname);
152 >        for (i=0, ne=0; ef && i < na; i++, ef>>=1)
153 >                if (ef & 1) {                   /* flagged as an expression? */
154 >                        if (ne >= MAXEXPR)
155 >                                objerror(m, INTERNAL, "too many expressions");
156 >                        initstr(arg[i], sbuf, 0);
157 >                        f->ep[ne++] = getE1();
158 >                        if (nextc != EOF)
159 >                                esyntax("unexpected character");
160 >                }
161 >        if (ef)
162 >                goto toofew;
163 >        if (i <= ff)                    /* find transform args */
164 >                i = ff+1;
165 >        while (i < na && !isxfopt(arg[i]))
166 >                i++;
167 >        if (i == na) {                  /* no transform */
168 >                f->fxp = f->bxp = &unitxf;
169 >        } else {                        /* get transform */
170 >                if ((f->bxp = (XF *)malloc(sizeof(XF))) == NULL)
171                          goto memerr;
172 <                mxf->sca = 1.0;
79 <                setident4(mxf->xfm);
80 <                if (invxf(mxf->xfm, &mxf->sca, n, sa) != n)
172 >                if (invxf(f->bxp, na-i, arg+i) != na-i)
173                          objerror(m, USER, "bad transform");
174 <                if (mxf->sca < 0.0)
175 <                        mxf->sca = -mxf->sca;
176 <                m->os = (char *)mxf;
174 >                if (f->bxp->sca < 0.0)
175 >                        f->bxp->sca = -f->bxp->sca;
176 >                if (dofwd) {                    /* do both transforms */
177 >                        if ((f->fxp = (XF *)malloc(sizeof(XF))) == NULL)
178 >                                goto memerr;
179 >                        xf(f->fxp, na-i, arg+i);
180 >                        if (f->fxp->sca < 0.0)
181 >                                f->fxp->sca = -f->fxp->sca;
182 >                }
183          }
184 <        setmap(m, r, mxf->xfm, mxf->sca);
185 <        return;
184 >        m->os = (char *)f;
185 >        return(f);
186 > toofew:
187 >        objerror(m, USER, "too few string arguments");
188   memerr:
189 <        error(SYSTEM, "out of memory in setfunc");
190 < #undef  mxf
189 >        error(SYSTEM, "out of memory in getfunc");
190 >        return NULL; /* pro forma return */
191   }
192  
193  
194 < loadfunc(fname)                 /* load definition file */
195 < char  *fname;
194 > void
195 > freefunc(                       /* free memory associated with modifier */
196 >        OBJREC  *m
197 > )
198   {
199 <        extern char  *libpath;          /* library search path */
199 >        MFUNC  *f;
200 >        int  i;
201 >
202 >        if ((f = (MFUNC *)m->os) == NULL)
203 >                return;
204 >        for (i = 0; f->ep[i] != NULL; i++)
205 >                epfree(f->ep[i],1);
206 >        if (f->ctx[0]) {                        /* done with definitions */
207 >                calcontext(f->ctx);
208 >                i = varvalue(REFVNAME)-.5;      /* reference_count-- */
209 >                if (i > 0)
210 >                        varset(REFVNAME, '=', (double)i);
211 >                else
212 >                        dcleanup(2);            /* remove definitions */
213 >                freestr(f->ctx);
214 >        }
215 >        if (f->bxp != &unitxf)
216 >                free((void *)f->bxp);
217 >        if ((f->fxp != NULL) & (f->fxp != &unitxf))
218 >                free((void *)f->fxp);
219 >        free((void *)f);
220 >        m->os = NULL;
221 > }
222 >
223 >
224 > int
225 > setfunc(                        /* set channels for function call */
226 >        OBJREC  *m,
227 >        RAY     *r
228 > )
229 > {
230 >        static RNUMBER  lastrno = ~0;
231 >        MFUNC           *f;
232 >                                        /* get function if any */
233 >        if ((f = (MFUNC *)m->os) == NULL)
234 >                objerror(m, CONSISTENCY, "setfunc called before getfunc");
235 >                
236 >        calcontext(f->ctx);             /* set evaluator context */
237 >                                        /* check to see if matrix set */
238 >        if ((m == fobj) & (r->rno == lastrno))
239 >                return(0);
240 >        fobj = m;
241 >        fray = r;
242 >        if (r->rox != NULL) {
243 >                if (f->bxp != &unitxf) {
244 >                        funcxf.sca = r->rox->b.sca * f->bxp->sca;
245 >                        multmat4(funcxf.xfm, r->rox->b.xfm, f->bxp->xfm);
246 >                } else
247 >                        funcxf = r->rox->b;
248 >        } else
249 >                funcxf = *f->bxp;
250 >        lastrno = r->rno;
251 >        eclock++;               /* notify expression evaluator */
252 >        return(1);
253 > }
254 >
255 >
256 > int
257 > worldfunc(                      /* special function context sans object */
258 >        const char      *ctx,
259 >        RAY     *r
260 > )
261 > {
262 >        static RNUMBER  lastrno = ~0;
263 >                                        /* set evaluator context */
264 >        calcontext((char *)ctx);
265 >                                        /* check if ray already set */
266 >        if ((fobj == NULL) & (r->rno == lastrno))
267 >                return(0);
268 >        fobj = NULL;
269 >        fray = r;
270 >        funcxf = unitxf;
271 >        lastrno = r->rno;
272 >        eclock++;               /* notify expression evaluator */
273 >        return(1);
274 > }
275 >
276 >
277 > void
278 > loadfunc(                       /* load definition file */
279 >        char  *fname
280 > )
281 > {
282          char  *ffname;
283  
284 <        if ((ffname = getpath(fname, libpath)) == NULL) {
284 >        if ((ffname = getpath(fname, getrlibpath(), R_OK)) == NULL) {
285                  sprintf(errmsg, "cannot find function file \"%s\"", fname);
286 <                error(USER, errmsg);
286 >                error(SYSTEM, errmsg);
287          }
288          fcompile(ffname);
289   }
290  
291  
292 < double
293 < l_arg()                         /* return nth real argument */
292 > static double
293 > l_arg(char *nm)                 /* return nth real argument */
294   {
295 <        extern double  argument();
112 <        register int  n;
295 >        int  n;
296  
297 +        if (fobj == NULL)
298 +                error(USER,
299 +                        "bad call to arg(n) - illegal constant in .cal file?");
300 +
301          n = argument(1) + .5;           /* round to integer */
302  
303          if (n < 1)
# Line 124 | Line 311 | l_arg()                                /* return nth real argument */
311   }
312  
313  
314 + static double
315 + l_erf(char *nm)                 /* error function */
316 + {
317 +        return(erf(argument(1)));
318 + }
319 +
320 +
321 + static double
322 + l_erfc(char *nm)                /* cumulative error function */
323 + {
324 +        return(erfc(argument(1)));
325 + }
326 +
327 +
328   double
329 < chanvalue(n)                    /* return channel n to calcomp */
330 < register int  n;
329 > chanvalue(                      /* return channel n to calcomp */
330 >        int  n
331 > )
332   {
333 <        double  res;
334 <        register RAY  *r;
333 >        if (fray == NULL)
334 >                esyntax("ray parameter used in constant expression");
335  
336 <        n--;                                    /* for convenience */
336 >        if (--n < 0)
337 >                goto badchan;
338  
339 <        if (n < 0 || n > 10)
137 <                error(USER, "illegal channel number");
339 >        if (n < 3)                      /* ray direction */
340  
341 <        if (n == 9) {                           /* distance */
341 >                return( (       fray->rdir[0]*funcxf.xfm[0][n] +
342 >                                fray->rdir[1]*funcxf.xfm[1][n] +
343 >                                fray->rdir[2]*funcxf.xfm[2][n]  )
344 >                         / funcxf.sca );
345  
346 <                res = fray->rot;
142 <                for (r = fray->parent; r != NULL; r = r->parent)
143 <                        res += r->rot;
144 <                res *= fxf.sca;
346 >        if (n < 6)                      /* surface normal */
347  
348 <        } else if (n == 10) {                   /* dot product */
348 >                return( (       fray->ron[0]*funcxf.xfm[0][n-3] +
349 >                                fray->ron[1]*funcxf.xfm[1][n-3] +
350 >                                fray->ron[2]*funcxf.xfm[2][n-3] )
351 >                         / funcxf.sca );
352  
353 <                res = fray->rod;
353 >        if (n < 9)                      /* intersection point */
354  
355 <        } else if (n < 3) {                     /* ray direction */
356 <                        res = ( fray->rdir[0]*fxf.xfm[0][n] +
357 <                                        fray->rdir[1]*fxf.xfm[1][n] +
358 <                                        fray->rdir[2]*fxf.xfm[2][n]     )
154 <                                 / fxf.sca ;
155 <        } else if (n < 6) {                     /* surface normal */
156 <                        res = ( fray->ron[0]*fxf.xfm[0][n-3] +
157 <                                        fray->ron[1]*fxf.xfm[1][n-3] +
158 <                                        fray->ron[2]*fxf.xfm[2][n-3]    )
159 <                                 / fxf.sca ;
160 <        } else {                                /* intersection */
161 <                        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] ;
165 <        }
355 >                return( fray->rop[0]*funcxf.xfm[0][n-6] +
356 >                                fray->rop[1]*funcxf.xfm[1][n-6] +
357 >                                fray->rop[2]*funcxf.xfm[2][n-6] +
358 >                                             funcxf.xfm[3][n-6] );
359  
360 <        return(res);
360 >        if (n == 9)                     /* total distance */
361 >                return(raydist(fray,PRIMARY) * funcxf.sca);
362 >
363 >        if (n == 10)                    /* dot product (range [-1,1]) */
364 >                return( fray->rod <= -1.0 ? -1.0 :
365 >                        fray->rod >= 1.0 ? 1.0 :
366 >                        fray->rod );
367 >
368 >        if (n == 11)                    /* scale */
369 >                return(funcxf.sca);
370 >
371 >        if (n < 15)                     /* origin */
372 >                return(funcxf.xfm[3][n-12]);
373 >
374 >        if (n < 18)                     /* i unit vector */
375 >                return(funcxf.xfm[0][n-15] / funcxf.sca);
376 >
377 >        if (n < 21)                     /* j unit vector */
378 >                return(funcxf.xfm[1][n-18] / funcxf.sca);
379 >
380 >        if (n < 24)                     /* k unit vector */
381 >                return(funcxf.xfm[2][n-21] / funcxf.sca);
382 >
383 >        if (n == 24)                    /* single ray (shadow) distance */
384 >                return((fray->rot+raydist(fray->parent,SHADOW)) * funcxf.sca);
385 >
386 >        if (n < 27)                     /* local (u,v) coordinates */
387 >                return(fray->uv[n-25]);
388 > badchan:
389 >        error(USER, "illegal channel number");
390 >        return(0.0);
391   }

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)