ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/pkgBSDF.c
Revision: 2.10
Committed: Tue Jun 3 21:31:51 2025 UTC (44 hours, 40 minutes ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.9: +2 -4 lines
Log Message:
refactor: More consistent use of global char * progname and fixargv0()

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: pkgBSDF.c,v 2.9 2024/12/08 18:33:23 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 /* 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, 0600);
56 if (fd < 0) {
57 fprintf(stderr, "Cannot open temp file '%s'\n", tmpfile);
58 return(0);
59 }
60 (void)write(fd, bsp->mgf, strlen(bsp->mgf));
61 close(fd);
62 /* set up command */
63 if (do_instance) {
64 sprintf(command, "mgf2rad -s %s | oconv -f - > %s.oct",
65 tmpfile, bsp->name);
66 } else {
67 fflush(stdout);
68 sprintf(command, "mgf2rad -s %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(char *fname)
86 {
87 int retOK;
88 SDData myBSDF;
89 char *pname, *fnbeg;
90 /* find and load the XML file */
91 retOK = strlen(fname);
92 if (retOK < 5 || strcasecmp(fname+retOK-4, ".xml")) {
93 fprintf(stderr, "%s: input does not end in '.xml'\n", fname);
94 return(0);
95 }
96 pname = getpath(fname, getrlibpath(), R_OK);
97 if (pname == NULL) {
98 fprintf(stderr, "%s: cannot find BSDF file\n", fname);
99 return(0);
100 }
101 fnbeg = strrchr(fname, DIRSEP);
102 if (fnbeg != NULL) /* eliminate directory */
103 fname = fnbeg+1;
104 SDclearBSDF(&myBSDF, fname);
105 if (SDreportError(SDloadFile(&myBSDF, pname), stderr))
106 return(0);
107 retOK = (myBSDF.dim[0] > FTINY) & (myBSDF.dim[1] > FTINY);
108 if (!retOK) {
109 fprintf(stderr, "%s: zero width or height\n", fname);
110 } else {
111 if (!do_stdout) {
112 char rname[SDnameLn+4];
113 strcpy(rname, myBSDF.name);
114 strcat(rname, ".rad");
115 retOK = (freopen(rname, "w", stdout) != NULL);
116 }
117 if (retOK) {
118 printf("# Produced by: %s %s\n", progname, fname);
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 fixargv0(argv[0]); /* sets global progname */
145
146 for (i = 1; i < argc && argv[i][0] == '-'; i++)
147 switch (argv[i][1]) {
148 case 'i':
149 do_instance = 1;
150 break;
151 case 's':
152 do_stdout = 1;
153 break;
154 default:
155 goto userr;
156 }
157 if (i >= argc) {
158 fprintf(stderr, "%s: missing XML input\n", argv[0]);
159 goto userr;
160 }
161 if (i < argc-1 && do_stdout) {
162 fprintf(stderr, "%s: cannot send multiple BSDFs to stdout\n",
163 argv[0]);
164 return(1);
165 }
166 for ( ; i < argc; i++)
167 if (!cvtBSDF(argv[i])) {
168 fprintf(stderr, "%s: conversion of '%s' failed\n",
169 argv[0], argv[i]);
170 status = 1;
171 }
172 return(status);
173 userr:
174 fprintf(stderr, "Usage: %s [-i][-s] input.xml ..\n", argv[0]);
175 return(1);
176 }