ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/mgflib/context.c
(Generate patch)

Comparing ray/src/cv/mgflib/context.c (file contents):
Revision 1.14 by greg, Thu Jul 7 17:27:58 1994 UTC vs.
Revision 1.19 by greg, Mon Mar 13 13:00:34 1995 UTC

# Line 36 | Line 36 | static LUTAB   clr_tab = LU_SINIT(free,free);  /* color l
36   static LUTAB    mat_tab = LU_SINIT(free,free);  /* material lookup table */
37   static LUTAB    vtx_tab = LU_SINIT(free,free);  /* vertex lookup table */
38  
39 <                                /* CIE 1931 Standard Observer */
40 < static C_COLOR  cie_xf = C_CIEX;
41 < static C_COLOR  cie_yf = C_CIEY;
42 < static C_COLOR  cie_zf = C_CIEZ;
39 >                                /* CIE 1931 Standard Observer curves */
40 > static C_COLOR  cie_xf = { 1, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
41 >                        {14,42,143,435,1344,2839,3483,3362,2908,1954,956,
42 >                        320,49,93,633,1655,2904,4334,5945,7621,9163,10263,
43 >                        10622,10026,8544,6424,4479,2835,1649,874,468,227,
44 >                        114,58,29,14,7,3,2,1,0}, 106836L, .467, .368, 362.230
45 >                        };
46 > static C_COLOR  cie_yf = { 1, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
47 >                        {0,1,4,12,40,116,230,380,600,910,1390,2080,3230,
48 >                        5030,7100,8620,9540,9950,9950,9520,8700,7570,6310,
49 >                        5030,3810,2650,1750,1070,610,320,170,82,41,21,10,
50 >                        5,2,1,1,0,0}, 106856L, .398, .542, 493.525
51 >                        };
52 > static C_COLOR  cie_zf = { 1, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
53 >                        {65,201,679,2074,6456,13856,17471,17721,16692,
54 >                        12876,8130,4652,2720,1582,782,422,203,87,39,21,17,
55 >                        11,8,3,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
56 >                        106770L, .147, .077, 54.363
57 >                        };
58 >                                /* Derived CIE 1931 Primaries (imaginary) */
59 > static C_COLOR  cie_xp = { 1, C_CDSPEC|C_CSSPEC|C_CSXY,
60 >                        {-174,-198,-195,-197,-202,-213,-235,-272,-333,
61 >                        -444,-688,-1232,-2393,-4497,-6876,-6758,-5256,
62 >                        -3100,-815,1320,3200,4782,5998,6861,7408,7754,
63 >                        7980,8120,8199,8240,8271,8292,8309,8283,8469,
64 >                        8336,8336,8336,8336,8336,8336},
65 >                        127424L, 1., .0,
66 >                        };
67 > static C_COLOR  cie_yp = { 1, C_CDSPEC|C_CSSPEC|C_CSXY,
68 >                        {-451,-431,-431,-430,-427,-417,-399,-366,-312,
69 >                        -204,57,691,2142,4990,8810,9871,9122,7321,5145,
70 >                        3023,1123,-473,-1704,-2572,-3127,-3474,-3704,
71 >                        -3846,-3927,-3968,-3999,-4021,-4038,-4012,-4201,
72 >                        -4066,-4066,-4066,-4066,-4066,-4066},
73 >                        -23035L, .0, 1.,
74 >                        };
75 > static C_COLOR  cie_zp = { 1, C_CDSPEC|C_CSSPEC|C_CSXY,
76 >                        {4051,4054,4052,4053,4054,4056,4059,4064,4071,
77 >                        4074,4056,3967,3677,2933,1492,313,-440,-795,
78 >                        -904,-918,-898,-884,-869,-863,-855,-855,-851,
79 >                        -848,-847,-846,-846,-846,-845,-846,-843,-845,
80 >                        -845,-845,-845,-845,-845},
81 >                        36057L, .0, .0,
82 >                        };
83  
84   static int      setspectrum();
85 + static int      setbbtemp();
86   static void     mixcolors();
87  
88  
# Line 105 | Line 146 | register char  **av;
146          case MG_E_CXY:          /* assign CIE XY value */
147                  if (ac != 3)
148                          return(MG_EARGC);
149 <                if (!isflt(av[1]) || !isflt(av[2]))
149 >                if (!isflt(av[1]) | !isflt(av[2]))
150                          return(MG_ETYPE);
151                  c_ccolor->cx = atof(av[1]);
152                  c_ccolor->cy = atof(av[2]);
# Line 118 | Line 159 | register char  **av;
159          case MG_E_CSPEC:        /* assign spectral values */
160                  if (ac < 5)
161                          return(MG_EARGC);
162 <                if (!isint(av[1]) || !isint(av[2]))
162 >                if (!isflt(av[1]) | !isflt(av[2]))
163                          return(MG_ETYPE);
164 <                return(setspectrum(c_ccolor, atoi(av[1]), atoi(av[2]),
164 >                return(setspectrum(c_ccolor, atof(av[1]), atof(av[2]),
165                                  ac-3, av+3));
166 +        case MG_E_CCT:          /* assign black body spectrum */
167 +                if (ac != 2)
168 +                        return(MG_EARGC);
169 +                if (!isflt(av[1]))
170 +                        return(MG_ETYPE);
171 +                return(setbbtemp(c_ccolor, atof(av[1])));
172          case MG_E_CMIX:         /* mix colors */
173                  if (ac < 5 || (ac-1)%2)
174                          return(MG_EARGC);
# Line 248 | Line 295 | register char  **av;
295          case MG_E_RS:           /* set specular reflectance */
296                  if (ac != 3)
297                          return(MG_EARGC);
298 <                if (!isflt(av[1]) || !isflt(av[2]))
298 >                if (!isflt(av[1]) | !isflt(av[2]))
299                          return(MG_ETYPE);
300                  c_cmaterial->rs = atof(av[1]);
301                  c_cmaterial->rs_a = atof(av[2]);
# Line 261 | Line 308 | register char  **av;
308          case MG_E_TS:           /* set specular transmittance */
309                  if (ac != 3)
310                          return(MG_EARGC);
311 <                if (!isflt(av[1]) || !isflt(av[2]))
311 >                if (!isflt(av[1]) | !isflt(av[2]))
312                          return(MG_ETYPE);
313                  c_cmaterial->ts = atof(av[1]);
314                  c_cmaterial->ts_a = atof(av[2]);
# Line 348 | Line 395 | register char  **av;
395          case MG_E_POINT:        /* set point */
396                  if (ac != 4)
397                          return(MG_EARGC);
398 <                if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3]))
398 >                if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3]))
399                          return(MG_ETYPE);
400                  c_cvertex->p[0] = atof(av[1]);
401                  c_cvertex->p[1] = atof(av[2]);
# Line 358 | Line 405 | register char  **av;
405          case MG_E_NORMAL:       /* set normal */
406                  if (ac != 4)
407                          return(MG_EARGC);
408 <                if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3]))
408 >                if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3]))
409                          return(MG_ETYPE);
410                  c_cvertex->n[0] = atof(av[1]);
411                  c_cvertex->n[1] = atof(av[2]);
# Line 376 | Line 423 | c_clearall()                   /* empty context tables */
423   {
424          c_uncolor = c_dfcolor;
425          c_ccolor = &c_uncolor;
426 +        c_ccname = NULL;
427          lu_done(&clr_tab);
428          c_unmaterial = c_dfmaterial;
429          c_cmaterial = &c_unmaterial;
430 +        c_cmname = NULL;
431          lu_done(&mat_tab);
432          c_unvertex = c_dfvertex;
433          c_cvertex = &c_unvertex;
434 +        c_cvname = NULL;
435          lu_done(&vtx_tab);
436   }
437  
# Line 442 | Line 492 | int    fl;
492          double  x, y, z;
493          register int    i;
494  
495 <        if (clr->flags & fl)            /* already done */
495 >        fl &= ~clr->flags;                      /* ignore what's done */
496 >        if (!fl)                                /* everything's done! */
497                  return;
498          if (!(clr->flags & (C_CSXY|C_CSSPEC)))  /* nothing set! */
499                  *clr = c_dfcolor;
500 <        else if (fl & C_CSXY) {         /* cspec -> cxy */
500 >        if (fl & C_CSXY) {              /* cspec -> cxy */
501                  x = y = z = 0.;
502                  for (i = 0; i < C_CNSS; i++) {
503                          x += cie_xf.ssamp[i] * clr->ssamp[i];
504                          y += cie_yf.ssamp[i] * clr->ssamp[i];
505                          z += cie_zf.ssamp[i] * clr->ssamp[i];
506                  }
507 +                x /= (double)cie_xf.ssum;
508 +                y /= (double)cie_yf.ssum;
509 +                z /= (double)cie_zf.ssum;
510                  z += x + y;
511                  clr->cx = x / z;
512                  clr->cy = y / z;
513                  clr->flags |= C_CSXY;
514 <        } else {                        /* cxy -> cspec */
515 <                z = (cie_xf.ssum + cie_yf.ssum + cie_zf.ssum) / 3.;
516 <                x = clr->cx * z / cie_xf.ssum;
517 <                y = clr->cy * z / cie_yf.ssum;
464 <                z = (1. - clr->cx - clr->cy) * z / cie_zf.ssum;
514 >        } else if (fl & C_CSSPEC) {     /* cxy -> cspec */
515 >                x = clr->cx;
516 >                y = clr->cy;
517 >                z = 1. - x - y;
518                  clr->ssum = 0;
519 <                for (i = 0; i < C_CNSS; i++)
520 <                        clr->ssum += clr->ssamp[i] =
521 <                                        x * cie_xf.ssamp[i] +
522 <                                        y * cie_yf.ssamp[i] +
523 <                                        z * cie_zf.ssamp[i] ;
519 >                for (i = 0; i < C_CNSS; i++) {
520 >                        clr->ssamp[i] = x*cie_xp.ssamp[i] + y*cie_yp.ssamp[i]
521 >                                        + z*cie_zp.ssamp[i] + .5;
522 >                        if (clr->ssamp[i] < 0)          /* out of gamut! */
523 >                                clr->ssamp[i] = 0;
524 >                        else
525 >                                clr->ssum += clr->ssamp[i];
526 >                }
527                  clr->flags |= C_CSSPEC;
528          }
529 +        if (fl & C_CSEFF) {             /* compute efficacy */
530 +                if (clr->flags & C_CSSPEC) {            /* from spectrum */
531 +                        y = 0.;
532 +                        for (i = 0; i < C_CNSS; i++)
533 +                                y += cie_yf.ssamp[i] * clr->ssamp[i];
534 +                        clr->eff = C_CLPWM * y / clr->ssum;
535 +                } else /* clr->flags & C_CSXY */ {      /* from (x,y) */
536 +                        clr->eff = clr->cx*cie_xf.eff + clr->cy*cie_yf.eff +
537 +                                        (1. - clr->cx - clr->cy)*cie_zf.eff;
538 +                }
539 +                clr->flags |= C_CSEFF;
540 +        }
541   }
542  
543  
544   static int
545   setspectrum(clr, wlmin, wlmax, ac, av)  /* convert a spectrum */
546   register C_COLOR        *clr;
547 < int     wlmin, wlmax;
547 > double  wlmin, wlmax;
548   int     ac;
549   char    **av;
550   {
551          double  scale;
552 <        float   *va;
553 <        register int    i;
554 <        int     wl, pos;
552 >        float   va[C_CNSS];
553 >        register int    i, pos;
554 >        int     n, imax;
555 >        int     wl;
556          double  wl0, wlstep;
557 <
558 <        if (wlmin < C_CMINWL || wlmin >= wlmax || wlmax > C_CMAXWL)
557 >                                        /* check bounds */
558 >        if (wlmax <= C_CMINWL | wlmax <= wlmin | wlmin >= C_CMAXWL)
559                  return(MG_EILL);
560 <        if ((va = (float *)malloc(ac*sizeof(float))) == NULL)
561 <                return(MG_EMEM);
560 >        wlstep = (wlmax - wlmin)/(ac-1);
561 >        while (wlmin < C_CMINWL) {
562 >                wlmin += wlstep;
563 >                ac--; av++;
564 >        }
565 >        while (wlmax > C_CMAXWL) {
566 >                wlmax -= wlstep;
567 >                ac--;
568 >        }
569 >        if (ac < 2)
570 >                return(MG_EILL);
571 >        imax = ac;                      /* box filter if necessary */
572 >        if (wlstep < C_CWLI) {
573 >                wlstep = C_CWLI;
574 >                imax = (wlmax - wlmin)/wlstep;
575 >        }
576          scale = 0.;                     /* get values and maximum */
577 <        for (i = 0; i < ac; i++) {
578 <                if (!isflt(av[i]))
579 <                        return(MG_ETYPE);
580 <                va[i] = atof(av[i]);
577 >        pos = 0;
578 >        for (i = 0; i < imax; i++) {
579 >                va[i] = 0.; n = 0;
580 >                while (pos < (i+.5)*ac/imax) {
581 >                        if (!isflt(av[pos]))
582 >                                return(MG_ETYPE);
583 >                        va[i] += atof(av[pos++]);
584 >                        n++;
585 >                }
586 >                if (n > 1)
587 >                        va[i] /= (double)n;
588                  if (va[i] < 0.)
589                          return(MG_EILL);
590                  if (va[i] > scale)
# Line 505 | Line 595 | char   **av;
595          scale = C_CMAXV / scale;
596          clr->ssum = 0;                  /* convert to our spacing */
597          wl0 = wlmin;
508        wlstep = (double)(wlmax - wlmin)/(ac-1);
598          pos = 0;
599          for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI)
600 <                if (wl < wlmin || wl > wlmax)
600 >                if (wl < wlmin | wl > wlmax)
601                          clr->ssamp[i] = 0;
602                  else {
603                          while (wl0 + wlstep < wl+FTINY) {
604                                  wl0 += wlstep;
605                                  pos++;
606                          }
607 <                        if (wl+FTINY >= wl0 && wl-FTINY <= wl0)
608 <                                clr->ssamp[i] = scale*va[pos];
607 >                        if (wl+FTINY >= wl0 & wl-FTINY <= wl0)
608 >                                clr->ssamp[i] = scale*va[pos] + .5;
609                          else            /* interpolate if necessary */
610 <                                clr->ssamp[i] = scale / wlstep *
610 >                                clr->ssamp[i] = .5 + scale / wlstep *
611                                                  ( va[pos]*(wl0+wlstep - wl) +
612                                                          va[pos+1]*(wl - wl0) );
613                          clr->ssum += clr->ssamp[i];
614                  }
615          clr->flags = C_CDSPEC|C_CSSPEC;
616          clr->clock++;
528        free((MEM_PTR)va);
617          return(MG_OK);
618   }
619  
# Line 540 | Line 628 | double w1, w2;
628          register int    i;
629  
630          if ((c1->flags|c2->flags) & C_CDSPEC) {         /* spectral mixing */
631 <                c_ccvt(c1, C_CSSPEC);
632 <                c_ccvt(c2, C_CSSPEC);
633 <                w1 /= (double)c1->ssum;
634 <                w2 /= (double)c2->ssum;
631 >                c_ccvt(c1, C_CSSPEC|C_CSEFF);
632 >                c_ccvt(c2, C_CSSPEC|C_CSEFF);
633 >                w1 /= c1->eff*c1->ssum;
634 >                w2 /= c2->eff*c2->ssum;
635                  scale = 0.;
636                  for (i = 0; i < C_CNSS; i++) {
637                          cmix[i] = w1*c1->ssamp[i] + w2*c2->ssamp[i];
# Line 564 | Line 652 | double w1, w2;
652                  cres->flags = C_CDXY|C_CSXY;
653          }
654   }
655 +
656 +
657 + #define C1              3.741832e-16    /* W-m^2 */
658 + #define C2              1.4388e-2       /* m-K */
659 +
660 + #define bbsp(l,t)       (C1/((l)*(l)*(l)*(l)*(l)*(exp(C2/((t)*(l)))-1.)))
661 + #define bblm(t)         (C2/5./(t))
662 +
663 + static int
664 + setbbtemp(clr, tk)              /* set black body spectrum */
665 + register C_COLOR        *clr;
666 + double  tk;
667 + {
668 +        double  sf, wl;
669 +        register int    i;
670 +
671 +        if (tk < 1000)
672 +                return(MG_EILL);
673 +        wl = bblm(tk);                  /* scalefactor based on peak */
674 +        if (wl < C_CMINWL*1e-9)
675 +                wl = C_CMINWL*1e-9;
676 +        else if (wl > C_CMAXWL*1e-9)
677 +                wl = C_CMAXWL*1e-9;
678 +        sf = C_CMAXV/bbsp(wl,tk);
679 +        clr->ssum = 0;
680 +        for (i = 0; i < C_CNSS; i++) {
681 +                wl = (C_CMINWL + i*C_CWLI)*1e-9;
682 +                clr->ssum += clr->ssamp[i] = sf*bbsp(wl,tk) + .5;
683 +        }
684 +        clr->flags = C_CDSPEC|C_CSSPEC;
685 +        clr->clock++;
686 +        return(MG_OK);
687 + }
688 +
689 + #undef  C1
690 + #undef  C2
691 + #undef  bbsp
692 + #undef  bblm

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines