ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/ies2rad.c
Revision: 2.8
Committed: Fri Jun 4 14:40:25 1993 UTC (30 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.7: +1 -0 lines
Log Message:
added math.h for atof() usage

File Contents

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