Changeset 19603


Ignore:
Timestamp:
Sep 2, 2019, 9:59:18 PM (2 weeks ago)
Author:
tbretz
Message:
The calculation for the ref index expected um not nm, the refraction produced invalid results in case R<0 (what does it mean?)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mars/msimreflector/MFresnelLens.cc

    r19600 r19603  
    168168}
    169169
    170 double CalcRefractiveIndex(const double &lambda)
     170double CalcRefractiveIndex(double lambda)
    171171{
    172172    // https://refractiveindex.info/?shelf=organic&book=poly(methyl_methacrylate)&page=Szczurowski
     
    176176    const double l2 = lambda*lambda;
    177177
    178     const double c0 = 0.99654/(1-0.00787/l2);
    179     const double c1 = 0.18964/(1-0.02191/l2);
    180     const double c2 = 0.00411/(1-3.85727/l2);
     178    const double c0 = 0.99654/(1-0.00787e6/l2);
     179    const double c1 = 0.18964/(1-0.02191e6/l2);
     180    const double c2 = 0.00411/(1-3.85727e6/l2);
    181181
    182182    return sqrt(1+c0+c1+c2);
    183183}
    184184
    185 void ApplyRefraction(MQuaternion &u, const TVector3 &n, const double &n1, const double &n2)
    186 {
    187     // u: incoming direction
     185bool ApplyRefraction(MQuaternion &u, const TVector3 &n, const double &n1, const double &n2)
     186{
     187    // From: https://stackoverflow.com/questions/29758545/how-to-find-refraction-vector-from-incoming-vector-and-surface-normal
     188    // Note that as a default u is supposed to be normalized such that z=1!
     189
     190    // u: incoming direction (normalized!)
    188191    // n: normal vector of surface
    189192    // n2: refractive index of new(?) medium
    190193    // n1: refractive index of old(?) medium
    191194
     195    // V_refraction = r*V_incedence + (rc - sqrt(1- r^2 (1-c^2)))n
     196    // r = n1/n2
     197    // c = -n dot V_incedence.
     198
     199    // The vector should be normalized already
     200    // u.NormalizeVector();
     201
    192202    const double r = n2/n1;
    193203    const double c = n*u.fVectorPart;
     
    195205    const double rc = r*c;
    196206
    197     const double R = 1 - r*r + rc*rc; // = 1 - r*r *(1-c*c);
    198 
    199     const double v = rc + sqrt(R<0 ? 0 : R);
    200 
    201     u = r*u - n*v;
     207    const double R = 1 - r*r + rc*rc; // = 1 - (r*r*(1-c*c));
     208
     209    // What is this? Total reflection?
     210    if (R<0)
     211        return false;
     212
     213    const double v = rc + sqrt(R);
     214
     215    u.fVectorPart = r*u.fVectorPart - v*n;
     216
     217    return true;
    202218}
    203219
     
    274290    // Apply refraction at lens entrance (change directional vector)
    275291    // FIXME: Surace roughness
    276     ApplyRefraction(u, norm, 1, n);
     292    if (!ApplyRefraction(u, norm, 1, n))
     293        return -1;
    277294
    278295    // Propagate ray until bottom of lens
     
    285302    // Normal surface (bottom of lens)
    286303    // FIXME: Surace roughness
    287     ApplyRefraction(u, TVector3(0, 0, 1), n, 1);
     304    if (!ApplyRefraction(u, TVector3(0, 0, 1), n, 1))
     305        return -1;
    288306
    289307    // To make this consistent with a mirror system,
Note: See TracChangeset for help on using the changeset viewer.