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

Comparing ray/src/cv/mgflib/xf.c (file contents):
Revision 1.2 by greg, Wed Jun 22 15:33:50 1994 UTC vs.
Revision 1.13 by gregl, Mon Dec 15 09:41:37 1997 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1994 Regents of the University of California */
1 > /* Copyright (c) 1995 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10  
11   #include <stdio.h>
12   #include <math.h>
13 + #include <stdlib.h>
14   #include <string.h>
15   #include "parser.h"
16  
# Line 21 | Line 22 | MAT4  m4ident = MAT4IDENT;
22  
23   static MAT4  m4tmp;             /* for efficiency */
24  
25 < int     xf_argc;                        /* total # transform args. */
26 < char    **xf_argv;                      /* transform arguments */
27 < XF_SPEC *xf_context;                    /* current context */
25 > XF_SPEC *xf_context;            /* current context */
26 > char    **xf_argend;            /* end of transform argument list */
27 > static char     **xf_argbeg;    /* beginning of transform argument list */
28  
28 static int      xf_maxarg;              /* # allocated arguments */
29  
30
30   int
31   xf_handler(ac, av)              /* handle xf entity */
32   int     ac;
33   char    **av;
34   {
36        register int    i;
35          register XF_SPEC        *spec;
36 <        XF      thisxf;
36 >        register int    n;
37 >        int     rv;
38  
39 <        if (ac == 1) {                  /* pop top transform */
39 >        if (ac == 1) {                  /* something with existing transform */
40                  if ((spec = xf_context) == NULL)
41 <                        return(MG_OK);          /* should be error? */
42 <                while (xf_argc > spec->xav0)
43 <                        free((MEM_PTR)xf_argv[--xf_argc]);
44 <                xf_argv[xf_argc] = NULL;
45 <                xf_context = spec->prev;
46 <                free((MEM_PTR)spec);
47 <                return(MG_OK);
41 >                        return(MG_ECNTXT);
42 >                n = -1;
43 >                if (spec->xarr != NULL) {       /* check for iteration */
44 >                        register struct xf_array        *ap = spec->xarr;
45 >
46 >                        (void)xf_aname((struct xf_array *)NULL);
47 >                        n = ap->ndim;
48 >                        while (n--) {
49 >                                if (++ap->aarg[n].i < ap->aarg[n].n)
50 >                                        break;
51 >                                (void)strcpy(ap->aarg[n].arg, "0");
52 >                                ap->aarg[n].i = 0;
53 >                        }
54 >                        if (n >= 0) {
55 >                                if ((rv = mg_fgoto(&ap->spos)) != MG_OK)
56 >                                        return(rv);
57 >                                sprintf(ap->aarg[n].arg, "%d", ap->aarg[n].i);
58 >                                (void)xf_aname(ap);
59 >                        }
60 >                }
61 >                if (n < 0) {                    /* pop transform */
62 >                        xf_context = spec->prev;
63 >                        free_xf(spec);
64 >                        return(MG_OK);
65 >                }
66 >        } else {                        /* else allocate transform */
67 >                if ((spec = new_xf(ac-1, av+1)) == NULL)
68 >                        return(MG_EMEM);
69 >                if (spec->xarr != NULL)
70 >                        (void)xf_aname(spec->xarr);
71 >                spec->prev = xf_context;        /* push onto stack */
72 >                xf_context = spec;
73          }
74                                          /* translate new specification */
75 <        if (xf(&thisxf, ac-1, av+1) != ac-1)
75 >        n = xf_ac(spec);
76 >        n -= xf_ac(spec->prev);         /* incremental comp. is more eff. */
77 >        if (xf(&spec->xf, n, xf_av(spec)) != n)
78                  return(MG_ETYPE);
79 <                                        /* allocate space for new transform */
80 <        spec = (XF_SPEC *)malloc(sizeof(XF_SPEC));
79 >                                        /* check for vertex reversal */
80 >        if ((spec->rev = (spec->xf.sca < 0.)))
81 >                spec->xf.sca = -spec->xf.sca;
82 >                                        /* compute total transformation */
83 >        if (spec->prev != NULL) {
84 >                multmat4(spec->xf.xfm, spec->xf.xfm, spec->prev->xf.xfm);
85 >                spec->xf.sca *= spec->prev->xf.sca;
86 >                spec->rev ^= spec->prev->rev;
87 >        }
88 >        spec->xid = comp_xfid(spec->xf.xfm);    /* compute unique ID */
89 >        return(MG_OK);
90 > }
91 >
92 >
93 > XF_SPEC *
94 > new_xf(ac, av)                  /* allocate new transform structure */
95 > int     ac;
96 > char    **av;
97 > {
98 >        register XF_SPEC        *spec;
99 >        register int    i;
100 >        char    *cp;
101 >        int     n, ndim;
102 >
103 >        ndim = 0;
104 >        n = 0;                          /* compute space req'd by arguments */
105 >        for (i = 0; i < ac; i++)
106 >                if (!strcmp(av[i], "-a")) {
107 >                        ndim++;
108 >                        i++;
109 >                } else
110 >                        n += strlen(av[i]) + 1;
111 >        if (ndim > XF_MAXDIM)
112 >                return(NULL);
113 >        spec = (XF_SPEC *)malloc(sizeof(XF_SPEC) + n);
114          if (spec == NULL)
115 <                return(MG_EMEM);
116 <        spec->xav0 = xf_argc;
117 <        spec->xac = ac-1;
115 >                return(NULL);
116 >        if (ndim) {
117 >                spec->xarr = (struct xf_array *)malloc(sizeof(struct xf_array));
118 >                if (spec->xarr == NULL)
119 >                        return(NULL);
120 >                mg_fgetpos(&spec->xarr->spos);
121 >                spec->xarr->ndim = 0;           /* incremented below */
122 >        } else
123 >                spec->xarr = NULL;
124 >        spec->xac = ac + xf_argc;
125                                          /* and store new xf arguments */
126 <        if (xf_argc+ac > xf_maxarg) {
127 <                if (!xf_maxarg)
128 <                        xf_argv = (char **)malloc(
129 <                                        (xf_maxarg=ac)*sizeof(char *));
130 <                else
131 <                        xf_argv = (char **)realloc((MEM_PTR)xf_argv,
132 <                                        (xf_maxarg+=ac)*sizeof(char *));
133 <                if (xf_argv == NULL)
134 <                        return(MG_EMEM);
126 >        if (xf_argbeg == NULL || xf_av(spec) < xf_argbeg) {
127 >                register char   **newav =
128 >                                (char **)malloc((spec->xac+1)*sizeof(char *));
129 >                if (newav == NULL)
130 >                        return(NULL);
131 >                for (i = xf_argc; i-- > 0; )
132 >                        newav[ac+i] = xf_argend[i-xf_context->xac];
133 >                *(xf_argend = newav + spec->xac) = NULL;
134 >                if (xf_argbeg != NULL)
135 >                        free((MEM_PTR)xf_argbeg);
136 >                xf_argbeg = newav;
137          }
138 <        for (i = 0; i < ac-1; i++) {
139 <                xf_argv[xf_argc] = (char *)malloc(strlen(av[i+1])+1);
140 <                if (xf_argv[xf_argc] == NULL)
141 <                        return(MG_EMEM);
142 <                strcpy(xf_argv[xf_argc++], av[i+1]);
138 >        cp = (char *)(spec + 1);        /* use memory allocated above */
139 >        for (i = 0; i < ac; i++)
140 >                if (!strcmp(av[i], "-a")) {
141 >                        xf_av(spec)[i++] = "-i";
142 >                        xf_av(spec)[i] = strcpy(
143 >                                        spec->xarr->aarg[spec->xarr->ndim].arg,
144 >                                        "0");
145 >                        spec->xarr->aarg[spec->xarr->ndim].i = 0;
146 >                        spec->xarr->aarg[spec->xarr->ndim++].n = atoi(av[i]);
147 >                } else {
148 >                        xf_av(spec)[i] = strcpy(cp, av[i]);
149 >                        cp += strlen(av[i]) + 1;
150 >                }
151 >        return(spec);
152 > }
153 >
154 >
155 > void
156 > free_xf(spec)                   /* free a transform */
157 > register XF_SPEC        *spec;
158 > {
159 >        if (spec->xarr != NULL)
160 >                free((MEM_PTR)spec->xarr);
161 >        free((MEM_PTR)spec);
162 > }
163 >
164 >
165 > int
166 > xf_aname(ap)                    /* put out name for this instance */
167 > register struct xf_array        *ap;
168 > {
169 >        static char     oname[10*XF_MAXDIM];
170 >        static char     *oav[3] = {mg_ename[MG_E_OBJECT], oname};
171 >        register int    i;
172 >        register char   *cp1, *cp2;
173 >
174 >        if (ap == NULL)
175 >                return(mg_handle(MG_E_OBJECT, 1, oav));
176 >        cp1 = oname;
177 >        *cp1 = 'a';
178 >        for (i = 0; i < ap->ndim; i++) {
179 >                for (cp2 = ap->aarg[i].arg; *cp2; )
180 >                        *++cp1 = *cp2++;
181 >                *++cp1 = '.';
182          }
183 <        xf_argv[xf_argc] = NULL;
184 <                                        /* compute total transformation */
78 <        if (xf_context != NULL) {
79 <                multmat4(spec->xf.xfm, xf_context->xf.xfm, thisxf.xfm);
80 <                spec->xf.sca = xf_context->xf.sca * thisxf.sca;
81 <        } else
82 <                spec->xf = thisxf;
83 <        spec->prev = xf_context;        /* push new transform onto stack */
84 <        xf_context = spec;
85 <        return(MG_OK);
183 >        *cp1 = '\0';
184 >        return(mg_handle(MG_E_OBJECT, 2, oav));
185   }
186  
187  
188 + long
189 + comp_xfid(xfm)                  /* compute unique ID from matrix */
190 + register MAT4   xfm;
191 + {
192 +        static char     shifttab[64] = { 15, 5, 11, 5, 6, 3,
193 +                                9, 15, 13, 2, 13, 5, 2, 12, 14, 11,
194 +                                11, 12, 12, 3, 2, 11, 8, 12, 1, 12,
195 +                                5, 4, 15, 9, 14, 5, 13, 14, 2, 10,
196 +                                10, 14, 12, 3, 5, 5, 14, 6, 12, 11,
197 +                                13, 9, 12, 8, 1, 6, 5, 12, 7, 13,
198 +                                15, 8, 9, 2, 6, 11, 9, 11 };
199 +        register int    i;
200 +        register long   xid;
201 +
202 +        xid = 0;                        /* compute unique transform id */
203 +        for (i = 0; i < sizeof(MAT4)/sizeof(unsigned short); i++)
204 +                xid ^= (long)(((unsigned short *)xfm)[i]) << shifttab[i&63];
205 +        return(xid);
206 + }
207 +
208 +
209   void
210   xf_clear()                      /* clear transform stack */
211   {
212          register XF_SPEC        *spec;
213  
214 <        while (xf_argc)
215 <                free((MEM_PTR)xf_argv[--xf_argc]);
216 <        if (xf_maxarg) {
97 <                free((MEM_PTR)xf_argv);
98 <                xf_argv = NULL;
99 <                xf_maxarg = 0;
214 >        if (xf_argbeg != NULL) {
215 >                free((MEM_PTR)xf_argbeg);
216 >                xf_argbeg = xf_argend = NULL;
217          }
218          while ((spec = xf_context) != NULL) {
219                  xf_context = spec->prev;
220 <                free((MEM_PTR)spec);
220 >                free_xf(spec);
221          }
222   }
223  
# Line 110 | Line 227 | xf_xfmpoint(v1, v2)            /* transform a point by the curre
227   FVECT   v1, v2;
228   {
229          if (xf_context == NULL) {
230 <                v1[0] = v2[0];
114 <                v1[1] = v2[1];
115 <                v1[2] = v2[2];
230 >                VCOPY(v1, v2);
231                  return;
232          }
233          multp3(v1, v2, xf_context->xf.xfm);
# Line 124 | Line 239 | xf_xfmvect(v1, v2)             /* transform a vector using curren
239   FVECT   v1, v2;
240   {
241          if (xf_context == NULL) {
242 <                v1[0] = v2[0];
128 <                v1[1] = v2[1];
129 <                v1[2] = v2[2];
242 >                VCOPY(v1, v2);
243                  return;
244          }
245          multv3(v1, v2, xf_context->xf.xfm);
# Line 207 | Line 320 | int
320   xf(ret, ac, av)                 /* get transform specification */
321   register XF  *ret;
322   int  ac;
323 < char  *av[];
323 > char  **av;
324   {
325          MAT4  xfmat, m4;
326          double  xfsca, dtmp;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines