1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
================================================================== |
6 |
Dump photon maps as RADIANCE scene description to stdout |
7 |
|
8 |
Roland Schregle (roland.schregle@{hslu.ch, gmail.com}) |
9 |
(c) Fraunhofer Institute for Solar Energy Systems, |
10 |
(c) Lucerne University of Applied Sciences and Arts, |
11 |
supported by the Swiss National Science Foundation (SNSF, #147053) |
12 |
================================================================== |
13 |
|
14 |
$Id: pmapdump.c,v 2.3 2015/05/08 13:20:23 rschregle Exp $ |
15 |
*/ |
16 |
|
17 |
|
18 |
|
19 |
#include "pmapio.h" |
20 |
#include "pmapparm.h" |
21 |
#include "pmaptype.h" |
22 |
#include "rtio.h" |
23 |
#include "resolu.h" |
24 |
#include "random.h" |
25 |
#include "math.h" |
26 |
|
27 |
|
28 |
/* Defaults */ |
29 |
/* Sphere radius as fraction of avg. intersphere dist */ |
30 |
/* Relative scale for sphere radius (fudge factor) */ |
31 |
/* Number of spheres */ |
32 |
#define RADCOEFF 0.05 |
33 |
#define RADSCALE 1.0 |
34 |
#define NSPHERES 10000 |
35 |
|
36 |
|
37 |
/* RADIANCE material and object defs for each photon type */ |
38 |
typedef struct { |
39 |
char *mat, *obj; |
40 |
} RadianceDef; |
41 |
|
42 |
|
43 |
static char header [] = "$Revision: 2.3 $"; |
44 |
|
45 |
|
46 |
/* Colour code is as follows: global = blue |
47 |
precomp global = cyan |
48 |
caustic = red |
49 |
volume = green |
50 |
direct = magenta |
51 |
contrib = yellow */ |
52 |
const RadianceDef radDefs [] = { |
53 |
{ "void plastic mat.global\n0\n0\n5 0 0 1 0 0\n", |
54 |
"mat.global sphere obj.global\n0\n0\n4 %g %g %g %g\n" |
55 |
}, |
56 |
{ "void plastic mat.pglobal\n0\n0\n5 0 1 1 0 0\n", |
57 |
"mat.pglobal sphere obj.global\n0\n0\n4 %g %g %g %g\n" |
58 |
}, |
59 |
{ "void plastic mat.caustic\n0\n0\n5 1 0 0 0 0\n", |
60 |
"mat.caustic sphere obj.caustic\n0\n0\n4 %g %g %g %g\n" |
61 |
}, |
62 |
{ "void plastic mat.volume\n0\n0\n5 0 1 0 0 0\n", |
63 |
"mat.volume sphere obj.volume\n0\n0\n4 %g %g %g %g\n" |
64 |
}, |
65 |
{ "void plastic mat.direct\n0\n0\n5 1 0 1 0 0\n", |
66 |
"mat.direct sphere obj.direct\n0\n0\n4 %g %g %g %g\n" |
67 |
}, |
68 |
{ "void plastic mat.contrib\n0\n0\n5 1 1 0 0 0\n", |
69 |
"mat.contrib sphere obj.contrib\n0\n0\n4 %g %g %g %g\n" |
70 |
} |
71 |
}; |
72 |
|
73 |
|
74 |
|
75 |
int main (int argc, char** argv) |
76 |
{ |
77 |
char format [128]; |
78 |
RREAL rad, radScale = RADSCALE, vol, dumpRatio; |
79 |
FVECT minPos, maxPos; |
80 |
unsigned arg, j, ptype; |
81 |
long numPhotons, numSpheres = NSPHERES; |
82 |
FILE *pmapFile; |
83 |
Photon p; |
84 |
|
85 |
if (argc < 2) { |
86 |
puts("Dump photon maps as RADIANCE scene description\n"); |
87 |
printf("Usage: %s [-r radscale1] [-n nspheres1] pmap1 " |
88 |
"[-r radscale2] [-n nspheres2] pmap2 ...\n", argv [0]); |
89 |
return 1; |
90 |
} |
91 |
|
92 |
for (arg = 1; arg < argc; arg++) { |
93 |
/* Parse options */ |
94 |
if (argv [arg][0] == '-') { |
95 |
switch (argv [arg][1]) { |
96 |
case 'r': |
97 |
if ((radScale = atof(argv [++arg])) <= 0) |
98 |
error(USER, "invalid radius scale"); |
99 |
break; |
100 |
|
101 |
case 'n': |
102 |
if ((numSpheres = parseMultiplier(argv [++arg])) <= 0) |
103 |
error(USER, "invalid number of spheres"); |
104 |
break; |
105 |
|
106 |
default: |
107 |
sprintf(errmsg, "unknown option %s", argv [arg]); |
108 |
error(USER, errmsg); |
109 |
return -1; |
110 |
} |
111 |
|
112 |
continue; |
113 |
} |
114 |
|
115 |
/* Dump photon map */ |
116 |
if (!(pmapFile = fopen(argv [arg], "rb"))) { |
117 |
sprintf(errmsg, "can't open %s", argv [arg]); |
118 |
error(SYSTEM, errmsg); |
119 |
} |
120 |
|
121 |
/* Get format string */ |
122 |
strcpy(format, PMAP_FORMAT_GLOB); |
123 |
if (checkheader(pmapFile, format, NULL) != 1) { |
124 |
sprintf(errmsg, "photon map file %s has unknown format %s", |
125 |
argv [arg], format); |
126 |
error(USER, errmsg); |
127 |
} |
128 |
|
129 |
/* Identify photon map type from format string */ |
130 |
for (ptype = 0; |
131 |
strcmp(pmapFormat [ptype], format) && ptype < NUM_PMAP_TYPES; |
132 |
ptype++); |
133 |
|
134 |
if (!validPmapType(ptype)) { |
135 |
sprintf(errmsg, "file %s contains an unknown photon map type", |
136 |
argv [arg]); |
137 |
error(USER, errmsg); |
138 |
} |
139 |
|
140 |
/* Get file format version and check for compatibility */ |
141 |
if (getint(sizeof(PMAP_FILEVER), pmapFile) != PMAP_FILEVER) |
142 |
error(USER, "incompatible photon map file format"); |
143 |
|
144 |
/* Dump command line as comment */ |
145 |
fputs("# ", stdout); |
146 |
printargs(argc, argv, stdout); |
147 |
fputc('\n', stdout); |
148 |
|
149 |
/* Dump material def */ |
150 |
fputs(radDefs [ptype].mat, stdout); |
151 |
fputc('\n', stdout); |
152 |
|
153 |
/* Get number of photons (is this sizeof() hack portable?) */ |
154 |
numPhotons = getint(sizeof(((PhotonMap*)NULL) -> heapSize), pmapFile); |
155 |
|
156 |
/* Skip avg photon flux */ |
157 |
for (j = 0; j < 3; j++) |
158 |
getflt(pmapFile); |
159 |
|
160 |
/* Get distribution extent (min & max photon positions) */ |
161 |
for (j = 0; j < 3; j++) { |
162 |
minPos [j] = getflt(pmapFile); |
163 |
maxPos [j] = getflt(pmapFile); |
164 |
} |
165 |
|
166 |
/* Skip centre of gravity, and avg photon dist to it */ |
167 |
for (j = 0; j < 4; j++) |
168 |
getflt(pmapFile); |
169 |
|
170 |
/* Sphere radius based on avg intersphere dist |
171 |
(= sphere distrib density ^-1/3) */ |
172 |
vol = (maxPos [0] - minPos [0]) * (maxPos [1] - minPos [1]) * |
173 |
(maxPos [2] - minPos [2]); |
174 |
rad = radScale * RADCOEFF * pow(vol / numSpheres, 1./3.); |
175 |
|
176 |
/* Photon dump probability to satisfy target sphere count */ |
177 |
dumpRatio = numSpheres < numPhotons ? (float)numSpheres / numPhotons |
178 |
: 1; |
179 |
|
180 |
while (numPhotons-- > 0) { |
181 |
/* Get photon position */ |
182 |
for (j = 0; j < 3; j++) |
183 |
p.pos [j] = getflt(pmapFile); |
184 |
|
185 |
/* Dump photon probabilistically acc. to target sphere count */ |
186 |
if (frandom() <= dumpRatio) { |
187 |
printf(radDefs [ptype].obj, p.pos [0], p.pos [1], p.pos [2], rad); |
188 |
fputc('\n', stdout); |
189 |
} |
190 |
|
191 |
/* Skip photon normal and flux */ |
192 |
for (j = 0; j < 3; j++) |
193 |
getint(sizeof(p.norm [j]), pmapFile); |
194 |
|
195 |
#ifdef PMAP_FLOAT_FLUX |
196 |
for (j = 0; j < 3; j++) |
197 |
getflt(pmapFile); |
198 |
#else |
199 |
for (j = 0; j < 4; j++) |
200 |
getint(1, pmapFile); |
201 |
#endif |
202 |
|
203 |
/* Skip primary ray index */ |
204 |
getint(sizeof(p.primary), pmapFile); |
205 |
|
206 |
/* Skip flags */ |
207 |
getint(sizeof(p.flags), pmapFile); |
208 |
|
209 |
if (feof(pmapFile)) { |
210 |
sprintf(errmsg, "error reading %s", argv [arg]); |
211 |
error(USER, errmsg); |
212 |
} |
213 |
} |
214 |
|
215 |
fclose(pmapFile); |
216 |
|
217 |
/* Reset defaults for next dump */ |
218 |
radScale = RADSCALE; |
219 |
numSpheres = NSPHERES; |
220 |
} |
221 |
|
222 |
return 0; |
223 |
} |