--- ray/src/cv/ies2rad.c 1991/11/12 17:02:02 2.1 +++ ray/src/cv/ies2rad.c 1994/05/23 10:45:41 2.11 @@ -1,4 +1,4 @@ -/* Copyright (c) 1990 Regents of the University of California */ +/* Copyright (c) 1992 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -11,8 +11,10 @@ static char SCCSid[] = "$SunId$ LBL"; */ #include +#include #include #include "color.h" +#include "paths.h" #define PI 3.14159265358979323846 /* floating comparisons */ @@ -35,11 +37,10 @@ static char SCCSid[] = "$SunId$ LBL"; /* string lengths */ #define MAXLINE 132 #define MAXWORD 76 -#define MAXPATH 128 /* file types */ #define T_RAD ".rad" #define T_DST ".dat" -#define T_TLT "+.dat" +#define T_TLT "%.dat" /* shape types */ #define RECT 1 #define DISK 2 @@ -49,7 +50,7 @@ static char SCCSid[] = "$SunId$ LBL"; #define F_M .3048 /* feet to meters */ -#define abspath(p) ((p)[0] == '/' || (p)[0] == '.') +#define abspath(p) (ISDIRSEP((p)[0]) || (p)[0] == '.') static char default_name[] = "default"; @@ -64,8 +65,6 @@ float defcolor[3] = {1.,1.,1.}; /* default lamp color float *lampcolor = defcolor; /* pointer to current lamp color */ double multiplier = 1.0; /* multiplier for all light sources */ char units[64] = "meters"; /* output units */ -double minaspect = 0.0; /* minimum allowed aspect ratio */ -int maxemitters = 1; /* maximum emitters per hemisphere */ double illumrad = 0.0; /* radius for illum sphere */ typedef struct { @@ -78,11 +77,15 @@ int gargc; /* global argc (minus filenames) */ char **gargv; /* global argv */ extern char *strcpy(), *strcat(), *stradd(), *tailtrunc(), *filetrunc(), - *filename(), *libname(), *fullname(), *malloc(); -extern double atof(); + *filename(), *libname(), *fullname(), *malloc(), + *getword(), *atos(); extern float *matchlamp(); +#define scnint(fp,ip) cvtint(ip,getword(fp)) +#define scnflt(fp,rp) cvtflt(rp,getword(fp)) +#define isint isflt /* IES allows real as integer */ + main(argc, argv) int argc; char *argv[]; @@ -157,14 +160,6 @@ char *argv[]; case 'o': /* output file name */ outfile = argv[++i]; break; - case 's': /* square emitters */ - minaspect = .6; - if (argv[i][2] == '/') { - maxemitters = atoi(argv[i]+3); - if (maxemitters < 1) - goto badopt; - } - break; case 'i': /* illum */ illumrad = atof(argv[++i]); if (illumrad < MINDIM) @@ -285,7 +280,7 @@ char *path, *fname, *suffix; else if (abspath(fname)) strcpy(stradd(path, fname, 0), suffix); else - libname(stradd(path, libdir, '/'), fname, suffix); + libname(stradd(path, libdir, DIRSEP), fname, suffix); return(path); } @@ -298,7 +293,7 @@ char *path, *fname, *suffix; if (abspath(fname)) strcpy(stradd(path, fname, 0), suffix); else - strcpy(stradd(stradd(path, prefdir, '/'), fname, 0), suffix); + strcpy(stradd(stradd(path, prefdir, DIRSEP), fname, 0), suffix); return(path); } @@ -311,7 +306,7 @@ register char *path; register char *cp; for (cp = path; *path; path++) - if (*path == '/') + if (ISDIRSEP(*path)) cp = path+1; return(cp); } @@ -324,7 +319,7 @@ char *path; register char *p1, *p2; for (p1 = p2 = path; *p2; p2++) - if (*p2 == '/') + if (ISDIRSEP(*p2)) p1 = p2; *p1 = '\0'; return(path); @@ -412,13 +407,16 @@ char *inpname, *outname; } if (lampcolor == NULL) { fprintf(stderr, "%s: warning - no lamp type\n", inpname); + fputs("# Unknown lamp type (used default)\n", outfp); lampcolor = defcolor; - } + } else if (lamptype == NULL) + fprintf(outfp,"# CIE(x,y) = (%f,%f)\n# Depreciation = %.1f%%\n", + lampcolor[3], lampcolor[4], 100.*lampcolor[5]); if (feof(inpfp)) { fprintf(stderr, "%s: not in IES format\n", inpname); goto readerr; } - sscanf(buf+TLTSTRLEN, "%s", tltid); + atos(tltid, MAXWORD, buf+TLTSTRLEN); if (inpfp == stdin) buf[0] = '\0'; else @@ -458,10 +456,10 @@ char *dir, *tltspec, *dfltname, *tltid; datin = in; strcpy(tltname, dfltname); } else { - if (tltspec[0] == '/') + if (ISDIRSEP(tltspec[0])) strcpy(buf, tltspec); else - strcpy(stradd(buf, dir, '/'), tltspec); + strcpy(stradd(buf, dir, DIRSEP), tltspec); if ((datin = fopen(buf, "r")) == NULL) { perror(buf); return(-1); @@ -475,7 +473,7 @@ char *dir, *tltspec, *dfltname, *tltid; fclose(datin); return(-1); } - if (fscanf(datin, "%d %d", &tlt_type, &nangles) != 2 + if (!scnint(datin,&tlt_type) || !scnint(datin,&nangles) || cvdata(datin,datout,1,&nangles,1.,minmax) != 0) { fprintf(stderr, "%s: data format error\n", tltspec); fclose(datout); @@ -527,10 +525,12 @@ char *mod, *name; int nangles[2], pmtype, unitype; double d1; - if (fscanf(in, "%*d %*f %lf %d %d %d %d %lf %lf %lf %lf %lf %lf", - &mult, &nangles[0], &nangles[1], &pmtype, &unitype, - &width, &length, &height, &bfactor, &pfactor, - &wattage) != 11) { + if (!isint(getword(in)) || !isflt(getword(in)) || !scnflt(in,&mult) + || !scnint(in,&nangles[0]) || !scnint(in,&nangles[1]) + || !scnint(in,&pmtype) || !scnint(in,&unitype) + || !scnflt(in,&width) || !scnflt(in,&length) + || !scnflt(in,&height) || !scnflt(in,&bfactor) + || !scnflt(in,&pfactor) || !scnflt(in,&wattage)) { fprintf(stderr, "dosource: bad lamp specification\n"); return(-1); } @@ -567,9 +567,9 @@ char *mod, *name; else if (pmtype == PM_B) fprintf(out, "5 "); else if (FEQ(bounds[1][0],90.) && FEQ(bounds[1][1],270.)) - fprintf(out, "8 "); + fprintf(out, "7 "); else - fprintf(out, "6 "); + fprintf(out, "5 "); fprintf(out, "%s %s source.cal ", srcshape.type==SPHERE ? "corr" : "flatcorr", libname(buf,name,T_DST)); @@ -588,7 +588,7 @@ char *mod, *name; fprintf(out, "src_phi2 "); else fprintf(out, "src_phi "); - fprintf(out, "src_theta -my "); + fprintf(out, "src_theta "); if (FEQ(bounds[1][0],90.) && FEQ(bounds[1][1],270.)) fprintf(out, "-rz -90 "); } else @@ -619,7 +619,7 @@ int dolower, doupper; lampcolor[2]/shp->area); if (doupper && dolower && shp->type != SPHERE && shp->h > MINDIM) { fprintf(fp, "\n%s glow %s_glow\n", mod, name); - fprintf(fp, "0\n0\n4 %g %g %g 0\n", + fprintf(fp, "0\n0\n4 %g %g %g -1\n", lampcolor[0]/shp->area, lampcolor[1]/shp->area, lampcolor[2]/shp->area); @@ -660,9 +660,9 @@ makeshape(shp, width, length, height) /* make source register SHAPE *shp; double width, length, height; { - if (illumrad >= MINDIM/2.) { + if (illumrad/meters2out >= MINDIM/2.) { shp->type = SPHERE; - shp->w = shp->l = shp->h = 2.*illumrad; + shp->w = shp->l = shp->h = 2.*illumrad / meters2out; } else if (width < MINDIM) { width = -width; if (width < MINDIM) { @@ -807,7 +807,7 @@ FILE *in, *out; int ndim, npts[]; double mult, lim[][2]; { - register double *pt[4]; + double *pt[4]; register int i, j; double val; int total; @@ -823,7 +823,8 @@ double mult, lim[][2]; for (i = 0; i < ndim; i++) { pt[i] = (double *)malloc(npts[i]*sizeof(double)); for (j = 0; j < npts[i]; j++) - fscanf(in, "%lf", &pt[i][j]); + if (!scnflt(in, &pt[i][j])) + return(-1); if (lim != NULL) { lim[i][0] = pt[i][0]; lim[i][1] = pt[i][npts[i]-1]; @@ -854,10 +855,57 @@ double mult, lim[][2]; for (i = 0; i < total; i++) { if (i%4 == 0) putc('\n', out); - if (fscanf(in, "%lf", &val) != 1) + if (!scnflt(in, &val)) return(-1); fprintf(out, "\t%g", val*mult); } putc('\n', out); return(0); +} + + +char * +getword(fp) /* scan a word from fp */ +register FILE *fp; +{ + static char word[MAXWORD]; + register char *cp; + register int c; + + while (isspace(c=getc(fp))) + ; + for (cp = word; c != EOF && cp < word+MAXWORD-1; + *cp++ = c, c = getc(fp)) + if (isspace(c) || c == ',') { + while (isspace(c)) + c = getc(fp); + if (c != EOF & c != ',') + ungetc(c, fp); + *cp = '\0'; + return(word); + } + *cp = '\0'; + return(cp > word ? word : NULL); +} + + +cvtint(ip, word) /* convert a word to an integer */ +int *ip; +char *word; +{ + if (word == NULL || !isint(word)) + return(0); + *ip = atoi(word); + return(1); +} + + +cvtflt(rp, word) /* convert a word to a double */ +double *rp; +char *word; +{ + if (word == NULL || !isflt(word)) + return(0); + *rp = atof(word); + return(1); }