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.15 by greg, Fri Jul 22 12:50:24 1994 UTC vs.
Revision 1.23 by greg, Mon Aug 28 16:44:12 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 64 | Line 105 | register char  **av;
105                          c_ccname = NULL;
106                          return(MG_OK);
107                  }
108 +                if (!isname(av[1]))
109 +                        return(MG_EILL);
110                  lp = lu_find(&clr_tab, av[1]);  /* lookup context */
111                  if (lp == NULL)
112                          return(MG_EMEM);
# Line 105 | Line 148 | register char  **av;
148          case MG_E_CXY:          /* assign CIE XY value */
149                  if (ac != 3)
150                          return(MG_EARGC);
151 <                if (!isflt(av[1]) || !isflt(av[2]))
151 >                if (!isflt(av[1]) | !isflt(av[2]))
152                          return(MG_ETYPE);
153                  c_ccolor->cx = atof(av[1]);
154                  c_ccolor->cy = atof(av[2]);
# Line 118 | Line 161 | register char  **av;
161          case MG_E_CSPEC:        /* assign spectral values */
162                  if (ac < 5)
163                          return(MG_EARGC);
164 <                if (!isint(av[1]) || !isint(av[2]))
164 >                if (!isflt(av[1]) | !isflt(av[2]))
165                          return(MG_ETYPE);
166 <                return(setspectrum(c_ccolor, atoi(av[1]), atoi(av[2]),
166 >                return(setspectrum(c_ccolor, atof(av[1]), atof(av[2]),
167                                  ac-3, av+3));
168 +        case MG_E_CCT:          /* assign black body spectrum */
169 +                if (ac != 2)
170 +                        return(MG_EARGC);
171 +                if (!isflt(av[1]))
172 +                        return(MG_ETYPE);
173 +                return(setbbtemp(c_ccolor, atof(av[1])));
174          case MG_E_CMIX:         /* mix colors */
175                  if (ac < 5 || (ac-1)%2)
176                          return(MG_EARGC);
# Line 174 | Line 223 | register char  **av;
223                          c_cmname = NULL;
224                          return(MG_OK);
225                  }
226 +                if (!isname(av[1]))
227 +                        return(MG_EILL);
228                  lp = lu_find(&mat_tab, av[1]);  /* lookup context */
229                  if (lp == NULL)
230                          return(MG_EMEM);
# Line 212 | Line 263 | register char  **av;
263                  *c_cmaterial = *(C_MATERIAL *)lp->data;
264                  c_cmaterial->clock = i + 1;
265                  return(MG_OK);
266 +        case MG_E_IR:           /* set index of refraction */
267 +                if (ac != 3)
268 +                        return(MG_EARGC);
269 +                if (!isflt(av[1]) | !isflt(av[2]))
270 +                        return(MG_ETYPE);
271 +                c_cmaterial->nr = atof(av[1]);
272 +                c_cmaterial->ni = atof(av[2]);
273 +                if (c_cmaterial->nr <= FTINY)
274 +                        return(MG_EILL);
275 +                c_cmaterial->clock++;
276 +                return(MG_OK);
277          case MG_E_RD:           /* set diffuse reflectance */
278                  if (ac != 2)
279                          return(MG_EARGC);
# Line 248 | Line 310 | register char  **av;
310          case MG_E_RS:           /* set specular reflectance */
311                  if (ac != 3)
312                          return(MG_EARGC);
313 <                if (!isflt(av[1]) || !isflt(av[2]))
313 >                if (!isflt(av[1]) | !isflt(av[2]))
314                          return(MG_ETYPE);
315                  c_cmaterial->rs = atof(av[1]);
316                  c_cmaterial->rs_a = atof(av[2]);
# Line 261 | Line 323 | register char  **av;
323          case MG_E_TS:           /* set specular transmittance */
324                  if (ac != 3)
325                          return(MG_EARGC);
326 <                if (!isflt(av[1]) || !isflt(av[2]))
326 >                if (!isflt(av[1]) | !isflt(av[2]))
327                          return(MG_ETYPE);
328                  c_cmaterial->ts = atof(av[1]);
329                  c_cmaterial->ts_a = atof(av[2]);
# Line 308 | Line 370 | register char  **av;
370                          c_cvname = NULL;
371                          return(MG_OK);
372                  }
373 +                if (!isname(av[1]))
374 +                        return(MG_EILL);
375                  lp = lu_find(&vtx_tab, av[1]);  /* lookup context */
376                  if (lp == NULL)
377                          return(MG_EMEM);
# Line 348 | Line 412 | register char  **av;
412          case MG_E_POINT:        /* set point */
413                  if (ac != 4)
414                          return(MG_EARGC);
415 <                if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3]))
415 >                if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3]))
416                          return(MG_ETYPE);
417                  c_cvertex->p[0] = atof(av[1]);
418                  c_cvertex->p[1] = atof(av[2]);
# Line 358 | Line 422 | register char  **av;
422          case MG_E_NORMAL:       /* set normal */
423                  if (ac != 4)
424                          return(MG_EARGC);
425 <                if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3]))
425 >                if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3]))
426                          return(MG_ETYPE);
427                  c_cvertex->n[0] = atof(av[1]);
428                  c_cvertex->n[1] = atof(av[2]);
# Line 445 | Line 509 | int    fl;
509          double  x, y, z;
510          register int    i;
511  
512 <        if (clr->flags & fl)            /* already done */
512 >        fl &= ~clr->flags;                      /* ignore what's done */
513 >        if (!fl)                                /* everything's done! */
514                  return;
515          if (!(clr->flags & (C_CSXY|C_CSSPEC)))  /* nothing set! */
516                  *clr = c_dfcolor;
517 <        else if (fl & C_CSXY) {         /* cspec -> cxy */
517 >        if (fl & C_CSXY) {              /* cspec -> cxy */
518                  x = y = z = 0.;
519                  for (i = 0; i < C_CNSS; i++) {
520                          x += cie_xf.ssamp[i] * clr->ssamp[i];
521                          y += cie_yf.ssamp[i] * clr->ssamp[i];
522                          z += cie_zf.ssamp[i] * clr->ssamp[i];
523                  }
524 +                x /= (double)cie_xf.ssum;
525 +                y /= (double)cie_yf.ssum;
526 +                z /= (double)cie_zf.ssum;
527                  z += x + y;
528                  clr->cx = x / z;
529                  clr->cy = y / z;
530                  clr->flags |= C_CSXY;
531 <        } else {                        /* cxy -> cspec */
532 <                z = (cie_xf.ssum + cie_yf.ssum + cie_zf.ssum) / 3.;
533 <                x = clr->cx * z / cie_xf.ssum;
534 <                y = clr->cy * z / cie_yf.ssum;
467 <                z = (1. - clr->cx - clr->cy) * z / cie_zf.ssum;
531 >        } else if (fl & C_CSSPEC) {     /* cxy -> cspec */
532 >                x = clr->cx;
533 >                y = clr->cy;
534 >                z = 1. - x - y;
535                  clr->ssum = 0;
536 <                for (i = 0; i < C_CNSS; i++)
537 <                        clr->ssum += clr->ssamp[i] =
538 <                                        x * cie_xf.ssamp[i] +
539 <                                        y * cie_yf.ssamp[i] +
540 <                                        z * cie_zf.ssamp[i] ;
536 >                for (i = 0; i < C_CNSS; i++) {
537 >                        clr->ssamp[i] = x*cie_xp.ssamp[i] + y*cie_yp.ssamp[i]
538 >                                        + z*cie_zp.ssamp[i] + .5;
539 >                        if (clr->ssamp[i] < 0)          /* out of gamut! */
540 >                                clr->ssamp[i] = 0;
541 >                        else
542 >                                clr->ssum += clr->ssamp[i];
543 >                }
544                  clr->flags |= C_CSSPEC;
545          }
546 +        if (fl & C_CSEFF) {             /* compute efficacy */
547 +                if (clr->flags & C_CSSPEC) {            /* from spectrum */
548 +                        y = 0.;
549 +                        for (i = 0; i < C_CNSS; i++)
550 +                                y += cie_yf.ssamp[i] * clr->ssamp[i];
551 +                        clr->eff = C_CLPWM * y / clr->ssum;
552 +                } else /* clr->flags & C_CSXY */ {      /* from (x,y) */
553 +                        clr->eff = clr->cx*cie_xf.eff + clr->cy*cie_yf.eff +
554 +                                        (1. - clr->cx - clr->cy)*cie_zf.eff;
555 +                }
556 +                clr->flags |= C_CSEFF;
557 +        }
558   }
559  
560  
561   static int
562   setspectrum(clr, wlmin, wlmax, ac, av)  /* convert a spectrum */
563   register C_COLOR        *clr;
564 < int     wlmin, wlmax;
564 > double  wlmin, wlmax;
565   int     ac;
566   char    **av;
567   {
568          double  scale;
569 <        float   *va;
570 <        register int    i;
571 <        int     wl, pos;
569 >        float   va[C_CNSS];
570 >        register int    i, pos;
571 >        int     n, imax;
572 >        int     wl;
573          double  wl0, wlstep;
574 <
575 <        if (wlmin < C_CMINWL || wlmin >= wlmax || wlmax > C_CMAXWL)
574 >        double  boxpos, boxstep;
575 >                                        /* check bounds */
576 >        if (wlmax <= C_CMINWL | wlmax <= wlmin | wlmin >= C_CMAXWL)
577                  return(MG_EILL);
578 <        if ((va = (float *)malloc(ac*sizeof(float))) == NULL)
579 <                return(MG_EMEM);
578 >        wlstep = (wlmax - wlmin)/(ac-1);
579 >        while (wlmin < C_CMINWL) {
580 >                wlmin += wlstep;
581 >                ac--; av++;
582 >        }
583 >        while (wlmax > C_CMAXWL) {
584 >                wlmax -= wlstep;
585 >                ac--;
586 >        }
587 >        imax = ac;                      /* box filter if necessary */
588 >        boxpos = 0;
589 >        boxstep = 1;
590 >        if (wlstep < C_CWLI) {
591 >                imax = (wlmax - wlmin)/C_CWLI + (1-FTINY);
592 >                boxpos = (wlmin - C_CMINWL)/C_CWLI;
593 >                boxstep = wlstep/C_CWLI;
594 >                wlstep = C_CWLI;
595 >        }
596          scale = 0.;                     /* get values and maximum */
597 <        for (i = 0; i < ac; i++) {
598 <                if (!isflt(av[i]))
599 <                        return(MG_ETYPE);
600 <                va[i] = atof(av[i]);
601 <                if (va[i] < 0.)
602 <                        return(MG_EILL);
597 >        pos = 0;
598 >        for (i = 0; i < imax; i++) {
599 >                va[i] = 0.; n = 0;
600 >                while (boxpos < i+.5 && pos < ac) {
601 >                        if (!isflt(av[pos]))
602 >                                return(MG_ETYPE);
603 >                        va[i] += atof(av[pos++]);
604 >                        n++;
605 >                        boxpos += boxstep;
606 >                }
607 >                if (n > 1)
608 >                        va[i] /= (double)n;
609                  if (va[i] > scale)
610                          scale = va[i];
611 +                else if (va[i] < -scale)
612 +                        scale = -va[i];
613          }
614 <        if (scale == 0.)
614 >        if (scale <= FTINY)
615                  return(MG_EILL);
616          scale = C_CMAXV / scale;
617          clr->ssum = 0;                  /* convert to our spacing */
618          wl0 = wlmin;
511        wlstep = (double)(wlmax - wlmin)/(ac-1);
619          pos = 0;
620          for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI)
621 <                if (wl < wlmin || wl > wlmax)
621 >                if (wl < wlmin | wl > wlmax)
622                          clr->ssamp[i] = 0;
623                  else {
624                          while (wl0 + wlstep < wl+FTINY) {
625                                  wl0 += wlstep;
626                                  pos++;
627                          }
628 <                        if (wl+FTINY >= wl0 && wl-FTINY <= wl0)
629 <                                clr->ssamp[i] = scale*va[pos];
628 >                        if (wl+FTINY >= wl0 & wl-FTINY <= wl0)
629 >                                clr->ssamp[i] = scale*va[pos] + .5;
630                          else            /* interpolate if necessary */
631 <                                clr->ssamp[i] = scale / wlstep *
631 >                                clr->ssamp[i] = .5 + scale / wlstep *
632                                                  ( va[pos]*(wl0+wlstep - wl) +
633                                                          va[pos+1]*(wl - wl0) );
634                          clr->ssum += clr->ssamp[i];
635                  }
636          clr->flags = C_CDSPEC|C_CSSPEC;
637          clr->clock++;
531        free((MEM_PTR)va);
638          return(MG_OK);
639   }
640  
# Line 543 | Line 649 | double w1, w2;
649          register int    i;
650  
651          if ((c1->flags|c2->flags) & C_CDSPEC) {         /* spectral mixing */
652 <                c_ccvt(c1, C_CSSPEC);
653 <                c_ccvt(c2, C_CSSPEC);
654 <                w1 /= (double)c1->ssum;
655 <                w2 /= (double)c2->ssum;
652 >                c_ccvt(c1, C_CSSPEC|C_CSEFF);
653 >                c_ccvt(c2, C_CSSPEC|C_CSEFF);
654 >                w1 /= c1->eff*c1->ssum;
655 >                w2 /= c2->eff*c2->ssum;
656                  scale = 0.;
657                  for (i = 0; i < C_CNSS; i++) {
658                          cmix[i] = w1*c1->ssamp[i] + w2*c2->ssamp[i];
# Line 567 | Line 673 | double w1, w2;
673                  cres->flags = C_CDXY|C_CSXY;
674          }
675   }
676 +
677 +
678 + #define C1              3.741832e-16    /* W-m^2 */
679 + #define C2              1.4388e-2       /* m-K */
680 +
681 + #define bbsp(l,t)       (C1/((l)*(l)*(l)*(l)*(l)*(exp(C2/((t)*(l)))-1.)))
682 + #define bblm(t)         (C2/5./(t))
683 +
684 + static int
685 + setbbtemp(clr, tk)              /* set black body spectrum */
686 + register C_COLOR        *clr;
687 + double  tk;
688 + {
689 +        double  sf, wl;
690 +        register int    i;
691 +
692 +        if (tk < 1000)
693 +                return(MG_EILL);
694 +        wl = bblm(tk);                  /* scalefactor based on peak */
695 +        if (wl < C_CMINWL*1e-9)
696 +                wl = C_CMINWL*1e-9;
697 +        else if (wl > C_CMAXWL*1e-9)
698 +                wl = C_CMAXWL*1e-9;
699 +        sf = C_CMAXV/bbsp(wl,tk);
700 +        clr->ssum = 0;
701 +        for (i = 0; i < C_CNSS; i++) {
702 +                wl = (C_CMINWL + i*C_CWLI)*1e-9;
703 +                clr->ssum += clr->ssamp[i] = sf*bbsp(wl,tk) + .5;
704 +        }
705 +        clr->flags = C_CDSPEC|C_CSSPEC;
706 +        clr->clock++;
707 +        return(MG_OK);
708 + }
709 +
710 + #undef  C1
711 + #undef  C2
712 + #undef  bbsp
713 + #undef  bblm

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines