4 |
|
|
5 |
|
/* |
6 |
|
====================================================================== |
7 |
< |
Dump photon maps as RADIANCE scene description to stdout |
7 |
> |
Dump photon maps as RADIANCE scene description or ASCII point list |
8 |
> |
to stdout |
9 |
|
|
10 |
|
Roland Schregle (roland.schregle@{hslu.ch, gmail.com}) |
11 |
|
(c) Fraunhofer Institute for Solar Energy Systems, |
12 |
+ |
supported by the German Research Foundation (DFG) |
13 |
+ |
under the FARESYS project. |
14 |
|
(c) Lucerne University of Applied Sciences and Arts, |
15 |
< |
supported by the Swiss National Science Foundation (SNSF, #147053) |
15 |
> |
supported by the Swiss National Science Foundation (SNSF #147053). |
16 |
> |
(c) Tokyo University of Science, |
17 |
> |
supported by the JSPS KAKENHI Grant Number JP19KK0115. |
18 |
|
====================================================================== |
19 |
|
|
20 |
|
$Id$ |
22 |
|
|
23 |
|
|
24 |
|
|
25 |
+ |
#include "pmap.h" |
26 |
|
#include "pmapio.h" |
21 |
– |
#include "pmapparm.h" |
22 |
– |
#include "pmaptype.h" |
27 |
|
#include "rtio.h" |
28 |
|
#include "resolu.h" |
29 |
|
#include "random.h" |
38 |
|
#define RADSCALE 1.0 |
39 |
|
#define NSPHERES 10000 |
40 |
|
|
41 |
+ |
/* Format for optional ASCII output as XYZ RGB points */ |
42 |
+ |
#define POINTFMT "%g\t%g\t%g\t%g\t%g\t%g\n" |
43 |
|
|
44 |
|
/* RADIANCE material and object defs for each photon type */ |
45 |
|
typedef struct { |
46 |
|
char *mat, *obj; |
47 |
|
} RadianceDef; |
48 |
|
|
43 |
– |
|
44 |
– |
/* We use %e for the material def to preserve precision when outputting |
45 |
– |
photon flux */ |
49 |
|
const RadianceDef radDefs [] = { |
50 |
< |
{ "void glow mat.global\n0\n0\n4 %e %e %e 0\n", |
50 |
> |
{ "void glow mat.global\n0\n0\n4 %g %g %g 0\n", |
51 |
|
"mat.global sphere obj.global\n0\n0\n4 %g %g %g %g\n" |
52 |
|
}, |
53 |
< |
{ "void glow mat.pglobal\n0\n0\n4 %e %e %e 0\n", |
53 |
> |
{ "void glow mat.pglobal\n0\n0\n4 %g %g %g 0\n", |
54 |
|
"mat.pglobal sphere obj.pglobal\n0\n0\n4 %g %g %g %g\n" |
55 |
|
}, |
56 |
< |
{ "void glow mat.caustic\n0\n0\n4 %e %e %e 0\n", |
56 |
> |
{ "void glow mat.caustic\n0\n0\n4 %g %g %g 0\n", |
57 |
|
"mat.caustic sphere obj.caustic\n0\n0\n4 %g %g %g %g\n" |
58 |
|
}, |
59 |
< |
{ "void glow mat.volume\n0\n0\n4 %e %e %e 0\n", |
59 |
> |
{ "void glow mat.volume\n0\n0\n4 %g %g %g 0\n", |
60 |
|
"mat.volume sphere obj.volume\n0\n0\n4 %g %g %g %g\n" |
61 |
|
}, |
62 |
< |
{ "void glow mat.direct\n0\n0\n4 %e %e %e 0\n", |
62 |
> |
{ "void glow mat.direct\n0\n0\n4 %g %g %g 0\n", |
63 |
|
"mat.direct sphere obj.direct\n0\n0\n4 %g %g %g %g\n" |
64 |
|
}, |
65 |
< |
{ "void glow mat.contrib\n0\n0\n4 %e %e %e 0\n", |
65 |
> |
{ "void glow mat.contrib\n0\n0\n4 %g %g %g 0\n", |
66 |
|
"mat.contrib sphere obj.contrib\n0\n0\n4 %g %g %g %g\n" |
67 |
|
} |
68 |
|
}; |
69 |
|
|
70 |
+ |
|
71 |
|
/* Default colour codes are as follows: global = blue |
72 |
|
precomp global = cyan |
73 |
|
caustic = red |
80 |
|
}; |
81 |
|
|
82 |
|
|
83 |
+ |
static int setBool(char *str, unsigned pos, unsigned *var) |
84 |
+ |
{ |
85 |
+ |
switch ((str) [pos]) { |
86 |
+ |
case '\0': |
87 |
+ |
*var = !*var; |
88 |
+ |
break; |
89 |
+ |
case 'y': case 'Y': case 't': case 'T': case '+': case '1': |
90 |
+ |
*var = 1; |
91 |
+ |
break; |
92 |
+ |
case 'n': case 'N': case 'f': case 'F': case '-': case '0': |
93 |
+ |
*var = 0; |
94 |
+ |
break; |
95 |
+ |
default: |
96 |
+ |
return 0; |
97 |
+ |
} |
98 |
+ |
|
99 |
+ |
return 1; |
100 |
+ |
} |
101 |
+ |
|
102 |
+ |
|
103 |
|
int main (int argc, char** argv) |
104 |
|
{ |
105 |
|
char format [MAXFMTLEN]; |
106 |
|
RREAL rad, radScale = RADSCALE, extent, dumpRatio; |
107 |
< |
unsigned arg, j, ptype, dim, fluxCol = 0; |
107 |
> |
unsigned arg, j, ptype, dim, fluxCol = 0, points = 0; |
108 |
|
long numSpheres = NSPHERES; |
109 |
< |
COLOR customCol = {0, 0, 0}; |
109 |
> |
COLOR col = {0, 0, 0}; |
110 |
|
FILE *pmapFile; |
111 |
|
PhotonMap pm; |
112 |
|
PhotonPrimary pri; |
115 |
|
char leafFname [1024]; |
116 |
|
#endif |
117 |
|
|
94 |
– |
int setBool(char *str, int pos, int *var) |
95 |
– |
{ |
96 |
– |
switch ((str) [pos]) { |
97 |
– |
case '\0': |
98 |
– |
*var = !*var; |
99 |
– |
break; |
100 |
– |
case 'y': case 'Y': case 't': case 'T': case '+': case '1': |
101 |
– |
*var = 1; |
102 |
– |
break; |
103 |
– |
case 'n': case 'N': case 'f': case 'F': case '-': case '0': |
104 |
– |
*var = 0; |
105 |
– |
break; |
106 |
– |
default: |
107 |
– |
return 0; |
108 |
– |
} |
109 |
– |
|
110 |
– |
return 1; |
111 |
– |
} |
112 |
– |
|
118 |
|
if (argc < 2) { |
119 |
< |
puts("Dump photon maps as RADIANCE scene description\n"); |
119 |
> |
puts("Dump photon maps as RADIANCE scene description " |
120 |
> |
"or ASCII point list\n"); |
121 |
|
printf("Usage: %s " |
122 |
< |
"[-r radscale1] [-n nspheres1] [-f | -c rcol1 gcol1 bcol1] pmap1 " |
123 |
< |
"[-r radscale2] [-n nspheres2] [-f | -c rcol2 gcol2 bcol2] pmap2 " |
122 |
> |
"[-a] [-r radscale1] [-n num1] " |
123 |
> |
"[-f | -c rcol1 gcol1 bcol1] pmap1 " |
124 |
> |
"[-a] [-r radscale2] [-n num2] " |
125 |
> |
"[-f | -c rcol2 gcol2 bcol2] pmap2 " |
126 |
|
"...\n", argv [0]); |
127 |
|
return 1; |
128 |
|
} |
131 |
|
/* Parse options */ |
132 |
|
if (argv [arg][0] == '-') { |
133 |
|
switch (argv [arg][1]) { |
134 |
+ |
case 'a': |
135 |
+ |
if (!setBool(argv [arg], 2, &points)) |
136 |
+ |
error(USER, "invalid option syntax at -a"); |
137 |
+ |
break; |
138 |
|
case 'r': |
139 |
|
if ((radScale = atof(argv [++arg])) <= 0) |
140 |
|
error(USER, "invalid radius scale"); |
142 |
|
|
143 |
|
case 'n': |
144 |
|
if ((numSpheres = parseMultiplier(argv [++arg])) <= 0) |
145 |
< |
error(USER, "invalid number of spheres"); |
145 |
> |
error(USER, "invalid number of points/spheres"); |
146 |
|
break; |
147 |
|
|
148 |
|
case 'c': |
153 |
|
error(USER, "invalid RGB colour"); |
154 |
|
|
155 |
|
for (j = 0; j < 3; j++) |
156 |
< |
customCol [j] = atof(argv [++arg]); |
156 |
> |
col [j] = atof(argv [++arg]); |
157 |
|
break; |
158 |
|
|
159 |
|
case 'f': |
160 |
< |
if (intens(customCol) > 0) |
160 |
> |
if (intens(col) > 0) |
161 |
|
error(USER, "-f and -c are mutually exclusive"); |
162 |
|
|
163 |
|
if (!setBool(argv [arg], 2, &fluxCol)) |
173 |
|
continue; |
174 |
|
} |
175 |
|
|
176 |
< |
/* Dump photon map */ |
176 |
> |
/* Open next photon map file */ |
177 |
|
if (!(pmapFile = fopen(argv [arg], "rb"))) { |
178 |
|
sprintf(errmsg, "can't open %s", argv [arg]); |
179 |
|
error(SYSTEM, errmsg); |
202 |
|
if (strcmp(getstr(format, pmapFile), PMAP_FILEVER)) |
203 |
|
error(USER, "incompatible photon map file format"); |
204 |
|
|
205 |
< |
/* Dump command line as comment */ |
206 |
< |
fputs("# ", stdout); |
207 |
< |
printargs(argc, argv, stdout); |
208 |
< |
fputc('\n', stdout); |
197 |
< |
|
198 |
< |
/* Dump common material def if constant for all photons, |
199 |
< |
i.e. independent of individual flux */ |
200 |
< |
if (!fluxCol) { |
201 |
< |
if (intens(customCol) > 0) |
202 |
< |
printf(radDefs [ptype].mat, |
203 |
< |
customCol [0], customCol [1], customCol [2]); |
204 |
< |
else |
205 |
< |
printf(radDefs [ptype].mat, colDefs [ptype][0], |
206 |
< |
colDefs [ptype][1], colDefs [ptype][2]); |
205 |
> |
if (!points) { |
206 |
> |
/* Dump command line as comment */ |
207 |
> |
fputs("# ", stdout); |
208 |
> |
printargs(argc, argv, stdout); |
209 |
|
fputc('\n', stdout); |
210 |
|
} |
211 |
+ |
|
212 |
+ |
/* Set point/sphere colour if independent of photon flux, |
213 |
+ |
output RADIANCE material def if required */ |
214 |
+ |
if (!fluxCol) { |
215 |
+ |
if (intens(col) <= 0) |
216 |
+ |
copycolor(col, colDefs [ptype]); |
217 |
+ |
if (!points) { |
218 |
+ |
printf(radDefs [ptype].mat, col [0], col [1], col [2]); |
219 |
+ |
fputc('\n', stdout); |
220 |
+ |
} |
221 |
+ |
} |
222 |
|
|
223 |
|
/* Get number of photons */ |
224 |
|
pm.numPhotons = getint(sizeof(pm.numPhotons), pmapFile); |
253 |
|
rad = radScale * RADCOEFF * pow(extent / numSpheres, 1./dim); |
254 |
|
|
255 |
|
/* Photon dump probability to satisfy target sphere count */ |
256 |
< |
dumpRatio = numSpheres < pm.numPhotons |
244 |
< |
? (float)numSpheres / pm.numPhotons : 1; |
256 |
> |
dumpRatio = min(1, (float)numSpheres / pm.numPhotons); |
257 |
|
|
258 |
|
/* Skip primary rays */ |
259 |
|
pm.numPrimary = getint(sizeof(pm.numPrimary), pmapFile); |
274 |
|
#ifdef PMAP_OOC |
275 |
|
/* Open leaf file with filename derived from pmap, replace pmapFile |
276 |
|
* (which is currently the node file) */ |
277 |
< |
strncpy(leafFname, argv [arg], 1024); |
278 |
< |
strncat(leafFname, PMAP_OOC_LEAFSUFFIX, 1024); |
277 |
> |
strncpy(leafFname, argv [arg], sizeof(leafFname) - 1); |
278 |
> |
strncat(leafFname, PMAP_OOC_LEAFSUFFIX, sizeof(leafFname) - 1); |
279 |
|
fclose(pmapFile); |
280 |
|
if (!(pmapFile = fopen(leafFname, "rb"))) { |
281 |
|
sprintf(errmsg, "cannot open leaf file %s", leafFname); |
283 |
|
} |
284 |
|
#endif |
285 |
|
|
286 |
< |
/* Load photons */ |
286 |
> |
/* Read photons */ |
287 |
|
while (pm.numPhotons-- > 0) { |
288 |
|
#ifdef PMAP_OOC |
289 |
|
/* Get entire photon record from ooC octree leaf file |
309 |
|
for (j = 0; j < 4; j++) |
310 |
|
p.flux [j] = getint(1, pmapFile); |
311 |
|
#endif |
312 |
+ |
|
313 |
+ |
|
314 |
|
|
315 |
|
/* Skip primary ray index */ |
316 |
|
getint(sizeof(p.primary), pmapFile); |
322 |
|
/* Dump photon probabilistically acc. to target sphere count */ |
323 |
|
if (frandom() <= dumpRatio) { |
324 |
|
if (fluxCol) { |
325 |
< |
/* Dump individual material def per photon acc. to flux */ |
326 |
< |
getPhotonFlux(&p, customCol); |
327 |
< |
printf(radDefs [ptype].mat, |
328 |
< |
customCol [0], customCol [1], customCol [2]); |
315 |
< |
fputc('\n', stdout); |
325 |
> |
/* Get photon flux */ |
326 |
> |
getPhotonFlux(&p, col); |
327 |
> |
/* Scale by dumpRatio for energy conservation */ |
328 |
> |
scalecolor(col, 1.0 / dumpRatio); |
329 |
|
} |
330 |
|
|
331 |
< |
printf(radDefs [ptype].obj, p.pos [0], p.pos [1], p.pos [2], rad); |
332 |
< |
fputc('\n', stdout); |
331 |
> |
if (!points) { |
332 |
> |
if (fluxCol) { |
333 |
> |
/* Dump material def if variable (depends on flux) */ |
334 |
> |
printf(radDefs [ptype].mat, col [0], col [1], col [2]); |
335 |
> |
fputc('\n', stdout); |
336 |
> |
} |
337 |
> |
printf(radDefs [ptype].obj, p.pos [0], p.pos [1], p.pos [2], |
338 |
> |
rad); |
339 |
> |
fputc('\n', stdout); |
340 |
> |
} |
341 |
> |
else /* Dump as XYZ RGB point */ |
342 |
> |
printf(POINTFMT, p.pos [0], p.pos [1], p.pos [2], |
343 |
> |
col [0], col [1] ,col [2]); |
344 |
|
} |
345 |
< |
|
345 |
> |
|
346 |
|
if (ferror(pmapFile) || feof(pmapFile)) { |
347 |
|
sprintf(errmsg, "error reading %s", argv [arg]); |
348 |
|
error(USER, errmsg); |
354 |
|
/* Reset defaults for next dump */ |
355 |
|
radScale = RADSCALE; |
356 |
|
numSpheres = NSPHERES; |
357 |
< |
customCol [0] = customCol [1] = customCol [2] = 0; |
358 |
< |
fluxCol = 0; |
357 |
> |
col [0] = col [1] = col [2] = 0; |
358 |
> |
fluxCol = points = 0; |
359 |
|
} |
360 |
|
|
361 |
|
return 0; |