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

Comparing ray/src/gen/mkillum.c (file contents):
Revision 2.21 by greg, Sun Sep 19 08:42:22 2004 UTC vs.
Revision 2.47 by greg, Wed Apr 23 02:35:26 2025 UTC

# Line 8 | Line 8 | static const char RCSid[] = "$Id$";
8   #include  <signal.h>
9   #include  <ctype.h>
10  
11 < #include  "platform.h"
11 > #include  "paths.h"             /* win_popen() */
12   #include  "mkillum.h"
13 < #include  "random.h"
13 > #include  "func.h"
14  
15                                  /* default parameters */
16   #define  SAMPDENS       48              /* points per projected steradian */
# Line 23 | Line 23 | static const char RCSid[] = "$Id$";
23   #define  S_COMPL        2               /* select all but element */
24   #define  S_ALL          3               /* select all */
25  
26                                /* rtrace command and defaults */
27 char  *rtargv[64] = { "rtrace", "-dj", ".25", "-dr", "3", "-dv-",
28                "-ab", "2", "-ad", "1024", "-as", "512", "-aa", ".1", };
29 int  rtargc = 14;
30                                /* overriding rtrace options */
31 char  *myrtopts[] = { "-I-", "-i-", "-ld-", "-ov", "-h-",
32                        "-fff", "-y", "0", NULL };
33
34 struct rtproc   rt0;            /* head of rtrace process list */
35
26   struct illum_args  thisillum = {        /* our illum and default values */
27                  0,
28                  DFLMAT,
# Line 47 | Line 37 | struct illum_args  thisillum = {       /* our illum and defa
37   char    matcheck[MAXSTR];       /* current material to include or exclude */
38   int     matselect = S_ALL;      /* selection criterion */
39  
50 FUN     ofun[NUMOTYPE] = INIT_OTYPE;    /* object types */
51
52 char    persistfn[] = "pfXXXXXX";       /* persist file name */
53
40   int     gargc;                  /* global argc */
41   char    **gargv;                /* global argv */
56 #define  progname       gargv[0]
42  
43   int     doneheader = 0;         /* printed header yet? */
44   #define  checkhead()    if (!doneheader++) printhead(gargc,gargv)
45  
46   int     warnings = 1;           /* print warnings? */
47  
48 < void init(int np);
49 < void filter(register FILE       *infp, char     *name);
48 > void init(char *octnm, int np);
49 > void filter(FILE        *infp, char     *name);
50   void xoptions(char      *s, char        *nm);
51   void printopts(void);
52 < void printhead(register int  ac, register char  **av);
52 > void printhead(int  ac, char  **av);
53   void xobject(FILE  *fp, char  *nm);
54  
55  
# Line 75 | Line 60 | main(          /* compute illum distributions using rtrace */
60   )
61   {
62          int     nprocs = 1;
78        char    *rtpath;
63          FILE    *fp;
64 <        register int    i;
64 >        int     rval;
65 >        int     i;
66                                  /* set global arguments */
67          gargv = argv;
68 <                                /* check for -n option */
69 <        if (!strcmp(argv[1], "-n")) {
70 <                nprocs = atoi(argv[2]);
71 <                if (nprocs <= 0)
72 <                        error(USER, "illegal number of processes");
73 <                i = 3;
74 <        } else
75 <                i = 1;
76 <                                /* set up rtrace command */
77 <        for ( ; i < argc; i++) {
78 <                if (argv[i][0] == '<' && argv[i][1] == '\0')
68 >        progname = gargv[0];
69 >                                /* set up rendering defaults */
70 >        dstrsrc = 0.5;
71 >        directrelay = 3;
72 >        ambounce = 2;
73 >                                /* initialize calcomp routines */
74 >        initfunc();
75 >                                /* get options from command line */
76 >        for (i = 1; i < argc; i++) {
77 >                while ((rval = expandarg(&argc, &argv, i)) > 0)
78 >                        ;
79 >                if (rval < 0) {
80 >                        sprintf(errmsg, "cannot expand '%s'", argv[i]);
81 >                        error(SYSTEM, errmsg);
82 >                }
83 >                if (argv[i][0] != '-')
84                          break;
85 <                rtargv[rtargc++] = argv[i];
86 <                if (argv[i][0] == '-' && argv[i][1] == 'w')
87 <                        switch (argv[i][2]) {
98 <                        case '\0':
99 <                                warnings = !warnings;
100 <                                break;
101 <                        case '+':
102 <                        case 'T': case 't':
103 <                        case 'Y': case 'y':
104 <                        case '1':
105 <                                warnings = 1;
106 <                                break;
107 <                        case '-':
108 <                        case 'F': case 'f':
109 <                        case 'N': case 'n':
110 <                        case '0':
111 <                                warnings = 0;
112 <                                break;
113 <                        }
114 <        }
115 <        gargc = i;
116 <        if (!strcmp(rtargv[--rtargc], "-defaults"))
117 <                nprocs = 0;
118 <        if (nprocs > 1) {       /* add persist file if parallel invocation */
119 <                rtargv[rtargc++] = "-PP";
120 <                rtargv[rtargc++] = mktemp(persistfn);
121 <        }
122 <                                /* add "mandatory" rtrace options */
123 <        for (i = 0; myrtopts[i] != NULL; i++)
124 <                rtargv[rtargc++] = myrtopts[i];
125 <                                /* finally, put back final argument */
126 <        rtargv[rtargc++] = argv[gargc-1];
127 <        rtargv[rtargc] = NULL;
128 <        if (!nprocs) {          /* just asking for defaults? */
129 <                printopts(); fflush(stdout);
130 <                rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
131 <                if (rtpath == NULL) {
132 <                        eputs(rtargv[0]);
133 <                        eputs(": command not found\n");
134 <                        exit(1);
85 >                if (!strcmp(argv[i], "-w")) {
86 >                        warnings = 0;
87 >                        continue;
88                  }
89 <                execv(rtpath, rtargv);
90 <                perror(rtpath); /* execv() should not return */
91 <                exit(1);
89 >                if (!strcmp(argv[i], "-n")) {
90 >                        nprocs = atoi(argv[++i]);
91 >                        if (nprocs <= 0)
92 >                                error(USER, "illegal number of processes");
93 >                        continue;
94 >                }
95 >                if (!strcmp(argv[i], "-defaults")) {
96 >                        printopts();
97 >                        print_rdefaults();
98 >                        quit(0);
99 >                }
100 >                rval = getrenderopt(argc-i, argv+i);
101 >                if (rval < 0) {
102 >                        sprintf(errmsg, "bad render option at '%s'", argv[i]);
103 >                        error(USER, errmsg);
104 >                }
105 >                i += rval;
106          }
107 <        if (gargc < 2 || argv[gargc-1][0] == '-')
107 >        gargc = ++i;
108 >                                /* add "mandatory" render options */
109 >        do_irrad = 0;
110 >        if (gargc > argc || argv[gargc-1][0] == '-')
111                  error(USER, "missing octree argument");
112                                  /* else initialize and run our calculation */
113 <        init(nprocs);
114 <        if (gargc+1 < argc)
113 >        init(argv[gargc-1], nprocs);
114 >        if (gargc < argc) {
115 >                if (gargc == argc-1 || argv[gargc][0] != '<' || argv[gargc][1])
116 >                        error(USER, "use '< file1 file2 ..' for multiple inputs");
117                  for (i = gargc+1; i < argc; i++) {
118                          if ((fp = fopen(argv[i], "r")) == NULL) {
119                                  sprintf(errmsg,
# Line 151 | Line 123 | main(          /* compute illum distributions using rtrace */
123                          filter(fp, argv[i]);
124                          fclose(fp);
125                  }
126 <        else
126 >        } else
127                  filter(stdin, "standard input");
128          quit(0);
129 +        return 0; /* pro forma return */
130   }
131  
159 static void
160 killpersist(void)                       /* kill persistent rtrace process */
161 {
162        FILE    *fp = fopen(persistfn, "r");
163        int     pid;
132  
165        if (fp == NULL)
166                return;
167        if (fscanf(fp, "%*s %d", &pid) != 1 || kill(pid, SIGALRM) < 0)
168                unlink(persistfn);
169        fclose(fp);
170 }
171
133   void
134 < quit(int status)                        /* exit with status */
134 > init(char *octnm, int np)               /* start rendering process(es) */
135   {
175        struct rtproc   *rtp;
176        int     rtstat;
177
178        if (rt0.next != NULL)           /* terminate persistent rtrace */
179                killpersist();
180                                        /* clean up rtrace process(es) */
181        for (rtp = &rt0; rtp != NULL; rtp = rtp->next) {
182                rtstat = close_process(&rtp->pd);
183                if (status == 0) {
184                        if (rtstat < 0)
185                                error(WARNING,
186                                "unknown return status from rtrace process");
187                        else
188                                status = rtstat;
189                }
190        }
191        exit(status);
192 }
193
194 void
195 init(int np)                            /* start rtrace and set up buffers */
196 {
197        struct rtproc   *rtp;
198        int     i;
199        int     maxbytes;
200                                        /* set up object functions */
201        ofun[OBJ_FACE].funp = o_face;
202        ofun[OBJ_SPHERE].funp = o_sphere;
203        ofun[OBJ_RING].funp = o_ring;
136                                          /* set up signal handling */
137 < #ifdef SIGPIPE /* not present on Windows */
137 >        signal(SIGINT, quit);
138 > #ifdef SIGHUP
139 >        signal(SIGHUP, quit);
140 > #endif
141 > #ifdef SIGTERM
142 >        signal(SIGTERM, quit);
143 > #endif
144 > #ifdef SIGPIPE
145          signal(SIGPIPE, quit);
146   #endif
147 <        rtp = &rt0;                     /* start rtrace process(es) */
148 <        for (i = 0; i++ < np; ) {
210 <                errno = 0;
211 <                maxbytes = open_process(&rtp->pd, rtargv);
212 <                if (maxbytes == 0) {
213 <                        eputs(rtargv[0]);
214 <                        eputs(": command not found\n");
215 <                        exit(1);
216 <                }
217 <                if (maxbytes < 0)
218 <                        error(SYSTEM, "cannot start rtrace process");
219 <                if (!i && np > 1)
220 <                        sleep(2);       /* wait for persist file */
221 <                rtp->bsiz = maxbytes/(6*sizeof(float));
222 <                rtp->buf = (float *)malloc(6*sizeof(float)*rtp->bsiz--);
223 <                rtp->dest = (float **)calloc(rtp->bsiz, sizeof(float *));
224 <                if (rtp->buf == NULL || rtp->dest == NULL)
225 <                        error(SYSTEM, "out of memory in init");
226 <                rtp->nrays = 0;
227 <                if (i == np)            /* last process? */
228 <                        break;
229 <                rtp->next = (struct rtproc *)malloc(sizeof(struct rtproc));
230 <                if (rtp->next == NULL)
231 <                        error(SYSTEM, "out of memory in init");
232 <                rtp = rtp->next;
233 <        }
234 <        rtp->next = NULL;
235 <                                        /* set up urand */
236 <        initurand(16384);
147 >                                        /* start rendering process(es) */
148 >        ray_pinit(octnm, np);
149   }
150  
151  
152   void
153   eputs(                          /* put string to stderr */
154 <        register char  *s
154 >        const char  *s
155   )
156   {
157          static int  midline = 0;
# Line 255 | Line 167 | eputs(                         /* put string to stderr */
167  
168  
169   void
170 < wputs(s)                        /* print warning if enabled */
259 < char  *s;
170 > wputs(const char *s)                    /* print warning if enabled */
171   {
172          if (warnings)
173                  eputs(s);
# Line 264 | Line 175 | char  *s;
175  
176  
177   void
178 + quit(int ec)                    /* make sure exit is called */
179 + {
180 +        if (ray_pnprocs > 0)    /* close children if any */
181 +                ray_pclose(0);
182 +        else if (ray_pnprocs < 0)
183 +                _exit(ec);      /* avoid flush() in child */
184 +        exit(ec);
185 + }
186 +
187 +
188 + void
189   filter(         /* process stream */
190 <        register FILE   *infp,
190 >        FILE    *infp,
191          char    *name
192   )
193   {
194          char    buf[512];
195          FILE    *pfp;
196 <        register int    c;
196 >        int     c;
197  
198          while ((c = getc(infp)) != EOF) {
199                  if (isspace(c))
# Line 288 | Line 210 | filter(                /* process stream */
210                                  error(SYSTEM, errmsg);
211                          }
212                          filter(pfp, buf);
213 <                        pclose(pfp);
213 >                        if (pclose(pfp) != 0) {
214 >                                sprintf(errmsg, "bad status from \"%s\"", buf);
215 >                                error(WARNING, errmsg);
216 >                        }
217                  } else {                                /* object */
218                          ungetc(c, infp);
219                          xobject(infp, name);
# Line 303 | Line 228 | xoptions(                      /* process options in string s */
228          char    *nm
229   )
230   {
306        extern FILE     *freopen();
231          char    buf[64];
232 +        int     negax;
233          int     nerrs = 0;
234 <        register char   *cp;
234 >        char    *cp;
235  
236          if (strncmp(s, "#@mkillum", 9) || !isspace(s[9])) {
237                  fputs(s, stdout);               /* not for us */
# Line 384 | Line 309 | xoptions(                      /* process options in string s */
309                          }
310                          cp = sskip(cp);
311                          continue;
312 <                case 'd':                       /* point sample density */
312 >                case 'd':                       /* sample density */
313                          if (*++cp != '=')
314                                  break;
315 <                        if (!isintd(++cp, " \t\n\r"))
316 <                                break;
317 <                        thisillum.sampdens = atoi(cp);
315 >                        if (!*++cp || isspace(*cp))
316 >                                continue;
317 >                        if (isintd(cp, " \t\n\r")) {
318 >                                thisillum.sampdens = atoi(cp);
319 >                        } else {
320 >                                error(WARNING, "direct BSDF input unsupported");
321 >                                goto opterr;
322 >                        }
323                          cp = sskip(cp);
324                          continue;
325 <                case 's':                       /* point super-samples */
325 >                case 's':                       /* surface super-samples */
326                          if (*++cp != '=')
327                                  break;
328                          if (!isintd(++cp, " \t\n\r"))
# Line 476 | Line 406 | printopts(void)                        /* print out option default values *
406                  printf("l+\t\t\t\t# light type on\n");
407          else
408                  printf("l-\t\t\t\t# light type off\n");
409 <        printf("d=%d\t\t\t\t# density of points\n", thisillum.sampdens);
410 <        printf("s=%d\t\t\t\t# samples per point\n", thisillum.nsamps);
409 >        printf("d=%d\t\t\t\t# density of directions\n", thisillum.sampdens);
410 >        printf("s=%d\t\t\t\t# samples per direction\n", thisillum.nsamps);
411          printf("b=%f\t\t\t# minimum average brightness\n", thisillum.minbrt);
412   }
413  
414  
415   void
416   printhead(                      /* print out header */
417 <        register int  ac,
418 <        register char  **av
417 >        int  ac,
418 >        char  **av
419   )
420   {
421          putchar('#');
# Line 552 | Line 482 | xobject(                               /* translate an object from fp */
482          checkhead();
483                                                  /* process object */
484          if (doit)
485 <                (*ofun[thisobj.otype].funp)(&thisobj, &thisillum, &rt0, nm);
485 >                switch (thisobj.otype) {
486 >                case OBJ_FACE:
487 >                        my_face(&thisobj, &thisillum, nm);
488 >                        break;
489 >                case OBJ_SPHERE:
490 >                        my_sphere(&thisobj, &thisillum, nm);
491 >                        break;
492 >                case OBJ_RING:
493 >                        my_ring(&thisobj, &thisillum, nm);
494 >                        break;
495 >                default:
496 >                        my_default(&thisobj, &thisillum, nm);
497 >                        break;
498 >                }
499          else
500                  printobj(thisillum.altmat, &thisobj);
501                                                  /* free arguments */
502          freefargs(&thisobj.oargs);
503          return;
504   readerr:
505 <        sprintf(errmsg, "(%s): error reading scene", nm);
505 >        sprintf(errmsg, "(%s): error reading input", nm);
506          error(USER, errmsg);
507   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines