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.11 by rschregle, Tue Oct 20 15:49:44 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    
10   $Id$
14   */
15  
16  
# Line 113 | Line 116 | void photonRay (const RAY *rayIn, RAY *rayOut,
116        VCOPY(rayOut -> rdir, rayIn -> rdir);
117     }
118     else if (fluxAtten) {
119 <      /* Attenuate and normalised flux for scattered rays */
119 >      /* Attenuate and normalise flux for scattered rays */
120        multcolor(rayOut -> rcol, fluxAtten);
121        colorNorm(rayOut -> rcol);
122     }
# Line 198 | Line 201 | static int isoSpecPhotonScatter (NORMDAT *nd, RAY *ray
201     int      niter, i = 0;
202    
203     /* Set up sample coordinates */  
204 <   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 <  
204 >   getperpendicular(u, nd -> pnorm, 1);  
205     fcross(v, nd -> pnorm, u);
206    
207     if (nd -> specfl & SP_REFL) {
# Line 267 | Line 265 | static void diffPhotonScatter (FVECT normal, RAY* rayO
265     int         i = 0;
266  
267     /* Set up sample coordinates */
268 <   do {
271 <      v [0] = v [1] = v [2] = 0;
272 <      v [i++] = 1;
273 <      fcross(u, v, normal);
274 <   } while (normalize(u) < FTINY);
275 <  
268 >   getperpendicular(u, normal, 1);
269     fcross(v, normal, u);
270    
271     /* Convert theta & phi to cartesian */
# Line 323 | Line 316 | static int normalPhotonScatter (OBJREC *mat, RAY *rayI
316        nd.specfl |= SP_FLAT;  
317        
318     /* Perturb normal */
319 <   if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > sqr(FTINY))
319 >   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > sqr(FTINY)) ))
320        nd.pdot = raynormal(nd.pnorm, rayIn);
321     else {
322        VCOPY(nd.pnorm, rayIn -> ron);
# Line 752 | Line 745 | static int dielectricPhotonScatter (OBJREC *mat, RAY *
745     /* get modifiers */
746     raytexture(rayIn, mat -> omod);                      
747    
748 <   if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > FTINY * FTINY)
748 >   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > FTINY * FTINY)))
749        /* Perturb normal */
750        cos1 = raynormal(dnorm, rayIn);
751     else {
# Line 899 | Line 892 | static int glassPhotonScatter (OBJREC *mat, RAY *rayIn
892     /* reorient if necessary */
893     if (rayIn -> rod < 0)
894        flipsurface(rayIn);
895 <   if ((hastexture = DOT(rayIn -> pert, rayIn -> pert)) > FTINY * FTINY)
895 >   if ((hastexture = (DOT(rayIn -> pert, rayIn -> pert) > FTINY * FTINY) ))
896        pdot = raynormal(pnorm, rayIn);
897     else {
898        VCOPY(pnorm, rayIn -> ron);
# Line 985 | Line 978 | static int aliasPhotonScatter (OBJREC *mat, RAY *rayIn
978    
979     /* Straight replacement? */
980     if (!mat -> oargs.nsargs) {
981 <      mat = objptr(mat -> omod);
982 <      photonScatter [mat -> otype] (mat, rayIn);
981 >      /* Skip void modifier! */
982 >      if (mat -> omod != OVOID) {
983 >         mat = objptr(mat -> omod);
984 >         photonScatter [mat -> otype] (mat, rayIn);
985 >      }
986        
987        return 0;
988     }
# Line 1257 | Line 1253 | static int mx_dataPhotonScatter (OBJREC *mat, RAY *ray
1253     if (errno)
1254        objerror(mat, WARNING, "compute error");
1255     else {
1256 <      mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]);
1257 <      photonScatter [mat -> otype] (mat, rayIn);
1256 >      OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1];
1257 >      
1258 >      if (mxMod != OVOID) {
1259 >         mat = objptr(mxMod);
1260 >         photonScatter [mat -> otype] (mat, rayIn);
1261 >      }
1262 >      else {
1263 >         /* Transfer ray if no modifier */
1264 >         RAY rayOut;
1265 >        
1266 >         photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1267 >         tracePhoton(&rayOut);      
1268 >      }            
1269     }
1270    
1271     return 0;
# Line 1310 | Line 1317 | static int mx_pdataPhotonScatter (OBJREC *mat, RAY *ra
1317     if (errno)
1318        objerror(mat, WARNING, "compute error");
1319     else {
1320 <      mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]);
1321 <      photonScatter [mat -> otype] (mat, rayIn);
1320 >      OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1];
1321 >      
1322 >      if (mxMod != OVOID) {
1323 >         mat = objptr(mxMod);
1324 >         photonScatter [mat -> otype] (mat, rayIn);
1325 >      }
1326 >      else {
1327 >         /* Transfer ray if no modifier */
1328 >         RAY rayOut;
1329 >        
1330 >         photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1331 >         tracePhoton(&rayOut);      
1332 >      }      
1333     }  
1334    
1335     return 0;
# Line 1350 | Line 1368 | static int mx_funcPhotonScatter (OBJREC *mat, RAY *ray
1368     if (errno)
1369        objerror(mat, WARNING, "compute error");
1370     else {        
1371 <      mat = objptr(mod [pmapRandom(rouletteState) < coef ? 0 : 1]);
1372 <      photonScatter [mat -> otype] (mat, rayIn);
1371 >      OBJECT mxMod = mod [pmapRandom(rouletteState) < coef ? 0 : 1];
1372 >      
1373 >      if (mxMod != OVOID) {
1374 >         mat = objptr(mxMod);
1375 >         photonScatter [mat -> otype] (mat, rayIn);
1376 >      }
1377 >      else {
1378 >         /* Transfer ray if no modifier */
1379 >         RAY rayOut;
1380 >        
1381 >         photonRay(rayIn, &rayOut, PMAP_XFER, NULL);
1382 >         tracePhoton(&rayOut);      
1383 >      }
1384     }
1385    
1386     return 0;
# Line 1383 | Line 1412 | static int pattexPhotonScatter (OBJREC *mat, RAY *rayI
1412  
1413  
1414  
1415 + /*
1416 +   ==================================================================
1417 +   The following code is
1418 +   (c) Lucerne University of Applied Sciences and Arts,
1419 +   supported by the Swiss National Science Foundation (SNSF, #147053)
1420 +   ==================================================================
1421 + */  
1422 +
1423   static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1424   /* Generate new photon ray for BSDF modifier and recurse. */
1425   {
1426     int      hitFront;
1427     SDError  err;
1428 +   SDValue  bsdfVal;
1429     FVECT           upvec;
1430     MFUNC           *mf;
1431     BSDFDAT      nd;
1432     RAY      rayOut;
1433 <
1433 >   COLOR    bsdfRGB;
1434 >   int      transmitted;
1435 >   double   prDiff, ptDiff, prDiffSD, ptDiffSD, prSpecSD, ptSpecSD,
1436 >            albedo, xi;
1437 >   const double patAlb = bright(rayIn -> pcol);
1438 >  
1439     /* Following code adapted from m_bsdf() */
1440     /* Check arguments */
1441     if (mat -> oargs.nsargs < 6 || mat -> oargs.nfargs > 9 ||
# Line 1408 | Line 1451 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1451          nd.thick = evalue(mf -> ep [0]);
1452          if ((-FTINY <= nd.thick) & (nd.thick <= FTINY))
1453                  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   }
1454  
1455     /* Get BSDF data */
1456     nd.sd = loadBSDF(mat -> oargs.sarg [1]);
1457    
1458 <   /* Diffuse reflectance */
1458 >   /* Extra diffuse reflectance from material def */
1459     if (hitFront) {
1460        if (mat -> oargs.nfargs < 3)
1461           setcolor(nd.rdiff, .0, .0, .0);
# Line 1439 | Line 1474 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1474     else setcolor(nd.rdiff, mat -> oargs.farg [3], mat -> oargs.farg [4],
1475                   mat -> oargs.farg [5]);
1476  
1477 <        /* Diffuse transmittance */
1477 >        /* Extra diffuse transmittance from material def */
1478          if (mat -> oargs.nfargs < 9)
1479             setcolor(nd.tdiff, .0, .0, .0);
1480     else setcolor(nd.tdiff, mat -> oargs.farg [6], mat -> oargs.farg [7],
# Line 1506 | Line 1541 | static int bsdfPhotonScatter (OBJREC *mat, RAY *rayIn)
1541        nd.pnorm [1] = -nd.pnorm [1];
1542        nd.pnorm [2] = -nd.pnorm [2];
1543     }
1544 <  
1545 <   /* Following code adapted from SDsampBSDF() */
1546 <   {
1547 <      SDSpectralDF   *rdf, *tdf;
1548 <      SDValue        bsdfVal;
1549 <      double         xi, rhoDiff = 0;
1550 <      float          coef [SDmaxCh];
1551 <      int            i, j, n, nr;
1552 <      SDComponent          *sdc;
1553 <      const SDCDst   **cdarr = NULL;
1554 <      
1555 <      /* Get diffuse albedo (?) */
1556 <      if (hitFront) {
1557 <         bsdfVal = nd.sd -> rLambFront;
1558 <         rdf = nd.sd -> rf;
1559 <         tdf = nd.sd -> tf ? nd.sd -> tf : nd.sd -> tb;
1560 <      }
1561 <      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));
1544 >
1545 >   /* Get scatter probabilities (weighted by pattern except for spec refl)
1546 >    * prDiff, ptDiff:      extra diffuse component in material def
1547 >    * prDiffSD, ptDiffSD:  diffuse (constant) component in SDF
1548 >    * prSpecSD, ptSpecSD:  non-diffuse ("specular") component in SDF
1549 >    * albedo:              sum of above, inverse absorption probability */
1550 >   prDiff   = colorAvg(nd.rdiff);
1551 >   ptDiff   = colorAvg(nd.tdiff);
1552 >   prDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampR, nd.sd);
1553 >   ptDiffSD = patAlb * SDdirectHemi(nd.vray, SDsampDf | SDsampT, nd.sd);
1554 >   prSpecSD = SDdirectHemi(nd.vray, SDsampSp | SDsampR, nd.sd);
1555 >   ptSpecSD = patAlb * SDdirectHemi(nd.vray, SDsampSp | SDsampT, nd.sd);
1556 >   albedo   = prDiff + ptDiff + prDiffSD + ptDiffSD + prSpecSD + ptSpecSD;
1557 >
1558 >   /*    
1559 >   if (albedo > 1)
1560 >      objerror(mat, WARNING, "Invalid albedo");
1561 >   */
1562          
1563 <      while (j-- > 0) {
1564 <         /* Sum up non-diffuse transmittance */
1565 <         cdarr [i + j] = (*tdf -> comp [j].func -> getCDist)(nd.vray, &tdf -> comp [j]);
1566 <        
1567 <         if (!cdarr [i + j])
1548 <            cdarr [i + j] = &SDemptyCD;
1549 <         else bsdfVal.cieY += cdarr [i + j] -> cTotal;
1550 <      }
1563 >   /* Insert direct and indirect photon hits if diffuse component */
1564 >   if (prDiff + ptDiff + prDiffSD + ptDiffSD > FTINY)
1565 >      addPhotons(rayIn);        
1566 >
1567 >   xi = pmapRandom(rouletteState);
1568        
1569 <      while (i-- > 0) {
1570 <         /* Sum up non-diffuse reflectance */
1571 <         cdarr [i] = (*rdf -> comp [i].func -> getCDist)(nd.vray, &rdf -> comp [i]);
1569 >   if (xi > albedo)
1570 >      /* Absorbtion */
1571 >      return 0;
1572 >  
1573 >   transmitted = 0;
1574 >
1575 >   if ((xi -= prDiff) <= 0) {
1576 >      /* Diffuse reflection (extra component in material def) */
1577 >      photonRay(rayIn, &rayOut, PMAP_DIFFREFL, nd.rdiff);
1578 >      diffPhotonScatter(nd.pnorm, &rayOut);
1579 >   }
1580 >  
1581 >   else if ((xi -= ptDiff) <= 0) {
1582 >      /* Diffuse transmission (extra component in material def) */
1583 >      flipsurface(rayIn);
1584 >      nd.thick = -nd.thick;
1585 >      photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, nd.tdiff);
1586 >      diffPhotonScatter(nd.pnorm, &rayOut);
1587 >      transmitted = 1;
1588 >   }
1589 >  
1590 >   else {   /* Sample SDF */
1591 >      if ((xi -= prDiffSD) <= 0) {
1592 >         /* Diffuse SDF reflection (constant component) */
1593 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1594 >                               SDsampDf | SDsampR, nd.sd)))
1595 >            objerror(mat, USER, transSDError(err));
1596          
1597 <         if (!cdarr [i])
1598 <            cdarr [i] = &SDemptyCD;
1599 <         else bsdfVal.cieY += cdarr [i] -> cTotal;
1597 >         /* Apply pattern to spectral component */
1598 >         ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1599 >         multcolor(bsdfRGB, rayIn -> pcol);
1600 >         photonRay(rayIn, &rayOut, PMAP_DIFFREFL, bsdfRGB);
1601        }
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         }
1602  
1603 <         if (i < nr) {
1604 <            /* Non-diffuse reflection */
1605 <            sdc = &rdf -> comp [i];
1606 <            rayOutType = PMAP_SPECREFL;
1607 <         }
1605 <         else {
1606 <            /* Non-diffuse transmission */
1607 <            sdc = &tdf -> comp [i - nr];
1608 <            rayOutType = PMAP_SPECTRANS;
1609 <         }
1603 >      else if ((xi -= ptDiffSD) <= 0) {
1604 >         /* Diffuse SDF transmission (constant component) */
1605 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1606 >                               SDsampDf | SDsampT, nd.sd)))
1607 >            objerror(mat, USER, transSDError(err));
1608          
1609 <         /* 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);
1609 >         /* Apply pattern to spectral component */
1610           ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1611 <        
1612 <         /* Xform outgoing dir to world coords */
1613 <         if ((err = SDmapDir(rayOut.rdir, nd.fromloc, rayOut.rdir))) {
1611 >         multcolor(bsdfRGB, rayIn -> pcol);
1612 >         addcolor(bsdfRGB, nd.tdiff);      
1613 >         flipsurface(rayIn);  /* Necessary? */
1614 >         nd.thick = -nd.thick;
1615 >         photonRay(rayIn, &rayOut, PMAP_DIFFTRANS, bsdfRGB);
1616 >         transmitted = 1;
1617 >      }
1618 >
1619 >      else if ((xi -= prSpecSD) <= 0) {
1620 >         /* Non-diffuse ("specular") SDF reflection */
1621 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1622 >                               SDsampSp | SDsampR, nd.sd)))
1623              objerror(mat, USER, transSDError(err));
1643            return 0;
1644         }
1624          
1625 <         photonRay(rayIn, &rayOut, rayOutType, bsdfRGB);
1625 >         ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1626 >         photonRay(rayIn, &rayOut, PMAP_SPECREFL, bsdfRGB);
1627        }
1628        
1629 <      if (cdarr)
1630 <         free(cdarr);
1629 >      else {
1630 >         /* Non-diffuse ("specular") SDF transmission */
1631 >         if ((err = SDsampBSDF(&bsdfVal, nd.vray, pmapRandom(scatterState),
1632 >                               SDsampSp | SDsampT, nd.sd)))
1633 >            objerror(mat, USER, transSDError(err));
1634 >
1635 >         /* Apply pattern to spectral component */
1636 >         ccy2rgb(&bsdfVal.spec, bsdfVal.cieY, bsdfRGB);
1637 >         multcolor(bsdfRGB, rayIn -> pcol);
1638 >         flipsurface(rayIn);  /* Necessary? */
1639 >         nd.thick = -nd.thick;
1640 >         photonRay(rayIn, &rayOut, PMAP_SPECTRANS, bsdfRGB);
1641 >         transmitted = 1;
1642 >      }      
1643 >      
1644 >      /* Xform outgoing dir to world coords */
1645 >      if ((err = SDmapDir(rayOut.rdir, nd.fromloc, nd.vray))) {
1646 >         objerror(mat, USER, transSDError(err));
1647 >         return 0;
1648 >      }
1649     }
1650 <                        
1651 <   /* Clean up BSDF */
1650 >      
1651 >   /* Clean up */
1652     SDfreeCache(nd.sd);
1653 +
1654 +   /* Need to offset ray origin to get past detail geometry? */
1655 +   if (transmitted && nd.thick != 0)
1656 +      VSUM(rayOut.rorg, rayOut.rorg, rayIn -> ron, -nd.thick);
1657  
1658     tracePhoton(&rayOut);
1659     return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines