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.6 by greg, Mon Jul 11 14:47:12 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 static XF_SPEC  *new_xf();
31 static long     comp_xfid();
32 static int      put_oname();
33
34
30   int
31   xf_handler(ac, av)              /* handle xf entity */
32   int     ac;
# Line 40 | Line 35 | char   **av;
35          register XF_SPEC        *spec;
36          register int    n;
37          int     rv;
43        XF      thisxf;
38  
39          if (ac == 1) {                  /* something with existing transform */
40                  if ((spec = xf_context) == NULL)
41 <                        return(MG_OK);          /* should be error? */
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)put_oname((struct xf_array *)NULL);
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)
# Line 61 | Line 55 | char   **av;
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)put_oname(ap);
59 <                        } else
66 <                                free((MEM_PTR)ap);
58 >                                (void)xf_aname(ap);
59 >                        }
60                  }
61                  if (n < 0) {                    /* pop transform */
69                        xf_argv[xf_argc=spec->xav0] = NULL;
62                          xf_context = spec->prev;
63 <                        free((MEM_PTR)spec);
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, spec->xac, &xf_argv[spec->xav0]) != spec->xac)
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 +                                        /* 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, thisxf.xfm, spec->prev->xf.xfm);
85 <                spec->xf.sca = thisxf.sca * spec->prev->xf.sca;
86 <        } else
87 <                spec->xf = thisxf;
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 < static XF_SPEC *
93 > XF_SPEC *
94   new_xf(ac, av)                  /* allocate new transform structure */
95   int     ac;
96   char    **av;
# Line 122 | Line 121 | char   **av;
121                  spec->xarr->ndim = 0;           /* incremented below */
122          } else
123                  spec->xarr = NULL;
124 <        spec->xav0 = xf_argc;
126 <        spec->xac = ac;
124 >        spec->xac = ac + xf_argc;
125                                          /* and store new xf arguments */
126 <        if (xf_argc+ac+1 > xf_maxarg) {
127 <                if (!xf_maxarg)
128 <                        xf_argv = (char **)malloc(
129 <                                        (xf_maxarg=ac+1)*sizeof(char *));
132 <                else
133 <                        xf_argv = (char **)realloc((MEM_PTR)xf_argv,
134 <                                (xf_maxarg=xf_argc+ac+1)*sizeof(char *));
135 <                if (xf_argv == NULL)
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          cp = (char *)(spec + 1);        /* use memory allocated above */
139          for (i = 0; i < ac; i++)
140                  if (!strcmp(av[i], "-a")) {
141 <                        xf_argv[xf_argc++] = "-i";
142 <                        xf_argv[xf_argc++] = strcpy(
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]);
146 >                        spec->xarr->aarg[spec->xarr->ndim++].n = atoi(av[i]);
147                  } else {
148 <                        xf_argv[xf_argc++] = strcpy(cp, av[i]);
148 >                        xf_av(spec)[i] = strcpy(cp, av[i]);
149                          cp += strlen(av[i]) + 1;
150                  }
151        xf_argv[xf_argc] = NULL;
152        if (spec->xarr != NULL)
153                (void)put_oname(spec->xarr);
151          return(spec);
152   }
153  
154  
155 < static int
156 < put_oname(ap)                   /* put out name for this instance */
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];
# Line 178 | Line 185 | register struct xf_array       *ap;
185   }
186  
187  
188 < static long
188 > long
189   comp_xfid(xfm)                  /* compute unique ID from matrix */
190   register MAT4   xfm;
191   {
# Line 204 | Line 211 | xf_clear()                     /* clear transform stack */
211   {
212          register XF_SPEC        *spec;
213  
214 <        if (xf_maxarg) {
215 <                free((MEM_PTR)xf_argv);
216 <                xf_argv = NULL;
210 <                xf_maxarg = 0;
214 >        if (xf_argbeg != NULL) {
215 >                free((MEM_PTR)xf_argbeg);
216 >                xf_argbeg = xf_argend = NULL;
217          }
212        xf_argc = 0;
218          while ((spec = xf_context) != NULL) {
219                  xf_context = spec->prev;
220 <                if (spec->xarr != NULL)
216 <                        free((MEM_PTR)spec->xarr);
217 <                free((MEM_PTR)spec);
220 >                free_xf(spec);
221          }
222   }
223  
# Line 224 | 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];
228 <                v1[1] = v2[1];
229 <                v1[2] = v2[2];
230 >                VCOPY(v1, v2);
231                  return;
232          }
233          multp3(v1, v2, xf_context->xf.xfm);
# Line 238 | 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];
242 <                v1[1] = v2[1];
243 <                v1[2] = v2[2];
242 >                VCOPY(v1, v2);
243                  return;
244          }
245          multv3(v1, v2, xf_context->xf.xfm);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines