--- ray/src/cv/ies2rad.c 1995/04/25 21:37:14 2.12 +++ ray/src/cv/ies2rad.c 2003/06/30 14:59:11 2.19 @@ -1,17 +1,17 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: ies2rad.c,v 2.19 2003/06/30 14:59:11 schorsch Exp $"; #endif - /* * Convert IES luminaire data to Radiance description * * 07Apr90 Greg Ward + * + * Fixed correction factor for flat sources 29Oct2001 GW */ #include #include +#include #include #include "color.h" #include "paths.h" @@ -108,6 +108,7 @@ 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 */ +int out2stdout = 0; /* put out to stdout r.t. file */ int instantiate = 0; /* instantiate geometry */ double illumrad = 0.0; /* radius for illum sphere */ @@ -123,9 +124,9 @@ int gargc; /* global argc (minus filenames) */ char **gargv; /* global argv */ extern char *strcpy(), *strcat(), *stradd(), *tailtrunc(), *filetrunc(), - *filename(), *libname(), *fullname(), *malloc(), - *getword(), *atos(); + *filename(), *libname(), *fullnam(), *getword(), *atos(); extern float *matchlamp(); +extern time_t fdate(); #define scnint(fp,ip) cvtint(ip,getword(fp)) #define scnflt(fp,rp) cvtflt(rp,getword(fp)) @@ -203,9 +204,12 @@ char *argv[]; case 'f': /* lamp data file */ lampdat = argv[++i]; break; - case 'o': /* output file name */ + case 'o': /* output file root name */ outfile = argv[++i]; break; + case 's': /* output to stdout */ + out2stdout = !out2stdout; + break; case 'i': /* illum */ illumrad = atof(argv[++i]); break; @@ -241,16 +245,15 @@ char *argv[]; exit(ies2rad(NULL, outfile) == 0 ? 0 : 1); else if (i == argc-1) exit(ies2rad(argv[i], outfile) == 0 ? 0 : 1); - else { - fprintf(stderr, "%s: single input file required\n", - argv[0]); - exit(1); - } + else + goto needsingle; } else if (i >= argc) { fprintf(stderr, "%s: missing output file specification\n", argv[0]); exit(1); } + if (out2stdout && i != argc-1) + goto needsingle; status = 0; for ( ; i < argc; i++) { tailtrunc(strcpy(outname,filename(argv[i]))); @@ -258,6 +261,9 @@ char *argv[]; status = 1; } exit(status); +needsingle: + fprintf(stderr, "%s: single input file required\n", argv[0]); + exit(1); } @@ -319,7 +325,7 @@ int sep; char * -fullname(path, fname, suffix) /* return full path name */ +fullnam(path, fname, suffix) /* return full path name */ char *path, *fname, *suffix; { if (prefdir != NULL && abspath(prefdir)) @@ -464,7 +470,9 @@ char *inpname, *outname; perror(inpname); return(-1); } - if ((outfp = fopen(fullname(buf,outname,T_RAD), "w")) == NULL) { + if (out2stdout) + outfp = stdout; + else if ((outfp = fopen(fullnam(buf,outname,T_RAD), "w")) == NULL) { perror(buf); fclose(inpfp); return(-1); @@ -530,7 +538,7 @@ char *inpname, *outname; readerr: fclose(inpfp); fclose(outfp); - unlink(fullname(buf,outname,T_RAD)); + unlink(fullnam(buf,outname,T_RAD)); return(-1); } @@ -541,7 +549,7 @@ char *dir, *tltspec, *dfltname, *tltid; { int nangles, tlt_type; double minmax[2]; - char buf[MAXPATH], tltname[MAXWORD]; + char buf[PATH_MAX], tltname[MAXWORD]; FILE *datin, *datout; if (!strcmp(tltspec, TLTNONE)) { @@ -562,7 +570,7 @@ char *dir, *tltspec, *dfltname, *tltid; tailtrunc(strcpy(tltname,filename(tltspec))); } if (datin != NULL) { - if ((datout = fopen(fullname(buf,tltname,T_TLT),"w")) == NULL) { + if ((datout = fopen(fullnam(buf,tltname,T_TLT),"w")) == NULL) { perror(buf); if (datin != in) fclose(datin); @@ -574,7 +582,7 @@ char *dir, *tltspec, *dfltname, *tltid; fclose(datout); if (datin != in) fclose(datin); - unlink(fullname(buf,tltname,T_TLT)); + unlink(fullnam(buf,tltname,T_TLT)); return(-1); } fclose(datout); @@ -613,12 +621,13 @@ SRCINFO *sinf; FILE *in, *out; char *mod, *name; { - char buf[MAXPATH], id[MAXWORD]; + char buf[PATH_MAX], id[MAXWORD]; FILE *datout; double mult, bfactor, pfactor, width, length, height, wattage; double bounds[2][2]; int nangles[2], pmtype, unitype; double d1; + int doupper, dolower, dosides; if (!isint(getword(in)) || !isflt(getword(in)) || !scnflt(in,&mult) || !scnint(in,&nangles[0]) || !scnint(in,&nangles[1]) @@ -643,14 +652,14 @@ char *mod, *name; fprintf(stderr, "dosource: illegal source dimensions"); return(-1); } - if ((datout = fopen(fullname(buf,name,T_DST), "w")) == NULL) { + if ((datout = fopen(fullnam(buf,name,T_DST), "w")) == NULL) { perror(buf); return(-1); } if (cvdata(in, datout, 2, nangles, 1./WHTEFFICACY, bounds) != 0) { fprintf(stderr, "dosource: bad distribution data\n"); fclose(datout); - unlink(fullname(buf,name,T_DST)); + unlink(fullnam(buf,name,T_DST)); return(-1); } fclose(datout); @@ -666,8 +675,13 @@ char *mod, *name; fprintf(out, "7 "); else fprintf(out, "5 "); + dolower = (bounds[0][0] < 90.); + doupper = (bounds[0][1] > 90.); + dosides = (doupper & dolower && sinf->h > MINDIM); fprintf(out, "%s %s source.cal ", - sinf->type==SPHERE ? "corr" : "flatcorr", + sinf->type==SPHERE ? "corr" : + !dosides ? "flatcorr" : + sinf->type==DISK ? "cylcorr" : "boxcorr", libname(buf,name,T_DST)); if (pmtype == PM_B) { if (FEQ(bounds[1][0],0.)) @@ -675,14 +689,17 @@ char *mod, *name; else fprintf(out, "srcB_horiz "); fprintf(out, "srcB_vert "); - } else { + } else /* pmtype == PM_A */ { if (nangles[1] >= 2) { d1 = bounds[1][1] - bounds[1][0]; if (d1 <= 90.+FTINY) fprintf(out, "src_phi4 "); - else if (d1 <= 180.+FTINY) - fprintf(out, "src_phi2 "); - else + else if (d1 <= 180.+FTINY) { + if (FEQ(bounds[1][0],90.)) + fprintf(out, "src_phi2+90 "); + else + fprintf(out, "src_phi2 "); + } else fprintf(out, "src_phi "); fprintf(out, "src_theta "); if (FEQ(bounds[1][0],90.) && FEQ(bounds[1][1],270.)) @@ -690,66 +707,53 @@ char *mod, *name; } else fprintf(out, "src_theta "); } - fprintf(out, "\n0\n1 %g\n", sinf->mult); + if (!dosides || sinf->type == SPHERE) + fprintf(out, "\n0\n1 %g\n", sinf->mult/sinf->area); + else if (sinf->type == DISK) + fprintf(out, "\n0\n3 %g %g %g\n", sinf->mult, + sinf->w, sinf->h); + else + fprintf(out, "\n0\n4 %g %g %g %g\n", sinf->mult, + sinf->l, sinf->w, sinf->h); if (putsource(sinf, out, id, filename(name), - bounds[0][0]<90., bounds[0][1]>90.) != 0) + dolower, doupper, dosides) != 0) return(-1); return(0); } -putsource(shp, fp, mod, name, dolower, doupper) /* put out source */ +putsource(shp, fp, mod, name, dolower, doupper, dosides) /* put out source */ SRCINFO *shp; FILE *fp; char *mod, *name; int dolower, doupper; { - char buf[MAXWORD]; + char lname[MAXWORD]; - fprintf(fp, "\n%s %s %s_light\n", mod, - shp->isillum ? "illum" : "light", - name); + strcat(strcpy(lname, name), "_light"); + fprintf(fp, "\n%s %s %s\n", mod, + shp->isillum ? "illum" : "light", lname); fprintf(fp, "0\n0\n3 %g %g %g\n", - lampcolor[0]/shp->area, - lampcolor[1]/shp->area, - lampcolor[2]/shp->area); - if (doupper && dolower && shp->type != SPHERE && shp->h > MINDIM) - if (shp->isillum) { - fprintf(fp, "\nvoid illum %s_glow\n", name); - fprintf(fp, "0\n0\n3 0 0 0\n"); - } else { - fprintf(fp, "\n%s glow %s_glow\n", mod, name); - fprintf(fp, "0\n0\n4 %g %g %g -1\n", - lampcolor[0]/shp->area, - lampcolor[1]/shp->area, - lampcolor[2]/shp->area); - } + lampcolor[0], lampcolor[1], lampcolor[2]); switch (shp->type) { case RECT: - strcat(strcpy(buf, name), "_light"); if (dolower) - putrectsrc(shp, fp, buf, name, 0); + putrectsrc(shp, fp, lname, name, 0); if (doupper) - putrectsrc(shp, fp, buf, name, 1); - if (doupper && dolower && shp->h > MINDIM) { - strcat(strcpy(buf, name), "_glow"); - putsides(shp, fp, buf, name); - } + putrectsrc(shp, fp, lname, name, 1); + if (dosides) + putsides(shp, fp, lname, name); break; case DISK: - strcat(strcpy(buf, name), "_light"); if (dolower) - putdisksrc(shp, fp, buf, name, 0); + putdisksrc(shp, fp, lname, name, 0); if (doupper) - putdisksrc(shp, fp, buf, name, 1); - if (doupper && dolower && shp->h > MINDIM) { - strcat(strcpy(buf, name), "_glow"); - putcyl(shp, fp, buf, name); - } + putdisksrc(shp, fp, lname, name, 1); + if (dosides) + putcyl(shp, fp, lname, name); break; case SPHERE: - strcat(strcpy(buf, name), "_light"); - putspheresrc(shp, fp, buf, name); + putspheresrc(shp, fp, lname, name); break; } return(0); @@ -951,7 +955,7 @@ double mult, lim[][2]; putc('\n', out); } } - free((char *)pt[i]); + free((void *)pt[i]); } for (i = 0; i < total; i++) { if (i%4 == 0) @@ -969,13 +973,13 @@ char * getword(fp) /* scan a word from fp */ register FILE *fp; { - static char word[MAXWORD]; + static char wrd[MAXWORD]; register char *cp; register int c; while (isspace(c=getc(fp))) ; - for (cp = word; c != EOF && cp < word+MAXWORD-1; + for (cp = wrd; c != EOF && cp < wrd+MAXWORD-1; *cp++ = c, c = getc(fp)) if (isspace(c) || c == ',') { while (isspace(c)) @@ -983,31 +987,31 @@ register FILE *fp; if (c != EOF & c != ',') ungetc(c, fp); *cp = '\0'; - return(word); + return(wrd); } *cp = '\0'; - return(cp > word ? word : NULL); + return(cp > wrd ? wrd : NULL); } -cvtint(ip, word) /* convert a word to an integer */ +cvtint(ip, wrd) /* convert a word to an integer */ int *ip; -char *word; +char *wrd; { - if (word == NULL || !isint(word)) + if (wrd == NULL || !isint(wrd)) return(0); - *ip = atoi(word); + *ip = atoi(wrd); return(1); } -cvtflt(rp, word) /* convert a word to a double */ +cvtflt(rp, wrd) /* convert a word to a double */ double *rp; -char *word; +char *wrd; { - if (word == NULL || !isflt(word)) + if (wrd == NULL || !isflt(wrd)) return(0); - *rp = atof(word); + *rp = atof(wrd); return(1); } @@ -1039,8 +1043,9 @@ FILE *outfp; /* close output file upon return */ if (instantiate) { /* instantiate octree */ strcpy(cp, "| oconv - > "); cp += 12; - fullname(cp,outname,T_OCT); - if (system(buf)) { /* create octree */ + fullnam(cp,outname,T_OCT); + if (fdate(inpname) > fdate(outname) && + system(buf)) { /* create octree */ fclose(outfp); return(-1); } @@ -1054,14 +1059,16 @@ FILE *outfp; /* close output file upon return */ fprintf(outfp, "0\n0\n"); fclose(outfp); } else { /* else append to luminaire file */ - fclose(outfp); if (!FEQ(meters2out, 1.0)) { /* apply scalefactor */ sprintf(cp, "| xform -s %f ", meters2out); cp += strlen(cp); } - strcpy(cp, ">> "); /* append works for DOS? */ - cp += 3; - fullname(cp,outname,T_RAD); + if (!out2stdout) { + fclose(outfp); + strcpy(cp, ">> "); /* append works for DOS? */ + cp += 3; + fullnam(cp,outname,T_RAD); + } if (system(buf)) return(-1); }