| 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 | 
  | 
  | 
        double  sca;                    /* scalefactor */ | 
| 20 | 
  | 
  | 
        double  xfm[4][4];              /* transform matrix */ | 
| 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 | 
  | 
  | 
setmap(m, r, sca, xfm)          /* set channels for function call */ | 
| 29 | 
  | 
  | 
OBJREC  *m; | 
| 30 | 
  | 
  | 
register RAY  *r; | 
| 31 | 
  | 
  | 
double  sca; | 
| 32 | 
  | 
  | 
double  xfm[4][4]; | 
| 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 | 
  | 
  | 
                scompile(NULL, "Dx=$1;Dy=$2;Dz=$3;"); | 
| 42 | 
  | 
  | 
                scompile(NULL, "Nx=$4;Ny=$5;Nz=$6;"); | 
| 43 | 
  | 
  | 
                scompile(NULL, "Px=$7;Py=$8;Pz=$9;"); | 
| 44 | 
  | 
  | 
                scompile(NULL, "T=$10;Rdot=$11;"); | 
| 45 | 
  | 
  | 
                funset("arg", 1, l_arg); | 
| 46 | 
  | 
  | 
                funset("noise3", 3, l_noise3); | 
| 47 | 
  | 
  | 
                funset("noise3a", 3, l_noise3a); | 
| 48 | 
  | 
  | 
                funset("noise3b", 3, l_noise3b); | 
| 49 | 
  | 
  | 
                funset("noise3c", 3, l_noise3c); | 
| 50 | 
  | 
  | 
                funset("hermite", 5, l_hermite); | 
| 51 | 
  | 
  | 
                funset("fnoise3", 3, l_fnoise3); | 
| 52 | 
  | 
  | 
                initfile = NULL; | 
| 53 | 
  | 
  | 
        } | 
| 54 | 
  | 
  | 
        fobj = m; | 
| 55 | 
  | 
  | 
        fray = r; | 
| 56 | 
  | 
  | 
        fxf.sca = r->ros * sca; | 
| 57 | 
  | 
  | 
        multmat4(fxf.xfm, r->rox, xfm); | 
| 58 | 
  | 
  | 
        eclock++;               /* notify expression evaluator */ | 
| 59 | 
  | 
  | 
} | 
| 60 | 
  | 
  | 
 | 
| 61 | 
  | 
  | 
 | 
| 62 | 
  | 
  | 
setfunc(m, r)                           /* simplified interface to setmap */ | 
| 63 | 
  | 
  | 
register OBJREC  *m; | 
| 64 | 
  | 
  | 
RAY  *r; | 
| 65 | 
  | 
  | 
{ | 
| 66 | 
greg | 
1.2 | 
        register XF  *mxf; | 
| 67 | 
greg | 
1.1 | 
 | 
| 68 | 
greg | 
1.2 | 
        if ((mxf = (XF *)m->os) == NULL) { | 
| 69 | 
greg | 
1.1 | 
                register int  n = m->oargs.nsargs; | 
| 70 | 
  | 
  | 
                register char  **sa = m->oargs.sarg; | 
| 71 | 
  | 
  | 
 | 
| 72 | 
  | 
  | 
                while (n > 0 && **sa != '-') { | 
| 73 | 
  | 
  | 
                        n--; | 
| 74 | 
  | 
  | 
                        sa++; | 
| 75 | 
  | 
  | 
                } | 
| 76 | 
  | 
  | 
                mxf = (XF *)malloc(sizeof(XF)); | 
| 77 | 
  | 
  | 
                if (mxf == NULL) | 
| 78 | 
  | 
  | 
                        goto memerr; | 
| 79 | 
  | 
  | 
                mxf->sca = 1.0; | 
| 80 | 
  | 
  | 
                setident4(mxf->xfm); | 
| 81 | 
  | 
  | 
                if (invxf(mxf->xfm, &mxf->sca, n, sa) != n) | 
| 82 | 
  | 
  | 
                        objerror(m, USER, "bad transform"); | 
| 83 | 
  | 
  | 
                if (mxf->sca < 0.0) | 
| 84 | 
  | 
  | 
                        mxf->sca = -mxf->sca; | 
| 85 | 
greg | 
1.2 | 
                m->os = (char *)mxf; | 
| 86 | 
greg | 
1.1 | 
        } | 
| 87 | 
  | 
  | 
        setmap(m, r, mxf->sca, mxf->xfm); | 
| 88 | 
  | 
  | 
        return; | 
| 89 | 
  | 
  | 
memerr: | 
| 90 | 
  | 
  | 
        error(SYSTEM, "out of memory in setfunc"); | 
| 91 | 
  | 
  | 
#undef  mxf | 
| 92 | 
  | 
  | 
} | 
| 93 | 
  | 
  | 
 | 
| 94 | 
  | 
  | 
 | 
| 95 | 
  | 
  | 
loadfunc(fname)                 /* load definition file */ | 
| 96 | 
  | 
  | 
char  *fname; | 
| 97 | 
  | 
  | 
{ | 
| 98 | 
  | 
  | 
        extern char  *libpath;          /* library search path */ | 
| 99 | 
  | 
  | 
        char  *ffname; | 
| 100 | 
  | 
  | 
 | 
| 101 | 
  | 
  | 
        if ((ffname = getpath(fname, libpath)) == NULL) { | 
| 102 | 
  | 
  | 
                sprintf(errmsg, "cannot find function file \"%s\"", fname); | 
| 103 | 
  | 
  | 
                error(USER, errmsg); | 
| 104 | 
  | 
  | 
        } | 
| 105 | 
  | 
  | 
        fcompile(ffname); | 
| 106 | 
  | 
  | 
} | 
| 107 | 
  | 
  | 
 | 
| 108 | 
  | 
  | 
 | 
| 109 | 
  | 
  | 
double | 
| 110 | 
  | 
  | 
l_arg()                         /* return nth real argument */ | 
| 111 | 
  | 
  | 
{ | 
| 112 | 
  | 
  | 
        extern double  argument(); | 
| 113 | 
  | 
  | 
        register int  n; | 
| 114 | 
  | 
  | 
 | 
| 115 | 
  | 
  | 
        n = argument(1) + .5;           /* round to integer */ | 
| 116 | 
  | 
  | 
 | 
| 117 | 
  | 
  | 
        if (n < 1) | 
| 118 | 
  | 
  | 
                return(fobj->oargs.nfargs); | 
| 119 | 
  | 
  | 
 | 
| 120 | 
  | 
  | 
        if (n > fobj->oargs.nfargs) { | 
| 121 | 
  | 
  | 
                sprintf(errmsg, "missing real argument %d", n); | 
| 122 | 
  | 
  | 
                objerror(fobj, USER, errmsg); | 
| 123 | 
  | 
  | 
        } | 
| 124 | 
  | 
  | 
        return(fobj->oargs.farg[n-1]); | 
| 125 | 
  | 
  | 
} | 
| 126 | 
  | 
  | 
 | 
| 127 | 
  | 
  | 
 | 
| 128 | 
  | 
  | 
double | 
| 129 | 
  | 
  | 
chanvalue(n)                    /* return channel n to calcomp */ | 
| 130 | 
  | 
  | 
register int  n; | 
| 131 | 
  | 
  | 
{ | 
| 132 | 
  | 
  | 
        double  res; | 
| 133 | 
  | 
  | 
        register RAY  *r; | 
| 134 | 
  | 
  | 
 | 
| 135 | 
  | 
  | 
        n--;                                    /* for convenience */ | 
| 136 | 
  | 
  | 
 | 
| 137 | 
  | 
  | 
        if (n < 0 || n > 10) | 
| 138 | 
  | 
  | 
                error(USER, "illegal channel number"); | 
| 139 | 
  | 
  | 
 | 
| 140 | 
  | 
  | 
        if (n == 9) {                           /* distance */ | 
| 141 | 
  | 
  | 
 | 
| 142 | 
  | 
  | 
                res = fray->rot; | 
| 143 | 
  | 
  | 
                for (r = fray->parent; r != NULL; r = r->parent) | 
| 144 | 
  | 
  | 
                        res += r->rot; | 
| 145 | 
  | 
  | 
                res *= fxf.sca; | 
| 146 | 
  | 
  | 
 | 
| 147 | 
  | 
  | 
        } else if (n == 10) {                   /* dot product */ | 
| 148 | 
  | 
  | 
 | 
| 149 | 
  | 
  | 
                res = fray->rod; | 
| 150 | 
  | 
  | 
 | 
| 151 | 
  | 
  | 
        } else if (n < 3) {                     /* ray direction */ | 
| 152 | 
  | 
  | 
                        res = ( fray->rdir[0]*fxf.xfm[0][n] + | 
| 153 | 
  | 
  | 
                                        fray->rdir[1]*fxf.xfm[1][n] + | 
| 154 | 
  | 
  | 
                                        fray->rdir[2]*fxf.xfm[2][n]     ) | 
| 155 | 
  | 
  | 
                                 / fxf.sca ; | 
| 156 | 
  | 
  | 
        } else if (n < 6) {                     /* surface normal */ | 
| 157 | 
  | 
  | 
                        res = ( fray->ron[0]*fxf.xfm[0][n-3] + | 
| 158 | 
  | 
  | 
                                        fray->ron[1]*fxf.xfm[1][n-3] + | 
| 159 | 
  | 
  | 
                                        fray->ron[2]*fxf.xfm[2][n-3]    ) | 
| 160 | 
  | 
  | 
                                 / fxf.sca ; | 
| 161 | 
  | 
  | 
        } else {                                /* intersection */ | 
| 162 | 
  | 
  | 
                        res =   fray->rop[0]*fxf.xfm[0][n-6] + | 
| 163 | 
  | 
  | 
                                        fray->rop[1]*fxf.xfm[1][n-6] + | 
| 164 | 
  | 
  | 
                                        fray->rop[2]*fxf.xfm[2][n-6] + | 
| 165 | 
  | 
  | 
                                                     fxf.xfm[3][n-6] ; | 
| 166 | 
  | 
  | 
        } | 
| 167 | 
  | 
  | 
 | 
| 168 | 
  | 
  | 
        return(res); | 
| 169 | 
  | 
  | 
} |