ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapio.c
Revision: 2.10
Committed: Mon Aug 14 21:12:10 2017 UTC (6 years, 8 months ago) by rschregle
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R1
Changes since 2.9: +5 -5 lines
Log Message:
Updated photon map code for Windows; no multproc or ooC for now

File Contents

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