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.20 by gwlarson, Fri Jun 4 16:22:06 1999 UTC vs.
Revision 2.35 by schorsch, Sun Nov 16 09:23:46 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  "paths.h"
16 + #include  "rtprocess.h" /* win_popen() */
17 + #include  "rtio.h"
18 + #include  "rtmath.h"
19   #include  "object.h"
22
20   #include  "otypes.h"
21  
22   int  xac;                               /* global xform argument count */
# Line 29 | Line 26 | int  xfa;                              /* start of xf arguments */
26   XF  tot;                                /* total transformation */
27   int  reverse;                           /* boolean true if scene mirrored */
28  
29 < int  invert = 0;                        /* boolean true to invert surfaces */
29 > int  invert;                            /* boolean true to invert surfaces */
30  
31 < int  expand = 1;                        /* boolean true to expand commands */
31 > int  expand;                            /* boolean true to expand commands */
32  
33 < char  *newmod = NULL;                   /* new modifier for surfaces */
33 > char  *newmod;                          /* new modifier for surfaces */
34  
35 < char  *idprefix = NULL;                 /* prefix for object identifiers */
35 > char  *idprefix;                        /* prefix for object identifiers */
36  
37 < #define  ALIAS          NUMOTYPE        /* put alias at end of array */
37 > FUN  ofun[NUMOTYPE] = INIT_OTYPE;       /* default types and actions */
38  
42 #define  NUMTYPES       (NUMOTYPE+1)    /* total number of object types */
43
44 FUN  ofun[NUMTYPES] = INIT_OTYPE;       /* default types and actions */
45
39   short  tinvers[NUMOTYPE];               /* inverse types for surfaces */
40  
41   int  nrept = 1;                         /* number of array repetitions */
42  
43   int stdinused = 0;                      /* stdin has been used by -f option? */
44  
45 < extern char  *malloc(), *fgets(), *fgetword();
53 <
54 < char  mainfn[MAXPATH];                  /* main file name */
45 > char  mainfn[PATH_MAX];                 /* main file name */
46   FILE  *mainfp = NULL;                   /* main file pointer */
47  
48   #define  progname  (xav[0])
49  
50 + static int doargf(int ac, char **av, int fi);
51 + static int doarray(int ac, char **av, int ai);
52 + static void xform(char *name, FILE *fin);
53 + static void xfcomm(char *fname, FILE *fin);
54 + static void xfobject(char *fname, FILE *fin);
55 + static int addxform(FILE *fin);
56 + static int alias(FILE *fin);
57 + static void initotypes(void);
58 + static void openmain(char *iname);
59  
60 < main(argc, argv)                /* get transform options and transform file */
61 < int  argc;
62 < char  *argv[];
60 >
61 > int
62 > main(           /* get transform options and transform file */
63 >        int  argc,
64 >        char  *argv[]
65 > )
66   {
67 <        char  *fname;
67 >        int  mal_prefix = 0;
68          int  a;
69                                          /* check for argument list file */
70          for (a = 1; a < argc; a++)
# Line 72 | Line 75 | char  *argv[];
75                  if (!strcmp(argv[a], "-a"))
76                          return(doarray(argc, argv, a));
77  
78 <        initotypes();
78 >        initotypes();                   /* initialize */
79 >        invert = 0;
80 >        expand = 1;
81 >        newmod = NULL;
82 >        idprefix = NULL;
83  
84          for (a = 1; a < argc; a++) {
85                  if (argv[a][0] == '-')
86                          switch (argv[a][1]) {
87                          case 'm':
88 <                                if (argv[a][2] || a+1 >= argc)
88 >                                if (argv[a][2] | (a+1 >= argc))
89                                          break;
90 <                                newmod = argv[++a];
90 >                                a++;
91 >                                if (newmod == NULL)
92 >                                        newmod = argv[a];
93                                  continue;
94                          case 'n':
95 <                                if (argv[a][2] || a+1 >= argc)
95 >                                if (argv[a][2] | (a+1 >= argc))
96                                          break;
97 <                                idprefix = argv[++a];
97 >                                a++;
98 >                                if (idprefix == NULL)
99 >                                        idprefix = argv[a];
100 >                                else {
101 >                                        register char   *newp;
102 >                                        newp = (char *)malloc(strlen(idprefix)+
103 >                                                        strlen(argv[a])+2);
104 >                                        if (newp == NULL)
105 >                                                exit(2);
106 >                                        sprintf(newp, "%s.%s",
107 >                                                        idprefix, argv[a]);
108 >                                        if (mal_prefix++)
109 >                                                free((void *)idprefix);
110 >                                        idprefix = newp;
111 >                                }
112                                  continue;
113                          case 'c':
114                                  if (argv[a][2])
# Line 100 | Line 123 | char  *argv[];
123                          case 'I':
124                                  if (argv[a][2])
125                                          break;
126 <                                invert = 1;
126 >                                invert = !invert;
127                                  continue;
128                          }
129                  break;
# Line 111 | Line 134 | char  *argv[];
134  
135          a += xf(&tot, argc-a, argv+a);
136  
137 <        if (reverse = tot.sca < 0.0)
137 >        if ( (reverse = tot.sca < 0.0) )
138                  tot.sca = -tot.sca;
139          if (invert)
140                  reverse = !reverse;
# Line 143 | Line 166 | char  *argv[];
166                          xform(mainfn, mainfp);
167                  }
168  
169 +        if (mal_prefix)
170 +                free((void *)idprefix);
171          return(0);
172   }
173  
174  
175 < doargf(ac, av, fi)                      /* take argument list from file */
176 < char  **av;
177 < int  ac, fi;
175 > int
176 > doargf(                 /* take argument list from file */
177 >        int ac,
178 >        char  **av,
179 >        int fi
180 > )
181   {
182 +        int  inquote;
183          char  *newav[256], **avp;
184          char  argbuf[1024];
185          char  newid[128];
# Line 165 | Line 194 | int  ac, fi;
194          }
195          if (av[fi+1][0] == '-' && av[fi+1][1] == '\0') {
196                  if (stdinused++) {
197 <                        fprintf(stderr, "%s: cannot use stdin more than once\n",
197 >                        fprintf(stderr,
198 >                                "%s: cannot use stdin more than once\n",
199                                          av[0]);
200                          exit(1);
201                  }
# Line 173 | Line 203 | int  ac, fi;
203                  n = 100;                /* we just don't know! */
204          } else {
205                  if ((argfp = fopen(av[fi+1], "r")) == NULL) {
206 <                        fprintf(stderr, "%s: cannot open argument file \"%s\"\n",
206 >                        fprintf(stderr,
207 >                                "%s: cannot open argument file \"%s\"\n",
208                                          av[0], av[fi+1]);
209                          exit(1);
210                  }
211                  n = 0;                  /* count number of lines in file */
212 <                while (fgets(argbuf,sizeof(argbuf),argfp) != NULL)
213 <                        n += argbuf[0] != '\n' & argbuf[0] != '#';
212 >                while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL)
213 >                        n += argbuf[0] && argbuf[0] != '#';
214                  if (!n) {
215                          fprintf(stderr, "%s: empty argument file \"%s\"\n",
216                                          av[0], av[fi+1]);
# Line 189 | Line 220 | int  ac, fi;
220                  rewind(argfp);
221          }
222          err = 0; k = 0;                 /* read each arg list and call main */
223 <        while (fgets(argbuf,sizeof(argbuf),argfp) != NULL) {
224 <                if (argbuf[0] == '\n' | argbuf[0] == '#')
223 >        while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL) {
224 >                if (!argbuf[0] || argbuf[0] == '#')
225                          continue;
226                  avp = newav+2;
227                  avp[0] = av[0];
# Line 200 | Line 231 | int  ac, fi;
231                  cp = argbuf;            /* parse new words */
232                  if (*cp == '!') cp++;
233                  if (!strncmp(cp, "xform ", 6)) cp += 6;
234 +                inquote = 0;
235                  for ( ; ; ) {
236 +                skipspaces:
237                          while (isspace(*cp))    /* nullify spaces */
238                                  *cp++ = '\0';
239 +                        if ((*cp == '"') | (*cp == '\''))
240 +                                inquote = *cp++;
241                          if (!*cp)               /* all done? */
242                                  break;
243 +                        if (cp[0] == '\\' && cp[1])
244 +                                if (*++cp == '\n')
245 +                                        goto skipspaces;
246                          avp[newac++] = cp;      /* add argument to list */
247 <                        while (*++cp && !isspace(*cp))
248 <                                ;
247 >                        if (inquote) {
248 >                                while (*++cp)
249 >                                        if (*cp == inquote) {
250 >                                                *cp++ = '\0';
251 >                                                break;
252 >                                        }
253 >                        } else {
254 >                                while (*++cp && !isspace(*cp))
255 >                                        ;
256 >                        }
257                  }
258                  for (i = fi+2; i < ac; i++)
259                          avp[newac++] = av[i];
# Line 238 | Line 284 | int  ac, fi;
284   }
285  
286  
287 < doarray(ac, av, ai)                     /* make array */
288 < char  **av;
289 < int  ac, ai;
287 > int
288 > doarray(                        /* make array */
289 >        int  ac,
290 >        char  **av,
291 >        int  ai
292 > )
293   {
294          char  *newav[256], **avp;
295          char  newid[128], repts[32];
# Line 283 | Line 332 | int  ac, ai;
332   }
333  
334  
335 < xform(name, fin)                        /* transform stream by tot.xfm */
336 < char  *name;
337 < register FILE  *fin;
335 > void
336 > xform(                  /* transform stream by tot.xfm */
337 >        char  *name,
338 >        register FILE  *fin
339 > )
340   {
341          int  nobjs = 0;
342          register int  c;
# Line 316 | Line 367 | register FILE  *fin;
367   }
368  
369  
370 < xfcomm(fname, fin)                      /* transform a command */
371 < char  *fname;
372 < FILE  *fin;
370 > void
371 > xfcomm(                 /* transform a command */
372 >        char  *fname,
373 >        FILE  *fin
374 > )
375   {
323        extern FILE  *popen();
324        extern char  *fgetline();
376          FILE  *pin;
377          char  buf[512];
378          int  i;
# Line 338 | Line 389 | FILE  *fin;
389          } else {
390                  printf("\n%s", buf);
391                  if (xac > 1) {
392 <                        printf(" | %s", xav[0]);
392 >                        printf(" | %s -e", xav[0]);     /* expand next time */
393                          for (i = 1; i < xac; i++)
394 <                                printf(" %s", xav[i]);
394 >                                if (i >= xfa || strcmp(xav[i], "-c"))
395 >                                        printf(" %s", xav[i]);
396                  }
397                  putchar('\n');
398          }
399   }
400  
401  
402 < xfobject(fname, fin)                            /* transform an object */
403 < char  *fname;
404 < FILE  *fin;
402 > void
403 > xfobject(                               /* transform an object */
404 >        char  *fname,
405 >        FILE  *fin
406 > )
407   {
354        extern char  *strcpy();
408          char  typ[16], nam[MAXSTR];
409          int  fn;
410                                                  /* modifier and type */
# Line 383 | Line 436 | FILE  *fin;
436   }
437  
438  
439 < o_default(fin)                  /* pass on arguments unchanged */
440 < FILE  *fin;
439 > int
440 > o_default(                      /* pass on arguments unchanged */
441 >        FILE  *fin
442 > )
443   {
444          register int  i;
445          FUNARGS  fa;
# Line 393 | Line 448 | FILE  *fin;
448                  return(-1);
449                                          /* string arguments */
450          printf("%d", fa.nsargs);
451 <        for (i = 0; i < fa.nsargs; i++)
452 <                printf(" %s", fa.sarg[i]);
451 >        for (i = 0; i < fa.nsargs; i++) {
452 >                fputc(' ', stdout);
453 >                fputword(fa.sarg[i], stdout);
454 >        }
455          printf("\n");
456   #ifdef  IARGS
457                                          /* integer arguments */
# Line 415 | Line 472 | FILE  *fin;
472   }
473  
474  
475 < addxform(fin)                   /* add xf arguments to strings */
476 < FILE  *fin;
475 > int
476 > addxform(                       /* add xf arguments to strings */
477 >        FILE  *fin
478 > )
479   {
480          register int  i;
481          int  resetarr = 0;
# Line 428 | Line 487 | FILE  *fin;
487          if (xac > xfa && strcmp(xav[xfa], "-i"))
488                  resetarr = 2;
489          printf("%d", fa.nsargs + resetarr + xac-xfa);
490 <        for (i = 0; i < fa.nsargs; i++)
491 <                printf(" %s", fa.sarg[i]);
490 >        for (i = 0; i < fa.nsargs; i++) {
491 >                fputc(' ', stdout);
492 >                fputword(fa.sarg[i], stdout);
493 >        }
494          if (resetarr)
495                  printf(" -i 1");
496          for (i = xfa; i < xac; i++)     /* add xf arguments */
# Line 455 | Line 516 | FILE  *fin;
516  
517  
518   int
519 < otype(ofname)                   /* get object function number from its name */
520 < register char  *ofname;
519 > alias(                  /* transfer alias */
520 >        FILE  *fin
521 > )
522   {
461        register int  i;
462
463        for (i = 0; i < NUMTYPES; i++)
464                if (!strcmp(ofun[i].funame, ofname))
465                        return(i);
466
467        return(-1);             /* not found */
468 }
469
470
471 alias(fin)                      /* transfer alias */
472 FILE  *fin;
473 {
523          char  aliasnm[MAXSTR];
524  
525          if (fgetword(aliasnm, MAXSTR, fin) == NULL)
# Line 480 | Line 529 | FILE  *fin;
529   }
530  
531  
532 < m_glow(fin)                     /* transform arguments for proximity light */
533 < FILE  *fin;
532 > int
533 > m_glow(                 /* transform arguments for proximity light */
534 >        FILE  *fin
535 > )
536   {
537          FUNARGS  fa;
538  
# Line 498 | Line 549 | FILE  *fin;
549   }
550  
551  
552 < m_spot(fin)                     /* transform arguments for spotlight */
553 < FILE  *fin;
552 > int
553 > m_spot(                 /* transform arguments for spotlight */
554 >        FILE  *fin
555 > )
556   {
557          FVECT  v;
558          FUNARGS  fa;
# Line 518 | Line 571 | FILE  *fin;
571   }
572  
573  
574 < m_mist(fin)             /* transform arguments for mist */
575 < FILE  *fin;
574 > int
575 > m_mist(         /* transform arguments for mist */
576 >        FILE  *fin
577 > )
578   {
579          FUNARGS  fa;
580          int     i;
# Line 560 | Line 615 | FILE  *fin;
615   }
616  
617  
618 < m_dielectric(fin)               /* transform arguments for dielectric */
619 < FILE  *fin;
618 > int
619 > m_dielectric(           /* transform arguments for dielectric */
620 >        FILE  *fin
621 > )
622   {
623          FUNARGS  fa;
624  
# Line 580 | Line 637 | FILE  *fin;
637   }
638  
639  
640 < m_interface(fin)                /* transform arguments for interface */
641 < FILE  *fin;
640 > int
641 > m_interface(            /* transform arguments for interface */
642 >        FILE  *fin
643 > )
644   {
645          FUNARGS  fa;
646  
# Line 605 | Line 664 | FILE  *fin;
664   }
665  
666  
667 < text(fin)                       /* transform text arguments */
668 < FILE  *fin;
667 > int
668 > text(                   /* transform text arguments */
669 >        FILE  *fin
670 > )
671   {
672          int  i;
673          FVECT  v;
# Line 618 | Line 679 | FILE  *fin;
679                  return(-1);
680                                          /* string arguments */
681          printf("%d", fa.nsargs);
682 <        for (i = 0; i < fa.nsargs; i++)
683 <                printf(" %s", fa.sarg[i]);
682 >        for (i = 0; i < fa.nsargs; i++) {
683 >                fputc(' ', stdout);
684 >                fputword(fa.sarg[i], stdout);
685 >        }
686          printf("\n0\n%d\n", fa.nfargs);
687                                          /* anchor point */
688          multp3(v, fa.farg, tot.xfm);
# Line 642 | Line 705 | FILE  *fin;
705   }
706  
707  
708 < o_source(fin)                   /* transform source arguments */
709 < FILE  *fin;
708 > int
709 > o_source(                       /* transform source arguments */
710 >        FILE  *fin
711 > )
712   {
713          FVECT  dv;
714          FUNARGS  fa;
# Line 663 | Line 728 | FILE  *fin;
728   }
729  
730  
731 < o_sphere(fin)                   /* transform sphere arguments */
732 < FILE  *fin;
731 > int
732 > o_sphere(                       /* transform sphere arguments */
733 >        FILE  *fin
734 > )
735   {
736          FVECT  cent;
737          double  rad;
# Line 687 | Line 754 | FILE  *fin;
754   }
755  
756  
757 < o_face(fin)                     /* transform face arguments */
758 < FILE  *fin;
757 > int
758 > o_face(                 /* transform face arguments */
759 >        FILE  *fin
760 > )
761   {
762          FVECT  p;
763          register int  i;
# Line 713 | Line 782 | FILE  *fin;
782   }
783  
784  
785 < o_cone(fin)                     /* transform cone and cup arguments */
786 < FILE  *fin;
785 > int
786 > o_cone(                 /* transform cone and cup arguments */
787 >        FILE  *fin
788 > )
789   {
790          FVECT  p0, p1;
791          double  r0, r1;
# Line 740 | Line 811 | FILE  *fin;
811   }
812  
813  
814 < o_cylinder(fin)                 /* transform cylinder and tube arguments */
815 < FILE  *fin;
814 > int
815 > o_cylinder(                     /* transform cylinder and tube arguments */
816 >        FILE  *fin
817 > )
818   {
819          FVECT  p0, p1;
820          double  rad;
# Line 765 | Line 838 | FILE  *fin;
838   }
839  
840  
841 < o_ring(fin)                     /* transform ring arguments */
842 < FILE  *fin;
841 > int
842 > o_ring(                 /* transform ring arguments */
843 >        FILE  *fin
844 > )
845   {
846          FVECT  p0, pd;
847          double  r0, r1;
# Line 796 | Line 871 | FILE  *fin;
871   }
872  
873  
874 < initotypes()                    /* initialize ofun[] array */
874 > void
875 > initotypes(void)                        /* initialize ofun[] array */
876   {
877          register int  i;
878  
879          if (ofun[OBJ_SOURCE].funp == o_source)
880                  return;                 /* done already */
805                                        /* alias is additional */
806        ofun[ALIAS].funame = ALIASID;
807        ofun[ALIAS].flags = 0;
881                                          /* functions get new transform */
882 <        for (i = 0; i < NUMTYPES; i++)
882 >        for (i = 0; i < NUMOTYPE; i++)
883                  if (hasfunc(i))
884                          ofun[i].funp = addxform;
885                                          /* special cases */
# Line 819 | Line 892 | initotypes()                   /* initialize ofun[] array */
892          ofun[OBJ_CYLINDER].funp =
893          ofun[OBJ_TUBE].funp = o_cylinder;
894          ofun[OBJ_RING].funp = o_ring;
895 <        ofun[OBJ_INSTANCE].funp = addxform;
895 >        ofun[OBJ_INSTANCE].funp =
896 >        ofun[OBJ_MESH].funp = addxform;
897          ofun[MAT_GLOW].funp = m_glow;
898          ofun[MAT_SPOT].funp = m_spot;
899          ofun[MAT_DIELECTRIC].funp = m_dielectric;
# Line 828 | Line 902 | initotypes()                   /* initialize ofun[] array */
902          ofun[PAT_CTEXT].funp =
903          ofun[PAT_BTEXT].funp =
904          ofun[MIX_TEXT].funp = text;
905 <        ofun[ALIAS].funp = alias;
905 >        ofun[MOD_ALIAS].funp = alias;
906                                          /* surface inverses */
907          tinvers[OBJ_FACE] = OBJ_FACE;
908          tinvers[OBJ_SOURCE] = OBJ_SOURCE;
# Line 844 | Line 918 | initotypes()                   /* initialize ofun[] array */
918  
919  
920   #ifdef  OLDXFORM
921 < openmain(fname)
922 < char  *fname;
921 > void
922 > openmain(
923 >        char  *fname
924 > )
925   {
926          if (fname == NULL) {
927                  strcpy(mainfn, "standard input");
# Line 867 | Line 943 | char  *fname;
943          strcpy(mainfn, fname);
944   }
945   #else
946 < openmain(fname)         /* open fname for input, changing to its directory */
947 < char  *fname;
946 > void
947 > openmain(               /* open input, changing directory for file */
948 >        char  *iname
949 > )
950   {
951 <        extern FILE  *tmpfile();
952 <        extern char  *getlibpath(), *getpath();
875 <        static char  origdir[MAXPATH];
876 <        static char  curfn[MAXPATH];
951 >        static char  origdir[PATH_MAX];
952 >        static char  curfn[PATH_MAX];
953          static int  diffdir;
954          register char  *fpath;
955  
956 <        if (fname == NULL) {                    /* standard input */
956 >        if (iname == NULL) {                    /* standard input */
957                  if (mainfp == NULL) {
958                          register int  c;
959                          strcpy(mainfn, "standard input");
# Line 894 | Line 970 | char  *fname;
970                          }
971                          while ((c = getc(stdin)) != EOF)
972                                  putc(c, mainfp);
897                        fclose(stdin);
973                  }
974                  rewind(mainfp);                 /* rewind copy */
975                  return;
976          }
977          if (mainfp == NULL) {                   /* first call, initialize */
978 <                getwd(origdir);
979 <        } else if (!strcmp(fname, curfn)) {     /* just need to rewind? */
978 >                getcwd(origdir, sizeof(origdir));
979 >        } else if (!strcmp(iname, curfn)) {     /* just need to rewind? */
980                  rewind(mainfp);
981                  return;
982          } else {                                /* else close old stream */
983                  fclose(mainfp);
984 <                if (diffdir) {
984 >                mainfp = NULL;
985 >                if (diffdir) {                  /* return to our directory */
986                          chdir(origdir);
987                          diffdir = 0;
988                  }
989          }
990 <        strcpy(curfn, fname);                   /* remember file name */
991 <                                                /* get full path */
992 <        if ((fpath = getpath(fname, getlibpath(), R_OK)) == NULL) {
990 >        strcpy(curfn, iname);                   /* remember input name */
991 >                                                /* get full path for file */
992 >        if ((fpath = getpath(iname, getrlibpath(), R_OK)) == NULL) {
993                  fprintf(stderr, "%s: cannot find file \"%s\"\n",
994 <                                progname, fname);
994 >                                progname, iname);
995                  exit(1);
996          }
997          if (fpath[0] == '.' && ISDIRSEP(fpath[1]))      /* remove leading ./ */
# Line 943 | Line 1019 | char  *fname;
1019                          diffdir++;
1020                  }
1021                                                  /* get final path component */
1022 <                for (fpath = fname+strlen(fname);
1023 <                                fpath > fname && !ISDIRSEP(fpath[-1]); fpath--)
1022 >                for (fpath = iname+strlen(iname);
1023 >                                fpath > iname && !ISDIRSEP(fpath[-1]); fpath--)
1024                          ;
1025          }
1026 <                                                /* open the file */
1026 >                                                /* finally, open the file */
1027          if ((mainfp = fopen(fpath, "r")) == NULL) {
1028                  fprintf(stderr, "%s: cannot open file \"%s\"\n",
1029                                  progname, mainfn);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines