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.13 by greg, Fri Jul 1 18:06:56 1994 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 */
40 static C_COLOR  cie_xf = C_CIEX;
41 static C_COLOR  cie_yf = C_CIEY;
42 static C_COLOR  cie_zf = C_CIEZ;
43
36   static int      setspectrum();
37 + static int      setbbtemp();
38   static void     mixcolors();
39  
40  
# Line 64 | 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);
65 +                c_ccname = lp->key;
66                  c_ccolor = (C_COLOR *)lp->data;
67                  if (ac == 2) {          /* reestablish previous context */
68                          if (c_ccolor == NULL)
# Line 83 | Line 79 | register char  **av;
79                          lp->data = (char *)malloc(sizeof(C_COLOR));
80                          if (lp->data == NULL)
81                                  return(MG_EMEM);
82 +                        c_ccname = lp->key;
83                          c_ccolor = (C_COLOR *)lp->data;
84                          c_ccolor->clock = 0;
85 +                        c_ccolor->client_data = NULL;
86                  }
89                c_ccname = lp->key;
87                  i = c_ccolor->clock;
88                  if (ac == 3) {          /* use default template */
89                          *c_ccolor = c_dfcolor;
# Line 104 | Line 101 | register char  **av;
101          case MG_E_CXY:          /* assign CIE XY value */
102                  if (ac != 3)
103                          return(MG_EARGC);
104 <                if (!isflt(av[1]) || !isflt(av[2]))
104 >                if (!isflt(av[1]) | !isflt(av[2]))
105                          return(MG_ETYPE);
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);
114          case MG_E_CSPEC:        /* assign spectral values */
115                  if (ac < 5)
116                          return(MG_EARGC);
117 <                if (!isint(av[1]) || !isint(av[2]))
117 >                if (!isflt(av[1]) | !isflt(av[2]))
118                          return(MG_ETYPE);
119 <                return(setspectrum(c_ccolor, atoi(av[1]), atoi(av[2]),
119 >                return(setspectrum(c_ccolor, atof(av[1]), atof(av[2]),
120                                  ac-3, av+3));
121 +        case MG_E_CCT:          /* assign black body spectrum */
122 +                if (ac != 2)
123 +                        return(MG_EARGC);
124 +                if (!isflt(av[1]))
125 +                        return(MG_ETYPE);
126 +                return(setbbtemp(c_ccolor, atof(av[1])));
127          case MG_E_CMIX:         /* mix colors */
128                  if (ac < 5 || (ac-1)%2)
129                          return(MG_EARGC);
130                  if (!isflt(av[1]))
131                          return(MG_ETYPE);
132                  wsum = atof(av[1]);
130                if (wsum < 0.)
131                        return(MG_EILL);
133                  if ((lp = lu_find(&clr_tab, av[2])) == NULL)
134                          return(MG_EMEM);
135                  if (lp->data == NULL)
# Line 138 | Line 139 | register char  **av;
139                          if (!isflt(av[i]))
140                                  return(MG_ETYPE);
141                          w = atof(av[i]);
141                        if (w < 0.)
142                                return(MG_EILL);
142                          if ((lp = lu_find(&clr_tab, av[i+1])) == NULL)
143                                  return(MG_EMEM);
144                          if (lp->data == NULL)
# Line 148 | 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 173 | 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);
182 +                c_cmname = lp->key;
183                  c_cmaterial = (C_MATERIAL *)lp->data;
184                  if (ac == 2) {          /* reestablish previous context */
185                          if (c_cmaterial == NULL)
# Line 192 | Line 196 | register char  **av;
196                          lp->data = (char *)malloc(sizeof(C_MATERIAL));
197                          if (lp->data == NULL)
198                                  return(MG_EMEM);
199 +                        c_cmname = lp->key;
200                          c_cmaterial = (C_MATERIAL *)lp->data;
201                          c_cmaterial->clock = 0;
202 +                        c_cmaterial->client_data = NULL;
203                  }
198                c_cmname = lp->key;
204                  i = c_cmaterial->clock;
205                  if (ac == 3) {          /* use default template */
206                          *c_cmaterial = c_dfmaterial;
# Line 210 | 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 238 | 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 246 | Line 262 | register char  **av;
262          case MG_E_RS:           /* set specular reflectance */
263                  if (ac != 3)
264                          return(MG_EARGC);
265 <                if (!isflt(av[1]) || !isflt(av[2]))
265 >                if (!isflt(av[1]) | !isflt(av[2]))
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 259 | Line 275 | register char  **av;
275          case MG_E_TS:           /* set specular transmittance */
276                  if (ac != 3)
277                          return(MG_EARGC);
278 <                if (!isflt(av[1]) || !isflt(av[2]))
278 >                if (!isflt(av[1]) | !isflt(av[2]))
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 306 | 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);
330 +                c_cvname = lp->key;
331                  c_cvertex = (C_VERTEX *)lp->data;
332                  if (ac == 2) {          /* reestablish previous context */
333                          if (c_cvertex == NULL)
# Line 325 | Line 344 | register char  **av;
344                          lp->data = (char *)malloc(sizeof(C_VERTEX));
345                          if (lp->data == NULL)
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                  }
330                c_cvname = lp->key;
352                  i = c_cvertex->clock;
353                  if (ac == 3) {          /* use default template */
354                          *c_cvertex = c_dfvertex;
# Line 345 | Line 366 | register char  **av;
366          case MG_E_POINT:        /* set point */
367                  if (ac != 4)
368                          return(MG_EARGC);
369 <                if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3]))
369 >                if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3]))
370                          return(MG_ETYPE);
371                  c_cvertex->p[0] = atof(av[1]);
372                  c_cvertex->p[1] = atof(av[2]);
# Line 355 | Line 376 | register char  **av;
376          case MG_E_NORMAL:       /* set normal */
377                  if (ac != 4)
378                          return(MG_EARGC);
379 <                if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3]))
379 >                if (!isflt(av[1]) | !isflt(av[2]) | !isflt(av[3]))
380                          return(MG_ETYPE);
381                  c_cvertex->n[0] = atof(av[1]);
382                  c_cvertex->n[1] = atof(av[2]);
# Line 373 | Line 394 | c_clearall()                   /* empty context tables */
394   {
395          c_uncolor = c_dfcolor;
396          c_ccolor = &c_uncolor;
397 +        c_ccname = NULL;
398          lu_done(&clr_tab);
399          c_unmaterial = c_dfmaterial;
400          c_cmaterial = &c_unmaterial;
401 +        c_cmname = NULL;
402          lu_done(&mat_tab);
403          c_unvertex = c_dfvertex;
404          c_cvertex = &c_unvertex;
405 +        c_cvname = NULL;
406          lu_done(&vtx_tab);
407   }
408  
# Line 419 | Line 443 | char   *name;
443   }
444  
445  
422 int
423 c_isgrey(clr)                   /* check if color is grey */
424 register C_COLOR        *clr;
425 {
426        if (!(clr->flags & (C_CSXY|C_CSSPEC)))
427                return(1);              /* no settings == grey */
428        c_ccvt(clr, C_CSXY);
429        return(clr->cx >= .323 && clr->cx <= .343 &&
430                        clr->cy >= .323 && clr->cy <= .343);
431 }
432
433
434 void
435 c_ccvt(clr, fl)                 /* convert color representations */
436 register C_COLOR        *clr;
437 int     fl;
438 {
439        double  x, y, z;
440        register int    i;
441
442        if (clr->flags & fl)            /* already done */
443                return;
444        if (!(clr->flags & (C_CSXY|C_CSSPEC)))  /* nothing set! */
445                *clr = c_dfcolor;
446        else if (fl & C_CSXY) {         /* cspec -> cxy */
447                x = y = z = 0.;
448                for (i = 0; i < C_CNSS; i++) {
449                        x += cie_xf.ssamp[i] * clr->ssamp[i];
450                        y += cie_yf.ssamp[i] * clr->ssamp[i];
451                        z += cie_zf.ssamp[i] * clr->ssamp[i];
452                }
453                z += x + y;
454                clr->cx = x / z;
455                clr->cy = y / z;
456                clr->flags |= C_CSXY;
457        } else {                        /* cxy -> cspec */
458                z = (cie_xf.ssum + cie_yf.ssum + cie_zf.ssum) / 3.;
459                x = clr->cx * z / cie_xf.ssum;
460                y = clr->cy * z / cie_yf.ssum;
461                z = (1. - clr->cx - clr->cy) * z / cie_zf.ssum;
462                clr->ssum = 0;
463                for (i = 0; i < C_CNSS; i++)
464                        clr->ssum += clr->ssamp[i] =
465                                        x * cie_xf.ssamp[i] +
466                                        y * cie_yf.ssamp[i] +
467                                        z * cie_zf.ssamp[i] ;
468                clr->flags |= C_CSSPEC;
469        }
470 }
471
472
446   static int
447   setspectrum(clr, wlmin, wlmax, ac, av)  /* convert a spectrum */
448   register C_COLOR        *clr;
449 < int     wlmin, wlmax;
449 > double  wlmin, wlmax;
450   int     ac;
451   char    **av;
452   {
453          double  scale;
454 <        float   *va;
455 <        register int    i;
456 <        int     wl, pos;
454 >        float   va[C_CNSS];
455 >        register int    i, pos;
456 >        int     n, imax;
457 >        int     wl;
458          double  wl0, wlstep;
459 <
460 <        if (wlmin < C_CMINWL || wlmin >= wlmax || wlmax > C_CMAXWL)
459 >        double  boxpos, boxstep;
460 >                                        /* check bounds */
461 >        if ((wlmax <= C_CMINWL) | (wlmax <= wlmin) | (wlmin >= C_CMAXWL))
462                  return(MG_EILL);
463 <        if ((va = (float *)malloc(ac*sizeof(float))) == NULL)
464 <                return(MG_EMEM);
463 >        wlstep = (wlmax - wlmin)/(ac-1);
464 >        while (wlmin < C_CMINWL) {
465 >                wlmin += wlstep;
466 >                ac--; av++;
467 >        }
468 >        while (wlmax > C_CMAXWL) {
469 >                wlmax -= wlstep;
470 >                ac--;
471 >        }
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;
480 >        }
481          scale = 0.;                     /* get values and maximum */
482 <        for (i = 0; i < ac; i++) {
483 <                if (!isflt(av[i]))
484 <                        return(MG_ETYPE);
485 <                va[i] = atof(av[i]);
486 <                if (va[i] < 0.)
487 <                        return(MG_EILL);
482 >        pos = 0;
483 >        for (i = 0; i < imax; i++) {
484 >                va[i] = 0.; n = 0;
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;
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;
505        wlstep = (double)(wlmax - wlmin)/(ac-1);
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)
514 <                                clr->ssamp[i] = scale*va[pos];
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] = scale / wlstep *
516 >                                clr->ssamp[i] = .5 + scale / wlstep *
517                                                  ( va[pos]*(wl0+wlstep - wl) +
518                                                          va[pos+1]*(wl - wl0) );
519                          clr->ssum += clr->ssamp[i];
520                  }
521          clr->flags = C_CDSPEC|C_CSSPEC;
522          clr->clock++;
525        free((MEM_PTR)va);
523          return(MG_OK);
524   }
525  
# Line 537 | Line 534 | double w1, w2;
534          register int    i;
535  
536          if ((c1->flags|c2->flags) & C_CDSPEC) {         /* spectral mixing */
537 <                c_ccvt(c1, C_CSSPEC);
538 <                c_ccvt(c2, C_CSSPEC);
539 <                w1 /= (double)c1->ssum;
540 <                w2 /= (double)c2->ssum;
537 >                c_ccvt(c1, C_CSSPEC|C_CSEFF);
538 >                c_ccvt(c2, C_CSSPEC|C_CSEFF);
539 >                w1 /= c1->eff*c1->ssum;
540 >                w2 /= c2->eff*c2->ssum;
541                  scale = 0.;
542                  for (i = 0; i < C_CNSS; i++) {
543                          cmix[i] = w1*c1->ssamp[i] + w2*c2->ssamp[i];
# Line 555 | 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;
562          }
563   }
564 +
565 +
566 + #define C1              3.741832e-16    /* W-m^2 */
567 + #define C2              1.4388e-2       /* m-K */
568 +
569 + #define bbsp(l,t)       (C1/((l)*(l)*(l)*(l)*(l)*(exp(C2/((t)*(l)))-1.)))
570 + #define bblm(t)         (C2/5./(t))
571 +
572 + static int
573 + setbbtemp(clr, tk)              /* set black body spectrum */
574 + register C_COLOR        *clr;
575 + double  tk;
576 + {
577 +        double  sf, wl;
578 +        register int    i;
579 +
580 +        if (tk < 1000)
581 +                return(MG_EILL);
582 +        wl = bblm(tk);                  /* scalefactor based on peak */
583 +        if (wl < C_CMINWL*1e-9)
584 +                wl = C_CMINWL*1e-9;
585 +        else if (wl > C_CMAXWL*1e-9)
586 +                wl = C_CMAXWL*1e-9;
587 +        sf = C_CMAXV/bbsp(wl,tk);
588 +        clr->ssum = 0;
589 +        for (i = 0; i < C_CNSS; i++) {
590 +                wl = (C_CMINWL + i*C_CWLI)*1e-9;
591 +                clr->ssum += clr->ssamp[i] = sf*bbsp(wl,tk) + .5;
592 +        }
593 +        clr->flags = C_CDSPEC|C_CSSPEC;
594 +        clr->clock++;
595 +        return(MG_OK);
596 + }
597 +
598 + #undef  C1
599 + #undef  C2
600 + #undef  bbsp
601 + #undef  bblm

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines