ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/mkillum3.c
Revision: 2.15
Committed: Sat Dec 16 18:35:27 2023 UTC (4 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.14: +10 -12 lines
Log Message:
perf(mkillum): Replaced brt() macro with proper one from color.h

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.15 static const char RCSid[] = "$Id: mkillum3.c,v 2.14 2016/09/15 22:34:41 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * Routines to print mkillum objects
6     */
7    
8     #include "mkillum.h"
9 greg 2.11 #include "paths.h"
10 greg 1.1
11     char DATORD[] = "RGB"; /* data ordering */
12     char DATSUF[] = ".dat"; /* data file suffix */
13     char DSTSUF[] = ".dist"; /* distribution suffix */
14     char FNCFNM[] = "illum.cal"; /* function file name */
15    
16 greg 2.14 static void compinv(COLORV *rinv, COLORV *rp, int m);
17     static void colorout(int p, COLORV *da, int n, int m, double mult, FILE *fp);
18     static void fputnum(double d, FILE *fp);
19     static void brightout(COLORV *da, int n, int m, double mult, FILE *fp);
20     static void fputeol(FILE *fp);
21     static void compavg(COLOR col, COLORV *da, int n);
22     static char * dfname(struct illum_args *il, int c);
23     static FILE * dfopen(struct illum_args *il, int c);
24 greg 1.1
25 schorsch 2.9
26     void
27     printobj( /* print out an object */
28     char *mod,
29 greg 2.14 OBJREC *obj
30 schorsch 2.9 )
31 greg 1.1 {
32 greg 2.14 int i;
33 greg 1.1
34 greg 2.3 if (issurface(obj->otype) && !strcmp(mod, VOIDID))
35     return; /* don't print void surfaces */
36 greg 1.1 printf("\n%s %s %s", mod, ofun[obj->otype].funame, obj->oname);
37     printf("\n%d", obj->oargs.nsargs);
38     for (i = 0; i < obj->oargs.nsargs; i++)
39     printf(" %s", obj->oargs.sarg[i]);
40     #ifdef IARGS
41     printf("\n%d", obj->oargs.niargs);
42     for (i = 0; i < obj->oargs.niargs; i++)
43     printf(" %d", obj->oargs.iarg[i]);
44     #else
45     printf("\n0");
46     #endif
47     printf("\n%d", obj->oargs.nfargs);
48     for (i = 0; i < obj->oargs.nfargs; i++) {
49     if (i%3 == 0)
50     putchar('\n');
51     printf(" %18.12g", obj->oargs.farg[i]);
52     }
53     putchar('\n');
54     }
55    
56    
57     char *
58 schorsch 2.9 dfname( /* return data file name */
59     struct illum_args *il,
60     int c
61     )
62 greg 1.1 {
63     char fname[MAXSTR];
64 greg 2.14 char *s;
65 greg 1.1
66     s = strcpy(fname, il->datafile);
67     s += strlen(s);
68     if (c) *s++ = c;
69     if (il->dfnum > 0) {
70     sprintf(s, "%d", il->dfnum);
71     s += strlen(s);
72     }
73     strcpy(s, DATSUF);
74     return(getpath(fname, NULL, 0));
75     }
76    
77    
78 greg 2.14 static FILE *
79 schorsch 2.9 dfopen( /* open data file */
80 greg 2.14 struct illum_args *il,
81 schorsch 2.9 int c
82     )
83 greg 1.1 {
84     char *fn;
85     FILE *fp;
86     /* get a usable file name */
87     for (fn = dfname(il, c);
88     !(il->flags & IL_DATCLB) && access(fn, F_OK) == 0;
89     fn = dfname(il, c))
90     il->dfnum++;
91     /* open it for writing */
92     if ((fp = fopen(fn, "w")) == NULL) {
93     sprintf(errmsg, "cannot open data file \"%s\"", fn);
94     error(SYSTEM, errmsg);
95     }
96     return(fp);
97     }
98    
99    
100 greg 2.14 void
101 schorsch 2.9 flatout( /* write hemispherical distribution */
102     struct illum_args *il,
103 greg 2.12 COLORV *da,
104 schorsch 2.9 int n,
105     int m,
106     FVECT u,
107     FVECT v,
108     FVECT w
109     )
110 greg 1.1 {
111 greg 2.12 COLORV *Ninv;
112 greg 1.1 FILE *dfp;
113     int i;
114    
115 greg 2.12 if ((Ninv = (COLORV *)malloc(3*m*sizeof(COLORV))) == NULL)
116 greg 2.5 error(SYSTEM, "out of memory in flatout");
117     compinv(Ninv, da, m);
118 greg 1.1 if (il->flags & IL_COLDST) {
119     printf("\n%s %s %s%s", VOIDID, ofun[PAT_CDATA].funame,
120     il->matname, DSTSUF);
121 greg 2.5 printf("\n9 red green blue");
122 greg 1.1 for (i = 0; i < 3; i++) {
123     dfp = dfopen(il, DATORD[i]);
124 greg 1.6 fprintf(dfp, "2\n%f %f %d\n%f %f %d\n",
125 greg 2.5 1.+.5/n, .5/n, n+1,
126 greg 1.6 0., 2.*PI, m+1);
127 greg 2.5 colorout(i, Ninv, 1, m, 1./il->nsamps/il->col[i], dfp);
128 greg 1.6 colorout(i, da, n, m, 1./il->nsamps/il->col[i], dfp);
129 greg 2.5 fputeol(dfp);
130 greg 1.1 fclose(dfp);
131     printf(" %s", dfname(il, DATORD[i]));
132     }
133     } else {
134     printf("\n%s %s %s%s", VOIDID, ofun[PAT_BDATA].funame,
135     il->matname, DSTSUF);
136 greg 2.5 printf("\n5 noneg");
137 greg 1.1 dfp = dfopen(il, 0);
138 greg 2.6 fprintf(dfp, "2\n%f %f %d\n%f %f %d\n",
139     1.+.5/n, .5/n, n+1,
140 greg 1.6 0., 2.*PI, m+1);
141 greg 2.15 brightout(Ninv, 1, m, 1./il->nsamps/bright(il->col), dfp);
142     brightout(da, n, m, 1./il->nsamps/bright(il->col), dfp);
143 greg 2.5 fputeol(dfp);
144 greg 1.1 fclose(dfp);
145     printf(" %s", dfname(il, 0));
146     }
147     printf("\n\t%s il_alth il_azih", FNCFNM);
148 greg 2.5 printf("\n0\n9\n");
149 greg 1.1 printf("\t%f\t%f\t%f\n", u[0], u[1], u[2]);
150     printf("\t%f\t%f\t%f\n", v[0], v[1], v[2]);
151     printf("\t%f\t%f\t%f\n", w[0], w[1], w[2]);
152     il->dfnum++;
153 greg 2.13 free(Ninv);
154 greg 1.1 }
155    
156    
157 greg 2.14 void
158 schorsch 2.9 roundout( /* write spherical distribution */
159     struct illum_args *il,
160 greg 2.12 COLORV *da,
161 schorsch 2.9 int n,
162     int m
163     )
164 greg 1.1 {
165 greg 2.12 COLORV *Ninv, *Sinv;
166 greg 1.1 FILE *dfp;
167     int i;
168    
169 greg 2.12 if ((Ninv = (COLORV *)malloc(3*m*sizeof(COLORV))) == NULL ||
170     (Sinv = (COLORV *)malloc(3*m*sizeof(COLORV))) == NULL)
171 greg 2.6 error(SYSTEM, "out of memory in roundout");
172 greg 2.5 compinv(Ninv, da, m);
173     compinv(Sinv, da+3*m*(n-1), m);
174 greg 1.1 if (il->flags & IL_COLDST) {
175     printf("\n%s %s %s%s", VOIDID, ofun[PAT_CDATA].funame,
176     il->matname, DSTSUF);
177 greg 2.5 printf("\n9 red green blue");
178 greg 1.1 for (i = 0; i < 3; i++) {
179     dfp = dfopen(il, DATORD[i]);
180 greg 1.6 fprintf(dfp, "2\n%f %f %d\n%f %f %d\n",
181 greg 2.5 1.+1./n, -1.-1./n, n+2,
182 greg 1.6 0., 2.*PI, m+1);
183 greg 2.5 colorout(i, Ninv, 1, m, 1./il->nsamps/il->col[i], dfp);
184 greg 1.6 colorout(i, da, n, m, 1./il->nsamps/il->col[i], dfp);
185 greg 2.5 colorout(i, Sinv, 1, m, 1./il->nsamps/il->col[i], dfp);
186     fputeol(dfp);
187 greg 1.1 fclose(dfp);
188     printf(" %s", dfname(il, DATORD[i]));
189     }
190     } else {
191     printf("\n%s %s %s%s", VOIDID, ofun[PAT_BDATA].funame,
192     il->matname, DSTSUF);
193 greg 2.5 printf("\n5 noneg");
194 greg 1.1 dfp = dfopen(il, 0);
195 greg 2.6 fprintf(dfp, "2\n%f %f %d\n%f %f %d\n",
196     1.+1./n, -1.-1./n, n+2,
197 greg 1.6 0., 2.*PI, m+1);
198 greg 2.15 brightout(Ninv, 1, m, 1./il->nsamps/bright(il->col), dfp);
199     brightout(da, n, m, 1./il->nsamps/bright(il->col), dfp);
200     brightout(Sinv, 1, m, 1./il->nsamps/bright(il->col), dfp);
201 greg 2.5 fputeol(dfp);
202 greg 1.1 fclose(dfp);
203     printf(" %s", dfname(il, 0));
204     }
205     printf("\n\t%s il_alt il_azi", FNCFNM);
206 greg 2.5 printf("\n0\n0\n");
207 greg 1.1 il->dfnum++;
208 greg 2.13 free(Ninv);
209     free(Sinv);
210 greg 1.2 }
211    
212    
213 greg 2.14 void
214 schorsch 2.9 illumout( /* print illum object */
215 greg 2.14 struct illum_args *il,
216 schorsch 2.9 OBJREC *ob
217     )
218 greg 1.2 {
219     double cout[3];
220    
221 greg 1.4 if (il->sampdens <= 0)
222     printf("\n%s ", VOIDID);
223     else
224     printf("\n%s%s ", il->matname, DSTSUF);
225     printf("%s %s", ofun[il->flags&IL_LIGHT?MAT_LIGHT:MAT_ILLUM].funame,
226 greg 1.2 il->matname);
227     if (il->flags & IL_LIGHT || !strcmp(il->altmat,VOIDID))
228     printf("\n0");
229     else
230     printf("\n1 %s", il->altmat);
231     if (il->flags & IL_COLAVG) {
232     cout[0] = il->col[0];
233     cout[1] = il->col[1];
234     cout[2] = il->col[2];
235     } else {
236 greg 2.15 cout[0] = cout[1] = cout[2] = bright(il->col);
237 greg 1.2 }
238 greg 1.3 printf("\n0\n3 %f %f %f\n", cout[0], cout[1], cout[2]);
239 greg 1.2
240     printobj(il->matname, ob);
241 greg 1.1 }
242    
243    
244 greg 2.14 static void
245 schorsch 2.9 compavg( /* compute average for set of data values */
246 greg 2.12 COLOR col,
247 greg 2.14 COLORV *da,
248 schorsch 2.9 int n
249     )
250 greg 1.1 {
251 greg 2.14 int i;
252 greg 1.1
253 greg 2.12 setcolor(col, 0.0, 0.0, 0.0);
254 greg 1.1 i = n;
255     while (i-- > 0) {
256 greg 2.12 addcolor(col, da);
257     da += 3;
258 greg 1.1 }
259 greg 2.12 scalecolor(col, 1./(double)n);
260 greg 2.4 }
261 greg 1.5
262 greg 2.4
263 greg 2.14 static void
264 schorsch 2.9 compinv( /* compute other side of row average */
265 greg 2.14 COLORV *rinv,
266     COLORV *rp,
267 schorsch 2.9 int m
268     )
269 greg 2.5 {
270 greg 2.12 COLOR avg;
271 greg 2.5
272     compavg(avg, rp, m); /* row average */
273     while (m-- > 0) {
274     *rinv++ = 2.*avg[0] - *rp++;
275     *rinv++ = 2.*avg[1] - *rp++;
276     *rinv++ = 2.*avg[2] - *rp++;
277     }
278     }
279    
280    
281 greg 2.14 int
282 schorsch 2.9 average( /* evaluate average value for distribution */
283 greg 2.14 struct illum_args *il,
284 greg 2.12 COLORV *da,
285 schorsch 2.9 int n
286     )
287 greg 2.4 {
288 greg 2.5 compavg(il->col, da, n); /* average */
289     if (il->nsamps > 1) {
290     il->col[0] /= (double)il->nsamps;
291     il->col[1] /= (double)il->nsamps;
292     il->col[2] /= (double)il->nsamps;
293     }
294 greg 2.4 /* brighter than minimum? */
295 greg 2.15 return(bright(il->col) > il->minbrt+FTINY);
296 greg 1.1 }
297    
298    
299 greg 1.6 static int colmcnt = 0; /* count of columns written */
300    
301 greg 2.14 static void
302 schorsch 2.9 fputnum( /* put out a number to fp */
303     double d,
304     FILE *fp
305     )
306 greg 1.6 {
307 greg 2.5 if (colmcnt++ % 5 == 0)
308 greg 1.6 putc('\n', fp);
309     fprintf(fp, " %11e", d);
310     }
311    
312    
313 greg 2.14 static void
314 schorsch 2.9 fputeol( /* write end of line to fp */
315 greg 2.14 FILE *fp
316 schorsch 2.9 )
317 greg 1.6 {
318     putc('\n', fp);
319     colmcnt = 0;
320     }
321    
322    
323 greg 2.14 static void
324 schorsch 2.9 colorout( /* put out color distribution data */
325     int p,
326 greg 2.14 COLORV *da,
327 schorsch 2.9 int n,
328     int m,
329     double mult,
330     FILE *fp
331     )
332 greg 1.1 {
333 greg 2.14 int i, j;
334 greg 1.1
335     for (i = 0; i < n; i++) {
336 greg 1.6 for (j = 0; j < m; j++) {
337     fputnum(mult*da[p], fp);
338     da += 3;
339     }
340     fputnum(mult*da[p-3*m], fp); /* wrap phi */
341 greg 1.1 }
342     }
343    
344    
345 greg 2.14 static void
346 schorsch 2.9 brightout( /* put out brightness distribution data */
347 greg 2.14 COLORV *da,
348 schorsch 2.9 int n,
349     int m,
350     double mult,
351     FILE *fp
352     )
353 greg 1.1 {
354 greg 2.14 int i, j;
355 greg 1.1
356     for (i = 0; i < n; i++) {
357 greg 1.6 for (j = 0; j < m; j++) {
358 greg 2.15 fputnum(mult*bright(da), fp);
359 greg 1.6 da += 3;
360     }
361 greg 2.15 fputnum(mult*bright(da-3*m), fp); /* wrap phi */
362 greg 1.1 }
363     }