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.17 by greg, Fri Feb 24 16:50:08 1995 UTC vs.
Revision 1.25 by gregl, Fri Oct 10 16:45:51 1997 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1994 Regents of the University of California */
1 > /* Copyright (c) 1997 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# 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, NULL, 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, NULL, 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, NULL, 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, NULL, 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, NULL, 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, NULL, 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 87 | Line 130 | register char  **av;
130                          c_ccname = lp->key;
131                          c_ccolor = (C_COLOR *)lp->data;
132                          c_ccolor->clock = 0;
133 +                        c_ccolor->client_data = NULL;
134                  }
135                  i = c_ccolor->clock;
136                  if (ac == 3) {          /* use default template */
# Line 105 | Line 149 | register char  **av;
149          case MG_E_CXY:          /* assign CIE XY value */
150                  if (ac != 3)
151                          return(MG_EARGC);
152 <                if (!isflt(av[1]) || !isflt(av[2]))
152 >                if (!isflt(av[1]) | !isflt(av[2]))
153                          return(MG_ETYPE);
154                  c_ccolor->cx = atof(av[1]);
155                  c_ccolor->cy = atof(av[2]);
# Line 118 | Line 162 | register char  **av;
162          case MG_E_CSPEC:        /* assign spectral values */
163                  if (ac < 5)
164                          return(MG_EARGC);
165 <                if (!isflt(av[1]) || !isflt(av[2]))
165 >                if (!isflt(av[1]) | !isflt(av[2]))
166                          return(MG_ETYPE);
167                  return(setspectrum(c_ccolor, atof(av[1]), atof(av[2]),
168                                  ac-3, av+3));
169 +        case MG_E_CCT:          /* assign black body spectrum */
170 +                if (ac != 2)
171 +                        return(MG_EARGC);
172 +                if (!isflt(av[1]))
173 +                        return(MG_ETYPE);
174 +                return(setbbtemp(c_ccolor, atof(av[1])));
175          case MG_E_CMIX:         /* mix colors */
176                  if (ac < 5 || (ac-1)%2)
177                          return(MG_EARGC);
178                  if (!isflt(av[1]))
179                          return(MG_ETYPE);
180                  wsum = atof(av[1]);
131                if (wsum < 0.)
132                        return(MG_EILL);
181                  if ((lp = lu_find(&clr_tab, av[2])) == NULL)
182                          return(MG_EMEM);
183                  if (lp->data == NULL)
# Line 139 | Line 187 | register char  **av;
187                          if (!isflt(av[i]))
188                                  return(MG_ETYPE);
189                          w = atof(av[i]);
142                        if (w < 0.)
143                                return(MG_EILL);
190                          if ((lp = lu_find(&clr_tab, av[i+1])) == NULL)
191                                  return(MG_EMEM);
192                          if (lp->data == NULL)
# Line 149 | Line 195 | register char  **av;
195                                          w, (C_COLOR *)lp->data);
196                          wsum += w;
197                  }
198 +                if (wsum <= 0.)
199 +                        return(MG_EILL);
200                  c_ccolor->clock++;
201                  return(MG_OK);
202          }
# Line 174 | Line 222 | register char  **av;
222                          c_cmname = NULL;
223                          return(MG_OK);
224                  }
225 +                if (!isname(av[1]))
226 +                        return(MG_EILL);
227                  lp = lu_find(&mat_tab, av[1]);  /* lookup context */
228                  if (lp == NULL)
229                          return(MG_EMEM);
# Line 197 | Line 247 | register char  **av;
247                          c_cmname = lp->key;
248                          c_cmaterial = (C_MATERIAL *)lp->data;
249                          c_cmaterial->clock = 0;
250 +                        c_cmaterial->client_data = NULL;
251                  }
252                  i = c_cmaterial->clock;
253                  if (ac == 3) {          /* use default template */
# 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 330 | Line 394 | register char  **av;
394                                  return(MG_EMEM);
395                          c_cvname = lp->key;
396                          c_cvertex = (C_VERTEX *)lp->data;
397 +                        c_cvertex->clock = 0;
398 +                        c_cvertex->client_data = NULL;
399                  }
400                  i = c_cvertex->clock;
401                  if (ac == 3) {          /* use default template */
# Line 348 | Line 414 | register char  **av;
414          case MG_E_POINT:        /* set point */
415                  if (ac != 4)
416                          return(MG_EARGC);
417 <                if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3]))
417 >                if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3]))
418                          return(MG_ETYPE);
419                  c_cvertex->p[0] = atof(av[1]);
420                  c_cvertex->p[1] = atof(av[2]);
# Line 358 | Line 424 | register char  **av;
424          case MG_E_NORMAL:       /* set normal */
425                  if (ac != 4)
426                          return(MG_EARGC);
427 <                if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3]))
427 >                if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3]))
428                          return(MG_ETYPE);
429                  c_cvertex->n[0] = atof(av[1]);
430                  c_cvertex->n[1] = atof(av[2]);
# Line 465 | Line 531 | int    fl;
531                  clr->cy = y / z;
532                  clr->flags |= C_CSXY;
533          } else if (fl & C_CSSPEC) {     /* cxy -> cspec */
534 <                z = (cie_xf.ssum + cie_yf.ssum + cie_zf.ssum) / 3.;
535 <                x = clr->cx * z / cie_xf.ssum;
536 <                y = clr->cy * z / cie_yf.ssum;
471 <                z = (1. - clr->cx - clr->cy) * z / cie_zf.ssum;
534 >                x = clr->cx;
535 >                y = clr->cy;
536 >                z = 1. - x - y;
537                  clr->ssum = 0;
538 <                for (i = 0; i < C_CNSS; i++)
539 <                        clr->ssum += clr->ssamp[i] =
540 <                                        x * cie_xf.ssamp[i] +
541 <                                        y * cie_yf.ssamp[i] +
542 <                                        z * cie_zf.ssamp[i] ;
538 >                for (i = 0; i < C_CNSS; i++) {
539 >                        clr->ssamp[i] = x*cie_xp.ssamp[i] + y*cie_yp.ssamp[i]
540 >                                        + z*cie_zp.ssamp[i] + .5;
541 >                        if (clr->ssamp[i] < 0)          /* out of gamut! */
542 >                                clr->ssamp[i] = 0;
543 >                        else
544 >                                clr->ssum += clr->ssamp[i];
545 >                }
546                  clr->flags |= C_CSSPEC;
547          }
548          if (fl & C_CSEFF) {             /* compute efficacy */
549 <                if (clr->flags & C_CDSPEC) {            /* from spectrum */
549 >                if (clr->flags & C_CSSPEC) {            /* from spectrum */
550                          y = 0.;
551                          for (i = 0; i < C_CNSS; i++)
552                                  y += cie_yf.ssamp[i] * clr->ssamp[i];
553                          clr->eff = C_CLPWM * y / clr->ssum;
554 <                } else /* clr->flags & C_CDXY */ {      /* from (x,y) */
554 >                } else /* clr->flags & C_CSXY */ {      /* from (x,y) */
555                          clr->eff = clr->cx*cie_xf.eff + clr->cy*cie_yf.eff +
556                                          (1. - clr->cx - clr->cy)*cie_zf.eff;
557                  }
# Line 505 | Line 573 | char   **av;
573          int     n, imax;
574          int     wl;
575          double  wl0, wlstep;
576 +        double  boxpos, boxstep;
577                                          /* check bounds */
578          if (wlmax <= C_CMINWL | wlmax <= wlmin | wlmin >= C_CMAXWL)
579                  return(MG_EILL);
# Line 517 | Line 586 | char   **av;
586                  wlmax -= wlstep;
587                  ac--;
588          }
520        if (ac < 2)
521                return(MG_EILL);
589          imax = ac;                      /* box filter if necessary */
590 +        boxpos = 0;
591 +        boxstep = 1;
592          if (wlstep < C_CWLI) {
593 +                imax = (wlmax - wlmin)/C_CWLI + (1-FTINY);
594 +                boxpos = (wlmin - C_CMINWL)/C_CWLI;
595 +                boxstep = wlstep/C_CWLI;
596                  wlstep = C_CWLI;
525                imax = (wlmax - wlmin)/wlstep;
597          }
598          scale = 0.;                     /* get values and maximum */
599          pos = 0;
600          for (i = 0; i < imax; i++) {
601                  va[i] = 0.; n = 0;
602 <                while (pos < (i+.5)*ac/imax) {
602 >                while (boxpos < i+.5 && pos < ac) {
603                          if (!isflt(av[pos]))
604                                  return(MG_ETYPE);
605                          va[i] += atof(av[pos++]);
606                          n++;
607 +                        boxpos += boxstep;
608                  }
609                  if (n > 1)
610                          va[i] /= (double)n;
539                if (va[i] < 0.)
540                        return(MG_EILL);
611                  if (va[i] > scale)
612                          scale = va[i];
613 +                else if (va[i] < -scale)
614 +                        scale = -va[i];
615          }
616 <        if (scale == 0.)
616 >        if (scale <= FTINY)
617                  return(MG_EILL);
618          scale = C_CMAXV / scale;
619          clr->ssum = 0;                  /* convert to our spacing */
# Line 556 | Line 628 | char   **av;
628                                  pos++;
629                          }
630                          if (wl+FTINY >= wl0 & wl-FTINY <= wl0)
631 <                                clr->ssamp[i] = scale*va[pos];
631 >                                clr->ssamp[i] = scale*va[pos] + .5;
632                          else            /* interpolate if necessary */
633 <                                clr->ssamp[i] = scale / wlstep *
633 >                                clr->ssamp[i] = .5 + scale / wlstep *
634                                                  ( va[pos]*(wl0+wlstep - wl) +
635                                                          va[pos+1]*(wl - wl0) );
636                          clr->ssum += clr->ssamp[i];
# Line 597 | Line 669 | double w1, w2;
669          } else {                                        /* CIE xy mixing */
670                  c_ccvt(c1, C_CSXY);
671                  c_ccvt(c2, C_CSXY);
672 <                scale = 1. / (w1/c1->cy + w2/c2->cy);
672 >                scale = w1/c1->cy + w2/c2->cy;
673 >                if (scale == 0.)
674 >                        return;
675 >                scale = 1. / scale;
676                  cres->cx = (c1->cx*w1/c1->cy + c2->cx*w2/c2->cy) * scale;
677                  cres->cy = (w1 + w2) * scale;
678                  cres->flags = C_CDXY|C_CSXY;
679          }
680   }
681 +
682 +
683 + #define C1              3.741832e-16    /* W-m^2 */
684 + #define C2              1.4388e-2       /* m-K */
685 +
686 + #define bbsp(l,t)       (C1/((l)*(l)*(l)*(l)*(l)*(exp(C2/((t)*(l)))-1.)))
687 + #define bblm(t)         (C2/5./(t))
688 +
689 + static int
690 + setbbtemp(clr, tk)              /* set black body spectrum */
691 + register C_COLOR        *clr;
692 + double  tk;
693 + {
694 +        double  sf, wl;
695 +        register int    i;
696 +
697 +        if (tk < 1000)
698 +                return(MG_EILL);
699 +        wl = bblm(tk);                  /* scalefactor based on peak */
700 +        if (wl < C_CMINWL*1e-9)
701 +                wl = C_CMINWL*1e-9;
702 +        else if (wl > C_CMAXWL*1e-9)
703 +                wl = C_CMAXWL*1e-9;
704 +        sf = C_CMAXV/bbsp(wl,tk);
705 +        clr->ssum = 0;
706 +        for (i = 0; i < C_CNSS; i++) {
707 +                wl = (C_CMINWL + i*C_CWLI)*1e-9;
708 +                clr->ssum += clr->ssamp[i] = sf*bbsp(wl,tk) + .5;
709 +        }
710 +        clr->flags = C_CDSPEC|C_CSSPEC;
711 +        clr->clock++;
712 +        return(MG_OK);
713 + }
714 +
715 + #undef  C1
716 + #undef  C2
717 + #undef  bbsp
718 + #undef  bblm

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines