ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/pkgBSDF.c
Revision: 2.2
Committed: Thu Aug 25 04:32:29 2011 UTC (12 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +11 -4 lines
Log Message:
Added search of library paths

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: pkgBSDF.c,v 2.1 2011/06/23 18:05:18 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);
57 if (fd < 0) {
58 fprintf(stderr, "Cannot open temp file '%s'\n", tmpfile);
59 return(0);
60 }
61 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;
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 SDclearBSDF(&myBSDF, fname);
103 if (SDreportEnglish(SDloadFile(&myBSDF, pname), stderr))
104 return(0);
105 retOK = (myBSDF.dim[0] > FTINY) & (myBSDF.dim[1] > FTINY);
106 if (!retOK) {
107 fprintf(stderr, "%s: zero width or height\n", fname);
108 } else {
109 if (!do_stdout) {
110 char rname[SDnameLn+4];
111 strcpy(rname, myBSDF.name);
112 strcat(rname, ".rad");
113 retOK = (freopen(rname, "w", stdout) != NULL);
114 }
115 if (retOK) {
116 if (myBSDF.mgf == NULL) {
117 faceBSDF(&myBSDF, .0);
118 } else {
119 faceBSDF(&myBSDF, myBSDF.dim[2]);
120 if (myBSDF.rb != NULL)
121 faceBSDF(&myBSDF, -myBSDF.dim[2]);
122 retOK = geomBSDF(&myBSDF);
123 }
124 }
125 }
126 SDfreeBSDF(&myBSDF); /* clean up and return */
127 return(retOK);
128 }
129
130
131 /* Generate BSDF-referencing Radiance scenes, one per XML input */
132 int
133 main(int argc, char *argv[])
134 {
135 int status = 0;
136 int i;
137
138 for (i = 1; i < argc && argv[i][0] == '-'; i++)
139 switch (argv[i][1]) {
140 case 'i':
141 do_instance = 1;
142 break;
143 case 's':
144 do_stdout = 1;
145 break;
146 default:
147 goto userr;
148 }
149 if (i >= argc) {
150 fprintf(stderr, "%s: missing XML input\n", argv[0]);
151 goto userr;
152 }
153 if (i < argc-1 && do_stdout) {
154 fprintf(stderr, "%s: cannot send multiple BSDFs to stdout\n",
155 argv[0]);
156 return(1);
157 }
158 for ( ; i < argc; i++)
159 if (!cvtBSDF(argv[i])) {
160 fprintf(stderr, "%s: conversion of '%s' failed\n",
161 argv[0], argv[i]);
162 status = 1;
163 }
164 return(status);
165 userr:
166 fprintf(stderr, "Usage: %s [-i][-s] input.xml ..\n", argv[0]);
167 return(1);
168 }