ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapio.c
Revision: 2.2
Committed: Fri May 8 13:20:23 2015 UTC (9 years ago) by rschregle
Content type: text/plain
Branch: MAIN
Changes since 2.1: +3 -2 lines
Log Message:
Double-counting bugfix for glow sources (thanks DGM!), revised copyright

File Contents

# Content
1 /*
2 ==================================================================
3 Photon map file I/O
4
5 Roland Schregle (roland.schregle@{hslu.ch, gmail.com})
6 (c) Fraunhofer Institute for Solar Energy Systems,
7 (c) Lucerne University of Applied Sciences and Arts,
8 supported by the Swiss National Science Foundation (SNSF, #147053)
9 ==================================================================
10
11 $Id: pmapio.c,v 2.1 2015/02/24 19:39:27 greg Exp $
12 */
13
14
15
16 #include "pmapio.h"
17 #include "pmapdiag.h"
18 #include "resolu.h"
19
20
21
22 void savePhotonMap (const PhotonMap *pmap, const char *fname,
23 PhotonMapType type, int argc, char **argv)
24 {
25 unsigned long i, j;
26 const Photon* p;
27 FILE* file;
28
29 if (!pmap || !pmap -> heap || !pmap -> heapSize ||
30 !validPmapType(type)) {
31 error(INTERNAL, "attempt to save empty or invalid photon map");
32 return;
33 }
34
35 if (photonRepTime) {
36 sprintf(errmsg, "Saving %s (%ld photons)...\n",
37 fname, pmap -> heapSize);
38 eputs(errmsg);
39 fflush(stderr);
40 }
41
42 if (!(file = fopen(fname, "wb"))) {
43 sprintf(errmsg, "can't open photon map file %s", fname);
44 error(SYSTEM, errmsg);
45 }
46
47 /* Write header */
48 newheader("RADIANCE", file);
49
50 /* Write command line */
51 printargs(argc, argv, file);
52
53 /* Include statistics in info text */
54 fprintf(file, "%ld photons @ (%.2e, %.2e, %.2e) avg watts\n"
55 "Extent [%.3f, %.3f, %.3f] [%.3f, %.3f, %.3f]\n",
56 pmap -> heapSize, pmap -> photonFlux [0],
57 pmap -> photonFlux [1], pmap -> photonFlux [2],
58 pmap -> minPos [0], pmap -> minPos [1], pmap -> minPos [2],
59 pmap -> maxPos [0], pmap -> maxPos [1], pmap -> maxPos [2]);
60 if (pmap -> primary)
61 fprintf(file, "%d primary rays\n", pmap -> primaryEnd + 1);
62
63 /* Write format */
64 fputformat((char*)pmapFormat [type], file);
65 fprintf(file, "VERSION=%d\n", PMAP_FILEVER);
66
67 /* Empty line = end of header */
68 putc('\n', file);
69
70 /* Write file format version */
71 putint(PMAP_FILEVER, sizeof(PMAP_FILEVER), file);
72
73 /* Write number of photons */
74 putint(pmap -> heapSize, sizeof(pmap -> heapSize), file);
75
76 /* Write average photon flux */
77 for (j = 0; j < 3; j++)
78 putflt(pmap -> photonFlux [j], file);
79
80 /* Write max and min photon positions */
81 for (j = 0; j < 3; j++) {
82 putflt(pmap -> minPos [j], file);
83 putflt(pmap -> maxPos [j], file);
84 }
85
86 /* Write centre of gravity */
87 for (j = 0; j < 3; j++)
88 putflt(pmap -> CoG [j], file);
89
90 /* Write avg distance to centre of gravity */
91 putflt(pmap -> CoGdist, file);
92
93 for (i = 0, p = pmap -> heap; i < pmap -> heapSize; i++, p++) {
94 /* Write photon attributes */
95 for (j = 0; j < 3; j++)
96 putflt(p -> pos [j], file);
97
98 /* Bytewise dump otherwise we have portability probs */
99 for (j = 0; j < 3; j++)
100 putint(p -> norm [j], 1, file);
101
102 #ifdef PMAP_FLOAT_FLUX
103 for (j = 0; j < 3; j++)
104 putflt(p -> flux [j], file);
105 #else
106 for (j = 0; j < 4; j++)
107 putint(p -> flux [j], 1, file);
108 #endif
109
110 putint(p -> primary, sizeof(p -> primary), file);
111 putint(p -> flags, 1, file);
112
113 if (ferror(file)) {
114 sprintf(errmsg, "error writing photon map file %s", fname);
115 error(SYSTEM, errmsg);
116 }
117 }
118
119 /* Write out primary photon rays (or just zero count if none) */
120 if (pmap -> primary) {
121 /* primaryEnd points to last primary ray in array, so increment for
122 * number of entries */
123 putint(pmap -> primaryEnd + 1, sizeof(pmap -> primaryEnd), file);
124
125 for (i = 0; i <= pmap -> primaryEnd; i++) {
126 PhotonPrimary *prim = pmap -> primary + i;
127
128 putint(prim -> srcIdx, sizeof(prim -> srcIdx), file);
129
130 for (j = 0; j < 3; j++)
131 putflt(prim -> dir [j], file);
132
133 for (j = 0; j < 3; j++)
134 putflt(prim -> org [j], file);
135
136 if (ferror(file))
137 error(SYSTEM, "error writing primary photon rays");
138 }
139 }
140 else putint(0, sizeof(pmap -> primaryEnd), file);
141
142 fclose(file);
143 }
144
145
146
147 PhotonMapType loadPhotonMap (PhotonMap *pmap, const char *fname)
148 {
149 Photon* p;
150 PhotonMapType ptype = PMAP_TYPE_NONE;
151 char format [128];
152 unsigned long i, j;
153 FILE *file;
154
155 if (!pmap)
156 return PMAP_TYPE_NONE;
157
158 if ((file = fopen(fname, "rb")) == NULL) {
159 sprintf(errmsg, "can't open photon map file %s", fname);
160 error(SYSTEM, errmsg);
161 }
162
163 /* Get format string */
164 strcpy(format, PMAP_FORMAT_GLOB);
165 if (checkheader(file, format, NULL) != 1) {
166 sprintf(errmsg, "photon map file %s has unknown format %s",
167 fname, format);
168 error(USER, errmsg);
169 }
170
171 /* Identify photon map type from format string */
172 for (ptype = 0;
173 strcmp(pmapFormat [ptype], format) && ptype < NUM_PMAP_TYPES;
174 ptype++);
175
176 if (!validPmapType(ptype)) {
177 sprintf(errmsg, "file %s contains an unknown photon map type", fname);
178 error(USER, errmsg);
179 }
180
181 initPhotonMap(pmap, ptype);
182
183 /* Get file format version and check for compatibility */
184 if (getint(sizeof(PMAP_FILEVER), file) != PMAP_FILEVER)
185 error(USER, "incompatible photon map file format");
186
187 /* Get number of photons */
188 pmap -> heapSize = pmap -> heapEnd =
189 getint(sizeof(pmap -> heapSize), file);
190 pmap -> heap = (Photon *)malloc(pmap -> heapSize * sizeof(Photon));
191 if (!pmap -> heap)
192 error(INTERNAL, "can't allocate photon heap from file");
193
194 /* Get average photon flux */
195 for (j = 0; j < 3; j++)
196 pmap -> photonFlux [j] = getflt(file);
197
198 /* Get max and min photon positions */
199 for (j = 0; j < 3; j++) {
200 pmap -> minPos [j] = getflt(file);
201 pmap -> maxPos [j] = getflt(file);
202 }
203
204 /* Get centre of gravity */
205 for (j = 0; j < 3; j++)
206 pmap -> CoG [j] = getflt(file);
207
208 /* Get avg distance to centre of gravity */
209 pmap -> CoGdist = getflt(file);
210
211 /* Get photon attributes */
212 for (i = 0, p = pmap -> heap; i < pmap -> heapSize; i++, p++) {
213 for (j = 0; j < 3; j++)
214 p -> pos [j] = getflt(file);
215
216 /* Bytewise grab otherwise we have portability probs */
217 for (j = 0; j < 3; j++)
218 p -> norm [j] = getint(1, file);
219
220 #ifdef PMAP_FLOAT_FLUX
221 for (j = 0; j < 3; j++)
222 p -> flux [j] = getflt(file);
223 #else
224 for (j = 0; j < 4; j++)
225 p -> flux [j] = getint(1, file);
226 #endif
227
228 p -> primary = getint(sizeof(p -> primary), file);
229 p -> flags = getint(1, file);
230
231 if (feof(file)) {
232 sprintf(errmsg, "error reading photon map file %s", fname);
233 error(SYSTEM, errmsg);
234 }
235 }
236
237 /* Get primary photon rays */
238 pmap -> primarySize = getint(sizeof(pmap -> primarySize), file);
239
240 if (pmap -> primarySize) {
241 pmap -> primaryEnd = pmap -> primarySize - 1;
242 pmap -> primary = (PhotonPrimary*)malloc(pmap -> primarySize *
243 sizeof(PhotonPrimary));
244 if (!pmap -> primary)
245 error(INTERNAL, "can't allocate primary photon rays");
246
247 for (i = 0; i < pmap -> primarySize; i++) {
248 PhotonPrimary *prim = pmap -> primary + i;
249
250 prim -> srcIdx = getint(sizeof(prim -> srcIdx), file);
251
252 for (j = 0; j < 3; j++)
253 prim -> dir [j] = getflt(file);
254
255 for (j = 0; j < 3; j++)
256 prim -> org [j] = getflt(file);
257
258 if (feof(file))
259 error(SYSTEM, "error reading primary photon rays");
260 }
261 }
262
263 fclose(file);
264
265 return ptype;
266 }