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.19 by greg, Mon Mar 13 13:00:34 1995 UTC vs.
Revision 1.29 by greg, Mon Jan 3 20:30:21 2011 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1994 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Context handlers
6   */
7  
8   #include <stdio.h>
9   #include <math.h>
10 + #include <stdlib.h>
11   #include <string.h>
12   #include "parser.h"
13   #include "lookup.h"
14  
15                                  /* default context values */
18 static C_COLOR          c_dfcolor = C_DEFCOLOR;
16   static C_MATERIAL       c_dfmaterial = C_DEFMATERIAL;
17   static C_VERTEX         c_dfvertex = C_DEFVERTEX;
18  
# Line 36 | Line 33 | static LUTAB   clr_tab = LU_SINIT(free,free);  /* color l
33   static LUTAB    mat_tab = LU_SINIT(free,free);  /* material lookup table */
34   static LUTAB    vtx_tab = LU_SINIT(free,free);  /* vertex lookup table */
35  
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
36   static int      setspectrum();
37   static int      setbbtemp();
38   static void     mixcolors();
# Line 105 | Line 57 | register char  **av;
57                          c_ccname = NULL;
58                          return(MG_OK);
59                  }
60 +                if (!isname(av[1]))
61 +                        return(MG_EILL);
62                  lp = lu_find(&clr_tab, av[1]);  /* lookup context */
63                  if (lp == NULL)
64                          return(MG_EMEM);
# Line 128 | Line 82 | register char  **av;
82                          c_ccname = lp->key;
83                          c_ccolor = (C_COLOR *)lp->data;
84                          c_ccolor->clock = 0;
85 +                        c_ccolor->client_data = NULL;
86                  }
87                  i = c_ccolor->clock;
88                  if (ac == 3) {          /* use default template */
# Line 151 | Line 106 | register char  **av;
106                  c_ccolor->cx = atof(av[1]);
107                  c_ccolor->cy = atof(av[2]);
108                  c_ccolor->flags = C_CDXY|C_CSXY;
109 <                if (c_ccolor->cx < 0. | c_ccolor->cy < 0. |
110 <                                c_ccolor->cx + c_ccolor->cy > 1.)
109 >                if ((c_ccolor->cx < 0.) | (c_ccolor->cy < 0.) |
110 >                                (c_ccolor->cx + c_ccolor->cy > 1.))
111                          return(MG_EILL);
112                  c_ccolor->clock++;
113                  return(MG_OK);
# Line 175 | Line 130 | register char  **av;
130                  if (!isflt(av[1]))
131                          return(MG_ETYPE);
132                  wsum = atof(av[1]);
178                if (wsum < 0.)
179                        return(MG_EILL);
133                  if ((lp = lu_find(&clr_tab, av[2])) == NULL)
134                          return(MG_EMEM);
135                  if (lp->data == NULL)
# Line 186 | Line 139 | register char  **av;
139                          if (!isflt(av[i]))
140                                  return(MG_ETYPE);
141                          w = atof(av[i]);
189                        if (w < 0.)
190                                return(MG_EILL);
142                          if ((lp = lu_find(&clr_tab, av[i+1])) == NULL)
143                                  return(MG_EMEM);
144                          if (lp->data == NULL)
# Line 196 | Line 147 | register char  **av;
147                                          w, (C_COLOR *)lp->data);
148                          wsum += w;
149                  }
150 +                if (wsum <= 0.)
151 +                        return(MG_EILL);
152                  c_ccolor->clock++;
153                  return(MG_OK);
154          }
# Line 221 | Line 174 | register char  **av;
174                          c_cmname = NULL;
175                          return(MG_OK);
176                  }
177 +                if (!isname(av[1]))
178 +                        return(MG_EILL);
179                  lp = lu_find(&mat_tab, av[1]);  /* lookup context */
180                  if (lp == NULL)
181                          return(MG_EMEM);
# Line 244 | Line 199 | register char  **av;
199                          c_cmname = lp->key;
200                          c_cmaterial = (C_MATERIAL *)lp->data;
201                          c_cmaterial->clock = 0;
202 +                        c_cmaterial->client_data = NULL;
203                  }
204                  i = c_cmaterial->clock;
205                  if (ac == 3) {          /* use default template */
# Line 259 | Line 215 | register char  **av;
215                  *c_cmaterial = *(C_MATERIAL *)lp->data;
216                  c_cmaterial->clock = i + 1;
217                  return(MG_OK);
218 +        case MG_E_IR:           /* set index of refraction */
219 +                if (ac != 3)
220 +                        return(MG_EARGC);
221 +                if (!isflt(av[1]) | !isflt(av[2]))
222 +                        return(MG_ETYPE);
223 +                c_cmaterial->nr = atof(av[1]);
224 +                c_cmaterial->ni = atof(av[2]);
225 +                if (c_cmaterial->nr <= FTINY)
226 +                        return(MG_EILL);
227 +                c_cmaterial->clock++;
228 +                return(MG_OK);
229          case MG_E_RD:           /* set diffuse reflectance */
230                  if (ac != 2)
231                          return(MG_EARGC);
232                  if (!isflt(av[1]))
233                          return(MG_ETYPE);
234                  c_cmaterial->rd = atof(av[1]);
235 <                if (c_cmaterial->rd < 0. | c_cmaterial->rd > 1.)
235 >                if ((c_cmaterial->rd < 0.) | (c_cmaterial->rd > 1.))
236                          return(MG_EILL);
237                  c_cmaterial->rd_c = *c_ccolor;
238                  c_cmaterial->clock++;
# Line 287 | Line 254 | register char  **av;
254                  if (!isflt(av[1]))
255                          return(MG_ETYPE);
256                  c_cmaterial->td = atof(av[1]);
257 <                if (c_cmaterial->td < 0. | c_cmaterial->td > 1.)
257 >                if ((c_cmaterial->td < 0.) | (c_cmaterial->td > 1.))
258                          return(MG_EILL);
259                  c_cmaterial->td_c = *c_ccolor;
260                  c_cmaterial->clock++;
# Line 299 | Line 266 | register char  **av;
266                          return(MG_ETYPE);
267                  c_cmaterial->rs = atof(av[1]);
268                  c_cmaterial->rs_a = atof(av[2]);
269 <                if (c_cmaterial->rs < 0. | c_cmaterial->rs > 1. |
270 <                                c_cmaterial->rs_a < 0.)
269 >                if ((c_cmaterial->rs < 0.) | (c_cmaterial->rs > 1.) |
270 >                                (c_cmaterial->rs_a < 0.))
271                          return(MG_EILL);
272                  c_cmaterial->rs_c = *c_ccolor;
273                  c_cmaterial->clock++;
# Line 312 | Line 279 | register char  **av;
279                          return(MG_ETYPE);
280                  c_cmaterial->ts = atof(av[1]);
281                  c_cmaterial->ts_a = atof(av[2]);
282 <                if (c_cmaterial->ts < 0. | c_cmaterial->ts > 1. |
283 <                                c_cmaterial->ts_a < 0.)
282 >                if ((c_cmaterial->ts < 0.) | (c_cmaterial->ts > 1.) |
283 >                                (c_cmaterial->ts_a < 0.))
284                          return(MG_EILL);
285                  c_cmaterial->ts_c = *c_ccolor;
286                  c_cmaterial->clock++;
# Line 355 | Line 322 | register char  **av;
322                          c_cvname = NULL;
323                          return(MG_OK);
324                  }
325 +                if (!isname(av[1]))
326 +                        return(MG_EILL);
327                  lp = lu_find(&vtx_tab, av[1]);  /* lookup context */
328                  if (lp == NULL)
329                          return(MG_EMEM);
# Line 377 | Line 346 | register char  **av;
346                                  return(MG_EMEM);
347                          c_cvname = lp->key;
348                          c_cvertex = (C_VERTEX *)lp->data;
349 +                        c_cvertex->clock = 0;
350 +                        c_cvertex->client_data = NULL;
351                  }
352                  i = c_cvertex->clock;
353                  if (ac == 3) {          /* use default template */
# Line 472 | Line 443 | char   *name;
443   }
444  
445  
475 int
476 c_isgrey(clr)                   /* check if color is grey */
477 register C_COLOR        *clr;
478 {
479        if (!(clr->flags & (C_CSXY|C_CSSPEC)))
480                return(1);              /* no settings == grey */
481        c_ccvt(clr, C_CSXY);
482        return(clr->cx >= .323 && clr->cx <= .343 &&
483                        clr->cy >= .323 && clr->cy <= .343);
484 }
485
486
487 void
488 c_ccvt(clr, fl)                 /* convert color representations */
489 register C_COLOR        *clr;
490 int     fl;
491 {
492        double  x, y, z;
493        register int    i;
494
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        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 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->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
446   static int
447   setspectrum(clr, wlmin, wlmax, ac, av)  /* convert a spectrum */
448   register C_COLOR        *clr;
# Line 554 | Line 456 | char   **av;
456          int     n, imax;
457          int     wl;
458          double  wl0, wlstep;
459 +        double  boxpos, boxstep;
460                                          /* check bounds */
461 <        if (wlmax <= C_CMINWL | wlmax <= wlmin | wlmin >= C_CMAXWL)
461 >        if ((wlmax <= C_CMINWL) | (wlmax <= wlmin) | (wlmin >= C_CMAXWL))
462                  return(MG_EILL);
463          wlstep = (wlmax - wlmin)/(ac-1);
464          while (wlmin < C_CMINWL) {
# Line 566 | Line 469 | char   **av;
469                  wlmax -= wlstep;
470                  ac--;
471          }
569        if (ac < 2)
570                return(MG_EILL);
472          imax = ac;                      /* box filter if necessary */
473 +        boxpos = 0;
474 +        boxstep = 1;
475          if (wlstep < C_CWLI) {
476 +                imax = (wlmax - wlmin)/C_CWLI + (1-FTINY);
477 +                boxpos = (wlmin - C_CMINWL)/C_CWLI;
478 +                boxstep = wlstep/C_CWLI;
479                  wlstep = C_CWLI;
574                imax = (wlmax - wlmin)/wlstep;
480          }
481          scale = 0.;                     /* get values and maximum */
482          pos = 0;
483          for (i = 0; i < imax; i++) {
484                  va[i] = 0.; n = 0;
485 <                while (pos < (i+.5)*ac/imax) {
485 >                while (boxpos < i+.5 && pos < ac) {
486                          if (!isflt(av[pos]))
487                                  return(MG_ETYPE);
488                          va[i] += atof(av[pos++]);
489                          n++;
490 +                        boxpos += boxstep;
491                  }
492                  if (n > 1)
493                          va[i] /= (double)n;
588                if (va[i] < 0.)
589                        return(MG_EILL);
494                  if (va[i] > scale)
495                          scale = va[i];
496 +                else if (va[i] < -scale)
497 +                        scale = -va[i];
498          }
499 <        if (scale == 0.)
499 >        if (scale <= FTINY)
500                  return(MG_EILL);
501          scale = C_CMAXV / scale;
502          clr->ssum = 0;                  /* convert to our spacing */
503          wl0 = wlmin;
504          pos = 0;
505          for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI)
506 <                if (wl < wlmin | wl > wlmax)
506 >                if ((wl < wlmin) | (wl > wlmax))
507                          clr->ssamp[i] = 0;
508                  else {
509                          while (wl0 + wlstep < wl+FTINY) {
510                                  wl0 += wlstep;
511                                  pos++;
512                          }
513 <                        if (wl+FTINY >= wl0 & wl-FTINY <= wl0)
513 >                        if ((wl+FTINY >= wl0) & (wl-FTINY <= wl0))
514                                  clr->ssamp[i] = scale*va[pos] + .5;
515                          else            /* interpolate if necessary */
516                                  clr->ssamp[i] = .5 + scale / wlstep *
# Line 646 | Line 552 | double w1, w2;
552          } else {                                        /* CIE xy mixing */
553                  c_ccvt(c1, C_CSXY);
554                  c_ccvt(c2, C_CSXY);
555 <                scale = 1. / (w1/c1->cy + w2/c2->cy);
555 >                scale = w1/c1->cy + w2/c2->cy;
556 >                if (scale == 0.)
557 >                        return;
558 >                scale = 1. / scale;
559                  cres->cx = (c1->cx*w1/c1->cy + c2->cx*w2/c2->cy) * scale;
560                  cres->cy = (w1 + w2) * scale;
561                  cres->flags = C_CDXY|C_CSXY;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines