ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapio.c
Revision: 2.6
Committed: Tue Aug 18 18:45:55 2015 UTC (8 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +4 -1 lines
Log Message:
Added missing RCSid forgotten during initial check-in

File Contents

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