ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapio.c
Revision: 2.1
Committed: Tue Feb 24 19:39:27 2015 UTC (9 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial check-in of photon map addition by Roland Schregle

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