Date post: | 07-Apr-2018 |
Category: |
Documents |
Upload: | c0de517eblogspotcom |
View: | 218 times |
Download: | 0 times |
8/4/2019 Mathematica - Fitting Penner's Skin Scattering
http://slidepdf.com/reader/full/mathematica-fitting-penners-skin-scattering 1/8
Coefficients and colors from d 'Eon and Luebke skin scattering work
ScatterCoeffs 0.0064, 0.0484, 0.187, 0.567, 1.99, 7.41 Sqrt2
ScatterColors 0.233, 0.455, 0.649, 0.1, 0.336, 0.344,
0.118, 0.198, 0, 0.113, 0.007, 0.007, 0.358, 0.004, 0, 0.078, 0, 0
GaussianFnvar_, x_ : 1 Sqrt2 Pi var Expx x 2 var
Skin scattering profile approximated a sum of multiple
gaussians. We don't want to work with threedimensional RGB
vectors so here one of the inputs selects which channel we want
ScatterFnx_, rgb_ :
ApplyPlus, TableGaussianFnvar, x , var, ScatterCoeffs ScatterColorsrgb
Out[1]= 0.00905097, 0.0684479, 0.264458, 0.801859, 2.81428, 10.4793Out[2]= 0.233, 0.455, 0.649, 0.1, 0.336, 0.344, 0.118, 0.198, 0,
0.113, 0.007, 0.007, 0.358, 0.004, 0, 0.078, 0, 0
In[5]:= This is the function defined with integrals, as in the paper
DiffuseLightΘ _, r_, rgb_ : NIntegrate
ClipCosΘ x , 0, 1 ScatterFn Abs2 r Sinx 2, rgb, x , Pi, Pi
DiffuseWeightr_, rgb_ : NIntegrate
ScatterFn Abs2 r Sinx 2, rgb, x , Pi, Pi
DiffuseIntegralΘ _, r_, rgb_ : DiffuseLightΘ, r, rgb DiffuseWeightr, rgb
This is a bit faster to evaluate,
it's a translation of the code used to generate the lookup table
DiffuseWeight2r_, rgb_ :
NSum ScatterFn Abs2 r Sinx 2, rgb, x , Pi, Pi, 2 Pi 20
DiffuseLight2Θ _, r_, rgb_ : NSum ClipCosΘ x , 0, 1
ScatterFn Abs2 r Sinx 2, rgb, x , Pi, Pi, 2 Pi 20
DiffuseIntegral2Θ _, r_, rgb_ : DiffuseLight2Θ, r, rgb DiffuseWeight2r, rgb
In[11]:= Let's theck that they are similar ...
DiffuseIntegral0.1, 8, 1
DiffuseIntegral20.1, 8, 1
DiffuseIntegral0.1, 15, 1
DiffuseIntegral20.1, 15, 1
Out[11]= 0.979266
Out[12]= 0.990889
Out[13]= 0.990641
Out[14]= 0.994601
In[16]:= Let's generate a table of data to be used for the numerical fitting...
channel 1; 1red 2green 3 blue
dataThetaRVal FlattenTableΘ, r , DiffuseIntegralΘ, r , channel,
Θ, 0, Pi, 2 Pi 20, r, 0.25, 6, 1 20, 1;
8/4/2019 Mathematica - Fitting Penner's Skin Scattering
http://slidepdf.com/reader/full/mathematica-fitting-penners-skin-scattering 2/8
an extended range that we will use to check how the fit extrapolates
dataIntervalExt FlattenTableΘ, r , Θ, 0, Pi, 2 Pi 20, r, 0, 12, 1 20, 1 ;
LineInterpa_,b_,x_,x0_,x1_ :
bClipxx0,0,1aClip1xx0x1x0,0,1
dataGraph ListPlot3DdataThetaRVal, InterpolationOrder 3
cosGraph ListPlot3D
x Firstx , Lastx , ClipCosFirstx , 0, 1 dataIntervalExt;
Plotting against the simple lambert cos lighting model,
of course that's not dependent on r ...
Show dataGraph, cosGraph
Out[18]=
Out[20]=
2 EricPenner_SkinScattering1.nb
8/4/2019 Mathematica - Fitting Penner's Skin Scattering
http://slidepdf.com/reader/full/mathematica-fitting-penners-skin-scattering 3/8
In[24]:= Ok, let's have a peek at our function to understand its shape... We'll plot a
single slice, then a graph with three slices and the Lambert's clamped cos
plotIn2d PlotDiffuseIntegralx , 5, 1, x, 0, Pi
PlotDiffuseIntegralx , 1 , 1, DiffuseIntegralx, 3, 1,
DiffuseIntegralx , 6 , 1, Max Cosx , 0 , x, 0, Pi
Out[24]=
0.5 1.0 1.5 2.0 2.5 3.0
0.2
0.4
0.6
0.8
Out[25]=
0.5 1.0 1.5 2.0 2.5 3.0
0.2
0.4
0.6
0.8
1.0
EricPenner_SkinScattering1.nb 3
8/4/2019 Mathematica - Fitting Penner's Skin Scattering
http://slidepdf.com/reader/full/mathematica-fitting-penners-skin-scattering 4/8
In[26]:= We have to find a function of cosΘ
which could look similar to the DiffuseIntegral
Show Plot Max Cosx 0.5 0.5, 0 ^ 3, x, 0, Pi, plotIn2d
Out[26]=
0.5 1.0 1.5 2.0 2.5 3.0
0.2
0.4
0.6
0.8
1.0
4 EricPenner_SkinScattering1.nb
8/4/2019 Mathematica - Fitting Penner's Skin Scattering
http://slidepdf.com/reader/full/mathematica-fitting-penners-skin-scattering 5/8
In[27]:=
Let's find the fit now, first we have to define a parametric function,
the fitting process will optimize the parameters to minimize the error
piecewise linear models...
fitFunctionΘ _,r_ : Max CosΘ Mina0ra1,1 Max a2ra3,0,0
fitFunctionΘ _,r_ :
Max Max CosΘ Mina0ra1,1 Max a2ra3,0,0,
Max CosΘ Mina4ra5,1 Max a6ra7,0,0
nonlinear models...
fitFunctionΘ _,r_ : CosΘa0ra1 a2ra3^ 3
fitFunctionΘ _, r_ :
CosΘ a0 r a1 a2 r a3^ 3 Clipa4 r a5, 0, 1
Max CosΘ, 0 1 Clipa4 r a5, 0, 1
fitting NonlinearModelFitdataThetaRVal, fitFunctionΘ, r ,
a0, a1, a2, a3, a4, a5, a6, a7, Θ, r , Method NMinimize We need to use NMinimize for nonsmooth models
fitParams fitting"BestFitParameters"
Plus Abs fitting"FitResiduals" Print the sum of residuals
We plot the fit function over
dataIntervalExt to check that it extrapolates well
dataFit x Firstx , Lastx , fitFunctionFirstx , Lastx . fitParams
dataIntervalExt;
fitGraph ListPlot3DdataFit, InterpolationOrder 3,
ColorFunction "SouthwestColors", Mesh None;
Show fitGraph, cosGraph
Show fitGraph, dataGraph
Out[28]= FittedModel Clip 0.817767 0.11103 r, 0, 1 1131111
Out[29]= a0 0.0605281, a1 0.0903942, a2 0.0210583,
a3 0.689657, a4 0.11103, a5 0.817767, a6 7.15776, a7 5.89292Out[30]= 12.4611
EricPenner_SkinScattering1.nb 5
8/4/2019 Mathematica - Fitting Penner's Skin Scattering
http://slidepdf.com/reader/full/mathematica-fitting-penners-skin-scattering 6/8
Out[33]=
Out[34]=
6 EricPenner_SkinScattering1.nb
8/4/2019 Mathematica - Fitting Penner's Skin Scattering
http://slidepdf.com/reader/full/mathematica-fitting-penners-skin-scattering 7/8
lightVector Normalize1, 2, 0;
Lambert
SphericalPlot3D1, Θ, 0, Pi, Φ, 0, Pi,
ColorFunctionScaling False, False, False, False, False, False,
ColorFunction x, y, z, Θ, Φ, r Max x, y, z.lightVector, 0
Our function
radius 3;
SphericalPlot3D1, Θ, 0, Pi, Φ, 0, Pi,
ColorFunctionScaling False, False, False, False, False, False,
ColorFunction x, y, z, Θ, Φ, r
fitFunction ArcCosx, y, z.lightVector, radius . fitParams
Out[45]=
EricPenner_SkinScattering1.nb 7
8/4/2019 Mathematica - Fitting Penner's Skin Scattering
http://slidepdf.com/reader/full/mathematica-fitting-penners-skin-scattering 8/8
Out[47]=
todo...
Use NMinimize to find a fit with a custom error metric, i.e. perceptual,
weighting more the darker areas or we can fit the log of the
functions or we could use the Weights option of NonlinearModelFit
Constraint fit i.e. zero at r1,
Θ2Pi or better, equal to CosΘ for r going to
Find artisttunable parameters and use Manipulate to validate them...
8 EricPenner_SkinScattering1.nb