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", |
126 |
1.+.5/n, .5/n, n+1, |
127 |
0., 2.*PI, m+1); |
128 |
brightout(Ninv, 1, m, 1./il->nsamps/brt(il->col), dfp); |
129 |
brightout(da, n, m, 1./il->nsamps/brt(il->col), dfp); |
130 |
fputeol(dfp); |
131 |
fclose(dfp); |
132 |
printf(" %s", dfname(il, 0)); |
133 |
} |
134 |
printf("\n\t%s il_alth il_azih", FNCFNM); |
135 |
printf("\n0\n9\n"); |
136 |
printf("\t%f\t%f\t%f\n", u[0], u[1], u[2]); |
137 |
printf("\t%f\t%f\t%f\n", v[0], v[1], v[2]); |
138 |
printf("\t%f\t%f\t%f\n", w[0], w[1], w[2]); |
139 |
il->dfnum++; |
140 |
free((char *)Ninv); |
141 |
} |
142 |
|
143 |
|
144 |
roundout(il, da, n, m) /* write spherical distribution */ |
145 |
struct illum_args *il; |
146 |
float *da; |
147 |
int n, m; |
148 |
{ |
149 |
float *Ninv, *Sinv; |
150 |
FILE *dfp; |
151 |
int i; |
152 |
|
153 |
if ((Ninv = (float *)malloc(3*m*sizeof(float))) == NULL || |
154 |
(Sinv = (float *)malloc(3*m*sizeof(float))) == NULL) |
155 |
error(SYSTEM, "out of memory in roundout"); |
156 |
compinv(Ninv, da, m); |
157 |
compinv(Sinv, da+3*m*(n-1), m); |
158 |
if (il->flags & IL_COLDST) { |
159 |
printf("\n%s %s %s%s", VOIDID, ofun[PAT_CDATA].funame, |
160 |
il->matname, DSTSUF); |
161 |
printf("\n9 red green blue"); |
162 |
for (i = 0; i < 3; i++) { |
163 |
dfp = dfopen(il, DATORD[i]); |
164 |
fprintf(dfp, "2\n%f %f %d\n%f %f %d\n", |
165 |
1.+1./n, -1.-1./n, n+2, |
166 |
0., 2.*PI, m+1); |
167 |
colorout(i, Ninv, 1, m, 1./il->nsamps/il->col[i], dfp); |
168 |
colorout(i, da, n, m, 1./il->nsamps/il->col[i], dfp); |
169 |
colorout(i, Sinv, 1, m, 1./il->nsamps/il->col[i], dfp); |
170 |
fputeol(dfp); |
171 |
fclose(dfp); |
172 |
printf(" %s", dfname(il, DATORD[i])); |
173 |
} |
174 |
} else { |
175 |
printf("\n%s %s %s%s", VOIDID, ofun[PAT_BDATA].funame, |
176 |
il->matname, DSTSUF); |
177 |
printf("\n5 noneg"); |
178 |
dfp = dfopen(il, 0); |
179 |
fprintf(dfp, "2\n%f %f %d\n%f %f %d\n", |
180 |
1.+1./n, -1.-1./n, n+2, |
181 |
0., 2.*PI, m+1); |
182 |
brightout(Ninv, 1, m, 1./il->nsamps/brt(il->col), dfp); |
183 |
brightout(da, n, m, 1./il->nsamps/brt(il->col), dfp); |
184 |
brightout(Sinv, 1, m, 1./il->nsamps/brt(il->col), dfp); |
185 |
fputeol(dfp); |
186 |
fclose(dfp); |
187 |
printf(" %s", dfname(il, 0)); |
188 |
} |
189 |
printf("\n\t%s il_alt il_azi", FNCFNM); |
190 |
printf("\n0\n0\n"); |
191 |
il->dfnum++; |
192 |
free((char *)Ninv); |
193 |
free((char *)Sinv); |
194 |
} |
195 |
|
196 |
|
197 |
illumout(il, ob) /* print illum object */ |
198 |
register struct illum_args *il; |
199 |
OBJREC *ob; |
200 |
{ |
201 |
double cout[3]; |
202 |
|
203 |
if (il->sampdens <= 0) |
204 |
printf("\n%s ", VOIDID); |
205 |
else |
206 |
printf("\n%s%s ", il->matname, DSTSUF); |
207 |
printf("%s %s", ofun[il->flags&IL_LIGHT?MAT_LIGHT:MAT_ILLUM].funame, |
208 |
il->matname); |
209 |
if (il->flags & IL_LIGHT || !strcmp(il->altmat,VOIDID)) |
210 |
printf("\n0"); |
211 |
else |
212 |
printf("\n1 %s", il->altmat); |
213 |
if (il->flags & IL_COLAVG) { |
214 |
cout[0] = il->col[0]; |
215 |
cout[1] = il->col[1]; |
216 |
cout[2] = il->col[2]; |
217 |
} else { |
218 |
cout[0] = cout[1] = cout[2] = brt(il->col); |
219 |
} |
220 |
printf("\n0\n3 %f %f %f\n", cout[0], cout[1], cout[2]); |
221 |
|
222 |
printobj(il->matname, ob); |
223 |
} |
224 |
|
225 |
|
226 |
compavg(col, da, n) /* compute average for set of data values */ |
227 |
float col[3]; |
228 |
register float *da; |
229 |
int n; |
230 |
{ |
231 |
register int i; |
232 |
|
233 |
col[0] = col[1] = col[2] = 0.; |
234 |
i = n; |
235 |
while (i-- > 0) { |
236 |
col[0] += *da++; |
237 |
col[1] += *da++; |
238 |
col[2] += *da++; |
239 |
} |
240 |
for (i = 0; i < 3; i++) |
241 |
col[i] /= (double)n; |
242 |
} |
243 |
|
244 |
|
245 |
compinv(rinv, rp, m) /* compute other side of row average */ |
246 |
register float *rinv, *rp; |
247 |
int m; |
248 |
{ |
249 |
float avg[3]; |
250 |
|
251 |
compavg(avg, rp, m); /* row average */ |
252 |
while (m-- > 0) { |
253 |
*rinv++ = 2.*avg[0] - *rp++; |
254 |
*rinv++ = 2.*avg[1] - *rp++; |
255 |
*rinv++ = 2.*avg[2] - *rp++; |
256 |
} |
257 |
} |
258 |
|
259 |
|
260 |
average(il, da, n) /* evaluate average value for distribution */ |
261 |
register struct illum_args *il; |
262 |
float *da; |
263 |
int n; |
264 |
{ |
265 |
compavg(il->col, da, n); /* average */ |
266 |
if (il->nsamps > 1) { |
267 |
il->col[0] /= (double)il->nsamps; |
268 |
il->col[1] /= (double)il->nsamps; |
269 |
il->col[2] /= (double)il->nsamps; |
270 |
} |
271 |
/* brighter than minimum? */ |
272 |
return(brt(il->col) > il->minbrt+FTINY); |
273 |
} |
274 |
|
275 |
|
276 |
static int colmcnt = 0; /* count of columns written */ |
277 |
|
278 |
fputnum(d, fp) /* put out a number to fp */ |
279 |
double d; |
280 |
FILE *fp; |
281 |
{ |
282 |
if (colmcnt++ % 5 == 0) |
283 |
putc('\n', fp); |
284 |
fprintf(fp, " %11e", d); |
285 |
} |
286 |
|
287 |
|
288 |
fputeol(fp) /* write end of line to fp */ |
289 |
register FILE *fp; |
290 |
{ |
291 |
putc('\n', fp); |
292 |
colmcnt = 0; |
293 |
} |
294 |
|
295 |
|
296 |
colorout(p, da, n, m, mult, fp) /* put out color distribution data */ |
297 |
int p; |
298 |
register float *da; |
299 |
int n, m; |
300 |
double mult; |
301 |
FILE *fp; |
302 |
{ |
303 |
register int i, j; |
304 |
|
305 |
for (i = 0; i < n; i++) { |
306 |
for (j = 0; j < m; j++) { |
307 |
fputnum(mult*da[p], fp); |
308 |
da += 3; |
309 |
} |
310 |
fputnum(mult*da[p-3*m], fp); /* wrap phi */ |
311 |
} |
312 |
} |
313 |
|
314 |
|
315 |
brightout(da, n, m, mult, fp) /* put out brightness distribution data */ |
316 |
register float *da; |
317 |
int n, m; |
318 |
double mult; |
319 |
FILE *fp; |
320 |
{ |
321 |
register int i, j; |
322 |
|
323 |
for (i = 0; i < n; i++) { |
324 |
for (j = 0; j < m; j++) { |
325 |
fputnum(mult*brt(da), fp); |
326 |
da += 3; |
327 |
} |
328 |
fputnum(mult*brt(da-3*m), fp); /* wrap phi */ |
329 |
} |
330 |
} |