53 |
|
char *khalffn = "klems_half.cal"; |
54 |
|
char *kquarterfn = "klems_quarter.cal"; |
55 |
|
|
56 |
< |
#define PARAMSTART "@rfluxmtx" /* string indicating parameters */ |
56 |
> |
/* string indicating parameters */ |
57 |
> |
const char PARAMSTART[] = "@rfluxmtx"; |
58 |
|
|
59 |
|
/* surface type IDs */ |
60 |
|
#define ST_NONE 0 |
96 |
|
|
97 |
|
PARAMS curparams; |
98 |
|
char curmod[128]; |
99 |
+ |
char newparams[1024]; |
100 |
|
|
101 |
|
typedef int SURFSAMP(FVECT, SURF *, double); |
102 |
|
|
110 |
|
static void |
111 |
|
clear_params(PARAMS *p, int reset_only) |
112 |
|
{ |
111 |
– |
curmod[0] = '\0'; |
113 |
|
while (p->slist != NULL) { |
114 |
|
SURF *sdel = p->slist; |
115 |
|
p->slist = sdel->next; |
124 |
|
p->outfn = NULL; |
125 |
|
return; |
126 |
|
} |
127 |
< |
memset(p, 0, sizeof(curparams)); |
127 |
> |
memset(p, 0, sizeof(PARAMS)); |
128 |
|
} |
129 |
|
|
130 |
|
/* Get surface type from name */ |
309 |
|
|
310 |
|
/* Parse program parameters (directives) */ |
311 |
|
static int |
312 |
< |
parse_params(char *pargs) |
312 |
> |
parse_params(PARAMS *p, char *pargs) |
313 |
|
{ |
314 |
|
char *cp = pargs; |
315 |
|
int nparams = 0; |
320 |
|
case 'h': |
321 |
|
if (*cp++ != '=') |
322 |
|
break; |
323 |
< |
curparams.hsiz = 0; |
323 |
> |
p->hsiz = 0; |
324 |
|
i = 0; |
325 |
|
while (*cp && !isspace(*cp)) { |
326 |
|
if (isdigit(*cp)) |
327 |
< |
curparams.hsiz = 10*curparams.hsiz + |
328 |
< |
*cp - '0'; |
328 |
< |
curparams.hemis[i++] = *cp++; |
327 |
> |
p->hsiz = 10*p->hsiz + *cp - '0'; |
328 |
> |
p->hemis[i++] = *cp++; |
329 |
|
} |
330 |
|
if (!i) |
331 |
|
break; |
332 |
< |
curparams.hemis[i] = '\0'; |
333 |
< |
curparams.hsiz += !curparams.hsiz; |
332 |
> |
p->hemis[i] = '\0'; |
333 |
> |
p->hsiz += !p->hsiz; |
334 |
|
++nparams; |
335 |
|
continue; |
336 |
|
case 'u': |
337 |
|
if (*cp++ != '=') |
338 |
|
break; |
339 |
< |
if (!get_direction(curparams.vup, cp)) |
339 |
> |
if (!get_direction(p->vup, cp)) |
340 |
|
break; |
341 |
|
++nparams; |
342 |
|
continue; |
349 |
|
if (!i) |
350 |
|
break; |
351 |
|
*--cp = '\0'; |
352 |
< |
curparams.outfn = savqstr(cp-i); |
352 |
> |
p->outfn = savqstr(cp-i); |
353 |
|
*cp++ = ' '; |
354 |
|
++nparams; |
355 |
|
continue; |
401 |
|
fputs(": undefined normal for hemisphere sampling\n", stderr); |
402 |
|
exit(1); |
403 |
|
} |
404 |
< |
if (normalize(curparams.vup) == 0) |
404 |
> |
if (normalize(curparams.vup) == 0) { |
405 |
|
if (fabs(curparams.nrm[2]) < .7) |
406 |
|
curparams.vup[2] = 1; |
407 |
|
else |
408 |
|
curparams.vup[1] = 1; |
409 |
+ |
} |
410 |
|
/* determine sample type/bin */ |
411 |
|
if (tolower(curparams.hemis[0]) == 'u' | curparams.hemis[0] == '1') { |
412 |
|
binv = "0"; /* uniform sampling -- one bin */ |
420 |
|
exit(1); |
421 |
|
} |
422 |
|
calfn = shirchiufn; shirchiufn = NULL; |
423 |
< |
sprintf(sbuf, "SCdim=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g", |
423 |
> |
sprintf(sbuf, "SCdim=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g", |
424 |
|
curparams.hsiz, |
425 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
426 |
|
curparams.vup[0], curparams.vup[1], curparams.vup[2]); |
430 |
|
} else if ((tolower(curparams.hemis[0]) == 'r') | |
431 |
|
(tolower(curparams.hemis[0]) == 't')) { |
432 |
|
calfn = reinhfn; reinhfn = NULL; |
433 |
< |
sprintf(sbuf, "MF=%d,Nx=%g,Ny=%g,Nz=%g,Ux=%g,Uy=%g,Uz=%g", |
433 |
> |
sprintf(sbuf, "MF=%d,rNx=%g,rNy=%g,rNz=%g,Ux=%g,Uy=%g,Uz=%g", |
434 |
|
curparams.hsiz, |
435 |
|
curparams.nrm[0], curparams.nrm[1], curparams.nrm[2], |
436 |
|
curparams.vup[0], curparams.vup[1], curparams.vup[2]); |
554 |
|
sp->priv = (void *)uva; |
555 |
|
} |
556 |
|
SDmultiSamp(samp2, 2, x); |
557 |
< |
samp2[0] = sp->farg[6] + sqrt(samp2[0]*sp->area*(1./PI)); |
557 |
> |
samp2[0] = sqrt(samp2[0]*sp->area*(1./PI) + sp->farg[6]*sp->farg[6]); |
558 |
|
samp2[1] *= 2.*PI; |
559 |
|
uv[0] = samp2[0]*tcos(samp2[1]); |
560 |
|
uv[1] = samp2[0]*tsin(samp2[1]); |
607 |
|
} |
608 |
|
ptp->ntris = 0; |
609 |
|
v2l->p = (void *)ptp; |
610 |
< |
if (!polyTriangulate(v2l, add_triangle)) |
610 |
> |
if (!polyTriangulate(v2l, add_triangle)) { |
611 |
> |
fprintf(stderr, |
612 |
> |
"%s: cannot triangulate polygon '%s'\n", |
613 |
> |
progname, sp->sname); |
614 |
|
return(0); |
615 |
+ |
} |
616 |
|
for (i = ptp->ntris; i--; ) { |
617 |
|
int a = ptp->tri[i].vndx[0]; |
618 |
|
int b = ptp->tri[i].vndx[1]; |
661 |
|
/* special case for lone surface */ |
662 |
|
if (p->nsurfs == 1) { |
663 |
|
sp = p->slist; |
664 |
< |
if (DOT(sp->snrm, rdir) >= -FTINY) |
665 |
< |
return(0); /* behind surface! */ |
664 |
> |
if (DOT(sp->snrm, rdir) >= -FTINY) { |
665 |
> |
fprintf(stderr, |
666 |
> |
"%s: internal - sample behind sender '%s'\n", |
667 |
> |
progname, sp->sname); |
668 |
> |
return(0); |
669 |
> |
} |
670 |
|
return((*orig_in_surf[sp->styp])(orig, sp, x)); |
671 |
|
} |
672 |
|
if (p->nsurfs > nall) { /* (re)allocate surface area cache */ |
680 |
|
projsa[i] = -DOT(sp->snrm, rdir) * sp->area; |
681 |
|
tarea += projsa[i] *= (double)(projsa[i] > FTINY); |
682 |
|
} |
683 |
< |
if (tarea <= FTINY) /* wrong side of sender? */ |
683 |
> |
if (tarea <= FTINY) { /* wrong side of sender? */ |
684 |
> |
fputs(progname, stderr); |
685 |
> |
fputs(": internal - sample behind all sender elements!\n", |
686 |
> |
stderr); |
687 |
|
return(0); |
688 |
+ |
} |
689 |
|
tarea *= x; /* get surface from list */ |
690 |
|
for (i = 0, sp = p->slist; tarea > projsa[i]; sp = sp->next) |
691 |
|
tarea -= projsa[i++]; |
758 |
|
#define T_NALT 7 |
759 |
|
static const int tnaz[T_NALT] = {30, 30, 24, 24, 18, 12, 6}; |
760 |
|
const int RowMax = T_NALT*p->hsiz + 1; |
761 |
< |
const double RAH = (.25*PI)/(RowMax-.5); |
761 |
> |
const double RAH = (.5*PI)/(RowMax-.5); |
762 |
|
#define rnaz(r) (r >= RowMax-1 ? 1 : p->hsiz*tnaz[r/p->hsiz]) |
763 |
|
int n = sampcnt; |
764 |
|
int row, col; |
783 |
|
SDmultiSamp(samp3, 3, (n+frandom())/sampcnt); |
784 |
|
alt = (row+samp3[1])*RAH; |
785 |
|
azi = (2.*PI)*(col+samp3[2]-.5)/rnaz(row); |
786 |
< |
duvw[2] = tcos(alt); /* measured from horizon */ |
786 |
> |
duvw[2] = cos(alt); /* measured from horizon */ |
787 |
|
duvw[0] = tcos(azi)*duvw[2]; |
788 |
|
duvw[1] = tsin(azi)*duvw[2]; |
789 |
|
duvw[2] = sqrt(1. - duvw[2]*duvw[2]); |
873 |
|
fputs(": undefined normal for sender sampling\n", stderr); |
874 |
|
return(-1); |
875 |
|
} |
876 |
< |
if (normalize(curparams.vup) == 0) |
876 |
> |
if (normalize(curparams.vup) == 0) { |
877 |
|
if (fabs(curparams.nrm[2]) < .7) |
878 |
|
curparams.vup[2] = 1; |
879 |
|
else |
880 |
|
curparams.vup[1] = 1; |
881 |
+ |
} |
882 |
|
VCROSS(curparams.udir, curparams.vup, curparams.nrm); |
883 |
|
if (normalize(curparams.udir) == 0) { |
884 |
|
fputs(progname, stderr); |
1002 |
|
case ST_SOURCE: |
1003 |
|
if (snew->nfargs != 4) |
1004 |
|
goto badcount; |
1005 |
< |
VCOPY(snew->snrm, snew->farg); |
1005 |
> |
for (n = 3; n--; ) /* need to reverse "normal" */ |
1006 |
> |
snew->snrm[n] = -snew->farg[n]; |
1007 |
|
if (normalize(snew->snrm) == 0) |
1008 |
|
goto badnorm; |
1009 |
|
snew->area = sin((PI/180./2.)*snew->farg[3]); |
1022 |
|
curparams.nsurfs++; |
1023 |
|
return; |
1024 |
|
badcount: |
1025 |
< |
fprintf(stderr, "%s: bad argument count for surface '%s'\n", |
1025 |
> |
fprintf(stderr, "%s: bad argument count for surface element '%s'\n", |
1026 |
|
progname, oname); |
1027 |
|
exit(1); |
1028 |
|
badnorm: |
1029 |
< |
fprintf(stderr, "%s: bad orientation for surface '%s'\n", |
1029 |
> |
fprintf(stderr, "%s: bad orientation for surface element '%s'\n", |
1030 |
|
progname, oname); |
1031 |
|
exit(1); |
1032 |
|
} |
1058 |
|
finish_receiver(); |
1059 |
|
clear_params(&curparams, 1); |
1060 |
|
} |
1061 |
+ |
parse_params(&curparams, newparams); |
1062 |
+ |
newparams[0] = '\0'; |
1063 |
|
strcpy(curmod, thismod); |
1064 |
|
} |
1065 |
|
add_surface(st, oname, fp); /* read & store surface */ |
1066 |
|
return(1); |
1067 |
|
} |
1068 |
|
/* else skip arguments */ |
1069 |
< |
if (!fscanf(fp, "%d", &n)) return; |
1069 |
> |
if (!fscanf(fp, "%d", &n)) return(0); |
1070 |
|
while (n-- > 0) fscanf(fp, "%*s"); |
1071 |
< |
if (!fscanf(fp, "%d", &n)) return; |
1071 |
> |
if (!fscanf(fp, "%d", &n)) return(0); |
1072 |
|
while (n-- > 0) fscanf(fp, "%*d"); |
1073 |
< |
if (!fscanf(fp, "%d", &n)) return; |
1073 |
> |
if (!fscanf(fp, "%d", &n)) return(0); |
1074 |
|
while (n-- > 0) fscanf(fp, "%*f"); |
1075 |
|
return(0); |
1076 |
|
} |
1096 |
|
fputs(": cannot use source as a sender!\n", stderr); |
1097 |
|
return(-1); |
1098 |
|
} |
1099 |
+ |
parse_params(&curparams, newparams); |
1100 |
+ |
newparams[0] = '\0'; |
1101 |
|
add_surface(st, oname, fp); /* read & store surface */ |
1102 |
|
return(0); |
1103 |
|
} |
1104 |
|
/* else skip arguments */ |
1105 |
< |
if (!fscanf(fp, "%d", &n)) return; |
1105 |
> |
if (!fscanf(fp, "%d", &n)) return(0); |
1106 |
|
while (n-- > 0) fscanf(fp, "%*s"); |
1107 |
< |
if (!fscanf(fp, "%d", &n)) return; |
1107 |
> |
if (!fscanf(fp, "%d", &n)) return(0); |
1108 |
|
while (n-- > 0) fscanf(fp, "%*d"); |
1109 |
< |
if (!fscanf(fp, "%d", &n)) return; |
1109 |
> |
if (!fscanf(fp, "%d", &n)) return(0); |
1110 |
|
while (n-- > 0) fscanf(fp, "%*f"); |
1111 |
|
return(0); |
1112 |
|
} |
1141 |
|
continue; |
1142 |
|
} |
1143 |
|
if (c == '#') { /* parameters/comment */ |
1144 |
< |
if (fscanf(fp, "%s", inpbuf) == 1 && |
1144 |
> |
if ((c = getc(fp)) == EOF || ungetc(c,fp) == EOF) |
1145 |
> |
break; |
1146 |
> |
if (!isspace(c) && fscanf(fp, "%s", inpbuf) == 1 && |
1147 |
|
!strcmp(inpbuf, PARAMSTART)) { |
1148 |
|
if (fgets(inpbuf, sizeof(inpbuf), fp) != NULL) |
1149 |
< |
parse_params(inpbuf); |
1149 |
> |
strcat(newparams, inpbuf); |
1150 |
|
continue; |
1151 |
|
} |
1152 |
< |
while ((c = getc(fp)) != EOF && c != '\n'); |
1152 |
> |
while ((c = getc(fp)) != EOF && c != '\n') |
1153 |
|
; /* else skipping comment */ |
1154 |
|
continue; |
1155 |
|
} |
1174 |
|
{ |
1175 |
|
char fmtopt[6] = "-faa"; /* default output is ASCII */ |
1176 |
|
char *xrs=NULL, *yrs=NULL, *ldopt=NULL; |
1177 |
+ |
int wantIrradiance = 0; |
1178 |
|
char *sendfn; |
1179 |
|
char sampcntbuf[32], nsbinbuf[32]; |
1180 |
|
FILE *rcfp; |
1199 |
|
case 'f': |
1200 |
|
case 'd': |
1201 |
|
case 'c': |
1202 |
< |
if (!(fmtopt[4] = argv[a][3])) |
1203 |
< |
fmtopt[4] = argv[a][2]; |
1204 |
< |
fmtopt[3] = argv[a][2]; |
1202 |
> |
if (!(fmtopt[3] = argv[a][3])) |
1203 |
> |
fmtopt[3] = argv[a][2]; |
1204 |
> |
fmtopt[2] = argv[a][2]; |
1205 |
|
na = 0; |
1206 |
|
continue; /* will pass later */ |
1207 |
|
default: |
1217 |
|
na = 0; |
1218 |
|
continue; |
1219 |
|
case 'c': /* number of samples */ |
1220 |
< |
sampcnt = atoi(argv[a+1]); |
1220 |
> |
sampcnt = atoi(argv[++a]); |
1221 |
|
if (sampcnt <= 0) |
1222 |
|
goto userr; |
1223 |
|
na = 0; /* we re-add this later */ |
1224 |
|
continue; |
1225 |
+ |
case 'I': /* only for pass-through mode */ |
1226 |
+ |
wantIrradiance = 1; |
1227 |
+ |
na = 0; |
1228 |
+ |
continue; |
1229 |
|
case 'V': /* options without arguments */ |
1230 |
|
case 'w': |
1231 |
|
case 'u': |
1277 |
|
if (sendfn[0] == '-') { /* user wants pass-through mode? */ |
1278 |
|
if (sendfn[1]) goto userr; |
1279 |
|
sendfn = NULL; |
1280 |
+ |
if (wantIrradiance) { |
1281 |
+ |
CHECKARGC(1); |
1282 |
+ |
rcarg[nrcargs++] = "-I"; |
1283 |
+ |
} |
1284 |
|
if (xrs) { |
1285 |
|
CHECKARGC(2); |
1286 |
|
rcarg[nrcargs++] = "-x"; |
1296 |
|
rcarg[nrcargs++] = ldopt; |
1297 |
|
} |
1298 |
|
if (sampcnt <= 0) sampcnt = 1; |
1299 |
< |
} else { /* else FVECT determines input format */ |
1300 |
< |
fmtopt[3] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f'; |
1299 |
> |
} else { /* else in sampling mode */ |
1300 |
> |
if (wantIrradiance) { |
1301 |
> |
fputs(progname, stderr); |
1302 |
> |
fputs(": -I supported for pass-through only\n", stderr); |
1303 |
> |
return(1); |
1304 |
> |
} |
1305 |
> |
fmtopt[2] = (sizeof(RREAL)==sizeof(double)) ? 'd' : 'f'; |
1306 |
|
if (sampcnt <= 0) sampcnt = 10000; |
1307 |
|
} |
1308 |
|
sprintf(sampcntbuf, "%d", sampcnt); |
1341 |
|
if (verbose) { |
1342 |
|
fprintf(stderr, "%s: sampling %d directions", progname, nsbins); |
1343 |
|
if (curparams.nsurfs > 1) |
1344 |
< |
fprintf(stderr, " (%d surfaces)\n", curparams.nsurfs); |
1344 |
> |
fprintf(stderr, " (%d elements)\n", curparams.nsurfs); |
1345 |
|
else |
1346 |
|
fputc('\n', stderr); |
1347 |
|
} |
1352 |
|
userr: |
1353 |
|
if (a < argc-2) |
1354 |
|
fprintf(stderr, "%s: unsupported option '%s'", progname, argv[a]); |
1355 |
< |
fprintf(stderr, "Usage: %s [rcontrib options] sender.rad receiver.rad [system.rad ..]\n", |
1355 |
> |
fprintf(stderr, "Usage: %s [-v][rcontrib options] sender.rad receiver.rad [system.rad ..]\n", |
1356 |
|
progname); |
1357 |
|
return(1); |
1358 |
|
} |