1 |
/* Copyright (c) 1990 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Convert IES luminaire data to Radiance description |
9 |
* |
10 |
* 07Apr90 Greg Ward |
11 |
*/ |
12 |
|
13 |
#include <stdio.h> |
14 |
#include <ctype.h> |
15 |
#include "color.h" |
16 |
|
17 |
#define PI 3.14159265358979323846 |
18 |
/* floating comparisons */ |
19 |
#define FTINY 1e-6 |
20 |
#define FEQ(a,b) ((a)<=(b)+FTINY&&(a)>=(b)-FTINY) |
21 |
/* tilt specs */ |
22 |
#define TLTSTR "TILT=" |
23 |
#define TLTSTRLEN 5 |
24 |
#define TLTNONE "NONE" |
25 |
#define TLTINCL "INCLUDE" |
26 |
#define TLT_VERT 1 |
27 |
#define TLT_H0 2 |
28 |
#define TLT_H90 3 |
29 |
/* photometric types */ |
30 |
#define PM_C 1 |
31 |
#define PM_B 2 |
32 |
/* unit types */ |
33 |
#define U_FEET 1 |
34 |
#define U_METERS 2 |
35 |
/* string lengths */ |
36 |
#define MAXLINE 132 |
37 |
#define MAXWORD 76 |
38 |
#define MAXPATH 128 |
39 |
/* file types */ |
40 |
#define T_RAD ".rad" |
41 |
#define T_DST ".dat" |
42 |
#define T_TLT "+.dat" |
43 |
/* shape types */ |
44 |
#define RECT 1 |
45 |
#define DISK 2 |
46 |
#define SPHERE 3 |
47 |
|
48 |
#define MINDIM .001 /* minimum dimension (point source) */ |
49 |
|
50 |
#define F_M .3048 /* feet to meters */ |
51 |
|
52 |
#define abspath(p) ((p)[0] == '/' || (p)[0] == '.') |
53 |
|
54 |
static char default_name[] = "default"; |
55 |
|
56 |
char *libdir = NULL; /* library directory location */ |
57 |
char *prefdir = NULL; /* subdirectory */ |
58 |
char *lampdat = "lamp.tab"; /* lamp data file */ |
59 |
|
60 |
double meters2out = 1.0; /* conversion from meters to output */ |
61 |
char *lamptype = NULL; /* selected lamp type */ |
62 |
char *deflamp = NULL; /* default lamp type */ |
63 |
float defcolor[3] = {1.,1.,1.}; /* default lamp color */ |
64 |
float *lampcolor = defcolor; /* pointer to current lamp color */ |
65 |
double multiplier = 1.0; /* multiplier for all light sources */ |
66 |
char units[64] = "meters"; /* output units */ |
67 |
double minaspect = 0.0; /* minimum allowed aspect ratio */ |
68 |
int maxemitters = 1; /* maximum emitters per hemisphere */ |
69 |
double illumrad = 0.0; /* radius for illum sphere */ |
70 |
|
71 |
typedef struct { |
72 |
int type; /* RECT, DISK, SPHERE */ |
73 |
double w, l, h; /* width, length, height */ |
74 |
double area; /* max. projected area */ |
75 |
} SHAPE; /* a source shape */ |
76 |
|
77 |
int gargc; /* global argc (minus filenames) */ |
78 |
char **gargv; /* global argv */ |
79 |
|
80 |
extern char *strcpy(), *strcat(), *stradd(), *tailtrunc(), *filetrunc(), |
81 |
*filename(), *libname(), *fullname(), *malloc(); |
82 |
extern float *matchlamp(); |
83 |
|
84 |
|
85 |
main(argc, argv) |
86 |
int argc; |
87 |
char *argv[]; |
88 |
{ |
89 |
char *outfile = NULL; |
90 |
int status; |
91 |
char outname[MAXWORD]; |
92 |
double d1; |
93 |
int i; |
94 |
|
95 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
96 |
switch (argv[i][1]) { |
97 |
case 'd': /* dimensions */ |
98 |
if (argv[i][2] == '\0') |
99 |
goto badopt; |
100 |
if (argv[i][3] == '\0') |
101 |
d1 = 1.0; |
102 |
else if (argv[i][3] == '/') { |
103 |
d1 = atof(argv[i]+4); |
104 |
if (d1 <= FTINY) |
105 |
goto badopt; |
106 |
} else |
107 |
goto badopt; |
108 |
switch (argv[i][2]) { |
109 |
case 'c': /* centimeters */ |
110 |
if (FEQ(d1,10.)) |
111 |
strcpy(units,"millimeters"); |
112 |
else { |
113 |
strcpy(units,"centimeters"); |
114 |
strcat(units,argv[i]+3); |
115 |
} |
116 |
meters2out = 100.*d1; |
117 |
break; |
118 |
case 'm': /* meters */ |
119 |
if (FEQ(d1,1000.)) |
120 |
strcpy(units,"millimeters"); |
121 |
else if (FEQ(d1,100.)) |
122 |
strcpy(units,"centimeters"); |
123 |
else { |
124 |
strcpy(units,"meters"); |
125 |
strcat(units,argv[i]+3); |
126 |
} |
127 |
meters2out = d1; |
128 |
break; |
129 |
case 'i': /* inches */ |
130 |
strcpy(units,"inches"); |
131 |
strcat(units,argv[i]+3); |
132 |
meters2out = d1*(12./F_M); |
133 |
break; |
134 |
case 'f': /* feet */ |
135 |
if (FEQ(d1,12.)) |
136 |
strcpy(units,"inches"); |
137 |
else { |
138 |
strcpy(units,"feet"); |
139 |
strcat(units,argv[i]+3); |
140 |
} |
141 |
meters2out = d1/F_M; |
142 |
break; |
143 |
default: |
144 |
goto badopt; |
145 |
} |
146 |
break; |
147 |
case 'l': /* library directory */ |
148 |
libdir = argv[++i]; |
149 |
break; |
150 |
case 'p': /* prefix subdirectory */ |
151 |
prefdir = argv[++i]; |
152 |
break; |
153 |
case 'f': /* lamp data file */ |
154 |
lampdat = argv[++i]; |
155 |
break; |
156 |
case 'o': /* output file name */ |
157 |
outfile = argv[++i]; |
158 |
break; |
159 |
case 's': /* square emitters */ |
160 |
minaspect = .6; |
161 |
if (argv[i][2] == '/') { |
162 |
maxemitters = atoi(argv[i]+3); |
163 |
if (maxemitters < 1) |
164 |
goto badopt; |
165 |
} |
166 |
break; |
167 |
case 'i': /* illum */ |
168 |
illumrad = atof(argv[++i]); |
169 |
if (illumrad < MINDIM) |
170 |
illumrad = MINDIM; |
171 |
break; |
172 |
case 't': /* override lamp type */ |
173 |
lamptype = argv[++i]; |
174 |
break; |
175 |
case 'u': /* default lamp type */ |
176 |
deflamp = argv[++i]; |
177 |
break; |
178 |
case 'c': /* default lamp color */ |
179 |
defcolor[0] = atof(argv[++i]); |
180 |
defcolor[1] = atof(argv[++i]); |
181 |
defcolor[2] = atof(argv[++i]); |
182 |
break; |
183 |
case 'm': /* multiplier */ |
184 |
multiplier = atof(argv[++i]); |
185 |
break; |
186 |
default: |
187 |
badopt: |
188 |
fprintf(stderr, "%s: bad option: %s\n", |
189 |
argv[0], argv[i]); |
190 |
exit(1); |
191 |
} |
192 |
gargc = i; |
193 |
gargv = argv; |
194 |
initlamps(); /* get lamp data (if needed) */ |
195 |
/* convert ies file(s) */ |
196 |
if (outfile != NULL) { |
197 |
if (i == argc) |
198 |
exit(ies2rad(NULL, outfile) == 0 ? 0 : 1); |
199 |
else if (i == argc-1) |
200 |
exit(ies2rad(argv[i], outfile) == 0 ? 0 : 1); |
201 |
else { |
202 |
fprintf(stderr, "%s: single input file required\n", |
203 |
argv[0]); |
204 |
exit(1); |
205 |
} |
206 |
} else if (i >= argc) { |
207 |
fprintf(stderr, "%s: missing output file specification\n", |
208 |
argv[0]); |
209 |
exit(1); |
210 |
} |
211 |
status = 0; |
212 |
for ( ; i < argc; i++) { |
213 |
tailtrunc(strcpy(outname,filename(argv[i]))); |
214 |
if (ies2rad(argv[i], outname) != 0) |
215 |
status = 1; |
216 |
} |
217 |
exit(status); |
218 |
} |
219 |
|
220 |
|
221 |
initlamps() /* set up lamps */ |
222 |
{ |
223 |
float *lcol; |
224 |
int status; |
225 |
|
226 |
if (lamptype != NULL && !strcmp(lamptype, default_name) && |
227 |
deflamp == NULL) |
228 |
return; /* no need for data */ |
229 |
/* else load file */ |
230 |
if ((status = loadlamps(lampdat)) < 0) |
231 |
exit(1); |
232 |
if (status == 0) { |
233 |
fprintf(stderr, "%s: warning - no lamp data\n", lampdat); |
234 |
lamptype = default_name; |
235 |
return; |
236 |
} |
237 |
if (deflamp != NULL) { /* match default type */ |
238 |
if ((lcol = matchlamp(deflamp)) == NULL) |
239 |
fprintf(stderr, |
240 |
"%s: warning - unknown default lamp type\n", |
241 |
deflamp); |
242 |
else |
243 |
copycolor(defcolor, lcol); |
244 |
} |
245 |
if (lamptype != NULL) { /* match selected type */ |
246 |
if (strcmp(lamptype, default_name)) { |
247 |
if ((lcol = matchlamp(lamptype)) == NULL) { |
248 |
fprintf(stderr, |
249 |
"%s: warning - unknown lamp type\n", |
250 |
lamptype); |
251 |
lamptype = default_name; |
252 |
} else |
253 |
copycolor(defcolor, lcol); |
254 |
} |
255 |
freelamps(); /* all done with data */ |
256 |
} |
257 |
/* else keep lamp data */ |
258 |
} |
259 |
|
260 |
|
261 |
char * |
262 |
stradd(dst, src, sep) /* add a string at dst */ |
263 |
register char *dst, *src; |
264 |
int sep; |
265 |
{ |
266 |
if (src && *src) { |
267 |
do |
268 |
*dst++ = *src++; |
269 |
while (*src); |
270 |
if (sep && dst[-1] != sep) |
271 |
*dst++ = sep; |
272 |
} |
273 |
*dst = '\0'; |
274 |
return(dst); |
275 |
} |
276 |
|
277 |
|
278 |
char * |
279 |
fullname(path, fname, suffix) /* return full path name */ |
280 |
char *path, *fname, *suffix; |
281 |
{ |
282 |
if (prefdir != NULL && abspath(prefdir)) |
283 |
libname(path, fname, suffix); |
284 |
else if (abspath(fname)) |
285 |
strcpy(stradd(path, fname, 0), suffix); |
286 |
else |
287 |
libname(stradd(path, libdir, '/'), fname, suffix); |
288 |
|
289 |
return(path); |
290 |
} |
291 |
|
292 |
|
293 |
char * |
294 |
libname(path, fname, suffix) /* return library relative name */ |
295 |
char *path, *fname, *suffix; |
296 |
{ |
297 |
if (abspath(fname)) |
298 |
strcpy(stradd(path, fname, 0), suffix); |
299 |
else |
300 |
strcpy(stradd(stradd(path, prefdir, '/'), fname, 0), suffix); |
301 |
|
302 |
return(path); |
303 |
} |
304 |
|
305 |
|
306 |
char * |
307 |
filename(path) /* get final component of pathname */ |
308 |
register char *path; |
309 |
{ |
310 |
register char *cp; |
311 |
|
312 |
for (cp = path; *path; path++) |
313 |
if (*path == '/') |
314 |
cp = path+1; |
315 |
return(cp); |
316 |
} |
317 |
|
318 |
|
319 |
char * |
320 |
filetrunc(path) /* truncate filename at end of path */ |
321 |
char *path; |
322 |
{ |
323 |
register char *p1, *p2; |
324 |
|
325 |
for (p1 = p2 = path; *p2; p2++) |
326 |
if (*p2 == '/') |
327 |
p1 = p2; |
328 |
*p1 = '\0'; |
329 |
return(path); |
330 |
} |
331 |
|
332 |
|
333 |
char * |
334 |
tailtrunc(name) /* truncate tail of filename */ |
335 |
char *name; |
336 |
{ |
337 |
register char *p1, *p2; |
338 |
|
339 |
for (p1 = filename(name); *p1 == '.'; p1++) |
340 |
; |
341 |
p2 = NULL; |
342 |
for ( ; *p1; p1++) |
343 |
if (*p1 == '.') |
344 |
p2 = p1; |
345 |
if (p2 != NULL) |
346 |
*p2 = '\0'; |
347 |
return(name); |
348 |
} |
349 |
|
350 |
|
351 |
blanktrunc(s) /* truncate spaces at end of line */ |
352 |
char *s; |
353 |
{ |
354 |
register char *cp; |
355 |
|
356 |
for (cp = s; *cp; cp++) |
357 |
; |
358 |
while (cp-- > s && isspace(*cp)) |
359 |
; |
360 |
*++cp = '\0'; |
361 |
} |
362 |
|
363 |
|
364 |
putheader(out) /* print header */ |
365 |
FILE *out; |
366 |
{ |
367 |
register int i; |
368 |
|
369 |
putc('#', out); |
370 |
for (i = 0; i < gargc; i++) { |
371 |
putc(' ', out); |
372 |
fputs(gargv[i], out); |
373 |
} |
374 |
fputs("\n# Dimensions in ", out); |
375 |
fputs(units, out); |
376 |
putc('\n', out); |
377 |
} |
378 |
|
379 |
|
380 |
ies2rad(inpname, outname) /* convert IES file */ |
381 |
char *inpname, *outname; |
382 |
{ |
383 |
char buf[MAXLINE], tltid[MAXWORD]; |
384 |
FILE *inpfp, *outfp; |
385 |
|
386 |
if (inpname == NULL) { |
387 |
inpname = "<stdin>"; |
388 |
inpfp = stdin; |
389 |
} else if ((inpfp = fopen(inpname, "r")) == NULL) { |
390 |
perror(inpname); |
391 |
return(-1); |
392 |
} |
393 |
if ((outfp = fopen(fullname(buf,outname,T_RAD), "w")) == NULL) { |
394 |
perror(buf); |
395 |
fclose(inpfp); |
396 |
return(-1); |
397 |
} |
398 |
putheader(outfp); |
399 |
if (lamptype == NULL) |
400 |
lampcolor = NULL; |
401 |
while (fgets(buf,sizeof(buf),inpfp) != NULL |
402 |
&& strncmp(buf,TLTSTR,TLTSTRLEN)) { |
403 |
blanktrunc(buf); |
404 |
if (!buf[0]) |
405 |
continue; |
406 |
fputs("#<", outfp); |
407 |
fputs(buf, outfp); |
408 |
putc('\n', outfp); |
409 |
if (lampcolor == NULL) |
410 |
lampcolor = matchlamp(buf); |
411 |
} |
412 |
if (lampcolor == NULL) { |
413 |
fprintf(stderr, "%s: warning - no lamp type\n", inpname); |
414 |
lampcolor = defcolor; |
415 |
} |
416 |
if (feof(inpfp)) { |
417 |
fprintf(stderr, "%s: not in IES format\n", inpname); |
418 |
goto readerr; |
419 |
} |
420 |
sscanf(buf+TLTSTRLEN, "%s", tltid); |
421 |
if (inpfp == stdin) |
422 |
buf[0] = '\0'; |
423 |
else |
424 |
filetrunc(strcpy(buf, inpname)); |
425 |
if (dotilt(inpfp, outfp, buf, tltid, outname, tltid) != 0) { |
426 |
fprintf(stderr, "%s: bad tilt data\n", inpname); |
427 |
goto readerr; |
428 |
} |
429 |
if (dosource(inpfp, outfp, tltid, outname) != 0) { |
430 |
fprintf(stderr, "%s: bad luminaire data\n", inpname); |
431 |
goto readerr; |
432 |
} |
433 |
fclose(outfp); |
434 |
fclose(inpfp); |
435 |
return(0); |
436 |
readerr: |
437 |
fclose(outfp); |
438 |
fclose(inpfp); |
439 |
unlink(fullname(buf,outname,T_RAD)); |
440 |
return(-1); |
441 |
} |
442 |
|
443 |
|
444 |
dotilt(in, out, dir, tltspec, dfltname, tltid) /* convert tilt data */ |
445 |
FILE *in, *out; |
446 |
char *dir, *tltspec, *dfltname, *tltid; |
447 |
{ |
448 |
int nangles, tlt_type; |
449 |
double minmax[2]; |
450 |
char buf[MAXPATH], tltname[MAXWORD]; |
451 |
FILE *datin, *datout; |
452 |
|
453 |
if (!strcmp(tltspec, TLTNONE)) { |
454 |
datin = NULL; |
455 |
strcpy(tltid, "void"); |
456 |
} else if (!strcmp(tltspec, TLTINCL)) { |
457 |
datin = in; |
458 |
strcpy(tltname, dfltname); |
459 |
} else { |
460 |
if (tltspec[0] == '/') |
461 |
strcpy(buf, tltspec); |
462 |
else |
463 |
strcpy(stradd(buf, dir, '/'), tltspec); |
464 |
if ((datin = fopen(buf, "r")) == NULL) { |
465 |
perror(buf); |
466 |
return(-1); |
467 |
} |
468 |
tailtrunc(strcpy(tltname,filename(tltspec))); |
469 |
} |
470 |
if (datin != NULL) { |
471 |
if ((datout = fopen(fullname(buf,tltname,T_TLT),"w")) == NULL) { |
472 |
perror(buf); |
473 |
if (datin != in) |
474 |
fclose(datin); |
475 |
return(-1); |
476 |
} |
477 |
if (fscanf(datin, "%d %d", &tlt_type, &nangles) != 2 |
478 |
|| cvdata(datin,datout,1,&nangles,1.,minmax) != 0) { |
479 |
fprintf(stderr, "%s: data format error\n", tltspec); |
480 |
fclose(datout); |
481 |
if (datin != in) |
482 |
fclose(datin); |
483 |
unlink(fullname(buf,tltname,T_TLT)); |
484 |
return(-1); |
485 |
} |
486 |
fclose(datout); |
487 |
if (datin != in) |
488 |
fclose(datin); |
489 |
strcat(strcpy(tltid, filename(tltname)), "_tilt"); |
490 |
fprintf(out, "\nvoid brightdata %s\n", tltid); |
491 |
libname(buf,tltname,T_TLT); |
492 |
switch (tlt_type) { |
493 |
case TLT_VERT: /* vertical */ |
494 |
fprintf(out, "4 noop %s tilt.cal %s\n", buf, |
495 |
minmax[1]>90.+FTINY ? "tilt_ang" : "tilt_ang2"); |
496 |
break; |
497 |
case TLT_H0: /* horiz. in 0 deg. plane */ |
498 |
fprintf(out, "6 noop %s tilt.cal %s -rz 90\n", buf, |
499 |
minmax[1]>90.+FTINY ? "tilt_xang" : "tilt_xang2"); |
500 |
break; |
501 |
case TLT_H90: |
502 |
fprintf(out, "4 noop %s tilt.cal %s\n", buf, |
503 |
minmax[1]>90.+FTINY ? "tilt_xang" : "tilt_xang2"); |
504 |
break; |
505 |
default: |
506 |
fprintf(stderr, |
507 |
"%s: illegal lamp to luminaire geometry (%d)\n", |
508 |
tltspec, tlt_type); |
509 |
return(-1); |
510 |
} |
511 |
fprintf(out, "0\n0\n"); |
512 |
} |
513 |
return(0); |
514 |
} |
515 |
|
516 |
|
517 |
dosource(in, out, mod, name) /* create source and distribution */ |
518 |
FILE *in, *out; |
519 |
char *mod, *name; |
520 |
{ |
521 |
SHAPE srcshape; |
522 |
char buf[MAXPATH], id[MAXWORD]; |
523 |
FILE *datout; |
524 |
double mult, bfactor, pfactor, width, length, height, wattage; |
525 |
double bounds[2][2]; |
526 |
int nangles[2], pmtype, unitype; |
527 |
double d1; |
528 |
|
529 |
if (fscanf(in, "%*d %*f %lf %d %d %d %d %lf %lf %lf %lf %lf %lf", |
530 |
&mult, &nangles[0], &nangles[1], &pmtype, &unitype, |
531 |
&width, &length, &height, &bfactor, &pfactor, |
532 |
&wattage) != 11) { |
533 |
fprintf(stderr, "dosource: bad lamp specification\n"); |
534 |
return(-1); |
535 |
} |
536 |
if (nangles[0] < 2 || nangles[1] < 1) { |
537 |
fprintf(stderr, "dosource: too few measured angles\n"); |
538 |
return(-1); |
539 |
} |
540 |
if (unitype == U_FEET) { |
541 |
width *= F_M; |
542 |
length *= F_M; |
543 |
height *= F_M; |
544 |
} |
545 |
if (makeshape(&srcshape, width, length, height) != 0) { |
546 |
fprintf(stderr, "dosource: illegal source dimensions"); |
547 |
return(-1); |
548 |
} |
549 |
if ((datout = fopen(fullname(buf,name,T_DST), "w")) == NULL) { |
550 |
perror(buf); |
551 |
return(-1); |
552 |
} |
553 |
if (cvdata(in, datout, 2, nangles, 1./WHTEFFICACY, bounds) != 0) { |
554 |
fprintf(stderr, "dosource: bad distribution data\n"); |
555 |
fclose(datout); |
556 |
unlink(fullname(buf,name,T_DST)); |
557 |
return(-1); |
558 |
} |
559 |
fclose(datout); |
560 |
fprintf(out, "# %g watt luminaire, lamp*ballast factor = %g\n", |
561 |
wattage, bfactor*pfactor); |
562 |
strcat(strcpy(id, filename(name)), "_dist"); |
563 |
fprintf(out, "\n%s brightdata %s\n", mod, id); |
564 |
if (nangles[1] < 2) |
565 |
fprintf(out, "4 "); |
566 |
else if (pmtype == PM_B) |
567 |
fprintf(out, "5 "); |
568 |
else if (FEQ(bounds[1][0],90.) && FEQ(bounds[1][1],270.)) |
569 |
fprintf(out, "8 "); |
570 |
else |
571 |
fprintf(out, "6 "); |
572 |
fprintf(out, "%s %s source.cal ", |
573 |
srcshape.type==SPHERE ? "corr" : "flatcorr", |
574 |
libname(buf,name,T_DST)); |
575 |
if (pmtype == PM_B) { |
576 |
if (FEQ(bounds[1][0],0.)) |
577 |
fprintf(out, "srcB_horiz2 "); |
578 |
else |
579 |
fprintf(out, "srcB_horiz "); |
580 |
fprintf(out, "srcB_vert "); |
581 |
} else { |
582 |
if (nangles[1] >= 2) { |
583 |
d1 = bounds[1][1] - bounds[1][0]; |
584 |
if (d1 <= 90.+FTINY) |
585 |
fprintf(out, "src_phi4 "); |
586 |
else if (d1 <= 180.+FTINY) |
587 |
fprintf(out, "src_phi2 "); |
588 |
else |
589 |
fprintf(out, "src_phi "); |
590 |
fprintf(out, "src_theta -my "); |
591 |
if (FEQ(bounds[1][0],90.) && FEQ(bounds[1][1],270.)) |
592 |
fprintf(out, "-rz -90 "); |
593 |
} else |
594 |
fprintf(out, "src_theta "); |
595 |
} |
596 |
fprintf(out, "\n0\n1 %g\n", multiplier*mult*bfactor*pfactor); |
597 |
if (putsource(&srcshape, out, id, filename(name), |
598 |
bounds[0][0]<90., bounds[0][1]>90.) != 0) |
599 |
return(-1); |
600 |
return(0); |
601 |
} |
602 |
|
603 |
|
604 |
putsource(shp, fp, mod, name, dolower, doupper) /* put out source */ |
605 |
SHAPE *shp; |
606 |
FILE *fp; |
607 |
char *mod, *name; |
608 |
int dolower, doupper; |
609 |
{ |
610 |
char buf[MAXWORD]; |
611 |
|
612 |
fprintf(fp, "\n%s %s %s_light\n", mod, |
613 |
illumrad>=MINDIM/2. ? "illum" : "light", |
614 |
name); |
615 |
fprintf(fp, "0\n0\n3 %g %g %g\n", |
616 |
lampcolor[0]/shp->area, |
617 |
lampcolor[1]/shp->area, |
618 |
lampcolor[2]/shp->area); |
619 |
if (doupper && dolower && shp->type != SPHERE && shp->h > MINDIM) { |
620 |
fprintf(fp, "\n%s glow %s_glow\n", mod, name); |
621 |
fprintf(fp, "0\n0\n4 %g %g %g 0\n", |
622 |
lampcolor[0]/shp->area, |
623 |
lampcolor[1]/shp->area, |
624 |
lampcolor[2]/shp->area); |
625 |
} |
626 |
switch (shp->type) { |
627 |
case RECT: |
628 |
strcat(strcpy(buf, name), "_light"); |
629 |
if (dolower) |
630 |
putrectsrc(shp, fp, buf, name, 0); |
631 |
if (doupper) |
632 |
putrectsrc(shp, fp, buf, name, 1); |
633 |
if (doupper && dolower && shp->h > MINDIM) { |
634 |
strcat(strcpy(buf, name), "_glow"); |
635 |
putsides(shp, fp, buf, name); |
636 |
} |
637 |
break; |
638 |
case DISK: |
639 |
strcat(strcpy(buf, name), "_light"); |
640 |
if (dolower) |
641 |
putdisksrc(shp, fp, buf, name, 0); |
642 |
if (doupper) |
643 |
putdisksrc(shp, fp, buf, name, 1); |
644 |
if (doupper && dolower && shp->h > MINDIM) { |
645 |
strcat(strcpy(buf, name), "_glow"); |
646 |
putcyl(shp, fp, buf, name); |
647 |
} |
648 |
break; |
649 |
case SPHERE: |
650 |
strcat(strcpy(buf, name), "_light"); |
651 |
putspheresrc(shp, fp, buf, name); |
652 |
break; |
653 |
} |
654 |
return(0); |
655 |
} |
656 |
|
657 |
|
658 |
makeshape(shp, width, length, height) /* make source shape */ |
659 |
register SHAPE *shp; |
660 |
double width, length, height; |
661 |
{ |
662 |
if (illumrad >= MINDIM/2.) { |
663 |
shp->type = SPHERE; |
664 |
shp->w = shp->l = shp->h = 2.*illumrad; |
665 |
} else if (width < MINDIM) { |
666 |
width = -width; |
667 |
if (width < MINDIM) { |
668 |
shp->type = SPHERE; |
669 |
shp->w = shp->l = shp->h = MINDIM; |
670 |
} else if (height < .5*width) { |
671 |
shp->type = DISK; |
672 |
shp->w = shp->l = width; |
673 |
if (height >= MINDIM) |
674 |
shp->h = height; |
675 |
else |
676 |
shp->h = .5*MINDIM; |
677 |
} else { |
678 |
shp->type = SPHERE; |
679 |
shp->w = shp->l = shp->h = width; |
680 |
} |
681 |
} else { |
682 |
shp->type = RECT; |
683 |
shp->w = width; |
684 |
if (length >= MINDIM) |
685 |
shp->l = length; |
686 |
else |
687 |
shp->l = MINDIM; |
688 |
if (height >= MINDIM) |
689 |
shp->h = height; |
690 |
else |
691 |
shp->h = .5*MINDIM; |
692 |
} |
693 |
switch (shp->type) { |
694 |
case RECT: |
695 |
shp->area = shp->w * shp->l; |
696 |
break; |
697 |
case DISK: |
698 |
case SPHERE: |
699 |
shp->area = PI/4. * shp->w * shp->w; |
700 |
break; |
701 |
} |
702 |
return(0); |
703 |
} |
704 |
|
705 |
|
706 |
putrectsrc(shp, fp, mod, name, up) /* rectangular source */ |
707 |
SHAPE *shp; |
708 |
FILE *fp; |
709 |
char *mod, *name; |
710 |
int up; |
711 |
{ |
712 |
if (up) |
713 |
putrect(shp, fp, mod, name, ".u", 4, 5, 7, 6); |
714 |
else |
715 |
putrect(shp, fp, mod, name, ".d", 0, 2, 3, 1); |
716 |
} |
717 |
|
718 |
|
719 |
putsides(shp, fp, mod, name) /* put out sides of box */ |
720 |
register SHAPE *shp; |
721 |
FILE *fp; |
722 |
char *mod, *name; |
723 |
{ |
724 |
putrect(shp, fp, mod, name, ".1", 0, 1, 5, 4); |
725 |
putrect(shp, fp, mod, name, ".2", 1, 3, 7, 5); |
726 |
putrect(shp, fp, mod, name, ".3", 3, 2, 6, 7); |
727 |
putrect(shp, fp, mod, name, ".4", 2, 0, 4, 6); |
728 |
} |
729 |
|
730 |
|
731 |
putrect(shp, fp, mod, name, suffix, a, b, c, d) /* put out a rectangle */ |
732 |
SHAPE *shp; |
733 |
FILE *fp; |
734 |
char *mod, *name, *suffix; |
735 |
int a, b, c, d; |
736 |
{ |
737 |
fprintf(fp, "\n%s polygon %s%s\n0\n0\n12\n", mod, name, suffix); |
738 |
putpoint(shp, fp, a); |
739 |
putpoint(shp, fp, b); |
740 |
putpoint(shp, fp, c); |
741 |
putpoint(shp, fp, d); |
742 |
} |
743 |
|
744 |
|
745 |
putpoint(shp, fp, p) /* put out a point */ |
746 |
register SHAPE *shp; |
747 |
FILE *fp; |
748 |
int p; |
749 |
{ |
750 |
static double mult[2] = {-.5, .5}; |
751 |
|
752 |
fprintf(fp, "\t%g\t%g\t%g\n", |
753 |
mult[p&1]*shp->l*meters2out, |
754 |
mult[p>>1&1]*shp->w*meters2out, |
755 |
mult[p>>2]*shp->h*meters2out); |
756 |
} |
757 |
|
758 |
|
759 |
putdisksrc(shp, fp, mod, name, up) /* put out a disk source */ |
760 |
register SHAPE *shp; |
761 |
FILE *fp; |
762 |
char *mod, *name; |
763 |
int up; |
764 |
{ |
765 |
if (up) { |
766 |
fprintf(fp, "\n%s ring %s.u\n", mod, name); |
767 |
fprintf(fp, "0\n0\n8\n"); |
768 |
fprintf(fp, "\t0 0 %g\n", .5*shp->h*meters2out); |
769 |
fprintf(fp, "\t0 0 1\n"); |
770 |
fprintf(fp, "\t0 %g\n", .5*shp->w*meters2out); |
771 |
} else { |
772 |
fprintf(fp, "\n%s ring %s.d\n", mod, name); |
773 |
fprintf(fp, "0\n0\n8\n"); |
774 |
fprintf(fp, "\t0 0 %g\n", -.5*shp->h*meters2out); |
775 |
fprintf(fp, "\t0 0 -1\n"); |
776 |
fprintf(fp, "\t0 %g\n", .5*shp->w*meters2out); |
777 |
} |
778 |
} |
779 |
|
780 |
|
781 |
putcyl(shp, fp, mod, name) /* put out a cylinder */ |
782 |
register SHAPE *shp; |
783 |
FILE *fp; |
784 |
char *mod, *name; |
785 |
{ |
786 |
fprintf(fp, "\n%s cylinder %s.c\n", mod, name); |
787 |
fprintf(fp, "0\n0\n7\n"); |
788 |
fprintf(fp, "\t0 0 %g\n", .5*shp->h*meters2out); |
789 |
fprintf(fp, "\t0 0 %g\n", -.5*shp->h*meters2out); |
790 |
fprintf(fp, "\t%g\n", .5*shp->w*meters2out); |
791 |
} |
792 |
|
793 |
|
794 |
putspheresrc(shp, fp, mod, name) /* put out a sphere source */ |
795 |
SHAPE *shp; |
796 |
FILE *fp; |
797 |
char *mod, *name; |
798 |
{ |
799 |
fprintf(fp, "\n%s sphere %s.s\n", mod, name); |
800 |
fprintf(fp, "0\n0\n4 0 0 0 %g\n", .5*shp->w*meters2out); |
801 |
} |
802 |
|
803 |
|
804 |
cvdata(in, out, ndim, npts, mult, lim) /* convert data */ |
805 |
FILE *in, *out; |
806 |
int ndim, npts[]; |
807 |
double mult, lim[][2]; |
808 |
{ |
809 |
register double *pt[4]; |
810 |
register int i, j; |
811 |
double val; |
812 |
int total; |
813 |
|
814 |
total = 1; j = 0; |
815 |
for (i = 0; i < ndim; i++) |
816 |
if (npts[i] > 1) { |
817 |
total *= npts[i]; |
818 |
j++; |
819 |
} |
820 |
fprintf(out, "%d\n", j); |
821 |
/* get coordinates */ |
822 |
for (i = 0; i < ndim; i++) { |
823 |
pt[i] = (double *)malloc(npts[i]*sizeof(double)); |
824 |
for (j = 0; j < npts[i]; j++) |
825 |
fscanf(in, "%lf", &pt[i][j]); |
826 |
if (lim != NULL) { |
827 |
lim[i][0] = pt[i][0]; |
828 |
lim[i][1] = pt[i][npts[i]-1]; |
829 |
} |
830 |
} |
831 |
/* write out in reverse */ |
832 |
for (i = ndim-1; i >= 0; i--) { |
833 |
if (npts[i] > 1) { |
834 |
for (j = 1; j < npts[i]-1; j++) |
835 |
if (!FEQ(pt[i][j]-pt[i][j-1], |
836 |
pt[i][j+1]-pt[i][j])) |
837 |
break; |
838 |
if (j == npts[i]-1) |
839 |
fprintf(out, "%g %g %d\n", pt[i][0], pt[i][j], |
840 |
npts[i]); |
841 |
else { |
842 |
fprintf(out, "0 0 %d", npts[i]); |
843 |
for (j = 0; j < npts[i]; j++) { |
844 |
if (j%4 == 0) |
845 |
putc('\n', out); |
846 |
fprintf(out, "\t%g", pt[i][j]); |
847 |
} |
848 |
putc('\n', out); |
849 |
} |
850 |
} |
851 |
free((char *)pt[i]); |
852 |
} |
853 |
for (i = 0; i < total; i++) { |
854 |
if (i%4 == 0) |
855 |
putc('\n', out); |
856 |
if (fscanf(in, "%lf", &val) != 1) |
857 |
return(-1); |
858 |
fprintf(out, "\t%g", val*mult); |
859 |
} |
860 |
putc('\n', out); |
861 |
return(0); |
862 |
} |