10 |
|
#include <ctype.h> |
11 |
|
#include "standard.h" |
12 |
|
#include "platform.h" |
13 |
+ |
#include "paths.h" |
14 |
|
#include "color.h" |
15 |
|
#include "resolu.h" |
16 |
|
#include "bsdf.h" |
17 |
+ |
#include "bsdf_m.h" |
18 |
|
|
19 |
|
char *progname; /* global argv[0] */ |
20 |
|
|
40 |
|
CMATRIX *cm; |
41 |
|
|
42 |
|
if ((nrows <= 0) | (ncols <= 0)) |
43 |
< |
return(NULL); |
43 |
> |
error(USER, "attempt to create empty matrix"); |
44 |
|
cm = (CMATRIX *)malloc(sizeof(CMATRIX) + |
45 |
|
3*sizeof(COLORV)*(nrows*ncols - 1)); |
46 |
|
if (cm == NULL) |
110 |
|
CMATRIX *cm; |
111 |
|
FILE *fp = stdin; |
112 |
|
|
113 |
+ |
if (ncols <= 0) |
114 |
+ |
error(USER, "Non-positive number of columns"); |
115 |
|
if (fname == NULL) |
116 |
|
fname = "<stdin>"; |
117 |
|
else if ((fp = fopen(fname, "r")) == NULL) { |
164 |
|
int maxrow = (nrows > 0 ? nrows : 32000); |
165 |
|
int r, c; |
166 |
|
for (r = 0; r < maxrow; r++) { |
167 |
< |
if (r >= cm->nrows) /* need more space? */ |
167 |
> |
if (r >= cm->nrows) /* need more space? */ |
168 |
|
cm = cm_resize(cm, 2*cm->nrows); |
169 |
|
for (c = 0; c < ncols; c++) { |
170 |
|
COLORV *cv = cm_lval(cm,r,c); |
171 |
|
if (fscanf(fp, COLSPEC, cv, cv+1, cv+2) != 3) |
172 |
< |
if ((nrows <= 0) & (r > 0) & (c == 0)) { |
172 |
> |
if ((nrows <= 0) & (r > 0) & !c) { |
173 |
|
cm = cm_resize(cm, maxrow=r); |
174 |
|
break; |
175 |
|
} else |
197 |
|
if (nread == cm->nrows*cm->ncols) |
198 |
|
/* need more space? */ |
199 |
|
cm = cm_resize(cm, 2*cm->nrows); |
200 |
< |
else if (nread % cm->ncols == 0) |
200 |
> |
else if (nread && !(nread % cm->ncols)) |
201 |
|
/* seem to be done */ |
202 |
|
cm = cm_resize(cm, nread/cm->ncols); |
203 |
|
else /* ended mid-row */ |
252 |
|
return(NULL); /* gratis return */ |
253 |
|
} |
254 |
|
|
255 |
< |
/* Multiply two matrices (or a matrix and a vector) and allocate the result*/ |
255 |
> |
/* Scale a matrix by a single value */ |
256 |
|
static CMATRIX * |
257 |
+ |
cm_scale(const CMATRIX *cm1, const COLOR sca) |
258 |
+ |
{ |
259 |
+ |
CMATRIX *cmr; |
260 |
+ |
int dr, dc; |
261 |
+ |
|
262 |
+ |
cmr = cm_alloc(cm1->nrows, cm1->ncols); |
263 |
+ |
if (cmr == NULL) |
264 |
+ |
return(NULL); |
265 |
+ |
for (dr = 0; dr < cmr->nrows; dr++) |
266 |
+ |
for (dc = 0; dc < cmr->ncols; dc++) { |
267 |
+ |
const COLORV *sp = cm_lval(cm1,dr,dc); |
268 |
+ |
COLORV *dp = cm_lval(cmr,dr,dc); |
269 |
+ |
dp[0] = sp[0] * sca[0]; |
270 |
+ |
dp[1] = sp[1] * sca[1]; |
271 |
+ |
dp[2] = sp[2] * sca[2]; |
272 |
+ |
} |
273 |
+ |
return(cmr); |
274 |
+ |
} |
275 |
+ |
|
276 |
+ |
/* Multiply two matrices (or a matrix and a vector) and allocate the result */ |
277 |
+ |
static CMATRIX * |
278 |
|
cm_multiply(const CMATRIX *cm1, const CMATRIX *cm2) |
279 |
|
{ |
280 |
|
CMATRIX *cmr; |
314 |
|
} |
315 |
|
} |
316 |
|
|
317 |
< |
/* convert a BSDF to our matrix representation */ |
317 |
> |
/* Convert a BSDF to our matrix representation */ |
318 |
|
static CMATRIX * |
319 |
< |
cm_bsdf(const struct BSDF_data *bsdf) |
319 |
> |
cm_bsdf(const COLOR bsdfLamb, const COLOR specCol, const SDMat *bsdf) |
320 |
|
{ |
321 |
|
CMATRIX *cm = cm_alloc(bsdf->nout, bsdf->ninc); |
322 |
< |
COLORV *mp = cm->cmem; |
322 |
> |
int nbadohm = 0; |
323 |
> |
int nneg = 0; |
324 |
|
int r, c; |
325 |
< |
|
326 |
< |
for (r = 0; r < cm->nrows; r++) |
327 |
< |
for (c = 0; c < cm->ncols; c++, mp += 3) |
328 |
< |
mp[0] = mp[1] = mp[2] = BSDF_value(bsdf,c,r); |
325 |
> |
/* reciprocity is "transparent" */ |
326 |
> |
for (c = 0; c < cm->ncols; c++) { |
327 |
> |
const double dom = mBSDF_incohm(bsdf,c); |
328 |
> |
/* projected solid angle */ |
329 |
> |
nbadohm += (dom <= 0); |
330 |
> |
|
331 |
> |
for (r = 0; r < cm->nrows; r++) { |
332 |
> |
float f = mBSDF_value(bsdf,c,r); |
333 |
> |
COLORV *mp = cm_lval(cm,r,c); |
334 |
> |
/* check BSDF value */ |
335 |
> |
if ((f <= 0) | (dom <= 0)) { |
336 |
> |
nneg += (f < -FTINY); |
337 |
> |
f = .0f; |
338 |
> |
} |
339 |
> |
copycolor(mp, specCol); |
340 |
> |
scalecolor(mp, f); |
341 |
> |
addcolor(mp, bsdfLamb); |
342 |
> |
scalecolor(mp, dom); |
343 |
> |
} |
344 |
> |
} |
345 |
> |
if (nneg | nbadohm) { |
346 |
> |
sprintf(errmsg, |
347 |
> |
"BTDF has %d negatives and %d bad incoming solid angles", |
348 |
> |
nneg, nbadohm); |
349 |
> |
error(WARNING, errmsg); |
350 |
> |
} |
351 |
|
return(cm); |
352 |
|
} |
353 |
|
|
354 |
+ |
/* Load and convert a matrix BSDF from the given XML file */ |
355 |
+ |
static CMATRIX * |
356 |
+ |
cm_loadBSDF(char *fname, COLOR cLamb) |
357 |
+ |
{ |
358 |
+ |
CMATRIX *Tmat; |
359 |
+ |
char *fpath; |
360 |
+ |
SDError ec; |
361 |
+ |
SDData myBSDF; |
362 |
+ |
SDSpectralDF *tdf; |
363 |
+ |
COLOR bsdfLamb, specCol; |
364 |
+ |
/* find path to BSDF file */ |
365 |
+ |
fpath = getpath(fname, getrlibpath(), R_OK); |
366 |
+ |
if (fpath == NULL) { |
367 |
+ |
sprintf(errmsg, "cannot find BSDF file '%s'", fname); |
368 |
+ |
error(USER, errmsg); |
369 |
+ |
} |
370 |
+ |
SDclearBSDF(&myBSDF, fname); /* load XML and check type */ |
371 |
+ |
ec = SDloadFile(&myBSDF, fpath); |
372 |
+ |
if (ec) |
373 |
+ |
error(USER, transSDError(ec)); |
374 |
+ |
ccy2rgb(&myBSDF.tLamb.spec, myBSDF.tLamb.cieY/PI, bsdfLamb); |
375 |
+ |
tdf = (myBSDF.tf != NULL) ? myBSDF.tf : myBSDF.tb; |
376 |
+ |
if (tdf == NULL) { /* no non-Lambertian transmission? */ |
377 |
+ |
if (cLamb != NULL) |
378 |
+ |
copycolor(cLamb, bsdfLamb); |
379 |
+ |
SDfreeBSDF(&myBSDF); |
380 |
+ |
return(NULL); |
381 |
+ |
} |
382 |
+ |
if (tdf->ncomp != 1 || tdf->comp[0].func != &SDhandleMtx) { |
383 |
+ |
sprintf(errmsg, "unsupported BSDF '%s'", fpath); |
384 |
+ |
error(USER, errmsg); |
385 |
+ |
} |
386 |
+ |
/* convert BTDF to matrix */ |
387 |
+ |
ccy2rgb(&tdf->comp[0].cspec[0], 1., specCol); |
388 |
+ |
Tmat = cm_bsdf(bsdfLamb, specCol, (SDMat *)tdf->comp[0].dist); |
389 |
+ |
if (cLamb != NULL) /* Lambertian is included */ |
390 |
+ |
setcolor(cLamb, .0, .0, .0); |
391 |
+ |
/* free BSDF and return */ |
392 |
+ |
SDfreeBSDF(&myBSDF); |
393 |
+ |
return(Tmat); |
394 |
+ |
} |
395 |
+ |
|
396 |
|
/* Sum together a set of images and write result to stdout */ |
397 |
|
static int |
398 |
< |
sum_images(const char *fspec, const CMATRIX *cv) |
398 |
> |
sum_images(const char *fspec, const CMATRIX *cv, FILE *fout) |
399 |
|
{ |
400 |
|
int myDT = DTfromHeader; |
401 |
|
CMATRIX *pmat; |
411 |
|
FILE *fp; |
412 |
|
int dt, xr, yr; |
413 |
|
COLORV *psp; |
414 |
+ |
/* check for zero */ |
415 |
+ |
if ((scv[RED] == 0) & (scv[GRN] == 0) & (scv[BLU] == 0) && |
416 |
+ |
(myDT != DTfromHeader) | (i < cv->nrows-1)) |
417 |
+ |
continue; |
418 |
|
/* open next picture */ |
419 |
|
sprintf(fname, fspec, i); |
420 |
|
if ((fp = fopen(fname, "r")) == NULL) { |
437 |
|
pmat = cm_alloc(myYR, myXR); |
438 |
|
memset(pmat->cmem, 0, sizeof(COLOR)*myXR*myYR); |
439 |
|
/* finish header */ |
440 |
< |
fputformat(myDT==DTrgbe ? COLRFMT : CIEFMT, stdout); |
441 |
< |
fputc('\n', stdout); |
442 |
< |
fprtresolu(myXR, myYR, stdout); |
443 |
< |
fflush(stdout); |
440 |
> |
fputformat(myDT==DTrgbe ? COLRFMT : CIEFMT, fout); |
441 |
> |
fputc('\n', fout); |
442 |
> |
fprtresolu(myXR, myYR, fout); |
443 |
> |
fflush(fout); |
444 |
|
} else if ((dt != myDT) | (xr != myXR) | (yr != myYR)) { |
445 |
|
sprintf(errmsg, "picture '%s' format/size mismatch", |
446 |
|
fname); |
465 |
|
free(scanline); |
466 |
|
/* write scanlines */ |
467 |
|
for (y = 0; y < myYR; y++) |
468 |
< |
if (fwritescan((COLOR *)cm_lval(pmat, y, 0), myXR, stdout) < 0) |
468 |
> |
if (fwritescan((COLOR *)cm_lval(pmat, y, 0), myXR, fout) < 0) |
469 |
|
return(0); |
470 |
|
cm_free(pmat); /* all done */ |
471 |
< |
return(fflush(stdout) == 0); |
471 |
> |
return(fflush(fout) == 0); |
472 |
|
} |
473 |
|
|
474 |
< |
/* check to see if a string contains a %d specification */ |
475 |
< |
int |
476 |
< |
hasDecimalSpec(const char *s) |
474 |
> |
/* check to see if a string contains a %d or %o specification */ |
475 |
> |
static int |
476 |
> |
hasNumberFormat(const char *s) |
477 |
|
{ |
478 |
< |
while (*s && *s != '%') |
479 |
< |
s++; |
480 |
< |
if (!*s) |
481 |
< |
return(0); |
482 |
< |
do |
483 |
< |
++s; |
484 |
< |
while (isdigit(*s)); |
485 |
< |
|
486 |
< |
return(*s == 'd'); |
478 |
> |
while (*s) { |
479 |
> |
while (*s != '%') |
480 |
> |
if (!*s++) |
481 |
> |
return(0); |
482 |
> |
if (*++s == '%') { /* ignore "%%" */ |
483 |
> |
++s; |
484 |
> |
continue; |
485 |
> |
} |
486 |
> |
while (isdigit(*s)) /* field length */ |
487 |
> |
++s; |
488 |
> |
/* field we'll use? */ |
489 |
> |
if ((*s == 'd') | (*s == 'i') | (*s == 'o') | |
490 |
> |
(*s == 'x') | (*s == 'X')) |
491 |
> |
return(1); |
492 |
> |
} |
493 |
> |
return(0); /* didn't find one */ |
494 |
|
} |
495 |
|
|
496 |
|
int |
497 |
|
main(int argc, char *argv[]) |
498 |
|
{ |
499 |
< |
CMATRIX *tvec, *Dmat, *Tmat, *ivec, *cvec; |
399 |
< |
struct BSDF_data *btdf; |
499 |
> |
CMATRIX *cvec; /* component vector result */ |
500 |
|
|
501 |
|
progname = argv[0]; |
502 |
|
|
503 |
< |
if ((argc < 4) | (argc > 5)) { |
504 |
< |
fprintf(stderr, "Usage: %s Vspec Tbsdf.xml Dmat.dat [tregvec]\n", |
505 |
< |
progname); |
503 |
> |
if ((argc < 2) | (argc > 5)) { |
504 |
> |
fprintf(stderr, "Usage: %s DCspec [tregvec]\n", progname); |
505 |
> |
fprintf(stderr, " or: %s Vspec Tbsdf.xml Dmat.dat [tregvec]\n", |
506 |
> |
progname); |
507 |
|
return(1); |
508 |
|
} |
509 |
< |
tvec = cm_load(argv[4], 0, 1, DTascii); /* argv[4]==NULL iff argc==4 */ |
510 |
< |
Dmat = cm_load(argv[3], 0, tvec->nrows, DTfromHeader); |
511 |
< |
btdf = load_BSDF(argv[2]); |
512 |
< |
if (btdf == NULL) |
513 |
< |
return(1); |
514 |
< |
if (btdf->ninc != Dmat->nrows) { |
515 |
< |
sprintf(errmsg, "Incoming BTDF dir (%d) mismatch to D (%d)", |
516 |
< |
btdf->ninc, Dmat->nrows); |
517 |
< |
error(USER, errmsg); |
518 |
< |
} |
509 |
> |
|
510 |
> |
if (argc > 3) { /* VTDs expression */ |
511 |
> |
CMATRIX *svec, *Dmat, *Tmat, *ivec; |
512 |
> |
COLOR tLamb; |
513 |
> |
/* get sky vector */ |
514 |
> |
svec = cm_load(argv[4], 0, 1, DTascii); |
515 |
> |
/* load BSDF */ |
516 |
> |
Tmat = cm_loadBSDF(argv[2], tLamb); |
517 |
> |
/* load Daylight matrix */ |
518 |
> |
Dmat = cm_load(argv[3], Tmat==NULL ? 0 : Tmat->ncols, |
519 |
> |
svec->nrows, DTfromHeader); |
520 |
|
/* multiply vector through */ |
521 |
< |
ivec = cm_multiply(Dmat, tvec); |
522 |
< |
cm_free(Dmat); cm_free(tvec); |
523 |
< |
Tmat = cm_bsdf(btdf); /* convert BTDF to matrix */ |
524 |
< |
free_BSDF(btdf); |
525 |
< |
cvec = cm_multiply(Tmat, ivec); /* cvec = component vector */ |
526 |
< |
cm_free(Tmat); cm_free(ivec); |
527 |
< |
if (hasDecimalSpec(argv[1])) { /* generating image */ |
521 |
> |
ivec = cm_multiply(Dmat, svec); |
522 |
> |
cm_free(Dmat); cm_free(svec); |
523 |
> |
if (Tmat == NULL) { /* diffuse only */ |
524 |
> |
cvec = cm_scale(ivec, tLamb); |
525 |
> |
} else { /* else apply BTDF matrix */ |
526 |
> |
cvec = cm_multiply(Tmat, ivec); |
527 |
> |
cm_free(Tmat); |
528 |
> |
} |
529 |
> |
cm_free(ivec); |
530 |
> |
} else { /* else just use sky vector */ |
531 |
> |
cvec = cm_load(argv[2], 0, 1, DTascii); |
532 |
> |
} |
533 |
> |
|
534 |
> |
if (hasNumberFormat(argv[1])) { /* generating image */ |
535 |
|
SET_FILE_BINARY(stdout); |
536 |
|
newheader("RADIANCE", stdout); |
537 |
|
printargs(argc, argv, stdout); |
538 |
|
fputnow(stdout); |
539 |
< |
if (!sum_images(argv[1], cvec)) |
539 |
> |
if (!sum_images(argv[1], cvec, stdout)) |
540 |
|
return(1); |
541 |
|
} else { /* generating vector */ |
542 |
|
CMATRIX *Vmat = cm_load(argv[1], 0, cvec->nrows, DTfromHeader); |