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

Comparing ray/src/util/rfluxmtx.c (file contents):
Revision 2.1 by greg, Sat Jul 19 01:19:49 2014 UTC vs.
Revision 2.44 by greg, Thu Apr 13 22:47:17 2017 UTC

# Line 11 | Line 11 | static const char RCSid[] = "$Id$";
11   #include <stdlib.h>
12   #include "rtio.h"
13   #include "rtmath.h"
14 + #include "paths.h"
15   #include "bsdf.h"
16   #include "bsdf_m.h"
17   #include "random.h"
18   #include "triangulate.h"
19   #include "platform.h"
20  
20 #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
21 #undef getc
22 #define getc    getc_unlocked
23 #endif
24
25 #ifdef _WIN32
26 #define SPECIALS        " \t$*?"
27 #define QUOTCHAR        '"'
28 #else
29 #define SPECIALS        " \t\n'\"()${}*?[]"
30 #define QUOTCHAR        '\''
31 #define ALTQUOT         '"'
32 #endif
33
21   #define MAXRCARG        512
22  
23   char            *progname;              /* global argv[0] */
24  
25 < int             verbose = 0;            /* verbose mode? */
25 > int             verbose = 0;            /* verbose mode (< 0 no warnings) */
26  
27   char            *rcarg[MAXRCARG+1] = {"rcontrib", "-fo+"};
28   int             nrcargs = 2;
# Line 45 | Line 32 | const char     overflowerr[] = "%s: too many arguments!\n"
32   #define CHECKARGC(n)    if (nrcargs >= MAXRCARG-(n)) \
33          { fprintf(stderr, overflowerr, progname); exit(1); }
34  
35 < int             sampcnt = 0;            /* sample count (unset) */
35 > int             sampcnt = 0;            /* sample count (0==unset) */
36  
37 < char            *reinhfn = "reinhart.cal";
38 < char            *shirchufn = "disk2square.cal";
37 > char            *reinhfn = "reinhartb.cal";
38 > char            *shirchiufn = "disk2square.cal";
39   char            *kfullfn = "klems_full.cal";
40   char            *khalffn = "klems_half.cal";
41   char            *kquarterfn = "klems_quarter.cal";
42  
43 < #define PARAMSTART      "@rfluxmtx"     /* string indicating parameters */
43 >                                        /* string indicating parameters */
44 > const char      PARAMSTART[] = "@rfluxmtx";
45  
46                                  /* surface type IDs */
47   #define ST_NONE         0
# Line 66 | Line 54 | typedef struct surf_s {
54          void            *priv;          /* private data (malloc'ed) */
55          char            sname[32];      /* surface name */
56          FVECT           snrm;           /* surface normal */
57 <        double          area;           /* surface area (or solid angle) */
57 >        double          area;           /* surface area / proj. solid angle */
58          short           styp;           /* surface type */
59 <        short           nfargs;         /* number of arguments */
60 <        double          farg[1];        /* arguments (extends struct) */
59 >        short           nfargs;         /* number of real arguments */
60 >        double          farg[1];        /* real values (extends struct) */
61   } SURF;                         /* surface structure */
62  
63   typedef struct {
64          FVECT   uva[2];                 /* tangent axes */
65          int     ntris;                  /* number of triangles */
66          struct ptri {
67 <                float   afrac;                  /* fraction of total area */
67 >                double  afrac;                  /* fraction of total area */
68                  short   vndx[3];                /* vertex indices */
69          }       tri[1];                 /* triangle array (extends struct) */
70   } POLYTRIS;                     /* triangulated polygon */
71  
72   typedef struct param_s {
73 <        char            hemis[32];      /* hemispherical sampling spec. */
73 >        char            sign;           /* '-' for axis reversal */
74 >        char            hemis[31];      /* hemispherical sampling spec. */
75          int             hsiz;           /* hemisphere basis size */
76          int             nsurfs;         /* number of surfaces */
77          SURF            *slist;         /* list of surfaces */
78          FVECT           vup;            /* up vector (zero if unset) */
79          FVECT           nrm;            /* average normal direction */
80 <        FVECT           udir, vdir;     /* v-up tangent axes */
80 >        FVECT           udir, vdir;     /* tangent axes */
81          char            *outfn;         /* output file name (receiver) */
82          int             (*sample_basis)(struct param_s *p, int, FILE *);
83   } PARAMS;                       /* sender/receiver parameters */
84  
85   PARAMS          curparams;
86   char            curmod[128];
87 + char            newparams[1024];
88  
89   typedef int     SURFSAMP(FVECT, SURF *, double);
90  
# Line 108 | Line 98 | SURFSAMP       *orig_in_surf[4] = {
98   static void
99   clear_params(PARAMS *p, int reset_only)
100   {
111        curmod[0] = '\0';
101          while (p->slist != NULL) {
102                  SURF    *sdel = p->slist;
103                  p->slist = sdel->next;
# Line 123 | Line 112 | clear_params(PARAMS *p, int reset_only)
112                  p->outfn = NULL;
113                  return;
114          }
115 <        memset(p, 0, sizeof(curparams));
115 >        memset(p, 0, sizeof(PARAMS));
116   }
117  
118   /* Get surface type from name */
# Line 143 | Line 132 | surf_type(const char *otype)
132   static char *
133   oconv_command(int ac, char *av[])
134   {
135 <        static char     oconvbuf[2048] = "!oconv -f";
136 <        char            *cp = oconvbuf + 9;
137 <
135 >        static char     oconvbuf[2048] = "!oconv -f ";
136 >        char            *cp = oconvbuf + 10;
137 >        char            *recv = *av++;
138 >        
139 >        if (ac-- <= 0)
140 >                return(NULL);
141 >        if (verbose < 0) {      /* turn off warnings */
142 >                strcpy(cp, "-w- ");
143 >                cp += 4;
144 >        }
145          while (ac-- > 0) {
150                if (cp >= oconvbuf+(sizeof(oconvbuf)-32)) {
151                        fputs(progname, stderr);
152                        fputs(": too many file arguments!\n", stderr);
153                        exit(1);
154                }
155                *cp++ = ' ';
146                  strcpy(cp, *av++);
147                  while (*cp) cp++;
148 +                *cp++ = ' ';
149 +                if (cp >= oconvbuf+(sizeof(oconvbuf)-32))
150 +                        goto overrun;
151          }
152 <        *cp = '\0';
152 >                                /* receiver goes last */
153 >        if (matchany(recv, SPECIALS)) {
154 >                *cp++ = QUOTCHAR;
155 >                while (*recv) {
156 >                        if (cp >= oconvbuf+(sizeof(oconvbuf)-3))
157 >                                goto overrun;
158 >                        *cp++ = *recv++;
159 >                }
160 >                *cp++ = QUOTCHAR;
161 >                *cp = '\0';
162 >        } else
163 >                strcpy(cp, recv);
164          return(oconvbuf);
165 + overrun:
166 +        fputs(progname, stderr);
167 +        fputs(": too many file arguments!\n", stderr);
168 +        exit(1);
169   }
170  
163 /* Check if any of the characters in str2 are found in str1 */
164 static int
165 matchany(const char *str1, const char *str2)
166 {
167        while (*str1) {
168                const char      *cp = str2;
169                while (*cp)
170                        if (*cp++ == *str1)
171                                return(*str1);
172                ++str1;
173        }
174        return(0);
175 }
176
177
178 /* Convert a set of arguments into a command line for pipe() or system() */
179 static char *
180 convert_commandline(char *cmd, const int len, char *av[])
181 {
182        int     match;
183        char    *cp;
184
185        for (cp = cmd; *av != NULL; av++) {
186                const int       n = strlen(*av);
187                if (cp+n >= cmd+(len-3)) {
188                        fputs(progname, stderr);
189                        return(NULL);
190                }
191                if ((match = matchany(*av, SPECIALS))) {
192                        const int       quote =
193 #ifdef ALTQUOT
194                                (match == QUOTCHAR) ? ALTQUOT :
195 #endif
196                                        QUOTCHAR;
197                        *cp++ = quote;
198                        strcpy(cp, *av);
199                        cp += n;
200                        *cp++ = quote;
201                } else {
202                        strcpy(cp, *av);
203                        cp += n;
204                }
205                *cp++ = ' ';
206        }
207        if (cp <= cmd)
208                return(NULL);
209        *--cp = '\0';
210        return(cmd);
211 }
212
171   /* Open a pipe to/from a command given as an argument list */
172   static FILE *
173   popen_arglist(char *av[], char *mode)
# Line 221 | Line 179 | popen_arglist(char *av[], char *mode)
179                  fputs(": command line too long in popen_arglist()\n", stderr);
180                  return(NULL);
181          }
182 <        if (verbose)
182 >        if (verbose > 0)
183                  fprintf(stderr, "%s: opening pipe %s: %s\n",
184                                  progname, (*mode=='w') ? "to" : "from", cmd);
185          return(popen(cmd, mode));
186   }
187  
188 < #ifdef _WIN32
188 > #if defined(_WIN32) || defined(_WIN64)
189   /* Execute system command (Windows version) */
190   static int
191   my_exec(char *av[])
# Line 239 | Line 197 | my_exec(char *av[])
197                  fputs(": command line too long in my_exec()\n", stderr);
198                  return(1);
199          }
200 <        if (verbose)
200 >        if (verbose > 0)
201                  fprintf(stderr, "%s: running: %s\n", progname, cmd);
202          return(system(cmd));
203   }
# Line 254 | Line 212 | my_exec(char *av[])
212                  fprintf(stderr, "%s: cannot locate %s\n", progname, av[0]);
213                  return(1);
214          }
215 <        if (verbose) {
215 >        if (verbose > 0) {
216                  char    cmd[4096];
217                  if (!convert_commandline(cmd, sizeof(cmd), av))
218                          strcpy(cmd, "COMMAND TOO LONG TO SHOW");
# Line 308 | Line 266 | nextchar:
266  
267   /* Parse program parameters (directives) */
268   static int
269 < parse_params(char *pargs)
269 > parse_params(PARAMS *p, char *pargs)
270   {
271          char    *cp = pargs;
272          int     nparams = 0;
273 +        int     quot;
274          int     i;
275  
276 <        for ( ; ; )
276 >        for ( ; ; ) {
277                  switch (*cp++) {
278                  case 'h':
279                          if (*cp++ != '=')
280                                  break;
281 <                        curparams.hsiz = 0;
281 >                        if ((*cp == '+') | (*cp == '-'))
282 >                                p->sign = *cp++;
283 >                        else
284 >                                p->sign = '+';
285 >                        p->hsiz = 0;
286                          i = 0;
287                          while (*cp && !isspace(*cp)) {
288                                  if (isdigit(*cp))
289 <                                        curparams.hsiz = 10*curparams.hsiz +
290 <                                                                *cp - '0';
328 <                                curparams.hemis[i++] = *cp++;
289 >                                        p->hsiz = 10*p->hsiz + *cp - '0';
290 >                                p->hemis[i++] = *cp++;
291                          }
292                          if (!i)
293                                  break;
294 <                        curparams.hemis[i] = '\0';
295 <                        curparams.hsiz += !curparams.hsiz;
294 >                        p->hemis[i] = '\0';
295 >                        p->hsiz += !p->hsiz;
296                          ++nparams;
297                          continue;
298                  case 'u':
299                          if (*cp++ != '=')
300                                  break;
301 <                        if (!get_direction(curparams.vup, cp))
301 >                        if (!get_direction(p->vup, cp))
302                                  break;
303 +                        while (*cp && !isspace(*cp++))
304 +                                ;
305                          ++nparams;
306                          continue;
307                  case 'o':
308                          if (*cp++ != '=')
309                                  break;
310 +                        quot = 0;
311 +                        if ((*cp == '"') | (*cp == '\''))
312 +                                quot = *cp++;
313                          i = 0;
314 <                        while (*cp && !isspace(*cp++))
315 <                                i++;
314 >                        while (*cp && (quot ? (*cp != quot) : !isspace(*cp))) {
315 >                                i++; cp++;
316 >                        }
317                          if (!i)
318                                  break;
319 <                        *--cp = '\0';
320 <                        curparams.outfn = savqstr(cp-i);
321 <                        *cp++ = ' ';
319 >                        if (!*cp) {
320 >                                if (quot)
321 >                                        break;
322 >                                cp[1] = '\0';
323 >                        }
324 >                        *cp = '\0';
325 >                        p->outfn = savqstr(cp-i);
326 >                        *cp++ = quot ? quot : ' ';
327                          ++nparams;
328                          continue;
329                  case ' ':
# Line 363 | Line 336 | parse_params(char *pargs)
336                  default:
337                          break;
338                  }
339 <        fprintf(stderr, "%s: bad parameter string '%s'\n", progname, pargs);
339 >                break;
340 >        }
341 >        fprintf(stderr, "%s: bad parameter string: %s", progname, pargs);
342          exit(1);
343          return(-1);     /* pro forma return */
344   }
# Line 372 | Line 347 | parse_params(char *pargs)
347   static void
348   finish_receiver(void)
349   {
350 <        char    binbuf[128];
350 >        char    sbuf[256];
351 >        int     uniform = 0;
352 >        char    *calfn = NULL;
353 >        char    *params = NULL;
354 >        char    *binv = NULL;
355 >        char    *binf = NULL;
356 >        char    *nbins = NULL;
357  
358          if (!curmod[0]) {
359                  fputs(progname, stderr);
# Line 384 | Line 365 | finish_receiver(void)
365                  rcarg[nrcargs++] = "-o";
366                  rcarg[nrcargs++] = curparams.outfn;
367          }
368 <                                        /* add bin specification */
368 >                                        /* check arguments */
369          if (!curparams.hemis[0]) {
370                  fputs(progname, stderr);
371                  fputs(": missing hemisphere sampling type!\n", stderr);
# Line 395 | Line 376 | finish_receiver(void)
376                  fputs(": undefined normal for hemisphere sampling\n", stderr);
377                  exit(1);
378          }
379 <        if (normalize(curparams.vup) == 0)
379 >        if (normalize(curparams.vup) == 0) {
380                  if (fabs(curparams.nrm[2]) < .7)
381                          curparams.vup[2] = 1;
382                  else
383                          curparams.vup[1] = 1;
384 <        if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') {
385 <                CHECKARGC(2);
386 <                rcarg[nrcargs++] = "-b";
387 <                rcarg[nrcargs++] = "0";
384 >        }
385 >                                        /* determine sample type/bin */
386 >        if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1')) {
387 >                sprintf(sbuf, "if(-Dx*%g-Dy*%g-Dz*%g,0,-1)",
388 >                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2]);
389 >                binv = savqstr(sbuf);
390 >                nbins = "1";            /* uniform sampling -- one bin */
391 >                uniform = 1;
392          } else if (tolower(curparams.hemis[0]) == 's' &&
393                                  tolower(curparams.hemis[1]) == 'c') {
394 <                if (shirchufn != NULL) {
410 <                        CHECKARGC(2);
411 <                        rcarg[nrcargs++] = "-f";
412 <                        rcarg[nrcargs++] = shirchufn;
413 <                        shirchufn = NULL;
414 <                }
415 < fputs("Shirley-Chiu unimplemented...\n",stderr); exit(1);
394 >                                        /* assign parameters */
395                  if (curparams.hsiz <= 1) {
396                          fputs(progname, stderr);
397                          fputs(": missing size for Shirley-Chiu sampling!\n", stderr);
398                          exit(1);
399                  }
400 +                calfn = shirchiufn; shirchiufn = NULL;
401 +                sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
402 +                                curparams.hsiz,
403 +                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
404 +                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
405 +                        curparams.sign);
406 +                params = savqstr(sbuf);
407 +                binv = "scbin";
408 +                nbins = "SCdim*SCdim";
409          } else if ((tolower(curparams.hemis[0]) == 'r') |
410                          (tolower(curparams.hemis[0]) == 't')) {
411 <                if (reinhfn != NULL) {
412 <                        CHECKARGC(2);
413 <                        rcarg[nrcargs++] = "-f";
414 <                        rcarg[nrcargs++] = reinhfn;
415 <                        reinhfn = NULL;
416 <                }
417 < fputs("Reinhart/Tregenza unimplemented...\n",stderr); exit(1);
411 >                calfn = reinhfn; reinhfn = NULL;
412 >                sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g,RHS=%c1",
413 >                                curparams.hsiz,
414 >                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
415 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2],
416 >                        curparams.sign);
417 >                params = savqstr(sbuf);
418 >                binv = "rbin";
419 >                nbins = "Nrbins";
420          } else if (tolower(curparams.hemis[0]) == 'k' &&
421                          !curparams.hemis[1] |
422                          (tolower(curparams.hemis[1]) == 'f') |
423                          (curparams.hemis[1] == '1')) {
424 <                if (kfullfn != NULL) {
425 <                        CHECKARGC(2);
426 <                        rcarg[nrcargs++] = "-f";
437 <                        rcarg[nrcargs++] = kfullfn;
438 <                        kfullfn = NULL;
439 <                }
440 <                CHECKARGC(4);
441 <                sprintf(binbuf, "kbin(%g,%g,%g,%g,%g,%g)",
442 <                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
443 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
444 <                rcarg[nrcargs++] = "-b";
445 <                rcarg[nrcargs++] = savqstr(binbuf);
446 <                rcarg[nrcargs++] = "-bn";
447 <                rcarg[nrcargs++] = "Nkbins";
424 >                calfn = kfullfn; kfullfn = NULL;
425 >                binf = "kbin";
426 >                nbins = "Nkbins";
427          } else if (tolower(curparams.hemis[0]) == 'k' &&
428                          (tolower(curparams.hemis[1]) == 'h') |
429                          (curparams.hemis[1] == '2')) {
430 <                if (khalffn != NULL) {
431 <                        CHECKARGC(2);
432 <                        rcarg[nrcargs++] = "-f";
454 <                        rcarg[nrcargs++] = khalffn;
455 <                        khalffn = NULL;
456 <                }
457 <                CHECKARGC(4);
458 <                sprintf(binbuf, "khbin(%g,%g,%g,%g,%g,%g)",
459 <                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
460 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
461 <                rcarg[nrcargs++] = "-b";
462 <                rcarg[nrcargs++] = savqstr(binbuf);
463 <                rcarg[nrcargs++] = "-bn";
464 <                rcarg[nrcargs++] = "Nkhbins";
430 >                calfn = khalffn; khalffn = NULL;
431 >                binf = "khbin";
432 >                nbins = "Nkhbins";
433          } else if (tolower(curparams.hemis[0]) == 'k' &&
434                          (tolower(curparams.hemis[1]) == 'q') |
435                          (curparams.hemis[1] == '4')) {
436 <                if (kquarterfn != NULL) {
437 <                        CHECKARGC(2);
438 <                        rcarg[nrcargs++] = "-f";
471 <                        rcarg[nrcargs++] = kquarterfn;
472 <                        kquarterfn = NULL;
473 <                }
474 <                CHECKARGC(4);
475 <                sprintf(binbuf, "kqbin(%g,%g,%g,%g,%g,%g)",
476 <                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
477 <                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
478 <                rcarg[nrcargs++] = "-b";
479 <                rcarg[nrcargs++] = savqstr(binbuf);
480 <                rcarg[nrcargs++] = "-bn";
481 <                rcarg[nrcargs++] = "Nkqbins";
436 >                calfn = kquarterfn; kquarterfn = NULL;
437 >                binf = "kqbin";
438 >                nbins = "Nkqbins";
439          } else {
440                  fprintf(stderr, "%s: unrecognized hemisphere sampling: h=%s\n",
441                                  progname, curparams.hemis);
442                  exit(1);
443          }
444 <        CHECKARGC(2);                   /* modifier argument goes last */
444 >        if (tolower(curparams.hemis[0]) == 'k') {
445 >                sprintf(sbuf, "RHS=%c1", curparams.sign);
446 >                params = savqstr(sbuf);
447 >        }
448 >        if (!uniform & (curparams.slist->styp == ST_SOURCE)) {
449 >                SURF    *sp;
450 >                for (sp = curparams.slist; sp != NULL; sp = sp->next)
451 >                        if (fabs(sp->area - PI) > 1e-3) {
452 >                                fprintf(stderr, "%s: source '%s' must be 180-degrees\n",
453 >                                                progname, sp->sname);
454 >                                exit(1);
455 >                        }
456 >        }
457 >        if (calfn != NULL) {            /* add cal file if needed */
458 >                CHECKARGC(2);
459 >                rcarg[nrcargs++] = "-f";
460 >                rcarg[nrcargs++] = calfn;
461 >        }
462 >        if (params != NULL) {           /* parameters _after_ cal file */
463 >                CHECKARGC(2);
464 >                rcarg[nrcargs++] = "-p";
465 >                rcarg[nrcargs++] = params;
466 >        }
467 >        if (nbins != NULL) {            /* add #bins if set */
468 >                CHECKARGC(2);
469 >                rcarg[nrcargs++] = "-bn";
470 >                rcarg[nrcargs++] = nbins;
471 >        }
472 >        if (binv != NULL) {
473 >                CHECKARGC(2);           /* assign bin variable */
474 >                rcarg[nrcargs++] = "-b";
475 >                rcarg[nrcargs++] = binv;
476 >        } else if (binf != NULL) {
477 >                CHECKARGC(2);           /* assign bin function */
478 >                rcarg[nrcargs++] = "-b";
479 >                sprintf(sbuf, "%s(%g,%g,%g,%g,%g,%g)", binf,
480 >                        curparams.nrm[0], curparams.nrm[1], curparams.nrm[2],
481 >                        curparams.vup[0], curparams.vup[1], curparams.vup[2]);
482 >                rcarg[nrcargs++] = savqstr(sbuf);
483 >        }
484 >        CHECKARGC(2);                           /* modifier argument goes last */
485          rcarg[nrcargs++] = "-m";
486          rcarg[nrcargs++] = savqstr(curmod);
487   }
# Line 495 | Line 492 | make_axes(FVECT uva[2], const FVECT nrm)
492   {
493          int     i;
494  
495 <        uva[1][0] = 0.5 - frandom();
499 <        uva[1][1] = 0.5 - frandom();
500 <        uva[1][2] = 0.5 - frandom();
501 <        for (i = 3; i--; )
502 <                if ((-0.6 < nrm[i]) & (nrm[i] < 0.6))
503 <                        break;
504 <        if (i < 0) {
495 >        if (!getperpendicular(uva[0], nrm, 1)) {
496                  fputs(progname, stderr);
497                  fputs(": bad surface normal in make_axes!\n", stderr);
498                  exit(1);
499          }
500 <        uva[1][i] = 1.0;
510 <        VCROSS(uva[0], uva[1], nrm);
511 <        normalize(uva[0]);
512 <        VCROSS(uva[1], nrm, uva[0]);
500 >        fcross(uva[1], nrm, uva[0]);
501   }
502  
503   /* Illegal sender surfaces end up here */
# Line 541 | Line 529 | ssamp_ring(FVECT orig, SURF *sp, double x)
529                  sp->priv = (void *)uva;
530          }
531          SDmultiSamp(samp2, 2, x);
532 <        samp2[0] = sp->farg[6] + sqrt(samp2[0]*sp->area*(1./PI));
532 >        samp2[0] = sqrt(samp2[0]*sp->area*(1./PI) + sp->farg[6]*sp->farg[6]);
533          samp2[1] *= 2.*PI;
534          uv[0] = samp2[0]*tcos(samp2[1]);
535          uv[1] = samp2[0]*tsin(samp2[1]);
# Line 594 | Line 582 | ssamp_poly(FVECT orig, SURF *sp, double x)
582                          }
583                          ptp->ntris = 0;
584                          v2l->p = (void *)ptp;
585 <                        if (!polyTriangulate(v2l, add_triangle))
585 >                        if (!polyTriangulate(v2l, add_triangle)) {
586 >                                fprintf(stderr,
587 >                                        "%s: cannot triangulate polygon '%s'\n",
588 >                                                progname, sp->sname);
589                                  return(0);
590 +                        }
591                          for (i = ptp->ntris; i--; ) {
592                                  int     a = ptp->tri[i].vndx[0];
593                                  int     b = ptp->tri[i].vndx[1];
# Line 614 | Line 606 | ssamp_poly(FVECT orig, SURF *sp, double x)
606                  sp->priv = (void *)ptp;
607          }
608                                          /* pick triangle by partial area */
609 <        for (i = 0; i < ptp->ntris && x > ptp->tri[i].afrac; i++)
609 >        for (i = 0; i < ptp->ntris-1 && x > ptp->tri[i].afrac; i++)
610                  x -= ptp->tri[i].afrac;
611          SDmultiSamp(samp2, 2, x/ptp->tri[i].afrac);
612          samp2[0] *= samp2[1] = sqrt(samp2[1]);
# Line 644 | Line 636 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
636                                          /* special case for lone surface */
637          if (p->nsurfs == 1) {
638                  sp = p->slist;
639 <                if (DOT(sp->snrm, rdir) >= -FTINY)
640 <                        return(0);      /* behind surface! */
639 >                if (DOT(sp->snrm, rdir) >= FTINY) {
640 >                        fprintf(stderr,
641 >                                "%s: internal - sample behind sender '%s'\n",
642 >                                        progname, sp->sname);
643 >                        return(0);
644 >                }
645                  return((*orig_in_surf[sp->styp])(orig, sp, x));
646          }
647          if (p->nsurfs > nall) {         /* (re)allocate surface area cache */
648                  if (projsa) free(projsa);
649                  projsa = (double *)malloc(sizeof(double)*p->nsurfs);
650 <                if (!projsa) return(0);
650 >                if (projsa == NULL) {
651 >                        fputs(progname, stderr);
652 >                        fputs(": out of memory in sample_origin!\n", stderr);
653 >                        exit(1);
654 >                }
655                  nall = p->nsurfs;
656          }
657                                          /* compute projected areas */
# Line 659 | Line 659 | sample_origin(PARAMS *p, FVECT orig, const FVECT rdir,
659                  projsa[i] = -DOT(sp->snrm, rdir) * sp->area;
660                  tarea += projsa[i] *= (double)(projsa[i] > FTINY);
661          }
662 <        if (tarea <= FTINY)             /* wrong side of sender? */
662 >        if (tarea <= FTINY) {           /* wrong side of sender? */
663 >                fputs(progname, stderr);
664 >                fputs(": internal - sample behind all sender elements!\n",
665 >                                stderr);
666                  return(0);
667 +        }
668          tarea *= x;                     /* get surface from list */
669          for (i = 0, sp = p->slist; tarea > projsa[i]; sp = sp->next)
670                  tarea -= projsa[i++];
# Line 690 | Line 694 | sample_uniform(PARAMS *p, int b, FILE *fp)
694                                                  duvw[2]*p->nrm[i] ;
695                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
696                          return(0);
697 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
697 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
698                          return(0);
699          }
700          return(1);
# Line 720 | Line 724 | sample_shirchiu(PARAMS *p, int b, FILE *fp)
724                                                  duvw[2]*p->nrm[i] ;
725                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
726                          return(0);
727 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
727 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
728                          return(0);
729          }
730          return(1);
# Line 733 | Line 737 | sample_reinhart(PARAMS *p, int b, FILE *fp)
737   #define T_NALT  7
738          static const int        tnaz[T_NALT] = {30, 30, 24, 24, 18, 12, 6};
739          const int               RowMax = T_NALT*p->hsiz + 1;
740 <        const double            RAH = (.25*PI)/(RowMax-.5);
740 >        const double            RAH = (.5*PI)/(RowMax-.5);
741   #define rnaz(r)                 (r >= RowMax-1 ? 1 : p->hsiz*tnaz[r/p->hsiz])
742          int                     n = sampcnt;
743          int                     row, col;
# Line 758 | Line 762 | sample_reinhart(PARAMS *p, int b, FILE *fp)
762                  SDmultiSamp(samp3, 3, (n+frandom())/sampcnt);
763                  alt = (row+samp3[1])*RAH;
764                  azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row);
765 <                duvw[2] = tcos(alt);    /* measured from horizon */
766 <                duvw[0] = tcos(azi)*duvw[2];
767 <                duvw[1] = tsin(azi)*duvw[2];
765 >                duvw[2] = cos(alt);     /* measured from horizon */
766 >                duvw[0] = tsin(azi)*duvw[2];
767 >                duvw[1] = tcos(azi)*duvw[2];
768                  duvw[2] = sqrt(1. - duvw[2]*duvw[2]);
769                  for (i = 3; i--; )
770                          orig_dir[1][i] = -duvw[0]*p->udir[i] -
# Line 768 | Line 772 | sample_reinhart(PARAMS *p, int b, FILE *fp)
772                                                  duvw[2]*p->nrm[i] ;
773                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp3[0]))
774                          return(0);
775 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
775 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
776                          return(0);
777          }
778          return(1);
# Line 811 | Line 815 | sample_klems(PARAMS *p, int b, FILE *fp)
815  
816          while (n--) {                   /* stratified sampling */
817                  SDmultiSamp(samp2, 2, (n+frandom())/sampcnt);
818 <                if (!bo_getvec(duvw, b+samp2[1], kbasis[bi]))
818 >                if (!fo_getvec(duvw, b+samp2[1], kbasis[bi]))
819                          return(0);
820                  for (i = 3; i--; )
821 <                        orig_dir[1][i] = duvw[0]*p->udir[i] +
822 <                                                duvw[1]*p->vdir[i] +
821 >                        orig_dir[1][i] = -duvw[0]*p->udir[i] -
822 >                                                duvw[1]*p->vdir[i] -
823                                                  duvw[2]*p->nrm[i] ;
824                  if (!sample_origin(p, orig_dir[0], orig_dir[1], samp2[0]))
825                          return(0);
826 <                if (fwrite(orig_dir, sizeof(FVECT), 2, fp) != 2)
826 >                if (putbinary(orig_dir, sizeof(FVECT), 2, fp) != 2)
827                          return(0);
828          }
829          return(1);
# Line 834 | Line 838 | prepare_sampler(void)
838                  fputs(": no sender surface!\n", stderr);
839                  return(-1);
840          }
841 <        if (curparams.outfn != NULL)    /* misplaced output file spec. */
841 >                                        /* misplaced output file spec. */
842 >        if ((curparams.outfn != NULL) & (verbose >= 0))
843                  fprintf(stderr, "%s: warning - ignoring output file in sender ('%s')\n",
844                                  progname, curparams.outfn);
845                                          /* check/set basis hemisphere */
# Line 848 | Line 853 | prepare_sampler(void)
853                  fputs(": undefined normal for sender sampling\n", stderr);
854                  return(-1);
855          }
856 <        if (normalize(curparams.vup) == 0)
856 >        if (normalize(curparams.vup) == 0) {
857                  if (fabs(curparams.nrm[2]) < .7)
858                          curparams.vup[2] = 1;
859                  else
860                          curparams.vup[1] = 1;
861 <        VCROSS(curparams.udir, curparams.vup, curparams.nrm);
861 >        }
862 >        fcross(curparams.udir, curparams.vup, curparams.nrm);
863          if (normalize(curparams.udir) == 0) {
864                  fputs(progname, stderr);
865                  fputs(": up vector coincides with sender normal\n", stderr);
866                  return(-1);
867          }
868 <        VCROSS(curparams.vdir, curparams.nrm, curparams.udir);
869 <        if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1')
868 >        fcross(curparams.vdir, curparams.nrm, curparams.udir);
869 >        if (curparams.sign == '-') {    /* left-handed coordinate system? */
870 >                curparams.udir[0] *= -1.;
871 >                curparams.udir[1] *= -1.;
872 >                curparams.udir[2] *= -1.;
873 >        }
874 >        if ((tolower(curparams.hemis[0]) == 'u') | (curparams.hemis[0] == '1'))
875                  curparams.sample_basis = sample_uniform;
876          else if (tolower(curparams.hemis[0]) == 's' &&
877                                  tolower(curparams.hemis[1]) == 'c')
# Line 939 | Line 950 | add_surface(int st, const char *oname, FILE *fp)
950          snew = (SURF *)malloc(sizeof(SURF) + sizeof(double)*(n-1));
951          if (snew == NULL) {
952                  fputs(progname, stderr);
953 <                fputs(": out of memory!\n", stderr);
953 >                fputs(": out of memory in add_surface!\n", stderr);
954                  exit(1);
955          }
956          strncpy(snew->sname, oname, sizeof(snew->sname)-1);
# Line 976 | Line 987 | add_surface(int st, const char *oname, FILE *fp)
987          case ST_SOURCE:
988                  if (snew->nfargs != 4)
989                          goto badcount;
990 <                VCOPY(snew->snrm, snew->farg);
990 >                for (n = 3; n--; )      /* need to reverse "normal" */
991 >                        snew->snrm[n] = -snew->farg[n];
992                  if (normalize(snew->snrm) == 0)
993                          goto badnorm;
994 <                snew->area = 2.*PI*(1. - cos((PI/180./2.)*snew->farg[3]));
994 >                snew->area = sin((PI/180./2.)*snew->farg[3]);
995 >                snew->area *= PI*snew->area;
996                  break;
997          }
998 <        if (snew->area <= FTINY) {
998 >        if ((snew->area <= FTINY) & (verbose >= 0)) {
999                  fprintf(stderr, "%s: warning - zero area for surface '%s'\n",
1000                                  progname, oname);
1001                  free(snew);
# Line 994 | Line 1007 | add_surface(int st, const char *oname, FILE *fp)
1007          curparams.nsurfs++;
1008          return;
1009   badcount:
1010 <        fprintf(stderr, "%s: bad argument count for surface '%s'\n",
1010 >        fprintf(stderr, "%s: bad argument count for surface element '%s'\n",
1011                          progname, oname);
1012          exit(1);
1013   badnorm:
1014 <        fprintf(stderr, "%s: bad orientation for surface '%s'\n",
1014 >        fprintf(stderr, "%s: bad orientation for surface element '%s'\n",
1015                          progname, oname);
1016          exit(1);
1017   }
# Line 1030 | Line 1043 | add_recv_object(FILE *fp)
1043                                  finish_receiver();
1044                                  clear_params(&curparams, 1);
1045                          }
1046 +                        parse_params(&curparams, newparams);
1047 +                        newparams[0] = '\0';
1048                          strcpy(curmod, thismod);
1049                  }
1050                  add_surface(st, oname, fp);     /* read & store surface */
1051                  return(1);
1052          }
1053                                          /* else skip arguments */
1054 <        if (!fscanf(fp, "%d", &n)) return;
1054 >        if (!fscanf(fp, "%d", &n)) return(0);
1055          while (n-- > 0) fscanf(fp, "%*s");
1056 <        if (!fscanf(fp, "%d", &n)) return;
1056 >        if (!fscanf(fp, "%d", &n)) return(0);
1057          while (n-- > 0) fscanf(fp, "%*d");
1058 <        if (!fscanf(fp, "%d", &n)) return;
1058 >        if (!fscanf(fp, "%d", &n)) return(0);
1059          while (n-- > 0) fscanf(fp, "%*f");
1060          return(0);
1061   }
# Line 1050 | Line 1065 | static int
1065   add_send_object(FILE *fp)
1066   {
1067          int             st;
1068 <        char            otype[32], oname[128];
1068 >        char            thismod[128], otype[32], oname[128];
1069          int             n;
1070  
1071 <        if (fscanf(fp, "%*s %s %s", otype, oname) != 2)
1071 >        if (fscanf(fp, "%s %s %s", thismod, otype, oname) != 3)
1072                  return(0);              /* must have hit EOF! */
1073          if (!strcmp(otype, "alias")) {
1074                  fscanf(fp, "%*s");      /* skip alias */
# Line 1066 | Line 1081 | add_send_object(FILE *fp)
1081                          fputs(": cannot use source as a sender!\n", stderr);
1082                          return(-1);
1083                  }
1084 +                if (strcmp(thismod, curmod)) {
1085 +                        if (curmod[0]) {
1086 +                                fputs(progname, stderr);
1087 +                                fputs(": warning - multiple modifiers in sender\n",
1088 +                                                stderr);
1089 +                        }
1090 +                        strcpy(curmod, thismod);
1091 +                }
1092 +                parse_params(&curparams, newparams);
1093 +                newparams[0] = '\0';
1094                  add_surface(st, oname, fp);     /* read & store surface */
1095                  return(0);
1096          }
1097                                          /* else skip arguments */
1098 <        if (!fscanf(fp, "%d", &n)) return;
1098 >        if (!fscanf(fp, "%d", &n)) return(0);
1099          while (n-- > 0) fscanf(fp, "%*s");
1100 <        if (!fscanf(fp, "%d", &n)) return;
1100 >        if (!fscanf(fp, "%d", &n)) return(0);
1101          while (n-- > 0) fscanf(fp, "%*d");
1102 <        if (!fscanf(fp, "%d", &n)) return;
1102 >        if (!fscanf(fp, "%d", &n)) return(0);
1103          while (n-- > 0) fscanf(fp, "%*f");
1104          return(0);
1105   }
# Line 1109 | Line 1134 | load_scene(const char *inspec, int (*ocb)(FILE *))
1134                          continue;
1135                  }
1136                  if (c == '#') {         /* parameters/comment */
1137 <                        if (fscanf(fp, "%s", inpbuf) == 1 &&
1137 >                        if ((c = getc(fp)) == EOF || ungetc(c,fp) == EOF)
1138 >                                break;
1139 >                        if (!isspace(c) && fscanf(fp, "%s", inpbuf) == 1 &&
1140                                          !strcmp(inpbuf, PARAMSTART)) {
1141                                  if (fgets(inpbuf, sizeof(inpbuf), fp) != NULL)
1142 <                                        parse_params(inpbuf);
1142 >                                        strcat(newparams, inpbuf);
1143                                  continue;
1144                          }
1145 <                        while ((c = getc(fp)) != EOF && c != '\n');
1145 >                        while ((c = getc(fp)) != EOF && c != '\n')
1146                                  ;       /* else skipping comment */
1147                          continue;
1148                  }
# Line 1139 | Line 1166 | int
1166   main(int argc, char *argv[])
1167   {
1168          char    fmtopt[6] = "-faa";     /* default output is ASCII */
1169 +        char    *xrs=NULL, *yrs=NULL, *ldopt=NULL;
1170 +        char    *iropt = NULL;
1171          char    *sendfn;
1172          char    sampcntbuf[32], nsbinbuf[32];
1173          FILE    *rcfp;
# Line 1146 | Line 1175 | main(int argc, char *argv[])
1175          int     a, i;
1176                                          /* screen rcontrib options */
1177          progname = argv[0];
1178 <        for (a = 1; a < argc-2 && argv[a][0] == '-'; a++) {
1179 <                int     na = 1;         /* !! Keep consistent !! */
1180 <                switch (argv[a][1]) {
1178 >        for (a = 1; a < argc-2; a++) {
1179 >                int     na;
1180 >                                        /* check for argument expansion */
1181 >                while ((na = expandarg(&argc, &argv, a)) > 0)
1182 >                        ;
1183 >                if (na < 0) {
1184 >                        fprintf(stderr, "%s: cannot expand '%s'\n",
1185 >                                        progname, argv[a]);
1186 >                        return(1);
1187 >                }
1188 >                if (argv[a][0] != '-' || !argv[a][1])
1189 >                        break;
1190 >                na = 1;
1191 >                switch (argv[a][1]) {   /* !! Keep consistent !! */
1192                  case 'v':               /* verbose mode */
1193 <                        verbose = !verbose;
1193 >                        verbose = 1;
1194                          na = 0;
1195                          continue;
1196                  case 'f':               /* special case for -fo, -ff, etc. */
# Line 1163 | Line 1203 | main(int argc, char *argv[])
1203                          case 'f':
1204                          case 'd':
1205                          case 'c':
1206 <                                if (!(fmtopt[4] = argv[a][3]))
1207 <                                        fmtopt[4] = argv[a][2];
1208 <                                fmtopt[3] = argv[a][2];
1206 >                                if (!(fmtopt[3] = argv[a][3]))
1207 >                                        fmtopt[3] = argv[a][2];
1208 >                                fmtopt[2] = argv[a][2];
1209                                  na = 0;
1210                                  continue;       /* will pass later */
1211                          default:
1212                                  goto userr;
1213                          }
1214                          break;
1215 +                case 'x':               /* x-resolution */
1216 +                        xrs = argv[++a];
1217 +                        na = 0;
1218 +                        continue;
1219 +                case 'y':               /* y-resolution */
1220 +                        yrs = argv[++a];
1221 +                        na = 0;
1222 +                        continue;
1223                  case 'c':               /* number of samples */
1224 <                        sampcnt = atoi(argv[a+1]);
1224 >                        sampcnt = atoi(argv[++a]);
1225                          if (sampcnt <= 0)
1226                                  goto userr;
1227                          na = 0;         /* we re-add this later */
1228                          continue;
1229 <                case 'V':               /* options without arguments */
1182 <                case 'w':
1183 <                case 'u':
1229 >                case 'I':               /* only for pass-through mode */
1230                  case 'i':
1231 +                        iropt = argv[a];
1232 +                        na = 0;
1233 +                        continue;
1234 +                case 'w':               /* options without arguments */
1235 +                        if (argv[a][2] != '+') verbose = -1;
1236 +                case 'V':
1237 +                case 'u':
1238                  case 'h':
1239 +                case 'r':
1240                          break;
1241                  case 'n':               /* options with 1 argument */
1242                  case 's':
# Line 1193 | Line 1247 | main(int argc, char *argv[])
1247                          if (argv[a][2] != 'v') goto userr;
1248                          break;
1249                  case 'l':               /* special case */
1250 <                        if (argv[a][2] == 'd') goto userr;
1250 >                        if (argv[a][2] == 'd') {
1251 >                                ldopt = argv[a];
1252 >                                na = 0;
1253 >                                continue;
1254 >                        }
1255                          na = 2;
1256                          break;
1257                  case 'd':               /* special case */
1258                          if (argv[a][2] != 'v') na = 2;
1259                          break;
1260                  case 'a':               /* special case */
1261 <                        na = (argv[a][2] == 'v') ? 4 : 2;
1261 >                        if (argv[a][2] == 'p') {
1262 >                                na = 2; /* photon map [+ bandwidth(s)] */
1263 >                                if (a < argc-3 && atoi(argv[a+1]) > 0)
1264 >                                        na += 1 + (a < argc-4 && atoi(argv[a+2]) > 0);
1265 >                        } else
1266 >                                na = (argv[a][2] == 'v') ? 4 : 2;
1267                          break;
1268                  case 'm':               /* special case */
1269                          if (!argv[a][2]) goto userr;
1270                          na = (argv[a][2] == 'e') | (argv[a][2] == 'a') ? 4 : 2;
1271                          break;
1209                case '\0':              /* pass-through mode */
1210                        goto done_opts;
1272                  default:                /* anything else is verbotten */
1273                          goto userr;
1274                  }
# Line 1217 | Line 1278 | main(int argc, char *argv[])
1278                  while (--na)            /* + arguments if any */
1279                          rcarg[nrcargs++] = argv[++a];
1280          }
1220 done_opts:
1281          if (a > argc-2)
1282                  goto userr;             /* check at end of options */
1283          sendfn = argv[a++];             /* assign sender & receiver inputs */
1284          if (sendfn[0] == '-') {         /* user wants pass-through mode? */
1285                  if (sendfn[1]) goto userr;
1286                  sendfn = NULL;
1287 +                if (iropt) {
1288 +                        CHECKARGC(1);
1289 +                        rcarg[nrcargs++] = iropt;
1290 +                }
1291 +                if (xrs) {
1292 +                        CHECKARGC(2);
1293 +                        rcarg[nrcargs++] = "-x";
1294 +                        rcarg[nrcargs++] = xrs;
1295 +                }
1296 +                if (yrs) {
1297 +                        CHECKARGC(2);
1298 +                        rcarg[nrcargs++] = "-y";
1299 +                        rcarg[nrcargs++] = yrs;
1300 +                }
1301 +                if (ldopt) {
1302 +                        CHECKARGC(1);
1303 +                        rcarg[nrcargs++] = ldopt;
1304 +                }
1305                  if (sampcnt <= 0) sampcnt = 1;
1306 <        } else {                        /* else FVECT determines input format */
1307 <                fmtopt[3] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f';
1306 >        } else {                        /* else in sampling mode */
1307 >                if (iropt) {
1308 >                        fputs(progname, stderr);
1309 >                        fputs(": -i, -I supported for pass-through only\n", stderr);
1310 >                        return(1);
1311 >                }
1312 > #ifdef SMLFLT
1313 >                fmtopt[2] = 'f';
1314 > #else
1315 >                fmtopt[2] = 'd';
1316 > #endif
1317                  if (sampcnt <= 0) sampcnt = 10000;
1318          }
1319          sprintf(sampcntbuf, "%d", sampcnt);
# Line 1245 | Line 1332 | done_opts:
1332                  return(my_exec(rcarg)); /* rcontrib does everything */
1333          }
1334          clear_params(&curparams, 0);    /* else load sender surface & params */
1335 +        curmod[0] = '\0';
1336          if (load_scene(sendfn, add_send_object) < 0)
1337                  return(1);
1338          if ((nsbins = prepare_sampler()) <= 0)
# Line 1262 | Line 1350 | done_opts:
1350   #ifdef getc_unlocked
1351          flockfile(rcfp);
1352   #endif
1353 <        if (verbose) {
1353 >        if (verbose > 0) {
1354                  fprintf(stderr, "%s: sampling %d directions", progname, nsbins);
1355                  if (curparams.nsurfs > 1)
1356 <                        fprintf(stderr, " (%d surfaces)\n", curparams.nsurfs);
1356 >                        fprintf(stderr, " (%d elements)\n", curparams.nsurfs);
1357                  else
1358                          fputc('\n', stderr);
1359          }
1360          for (i = 0; i < nsbins; i++)    /* send rcontrib ray samples */
1361                  if (!(*curparams.sample_basis)(&curparams, i, rcfp))
1362                          return(1);
1363 <        return(pclose(rcfp) == 0);      /* all finished! */
1363 >        return(pclose(rcfp) < 0);       /* all finished! */
1364   userr:
1365          if (a < argc-2)
1366                  fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]);
1367 <        fprintf(stderr, "Usage: %s [rcontrib options] sender.rad receiver.rad [system.rad ..]\n",
1367 >        fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [-i system.oct] [system.rad ..]\n",
1368                                  progname);
1369          return(1);
1370   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines