ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/pkgBSDF.c
Revision: 2.1
Committed: Thu Jun 23 18:05:18 2011 UTC (12 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Created pkgBSDF program

File Contents

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