1 |
greg |
2.4 |
#ifndef lint |
2 |
rschregle |
2.18 |
static const char RCSid[] = "$Id: pmapdump.c,v 2.17 2020/08/07 01:21:13 rschregle Exp $"; |
3 |
greg |
2.4 |
#endif |
4 |
rschregle |
2.7 |
|
5 |
greg |
2.1 |
/* |
6 |
rschregle |
2.7 |
====================================================================== |
7 |
rschregle |
2.14 |
Dump photon maps as RADIANCE scene description or ASCII point list |
8 |
|
|
to stdout |
9 |
greg |
2.1 |
|
10 |
|
|
Roland Schregle (roland.schregle@{hslu.ch, gmail.com}) |
11 |
|
|
(c) Fraunhofer Institute for Solar Energy Systems, |
12 |
rschregle |
2.18 |
supported by the German Research Foundation |
13 |
|
|
(DFG LU-204/10-2, "Fassadenintegrierte Regelsysteme FARESYS") |
14 |
rschregle |
2.3 |
(c) Lucerne University of Applied Sciences and Arts, |
15 |
rschregle |
2.18 |
supported by the Swiss National Science Foundation |
16 |
|
|
(SNSF #147053, "Daylight Redirecting Components") |
17 |
rschregle |
2.16 |
(c) Tokyo University of Science, |
18 |
rschregle |
2.18 |
supported by the JSPS Grants-in-Aid for Scientific Research |
19 |
|
|
(KAKENHI JP19KK0115, "Three-Dimensional Light Flow") |
20 |
rschregle |
2.7 |
====================================================================== |
21 |
greg |
2.1 |
|
22 |
rschregle |
2.18 |
$Id: pmapdump.c,v 2.17 2020/08/07 01:21:13 rschregle Exp $ |
23 |
greg |
2.1 |
*/ |
24 |
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
rschregle |
2.14 |
#include "pmap.h" |
28 |
greg |
2.1 |
#include "pmapio.h" |
29 |
|
|
#include "rtio.h" |
30 |
|
|
#include "resolu.h" |
31 |
|
|
#include "random.h" |
32 |
greg |
2.2 |
#include "math.h" |
33 |
greg |
2.1 |
|
34 |
|
|
|
35 |
|
|
/* Defaults */ |
36 |
|
|
/* Sphere radius as fraction of avg. intersphere dist */ |
37 |
|
|
/* Relative scale for sphere radius (fudge factor) */ |
38 |
|
|
/* Number of spheres */ |
39 |
|
|
#define RADCOEFF 0.05 |
40 |
|
|
#define RADSCALE 1.0 |
41 |
|
|
#define NSPHERES 10000 |
42 |
|
|
|
43 |
rschregle |
2.14 |
/* Format for optional ASCII output as XYZ RGB points */ |
44 |
|
|
#define POINTFMT "%g\t%g\t%g\t%g\t%g\t%g\n" |
45 |
greg |
2.1 |
|
46 |
|
|
/* RADIANCE material and object defs for each photon type */ |
47 |
|
|
typedef struct { |
48 |
|
|
char *mat, *obj; |
49 |
|
|
} RadianceDef; |
50 |
|
|
|
51 |
|
|
const RadianceDef radDefs [] = { |
52 |
rschregle |
2.14 |
{ "void glow mat.global\n0\n0\n4 %g %g %g 0\n", |
53 |
greg |
2.1 |
"mat.global sphere obj.global\n0\n0\n4 %g %g %g %g\n" |
54 |
|
|
}, |
55 |
rschregle |
2.14 |
{ "void glow mat.pglobal\n0\n0\n4 %g %g %g 0\n", |
56 |
rschregle |
2.13 |
"mat.pglobal sphere obj.pglobal\n0\n0\n4 %g %g %g %g\n" |
57 |
greg |
2.1 |
}, |
58 |
rschregle |
2.14 |
{ "void glow mat.caustic\n0\n0\n4 %g %g %g 0\n", |
59 |
greg |
2.1 |
"mat.caustic sphere obj.caustic\n0\n0\n4 %g %g %g %g\n" |
60 |
|
|
}, |
61 |
rschregle |
2.14 |
{ "void glow mat.volume\n0\n0\n4 %g %g %g 0\n", |
62 |
greg |
2.1 |
"mat.volume sphere obj.volume\n0\n0\n4 %g %g %g %g\n" |
63 |
|
|
}, |
64 |
rschregle |
2.14 |
{ "void glow mat.direct\n0\n0\n4 %g %g %g 0\n", |
65 |
greg |
2.1 |
"mat.direct sphere obj.direct\n0\n0\n4 %g %g %g %g\n" |
66 |
|
|
}, |
67 |
rschregle |
2.14 |
{ "void glow mat.contrib\n0\n0\n4 %g %g %g 0\n", |
68 |
greg |
2.1 |
"mat.contrib sphere obj.contrib\n0\n0\n4 %g %g %g %g\n" |
69 |
|
|
} |
70 |
|
|
}; |
71 |
|
|
|
72 |
rschregle |
2.14 |
|
73 |
rschregle |
2.12 |
/* Default colour codes are as follows: global = blue |
74 |
|
|
precomp global = cyan |
75 |
|
|
caustic = red |
76 |
|
|
volume = green |
77 |
|
|
direct = magenta |
78 |
|
|
contrib = yellow */ |
79 |
|
|
const COLOR colDefs [] = { |
80 |
rschregle |
2.13 |
{0.25, 0.25, 2}, {0.1, 1, 1}, {1, 0.1, 0.1}, |
81 |
|
|
{0.1, 1, 0.1}, {1, 0.1, 1}, {1, 1, 0.1} |
82 |
rschregle |
2.12 |
}; |
83 |
greg |
2.1 |
|
84 |
|
|
|
85 |
rschregle |
2.14 |
static int setBool(char *str, unsigned pos, unsigned *var) |
86 |
|
|
{ |
87 |
|
|
switch ((str) [pos]) { |
88 |
|
|
case '\0': |
89 |
|
|
*var = !*var; |
90 |
|
|
break; |
91 |
|
|
case 'y': case 'Y': case 't': case 'T': case '+': case '1': |
92 |
|
|
*var = 1; |
93 |
|
|
break; |
94 |
|
|
case 'n': case 'N': case 'f': case 'F': case '-': case '0': |
95 |
|
|
*var = 0; |
96 |
|
|
break; |
97 |
|
|
default: |
98 |
|
|
return 0; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
return 1; |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
|
105 |
greg |
2.1 |
int main (int argc, char** argv) |
106 |
|
|
{ |
107 |
greg |
2.11 |
char format [MAXFMTLEN]; |
108 |
rschregle |
2.9 |
RREAL rad, radScale = RADSCALE, extent, dumpRatio; |
109 |
rschregle |
2.14 |
unsigned arg, j, ptype, dim, fluxCol = 0, points = 0; |
110 |
rschregle |
2.7 |
long numSpheres = NSPHERES; |
111 |
rschregle |
2.14 |
COLOR col = {0, 0, 0}; |
112 |
rschregle |
2.7 |
FILE *pmapFile; |
113 |
|
|
PhotonMap pm; |
114 |
|
|
PhotonPrimary pri; |
115 |
|
|
Photon p; |
116 |
|
|
#ifdef PMAP_OOC |
117 |
|
|
char leafFname [1024]; |
118 |
|
|
#endif |
119 |
rschregle |
2.13 |
|
120 |
greg |
2.1 |
if (argc < 2) { |
121 |
rschregle |
2.14 |
puts("Dump photon maps as RADIANCE scene description " |
122 |
|
|
"or ASCII point list\n"); |
123 |
rschregle |
2.12 |
printf("Usage: %s " |
124 |
rschregle |
2.14 |
"[-a] [-r radscale1] [-n num1] " |
125 |
|
|
"[-f | -c rcol1 gcol1 bcol1] pmap1 " |
126 |
|
|
"[-a] [-r radscale2] [-n num2] " |
127 |
|
|
"[-f | -c rcol2 gcol2 bcol2] pmap2 " |
128 |
rschregle |
2.12 |
"...\n", argv [0]); |
129 |
greg |
2.1 |
return 1; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
for (arg = 1; arg < argc; arg++) { |
133 |
|
|
/* Parse options */ |
134 |
|
|
if (argv [arg][0] == '-') { |
135 |
|
|
switch (argv [arg][1]) { |
136 |
rschregle |
2.14 |
case 'a': |
137 |
|
|
if (!setBool(argv [arg], 2, &points)) |
138 |
|
|
error(USER, "invalid option syntax at -a"); |
139 |
|
|
break; |
140 |
greg |
2.1 |
case 'r': |
141 |
|
|
if ((radScale = atof(argv [++arg])) <= 0) |
142 |
|
|
error(USER, "invalid radius scale"); |
143 |
|
|
break; |
144 |
|
|
|
145 |
|
|
case 'n': |
146 |
|
|
if ((numSpheres = parseMultiplier(argv [++arg])) <= 0) |
147 |
rschregle |
2.14 |
error(USER, "invalid number of points/spheres"); |
148 |
greg |
2.1 |
break; |
149 |
|
|
|
150 |
rschregle |
2.12 |
case 'c': |
151 |
rschregle |
2.13 |
if (fluxCol) |
152 |
|
|
error(USER, "-f and -c are mutually exclusive"); |
153 |
|
|
|
154 |
|
|
if (badarg(argc - arg - 1, &argv [arg + 1], "fff")) |
155 |
|
|
error(USER, "invalid RGB colour"); |
156 |
|
|
|
157 |
rschregle |
2.12 |
for (j = 0; j < 3; j++) |
158 |
rschregle |
2.14 |
col [j] = atof(argv [++arg]); |
159 |
rschregle |
2.13 |
break; |
160 |
|
|
|
161 |
|
|
case 'f': |
162 |
rschregle |
2.14 |
if (intens(col) > 0) |
163 |
rschregle |
2.13 |
error(USER, "-f and -c are mutually exclusive"); |
164 |
|
|
|
165 |
|
|
if (!setBool(argv [arg], 2, &fluxCol)) |
166 |
|
|
error(USER, "invalid option syntax at -f"); |
167 |
rschregle |
2.12 |
break; |
168 |
|
|
|
169 |
greg |
2.1 |
default: |
170 |
|
|
sprintf(errmsg, "unknown option %s", argv [arg]); |
171 |
|
|
error(USER, errmsg); |
172 |
|
|
return -1; |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
continue; |
176 |
|
|
} |
177 |
rschregle |
2.13 |
|
178 |
rschregle |
2.14 |
/* Open next photon map file */ |
179 |
greg |
2.1 |
if (!(pmapFile = fopen(argv [arg], "rb"))) { |
180 |
|
|
sprintf(errmsg, "can't open %s", argv [arg]); |
181 |
|
|
error(SYSTEM, errmsg); |
182 |
|
|
} |
183 |
rschregle |
2.13 |
|
184 |
greg |
2.1 |
/* Get format string */ |
185 |
|
|
strcpy(format, PMAP_FORMAT_GLOB); |
186 |
|
|
if (checkheader(pmapFile, format, NULL) != 1) { |
187 |
|
|
sprintf(errmsg, "photon map file %s has unknown format %s", |
188 |
|
|
argv [arg], format); |
189 |
|
|
error(USER, errmsg); |
190 |
|
|
} |
191 |
rschregle |
2.13 |
|
192 |
greg |
2.1 |
/* Identify photon map type from format string */ |
193 |
|
|
for (ptype = 0; |
194 |
rschregle |
2.6 |
ptype < NUM_PMAP_TYPES && strcmp(pmapFormat [ptype], format); |
195 |
greg |
2.1 |
ptype++); |
196 |
rschregle |
2.13 |
|
197 |
greg |
2.1 |
if (!validPmapType(ptype)) { |
198 |
|
|
sprintf(errmsg, "file %s contains an unknown photon map type", |
199 |
|
|
argv [arg]); |
200 |
|
|
error(USER, errmsg); |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
/* Get file format version and check for compatibility */ |
204 |
rschregle |
2.7 |
if (strcmp(getstr(format, pmapFile), PMAP_FILEVER)) |
205 |
greg |
2.1 |
error(USER, "incompatible photon map file format"); |
206 |
rschregle |
2.13 |
|
207 |
rschregle |
2.14 |
if (!points) { |
208 |
|
|
/* Dump command line as comment */ |
209 |
|
|
fputs("# ", stdout); |
210 |
|
|
printargs(argc, argv, stdout); |
211 |
|
|
fputc('\n', stdout); |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
/* Set point/sphere colour if independent of photon flux, |
215 |
|
|
output RADIANCE material def if required */ |
216 |
rschregle |
2.13 |
if (!fluxCol) { |
217 |
rschregle |
2.14 |
if (intens(col) <= 0) |
218 |
|
|
copycolor(col, colDefs [ptype]); |
219 |
|
|
if (!points) { |
220 |
|
|
printf(radDefs [ptype].mat, col [0], col [1], col [2]); |
221 |
|
|
fputc('\n', stdout); |
222 |
|
|
} |
223 |
rschregle |
2.13 |
} |
224 |
rschregle |
2.18 |
|
225 |
|
|
/* Get number of photons as fixed size, which possibly results in |
226 |
|
|
* padding of MSB with 0 on some platforms. Unlike sizeof() however, |
227 |
|
|
* this ensures portability since this value may span 32 or 64 bits |
228 |
|
|
* depending on platform. */ |
229 |
|
|
pm.numPhotons = getint(PMAP_LONGSIZE, pmapFile); |
230 |
rschregle |
2.13 |
|
231 |
greg |
2.1 |
/* Skip avg photon flux */ |
232 |
|
|
for (j = 0; j < 3; j++) |
233 |
|
|
getflt(pmapFile); |
234 |
rschregle |
2.13 |
|
235 |
greg |
2.1 |
/* Get distribution extent (min & max photon positions) */ |
236 |
|
|
for (j = 0; j < 3; j++) { |
237 |
rschregle |
2.7 |
pm.minPos [j] = getflt(pmapFile); |
238 |
|
|
pm.maxPos [j] = getflt(pmapFile); |
239 |
greg |
2.1 |
} |
240 |
|
|
|
241 |
|
|
/* Skip centre of gravity, and avg photon dist to it */ |
242 |
|
|
for (j = 0; j < 4; j++) |
243 |
|
|
getflt(pmapFile); |
244 |
|
|
|
245 |
rschregle |
2.9 |
/* Sphere radius based on avg intersphere dist depending on |
246 |
|
|
whether the distribution occupies a 1D line (!), a 2D plane, |
247 |
|
|
or 3D volume (= sphere distrib density ^-1/d, where d is the |
248 |
|
|
dimensionality of the distribution) */ |
249 |
|
|
for (j = 0, extent = 1.0, dim = 0; j < 3; j++) { |
250 |
|
|
rad = pm.maxPos [j] - pm.minPos [j]; |
251 |
|
|
|
252 |
|
|
if (rad > FTINY) { |
253 |
|
|
dim++; |
254 |
|
|
extent *= rad; |
255 |
|
|
} |
256 |
|
|
} |
257 |
|
|
|
258 |
|
|
rad = radScale * RADCOEFF * pow(extent / numSpheres, 1./dim); |
259 |
greg |
2.1 |
|
260 |
|
|
/* Photon dump probability to satisfy target sphere count */ |
261 |
rschregle |
2.14 |
dumpRatio = min(1, (float)numSpheres / pm.numPhotons); |
262 |
greg |
2.1 |
|
263 |
rschregle |
2.7 |
/* Skip primary rays */ |
264 |
|
|
pm.numPrimary = getint(sizeof(pm.numPrimary), pmapFile); |
265 |
|
|
while (pm.numPrimary-- > 0) { |
266 |
rschregle |
2.8 |
/* Skip source index & incident dir */ |
267 |
rschregle |
2.10 |
getint(sizeof(pri.srcIdx), pmapFile); |
268 |
|
|
#ifdef PMAP_PRIMARYDIR |
269 |
|
|
/* Skip primary incident dir */ |
270 |
|
|
getint(sizeof(pri.dir), pmapFile); |
271 |
rschregle |
2.13 |
#endif |
272 |
|
|
#ifdef PMAP_PRIMARYPOS |
273 |
rschregle |
2.8 |
/* Skip primary hitpoint */ |
274 |
rschregle |
2.7 |
for (j = 0; j < 3; j++) |
275 |
|
|
getflt(pmapFile); |
276 |
rschregle |
2.8 |
#endif |
277 |
rschregle |
2.7 |
} |
278 |
|
|
|
279 |
|
|
#ifdef PMAP_OOC |
280 |
|
|
/* Open leaf file with filename derived from pmap, replace pmapFile |
281 |
|
|
* (which is currently the node file) */ |
282 |
rschregle |
2.16 |
strncpy(leafFname, argv [arg], sizeof(leafFname) - 1); |
283 |
|
|
strncat(leafFname, PMAP_OOC_LEAFSUFFIX, sizeof(leafFname) - 1); |
284 |
rschregle |
2.7 |
fclose(pmapFile); |
285 |
|
|
if (!(pmapFile = fopen(leafFname, "rb"))) { |
286 |
|
|
sprintf(errmsg, "cannot open leaf file %s", leafFname); |
287 |
|
|
error(SYSTEM, errmsg); |
288 |
|
|
} |
289 |
|
|
#endif |
290 |
|
|
|
291 |
rschregle |
2.14 |
/* Read photons */ |
292 |
rschregle |
2.7 |
while (pm.numPhotons-- > 0) { |
293 |
|
|
#ifdef PMAP_OOC |
294 |
rschregle |
2.13 |
/* Get entire photon record from ooC octree leaf file |
295 |
rschregle |
2.7 |
!!! OOC PMAP FILES CURRENTLY DON'T USE PORTABLE I/O !!! */ |
296 |
|
|
if (!fread(&p, sizeof(p), 1, pmapFile)) { |
297 |
|
|
sprintf(errmsg, "error reading OOC leaf file %s", leafFname); |
298 |
|
|
error(SYSTEM, errmsg); |
299 |
|
|
} |
300 |
rschregle |
2.13 |
#else /* kd-tree */ |
301 |
|
|
/* Get photon position */ |
302 |
greg |
2.1 |
for (j = 0; j < 3; j++) |
303 |
|
|
p.pos [j] = getflt(pmapFile); |
304 |
rschregle |
2.13 |
|
305 |
|
|
/* Get photon normal (currently not used) */ |
306 |
greg |
2.1 |
for (j = 0; j < 3; j++) |
307 |
rschregle |
2.13 |
p.norm [j] = getint(1, pmapFile); |
308 |
|
|
|
309 |
|
|
/* Get photon flux */ |
310 |
|
|
#ifdef PMAP_FLOAT_FLUX |
311 |
rschregle |
2.7 |
for (j = 0; j < 3; j++) |
312 |
rschregle |
2.13 |
p.flux [j] = getflt(pmapFile); |
313 |
|
|
#else |
314 |
rschregle |
2.7 |
for (j = 0; j < 4; j++) |
315 |
rschregle |
2.13 |
p.flux [j] = getint(1, pmapFile); |
316 |
|
|
#endif |
317 |
rschregle |
2.15 |
|
318 |
|
|
|
319 |
greg |
2.1 |
|
320 |
|
|
/* Skip primary ray index */ |
321 |
|
|
getint(sizeof(p.primary), pmapFile); |
322 |
|
|
|
323 |
|
|
/* Skip flags */ |
324 |
|
|
getint(sizeof(p.flags), pmapFile); |
325 |
rschregle |
2.7 |
#endif |
326 |
rschregle |
2.13 |
|
327 |
|
|
/* Dump photon probabilistically acc. to target sphere count */ |
328 |
|
|
if (frandom() <= dumpRatio) { |
329 |
rschregle |
2.15 |
if (fluxCol) { |
330 |
rschregle |
2.14 |
/* Get photon flux */ |
331 |
|
|
getPhotonFlux(&p, col); |
332 |
rschregle |
2.15 |
/* Scale by dumpRatio for energy conservation */ |
333 |
|
|
scalecolor(col, 1.0 / dumpRatio); |
334 |
|
|
} |
335 |
rschregle |
2.14 |
|
336 |
|
|
if (!points) { |
337 |
|
|
if (fluxCol) { |
338 |
|
|
/* Dump material def if variable (depends on flux) */ |
339 |
|
|
printf(radDefs [ptype].mat, col [0], col [1], col [2]); |
340 |
|
|
fputc('\n', stdout); |
341 |
|
|
} |
342 |
|
|
printf(radDefs [ptype].obj, p.pos [0], p.pos [1], p.pos [2], |
343 |
|
|
rad); |
344 |
rschregle |
2.13 |
fputc('\n', stdout); |
345 |
|
|
} |
346 |
rschregle |
2.14 |
else /* Dump as XYZ RGB point */ |
347 |
|
|
printf(POINTFMT, p.pos [0], p.pos [1], p.pos [2], |
348 |
|
|
col [0], col [1] ,col [2]); |
349 |
rschregle |
2.13 |
} |
350 |
rschregle |
2.14 |
|
351 |
rschregle |
2.7 |
if (ferror(pmapFile) || feof(pmapFile)) { |
352 |
greg |
2.1 |
sprintf(errmsg, "error reading %s", argv [arg]); |
353 |
|
|
error(USER, errmsg); |
354 |
|
|
} |
355 |
|
|
} |
356 |
|
|
|
357 |
|
|
fclose(pmapFile); |
358 |
|
|
|
359 |
|
|
/* Reset defaults for next dump */ |
360 |
|
|
radScale = RADSCALE; |
361 |
|
|
numSpheres = NSPHERES; |
362 |
rschregle |
2.14 |
col [0] = col [1] = col [2] = 0; |
363 |
|
|
fluxCol = points = 0; |
364 |
greg |
2.1 |
} |
365 |
|
|
|
366 |
|
|
return 0; |
367 |
|
|
} |