ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgf2rad.c
Revision: 2.17
Committed: Mon May 1 17:05:22 1995 UTC (29 years, 6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.16: +1 -1 lines
Log Message:
bug fix in r_ies

File Contents

# Content
1 /* Copyright (c) 1994 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * Convert MGF (Materials and Geometry Format) to Radiance
9 */
10
11 #include <stdio.h>
12 #include <math.h>
13 #include <string.h>
14 #include "mgflib/parser.h"
15 #include "color.h"
16 #include "tmesh.h"
17
18 #define putv(v) printf("%18.12g %18.12g %18.12g\n",(v)[0],(v)[1],(v)[2])
19
20 #define invert (xf_context != NULL && xf_context->rev)
21
22 double glowdist = FHUGE; /* glow test distance */
23
24 double emult = 1.; /* emitter multiplier */
25
26 FILE *matfp = stdout; /* material output file */
27
28 int r_comment(), r_cone(), r_cyl(), r_face(), r_ies(), r_ring(), r_sph();
29 char *material(), *object(), *addarg();
30
31
32 main(argc, argv) /* convert files to stdout */
33 int argc;
34 char *argv[];
35 {
36 int i, rv;
37 /* initialize dispatch table */
38 mg_ehand[MG_E_COMMENT] = r_comment; /* we pass comments */
39 mg_ehand[MG_E_COLOR] = c_hcolor; /* they get color */
40 mg_ehand[MG_E_CONE] = r_cone; /* we do cones */
41 mg_ehand[MG_E_CMIX] = c_hcolor; /* they mix colors */
42 mg_ehand[MG_E_CSPEC] = c_hcolor; /* they get spectra */
43 mg_ehand[MG_E_CXY] = c_hcolor; /* they get chromaticities */
44 mg_ehand[MG_E_CCT] = c_hcolor; /* they get color temp's */
45 mg_ehand[MG_E_CYL] = r_cyl; /* we do cylinders */
46 mg_ehand[MG_E_ED] = c_hmaterial; /* they get emission */
47 mg_ehand[MG_E_FACE] = r_face; /* we do faces */
48 mg_ehand[MG_E_IES] = r_ies; /* we do IES files */
49 mg_ehand[MG_E_IR] = c_hmaterial; /* they get refractive index */
50 mg_ehand[MG_E_MATERIAL] = c_hmaterial; /* they get materials */
51 mg_ehand[MG_E_NORMAL] = c_hvertex; /* they get normals */
52 mg_ehand[MG_E_OBJECT] = obj_handler; /* they track object names */
53 mg_ehand[MG_E_POINT] = c_hvertex; /* they get points */
54 mg_ehand[MG_E_RD] = c_hmaterial; /* they get diffuse refl. */
55 mg_ehand[MG_E_RING] = r_ring; /* we do rings */
56 mg_ehand[MG_E_RS] = c_hmaterial; /* they get specular refl. */
57 mg_ehand[MG_E_SIDES] = c_hmaterial; /* they get # sides */
58 mg_ehand[MG_E_SPH] = r_sph; /* we do spheres */
59 mg_ehand[MG_E_TD] = c_hmaterial; /* they get diffuse trans. */
60 mg_ehand[MG_E_TS] = c_hmaterial; /* they get specular trans. */
61 mg_ehand[MG_E_VERTEX] = c_hvertex; /* they get vertices */
62 mg_ehand[MG_E_XF] = xf_handler; /* they track transforms */
63 mg_init(); /* initialize the parser */
64 /* get our options & print header */
65 printf("## %s", argv[0]);
66 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
67 printf(" %s", argv[i]);
68 switch (argv[i][1]) {
69 case 'g': /* glow distance (meters) */
70 if (argv[i][2] || badarg(argc-i-1, argv+i+1, "f"))
71 goto userr;
72 glowdist = atof(argv[++i]);
73 printf(" %s", argv[i]);
74 break;
75 case 'e': /* emitter multiplier */
76 if (argv[i][2] || badarg(argc-i-1, argv+i+1, "f"))
77 goto userr;
78 emult = atof(argv[++i]);
79 printf(" %s", argv[i]);
80 break;
81 case 'm': /* materials file */
82 matfp = fopen(argv[++i], "a");
83 if (matfp == NULL) {
84 fprintf(stderr, "%s: cannot append\n", argv[i]);
85 exit(1);
86 }
87 printf(" %s", argv[i]);
88 break;
89 default:
90 goto userr;
91 }
92 }
93 putchar('\n');
94 if (i == argc) { /* convert stdin */
95 if ((rv = mg_load(NULL)) != MG_OK)
96 exit(1);
97 } else /* convert each file */
98 for ( ; i < argc; i++) {
99 printf("## %s %s ##############################\n",
100 argv[0], argv[i]);
101 if ((rv = mg_load(argv[i])) != MG_OK)
102 exit(1);
103 }
104 exit(0);
105 userr:
106 fprintf(stderr, "Usage: %s [-g dist][-e mult][-m matf] [file.mgf] ..\n",
107 argv[0]);
108 exit(1);
109 }
110
111
112 int
113 r_comment(ac, av) /* repeat a comment verbatim */
114 register int ac;
115 register char **av;
116 {
117 putchar('#'); /* use Radiance comment character */
118 while (--ac) { /* pass through verbatim */
119 putchar(' ');
120 fputs(*++av, stdout);
121 }
122 putchar('\n');
123 return(MG_OK);
124 }
125
126
127 int
128 r_cone(ac, av) /* put out a cone */
129 int ac;
130 char **av;
131 {
132 static int ncones;
133 char *mat;
134 double r1, r2;
135 C_VERTEX *cv1, *cv2;
136 FVECT p1, p2;
137 int inv;
138 /* check argument count and type */
139 if (ac != 5)
140 return(MG_EARGC);
141 if (!isflt(av[2]) || !isflt(av[4]))
142 return(MG_ETYPE);
143 /* get the endpoint vertices */
144 if ((cv1 = c_getvert(av[1])) == NULL ||
145 (cv2 = c_getvert(av[3])) == NULL)
146 return(MG_EUNDEF);
147 xf_xfmpoint(p1, cv1->p); /* transform endpoints */
148 xf_xfmpoint(p2, cv2->p);
149 r1 = xf_scale(atof(av[2])); /* scale radii */
150 r2 = xf_scale(atof(av[4]));
151 inv = r1 < 0.; /* check for inverted cone */
152 if (r1 == 0.) { /* check for illegal radii */
153 if (r2 == 0.)
154 return(MG_EILL);
155 inv = r2 < 0.;
156 } else if (r2 != 0. && inv ^ r2 < 0.)
157 return(MG_EILL);
158 if (inv) {
159 r1 = -r1;
160 r2 = -r2;
161 }
162 if ((mat = material()) == NULL) /* get material */
163 return(MG_EBADMAT);
164 /* spit the sucker out */
165 printf("\n%s %s %sc%d\n", mat, inv ? "cup" : "cone",
166 object(), ++ncones);
167 printf("0\n0\n8\n");
168 putv(p1);
169 putv(p2);
170 printf("%18.12g %18.12g\n", r1, r2);
171 return(MG_OK);
172 }
173
174
175 int
176 r_cyl(ac, av) /* put out a cylinder */
177 int ac;
178 char **av;
179 {
180 static int ncyls;
181 char *mat;
182 double rad;
183 C_VERTEX *cv1, *cv2;
184 FVECT p1, p2;
185 int inv;
186 /* check argument count and type */
187 if (ac != 4)
188 return(MG_EARGC);
189 if (!isflt(av[2]))
190 return(MG_ETYPE);
191 /* get the endpoint vertices */
192 if ((cv1 = c_getvert(av[1])) == NULL ||
193 (cv2 = c_getvert(av[3])) == NULL)
194 return(MG_EUNDEF);
195 xf_xfmpoint(p1, cv1->p); /* transform endpoints */
196 xf_xfmpoint(p2, cv2->p);
197 rad = xf_scale(atof(av[2])); /* scale radius */
198 if ((inv = rad < 0.)) /* check for inverted cylinder */
199 rad = -rad;
200 if ((mat = material()) == NULL) /* get material */
201 return(MG_EBADMAT);
202 /* spit out the primitive */
203 printf("\n%s %s %scy%d\n", mat, inv ? "tube" : "cylinder",
204 object(), ++ncyls);
205 printf("0\n0\n7\n");
206 putv(p1);
207 putv(p2);
208 printf("%18.12g\n", rad);
209 return(MG_OK);
210 }
211
212
213 int
214 r_sph(ac, av) /* put out a sphere */
215 int ac;
216 char **av;
217 {
218 static int nsphs;
219 char *mat;
220 double rad;
221 C_VERTEX *cv;
222 FVECT cent;
223 int inv;
224 /* check argument count and type */
225 if (ac != 3)
226 return(MG_EARGC);
227 if (!isflt(av[2]))
228 return(MG_ETYPE);
229 if ((cv = c_getvert(av[1])) == NULL) /* get center vertex */
230 return(MG_EUNDEF);
231 xf_xfmpoint(cent, cv->p); /* transform center */
232 rad = xf_scale(atof(av[2])); /* scale radius */
233 if ((inv = rad < 0.)) /* check for inversion */
234 rad = -rad;
235 if ((mat = material()) == NULL) /* get material */
236 return(MG_EBADMAT);
237 /* spit out primitive */
238 printf("\n%s %s %ss%d\n", mat, inv ? "bubble" : "sphere",
239 object(), ++nsphs);
240 printf("0\n0\n4 %18.12g %18.12g %18.12g %18.12g\n",
241 cent[0], cent[1], cent[2], rad);
242 return(MG_OK);
243 }
244
245
246 int
247 r_ring(ac, av) /* put out a ring */
248 int ac;
249 char **av;
250 {
251 static int nrings;
252 char *mat;
253 double r1, r2;
254 C_VERTEX *cv;
255 FVECT cent, norm;
256 /* check argument count and type */
257 if (ac != 4)
258 return(MG_EARGC);
259 if (!isflt(av[2]) || !isflt(av[3]))
260 return(MG_ETYPE);
261 if ((cv = c_getvert(av[1])) == NULL) /* get center vertex */
262 return(MG_EUNDEF);
263 if (is0vect(cv->n)) /* make sure we have normal */
264 return(MG_EILL);
265 xf_xfmpoint(cent, cv->p); /* transform center */
266 xf_rotvect(norm, cv->n); /* rotate normal */
267 r1 = xf_scale(atof(av[2])); /* scale radii */
268 r2 = xf_scale(atof(av[3]));
269 if (r1 < 0. | r2 <= r1)
270 return(MG_EILL);
271 if ((mat = material()) == NULL) /* get material */
272 return(MG_EBADMAT);
273 /* spit out primitive */
274 printf("\n%s ring %sr%d\n", mat, object(), ++nrings);
275 printf("0\n0\n8\n");
276 putv(cent);
277 putv(norm);
278 printf("%18.12g %18.12g\n", r1, r2);
279 return(MG_OK);
280 }
281
282
283 int
284 r_face(ac, av) /* convert a face */
285 int ac;
286 char **av;
287 {
288 static int nfaces;
289 char *mat;
290 register int i;
291 register C_VERTEX *cv;
292 FVECT v;
293 int rv;
294 /* check argument count and type */
295 if (ac < 4)
296 return(MG_EARGC);
297 if ((mat = material()) == NULL) /* get material */
298 return(MG_EBADMAT);
299 if (ac <= 5) { /* check for smoothing */
300 for (i = 1; i < ac; i++) {
301 if ((cv = c_getvert(av[i])) == NULL)
302 return(MG_EUNDEF);
303 if (is0vect(cv->n))
304 break;
305 }
306 if (i == ac) { /* break into triangles */
307 do_tri(mat, av[1], av[2], av[3]);
308 if (ac == 5)
309 do_tri(mat, av[3], av[4], av[1]);
310 return(MG_OK);
311 }
312 }
313 /* spit out unsmoothed primitive */
314 printf("\n%s polygon %sf%d\n", mat, object(), ++nfaces);
315 printf("0\n0\n%d\n", 3*(ac-1));
316 for (i = 1; i < ac; i++) { /* get, transform, print each vertex */
317 if ((cv = c_getvert(av[invert ? ac-i : i])) == NULL)
318 return(MG_EUNDEF);
319 xf_xfmpoint(v, cv->p);
320 putv(v);
321 }
322 return(MG_OK);
323 }
324
325
326 int
327 r_ies(ac, av) /* convert an IES luminaire file */
328 int ac;
329 char **av;
330 {
331 int xa0 = 2;
332 char combuf[128];
333 char fname[48];
334 char *oname;
335 register char *op;
336 register int i;
337 /* check argument count */
338 if (ac < 2)
339 return(MG_EARGC);
340 /* construct output file name */
341 if ((op = strrchr(av[1], '/')) == NULL)
342 op = av[1];
343 (void)strcpy(fname, op);
344 if ((op = strrchr(fname, '.')) == NULL)
345 op = fname + strlen(fname);
346 (void)strcpy(op, ".rad");
347 /* see if we need to run ies2rad */
348 if (access(fname, 0) == -1) {
349 (void)strcpy(combuf, "ies2rad");/* build ies2rad command */
350 op = combuf + 7; /* get -m option (first) */
351 if (ac-xa0 >= 2 && !strcmp(av[xa0], "-m")) {
352 if (!isflt(av[xa0+1]))
353 return(MG_ETYPE);
354 op = addarg(addarg(op, "-m"), av[xa0+1]);
355 xa0 += 2;
356 }
357 *op++ = ' '; /* build IES filename */
358 i = 0;
359 if (mg_file != NULL &&
360 (oname = strrchr(mg_file->fname,'/')) != NULL) {
361 i = oname - mg_file->fname + 1;
362 (void)strcpy(op, mg_file->fname);
363 }
364 (void)strcpy(op+i, av[1]);
365 if (access(op, 0) == -1) /* check for file existence */
366 return(MG_ENOFILE);
367 system(combuf); /* run ies2rad */
368 if (access(fname, 0) == -1) /* check success */
369 return(MG_EINCL);
370 }
371 printf("\n!xform"); /* put out xform command */
372 oname = object();
373 if (*oname) {
374 printf(" -n ");
375 for (op = oname; op[1]; op++) /* remove trailing separator */
376 putchar(*op);
377 }
378 for (i = xa0; i < ac; i++)
379 printf(" %s", av[i]);
380 if (ac > xa0 && xf_argc > 0)
381 printf(" -i 1");
382 for (i = 0; i < xf_argc; i++)
383 printf(" %s", xf_argv[i]);
384 printf(" %s\n", fname);
385 return(MG_OK);
386 }
387
388
389 do_tri(mat, vn1, vn2, vn3) /* put out smoothed triangle */
390 char *mat, *vn1, *vn2, *vn3;
391 {
392 static int ntris;
393 BARYCCM bvecs;
394 FLOAT bcoor[3][3];
395 C_VERTEX *cv1, *cv2, *cv3;
396 FVECT v1, v2, v3;
397 FVECT n1, n2, n3;
398 register int i;
399 /* the following is repeat code, so assume it's OK */
400 cv2 = c_getvert(vn2);
401 if (invert) {
402 cv3 = c_getvert(vn1);
403 cv1 = c_getvert(vn3);
404 } else {
405 cv1 = c_getvert(vn1);
406 cv3 = c_getvert(vn3);
407 }
408 xf_xfmpoint(v1, cv1->p);
409 xf_xfmpoint(v2, cv2->p);
410 xf_xfmpoint(v3, cv3->p);
411 /* compute barycentric coords. */
412 if (comp_baryc(&bvecs, v1, v2, v3) < 0)
413 return; /* degenerate triangle! */
414 printf("\n%s texfunc T-nor\n", mat); /* put out texture */
415 printf("4 dx dy dz %s\n0\n", TCALNAME);
416 xf_rotvect(n1, cv1->n);
417 xf_rotvect(n2, cv2->n);
418 xf_rotvect(n3, cv3->n);
419 for (i = 0; i < 3; i++) {
420 bcoor[i][0] = n1[i];
421 bcoor[i][1] = n2[i];
422 bcoor[i][2] = n3[i];
423 }
424 put_baryc(&bvecs, bcoor, 3);
425 /* put out triangle */
426 printf("\nT-nor polygon %st%d\n", object(), ++ntris);
427 printf("0\n0\n9\n");
428 putv(v1);
429 putv(v2);
430 putv(v3);
431 }
432
433
434 char *
435 material() /* get (and print) current material */
436 {
437 char *mname = "mat";
438 COLOR radrgb, c2;
439 double d;
440 register int i;
441
442 if (c_cmname != NULL)
443 mname = c_cmname;
444 if (!c_cmaterial->clock)
445 return(mname); /* already current */
446 /* else update output */
447 c_cmaterial->clock = 0;
448 if (c_cmaterial->ed > .1) { /* emitter */
449 cvtcolor(radrgb, &c_cmaterial->ed_c,
450 emult*c_cmaterial->ed/(PI*WHTEFFICACY));
451 if (glowdist < FHUGE) { /* do a glow */
452 fprintf(matfp, "\nvoid glow %s\n0\n0\n", mname);
453 fprintf(matfp, "4 %f %f %f %f\n", colval(radrgb,RED),
454 colval(radrgb,GRN),
455 colval(radrgb,BLU), glowdist);
456 } else {
457 fprintf(matfp, "\nvoid light %s\n0\n0\n", mname);
458 fprintf(matfp, "3 %f %f %f\n", colval(radrgb,RED),
459 colval(radrgb,GRN),
460 colval(radrgb,BLU));
461 }
462 return(mname);
463 }
464 d = c_cmaterial->rd + c_cmaterial->td +
465 c_cmaterial->rs + c_cmaterial->ts;
466 if (d < 0. | d > 1.)
467 return(NULL);
468 /* check for glass/dielectric */
469 if (c_cmaterial->nr > 1.1 &&
470 c_cmaterial->ts > .25 && c_cmaterial->rs <= .125 &&
471 c_cmaterial->td <= .01 && c_cmaterial->rd <= .01 &&
472 c_cmaterial->rs_a <= .01 && c_cmaterial->ts_a <= .01) {
473 cvtcolor(radrgb, &c_cmaterial->ts_c,
474 c_cmaterial->ts + c_cmaterial->rs);
475 if (c_cmaterial->sided) { /* dielectric */
476 colval(radrgb,RED) = pow(colval(radrgb,RED),
477 1./C_1SIDEDTHICK);
478 colval(radrgb,GRN) = pow(colval(radrgb,GRN),
479 1./C_1SIDEDTHICK);
480 colval(radrgb,BLU) = pow(colval(radrgb,BLU),
481 1./C_1SIDEDTHICK);
482 fprintf(matfp, "\nvoid dielectric %s\n0\n0\n", mname);
483 fprintf(matfp, "5 %g %g %g %f 0\n", colval(radrgb,RED),
484 colval(radrgb,GRN), colval(radrgb,BLU),
485 c_cmaterial->nr);
486 return(mname);
487 }
488 /* glass */
489 fprintf(matfp, "\nvoid glass %s\n0\n0\n", mname);
490 fprintf(matfp, "4 %f %f %f %f\n", colval(radrgb,RED),
491 colval(radrgb,GRN), colval(radrgb,BLU),
492 c_cmaterial->nr);
493 return(mname);
494 }
495 /* check for trans */
496 if (c_cmaterial->td > .01 || c_cmaterial->ts > .01) {
497 double ts, a5, a6;
498
499 if (c_cmaterial->sided) {
500 ts = sqrt(c_cmaterial->ts); /* approximate */
501 a5 = .5;
502 } else {
503 ts = c_cmaterial->ts;
504 a5 = 1.;
505 }
506 /* average colors */
507 d = c_cmaterial->rd + c_cmaterial->td + ts;
508 cvtcolor(radrgb, &c_cmaterial->rd_c, c_cmaterial->rd/d);
509 cvtcolor(c2, &c_cmaterial->td_c, c_cmaterial->td/d);
510 addcolor(radrgb, c2);
511 cvtcolor(c2, &c_cmaterial->ts_c, ts/d);
512 addcolor(radrgb, c2);
513 if (c_cmaterial->rs + ts > .0001)
514 a5 = (c_cmaterial->rs*c_cmaterial->rs_a +
515 ts*a5*c_cmaterial->ts_a) /
516 (c_cmaterial->rs + ts);
517 a6 = (c_cmaterial->td + ts) /
518 (c_cmaterial->rd + c_cmaterial->td + ts);
519 if (a6 < .999)
520 d = c_cmaterial->rd/(1. - c_cmaterial->rs)/(1. - a6);
521 else
522 d = c_cmaterial->td + ts;
523 scalecolor(radrgb, d);
524 fprintf(matfp, "\nvoid trans %s\n0\n0\n", mname);
525 fprintf(matfp, "7 %f %f %f\n", colval(radrgb,RED),
526 colval(radrgb,GRN), colval(radrgb,BLU));
527 fprintf(matfp, "\t%f %f %f %f\n", c_cmaterial->rs, a5, a6,
528 ts/(ts + c_cmaterial->td));
529 return(mname);
530 }
531 /* check for plastic */
532 if (c_cmaterial->rs < .1 && (c_cmaterial->rs < .01 ||
533 c_isgrey(&c_cmaterial->rs_c))) {
534 cvtcolor(radrgb, &c_cmaterial->rd_c,
535 c_cmaterial->rd/(1.-c_cmaterial->rs));
536 fprintf(matfp, "\nvoid plastic %s\n0\n0\n", mname);
537 fprintf(matfp, "5 %f %f %f %f %f\n", colval(radrgb,RED),
538 colval(radrgb,GRN), colval(radrgb,BLU),
539 c_cmaterial->rs, c_cmaterial->rs_a);
540 return(mname);
541 }
542 /* else it's metal */
543 /* average colors */
544 cvtcolor(radrgb, &c_cmaterial->rd_c, c_cmaterial->rd);
545 cvtcolor(c2, &c_cmaterial->rs_c, c_cmaterial->rs);
546 addcolor(radrgb, c2);
547 fprintf(matfp, "\nvoid metal %s\n0\n0\n", mname);
548 fprintf(matfp, "5 %f %f %f %f %f\n", colval(radrgb,RED),
549 colval(radrgb,GRN), colval(radrgb,BLU),
550 c_cmaterial->rs/(c_cmaterial->rd + c_cmaterial->rs),
551 c_cmaterial->rs_a);
552 return(mname);
553 }
554
555
556 cvtcolor(radrgb, ciec, intensity) /* convert a CIE XYZ color to RGB */
557 COLOR radrgb;
558 register C_COLOR *ciec;
559 double intensity;
560 {
561 static COLOR ciexyz;
562
563 c_ccvt(ciec, C_CSXY); /* get xy representation */
564 ciexyz[1] = intensity;
565 ciexyz[0] = ciec->cx/ciec->cy*ciexyz[1];
566 ciexyz[2] = ciexyz[1]*(1./ciec->cy - 1.) - ciexyz[0];
567 cie_rgb(radrgb, ciexyz);
568 }
569
570
571 char *
572 object() /* return current object name */
573 {
574 static char objbuf[64];
575 register int i;
576 register char *cp;
577 int len;
578 /* tracked by obj_handler */
579 i = obj_nnames - sizeof(objbuf)/16;
580 if (i < 0)
581 i = 0;
582 for (cp = objbuf; i < obj_nnames &&
583 cp + (len=strlen(obj_name[i])) < objbuf+sizeof(objbuf)-1;
584 i++, *cp++ = '.') {
585 strcpy(cp, obj_name[i]);
586 cp += len;
587 }
588 *cp = '\0';
589 return(objbuf);
590 }
591
592
593 char *
594 addarg(op, arg) /* add argument and advance pointer */
595 register char *op, *arg;
596 {
597 *op = ' ';
598 while (*++op = *arg++)
599 ;
600 return(op);
601 }