ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapmat.c
(Generate patch)

Comparing ray/src/rt/pmapmat.c (file contents):
Revision 2.1 by greg, Tue Feb 24 19:39:27 2015 UTC vs.
Revision 2.8 by greg, Tue Aug 18 18:45:55 2015 UTC

# Line 1 | Line 1
1 + #ifndef lint
2 + static const char RCSid[] = "$Id$";
3 + #endif
4   /*
5     ==================================================================
6     Photon map support routines for scattering by materials.
7  
8     Roland Schregle (roland.schregle@{hslu.ch, gmail.com})
9     (c) Fraunhofer Institute for Solar Energy Systems,
10 <       Lucerne University of Applied Sciences & Arts
10 >   (c) Lucerne University of Applied Sciences and Arts,
11 >   supported by the Swiss National Science Foundation (SNSF, #147053)
12     ==================================================================
13    
14     $Id$
# Line 113 | Line 117 | void photonRay (const RAY *rayIn, RAY *rayOut,
117        VCOPY(rayOut -> rdir, rayIn -> rdir);
118     }
119     else if (fluxAtten) {
120 <      /* Attenuate and normalised flux for scattered rays */
120 >      /* Attenuate and normalise flux for scattered rays */
121        multcolor(rayOut -> rcol, fluxAtten);
122        colorNorm(rayOut -> rcol);
123     }
# Line 198 | Line 202 | static int isoSpecPhotonScatter (NORMDAT *nd, RAY *ray
202     int      niter, i = 0;
203    
204     /* Set up sample coordinates */  
205 <   do {
202 <      v [0] = v [1] = v [2] = 0;
203 <      v [i++] = 1;
204 <      fcross(u, v, nd -> pnorm);
205 <   } while (normalize(u) < FTINY);
206 <  
205 >   getperpendicular(u, nd -> pnorm, 1);  
206     fcross(v, nd -> pnorm, u);
207    
208     if (nd -> specfl & SP_REFL) {
# Line 267 | Line 266 | static void diffPhotonScatter (FVECT normal, RAY* rayO
266     int         i = 0;
267  
268     /* Set up sample coordinates */
269 <   do {
271 <      v [0] = v [1] = v [2] = 0;
272 <      v [i++] = 1;
273 <      fcross(u, v, normal);
274 <   } while (normalize(u) < FTINY);
275 <  
269 >   getperpendicular(u, normal, 1);
270     fcross(v, normal, u);
271    
272     /* Convert theta & phi to cartesian */
# Line 323 | Line 317 | static int normalPhotonScatter (OBJREC *mat, RAY *rayI
317        nd.specfl |= SP_FLAT;  
318        
319     /* Perturb normal */
320 <   if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > sqr(FTINY))
320 >   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY)) ))
321        nd.pdot = raynormal(nd.pnorm, rayIn);
322     else {
323        VCOPY(nd.pnorm, rayIn -> ron);
# Line 752 | Line 746 | static int dielectricPhotonScatter (OBJREC *mat, RAY *
746     /* get modifiers */
747     raytexture(rayIn, mat -> omod);                      
748    
749 <   if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > FTINY * FTINY)
749 >   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > FTINY * FTINY)))
750        /* Perturb normal */
751        cos1 = raynormal(dnorm, rayIn);
752     else {
# Line 899 | Line 893 | static int glassPhotonScatter (OBJREC *mat, RAY *rayIn
893     /* reorient if necessary */
894     if (rayIn -> rod < 0)
895        flipsurface(rayIn);
896 <   if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > FTINY * FTINY)
896 >   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > FTINY * FTINY) ))
897        pdot = raynormal(pnorm, rayIn);
898     else {
899        VCOPY(pnorm, rayIn -> ron);
# Line 1383 | Line 1377 | static int pattexPhotonScatter (OBJREC *mat, RAY *rayI
1377  
1378  
1379  
1380 + /*
1381 +   ==================================================================
1382 +   The following code is
1383 +   (c) Lucerne University of Applied Sciences and Arts,
1384 +   supported by the Swiss National Science Foundation (SNSF, #147053)
1385 +   ==================================================================
1386 + */  
1387 +
1388   static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1389   /* Generate new photon ray for BSDF modifier and recurse. */
1390   {
1391     int      hitFront;
1392     SDError  err;
1393 +   SDValue  bsdfVal;
1394     FVECT           upvec;
1395     MFUNC           *mf;
1396     BSDFDAT      nd;
1397     RAY      rayOut;
1398 <
1398 >   COLOR    bsdfRGB;
1399 >   int      transmitted;
1400 >   double   prDiff, ptDiff, prDiffSD, ptDiffSD, prSpecSD, ptSpecSD,
1401 >            albedo, xi;
1402 >   const double patAlb = bright(rayIn -> pcol);
1403 >  
1404     /* Following code adapted from m_bsdf() */
1405     /* Check arguments */
1406     if (mat -> oargs.nsargs < 6 || mat -> oargs.nfargs > 9 ||
# Line 1408 | Line 1416 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1416          nd.thick = evalue(mf -> ep [0]);
1417          if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
1418                  nd.thick = .0;
1411                
1412   if (nd.thick != .0 || (!hitFront && !backvis)) {
1413      /* Proxy geometry present, so use it instead and transfer ray */
1414      photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1415      tracePhoton(&rayOut);
1416      
1417      return 0;
1418   }
1419  
1420     /* Get BSDF data */
1421     nd.sd = loadBSDF(mat -> oargs.sarg [1]);
1422    
1423 <   /* Diffuse reflectance */
1423 >   /* Extra diffuse reflectance from material def */
1424     if (hitFront) {
1425        if (mat -> oargs.nfargs < 3)
1426           setcolor(nd.rdiff, .0, .0, .0);
# Line 1439 | Line 1439 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1439     else setcolor(nd.rdiff, mat -> oargs.farg [3], mat -> oargs.farg [4],
1440                   mat -> oargs.farg [5]);
1441  
1442 <        /* Diffuse transmittance */
1442 >        /* Extra diffuse transmittance from material def */
1443          if (mat -> oargs.nfargs < 9)
1444             setcolor(nd.tdiff, .0, .0, .0);
1445     else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7],
# Line 1506 | Line 1506 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1506        nd.pnorm [1] = -nd.pnorm [1];
1507        nd.pnorm [2] = -nd.pnorm [2];
1508     }
1509 <  
1510 <   /* Following code adapted from SDsampBSDF() */
1511 <   {
1512 <      SDSpectralDF   *rdf, *tdf;
1513 <      SDValue        bsdfVal;
1514 <      double         xi, rhoDiff = 0;
1515 <      float          coef [SDmaxCh];
1516 <      int            i, j, n, nr;
1517 <      SDComponent          *sdc;
1518 <      const SDCDst   **cdarr = NULL;
1519 <      
1520 <      /* Get diffuse albedo (?) */
1521 <      if (hitFront) {
1522 <         bsdfVal = nd.sd -> rLambFront;
1523 <         rdf = nd.sd -> rf;
1524 <         tdf = nd.sd -> tf ? nd.sd -> tf : nd.sd -> tb;
1525 <      }
1526 <      else {
1527 <         bsdfVal = nd.sd -> rLambBack;
1528 <         rdf = nd.sd -> rb;
1529 <         tdf = nd.sd -> tb ? nd.sd -> tb : nd.sd -> tf;
1530 <      }
1531 <      
1532 <      rhoDiff = bsdfVal.cieY;
1533 <      bsdfVal.cieY += nd.sd -> tLamb.cieY;
1534 <      
1535 <      /* Allocate non-diffuse sampling */
1536 <      i = nr = rdf ? rdf -> ncomp : 0;
1537 <      j = tdf ? tdf -> ncomp : 0;
1538 <      n = i + j;
1539 <      
1540 <      if (n > 0 && !(cdarr = (const SDCDst**)malloc(n * sizeof(SDCDst*))))
1541 <         objerror(mat, USER, transSDError(SDEmemory));
1509 >
1510 >   /* Get scatter probabilities (weighted by pattern except for spec refl)
1511 >    * prDiff, ptDiff:      extra diffuse component in material def
1512 >    * prDiffSD, ptDiffSD:  diffuse (constant) component in SDF
1513 >    * prSpecSD, ptSpecSD:  non-diffuse ("specular") component in SDF
1514 >    * albedo:              sum of above, inverse absorption probability */
1515 >   prDiff   = colorAvg(nd.rdiff);
1516 >   ptDiff   = colorAvg(nd.tdiff);
1517 >   prDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampR, nd.sd);
1518 >   ptDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampT, nd.sd);
1519 >   prSpecSD = SDdirectHemi(nd.vray, SDsampSp | SDsampR, nd.sd);
1520 >   ptSpecSD = patAlb * SDdirectHemi(nd.vray, SDsampSp | SDsampT, nd.sd);
1521 >   albedo   = prDiff + ptDiff + prDiffSD + ptDiffSD + prSpecSD + ptSpecSD;
1522 >
1523 >   /*    
1524 >   if (albedo > 1)
1525 >      objerror(mat, WARNING, "Invalid albedo");
1526 >   */
1527          
1528 <      while (j-- > 0) {
1529 <         /* Sum up non-diffuse transmittance */
1530 <         cdarr [i + j] = (*tdf -> comp [j].func -> getCDist)(nd.vray, &tdf -> comp [j]);
1531 <        
1532 <         if (!cdarr [i + j])
1548 <            cdarr [i + j] = &SDemptyCD;
1549 <         else bsdfVal.cieY += cdarr [i + j] -> cTotal;
1550 <      }
1528 >   /* Insert direct and indirect photon hits if diffuse component */
1529 >   if (prDiff + ptDiff + prDiffSD + ptDiffSD > FTINY)
1530 >      addPhotons(rayIn);        
1531 >
1532 >   xi = pmapRandom(rouletteState);
1533        
1534 <      while (i-- > 0) {
1535 <         /* Sum up non-diffuse reflectance */
1536 <         cdarr [i] = (*rdf -> comp [i].func -> getCDist)(nd.vray, &rdf -> comp [i]);
1534 >   if (xi > albedo)
1535 >      /* Absorbtion */
1536 >      return 0;
1537 >  
1538 >   transmitted = 0;
1539 >
1540 >   if ((xi -= prDiff) <= 0) {
1541 >      /* Diffuse reflection (extra component in material def) */
1542 >      photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff);
1543 >      diffPhotonScatter(nd.pnorm, &rayOut);
1544 >   }
1545 >  
1546 >   else if ((xi -= ptDiff) <= 0) {
1547 >      /* Diffuse transmission (extra component in material def) */
1548 >      flipsurface(rayIn);
1549 >      nd.thick = -nd.thick;
1550 >      photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff);
1551 >      diffPhotonScatter(nd.pnorm, &rayOut);
1552 >      transmitted = 1;
1553 >   }
1554 >  
1555 >   else {   /* Sample SDF */
1556 >      if ((xi -= prDiffSD) <= 0) {
1557 >         /* Diffuse SDF reflection (constant component) */
1558 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1559 >                               SDsampDf | SDsampR, nd.sd)))
1560 >            objerror(mat, USER, transSDError(err));
1561          
1562 <         if (!cdarr [i])
1563 <            cdarr [i] = &SDemptyCD;
1564 <         else bsdfVal.cieY += cdarr [i] -> cTotal;
1562 >         /* Apply pattern to spectral component */
1563 >         ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1564 >         multcolor(bsdfRGB, rayIn -> pcol);
1565 >         photonRay(rayIn, &rayOut, PMAP_DIFFREFL, bsdfRGB);
1566        }
1560      
1561      if (bsdfVal.cieY <= FTINY) {
1562         /* Don't bother sampling, just absorb photon */
1563         if (cdarr)
1564            free(cdarr);
1565         return 0;
1566      }      
1567      
1568      /* Insert direct and indirect photon hits if diffuse component */
1569      if (rhoDiff > FTINY || nd.sd -> tLamb.cieY > FTINY)
1570         addPhotons(rayIn);    
1571        
1572      xi = pmapRandom(rouletteState);
1573      
1574      if ((xi -= rhoDiff) <= 0) {
1575         /* Diffuse reflection */
1576         photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff);
1577         diffPhotonScatter(nd.pnorm, &rayOut);
1578      }
1579      else if ((xi -= nd.sd -> tLamb.cieY) <= 0) {
1580         /* Diffuse transmission */
1581         flipsurface(rayIn);
1582         photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff);
1583         bsdfVal.spec = nd.sd -> tLamb.spec;
1584         diffPhotonScatter(nd.pnorm, &rayOut);
1585      }
1586      else {
1587         int rayOutType;
1588         COLOR bsdfRGB;
1589            
1590         /* Non-diffuse CDF inversion (?) */
1591         for (i = 0; i < n && (xi -= cdarr [i] -> cTotal) > 0; i++);
1592        
1593         if (i >= n) {
1594            /* Absorbed -- photon went Deer Hunter */
1595            if (cdarr)
1596               free(cdarr);
1597            return 0;
1598         }
1567  
1568 <         if (i < nr) {
1569 <            /* Non-diffuse reflection */
1570 <            sdc = &rdf -> comp [i];
1571 <            rayOutType = PMAP_SPECREFL;
1572 <         }
1605 <         else {
1606 <            /* Non-diffuse transmission */
1607 <            sdc = &tdf -> comp [i - nr];
1608 <            rayOutType = PMAP_SPECTRANS;
1609 <         }
1568 >      else if ((xi -= ptDiffSD) <= 0) {
1569 >         /* Diffuse SDF transmission (constant component) */
1570 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1571 >                               SDsampDf | SDsampT, nd.sd)))
1572 >            objerror(mat, USER, transSDError(err));
1573          
1574 <         /* Generate non-diff sample dir */
1612 <         VCOPY(rayOut.rdir, nd.vray);
1613 <         err = (*sdc -> func -> sampCDist)
1614 <               (rayOut.rdir, pmapRandom(scatterState), cdarr [i]);              
1615 <         if (err)
1616 <            objerror(mat, USER, transSDError(SDEinternal));
1617 <
1618 <         /* Get colour */
1619 <         j = (*sdc -> func -> getBSDFs)(coef, rayOut.rdir, nd.vray, sdc);
1620 <        
1621 <         if (j <= 0) {
1622 <            sprintf(SDerrorDetail, "BSDF \"%s\" sampling value error",
1623 <                    nd.sd -> name);
1624 <            objerror(mat, USER, transSDError(SDEinternal));
1625 <         }
1626 <        
1627 <         bsdfVal.spec = sdc -> cspec [0];
1628 <         rhoDiff = coef [0];
1629 <        
1630 <         while (--j) {
1631 <            c_cmix(&bsdfVal.spec, rhoDiff, &bsdfVal.spec, coef [j],
1632 <                   &sdc -> cspec [j]);
1633 <            rhoDiff += coef [j];
1634 <         }
1635 <        
1636 <         /* ? */
1637 <         c_ccvt(&bsdfVal.spec, C_CSXY + C_CSSPEC);
1574 >         /* Apply pattern to spectral component */
1575           ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1576 <        
1577 <         /* Xform outgoing dir to world coords */
1578 <         if ((err = SDmapDir(rayOut.rdir, nd.fromloc, rayOut.rdir))) {
1576 >         multcolor(bsdfRGB, rayIn -> pcol);
1577 >         addcolor(bsdfRGB, nd.tdiff);      
1578 >         flipsurface(rayIn);  /* Necessary? */
1579 >         nd.thick = -nd.thick;
1580 >         photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, bsdfRGB);
1581 >         transmitted = 1;
1582 >      }
1583 >
1584 >      else if ((xi -= prSpecSD) <= 0) {
1585 >         /* Non-diffuse ("specular") SDF reflection */
1586 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1587 >                               SDsampSp | SDsampR, nd.sd)))
1588              objerror(mat, USER, transSDError(err));
1643            return 0;
1644         }
1589          
1590 <         photonRay(rayIn, &rayOut, rayOutType, bsdfRGB);
1590 >         ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1591 >         photonRay(rayIn, &rayOut, PMAP_SPECREFL, bsdfRGB);
1592        }
1593        
1594 <      if (cdarr)
1595 <         free(cdarr);
1594 >      else {
1595 >         /* Non-diffuse ("specular") SDF transmission */
1596 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1597 >                               SDsampSp | SDsampT, nd.sd)))
1598 >            objerror(mat, USER, transSDError(err));
1599 >
1600 >         /* Apply pattern to spectral component */
1601 >         ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1602 >         multcolor(bsdfRGB, rayIn -> pcol);
1603 >         flipsurface(rayIn);  /* Necessary? */
1604 >         nd.thick = -nd.thick;
1605 >         photonRay(rayIn, &rayOut, PMAP_SPECTRANS, bsdfRGB);
1606 >         transmitted = 1;
1607 >      }      
1608 >      
1609 >      /* Xform outgoing dir to world coords */
1610 >      if ((err = SDmapDir(rayOut.rdir, nd.fromloc, nd.vray))) {
1611 >         objerror(mat, USER, transSDError(err));
1612 >         return 0;
1613 >      }
1614     }
1615 <                        
1616 <   /* Clean up BSDF */
1615 >      
1616 >   /* Clean up */
1617     SDfreeCache(nd.sd);
1618 +
1619 +   /* Need to offset ray origin to get past detail geometry? */
1620 +   if (transmitted && nd.thick != 0)
1621 +      VSUM(rayOut.rorg, rayOut.rorg, rayIn -> ron, -nd.thick);
1622  
1623     tracePhoton(&rayOut);
1624     return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines