| 1 | greg | 1.1 | /* Copyright (c) 1986 Regents of the University of California */ | 
| 2 |  |  |  | 
| 3 |  |  | #ifndef lint | 
| 4 |  |  | static char SCCSid[] = "$SunId$ LBL"; | 
| 5 |  |  | #endif | 
| 6 |  |  |  | 
| 7 |  |  | /* | 
| 8 |  |  | *  func.c - interface to calcomp functions. | 
| 9 |  |  | * | 
| 10 |  |  | *     4/7/86 | 
| 11 |  |  | */ | 
| 12 |  |  |  | 
| 13 |  |  | #include  "ray.h" | 
| 14 |  |  |  | 
| 15 |  |  | #include  "otypes.h" | 
| 16 |  |  |  | 
| 17 |  |  |  | 
| 18 |  |  | typedef struct { | 
| 19 | greg | 1.3 | double  xfm[4][4];              /* transform matrix */ | 
| 20 | greg | 1.1 | double  sca;                    /* scalefactor */ | 
| 21 |  |  | }  XF; | 
| 22 |  |  |  | 
| 23 |  |  | static OBJREC  *fobj;           /* current function object */ | 
| 24 |  |  | static RAY  *fray;              /* current function ray */ | 
| 25 |  |  | static XF  fxf;                 /* current transformation */ | 
| 26 |  |  |  | 
| 27 |  |  |  | 
| 28 | greg | 1.3 | setmap(m, r, xfm, sca)          /* set channels for function call */ | 
| 29 | greg | 1.1 | OBJREC  *m; | 
| 30 |  |  | register RAY  *r; | 
| 31 | greg | 1.3 | double  xfm[4][4]; | 
| 32 | greg | 1.1 | double  sca; | 
| 33 |  |  | { | 
| 34 |  |  | extern double  l_noise3(), l_noise3a(), l_noise3b(), l_noise3c(); | 
| 35 |  |  | extern double  l_hermite(), l_fnoise3(), l_arg(); | 
| 36 |  |  | extern long  eclock; | 
| 37 |  |  | static char  *initfile = "rayinit.cal"; | 
| 38 |  |  |  | 
| 39 |  |  | if (initfile != NULL) { | 
| 40 |  |  | loadfunc(initfile); | 
| 41 | greg | 1.9 | scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0); | 
| 42 |  |  | scompile("Nx=$4;Ny=$5;Nz=$6;", NULL, 0); | 
| 43 |  |  | scompile("Px=$7;Py=$8;Pz=$9;", NULL, 0); | 
| 44 |  |  | scompile("T=$10;Rdot=$11;", NULL, 0); | 
| 45 |  |  | scompile("S=$12;Tx=$13;Ty=$14;Tz=$15;", NULL, 0); | 
| 46 |  |  | scompile("Ix=$16;Iy=$17;Iz=$18;", NULL, 0); | 
| 47 |  |  | scompile("Jx=$19;Jy=$20;Jz=$21;", NULL, 0); | 
| 48 |  |  | scompile("Kx=$22;Ky=$23;Kz=$24;", NULL, 0); | 
| 49 | greg | 1.1 | funset("arg", 1, l_arg); | 
| 50 |  |  | funset("noise3", 3, l_noise3); | 
| 51 |  |  | funset("noise3a", 3, l_noise3a); | 
| 52 |  |  | funset("noise3b", 3, l_noise3b); | 
| 53 |  |  | funset("noise3c", 3, l_noise3c); | 
| 54 |  |  | funset("hermite", 5, l_hermite); | 
| 55 |  |  | funset("fnoise3", 3, l_fnoise3); | 
| 56 |  |  | initfile = NULL; | 
| 57 |  |  | } | 
| 58 |  |  | fobj = m; | 
| 59 |  |  | fray = r; | 
| 60 | greg | 1.4 | fxf.sca = r->robs * sca; | 
| 61 |  |  | multmat4(fxf.xfm, r->robx, xfm); | 
| 62 | greg | 1.1 | eclock++;               /* notify expression evaluator */ | 
| 63 |  |  | } | 
| 64 |  |  |  | 
| 65 |  |  |  | 
| 66 |  |  | setfunc(m, r)                           /* simplified interface to setmap */ | 
| 67 |  |  | register OBJREC  *m; | 
| 68 |  |  | RAY  *r; | 
| 69 |  |  | { | 
| 70 | greg | 1.2 | register XF  *mxf; | 
| 71 | greg | 1.1 |  | 
| 72 | greg | 1.2 | if ((mxf = (XF *)m->os) == NULL) { | 
| 73 | greg | 1.5 | register int  n; | 
| 74 |  |  | register char  **sa; | 
| 75 | greg | 1.1 |  | 
| 76 | greg | 1.5 | for (n = m->oargs.nsargs, sa = m->oargs.sarg; | 
| 77 |  |  | n > 0 && **sa != '-'; n--, sa++) | 
| 78 |  |  | ; | 
| 79 | greg | 1.1 | mxf = (XF *)malloc(sizeof(XF)); | 
| 80 |  |  | if (mxf == NULL) | 
| 81 |  |  | goto memerr; | 
| 82 |  |  | if (invxf(mxf->xfm, &mxf->sca, n, sa) != n) | 
| 83 |  |  | objerror(m, USER, "bad transform"); | 
| 84 |  |  | if (mxf->sca < 0.0) | 
| 85 |  |  | mxf->sca = -mxf->sca; | 
| 86 | greg | 1.2 | m->os = (char *)mxf; | 
| 87 | greg | 1.1 | } | 
| 88 | greg | 1.3 | setmap(m, r, mxf->xfm, mxf->sca); | 
| 89 | greg | 1.1 | return; | 
| 90 |  |  | memerr: | 
| 91 |  |  | error(SYSTEM, "out of memory in setfunc"); | 
| 92 |  |  | #undef  mxf | 
| 93 |  |  | } | 
| 94 |  |  |  | 
| 95 |  |  |  | 
| 96 |  |  | loadfunc(fname)                 /* load definition file */ | 
| 97 |  |  | char  *fname; | 
| 98 |  |  | { | 
| 99 |  |  | extern char  *libpath;          /* library search path */ | 
| 100 |  |  | char  *ffname; | 
| 101 |  |  |  | 
| 102 | greg | 1.6 | if ((ffname = getpath(fname, libpath, R_OK)) == NULL) { | 
| 103 | greg | 1.1 | sprintf(errmsg, "cannot find function file \"%s\"", fname); | 
| 104 |  |  | error(USER, errmsg); | 
| 105 |  |  | } | 
| 106 |  |  | fcompile(ffname); | 
| 107 |  |  | } | 
| 108 |  |  |  | 
| 109 |  |  |  | 
| 110 |  |  | double | 
| 111 |  |  | l_arg()                         /* return nth real argument */ | 
| 112 |  |  | { | 
| 113 |  |  | extern double  argument(); | 
| 114 |  |  | register int  n; | 
| 115 |  |  |  | 
| 116 |  |  | n = argument(1) + .5;           /* round to integer */ | 
| 117 |  |  |  | 
| 118 |  |  | if (n < 1) | 
| 119 |  |  | return(fobj->oargs.nfargs); | 
| 120 |  |  |  | 
| 121 |  |  | if (n > fobj->oargs.nfargs) { | 
| 122 |  |  | sprintf(errmsg, "missing real argument %d", n); | 
| 123 |  |  | objerror(fobj, USER, errmsg); | 
| 124 |  |  | } | 
| 125 |  |  | return(fobj->oargs.farg[n-1]); | 
| 126 |  |  | } | 
| 127 |  |  |  | 
| 128 |  |  |  | 
| 129 |  |  | double | 
| 130 |  |  | chanvalue(n)                    /* return channel n to calcomp */ | 
| 131 |  |  | register int  n; | 
| 132 |  |  | { | 
| 133 | greg | 1.8 | double  sum; | 
| 134 | greg | 1.1 | register RAY  *r; | 
| 135 |  |  |  | 
| 136 | greg | 1.10 | if (--n < 0) | 
| 137 |  |  | goto badchan; | 
| 138 | greg | 1.1 |  | 
| 139 | greg | 1.8 | if (n < 3)                      /* ray direction */ | 
| 140 | greg | 1.1 |  | 
| 141 | greg | 1.8 | return( (       fray->rdir[0]*fxf.xfm[0][n] + | 
| 142 |  |  | fray->rdir[1]*fxf.xfm[1][n] + | 
| 143 |  |  | fray->rdir[2]*fxf.xfm[2][n]     ) | 
| 144 |  |  | / fxf.sca ); | 
| 145 | greg | 1.1 |  | 
| 146 | greg | 1.8 | if (n < 6)                      /* surface normal */ | 
| 147 |  |  |  | 
| 148 |  |  | return( (       fray->ron[0]*fxf.xfm[0][n-3] + | 
| 149 |  |  | fray->ron[1]*fxf.xfm[1][n-3] + | 
| 150 |  |  | fray->ron[2]*fxf.xfm[2][n-3]    ) | 
| 151 |  |  | / fxf.sca ); | 
| 152 |  |  |  | 
| 153 |  |  | if (n < 9)                      /* intersection */ | 
| 154 |  |  |  | 
| 155 |  |  | return( fray->rop[0]*fxf.xfm[0][n-6] + | 
| 156 |  |  | fray->rop[1]*fxf.xfm[1][n-6] + | 
| 157 |  |  | fray->rop[2]*fxf.xfm[2][n-6] + | 
| 158 |  |  | fxf.xfm[3][n-6] ); | 
| 159 |  |  |  | 
| 160 | greg | 1.10 | if (n == 9) {                   /* distance */ | 
| 161 |  |  |  | 
| 162 |  |  | sum = fray->rot; | 
| 163 |  |  | for (r = fray->parent; r != NULL; r = r->parent) | 
| 164 |  |  | sum += r->rot; | 
| 165 |  |  | return(sum * fxf.sca); | 
| 166 |  |  |  | 
| 167 |  |  | } | 
| 168 |  |  | if (n == 10)                    /* dot product */ | 
| 169 |  |  | return(fray->rod); | 
| 170 |  |  |  | 
| 171 | greg | 1.8 | if (n == 11)                    /* scale */ | 
| 172 |  |  | return(fxf.sca); | 
| 173 |  |  |  | 
| 174 |  |  | if (n < 15)                     /* origin */ | 
| 175 |  |  | return(fxf.xfm[3][n-12]); | 
| 176 |  |  |  | 
| 177 |  |  | if (n < 18)                     /* i unit vector */ | 
| 178 |  |  | return(fxf.xfm[0][n-15] / fxf.sca); | 
| 179 |  |  |  | 
| 180 |  |  | if (n < 21)                     /* j unit vector */ | 
| 181 |  |  | return(fxf.xfm[1][n-15] / fxf.sca); | 
| 182 |  |  |  | 
| 183 |  |  | if (n < 24)                     /* k unit vector */ | 
| 184 |  |  | return(fxf.xfm[2][n-21] / fxf.sca); | 
| 185 | greg | 1.10 | badchan: | 
| 186 |  |  | error(USER, "illegal channel number"); | 
| 187 | greg | 1.1 | } |