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