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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines