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.26 by gregl, Mon Dec 15 09:41:37 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 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10  
11   #include <stdio.h>
12   #include <math.h>
13 + #include <stdlib.h>
14   #include <string.h>
15   #include "parser.h"
16   #include "lookup.h"
# Line 36 | Line 37 | static LUTAB   clr_tab = LU_SINIT(free,free);  /* color l
37   static LUTAB    mat_tab = LU_SINIT(free,free);  /* material lookup table */
38   static LUTAB    vtx_tab = LU_SINIT(free,free);  /* vertex lookup table */
39  
40 <                                /* CIE 1931 Standard Observer */
41 < static C_COLOR  cie_xf = C_CIEX;
42 < static C_COLOR  cie_yf = C_CIEY;
43 < static C_COLOR  cie_zf = C_CIEZ;
40 >                                /* CIE 1931 Standard Observer curves */
41 > static C_COLOR  cie_xf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
42 >                        {14,42,143,435,1344,2839,3483,3362,2908,1954,956,
43 >                        320,49,93,633,1655,2904,4334,5945,7621,9163,10263,
44 >                        10622,10026,8544,6424,4479,2835,1649,874,468,227,
45 >                        114,58,29,14,7,3,2,1,0}, 106836L, .467, .368, 362.230
46 >                        };
47 > static C_COLOR  cie_yf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
48 >                        {0,1,4,12,40,116,230,380,600,910,1390,2080,3230,
49 >                        5030,7100,8620,9540,9950,9950,9520,8700,7570,6310,
50 >                        5030,3810,2650,1750,1070,610,320,170,82,41,21,10,
51 >                        5,2,1,1,0,0}, 106856L, .398, .542, 493.525
52 >                        };
53 > static C_COLOR  cie_zf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
54 >                        {65,201,679,2074,6456,13856,17471,17721,16692,
55 >                        12876,8130,4652,2720,1582,782,422,203,87,39,21,17,
56 >                        11,8,3,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
57 >                        106770L, .147, .077, 54.363
58 >                        };
59 >                                /* Derived CIE 1931 Primaries (imaginary) */
60 > static C_COLOR  cie_xp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY,
61 >                        {-174,-198,-195,-197,-202,-213,-235,-272,-333,
62 >                        -444,-688,-1232,-2393,-4497,-6876,-6758,-5256,
63 >                        -3100,-815,1320,3200,4782,5998,6861,7408,7754,
64 >                        7980,8120,8199,8240,8271,8292,8309,8283,8469,
65 >                        8336,8336,8336,8336,8336,8336},
66 >                        127424L, 1., .0,
67 >                        };
68 > static C_COLOR  cie_yp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY,
69 >                        {-451,-431,-431,-430,-427,-417,-399,-366,-312,
70 >                        -204,57,691,2142,4990,8810,9871,9122,7321,5145,
71 >                        3023,1123,-473,-1704,-2572,-3127,-3474,-3704,
72 >                        -3846,-3927,-3968,-3999,-4021,-4038,-4012,-4201,
73 >                        -4066,-4066,-4066,-4066,-4066,-4066},
74 >                        -23035L, .0, 1.,
75 >                        };
76 > static C_COLOR  cie_zp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY,
77 >                        {4051,4054,4052,4053,4054,4056,4059,4064,4071,
78 >                        4074,4056,3967,3677,2933,1492,313,-440,-795,
79 >                        -904,-918,-898,-884,-869,-863,-855,-855,-851,
80 >                        -848,-847,-846,-846,-846,-845,-846,-843,-845,
81 >                        -845,-845,-845,-845,-845},
82 >                        36057L, .0, .0,
83 >                        };
84  
85   static int      setspectrum();
86 + static int      setbbtemp();
87   static void     mixcolors();
88  
89  
# Line 64 | Line 106 | register char  **av;
106                          c_ccname = NULL;
107                          return(MG_OK);
108                  }
109 +                if (!isname(av[1]))
110 +                        return(MG_EILL);
111                  lp = lu_find(&clr_tab, av[1]);  /* lookup context */
112                  if (lp == NULL)
113                          return(MG_EMEM);
# Line 87 | Line 131 | register char  **av;
131                          c_ccname = lp->key;
132                          c_ccolor = (C_COLOR *)lp->data;
133                          c_ccolor->clock = 0;
134 +                        c_ccolor->client_data = NULL;
135                  }
136                  i = c_ccolor->clock;
137                  if (ac == 3) {          /* use default template */
# Line 105 | Line 150 | register char  **av;
150          case MG_E_CXY:          /* assign CIE XY value */
151                  if (ac != 3)
152                          return(MG_EARGC);
153 <                if (!isflt(av[1]) || !isflt(av[2]))
153 >                if (!isflt(av[1]) | !isflt(av[2]))
154                          return(MG_ETYPE);
155                  c_ccolor->cx = atof(av[1]);
156                  c_ccolor->cy = atof(av[2]);
# Line 118 | Line 163 | register char  **av;
163          case MG_E_CSPEC:        /* assign spectral values */
164                  if (ac < 5)
165                          return(MG_EARGC);
166 <                if (!isflt(av[1]) || !isflt(av[2]))
166 >                if (!isflt(av[1]) | !isflt(av[2]))
167                          return(MG_ETYPE);
168                  return(setspectrum(c_ccolor, atof(av[1]), atof(av[2]),
169                                  ac-3, av+3));
170 +        case MG_E_CCT:          /* assign black body spectrum */
171 +                if (ac != 2)
172 +                        return(MG_EARGC);
173 +                if (!isflt(av[1]))
174 +                        return(MG_ETYPE);
175 +                return(setbbtemp(c_ccolor, atof(av[1])));
176          case MG_E_CMIX:         /* mix colors */
177                  if (ac < 5 || (ac-1)%2)
178                          return(MG_EARGC);
179                  if (!isflt(av[1]))
180                          return(MG_ETYPE);
181                  wsum = atof(av[1]);
131                if (wsum < 0.)
132                        return(MG_EILL);
182                  if ((lp = lu_find(&clr_tab, av[2])) == NULL)
183                          return(MG_EMEM);
184                  if (lp->data == NULL)
# Line 139 | Line 188 | register char  **av;
188                          if (!isflt(av[i]))
189                                  return(MG_ETYPE);
190                          w = atof(av[i]);
142                        if (w < 0.)
143                                return(MG_EILL);
191                          if ((lp = lu_find(&clr_tab, av[i+1])) == NULL)
192                                  return(MG_EMEM);
193                          if (lp->data == NULL)
# Line 149 | Line 196 | register char  **av;
196                                          w, (C_COLOR *)lp->data);
197                          wsum += w;
198                  }
199 +                if (wsum <= 0.)
200 +                        return(MG_EILL);
201                  c_ccolor->clock++;
202                  return(MG_OK);
203          }
# 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 197 | Line 248 | register char  **av;
248                          c_cmname = lp->key;
249                          c_cmaterial = (C_MATERIAL *)lp->data;
250                          c_cmaterial->clock = 0;
251 +                        c_cmaterial->client_data = NULL;
252                  }
253                  i = c_cmaterial->clock;
254                  if (ac == 3) {          /* use default template */
# Line 212 | Line 264 | register char  **av;
264                  *c_cmaterial = *(C_MATERIAL *)lp->data;
265                  c_cmaterial->clock = i + 1;
266                  return(MG_OK);
267 +        case MG_E_IR:           /* set index of refraction */
268 +                if (ac != 3)
269 +                        return(MG_EARGC);
270 +                if (!isflt(av[1]) | !isflt(av[2]))
271 +                        return(MG_ETYPE);
272 +                c_cmaterial->nr = atof(av[1]);
273 +                c_cmaterial->ni = atof(av[2]);
274 +                if (c_cmaterial->nr <= FTINY)
275 +                        return(MG_EILL);
276 +                c_cmaterial->clock++;
277 +                return(MG_OK);
278          case MG_E_RD:           /* set diffuse reflectance */
279                  if (ac != 2)
280                          return(MG_EARGC);
# Line 248 | Line 311 | register char  **av;
311          case MG_E_RS:           /* set specular reflectance */
312                  if (ac != 3)
313                          return(MG_EARGC);
314 <                if (!isflt(av[1]) || !isflt(av[2]))
314 >                if (!isflt(av[1]) | !isflt(av[2]))
315                          return(MG_ETYPE);
316                  c_cmaterial->rs = atof(av[1]);
317                  c_cmaterial->rs_a = atof(av[2]);
# Line 261 | Line 324 | register char  **av;
324          case MG_E_TS:           /* set specular transmittance */
325                  if (ac != 3)
326                          return(MG_EARGC);
327 <                if (!isflt(av[1]) || !isflt(av[2]))
327 >                if (!isflt(av[1]) | !isflt(av[2]))
328                          return(MG_ETYPE);
329                  c_cmaterial->ts = atof(av[1]);
330                  c_cmaterial->ts_a = atof(av[2]);
# Line 308 | Line 371 | register char  **av;
371                          c_cvname = NULL;
372                          return(MG_OK);
373                  }
374 +                if (!isname(av[1]))
375 +                        return(MG_EILL);
376                  lp = lu_find(&vtx_tab, av[1]);  /* lookup context */
377                  if (lp == NULL)
378                          return(MG_EMEM);
# Line 330 | Line 395 | register char  **av;
395                                  return(MG_EMEM);
396                          c_cvname = lp->key;
397                          c_cvertex = (C_VERTEX *)lp->data;
398 +                        c_cvertex->clock = 0;
399 +                        c_cvertex->client_data = NULL;
400                  }
401                  i = c_cvertex->clock;
402                  if (ac == 3) {          /* use default template */
# Line 348 | Line 415 | register char  **av;
415          case MG_E_POINT:        /* set point */
416                  if (ac != 4)
417                          return(MG_EARGC);
418 <                if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3]))
418 >                if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3]))
419                          return(MG_ETYPE);
420                  c_cvertex->p[0] = atof(av[1]);
421                  c_cvertex->p[1] = atof(av[2]);
# Line 358 | Line 425 | register char  **av;
425          case MG_E_NORMAL:       /* set normal */
426                  if (ac != 4)
427                          return(MG_EARGC);
428 <                if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3]))
428 >                if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3]))
429                          return(MG_ETYPE);
430                  c_cvertex->n[0] = atof(av[1]);
431                  c_cvertex->n[1] = atof(av[2]);
# Line 465 | Line 532 | int    fl;
532                  clr->cy = y / z;
533                  clr->flags |= C_CSXY;
534          } else if (fl & C_CSSPEC) {     /* cxy -> cspec */
535 <                z = (cie_xf.ssum + cie_yf.ssum + cie_zf.ssum) / 3.;
536 <                x = clr->cx * z / cie_xf.ssum;
537 <                y = clr->cy * z / cie_yf.ssum;
471 <                z = (1. - clr->cx - clr->cy) * z / cie_zf.ssum;
535 >                x = clr->cx;
536 >                y = clr->cy;
537 >                z = 1. - x - y;
538                  clr->ssum = 0;
539 <                for (i = 0; i < C_CNSS; i++)
540 <                        clr->ssum += clr->ssamp[i] =
541 <                                        x * cie_xf.ssamp[i] +
542 <                                        y * cie_yf.ssamp[i] +
543 <                                        z * cie_zf.ssamp[i] ;
539 >                for (i = 0; i < C_CNSS; i++) {
540 >                        clr->ssamp[i] = x*cie_xp.ssamp[i] + y*cie_yp.ssamp[i]
541 >                                        + z*cie_zp.ssamp[i] + .5;
542 >                        if (clr->ssamp[i] < 0)          /* out of gamut! */
543 >                                clr->ssamp[i] = 0;
544 >                        else
545 >                                clr->ssum += clr->ssamp[i];
546 >                }
547                  clr->flags |= C_CSSPEC;
548          }
549          if (fl & C_CSEFF) {             /* compute efficacy */
550 <                if (clr->flags & C_CDSPEC) {            /* from spectrum */
550 >                if (clr->flags & C_CSSPEC) {            /* from spectrum */
551                          y = 0.;
552                          for (i = 0; i < C_CNSS; i++)
553                                  y += cie_yf.ssamp[i] * clr->ssamp[i];
554                          clr->eff = C_CLPWM * y / clr->ssum;
555 <                } else /* clr->flags & C_CDXY */ {      /* from (x,y) */
555 >                } else /* clr->flags & C_CSXY */ {      /* from (x,y) */
556                          clr->eff = clr->cx*cie_xf.eff + clr->cy*cie_yf.eff +
557                                          (1. - clr->cx - clr->cy)*cie_zf.eff;
558                  }
# Line 505 | Line 574 | char   **av;
574          int     n, imax;
575          int     wl;
576          double  wl0, wlstep;
577 +        double  boxpos, boxstep;
578                                          /* check bounds */
579          if (wlmax <= C_CMINWL | wlmax <= wlmin | wlmin >= C_CMAXWL)
580                  return(MG_EILL);
# Line 517 | Line 587 | char   **av;
587                  wlmax -= wlstep;
588                  ac--;
589          }
520        if (ac < 2)
521                return(MG_EILL);
590          imax = ac;                      /* box filter if necessary */
591 +        boxpos = 0;
592 +        boxstep = 1;
593          if (wlstep < C_CWLI) {
594 +                imax = (wlmax - wlmin)/C_CWLI + (1-FTINY);
595 +                boxpos = (wlmin - C_CMINWL)/C_CWLI;
596 +                boxstep = wlstep/C_CWLI;
597                  wlstep = C_CWLI;
525                imax = (wlmax - wlmin)/wlstep;
598          }
599          scale = 0.;                     /* get values and maximum */
600          pos = 0;
601          for (i = 0; i < imax; i++) {
602                  va[i] = 0.; n = 0;
603 <                while (pos < (i+.5)*ac/imax) {
603 >                while (boxpos < i+.5 && pos < ac) {
604                          if (!isflt(av[pos]))
605                                  return(MG_ETYPE);
606                          va[i] += atof(av[pos++]);
607                          n++;
608 +                        boxpos += boxstep;
609                  }
610                  if (n > 1)
611                          va[i] /= (double)n;
539                if (va[i] < 0.)
540                        return(MG_EILL);
612                  if (va[i] > scale)
613                          scale = va[i];
614 +                else if (va[i] < -scale)
615 +                        scale = -va[i];
616          }
617 <        if (scale == 0.)
617 >        if (scale <= FTINY)
618                  return(MG_EILL);
619          scale = C_CMAXV / scale;
620          clr->ssum = 0;                  /* convert to our spacing */
# Line 556 | Line 629 | char   **av;
629                                  pos++;
630                          }
631                          if (wl+FTINY >= wl0 & wl-FTINY <= wl0)
632 <                                clr->ssamp[i] = scale*va[pos];
632 >                                clr->ssamp[i] = scale*va[pos] + .5;
633                          else            /* interpolate if necessary */
634 <                                clr->ssamp[i] = scale / wlstep *
634 >                                clr->ssamp[i] = .5 + scale / wlstep *
635                                                  ( va[pos]*(wl0+wlstep - wl) +
636                                                          va[pos+1]*(wl - wl0) );
637                          clr->ssum += clr->ssamp[i];
# Line 597 | Line 670 | double w1, w2;
670          } else {                                        /* CIE xy mixing */
671                  c_ccvt(c1, C_CSXY);
672                  c_ccvt(c2, C_CSXY);
673 <                scale = 1. / (w1/c1->cy + w2/c2->cy);
673 >                scale = w1/c1->cy + w2/c2->cy;
674 >                if (scale == 0.)
675 >                        return;
676 >                scale = 1. / scale;
677                  cres->cx = (c1->cx*w1/c1->cy + c2->cx*w2/c2->cy) * scale;
678                  cres->cy = (w1 + w2) * scale;
679                  cres->flags = C_CDXY|C_CSXY;
680          }
681   }
682 +
683 +
684 + #define C1              3.741832e-16    /* W-m^2 */
685 + #define C2              1.4388e-2       /* m-K */
686 +
687 + #define bbsp(l,t)       (C1/((l)*(l)*(l)*(l)*(l)*(exp(C2/((t)*(l)))-1.)))
688 + #define bblm(t)         (C2/5./(t))
689 +
690 + static int
691 + setbbtemp(clr, tk)              /* set black body spectrum */
692 + register C_COLOR        *clr;
693 + double  tk;
694 + {
695 +        double  sf, wl;
696 +        register int    i;
697 +
698 +        if (tk < 1000)
699 +                return(MG_EILL);
700 +        wl = bblm(tk);                  /* scalefactor based on peak */
701 +        if (wl < C_CMINWL*1e-9)
702 +                wl = C_CMINWL*1e-9;
703 +        else if (wl > C_CMAXWL*1e-9)
704 +                wl = C_CMAXWL*1e-9;
705 +        sf = C_CMAXV/bbsp(wl,tk);
706 +        clr->ssum = 0;
707 +        for (i = 0; i < C_CNSS; i++) {
708 +                wl = (C_CMINWL + i*C_CWLI)*1e-9;
709 +                clr->ssum += clr->ssamp[i] = sf*bbsp(wl,tk) + .5;
710 +        }
711 +        clr->flags = C_CDSPEC|C_CSSPEC;
712 +        clr->clock++;
713 +        return(MG_OK);
714 + }
715 +
716 + #undef  C1
717 + #undef  C2
718 + #undef  bbsp
719 + #undef  bblm

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines