/* A macro for a convex-concave plus/plus 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 = no & AspR2 = no )
    #declare Sag2 = R2 - sqrt(R2*R2-D2*D2) + Thk+.01;
    #declare LensPP =  intersection { sphere { 0,R1 translate z*R1 }
                                                    sphere { 0,R2 translate z*(R2 + Thk) inverse }
                                                    cylinder { 0,z*Sag2,D1 texture { LensBlnk } hollow on}
                                                    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;
    #if(Sag1 < 0) #declare Cutplane = Sag1; #end
    #if(Sag1 >=0) #declare Cutplane = 0; #end
    #declare Sag2 = R2 - sqrt(R2*R2 - D2*D2);
    #declare LensPP = 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 { <-D1,-D1,-D1>,<D1,D1,D1>} }
                                                                   }
                                                   sphere { 0,R2 translate z*(R2+Thk ) inverse }
                                                   cylinder { z*Cutplane,z*(Sag2+Thk),D1 texture{ LensBlnk } hollow on }
                                                   material { LensOpen }
                                                  }
#end

#if ( AspR2 = yes)
      #declare SagR1 = R1 - sqrt(R1*R1 - D1*D1);
      #declare SagR2 = R2 - sqrt(R2*R2 - D2*D2);
      #declare SagA2 = 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 Sag2 = SagR2 + SagA2;
      #if(Sag2 < 0) #declare Cutplane = Thk+SagR2; #end
      #if(Sag2 >=0) #declare Cutplane = Sag2+Thk ; #end
      #declare LensPP = intersection { sphere { 0,R1 translate z*R1 }
                                                     isosurface { function { R2- sqrt(R2*R2-(x*x+y*y)) + Thk
                                                                                     + 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 IsoM axgrad accuracy Evalac
                                                                       max_trace IsoMaxtrace
                                                                       contained_by { box { <-D1,-D1,-D1>,<D1,D1,D1> } }
                                                                       inverse
                                                                     }
                                                         cylinder { 0,z*Cutplane,D1 texture{ LensBlnk} hollow on }
                                                         material { LensOpen }
                                                   }
#end
#undef LensOpen