ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapdump.c
Revision: 2.2
Committed: Tue Apr 21 19:16:51 2015 UTC (9 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +4 -3 lines
Log Message:
Fixes for Windows and photon map

File Contents

# User Rev Content
1 greg 2.1 /*
2     ===============================================================
3     Dump photon maps as RADIANCE scene description to stdout
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 greg 2.2 $Id: pmapdump.c,v 4.13 2015/04/21 09:29:28 taschreg Exp taschreg $
11 greg 2.1 */
12    
13    
14    
15     #include "pmapio.h"
16     #include "pmapparm.h"
17     #include "pmaptype.h"
18     #include "rtio.h"
19     #include "resolu.h"
20     #include "random.h"
21 greg 2.2 #include "math.h"
22 greg 2.1
23    
24     /* Defaults */
25     /* Sphere radius as fraction of avg. intersphere dist */
26     /* Relative scale for sphere radius (fudge factor) */
27     /* Number of spheres */
28     #define RADCOEFF 0.05
29     #define RADSCALE 1.0
30     #define NSPHERES 10000
31    
32    
33     /* RADIANCE material and object defs for each photon type */
34     typedef struct {
35     char *mat, *obj;
36     } RadianceDef;
37    
38    
39 greg 2.2 static char header [] = "$Revision: 4.13 $";
40 greg 2.1
41    
42     /* Colour code is as follows: global = blue
43     precomp global = cyan
44     caustic = red
45     volume = green
46     direct = magenta
47     contrib = yellow */
48     const RadianceDef radDefs [] = {
49     { "void plastic mat.global\n0\n0\n5 0 0 1 0 0\n",
50     "mat.global sphere obj.global\n0\n0\n4 %g %g %g %g\n"
51     },
52     { "void plastic mat.pglobal\n0\n0\n5 0 1 1 0 0\n",
53     "mat.pglobal sphere obj.global\n0\n0\n4 %g %g %g %g\n"
54     },
55     { "void plastic mat.caustic\n0\n0\n5 1 0 0 0 0\n",
56     "mat.caustic sphere obj.caustic\n0\n0\n4 %g %g %g %g\n"
57     },
58     { "void plastic mat.volume\n0\n0\n5 0 1 0 0 0\n",
59     "mat.volume sphere obj.volume\n0\n0\n4 %g %g %g %g\n"
60     },
61     { "void plastic mat.direct\n0\n0\n5 1 0 1 0 0\n",
62     "mat.direct sphere obj.direct\n0\n0\n4 %g %g %g %g\n"
63     },
64     { "void plastic mat.contrib\n0\n0\n5 1 1 0 0 0\n",
65     "mat.contrib sphere obj.contrib\n0\n0\n4 %g %g %g %g\n"
66     }
67     };
68    
69    
70    
71     int main (int argc, char** argv)
72     {
73     char format [128];
74     RREAL rad, radScale = RADSCALE, vol, dumpRatio;
75     FVECT minPos, maxPos;
76     unsigned arg, j, ptype;
77     long numPhotons, numSpheres = NSPHERES;
78     FILE *pmapFile;
79     Photon p;
80    
81     if (argc < 2) {
82     puts("Dump photon maps as RADIANCE scene description\n");
83     printf("Usage: %s [-r radscale1] [-n nspheres1] pmap1 "
84     "[-r radscale2] [-n nspheres2] pmap2 ...\n", argv [0]);
85     return 1;
86     }
87    
88     for (arg = 1; arg < argc; arg++) {
89     /* Parse options */
90     if (argv [arg][0] == '-') {
91     switch (argv [arg][1]) {
92     case 'r':
93     if ((radScale = atof(argv [++arg])) <= 0)
94     error(USER, "invalid radius scale");
95     break;
96    
97     case 'n':
98     if ((numSpheres = parseMultiplier(argv [++arg])) <= 0)
99     error(USER, "invalid number of spheres");
100     break;
101    
102     default:
103     sprintf(errmsg, "unknown option %s", argv [arg]);
104     error(USER, errmsg);
105     return -1;
106     }
107    
108     continue;
109     }
110    
111     /* Dump photon map */
112     if (!(pmapFile = fopen(argv [arg], "rb"))) {
113     sprintf(errmsg, "can't open %s", argv [arg]);
114     error(SYSTEM, errmsg);
115     }
116    
117     /* Get format string */
118     strcpy(format, PMAP_FORMAT_GLOB);
119     if (checkheader(pmapFile, format, NULL) != 1) {
120     sprintf(errmsg, "photon map file %s has unknown format %s",
121     argv [arg], format);
122     error(USER, errmsg);
123     }
124    
125     /* Identify photon map type from format string */
126     for (ptype = 0;
127     strcmp(pmapFormat [ptype], format) && ptype < NUM_PMAP_TYPES;
128     ptype++);
129    
130     if (!validPmapType(ptype)) {
131     sprintf(errmsg, "file %s contains an unknown photon map type",
132     argv [arg]);
133     error(USER, errmsg);
134     }
135    
136     /* Get file format version and check for compatibility */
137     if (getint(sizeof(PMAP_FILEVER), pmapFile) != PMAP_FILEVER)
138     error(USER, "incompatible photon map file format");
139    
140     /* Dump command line as comment */
141     fputs("# ", stdout);
142     printargs(argc, argv, stdout);
143     fputc('\n', stdout);
144    
145     /* Dump material def */
146     fputs(radDefs [ptype].mat, stdout);
147     fputc('\n', stdout);
148    
149     /* Get number of photons (is this sizeof() hack portable?) */
150     numPhotons = getint(sizeof(((PhotonMap*)NULL) -> heapSize), pmapFile);
151    
152     /* Skip avg photon flux */
153     for (j = 0; j < 3; j++)
154     getflt(pmapFile);
155    
156     /* Get distribution extent (min & max photon positions) */
157     for (j = 0; j < 3; j++) {
158     minPos [j] = getflt(pmapFile);
159     maxPos [j] = getflt(pmapFile);
160     }
161    
162     /* Skip centre of gravity, and avg photon dist to it */
163     for (j = 0; j < 4; j++)
164     getflt(pmapFile);
165    
166     /* Sphere radius based on avg intersphere dist
167     (= sphere distrib density ^-1/3) */
168     vol = (maxPos [0] - minPos [0]) * (maxPos [1] - minPos [1]) *
169     (maxPos [2] - minPos [2]);
170 greg 2.2 rad = radScale * RADCOEFF * pow(vol / numSpheres, 1./3.);
171 greg 2.1
172     /* Photon dump probability to satisfy target sphere count */
173     dumpRatio = numSpheres < numPhotons ? (float)numSpheres / numPhotons
174     : 1;
175    
176     while (numPhotons-- > 0) {
177     /* Get photon position */
178     for (j = 0; j < 3; j++)
179     p.pos [j] = getflt(pmapFile);
180    
181     /* Dump photon probabilistically acc. to target sphere count */
182     if (frandom() <= dumpRatio) {
183     printf(radDefs [ptype].obj, p.pos [0], p.pos [1], p.pos [2], rad);
184     fputc('\n', stdout);
185     }
186    
187     /* Skip photon normal and flux */
188     for (j = 0; j < 3; j++)
189     getint(sizeof(p.norm [j]), pmapFile);
190    
191     #ifdef PMAP_FLOAT_FLUX
192     for (j = 0; j < 3; j++)
193     getflt(pmapFile);
194     #else
195     for (j = 0; j < 4; j++)
196     getint(1, pmapFile);
197     #endif
198    
199     /* Skip primary ray index */
200     getint(sizeof(p.primary), pmapFile);
201    
202     /* Skip flags */
203     getint(sizeof(p.flags), pmapFile);
204    
205     if (feof(pmapFile)) {
206     sprintf(errmsg, "error reading %s", argv [arg]);
207     error(USER, errmsg);
208     }
209     }
210    
211     fclose(pmapFile);
212    
213     /* Reset defaults for next dump */
214     radScale = RADSCALE;
215     numSpheres = NSPHERES;
216     }
217    
218     return 0;
219     }