ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapio.c
Revision: 2.5
Committed: Fri May 22 14:09:01 2015 UTC (9 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +4 -4 lines
Log Message:
Removed redundant photon map type parameter

File Contents

# User Rev Content
1 greg 2.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 rschregle 2.2 (c) Lucerne University of Applied Sciences and Arts,
8     supported by the Swiss National Science Foundation (SNSF, #147053)
9 greg 2.1 ==================================================================
10    
11 greg 2.5 $Id: pmapio.c,v 2.4 2015/05/20 14:44:12 greg Exp $
12 greg 2.1 */
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 greg 2.5 int argc, char **argv)
24 greg 2.1 {
25     unsigned long i, j;
26     const Photon* p;
27     FILE* file;
28    
29     if (!pmap || !pmap -> heap || !pmap -> heapSize ||
30 greg 2.5 !validPmapType(pmap -> type)) {
31 greg 2.1 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 greg 2.5 fputformat((char*)pmapFormat [pmap -> type], file);
65 greg 2.1 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 greg 2.4 putint(prim -> dir, sizeof(prim -> dir), file);
131 greg 2.1
132     for (j = 0; j < 3; j++)
133 greg 2.3 putflt(prim -> pos [j], file);
134 greg 2.1
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 greg 2.4 prim -> dir = getint(sizeof(prim -> dir), file);
252 greg 2.1
253     for (j = 0; j < 3; j++)
254 greg 2.3 prim -> pos [j] = getflt(file);
255 greg 2.1
256     if (feof(file))
257     error(SYSTEM, "error reading primary photon rays");
258     }
259     }
260    
261     fclose(file);
262    
263     return ptype;
264     }