ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapdump.c
(Generate patch)

Comparing ray/src/rt/pmapdump.c (file contents):
Revision 2.4 by greg, Tue Aug 18 18:45:55 2015 UTC vs.
Revision 2.12 by rschregle, Wed Nov 21 19:30:59 2018 UTC

# Line 1 | Line 1
1   #ifndef lint
2   static const char RCSid[] = "$Id$";
3   #endif
4 +
5   /*
6 <   ==================================================================
6 >   ======================================================================
7     Dump photon maps as RADIANCE scene description to stdout
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 <   ==================================================================
12 >       supported by the Swiss National Science Foundation (SNSF, #147053)
13 >   ======================================================================
14    
15     $Id$
16   */
# Line 24 | Line 25 | static const char RCSid[] = "$Id$";
25   #include "random.h"
26   #include "math.h"
27  
28 + #define PMAPDUMP_REC "$Revision$"  
29  
30 +
31   /* Defaults */
32   /*    Sphere radius as fraction of avg. intersphere dist */
33   /*    Relative scale for sphere radius (fudge factor) */
# Line 39 | Line 42 | typedef struct {
42     char *mat, *obj;
43   } RadianceDef;
44  
42  
43 static char header [] = "$Revision$";
45  
45
46 /* Colour code is as follows:    global         = blue
47                                 precomp global = cyan
48                                 caustic        = red
49                                 volume         = green
50                                 direct         = magenta
51                                 contrib        = yellow */  
46   const RadianceDef radDefs [] = {
47 <   {  "void plastic mat.global\n0\n0\n5 0 0 1 0 0\n",
47 >   {  "void plastic mat.global\n0\n0\n5 %f %f %f 0 0\n",
48        "mat.global sphere obj.global\n0\n0\n4 %g %g %g %g\n"
49     },
50 <   {  "void plastic mat.pglobal\n0\n0\n5 0 1 1 0 0\n",
50 >   {  "void plastic mat.pglobal\n0\n0\n5 %f %f %f 0 0\n",
51        "mat.pglobal sphere obj.global\n0\n0\n4 %g %g %g %g\n"
52     },
53 <   {  "void plastic mat.caustic\n0\n0\n5 1 0 0 0 0\n",
53 >   {  "void plastic mat.caustic\n0\n0\n5 %f %f %f 0 0\n",
54        "mat.caustic sphere obj.caustic\n0\n0\n4 %g %g %g %g\n"
55     },
56 <   {  "void plastic mat.volume\n0\n0\n5 0 1 0 0 0\n",
56 >   {  "void plastic mat.volume\n0\n0\n5 %f %f %f 0 0\n",
57        "mat.volume sphere obj.volume\n0\n0\n4 %g %g %g %g\n"
58     },
59 <   {  "void plastic mat.direct\n0\n0\n5 1 0 1 0 0\n",
59 >   {  "void plastic mat.direct\n0\n0\n5 %f %f %f 0 0\n",
60        "mat.direct sphere obj.direct\n0\n0\n4 %g %g %g %g\n"
61     },
62 <   {  "void plastic mat.contrib\n0\n0\n5 1 1 0 0 0\n",
62 >   {  "void plastic mat.contrib\n0\n0\n5 %f %f %f 0 0\n",
63        "mat.contrib sphere obj.contrib\n0\n0\n4 %g %g %g %g\n"
64     }
65   };
66  
67 + /* Default colour codes are as follows:   global         = blue
68 +                                          precomp global = cyan
69 +                                          caustic        = red
70 +                                          volume         = green
71 +                                          direct         = magenta
72 +                                          contrib        = yellow */
73 + const COLOR colDefs [] = {
74 +   {0, 0, 1}, {0, 1, 1}, {1, 0, 0}, {0, 1, 0}, {1, 0, 1}, {1, 1, 0}
75 + };
76  
77  
78   int main (int argc, char** argv)
79   {
80 <   char format [128];
81 <   RREAL rad, radScale = RADSCALE, vol, dumpRatio;
82 <   FVECT minPos, maxPos;
83 <   unsigned arg, j, ptype;
84 <   long numPhotons, numSpheres = NSPHERES;
85 <   FILE *pmapFile;
86 <   Photon p;
80 >   char           format [MAXFMTLEN];
81 >   RREAL          rad, radScale = RADSCALE, extent, dumpRatio;
82 >   unsigned       arg, j, ptype, dim;
83 >   long           numSpheres = NSPHERES;
84 >   COLOR          customCol = {0, 0, 0};
85 >   FILE           *pmapFile;
86 >   PhotonMap      pm;
87 >   PhotonPrimary  pri;
88 >   Photon         p;
89 > #ifdef PMAP_OOC
90 >   char           leafFname [1024];
91 > #endif
92    
93     if (argc < 2) {
94        puts("Dump photon maps as RADIANCE scene description\n");
95 <      printf("Usage: %s [-r radscale1] [-n nspheres1] pmap1 "
96 <             "[-r radscale2] [-n nspheres2] pmap2 ...\n", argv [0]);
95 >      printf("Usage: %s "
96 >             "[-r radscale1] [-n nspheres1] [-c rcol1 gcol1 bcol1] pmap1 "
97 >             "[-r radscale2] [-n nspheres2] [-c rcol2 gcol2 bcol2] pmap2 "
98 >             "...\n", argv [0]);
99        return 1;
100     }
101    
# Line 103 | Line 113 | int main (int argc, char** argv)
113                    error(USER, "invalid number of spheres");
114                 break;
115                
116 +            case 'c':
117 +               for (j = 0; j < 3; j++)
118 +                  if ((customCol [j] = atof(argv [++arg])) <= 0)
119 +                     error(USER, "invalid RGB colour");
120 +               break;
121 +              
122              default:
123                 sprintf(errmsg, "unknown option %s", argv [arg]);
124                 error(USER, errmsg);
# Line 128 | Line 144 | int main (int argc, char** argv)
144        
145        /* Identify photon map type from format string */
146        for (ptype = 0;
147 <           strcmp(pmapFormat [ptype], format) && ptype < NUM_PMAP_TYPES;
147 >           ptype < NUM_PMAP_TYPES && strcmp(pmapFormat [ptype], format);
148             ptype++);
149        
150        if (!validPmapType(ptype)) {
# Line 138 | Line 154 | int main (int argc, char** argv)
154        }
155  
156        /* Get file format version and check for compatibility */
157 <      if (getint(sizeof(PMAP_FILEVER), pmapFile) != PMAP_FILEVER)
157 >      if (strcmp(getstr(format, pmapFile), PMAP_FILEVER))      
158           error(USER, "incompatible photon map file format");
159          
160        /* Dump command line as comment */
# Line 146 | Line 162 | int main (int argc, char** argv)
162        printargs(argc, argv, stdout);
163        fputc('\n', stdout);
164        
165 <      /* Dump material def */  
166 <      fputs(radDefs [ptype].mat, stdout);
165 >      /* Dump material def */
166 >      if (intens(customCol) > 0)
167 >         printf(radDefs [ptype].mat,
168 >                customCol [0], customCol [1], customCol [2]);
169 >      else
170 >         printf(radDefs [ptype].mat,
171 >                colDefs [ptype][0], colDefs [ptype][1], colDefs [ptype][2]);
172        fputc('\n', stdout);
173        
174 <      /* Get number of photons (is this sizeof() hack portable?) */
175 <      numPhotons = getint(sizeof(((PhotonMap*)NULL) -> heapSize), pmapFile);
174 >      /* Get number of photons */
175 >      pm.numPhotons = getint(sizeof(pm.numPhotons), pmapFile);
176        
177        /* Skip avg photon flux */
178        for (j = 0; j < 3; j++)
# Line 159 | Line 180 | int main (int argc, char** argv)
180        
181        /* Get distribution extent (min & max photon positions) */
182        for (j = 0; j < 3; j++) {
183 <         minPos [j] = getflt(pmapFile);
184 <         maxPos [j] = getflt(pmapFile);
183 >         pm.minPos [j] = getflt(pmapFile);
184 >         pm.maxPos [j] = getflt(pmapFile);
185        }
186        
187        /* Skip centre of gravity, and avg photon dist to it */
188        for (j = 0; j < 4; j++)
189           getflt(pmapFile);
190        
191 <      /* Sphere radius based on avg intersphere dist
192 <         (= sphere distrib density ^-1/3) */
193 <      vol = (maxPos [0] - minPos [0]) * (maxPos [1] - minPos [1]) *
194 <            (maxPos [2] - minPos [2]);
195 <      rad = radScale * RADCOEFF * pow(vol / numSpheres, 1./3.);
191 >      /* Sphere radius based on avg intersphere dist depending on
192 >         whether the distribution occupies a 1D line (!), a 2D plane,
193 >         or 3D volume (= sphere distrib density ^-1/d, where d is the
194 >         dimensionality of the distribution) */
195 >      for (j = 0, extent = 1.0, dim = 0; j < 3; j++) {
196 >         rad = pm.maxPos [j] - pm.minPos [j];
197 >        
198 >         if (rad > FTINY) {
199 >            dim++;
200 >            extent *= rad;
201 >         }
202 >      }
203 >
204 >      rad = radScale * RADCOEFF * pow(extent / numSpheres, 1./dim);
205        
206        /* Photon dump probability to satisfy target sphere count */
207 <      dumpRatio = numSpheres < numPhotons ? (float)numSpheres / numPhotons
208 <                                          : 1;
207 >      dumpRatio = numSpheres < pm.numPhotons
208 >                  ? (float)numSpheres / pm.numPhotons : 1;
209        
210 <      while (numPhotons-- > 0) {
210 >      /* Skip primary rays */
211 >      pm.numPrimary = getint(sizeof(pm.numPrimary), pmapFile);
212 >      while (pm.numPrimary-- > 0) {
213 >         /* Skip source index & incident dir */
214 >         getint(sizeof(pri.srcIdx), pmapFile);
215 > #ifdef PMAP_PRIMARYDIR
216 >         /* Skip primary incident dir */
217 >         getint(sizeof(pri.dir), pmapFile);        
218 > #endif        
219 > #ifdef PMAP_PRIMARYPOS        
220 >         /* Skip primary hitpoint */
221 >         for (j = 0; j < 3; j++)
222 >            getflt(pmapFile);
223 > #endif
224 >      }
225 >
226 > #ifdef PMAP_OOC
227 >      /* Open leaf file with filename derived from pmap, replace pmapFile
228 >       * (which is currently the node file) */
229 >      strncpy(leafFname, argv [arg], 1024);
230 >      strncat(leafFname, PMAP_OOC_LEAFSUFFIX, 1024);
231 >      fclose(pmapFile);
232 >      if (!(pmapFile = fopen(leafFname, "rb"))) {
233 >         sprintf(errmsg, "cannot open leaf file %s", leafFname);
234 >         error(SYSTEM, errmsg);
235 >      }
236 > #endif
237 >            
238 >      /* Load photons */      
239 >      while (pm.numPhotons-- > 0) {
240 > #ifdef PMAP_OOC
241 >         /* Get entire photon record
242 >            !!! OOC PMAP FILES CURRENTLY DON'T USE PORTABLE I/O !!! */
243 >         if (!fread(&p, sizeof(p), 1, pmapFile)) {
244 >            sprintf(errmsg, "error reading OOC leaf file %s", leafFname);
245 >            error(SYSTEM, errmsg);
246 >         }
247 > #else        
248           /* Get photon position */            
249           for (j = 0; j < 3; j++)
250              p.pos [j] = getflt(pmapFile);
251 <
251 > #endif
252           /* Dump photon probabilistically acc. to target sphere count */
253           if (frandom() <= dumpRatio) {
254              printf(radDefs [ptype].obj, p.pos [0], p.pos [1], p.pos [2], rad);
255              fputc('\n', stdout);
256           }
257          
258 + #ifndef PMAP_OOC
259           /* Skip photon normal and flux */
260           for (j = 0; j < 3; j++)
261              getint(sizeof(p.norm [j]), pmapFile);
262              
263 <         #ifdef PMAP_FLOAT_FLUX
264 <            for (j = 0; j < 3; j++)
265 <               getflt(pmapFile);
266 <         #else      
267 <            for (j = 0; j < 4; j++)
268 <               getint(1, pmapFile);
269 <         #endif
263 > #ifdef PMAP_FLOAT_FLUX
264 >         for (j = 0; j < 3; j++)
265 >            getflt(pmapFile);
266 > #else      
267 >         for (j = 0; j < 4; j++)
268 >            getint(1, pmapFile);
269 > #endif
270  
271           /* Skip primary ray index */
272           getint(sizeof(p.primary), pmapFile);
273  
274           /* Skip flags */
275           getint(sizeof(p.flags), pmapFile);
276 + #endif
277          
278 <         if (feof(pmapFile)) {
278 >         if (ferror(pmapFile) || feof(pmapFile)) {
279              sprintf(errmsg, "error reading %s", argv [arg]);
280              error(USER, errmsg);
281           }
# Line 217 | Line 286 | int main (int argc, char** argv)
286        /* Reset defaults for next dump */
287        radScale = RADSCALE;
288        numSpheres = NSPHERES;
289 +      customCol [0] = customCol [1] = customCol [2] = 0;
290     }
291    
292     return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines