ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/mkillum3.c
Revision: 2.5
Committed: Wed Aug 12 14:18:04 1992 UTC (32 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.4: +57 -28 lines
Log Message:
simplified interpolation of polar regions

File Contents

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