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

Comparing ray/src/gen/xform.c (file contents):
Revision 2.12 by greg, Wed Jun 12 11:55:27 1996 UTC vs.
Revision 2.34 by schorsch, Mon Oct 27 10:27:25 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1996 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  xform.c - program to transform object files.
6   *              Transformations must preserve aspect ratio.
# Line 12 | Line 9 | static char SCCSid[] = "$SunId$ LBL";
9   *     11/6/86          Finally added error checking!
10   */
11  
15 #include  "standard.h"
16
17 #include  "paths.h"
18
12   #include  <ctype.h>
13  
14 + #include  "platform.h"
15 + #include  "rtmath.h"
16 + #include  "rtprocess.h" /* win_popen() */
17 + #include  "paths.h"
18   #include  "object.h"
22
19   #include  "otypes.h"
20  
21   int  xac;                               /* global xform argument count */
# Line 29 | Line 25 | int  xfa;                              /* start of xf arguments */
25   XF  tot;                                /* total transformation */
26   int  reverse;                           /* boolean true if scene mirrored */
27  
28 < int  invert = 0;                        /* boolean true to invert surfaces */
28 > int  invert;                            /* boolean true to invert surfaces */
29  
30 < int  expand = 0;                        /* boolean true to expand commands */
30 > int  expand;                            /* boolean true to expand commands */
31  
32 < char  *newmod = NULL;                   /* new modifier for surfaces */
32 > char  *newmod;                          /* new modifier for surfaces */
33  
34 < char  *idprefix = NULL;                 /* prefix for object identifiers */
34 > char  *idprefix;                        /* prefix for object identifiers */
35  
36 < #define  ALIAS          NUMOTYPE        /* put alias at end of array */
36 > FUN  ofun[NUMOTYPE] = INIT_OTYPE;       /* default types and actions */
37  
42 #define  NUMTYPES       (NUMOTYPE+1)    /* total number of object types */
43
44 FUN  ofun[NUMTYPES] = INIT_OTYPE;       /* default types and actions */
45
38   short  tinvers[NUMOTYPE];               /* inverse types for surfaces */
39  
40   int  nrept = 1;                         /* number of array repetitions */
41  
42 < extern char  *malloc(), *fgetword();
42 > int stdinused = 0;                      /* stdin has been used by -f option? */
43  
44 < char  *mainfn[MAXPATH];                 /* main file name */
44 > char  mainfn[PATH_MAX];                 /* main file name */
45   FILE  *mainfp = NULL;                   /* main file pointer */
46  
47   #define  progname  (xav[0])
# Line 59 | Line 51 | main(argc, argv)               /* get transform options and transfo
51   int  argc;
52   char  *argv[];
53   {
54 +        int  mal_prefix = 0;
55          char  *fname;
56          int  a;
57 <                                        /* check for array */
57 >                                        /* check for argument list file */
58          for (a = 1; a < argc; a++)
59 +                if (!strcmp(argv[a], "-f"))
60 +                        return(doargf(argc, argv, a));
61 +                                        /* check for regular array */
62 +        for (a = 1; a < argc; a++)
63                  if (!strcmp(argv[a], "-a"))
64                          return(doarray(argc, argv, a));
65  
66 <        initotypes();
66 >        initotypes();                   /* initialize */
67 >        invert = 0;
68 >        expand = 1;
69 >        newmod = NULL;
70 >        idprefix = NULL;
71  
72          for (a = 1; a < argc; a++) {
73                  if (argv[a][0] == '-')
74                          switch (argv[a][1]) {
75                          case 'm':
76 <                                if (argv[a][2] || a+1 >= argc)
76 >                                if (argv[a][2] | (a+1 >= argc))
77                                          break;
78 <                                newmod = argv[++a];
78 >                                a++;
79 >                                if (newmod == NULL)
80 >                                        newmod = argv[a];
81                                  continue;
82                          case 'n':
83 <                                if (argv[a][2] || a+1 >= argc)
83 >                                if (argv[a][2] | (a+1 >= argc))
84                                          break;
85 <                                idprefix = argv[++a];
85 >                                a++;
86 >                                if (idprefix == NULL)
87 >                                        idprefix = argv[a];
88 >                                else {
89 >                                        register char   *newp;
90 >                                        newp = (char *)malloc(strlen(idprefix)+
91 >                                                        strlen(argv[a])+2);
92 >                                        if (newp == NULL)
93 >                                                exit(2);
94 >                                        sprintf(newp, "%s.%s",
95 >                                                        idprefix, argv[a]);
96 >                                        if (mal_prefix++)
97 >                                                free((void *)idprefix);
98 >                                        idprefix = newp;
99 >                                }
100                                  continue;
101 +                        case 'c':
102 +                                if (argv[a][2])
103 +                                        break;
104 +                                expand = 0;
105 +                                continue;
106                          case 'e':
107                                  if (argv[a][2])
108                                          break;
# Line 89 | Line 111 | char  *argv[];
111                          case 'I':
112                                  if (argv[a][2])
113                                          break;
114 <                                invert = 1;
114 >                                invert = !invert;
115                                  continue;
116                          }
117                  break;
# Line 100 | Line 122 | char  *argv[];
122  
123          a += xf(&tot, argc-a, argv+a);
124  
125 <        if (reverse = tot.sca < 0.0)
125 >        if ( (reverse = tot.sca < 0.0) )
126                  tot.sca = -tot.sca;
127          if (invert)
128                  reverse = !reverse;
# Line 119 | Line 141 | char  *argv[];
141          putchar('\n');
142                                          /* transform input */
143          if (xac == argc) {
144 +                if (stdinused) {
145 +                        fprintf(stderr, "%s: cannot use stdin more than once\n",
146 +                                        argv[0]);
147 +                        exit(1);
148 +                }
149                  openmain(NULL);
150                  xform(mainfn, mainfp);
151          } else
# Line 127 | Line 154 | char  *argv[];
154                          xform(mainfn, mainfp);
155                  }
156  
157 +        if (mal_prefix)
158 +                free((void *)idprefix);
159          return(0);
160   }
161  
162  
163 + doargf(ac, av, fi)                      /* take argument list from file */
164 + char  **av;
165 + int  ac, fi;
166 + {
167 +        int  inquote;
168 +        char  *newav[256], **avp;
169 +        char  argbuf[1024];
170 +        char  newid[128];
171 +        char  *oldid;
172 +        register char   *cp;
173 +        FILE    *argfp;
174 +        int  n, i, k, newac, err;
175 +        
176 +        if (fi >= ac-1 || (av[fi+1][0] == '-' && av[fi+1][1] != '\0')) {
177 +                fprintf(stderr, "%s: missing file for -f option\n", av[0]);
178 +                exit(1);
179 +        }
180 +        if (av[fi+1][0] == '-' && av[fi+1][1] == '\0') {
181 +                if (stdinused++) {
182 +                        fprintf(stderr,
183 +                                "%s: cannot use stdin more than once\n",
184 +                                        av[0]);
185 +                        exit(1);
186 +                }
187 +                argfp = stdin;
188 +                n = 100;                /* we just don't know! */
189 +        } else {
190 +                if ((argfp = fopen(av[fi+1], "r")) == NULL) {
191 +                        fprintf(stderr,
192 +                                "%s: cannot open argument file \"%s\"\n",
193 +                                        av[0], av[fi+1]);
194 +                        exit(1);
195 +                }
196 +                n = 0;                  /* count number of lines in file */
197 +                while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL)
198 +                        n += argbuf[0] && argbuf[0] != '#';
199 +                if (!n) {
200 +                        fprintf(stderr, "%s: empty argument file \"%s\"\n",
201 +                                        av[0], av[fi+1]);
202 +                        exit(1);
203 +                }
204 +                nrept *= n;
205 +                rewind(argfp);
206 +        }
207 +        err = 0; k = 0;                 /* read each arg list and call main */
208 +        while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL) {
209 +                if (!argbuf[0] || argbuf[0] == '#')
210 +                        continue;
211 +                avp = newav+2;
212 +                avp[0] = av[0];
213 +                for (i = 1; i < fi; i++)
214 +                        avp[i] = av[i];
215 +                newac = i;
216 +                cp = argbuf;            /* parse new words */
217 +                if (*cp == '!') cp++;
218 +                if (!strncmp(cp, "xform ", 6)) cp += 6;
219 +                inquote = 0;
220 +                for ( ; ; ) {
221 +                skipspaces:
222 +                        while (isspace(*cp))    /* nullify spaces */
223 +                                *cp++ = '\0';
224 +                        if ((*cp == '"') | (*cp == '\''))
225 +                                inquote = *cp++;
226 +                        if (!*cp)               /* all done? */
227 +                                break;
228 +                        if (cp[0] == '\\' && cp[1])
229 +                                if (*++cp == '\n')
230 +                                        goto skipspaces;
231 +                        avp[newac++] = cp;      /* add argument to list */
232 +                        if (inquote) {
233 +                                while (*++cp)
234 +                                        if (*cp == inquote) {
235 +                                                *cp++ = '\0';
236 +                                                break;
237 +                                        }
238 +                        } else {
239 +                                while (*++cp && !isspace(*cp))
240 +                                        ;
241 +                        }
242 +                }
243 +                for (i = fi+2; i < ac; i++)
244 +                        avp[newac++] = av[i];
245 +                avp[newac] = NULL;
246 +                oldid = NULL;
247 +                for (i = 2; i < newac; i++)
248 +                        if (!strcmp(avp[i-1], "-n")) {
249 +                                oldid = avp[i];
250 +                                avp[i] = newid;
251 +                                break;
252 +                        }
253 +                if (oldid == NULL) {
254 +                        newav[0] = av[0];
255 +                        newav[1] = "-n";
256 +                        newav[2] = newid;
257 +                        avp = newav;
258 +                        newac += 2;
259 +                }
260 +                if (oldid == NULL)
261 +                        sprintf(newid, "i%d", k);
262 +                else
263 +                        sprintf(newid, "%s.%d", oldid, k);
264 +                err |= main(newac, avp);
265 +                k++;
266 +        }
267 +        fclose(argfp);
268 +        return(err);
269 + }
270 +
271 +
272   doarray(ac, av, ai)                     /* make array */
273   char  **av;
274   int  ac, ai;
# Line 140 | Line 278 | int  ac, ai;
278          char  *oldid = NULL;
279          int  n, i, err;
280          
281 +        if (ai >= ac-1 || (n = atoi(av[ai+1])) <= 0) {
282 +                fprintf(stderr, "%s: missing count for -a option\n", av[0]);
283 +                exit(1);
284 +        }
285 +        nrept *= n;
286          avp = newav+2;
287          avp[0] = av[0];
288          for (i = 1; i < ac; i++)
# Line 158 | Line 301 | int  ac, ai;
301                  avp = newav;
302                  ac += 2;
303          }
161        nrept *= n = atoi(av[ai+1]);
304          err = 0;
305          for (i = 0; i < n; i++) {
306                  if (oldid == NULL)
# Line 209 | Line 351 | xfcomm(fname, fin)                     /* transform a command */
351   char  *fname;
352   FILE  *fin;
353   {
212        extern FILE  *popen();
213        extern char  *fgetline();
354          FILE  *pin;
355          char  buf[512];
356          int  i;
# Line 227 | Line 367 | FILE  *fin;
367          } else {
368                  printf("\n%s", buf);
369                  if (xac > 1) {
370 <                        printf(" | %s -e", xav[0]);
370 >                        printf(" | %s -e", xav[0]);     /* expand next time */
371                          for (i = 1; i < xac; i++)
372 <                                printf(" %s", xav[i]);
372 >                                if (i >= xfa || strcmp(xav[i], "-c"))
373 >                                        printf(" %s", xav[i]);
374                  }
375                  putchar('\n');
376          }
# Line 240 | Line 381 | xfobject(fname, fin)                           /* transform an object */
381   char  *fname;
382   FILE  *fin;
383   {
243        extern char  *strcpy();
384          char  typ[16], nam[MAXSTR];
385          int  fn;
386                                                  /* modifier and type */
# Line 282 | Line 422 | FILE  *fin;
422                  return(-1);
423                                          /* string arguments */
424          printf("%d", fa.nsargs);
425 <        for (i = 0; i < fa.nsargs; i++)
426 <                printf(" %s", fa.sarg[i]);
425 >        for (i = 0; i < fa.nsargs; i++) {
426 >                fputc(' ', stdout);
427 >                fputword(fa.sarg[i], stdout);
428 >        }
429          printf("\n");
430   #ifdef  IARGS
431                                          /* integer arguments */
# Line 317 | Line 459 | FILE  *fin;
459          if (xac > xfa && strcmp(xav[xfa], "-i"))
460                  resetarr = 2;
461          printf("%d", fa.nsargs + resetarr + xac-xfa);
462 <        for (i = 0; i < fa.nsargs; i++)
463 <                printf(" %s", fa.sarg[i]);
462 >        for (i = 0; i < fa.nsargs; i++) {
463 >                fputc(' ', stdout);
464 >                fputword(fa.sarg[i], stdout);
465 >        }
466          if (resetarr)
467                  printf(" -i 1");
468          for (i = xfa; i < xac; i++)     /* add xf arguments */
# Line 343 | Line 487 | FILE  *fin;
487   }
488  
489  
346 int
347 otype(ofname)                   /* get object function number from its name */
348 register char  *ofname;
349 {
350        register int  i;
351
352        for (i = 0; i < NUMTYPES; i++)
353                if (!strcmp(ofun[i].funame, ofname))
354                        return(i);
355
356        return(-1);             /* not found */
357 }
358
359
490   alias(fin)                      /* transfer alias */
491   FILE  *fin;
492   {
# Line 415 | Line 545 | FILE  *fin;
545  
546          if (readfargs(&fa, fin) != 1)
547                  return(-1);
548 <        if (fa.nfargs > 5)
548 >        if (fa.nfargs > 7)
549                  return(-1);
550          printf("%d", fa.nsargs);
551          if (idprefix == NULL)
# Line 441 | Line 571 | FILE  *fin;
571          if (fa.nfargs > 2)
572                  printf(" %12.6g %12.6g %12.6g", fa.farg[0]/tot.sca,
573                                  fa.farg[1]/tot.sca, fa.farg[2]/tot.sca);
574 <        if (fa.nfargs > 3)
575 <                printf(" %12.6g", fa.farg[3]);
446 <        if (fa.nfargs > 4)
447 <                printf(" %12.6g", fa.farg[4]);
574 >        for (i = 3; i < fa.nfargs; i++)
575 >                printf(" %12.6g", fa.farg[i]);
576          printf("\n");
577          freefargs(&fa);
578          return(0);
# Line 509 | Line 637 | FILE  *fin;
637                  return(-1);
638                                          /* string arguments */
639          printf("%d", fa.nsargs);
640 <        for (i = 0; i < fa.nsargs; i++)
641 <                printf(" %s", fa.sarg[i]);
640 >        for (i = 0; i < fa.nsargs; i++) {
641 >                fputc(' ', stdout);
642 >                fputword(fa.sarg[i], stdout);
643 >        }
644          printf("\n0\n%d\n", fa.nfargs);
645                                          /* anchor point */
646          multp3(v, fa.farg, tot.xfm);
# Line 693 | Line 823 | initotypes()                   /* initialize ofun[] array */
823  
824          if (ofun[OBJ_SOURCE].funp == o_source)
825                  return;                 /* done already */
696                                        /* alias is additional */
697        ofun[ALIAS].funame = ALIASID;
698        ofun[ALIAS].flags = 0;
826                                          /* functions get new transform */
827 <        for (i = 0; i < NUMTYPES; i++)
827 >        for (i = 0; i < NUMOTYPE; i++)
828                  if (hasfunc(i))
829                          ofun[i].funp = addxform;
830                                          /* special cases */
# Line 710 | Line 837 | initotypes()                   /* initialize ofun[] array */
837          ofun[OBJ_CYLINDER].funp =
838          ofun[OBJ_TUBE].funp = o_cylinder;
839          ofun[OBJ_RING].funp = o_ring;
840 <        ofun[OBJ_INSTANCE].funp = addxform;
840 >        ofun[OBJ_INSTANCE].funp =
841 >        ofun[OBJ_MESH].funp = addxform;
842          ofun[MAT_GLOW].funp = m_glow;
843          ofun[MAT_SPOT].funp = m_spot;
844          ofun[MAT_DIELECTRIC].funp = m_dielectric;
# Line 719 | Line 847 | initotypes()                   /* initialize ofun[] array */
847          ofun[PAT_CTEXT].funp =
848          ofun[PAT_BTEXT].funp =
849          ofun[MIX_TEXT].funp = text;
850 <        ofun[ALIAS].funp = alias;
850 >        ofun[MOD_ALIAS].funp = alias;
851                                          /* surface inverses */
852          tinvers[OBJ_FACE] = OBJ_FACE;
853          tinvers[OBJ_SOURCE] = OBJ_SOURCE;
# Line 758 | Line 886 | char  *fname;
886          strcpy(mainfn, fname);
887   }
888   #else
889 < openmain(fname)         /* open fname for input, changing to its directory */
890 < char  *fname;
889 > openmain(iname)         /* open input, changing directory for file */
890 > char  *iname;
891   {
892 <        extern FILE  *tmpfile();
893 <        extern char  *getlibpath(), *getpath();
894 <        static char  origdir[MAXPATH];
895 <        static char  curdir[MAXPATH];
768 <        char  newdir[MAXPATH], *cp;
892 >        static char  origdir[PATH_MAX];
893 >        static char  curfn[PATH_MAX];
894 >        static int  diffdir;
895 >        register char  *fpath;
896  
897 <        if (fname == NULL) {                    /* standard input */
897 >        if (iname == NULL) {                    /* standard input */
898                  if (mainfp == NULL) {
899                          register int  c;
900                          strcpy(mainfn, "standard input");
# Line 784 | Line 911 | char  *fname;
911                          }
912                          while ((c = getc(stdin)) != EOF)
913                                  putc(c, mainfp);
787                        fclose(stdin);
914                  }
915                  rewind(mainfp);                 /* rewind copy */
916                  return;
917          }
792                                                /* get full path */
793        if ((cp = getpath(fname, getlibpath(), R_OK)) == NULL) {
794                fprintf(stderr, "%s: cannot find file \"%s\"\n",
795                                progname, fname);
796                exit(1);
797        }
798        if (cp[0] == '.' && ISDIRSEP(cp[1]))    /* remove leading ./ */
799                cp += 2;
918          if (mainfp == NULL) {                   /* first call, initialize */
919 <                getwd(origdir);
920 <                strcpy(curdir, origdir);
803 <        } else if (!strcmp(cp, mainfn)) {       /* just need to rewind? */
919 >                getcwd(origdir, sizeof(origdir));
920 >        } else if (!strcmp(iname, curfn)) {     /* just need to rewind? */
921                  rewind(mainfp);
922                  return;
923 <        } else                                  /* else close old stream */
923 >        } else {                                /* else close old stream */
924                  fclose(mainfp);
925 +                mainfp = NULL;
926 +                if (diffdir) {                  /* return to our directory */
927 +                        chdir(origdir);
928 +                        diffdir = 0;
929 +                }
930 +        }
931 +        strcpy(curfn, iname);                   /* remember input name */
932 +                                                /* get full path for file */
933 +        if ((fpath = getpath(iname, getrlibpath(), R_OK)) == NULL) {
934 +                fprintf(stderr, "%s: cannot find file \"%s\"\n",
935 +                                progname, iname);
936 +                exit(1);
937 +        }
938 +        if (fpath[0] == '.' && ISDIRSEP(fpath[1]))      /* remove leading ./ */
939 +                fpath += 2;
940                                                  /* record path name */
941 <        strcpy(mainfn, cp);
942 <        if (expand) {                           /* get directory component */
943 <                if (ISDIRSEP(cp[0]))
944 <                        strcpy(newdir, cp);
945 <                else
946 <                        sprintf(newdir, "%s%c%s", origdir, DIRSEP, cp);
947 <                for (cp = newdir+strlen(newdir); !ISDIRSEP(cp[-1]); cp--)
948 <                        ;
941 >        strcpy(mainfn, fpath);
942 >        if (expand) {                           /* change to local directory */
943 >                register char  *cp = fpath + strlen(fpath);     /* get dir. */
944 >                while (cp > fpath) {
945 >                        cp--;
946 >                        if (ISDIRSEP(*cp)) {
947 >                                if (cp == fpath)
948 >                                        cp++;   /* root special case */
949 >                                break;
950 >                        }
951 >                }
952                  *cp = '\0';
953 <                if (strcmp(newdir, curdir)) {   /* change to new directory? */
954 <                        if (chdir(newdir) < 0) {
953 >                if (fpath[0]) {                 /* change to new directory? */
954 >                        if (chdir(fpath) < 0) {
955                                  fprintf(stderr,
956                                  "%s: cannot change directory to \"%s\"\n",
957 <                                                progname, newdir);
957 >                                                progname, fpath);
958                                  exit(1);
959                          }
960 <                        strcpy(curdir, newdir);
960 >                        diffdir++;
961                  }
962                                                  /* get final path component */
963 <                for (cp = fname+strlen(fname);
964 <                                cp > fname && !ISDIRSEP(cp[-1]); cp--)
963 >                for (fpath = iname+strlen(iname);
964 >                                fpath > iname && !ISDIRSEP(fpath[-1]); fpath--)
965                          ;
966          }
967 <                                                /* open the file */
968 <        if ((mainfp = fopen(cp, "r")) == NULL) {
967 >                                                /* finally, open the file */
968 >        if ((mainfp = fopen(fpath, "r")) == NULL) {
969                  fprintf(stderr, "%s: cannot open file \"%s\"\n",
970                                  progname, mainfn);
971                  exit(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines