1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: bsdf2ttree.c,v 2.1 2012/10/19 04:14:29 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Load measured BSDF interpolant and write out as XML file with tensor tree. |
6 |
* |
7 |
* G. Ward |
8 |
*/ |
9 |
|
10 |
#define _USE_MATH_DEFINES |
11 |
#include <stdio.h> |
12 |
#include <stdlib.h> |
13 |
#include <math.h> |
14 |
#include "platform.h" |
15 |
#include "bsdfrep.h" |
16 |
/* global argv[0] */ |
17 |
char *progname; |
18 |
/* percentage to cull (<0 to turn off) */ |
19 |
int pctcull = 90; |
20 |
/* sampling order */ |
21 |
int samp_order = 6; |
22 |
|
23 |
/* Interpolate and output isotropic BSDF data */ |
24 |
static void |
25 |
interp_isotropic() |
26 |
{ |
27 |
const int sqres = 1<<samp_order; |
28 |
FILE *ofp = NULL; |
29 |
char cmd[128]; |
30 |
int ix, ox, oy; |
31 |
FVECT ivec, ovec; |
32 |
double bsdf; |
33 |
#if DEBUG |
34 |
fprintf(stderr, "Writing isotropic order %d ", samp_order); |
35 |
if (pctcull >= 0) fprintf(stderr, "data with %d%% culling\n", pctcull); |
36 |
else fputs("raw data\n", stderr); |
37 |
#endif |
38 |
if (pctcull >= 0) { /* begin output */ |
39 |
sprintf(cmd, "rttree_reduce -h -a -fd -r 3 -t %d -g %d", |
40 |
pctcull, samp_order); |
41 |
fflush(stdout); |
42 |
ofp = popen(cmd, "w"); |
43 |
if (ofp == NULL) { |
44 |
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
45 |
progname); |
46 |
exit(1); |
47 |
} |
48 |
SET_FILE_BINARY(ofp); |
49 |
} else |
50 |
fputs("{\n", stdout); |
51 |
/* run through directions */ |
52 |
for (ix = 0; ix < sqres/2; ix++) { |
53 |
RBFNODE *rbf; |
54 |
SDsquare2disk(ivec, (ix+.5)/sqres, .5); |
55 |
ivec[2] = input_orient * |
56 |
sqrt(1. - ivec[0]*ivec[0] - ivec[1]*ivec[1]); |
57 |
rbf = advect_rbf(ivec); |
58 |
for (ox = 0; ox < sqres; ox++) |
59 |
for (oy = 0; oy < sqres; oy++) { |
60 |
SDsquare2disk(ovec, (ox+.5)/sqres, (oy+.5)/sqres); |
61 |
ovec[2] = output_orient * |
62 |
sqrt(1. - ovec[0]*ovec[0] - ovec[1]*ovec[1]); |
63 |
bsdf = eval_rbfrep(rbf, ovec) / fabs(ovec[2]); |
64 |
if (pctcull >= 0) |
65 |
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
66 |
else |
67 |
printf("\t%.3e\n", bsdf); |
68 |
} |
69 |
free(rbf); |
70 |
} |
71 |
if (pctcull >= 0) { /* finish output */ |
72 |
if (pclose(ofp)) { |
73 |
fprintf(stderr, "%s: error running '%s'\n", |
74 |
progname, cmd); |
75 |
exit(1); |
76 |
} |
77 |
} else { |
78 |
for (ix = sqres*sqres*sqres/2; ix--; ) |
79 |
fputs("\t0\n", stdout); |
80 |
fputs("}\n", stdout); |
81 |
} |
82 |
} |
83 |
|
84 |
/* Interpolate and output anisotropic BSDF data */ |
85 |
static void |
86 |
interp_anisotropic() |
87 |
{ |
88 |
const int sqres = 1<<samp_order; |
89 |
FILE *ofp = NULL; |
90 |
char cmd[128]; |
91 |
int ix, iy, ox, oy; |
92 |
FVECT ivec, ovec; |
93 |
double bsdf; |
94 |
#if DEBUG |
95 |
fprintf(stderr, "Writing anisotropic order %d ", samp_order); |
96 |
if (pctcull >= 0) fprintf(stderr, "data with %d%% culling\n", pctcull); |
97 |
else fputs("raw data\n", stderr); |
98 |
#endif |
99 |
if (pctcull >= 0) { /* begin output */ |
100 |
sprintf(cmd, "rttree_reduce -h -a -fd -r 4 -t %d -g %d", |
101 |
pctcull, samp_order); |
102 |
fflush(stdout); |
103 |
ofp = popen(cmd, "w"); |
104 |
if (ofp == NULL) { |
105 |
fprintf(stderr, "%s: cannot create pipe to rttree_reduce\n", |
106 |
progname); |
107 |
exit(1); |
108 |
} |
109 |
} else |
110 |
fputs("{\n", stdout); |
111 |
/* run through directions */ |
112 |
for (ix = 0; ix < sqres; ix++) |
113 |
for (iy = 0; iy < sqres; iy++) { |
114 |
RBFNODE *rbf; |
115 |
SDsquare2disk(ivec, (ix+.5)/sqres, (iy+.5)/sqres); |
116 |
ivec[2] = input_orient * |
117 |
sqrt(1. - ivec[0]*ivec[0] - ivec[1]*ivec[1]); |
118 |
rbf = advect_rbf(ivec); |
119 |
for (ox = 0; ox < sqres; ox++) |
120 |
for (oy = 0; oy < sqres; oy++) { |
121 |
SDsquare2disk(ovec, (ox+.5)/sqres, (oy+.5)/sqres); |
122 |
ovec[2] = output_orient * |
123 |
sqrt(1. - ovec[0]*ovec[0] - ovec[1]*ovec[1]); |
124 |
bsdf = eval_rbfrep(rbf, ovec) / fabs(ovec[2]); |
125 |
if (pctcull >= 0) |
126 |
fwrite(&bsdf, sizeof(bsdf), 1, ofp); |
127 |
else |
128 |
printf("\t%.3e\n", bsdf); |
129 |
} |
130 |
free(rbf); |
131 |
} |
132 |
if (pctcull >= 0) { /* finish output */ |
133 |
if (pclose(ofp)) { |
134 |
fprintf(stderr, "%s: error running '%s'\n", |
135 |
progname, cmd); |
136 |
exit(1); |
137 |
} |
138 |
} else |
139 |
fputs("}\n", stdout); |
140 |
} |
141 |
|
142 |
/* Read in BSDF and interpolate as tensor tree representation */ |
143 |
int |
144 |
main(int argc, char *argv[]) |
145 |
{ |
146 |
FILE *fpin = stdin; |
147 |
int i; |
148 |
|
149 |
progname = argv[0]; /* get options */ |
150 |
while (argc > 2 && argv[1][0] == '-') { |
151 |
switch (argv[1][1]) { |
152 |
case 't': |
153 |
pctcull = atoi(argv[2]); |
154 |
break; |
155 |
case 'g': |
156 |
samp_order = atoi(argv[2]); |
157 |
break; |
158 |
default: |
159 |
goto userr; |
160 |
} |
161 |
argv += 2; argc -= 2; |
162 |
} |
163 |
if (argc == 2) { /* open input if given */ |
164 |
fpin = fopen(argv[1], "r"); |
165 |
if (fpin == NULL) { |
166 |
fprintf(stderr, "%s: cannot open BSDF interpolant '%s'\n", |
167 |
progname, argv[1]); |
168 |
return(1); |
169 |
} |
170 |
} else if (argc != 1) |
171 |
goto userr; |
172 |
SET_FILE_BINARY(fpin); /* load BSDF interpolant */ |
173 |
if (!load_bsdf_rep(fpin)) |
174 |
return(1); |
175 |
draw_edges(); |
176 |
/* xml_prologue(); /* start XML output */ |
177 |
if (single_plane_incident) /* resample dist. */ |
178 |
interp_isotropic(); |
179 |
else |
180 |
interp_anisotropic(); |
181 |
/* xml_epilogue(); /* finish XML output */ |
182 |
return(0); |
183 |
userr: |
184 |
fprintf(stderr, |
185 |
"Usage: %s [-t pctcull][-g log2grid] [bsdf.sir] > bsdf.xml\n", |
186 |
progname); |
187 |
return(1); |
188 |
} |