/* A macro for a convex-convex (plus/minus) 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 Sag2 = R2 - sqrt(R2*R2-D2*D2) + Thk+.01;
    #declare LensPM = intersection { sphere { 0,R1 translate z*R1 }

                                                   sphere { 0,R2 translate z*(-R2+Thk) }
                                                   cylinder {-z*1,z*(Thk+1),D1 texture { LensBlnk } hollow on }
                                                   material { LensOpen }
                                                 }
#end

#if ( AspR1 = 1)
    #declare SagSph1 = R1 - sqrt(R1*R1 - D1*D1);
    #declare SagAsp1 = 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(SagAsp1,9,3))
    #declare Sag1 = SagSph1 + SagAsp1;
    #if(Sag1 > 0 ) #declare Cutplane = 0; #end
    #if(Sag1 <= 0) #declare Cutplane = Sag1; #end
    #declare Sag2 = R2 - sqrt(R2*R2 - D2*D2); #declare BoxD = max(D1,D2);
    #warning concat( str(SagSph1,9,3),str(SagAsp1,9,3),"\n")
    #declare LensPM = 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 { <-BoxD,-BoxD,-100>,<BoxD,BoxD,Thk+20 >} }
                                                                   }
                                                        sphere { 0,R2 translate -z*(R2-Thk ) }
                                                          plane { z, Cutplane texture { LensBlnk } inverse}
                                                       cylinder { z*Cutplane,z*Thk,BoxD texture { LensBlnk } hollow on }
                                                      material { LensOpen }
                                                  }
#end

#if ( AspR2 = 1)
    #declare SagSph1 = R1 - sqrt(R1*R1 - D1*D1);
    #declare SagSph2 = -(R2 - sqrt(R2*R2 - D2*D2));
    #declare SagAsp2 = 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(SagAsp2,9,3))
    #declare Sag2 = SagSph2 + SagAsp2;
    #if(Sag2 > 0 ) #declare Cutplane = Sag2+Thk; #end
    #if(Sag2 <= 0) #declare Cutplane = Thk; #end
    #warning concat( str(SagSph2,9,3),str(SagAsp2,9,3),str(Sag2,9,3),str(Cutplane,9,3),"\n")
    #declare LensPM = intersection { sphere { 0,R1 translate z*R1 }
                                                   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 { <-D1,-D1,-1>,<D1,D1,20> } }
                                                   }
                                                  cylinder {-z*0.1,z*20,D1-0.01 texture { LensBlnk} hollow on }
                                                  material { LensOpen }
                                                 }
#end
#undef LensOpen