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.8 by greg, Tue Apr 18 16:52:28 1995 UTC

# Line 27 | Line 27 | XF_SPEC        *xf_context;                    /* current context */
27  
28   static int      xf_maxarg;              /* # allocated arguments */
29  
30 + static XF_SPEC  *new_xf();
31 + static long     comp_xfid();
32 + static int      put_oname();
33  
34 +
35   int
36   xf_handler(ac, av)              /* handle xf entity */
37   int     ac;
38   char    **av;
39   {
36        register int    i;
40          register XF_SPEC        *spec;
41 <        XF      thisxf;
41 >        register int    n;
42 >        int     rv;
43  
44 <        if (ac == 1) {                  /* pop top transform */
44 >        if (ac == 1) {                  /* something with existing transform */
45                  if ((spec = xf_context) == NULL)
46                          return(MG_OK);          /* should be error? */
47 <                while (xf_argc > spec->xav0)
48 <                        free((MEM_PTR)xf_argv[--xf_argc]);
49 <                xf_argv[xf_argc] = NULL;
50 <                xf_context = spec->prev;
51 <                free((MEM_PTR)spec);
52 <                return(MG_OK);
47 >                n = -1;
48 >                if (spec->xarr != NULL) {       /* check for iteration */
49 >                        register struct xf_array        *ap = spec->xarr;
50 >
51 >                        (void)put_oname((struct xf_array *)NULL);
52 >                        n = ap->ndim;
53 >                        while (n--) {
54 >                                if (++ap->aarg[n].i < ap->aarg[n].n)
55 >                                        break;
56 >                                (void)strcpy(ap->aarg[n].arg, "0");
57 >                                ap->aarg[n].i = 0;
58 >                        }
59 >                        if (n >= 0) {
60 >                                if ((rv = mg_fgoto(&ap->spos)) != MG_OK)
61 >                                        return(rv);
62 >                                sprintf(ap->aarg[n].arg, "%d", ap->aarg[n].i);
63 >                                (void)put_oname(ap);
64 >                        } else
65 >                                free((MEM_PTR)ap);
66 >                }
67 >                if (n < 0) {                    /* pop transform */
68 >                        xf_argv[xf_argc=spec->xav0] = NULL;
69 >                        xf_context = spec->prev;
70 >                        free((MEM_PTR)spec);
71 >                        return(MG_OK);
72 >                }
73 >        } else {                        /* else allocate transform */
74 >                if ((spec = new_xf(ac-1, av+1)) == NULL)
75 >                        return(MG_EMEM);
76 >                spec->prev = xf_context;        /* push onto stack */
77 >                xf_context = spec;
78          }
79                                          /* translate new specification */
80 <        if (xf(&thisxf, ac-1, av+1) != ac-1)
80 >        if (xf(&spec->xf, spec->xac, &xf_argv[spec->xav0]) != spec->xac)
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);
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->xav0 = xf_argc;
128 <        spec->xac = ac-1;
128 >        spec->xac = ac;
129                                          /* and store new xf arguments */
130 <        if (xf_argc+ac > xf_maxarg) {
130 >        if (xf_argc+ac+1 > xf_maxarg) {
131                  if (!xf_maxarg)
132                          xf_argv = (char **)malloc(
133 <                                        (xf_maxarg=ac)*sizeof(char *));
133 >                                        (xf_maxarg=ac+1)*sizeof(char *));
134                  else
135                          xf_argv = (char **)realloc((MEM_PTR)xf_argv,
136 <                                        (xf_maxarg+=ac)*sizeof(char *));
136 >                                (xf_maxarg=xf_argc+ac+1)*sizeof(char *));
137                  if (xf_argv == NULL)
138 <                        return(MG_EMEM);
138 >                        return(NULL);
139          }
140 <        for (i = 0; i < ac-1; i++) {
141 <                xf_argv[xf_argc] = (char *)malloc(strlen(av[i+1])+1);
142 <                if (xf_argv[xf_argc] == NULL)
143 <                        return(MG_EMEM);
144 <                strcpy(xf_argv[xf_argc++], av[i+1]);
145 <        }
140 >        cp = (char *)(spec + 1);        /* use memory allocated above */
141 >        for (i = 0; i < ac; i++)
142 >                if (!strcmp(av[i], "-a")) {
143 >                        xf_argv[xf_argc++] = "-i";
144 >                        xf_argv[xf_argc++] = strcpy(
145 >                                        spec->xarr->aarg[spec->xarr->ndim].arg,
146 >                                        "0");
147 >                        spec->xarr->aarg[spec->xarr->ndim].i = 0;
148 >                        spec->xarr->aarg[spec->xarr->ndim++].n = atoi(av[++i]);
149 >                } else {
150 >                        xf_argv[xf_argc++] = strcpy(cp, av[i]);
151 >                        cp += strlen(av[i]) + 1;
152 >                }
153          xf_argv[xf_argc] = NULL;
154 <                                        /* compute total transformation */
155 <        if (xf_context != NULL) {
156 <                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);
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 +        *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  
94        while (xf_argc)
95                free((MEM_PTR)xf_argv[--xf_argc]);
209          if (xf_maxarg) {
210                  free((MEM_PTR)xf_argv);
211                  xf_argv = NULL;
212                  xf_maxarg = 0;
213          }
214 +        xf_argc = 0;
215          while ((spec = xf_context) != NULL) {
216                  xf_context = spec->prev;
217 +                if (spec->xarr != NULL)
218 +                        free((MEM_PTR)spec->xarr);
219                  free((MEM_PTR)spec);
220          }
221   }
# Line 207 | Line 323 | int
323   xf(ret, ac, av)                 /* get transform specification */
324   register XF  *ret;
325   int  ac;
326 < char  *av[];
326 > char  **av;
327   {
328          MAT4  xfmat, m4;
329          double  xfsca, dtmp;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines