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.3 by greg, Fri Jun 24 15:32:40 1994 UTC vs.
Revision 1.9 by greg, Wed May 10 22:20:57 1995 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines