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.13 by rschregle, Thu Jan 10 17:32:39 2019 UTC vs.
Revision 2.16 by rschregle, Tue Jul 21 16:10:42 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 +       as part of the FARESYS project
14     (c) Lucerne University of Applied Sciences and Arts,
15         supported by the Swiss National Science Foundation (SNSF, #147053)
16 +   (c) Tokyo University of Science,
17 +       supported by the Japan Society for the Promotion of Science (JSPS)
18 +       under the Grants-in-Aid for Scientific Research Program (KAKENHI),
19 +       grant number JP19KK0115.
20     ======================================================================
21    
22     $Id$
# Line 17 | Line 24 | static const char RCSid[] = "$Id$";
24  
25  
26  
27 + #include "pmap.h"
28   #include "pmapio.h"
21 #include "pmapparm.h"
22 #include "pmaptype.h"
29   #include "rtio.h"
30   #include "resolu.h"
31   #include "random.h"
# Line 34 | Line 40 | static const char RCSid[] = "$Id$";
40   #define RADSCALE 1.0
41   #define NSPHERES 10000
42  
43 + /* Format for optional ASCII output as XYZ RGB points */
44 + #define POINTFMT "%g\t%g\t%g\t%g\t%g\t%g\n"
45  
46   /* RADIANCE material and object defs for each photon type */
47   typedef struct {
48     char *mat, *obj;
49   } RadianceDef;
50  
43
44 /* We use %e for the material def to preserve precision when outputting
45   photon flux */
51   const RadianceDef radDefs [] = {
52 <   {  "void glow mat.global\n0\n0\n4 %e %e %e 0\n",
52 >   {  "void glow mat.global\n0\n0\n4 %g %g %g 0\n",
53        "mat.global sphere obj.global\n0\n0\n4 %g %g %g %g\n"
54     },
55 <   {  "void glow mat.pglobal\n0\n0\n4 %e %e %e 0\n",
55 >   {  "void glow mat.pglobal\n0\n0\n4 %g %g %g 0\n",
56        "mat.pglobal sphere obj.pglobal\n0\n0\n4 %g %g %g %g\n"
57     },
58 <   {  "void glow mat.caustic\n0\n0\n4 %e %e %e 0\n",
58 >   {  "void glow mat.caustic\n0\n0\n4 %g %g %g 0\n",
59        "mat.caustic sphere obj.caustic\n0\n0\n4 %g %g %g %g\n"
60     },
61 <   {  "void glow mat.volume\n0\n0\n4 %e %e %e 0\n",
61 >   {  "void glow mat.volume\n0\n0\n4 %g %g %g 0\n",
62        "mat.volume sphere obj.volume\n0\n0\n4 %g %g %g %g\n"
63     },
64 <   {  "void glow mat.direct\n0\n0\n4 %e %e %e 0\n",
64 >   {  "void glow mat.direct\n0\n0\n4 %g %g %g 0\n",
65        "mat.direct sphere obj.direct\n0\n0\n4 %g %g %g %g\n"
66     },
67 <   {  "void glow mat.contrib\n0\n0\n4 %e %e %e 0\n",
67 >   {  "void glow mat.contrib\n0\n0\n4 %g %g %g 0\n",
68        "mat.contrib sphere obj.contrib\n0\n0\n4 %g %g %g %g\n"
69     }
70   };
71  
72 +
73   /* Default colour codes are as follows:   global         = blue
74                                            precomp global = cyan
75                                            caustic        = red
# Line 76 | Line 82 | const COLOR colDefs [] = {
82   };
83  
84  
85 + static int setBool(char *str, unsigned pos, unsigned *var)
86 + {
87 +   switch ((str) [pos]) {
88 +      case '\0':
89 +         *var = !*var;
90 +         break;
91 +      case 'y': case 'Y': case 't': case 'T': case '+': case '1':
92 +         *var = 1;
93 +         break;
94 +      case 'n': case 'N': case 'f': case 'F': case '-': case '0':
95 +         *var = 0;
96 +         break;
97 +      default:
98 +         return 0;
99 +   }
100 +  
101 +   return 1;
102 + }
103 +
104 +
105   int main (int argc, char** argv)
106   {
107     char           format [MAXFMTLEN];
108     RREAL          rad, radScale = RADSCALE, extent, dumpRatio;
109 <   unsigned       arg, j, ptype, dim, fluxCol = 0;
109 >   unsigned       arg, j, ptype, dim, fluxCol = 0, points = 0;
110     long           numSpheres = NSPHERES;
111 <   COLOR          customCol = {0, 0, 0};
111 >   COLOR          col = {0, 0, 0};
112     FILE           *pmapFile;
113     PhotonMap      pm;
114     PhotonPrimary  pri;
# Line 91 | Line 117 | int main (int argc, char** argv)
117     char           leafFname [1024];
118   #endif
119  
94   int setBool(char *str, int pos, int *var)
95   {
96      switch ((str) [pos]) {
97         case '\0':
98            *var = !*var;
99            break;
100         case 'y': case 'Y': case 't': case 'T': case '+': case '1':
101            *var = 1;
102            break;
103         case 'n': case 'N': case 'f': case 'F': case '-': case '0':
104            *var = 0;
105            break;
106         default:
107            return 0;
108      }
109      
110      return 1;
111   }
112
120     if (argc < 2) {
121 <      puts("Dump photon maps as RADIANCE scene description\n");
121 >      puts("Dump photon maps as RADIANCE scene description "
122 >           "or ASCII point list\n");
123        printf("Usage: %s "
124 <             "[-r radscale1] [-n nspheres1] [-f | -c rcol1 gcol1 bcol1] pmap1 "
125 <             "[-r radscale2] [-n nspheres2] [-f | -c rcol2 gcol2 bcol2] pmap2 "
124 >             "[-a] [-r radscale1] [-n num1] "
125 >             "[-f | -c rcol1 gcol1 bcol1] pmap1 "
126 >             "[-a] [-r radscale2] [-n num2] "
127 >             "[-f | -c rcol2 gcol2 bcol2] pmap2 "
128               "...\n", argv [0]);
129        return 1;
130     }
# Line 123 | Line 133 | int main (int argc, char** argv)
133        /* Parse options */
134        if (argv [arg][0] == '-') {
135           switch (argv [arg][1]) {
136 +            case 'a':
137 +               if (!setBool(argv [arg], 2, &points))
138 +                  error(USER, "invalid option syntax at -a");
139 +               break;
140              case 'r':
141                 if ((radScale = atof(argv [++arg])) <= 0)
142                    error(USER, "invalid radius scale");
# Line 130 | Line 144 | int main (int argc, char** argv)
144                
145              case 'n':
146                 if ((numSpheres = parseMultiplier(argv [++arg])) <= 0)
147 <                  error(USER, "invalid number of spheres");
147 >                  error(USER, "invalid number of points/spheres");
148                 break;
149                
150              case 'c':
# Line 141 | Line 155 | int main (int argc, char** argv)
155                    error(USER, "invalid RGB colour");
156                                  
157                 for (j = 0; j < 3; j++)
158 <                  customCol [j] = atof(argv [++arg]);
158 >                  col [j] = atof(argv [++arg]);
159                 break;
160                
161              case 'f':
162 <               if (intens(customCol) > 0)
162 >               if (intens(col) > 0)
163                    error(USER, "-f and -c are mutually exclusive");
164                    
165                 if (!setBool(argv [arg], 2, &fluxCol))
# Line 161 | Line 175 | int main (int argc, char** argv)
175           continue;
176        }
177  
178 <      /* Dump photon map */
178 >      /* Open next photon map file */
179        if (!(pmapFile = fopen(argv [arg], "rb"))) {
180           sprintf(errmsg, "can't open %s", argv [arg]);
181           error(SYSTEM, errmsg);
# Line 190 | Line 204 | int main (int argc, char** argv)
204        if (strcmp(getstr(format, pmapFile), PMAP_FILEVER))      
205           error(USER, "incompatible photon map file format");
206  
207 <      /* Dump command line as comment */
208 <      fputs("# ", stdout);
209 <      printargs(argc, argv, stdout);
210 <      fputc('\n', stdout);
197 <
198 <      /* Dump common material def if constant for all photons,
199 <         i.e. independent of individual flux */
200 <      if (!fluxCol) {
201 <         if (intens(customCol) > 0)
202 <            printf(radDefs [ptype].mat,
203 <                   customCol [0], customCol [1], customCol [2]);
204 <         else
205 <            printf(radDefs [ptype].mat, colDefs [ptype][0],
206 <                   colDefs [ptype][1], colDefs [ptype][2]);
207 >      if (!points) {
208 >         /* Dump command line as comment */
209 >         fputs("# ", stdout);
210 >         printargs(argc, argv, stdout);
211           fputc('\n', stdout);
212        }
213 +        
214 +      /* Set point/sphere colour if independent of photon flux,
215 +         output RADIANCE material def if required */
216 +      if (!fluxCol) {
217 +         if (intens(col) <= 0)
218 +            copycolor(col, colDefs [ptype]);
219 +         if (!points) {
220 +            printf(radDefs [ptype].mat, col [0], col [1], col [2]);
221 +            fputc('\n', stdout);
222 +         }
223 +      }
224        
225        /* Get number of photons */
226        pm.numPhotons = getint(sizeof(pm.numPhotons), pmapFile);
# Line 240 | Line 255 | int main (int argc, char** argv)
255        rad = radScale * RADCOEFF * pow(extent / numSpheres, 1./dim);
256        
257        /* Photon dump probability to satisfy target sphere count */
258 <      dumpRatio = numSpheres < pm.numPhotons
244 <                  ? (float)numSpheres / pm.numPhotons : 1;
258 >      dumpRatio = min(1, (float)numSpheres / pm.numPhotons);
259        
260        /* Skip primary rays */
261        pm.numPrimary = getint(sizeof(pm.numPrimary), pmapFile);
# Line 262 | Line 276 | int main (int argc, char** argv)
276   #ifdef PMAP_OOC
277        /* Open leaf file with filename derived from pmap, replace pmapFile
278         * (which is currently the node file) */
279 <      strncpy(leafFname, argv [arg], 1024);
280 <      strncat(leafFname, PMAP_OOC_LEAFSUFFIX, 1024);
279 >      strncpy(leafFname, argv [arg], sizeof(leafFname) - 1);
280 >      strncat(leafFname, PMAP_OOC_LEAFSUFFIX, sizeof(leafFname) - 1);
281        fclose(pmapFile);
282        if (!(pmapFile = fopen(leafFname, "rb"))) {
283           sprintf(errmsg, "cannot open leaf file %s", leafFname);
# Line 271 | Line 285 | int main (int argc, char** argv)
285        }
286   #endif
287              
288 <      /* Load photons */      
288 >      /* Read photons */
289        while (pm.numPhotons-- > 0) {
290   #ifdef PMAP_OOC
291           /* Get entire photon record from ooC octree leaf file
# Line 297 | Line 311 | int main (int argc, char** argv)
311           for (j = 0; j < 4; j++)
312              p.flux [j] = getint(1, pmapFile);
313     #endif
314 +  
315 +        
316  
317           /* Skip primary ray index */
318           getint(sizeof(p.primary), pmapFile);
# Line 308 | Line 324 | int main (int argc, char** argv)
324           /* Dump photon probabilistically acc. to target sphere count */
325           if (frandom() <= dumpRatio) {
326              if (fluxCol) {
327 <               /* Dump individual material def per photon acc. to flux */
328 <               getPhotonFlux(&p, customCol);
329 <               printf(radDefs [ptype].mat,
330 <                      customCol [0], customCol [1], customCol [2]);
315 <               fputc('\n', stdout);
327 >               /* Get photon flux */
328 >               getPhotonFlux(&p, col);
329 >               /* Scale by dumpRatio for energy conservation */
330 >               scalecolor(col, 1.0 / dumpRatio);
331              }
332              
333 <            printf(radDefs [ptype].obj, p.pos [0], p.pos [1], p.pos [2], rad);
334 <            fputc('\n', stdout);
333 >            if (!points) {
334 >               if (fluxCol) {
335 >                  /* Dump material def if variable (depends on flux) */
336 >                  printf(radDefs [ptype].mat, col [0], col [1], col [2]);
337 >                  fputc('\n', stdout);
338 >               }
339 >               printf(radDefs [ptype].obj, p.pos [0], p.pos [1], p.pos [2],
340 >                      rad);
341 >               fputc('\n', stdout);
342 >            }
343 >            else /* Dump as XYZ RGB point */
344 >               printf(POINTFMT, p.pos [0], p.pos [1], p.pos [2],
345 >                      col [0], col [1] ,col [2]);
346           }
347 <              
347 >        
348           if (ferror(pmapFile) || feof(pmapFile)) {
349              sprintf(errmsg, "error reading %s", argv [arg]);
350              error(USER, errmsg);
# Line 330 | Line 356 | int main (int argc, char** argv)
356        /* Reset defaults for next dump */
357        radScale = RADSCALE;
358        numSpheres = NSPHERES;
359 <      customCol [0] = customCol [1] = customCol [2] = 0;
360 <      fluxCol = 0;
359 >      col [0] = col [1] = col [2] = 0;
360 >      fluxCol = points = 0;
361     }
362    
363     return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines