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.12 by rschregle, Wed Nov 21 19:30:59 2018 UTC vs.
Revision 2.17 by rschregle, Fri Aug 7 01:21:13 2020 UTC

# Line 4 | Line 4 | static const char RCSid[] = "$Id$";
4  
5   /*
6     ======================================================================
7 <   Dump photon maps as RADIANCE scene description to stdout
7 >   Dump photon maps as RADIANCE scene description or ASCII point list
8 >   to stdout
9  
10     Roland Schregle (roland.schregle@{hslu.ch, gmail.com})
11     (c) Fraunhofer Institute for Solar Energy Systems,
12 +       supported by the German Research Foundation (DFG)
13 +       under the FARESYS project.
14     (c) Lucerne University of Applied Sciences and Arts,
15 <       supported by the Swiss National Science Foundation (SNSF, #147053)
15 >       supported by the Swiss National Science Foundation (SNSF #147053).
16 >   (c) Tokyo University of Science,
17 >       supported by the JSPS KAKENHI Grant Number JP19KK0115.
18     ======================================================================
19    
20     $Id$
# Line 17 | Line 22 | static const char RCSid[] = "$Id$";
22  
23  
24  
25 + #include "pmap.h"
26   #include "pmapio.h"
21 #include "pmapparm.h"
22 #include "pmaptype.h"
27   #include "rtio.h"
28   #include "resolu.h"
29   #include "random.h"
30   #include "math.h"
31  
28 #define PMAPDUMP_REC "$Revision$"  
32  
30
33   /* Defaults */
34   /*    Sphere radius as fraction of avg. intersphere dist */
35   /*    Relative scale for sphere radius (fudge factor) */
# Line 36 | Line 38 | static const char RCSid[] = "$Id$";
38   #define RADSCALE 1.0
39   #define NSPHERES 10000
40  
41 + /* Format for optional ASCII output as XYZ RGB points */
42 + #define POINTFMT "%g\t%g\t%g\t%g\t%g\t%g\n"
43  
44   /* RADIANCE material and object defs for each photon type */
45   typedef struct {
46     char *mat, *obj;
47   } RadianceDef;
48  
45
49   const RadianceDef radDefs [] = {
50 <   {  "void plastic mat.global\n0\n0\n5 %f %f %f 0 0\n",
50 >   {  "void glow mat.global\n0\n0\n4 %g %g %g 0\n",
51        "mat.global sphere obj.global\n0\n0\n4 %g %g %g %g\n"
52     },
53 <   {  "void plastic mat.pglobal\n0\n0\n5 %f %f %f 0 0\n",
54 <      "mat.pglobal sphere obj.global\n0\n0\n4 %g %g %g %g\n"
53 >   {  "void glow mat.pglobal\n0\n0\n4 %g %g %g 0\n",
54 >      "mat.pglobal sphere obj.pglobal\n0\n0\n4 %g %g %g %g\n"
55     },
56 <   {  "void plastic mat.caustic\n0\n0\n5 %f %f %f 0 0\n",
56 >   {  "void glow mat.caustic\n0\n0\n4 %g %g %g 0\n",
57        "mat.caustic sphere obj.caustic\n0\n0\n4 %g %g %g %g\n"
58     },
59 <   {  "void plastic mat.volume\n0\n0\n5 %f %f %f 0 0\n",
59 >   {  "void glow mat.volume\n0\n0\n4 %g %g %g 0\n",
60        "mat.volume sphere obj.volume\n0\n0\n4 %g %g %g %g\n"
61     },
62 <   {  "void plastic mat.direct\n0\n0\n5 %f %f %f 0 0\n",
62 >   {  "void glow mat.direct\n0\n0\n4 %g %g %g 0\n",
63        "mat.direct sphere obj.direct\n0\n0\n4 %g %g %g %g\n"
64     },
65 <   {  "void plastic mat.contrib\n0\n0\n5 %f %f %f 0 0\n",
65 >   {  "void glow mat.contrib\n0\n0\n4 %g %g %g 0\n",
66        "mat.contrib sphere obj.contrib\n0\n0\n4 %g %g %g %g\n"
67     }
68   };
69  
70 +
71   /* Default colour codes are as follows:   global         = blue
72                                            precomp global = cyan
73                                            caustic        = red
# Line 71 | Line 75 | const RadianceDef radDefs [] = {
75                                            direct         = magenta
76                                            contrib        = yellow */
77   const COLOR colDefs [] = {
78 <   {0, 0, 1}, {0, 1, 1}, {1, 0, 0}, {0, 1, 0}, {1, 0, 1}, {1, 1, 0}
78 >   {0.25, 0.25, 2}, {0.1, 1, 1}, {1, 0.1, 0.1},
79 >   {0.1, 1, 0.1}, {1, 0.1, 1}, {1, 1, 0.1}
80   };
81  
82  
83 + static int setBool(char *str, unsigned pos, unsigned *var)
84 + {
85 +   switch ((str) [pos]) {
86 +      case '\0':
87 +         *var = !*var;
88 +         break;
89 +      case 'y': case 'Y': case 't': case 'T': case '+': case '1':
90 +         *var = 1;
91 +         break;
92 +      case 'n': case 'N': case 'f': case 'F': case '-': case '0':
93 +         *var = 0;
94 +         break;
95 +      default:
96 +         return 0;
97 +   }
98 +  
99 +   return 1;
100 + }
101 +
102 +
103   int main (int argc, char** argv)
104   {
105     char           format [MAXFMTLEN];
106     RREAL          rad, radScale = RADSCALE, extent, dumpRatio;
107 <   unsigned       arg, j, ptype, dim;
107 >   unsigned       arg, j, ptype, dim, fluxCol = 0, points = 0;
108     long           numSpheres = NSPHERES;
109 <   COLOR          customCol = {0, 0, 0};
109 >   COLOR          col = {0, 0, 0};
110     FILE           *pmapFile;
111     PhotonMap      pm;
112     PhotonPrimary  pri;
# Line 89 | Line 114 | int main (int argc, char** argv)
114   #ifdef PMAP_OOC
115     char           leafFname [1024];
116   #endif
117 <  
117 >
118     if (argc < 2) {
119 <      puts("Dump photon maps as RADIANCE scene description\n");
119 >      puts("Dump photon maps as RADIANCE scene description "
120 >           "or ASCII point list\n");
121        printf("Usage: %s "
122 <             "[-r radscale1] [-n nspheres1] [-c rcol1 gcol1 bcol1] pmap1 "
123 <             "[-r radscale2] [-n nspheres2] [-c rcol2 gcol2 bcol2] pmap2 "
122 >             "[-a] [-r radscale1] [-n num1] "
123 >             "[-f | -c rcol1 gcol1 bcol1] pmap1 "
124 >             "[-a] [-r radscale2] [-n num2] "
125 >             "[-f | -c rcol2 gcol2 bcol2] pmap2 "
126               "...\n", argv [0]);
127        return 1;
128     }
# Line 103 | Line 131 | int main (int argc, char** argv)
131        /* Parse options */
132        if (argv [arg][0] == '-') {
133           switch (argv [arg][1]) {
134 +            case 'a':
135 +               if (!setBool(argv [arg], 2, &points))
136 +                  error(USER, "invalid option syntax at -a");
137 +               break;
138              case 'r':
139                 if ((radScale = atof(argv [++arg])) <= 0)
140                    error(USER, "invalid radius scale");
# Line 110 | Line 142 | int main (int argc, char** argv)
142                
143              case 'n':
144                 if ((numSpheres = parseMultiplier(argv [++arg])) <= 0)
145 <                  error(USER, "invalid number of spheres");
145 >                  error(USER, "invalid number of points/spheres");
146                 break;
147                
148              case 'c':
149 +               if (fluxCol)
150 +                  error(USER, "-f and -c are mutually exclusive");
151 +              
152 +               if (badarg(argc - arg - 1, &argv [arg + 1], "fff"))
153 +                  error(USER, "invalid RGB colour");
154 +                                
155                 for (j = 0; j < 3; j++)
156 <                  if ((customCol [j] = atof(argv [++arg])) <= 0)
119 <                     error(USER, "invalid RGB colour");
156 >                  col [j] = atof(argv [++arg]);
157                 break;
158                
159 +            case 'f':
160 +               if (intens(col) > 0)
161 +                  error(USER, "-f and -c are mutually exclusive");
162 +                  
163 +               if (!setBool(argv [arg], 2, &fluxCol))
164 +                  error(USER, "invalid option syntax at -f");
165 +               break;
166 +              
167              default:
168                 sprintf(errmsg, "unknown option %s", argv [arg]);
169                 error(USER, errmsg);
# Line 127 | Line 172 | int main (int argc, char** argv)
172          
173           continue;
174        }
175 <      
176 <      /* Dump photon map */
175 >
176 >      /* Open next photon map file */
177        if (!(pmapFile = fopen(argv [arg], "rb"))) {
178           sprintf(errmsg, "can't open %s", argv [arg]);
179           error(SYSTEM, errmsg);
180        }
181 <        
181 >
182        /* Get format string */
183        strcpy(format, PMAP_FORMAT_GLOB);
184        if (checkheader(pmapFile, format, NULL) != 1) {
# Line 141 | Line 186 | int main (int argc, char** argv)
186                   argv [arg], format);
187           error(USER, errmsg);
188        }
189 <      
189 >
190        /* Identify photon map type from format string */
191        for (ptype = 0;
192             ptype < NUM_PMAP_TYPES && strcmp(pmapFormat [ptype], format);
193             ptype++);
194 <      
194 >
195        if (!validPmapType(ptype)) {
196           sprintf(errmsg, "file %s contains an unknown photon map type",
197                  argv [arg]);
# Line 156 | Line 201 | int main (int argc, char** argv)
201        /* Get file format version and check for compatibility */
202        if (strcmp(getstr(format, pmapFile), PMAP_FILEVER))      
203           error(USER, "incompatible photon map file format");
204 +
205 +      if (!points) {
206 +         /* Dump command line as comment */
207 +         fputs("# ", stdout);
208 +         printargs(argc, argv, stdout);
209 +         fputc('\n', stdout);
210 +      }
211          
212 <      /* Dump command line as comment */
213 <      fputs("# ", stdout);
214 <      printargs(argc, argv, stdout);
215 <      fputc('\n', stdout);
212 >      /* Set point/sphere colour if independent of photon flux,
213 >         output RADIANCE material def if required */
214 >      if (!fluxCol) {
215 >         if (intens(col) <= 0)
216 >            copycolor(col, colDefs [ptype]);
217 >         if (!points) {
218 >            printf(radDefs [ptype].mat, col [0], col [1], col [2]);
219 >            fputc('\n', stdout);
220 >         }
221 >      }
222        
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      
223        /* Get number of photons */
224        pm.numPhotons = getint(sizeof(pm.numPhotons), pmapFile);
225 <      
225 >
226        /* Skip avg photon flux */
227        for (j = 0; j < 3; j++)
228           getflt(pmapFile);
229 <      
229 >
230        /* Get distribution extent (min & max photon positions) */
231        for (j = 0; j < 3; j++) {
232           pm.minPos [j] = getflt(pmapFile);
# Line 204 | Line 253 | int main (int argc, char** argv)
253        rad = radScale * RADCOEFF * pow(extent / numSpheres, 1./dim);
254        
255        /* Photon dump probability to satisfy target sphere count */
256 <      dumpRatio = numSpheres < pm.numPhotons
208 <                  ? (float)numSpheres / pm.numPhotons : 1;
256 >      dumpRatio = min(1, (float)numSpheres / pm.numPhotons);
257        
258        /* Skip primary rays */
259        pm.numPrimary = getint(sizeof(pm.numPrimary), pmapFile);
# Line 215 | Line 263 | int main (int argc, char** argv)
263   #ifdef PMAP_PRIMARYDIR
264           /* Skip primary incident dir */
265           getint(sizeof(pri.dir), pmapFile);        
266 < #endif        
267 < #ifdef PMAP_PRIMARYPOS        
266 > #endif
267 > #ifdef PMAP_PRIMARYPOS
268           /* Skip primary hitpoint */
269           for (j = 0; j < 3; j++)
270              getflt(pmapFile);
# Line 226 | Line 274 | int main (int argc, char** argv)
274   #ifdef PMAP_OOC
275        /* Open leaf file with filename derived from pmap, replace pmapFile
276         * (which is currently the node file) */
277 <      strncpy(leafFname, argv [arg], 1024);
278 <      strncat(leafFname, PMAP_OOC_LEAFSUFFIX, 1024);
277 >      strncpy(leafFname, argv [arg], sizeof(leafFname) - 1);
278 >      strncat(leafFname, PMAP_OOC_LEAFSUFFIX, sizeof(leafFname) - 1);
279        fclose(pmapFile);
280        if (!(pmapFile = fopen(leafFname, "rb"))) {
281           sprintf(errmsg, "cannot open leaf file %s", leafFname);
# Line 235 | Line 283 | int main (int argc, char** argv)
283        }
284   #endif
285              
286 <      /* Load photons */      
286 >      /* Read photons */
287        while (pm.numPhotons-- > 0) {
288   #ifdef PMAP_OOC
289 <         /* Get entire photon record
289 >         /* Get entire photon record from ooC octree leaf file
290              !!! OOC PMAP FILES CURRENTLY DON'T USE PORTABLE I/O !!! */
291           if (!fread(&p, sizeof(p), 1, pmapFile)) {
292              sprintf(errmsg, "error reading OOC leaf file %s", leafFname);
293              error(SYSTEM, errmsg);
294           }
295 < #else        
296 <         /* Get photon position */            
295 > #else /* kd-tree */
296 >         /* Get photon position */
297           for (j = 0; j < 3; j++)
298              p.pos [j] = getflt(pmapFile);
299 < #endif
300 <         /* 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 */
299 >
300 >         /* Get photon normal (currently not used) */
301           for (j = 0; j < 3; j++)
302 <            getint(sizeof(p.norm [j]), pmapFile);
303 <            
304 < #ifdef PMAP_FLOAT_FLUX
302 >            p.norm [j] = getint(1, pmapFile);
303 >
304 >         /* Get photon flux */
305 >   #ifdef PMAP_FLOAT_FLUX
306           for (j = 0; j < 3; j++)
307 <            getflt(pmapFile);
308 < #else      
307 >            p.flux [j] = getflt(pmapFile);
308 >   #else
309           for (j = 0; j < 4; j++)
310 <            getint(1, pmapFile);
311 < #endif
310 >            p.flux [j] = getint(1, pmapFile);
311 >   #endif
312 >  
313 >        
314  
315           /* Skip primary ray index */
316           getint(sizeof(p.primary), pmapFile);
# Line 274 | Line 318 | int main (int argc, char** argv)
318           /* Skip flags */
319           getint(sizeof(p.flags), pmapFile);
320   #endif
321 +
322 +         /* Dump photon probabilistically acc. to target sphere count */
323 +         if (frandom() <= dumpRatio) {
324 +            if (fluxCol) {
325 +               /* Get photon flux */
326 +               getPhotonFlux(&p, col);
327 +               /* Scale by dumpRatio for energy conservation */
328 +               scalecolor(col, 1.0 / dumpRatio);
329 +            }
330 +            
331 +            if (!points) {
332 +               if (fluxCol) {
333 +                  /* Dump material def if variable (depends on flux) */
334 +                  printf(radDefs [ptype].mat, col [0], col [1], col [2]);
335 +                  fputc('\n', stdout);
336 +               }
337 +               printf(radDefs [ptype].obj, p.pos [0], p.pos [1], p.pos [2],
338 +                      rad);
339 +               fputc('\n', stdout);
340 +            }
341 +            else /* Dump as XYZ RGB point */
342 +               printf(POINTFMT, p.pos [0], p.pos [1], p.pos [2],
343 +                      col [0], col [1] ,col [2]);
344 +         }
345          
346           if (ferror(pmapFile) || feof(pmapFile)) {
347              sprintf(errmsg, "error reading %s", argv [arg]);
# Line 286 | Line 354 | int main (int argc, char** argv)
354        /* Reset defaults for next dump */
355        radScale = RADSCALE;
356        numSpheres = NSPHERES;
357 <      customCol [0] = customCol [1] = customCol [2] = 0;
357 >      col [0] = col [1] = col [2] = 0;
358 >      fluxCol = points = 0;
359     }
360    
361     return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines