/*  This include file creates an  MM  (
minus/minus i.e concave/convex) aspheric lens element  lens represented by spheres or aspheres.  The aspheres are isosurfaces.

The lenses are bounded by plane(s) and/or a cylinder whose dimensions are calculated from the spherical surfaces.  In the cases where the aspherics are very large and there would be consequent errors in the profile then some intelligent use of the parameter 'Cutplane' will be required.

*/ 
                  
#declare ThisOrigin = NextOrigin;    #declare NextOrigin =  ThisOrigin+Thk+Nex;
//#debug concat("          Vertex of surface 1 of lens",str(LnzNum,4,0),str(ThisOrigin,9,3))

#if(AspR1=0 & AspR2 = 0)
     #declare Sag1    = -(R1 - sqrt(R1*R1 - D1*D1));   #declare Sag2 = -(R2 -sqrt(R2*R2 -D2*D2)); 
     #declare LensMM  = intersection { sphere { 0,R2 translate -z*R2 +z*Thk }  
                                                    sphere { 0,R1 translate -z*R1 inverse }
                                                    cylinder { z*Sag1,z*Thk,D2 texture { LensBlnk }  }
                                                    material { LensOpen }
                                                 }
#end

#if ( AspR1 = yes)  
       #declare SagR1  =  -(R1-sqrt(R1*R1 - D1*D1));
       #declare SagA1  =   Ad*pow(D1,4)+ Ae*pow(D1,6) + Af*pow(D1,8) + Ag*pow(D1,10); ;  
  //   #debug concat ( "Aspheric sag on surface 1 of lens ",str(LnzNum,3,0),str(SagA1,9,3))
       #declare Sag1   =   SagR1 + SagA1;
       #declare SagR2  =  -(R2-sqrt(R2*R2 - D2*D2));                                    
       #declare LensMM = intersection { isosurface { function  {  R1 - sqrt(R1*R1 - x*x - y*y) -
                                                                                        ( Ad*pow((x*x+y*y),2) + Ae*pow((x*x+y*y),3) +
                                                                                           Af*pow((x*x+y*y),4) + Ag*pow((x*x+y*y),5)
                                                                                        )

                                                                                       + z
                                                                                     }
                                                                         max_gradient IsoMaxgrad
                                                                         accuracy Evalac
                                                                         max_trace IsoMaxtrace                                                     

                                                       contained_by { box { <-D2,-D2,-300>,<D2,D2,Thk+1> } }
                                                       inverse
                                                     }
                                                       sphere { z*(-R2+Thk),R2 }
                                                       plane { z,Sag1 texture { LensBlnk} inverse }
                                                       cylinder { z*(Sag1-0.01),z*(Thk+100),D2 texture { LensBlnk } hollow  on }
                                                       material { LensOpen }
                                                     }                                                      
                                    
#end
#if ( AspR2 = yes)
       #declare Sag1  =  -(R1-sqrt(R1*R1 - D1*D1));
       #declare Sag2  =  -(R2-sqrt(R2*R2 - D2*D2));
       #declare Asp2  =   Ad*pow(D2,4) + Ae*pow(D2,6) + Af*pow(D2,8) + Ag*pow(D2,10);
       #debug concat ( "Aspheric sag on surface 2 of lens ",str(LnzNum,3,0),str(SagA2,9,3))
       #declare Sag   =   Sag2 + Asp2;
       #declare LensMM   = intersection { isosurface { function {  R2 - sqrt(R2*R2 -(x*x +y*y)) -                                                                                                                                                                Ad*pow((x*x+y*y),2) - Ae*pow((x*x+y*y),3)-
                                                                                       
                 Af*pow((x*x+y*y),4) - Ag*pow((x*x+y*y),5) )

                                                                                       + z - Thk                    
                                                                                    }
                                                         max _gradient IsoMaxgrad
                                                         accuracy Evalac
                                                         max_trace  IsoMaxtrace        

                                                         contained_by { box { <-D2,-D2,-30>,<D2,D2,30> } }
                                                       }
                                                         sphere { 0,R1 translate -z*R1 inverse}
                                                         cylinder { z*(Sag1-.01),z*(Thk+Asp2),D2 texture { LensBlnk } hollow  on}
                                                         material { LensOpen }
                                                      }                              
                                    
#end
#undef LensOpen