ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/pkgBSDF.c
Revision: 2.6
Committed: Sun Aug 11 00:54:12 2013 UTC (11 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2P2, rad4R2, rad4R2P1
Changes since 2.5: +5 -2 lines
Log Message:
Eliminated annoying prefix directory from BSDF names

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: pkgBSDF.c,v 2.5 2013/04/21 22:56:14 greg Exp $";
3 #endif
4 /*
5 * Take BSDF XML file and generate a referencing Radiance object
6 */
7
8 #include "rtio.h"
9 #include "paths.h"
10 #include "bsdf.h"
11
12 int do_stdout = 0; /* send Radiance object to stdout? */
13 int do_instance = 0; /* produce instance/octree pairs? */
14
15
16 /* Return appropriate suffix for Z offset */
17 static const char *
18 get_suffix(double zval)
19 {
20 return((zval > FTINY) ? "_f" : (zval < -FTINY) ? "_b" : "");
21 }
22
23
24 /* Produce a single rectangle corresponding to the given BSDF dimensions */
25 static void
26 faceBSDF(const SDData *bsp, double zval)
27 {
28 const char *sfx = get_suffix(zval);
29 /* output material */
30 printf("void BSDF m_%s%s\n", bsp->name, sfx);
31 printf("6 %g %s.xml 0 1 0 .\n", zval*1.00004, bsp->name);
32 printf("0\n0\n\n");
33 /* output surface */
34 zval *= (zval < 0) + 0.00002;
35 printf("m_%s%s polygon %s%s\n", bsp->name, sfx, bsp->name, sfx);
36 printf("0\n0\n12\n");
37 printf("\t%.6e\t%.6e\t%.6e\n",
38 -.5*bsp->dim[0], -.5*bsp->dim[1], zval);
39 printf("\t%.6e\t%.6e\t%.6e\n",
40 .5*bsp->dim[0], -.5*bsp->dim[1], zval);
41 printf("\t%.6e\t%.6e\t%.6e\n",
42 .5*bsp->dim[0], .5*bsp->dim[1], zval);
43 printf("\t%.6e\t%.6e\t%.6e\n\n",
44 -.5*bsp->dim[0], .5*bsp->dim[1], zval);
45 }
46
47
48 /* Convert BSDF (MGF) geometry to Radiance description */
49 static int
50 geomBSDF(const SDData *bsp)
51 {
52 char tmpfile[64];
53 char command[SDnameLn+64];
54 int fd;
55 /* write MGF to temp file */
56 fd = open(mktemp(strcpy(tmpfile,TEMPLATE)), O_WRONLY|O_CREAT|O_EXCL, 0600);
57 if (fd < 0) {
58 fprintf(stderr, "Cannot open temp file '%s'\n", tmpfile);
59 return(0);
60 }
61 (void)write(fd, bsp->mgf, strlen(bsp->mgf));
62 close(fd);
63 /* set up command */
64 if (do_instance) {
65 sprintf(command, "mgf2rad %s | oconv -f - > %s.oct",
66 tmpfile, bsp->name);
67 } else {
68 fflush(stdout);
69 sprintf(command, "mgf2rad %s", tmpfile);
70 }
71 if (system(command)) {
72 fprintf(stderr, "Error running: %s\n", command);
73 return(0);
74 }
75 unlink(tmpfile); /* remove temp file */
76 if (do_instance) { /* need to output instance? */
77 printf("void instance %s_g\n", bsp->name);
78 printf("1 %s.oct\n0\n0\n\n", bsp->name);
79 }
80 return(1);
81 }
82
83
84 /* Load a BSDF XML file and produce a corresponding Radiance object */
85 static int
86 cvtBSDF(char *fname)
87 {
88 int retOK;
89 SDData myBSDF;
90 char *pname, *fnbeg;
91 /* find and load the XML file */
92 retOK = strlen(fname);
93 if (retOK < 5 || strcmp(fname+retOK-4, ".xml")) {
94 fprintf(stderr, "%s: input does not end in '.xml'\n", fname);
95 return(0);
96 }
97 pname = getpath(fname, getrlibpath(), R_OK);
98 if (pname == NULL) {
99 fprintf(stderr, "%s: cannot find BSDF file\n", fname);
100 return(0);
101 }
102 fnbeg = strrchr(fname, DIRSEP);
103 if (fnbeg != NULL) /* eliminate directory */
104 fname = fnbeg+1;
105 SDclearBSDF(&myBSDF, fname);
106 if (SDreportError(SDloadFile(&myBSDF, pname), stderr))
107 return(0);
108 retOK = (myBSDF.dim[0] > FTINY) & (myBSDF.dim[1] > FTINY);
109 if (!retOK) {
110 fprintf(stderr, "%s: zero width or height\n", fname);
111 } else {
112 if (!do_stdout) {
113 char rname[SDnameLn+4];
114 strcpy(rname, myBSDF.name);
115 strcat(rname, ".rad");
116 retOK = (freopen(rname, "w", stdout) != NULL);
117 }
118 if (retOK) {
119 if (myBSDF.matn[0] && myBSDF.makr[0])
120 printf("# Material '%s' by '%s'\n\n",
121 myBSDF.matn, myBSDF.makr);
122 if (myBSDF.mgf == NULL) {
123 faceBSDF(&myBSDF, .0);
124 } else {
125 faceBSDF(&myBSDF, myBSDF.dim[2]);
126 if (myBSDF.rb != NULL)
127 faceBSDF(&myBSDF, -myBSDF.dim[2]);
128 retOK = geomBSDF(&myBSDF);
129 }
130 }
131 }
132 SDfreeBSDF(&myBSDF); /* clean up and return */
133 return(retOK);
134 }
135
136
137 /* Generate BSDF-referencing Radiance scenes, one per XML input */
138 int
139 main(int argc, char *argv[])
140 {
141 int status = 0;
142 int i;
143
144 for (i = 1; i < argc && argv[i][0] == '-'; i++)
145 switch (argv[i][1]) {
146 case 'i':
147 do_instance = 1;
148 break;
149 case 's':
150 do_stdout = 1;
151 break;
152 default:
153 goto userr;
154 }
155 if (i >= argc) {
156 fprintf(stderr, "%s: missing XML input\n", argv[0]);
157 goto userr;
158 }
159 if (i < argc-1 && do_stdout) {
160 fprintf(stderr, "%s: cannot send multiple BSDFs to stdout\n",
161 argv[0]);
162 return(1);
163 }
164 for ( ; i < argc; i++)
165 if (!cvtBSDF(argv[i])) {
166 fprintf(stderr, "%s: conversion of '%s' failed\n",
167 argv[0], argv[i]);
168 status = 1;
169 }
170 return(status);
171 userr:
172 fprintf(stderr, "Usage: %s [-i][-s] input.xml ..\n", argv[0]);
173 return(1);
174 }