Optica creates CDF content on the Web for optics

Donald Barnhart and Ann Williamson

Optica Software (Division of Barnhart Optical Research)

Abstract

Optica can be used to create interactive dynamic CDF content on the web of optical systems for teaching optics and sharing of optical models. The implications of this new paradigm could be revolutionary for the optics community. Since Optica can import the file formats of other popular optical design software packages, such as Zemax and Code V, the consequences of this new format could be quite far ranging. In particular, we hope to provide an on-line service for users of other optical modeling packages to upload their data files and immediately link to an interactive web-based model of their specific optical design. As such, new users need not purchase Optica or Mathematica to begin reaping the benefits of the CDF format. In this presentation, we will demonstrate how Optica can be used to generate CDF material and discuss our strategy to exploiting this exciting new format.

Optica Business Time-line

1991 Began development of Optica (Called LensLab)

December 1994 Optica released as first marketed package from Wolfram Research

Fall 2002 Optica business licensed to i-Cyt Mission Technology

December 2009 Independent business created for Optica.
	Technical Support and Sales: Ann Williamson
	Principle Development: Donald Barnhart
	Engineering: Lon Westfall

How Optica creates interactive content

Optica creates interactive content using the ManipulateSystem function

ManipulateSystem works for optics in an analogous way to the standard built-in Manipulate function of Mathematica

Here is a “Hello World” example that uses ManipulateSystem to build interactive content

Manipulate[
 Column[
  {OpenerView[{"TopView", Show[{Graphics[{{Opacity[0.8], GeometricTransformation[staticpolygons111572820111019113151440004, 
           Dynamic[BesselJ[1., 3.]; {{{Cos[Degree*θ2], -Sin[Degree*θ2]}, {Sin[Degree*θ2], Cos[Degree*θ2]}}, {xx2, yy2}}]], 
          staticpolygons121572820111019113151442056, Opacity[1], RGBColor[0.038747429704798886, 0.9161617552424038, 0.039847212269181365], 
          AbsoluteThickness[Medium], Line[Dynamic[Refresh[({#1[[{1, 2}]], #1[[{4, 5}]]} & ) /@ Select[raytracefunction11572820111019113151469728[xx1, 
                yy1, θ1, xx2, yy2, θ2], Length[#1] == 6 && #1[[1]] > -9.87654321*^8 & ], TrackedSymbols :> {xx1, yy1, θ1, xx2, yy2, θ2}], 
            SynchronousUpdating -> False]], Arrowheads[Large], Thickness[Large], Dashing[{Small, Small}], RGBColor[1, 1, 0], 
          Arrow[Dynamic[If[laststartposθ1 != {xx1, yy1} || lastθ1 =!= θ1, laststartposθ1 = {xx1, yy1}; lastθ1 = θ1; {xθ1, yθ1} = 
                laststartposθ1 + radiusθ1*{Cos[θ1*Degree], Sin[θ1*Degree]}]; {laststartposθ1, {xθ1, yθ1}}, 
            ({laststartposθ1, {xθ1, yθ1}} = #1) & ]], Tooltip[Locator[Dynamic[BesselJ[1., 3.]; (If[{xθ1, yθ1} != lastlocposθ1 || 
                 lastθ1 != θ1, lastlocposθ1 = {xθ1, yθ1}; lastθ1 = θ1; {xθ1, yθ1} = laststartposθ1 + radiusθ1*
                    {Cos[θ1*Degree], Sin[θ1*Degree]}]; {xθ1, yθ1}), ({xθ1, yθ1} = #1; xoffθ1 = xθ1 - xx1; yoffθ1 = yθ1 - yy1; 
               radiusθ1 = Sqrt[xoffθ1^2 + yoffθ1^2]; θ1 = Chop[ArcTan[xoffθ1, yoffθ1]/Degree]; {xθ1, yθ1}) & ], 
            Graphics[{RGBColor[1/3, 1/3, 1], Disk[{0, 0}, 0.5]}, ImageSize -> 10]], Dynamic[StringJoin["θ1 = ", ToString[θ1]]]], 
          Arrowheads[Large], Thickness[Large], Dashing[{Small, Small}], RGBColor[1, 1, 0], 
          Arrow[Dynamic[If[laststartposθ2 != {xx2, yy2} || lastθ2 =!= θ2, laststartposθ2 = {xx2, yy2}; lastθ2 = θ2; {xθ2, yθ2} = 
                laststartposθ2 + radiusθ2*{Cos[θ2*Degree], Sin[θ2*Degree]}]; {laststartposθ2, {xθ2, yθ2}}, 
            ({laststartposθ2, {xθ2, yθ2}} = #1) & ]], Tooltip[Locator[Dynamic[BesselJ[1., 3.]; (If[{xθ2, yθ2} != lastlocposθ2 || 
                 lastθ2 != θ2, lastlocposθ2 = {xθ2, yθ2}; lastθ2 = θ2; {xθ2, yθ2} = laststartposθ2 + radiusθ2*
                    {Cos[θ2*Degree], Sin[θ2*Degree]}]; {xθ2, yθ2}), ({xθ2, yθ2} = #1; xoffθ2 = xθ2 - xx2; yoffθ2 = yθ2 - yy2; 
               radiusθ2 = Sqrt[xoffθ2^2 + yoffθ2^2]; θ2 = Chop[ArcTan[xoffθ2, yoffθ2]/Degree]; {xθ2, yθ2}) & ], 
            Graphics[{RGBColor[1/3, 1/3, 1], Disk[{0, 0}, 0.5]}, ImageSize -> 10]], Dynamic[StringJoin["θ2 = ", ToString[θ2]]]], 
          Tooltip[Locator[Dynamic[BesselJ[1., 3.]; {xx1, yy1}, ({xx1, yy1} = Chop[#1]) & ], Graphics[{RGBColor[1, 1/3, 1/3], Disk[{0, 0}, 0.8]}, 
             ImageSize -> 10]], Dynamic[StringJoin["{xx1, yy1} = ", ToString[Chop[{xx1, yy1}]]]]], 
          Tooltip[Locator[Dynamic[BesselJ[1., 3.]; {xx2, yy2}, ({xx2, yy2} = Chop[#1]) & ], Graphics[{RGBColor[1, 1/3, 1/3], Disk[{0, 0}, 0.8]}, 
             ImageSize -> 10]], Dynamic[StringJoin["{xx2, yy2} = ", ToString[Chop[{xx2, yy2}]]]]]}}]}, 
      {PlotLabel -> "TopView", ImageSize -> 800, AspectRatio -> Automatic}]}, True], 
   Tooltip[Row[{Style["", Italic, 10], Hyperlink[Style["<< Crafted with Optica™ >>", Italic, 10], "http://www.opticasoftware.com"], 
      Style["", Italic, 10]}], "Click here to visit www.opticasoftware.com"]}, Center], {{xx1, 5.}, 0.05, 10, ControlType -> None}, 
 {{yy1, 0.}, -10, 10, ControlType -> None}, {{θ1, 0.}, -180., 180., ControlType -> None}, {{xx2, 10.}, 0.1, 20, ControlType -> None}, 
 {{yy2, 0.}, -10, 10, ControlType -> None}, {{θ2, 0.}, -180., 180., ControlType -> None}, {{minxx1, 0.05}, ControlType -> None}, 
 {{maxxx1, 10}, ControlType -> None}, {{minyy1, -10}, ControlType -> None}, {{maxyy1, 10}, ControlType -> None}, 
 {{minθ1, -180.}, ControlType -> None}, {{maxθ1, 180.}, ControlType -> None}, {{minxx2, 0.1}, ControlType -> None}, 
 {{maxxx2, 20}, ControlType -> None}, {{minyy2, -10}, ControlType -> None}, {{maxyy2, 10}, ControlType -> None}, 
 {{minθ2, -180.}, ControlType -> None}, {{maxθ2, 180.}, ControlType -> None}, 
 Button["Report", $OpticalSystemSettings = {SymbolicValues -> MapThread[ToExpression[#1] -> #2 & , {{"xx1", "xx2", "yy1", "yy2", "θ1", "θ2"}, 
        {xx1, xx2, yy1, yy2, θ1, θ2}}], SymbolicRanges -> {xx1 -> {0.05, 10}, yy1 -> {-10, 10}, θ1 -> {-180., 180.}, xx2 -> {0.1, 20}, 
       yy2 -> {-10, 10}, θ2 -> {-180., 180.}}}; $OpticalSystemScript = "{Move[LineOfRays[25, NumberOfRays -> 11], {{xx1, 5.}, {yy1, 0}}, {θ1, 0.}, \
SymbolicValues -> {xx1 -> 5., yy1 -> 0., θ1 -> 0.}], Move[PlanoConvexLens[100, 50, 10], {{xx2, 10.}, {yy2, 0}}, {θ2, 0.}, SymbolicValues -> {xx2 -> \
10., yy2 -> 0., θ2 -> 0.}], Boundary[{0., -25., -25.}, {100., 25., 25.}]}"; 
   Print[InputForm[StringJoin["SymbolicValues -> ", ToString[MapThread[StringJoin[#1, "->", ToString[#2, InputForm]] & , 
        {{"xx1", "xx2", "yy1", "yy2", "θ1", "θ2"}, {xx1, xx2, yy1, yy2, θ1, θ2}}]]]]], ImageSize -> Automatic], 
 {{xθ1, 7.5}, ControlType -> None}, {{yθ1, 0.}, ControlType -> None}, {{radiusθ1, 2.5}, ControlType -> None}, 
 {{xoffθ1, 2.5}, ControlType -> None}, {{yoffθ1, 0.}, ControlType -> None}, {{laststartposθ1, {5., 0.}}, ControlType -> None}, 
 {{lastlocposθ1, {7.5, 0.}}, ControlType -> None}, {{lastθ1, 0}, ControlType -> None}, {{xθ2, 12.5}, ControlType -> None}, 
 {{yθ2, 0.}, ControlType -> None}, {{radiusθ2, 2.5}, ControlType -> None}, {{xoffθ2, 2.5}, ControlType -> None}, 
 {{yoffθ2, 0.}, ControlType -> None}, {{laststartposθ2, {10., 0.}}, ControlType -> None}, {{lastlocposθ2, {12.5, 0.}}, ControlType -> None}, 
 {{lastθ2, 0}, ControlType -> None}, {{staticpolygons111572820111019113151440004, {AbsoluteThickness[1], AbsolutePointSize[1], EdgeForm[], 
    RGBColor[0.4, 0.6, 0.8], Polygon[{{{6.421974580955592, -25.}, {4.3675065668300235, -20.833333333333336}, 
       {2.7504488342847893, -16.666666666666668}, {1.5286341456750776, -12.500000000000002}, {0.6737579958920605, -8.333333333333336}, 
       {0.16761639451521404, -4.166666666666671}, {0., -3.552713678800501*^-15}, {0.16761639451521404, 4.166666666666664}, 
       {0.6737579958920605, 8.333333333333329}, {1.5286341456750776, 12.499999999999993}, {2.750448834284782, 16.666666666666657}, 
       {4.3675065668300235, 20.83333333333333}, {6.421974580955585, 24.999999999999993}, {10., 25.}, {10., 20.833333333333336}, 
       {10., 16.666666666666668}, {10., 12.500000000000002}, {10., 8.333333333333336}, {10., 4.166666666666671}, {10., 3.552713678800501*^-15}, 
       {10., -4.166666666666664}, {10., -8.333333333333329}, {10., -12.499999999999993}, {10., -16.666666666666657}, {10., -20.83333333333333}, 
       {10., -24.999999999999993}, {6.421974580955592, -25.}}}]}}, ControlType -> None}, 
 {{staticpolygons121572820111019113151442056, {GrayLevel[0], AbsoluteThickness[0.5], 
    Line[{{{100., 25.}, {100., -25.}, {100., -25.}, {100., 25.}, {100., 25.}}, {{100., -25.}, {0., -25.}, {0., -25.}, {100., -25.}, {100., -25.}}, 
      {{0., -25.}, {0., 25.}, {0., 25.}, {0., -25.}, {0., -25.}}, {{0., 25.}, {100., 25.}, {100., 25.}, {0., 25.}, {0., 25.}}, 
      {{100., -25.}, {100., 25.}, {0., 25.}, {0., -25.}, {100., -25.}}, {{100., 25.}, {100., -25.}, {0., -25.}, {0., 25.}, {100., 25.}}}]}}, 
  ControlType -> None}, Initialization :> (raytracefunction11572820111019113151469728 = 
   Compile[{{xx1, _Real}, {yy1, _Real}, {θ1, _Real}, {xx2, _Real}, {yy2, _Real}, {θ2, _Real}}, 
    Module[{activeregion, allcreatedamplitudes, allcreatedrays, allowzeroraylength, alternateindex, amplitude, axismagnitude, axismatrix, clonedata, 
      clonegenerationnumber, cloneinput, clonenumber, cloneposition, clonereportedrays, clonerotation, clonescale, clonescalevector, 
      clonesorthistogram, clonetraceinput, clonetraceoutput, closestdistances, closestposition, closestpositions, closestraylength, completedray, 
      componentnumber, cosangle, crossmatrix, deflecteddummyrayspresent, deflectedlocalrays, deflectedrays, deflectedrayspresent, dummymatrix, 
      dummymatrixlist, dummyvector, frontid, generationlimit, generationnumber, holeintersections, holeQ, holeQlist, hor, index, index1, index2, 
      inrays, intensity, intensityscale, intersectionnumber, intersections, intersectionslist, intersectionspresent, localdummyoutrayspresent, 
      localoutrays, localoutrayspresent, magnitude, maxclonegenerationnumber, maximumposition, minimumraylength, minresonateraylength, n12, 
      newcomponentnumber, newdeflectedrays, newdistance, newsurfaceid, newsurfacenumber, nonintersectingrays, normalizedpolarization, 
      oldgenerationnumber, opticallength, originalray, originalraytilt, originalrotation, originalsurfnorm, outrays, parallelamplitude, PAxis, 
      perpendicularamplitude, phaseshift, pol1, pol2, pol3, previoustilt, propagate, propagatedrays, r1, r2, r3, r4, r5, r6, r7, r8, r9, ray1, ray2, 
      ray3, LocRayL, rayslot, raysreported, raytiltx, raytiltz, reporteddummyrayspresent, reportedlocalrays, reportedrays, reportedrayspresent, 
      resonateintersections, resonatenumber, resonateraylength, rex, rey, rez, rotationaxis, rsx, LocRSx, rsy, LocRSy, rsz, LocRSz, RTx, LocRTy, 
      LocRTz, rx, rxhlp, ry, ryhlp, rz, rzhlp, LocSCx, LocSCy, sid, sinangle, sno1, sno2, sno3, sno4, surfaceid, surfacenumber, 
      surfacerotationmatrix, surfacetranslationvector, surfnorm, symr1, symr2, symr3, symr4, symr5, symr6, symr7, symr8, symr9, tempclonedata, 
      terminatedeflection, thresholdintensity, tiltmagnitude, transmatrix, LocTV1, LocTV10, LocTV2, LocTV3, LocTV4, LocTV5, LocTV6, LocTV7, LocTV8, 
      LocTV9, u, unfilteredintersections, unit, uu, ver, wavelength, XAxis, xunit, YAxis, yunit, ZAxis, zunit}, 
     activeregion = 1.; allcreatedamplitudes = {{0. + 0.*I, 0. + 0.*I, 0. + 0.*I}}; allcreatedrays = {{1.}}; allowzeroraylength = True; 
      alternateindex = 1.; amplitude = {1. + 0.*I, 0. + 0.*I, 0. + 0.*I}; axismagnitude = 1.; 
      axismatrix = {{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}; clonedata = {{1.}}; clonegenerationnumber = 1.; cloneinput = {1.}; clonenumber = 1; 
      cloneposition = {0., 0., 0.}; clonereportedrays = {{1.}}; clonerotation = {{1.}}; clonescale = 1.; clonescalevector = {1.}; 
      clonesorthistogram = {1}; clonetraceinput = {{1.}}; clonetraceoutput = {{1.}}; closestdistances = {1.}; closestposition = 0; 
      closestpositions = {0}; closestraylength = 1.; completedray = {1.}; componentnumber = -1.; cosangle = 1.; 
      crossmatrix = {{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}; deflecteddummyrayspresent = False; deflectedlocalrays = {{1.}}; 
      deflectedrays = {{1.}}; deflectedrayspresent = False; dummymatrix = {{1.}}; dummymatrixlist = {{{1.}}}; dummyvector = {1.}; frontid = 1.; 
      generationlimit = 200.; generationnumber = 1.; holeintersections = {{-1., 0., 0.}}; holeQ = False; holeQlist = {0}; hor = 1. + 0.*I; 
      index = 1.; index1 = 1.; index2 = 1.; inrays = {{1.}}; intensity = 1.; intensityscale = 1.; intersectionnumber = 1.; 
      intersections = {{-1., 0., 0.}}; intersectionslist = {{{-1., 0., 0.}}}; intersectionspresent = False; localdummyoutrayspresent = False; 
      localoutrays = {{1.}}; localoutrayspresent = False; magnitude = 1.; maxclonegenerationnumber = 1.; maximumposition = 1; 
      minimumraylength = 0.0001; minresonateraylength = 1.; n12 = 1.; newcomponentnumber = -1.; newdeflectedrays = {{1.}}; newdistance = 1.; 
      newsurfaceid = -1.; newsurfacenumber = -1.; nonintersectingrays = {{1.}}; normalizedpolarization = {1., 1.}; oldgenerationnumber = 1.; 
      opticallength = 1.; originalray = {1.}; originalraytilt = {1.}; originalrotation = {{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}; 
      originalsurfnorm = {{1.}}; outrays = {{1.}}; parallelamplitude = 1. + 0.*I; PAxis = {1., 0., 0.}; perpendicularamplitude = 1. + 0.*I; 
      phaseshift = 0.; pol1 = 1.; pol2 = 1.; pol3 = 1.; previoustilt = {1., 0., 0.}; propagate = True; propagatedrays = {{1.}}; r1 = 1.; r2 = 1.; 
      r3 = 1.; r4 = 1.; r5 = 1.; r6 = 1.; r7 = 1.; r8 = 1.; r9 = 1.; ray1 = 1.; ray2 = 1.; ray3 = 1.; LocRayL = 1.; rayslot = 1.; 
      raysreported = False; raytiltx = 1.; raytiltz = 1.; reporteddummyrayspresent = False; reportedlocalrays = {{1.}}; reportedrays = {{1.}}; 
      reportedrayspresent = False; resonateintersections = {{-1., 0., 0.}}; resonatenumber = 0; resonateraylength = 1.; rex = 1.; rey = 1.; rez = 1.; 
      rsx = 1.; LocRSx = 1.; rsy = 1.; LocRSy = 1.; rsz = 1.; LocRSz = 1.; RTx = 1.; LocRTy = 1.; LocRTz = 1.; rx = 1.; rxhlp = 1.; ry = 1.; 
      ryhlp = 1.; rz = 1.; rzhlp = 1.; LocSCx = 1.; LocSCy = 1.; sid = 1.; sinangle = 1.; sno1 = 1.; sno2 = 1.; sno3 = 1.; sno4 = 1.; 
      surfaceid = -1.; surfacenumber = -1.; surfacerotationmatrix = {{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}; 
      surfacetranslationvector = {0., 0., 0.}; surfnorm = {{1.}}; symr1 = 1.; symr2 = 1.; symr3 = 1.; symr4 = 1.; symr5 = 1.; symr6 = 1.; symr7 = 1.; 
      symr8 = 1.; symr9 = 1.; tempclonedata = {{1.}}; terminatedeflection = False; thresholdintensity = 1.; tiltmagnitude = 1.; 
      transmatrix = {{{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}}; LocTV1 = 1.; LocTV10 = 1.; LocTV2 = 1.; LocTV3 = 1.; LocTV4 = 1.; LocTV5 = 1.; 
      LocTV6 = 1.; LocTV7 = 1.; LocTV8 = 1.; LocTV9 = 1.; u = {{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}; unfilteredintersections = {{-1., 0., 0.}}; 
      unit = {{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}; uu = {{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}; ver = 0. + 0.*I; wavelength = 1.; 
      XAxis = {1., 0., 0.}; xunit = 1.; YAxis = {1., 0., 0.}; yunit = 1.; ZAxis = {1., 0., 0.}; zunit = 1.; 
      propagatedrays = Join[{symr1, symr2, symr3, symr4, symr5, symr6, symr7, symr8, symr9} = 
          Re[{Cos[Degree*θ1], Sin[Degree*θ1], 0, -Sin[Degree*θ1], Cos[Degree*θ1], 0, 0, 0, 1}]; 
         (({rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, 
              phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, oldgenerationnumber, 
              intersectionnumber, pol1, pol2, pol3} = #1; {LocTV1, LocTV2, LocTV3} = {rsx*symr1 + rsy*symr4 + rsz*symr7 + xx1, 
              rsx*symr2 + rsy*symr5 + rsz*symr8 + yy1, rsx*symr3 + rsy*symr6 + rsz*symr9}; {rsx, rsy, rsz} = {LocTV1, LocTV2, LocTV3}; 
            {LocTV1, LocTV2, LocTV3} = {rex*symr1 + rey*symr4 + rez*symr7 + xx1, rex*symr2 + rey*symr5 + rez*symr8 + yy1, 
              rex*symr3 + rey*symr6 + rez*symr9}; {rex, rey, rez} = {LocTV1, LocTV2, LocTV3}; {LocTV1, LocTV2, LocTV3} = 
             {r1*symr1 + r2*symr4 + r3*symr7, r1*symr2 + r2*symr5 + r3*symr8, r1*symr3 + r2*symr6 + r3*symr9}; {LocTV4, LocTV5, LocTV6} = 
             {r4*symr1 + r5*symr4 + r6*symr7, r4*symr2 + r5*symr5 + r6*symr8, r4*symr3 + r5*symr6 + r6*symr9}; {LocTV7, LocTV8, LocTV9} = 
             {r7*symr1 + r8*symr4 + r9*symr7, r7*symr2 + r8*symr5 + r9*symr8, r7*symr3 + r8*symr6 + r9*symr9}; 
            {{r1, r2, r3}, {r4, r5, r6}, {r7, r8, r9}} = {{LocTV1, LocTV2, LocTV3}, {LocTV4, LocTV5, LocTV6}, {LocTV7, LocTV8, LocTV9}}; 
            {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, 
             phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, 
             intersectionnumber, pol1, pol2, pol3}) & ) /@ {{0., -12.5, 0., 0., -12.5, 0., 1., 0., 0., 0., 1., 0., 0., 0., 1., 0.532, 100., 
            0.09090909090909091, 0., 1.00026945145708, 0., 0., 1., 0., 0., 0., 0., 122491., 122491., 1., 11., 1., 0., 1., 0., 0., 0., 1., 0.}, 
           {0., -10., 0., 0., -10., 0., 1., 0., 0., 0., 1., 0., 0., 0., 1., 0.532, 100., 0.09090909090909091, 0., 1.00026945145708, 0., 0., 1., 0., 
            0., 0., 0., 122491., 122491., 2., 11., 2., 0., 1., 0., 0., 0., 1., 0.}, {0., -7.5, 0., 0., -7.5, 0., 1., 0., 0., 0., 1., 0., 0., 0., 1., 
            0.532, 100., 0.09090909090909091, 0., 1.00026945145708, 0., 0., 1., 0., 0., 0., 0., 122491., 122491., 3., 11., 3., 0., 1., 0., 0., 0., 
            1., 0.}, {0., -5., 0., 0., -5., 0., 1., 0., 0., 0., 1., 0., 0., 0., 1., 0.532, 100., 0.09090909090909091, 0., 1.00026945145708, 0., 0., 
            1., 0., 0., 0., 0., 122491., 122491., 4., 11., 4., 0., 1., 0., 0., 0., 1., 0.}, {0., -2.5, 0., 0., -2.5, 0., 1., 0., 0., 0., 1., 0., 0., 
            0., 1., 0.532, 100., 0.09090909090909091, 0., 1.00026945145708, 0., 0., 1., 0., 0., 0., 0., 122491., 122491., 5., 11., 5., 0., 1., 0., 
            0., 0., 1., 0.}, {0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 0., 0., 0., 1., 0.532, 100., 0.09090909090909091, 0., 1.00026945145708, 0., 
            0., 1., 0., 0., 0., 0., 122491., 122491., 6., 11., 6., 0., 1., 0., 0., 0., 1., 0.}, {0., 2.5, 0., 0., 2.5, 0., 1., 0., 0., 0., 1., 0., 
            0., 0., 1., 0.532, 100., 0.09090909090909091, 0., 1.00026945145708, 0., 0., 1., 0., 0., 0., 0., 122491., 122491., 7., 11., 7., 0., 1., 
            0., 0., 0., 1., 0.}, {0., 5., 0., 0., 5., 0., 1., 0., 0., 0., 1., 0., 0., 0., 1., 0.532, 100., 0.09090909090909091, 0., 1.00026945145708, 
            0., 0., 1., 0., 0., 0., 0., 122491., 122491., 8., 11., 8., 0., 1., 0., 0., 0., 1., 0.}, {0., 7.5, 0., 0., 7.5, 0., 1., 0., 0., 0., 1., 
            0., 0., 0., 1., 0.532, 100., 0.09090909090909091, 0., 1.00026945145708, 0., 0., 1., 0., 0., 0., 0., 122491., 122491., 9., 11., 9., 0., 
            1., 0., 0., 0., 1., 0.}, {0., 10., 0., 0., 10., 0., 1., 0., 0., 0., 1., 0., 0., 0., 1., 0.532, 100., 0.09090909090909091, 0., 
            1.00026945145708, 0., 0., 1., 0., 0., 0., 0., 122491., 122491., 10., 11., 10., 0., 1., 0., 0., 0., 1., 0.}, 
           {0., 12.5, 0., 0., 12.5, 0., 1., 0., 0., 0., 1., 0., 0., 0., 1., 0.532, 100., 0.09090909090909091, 0., 1.00026945145708, 0., 0., 1., 0., 
            0., 0., 0., 122491., 122491., 11., 11., 11., 0., 1., 0., 0., 0., 1., 0.}}]; 
      propagatedrays = (({rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, 
            opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, 
            oldgenerationnumber, intersectionnumber, pol1, pol2, pol3} = #1; componentnumber = 1.; frontid = 1.; 
          {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, 
           phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, 
           intersectionnumber, pol1, pol2, pol3}) & ) /@ propagatedrays; thresholdintensity = 1.; generationlimit = 200.; 
      reportedrays = {{rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, 
         opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, 
         generationnumber, intersectionnumber, pol1, pol2, pol3}}; generationnumber = 0.; (inrays = propagatedrays; 
       outrays = {{1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 1., 1., 1., 1., 0., -1., -1., -1., 1., 1., 1., 1., 
          1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}}; deflectedrayspresent = True; reportedrayspresent = False; propagate = True; 
       While[deflectedrayspresent && propagate, generationnumber += 1.; (deflecteddummyrayspresent = False; deflectedrayspresent = False; 
          reportedrayspresent = False; reporteddummyrayspresent = True; localdummyoutrayspresent = False; localoutrayspresent = False; 
          {deflectedlocalrays, localoutrays, reportedlocalrays} = (Flatten[#1, 1] & ) /@ 
            Transpose[(({rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, 
                  opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, 
                  oldgenerationnumber, intersectionnumber, pol1, pol2, pol3} = #1; If[intensity > thresholdintensity && generationnumber < 
                   generationlimit, (resonatenumber = 0; resonateraylength = 9.87654321*^8; resonateintersections = {{-1., 0., 0.}}; 
                   MapIndexed[If[#1[[1,1]] > -1. && #1[[1,1]] < resonateraylength, resonateraylength = #1[[1,1]]; resonateintersections = #1; 
                       resonatenumber = First[#2], resonatenumber] & , intersectionslist = {{{-1., 0., 0.}}}; holeQlist = {0}; 
                     ({rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, 
                        opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, 
                        rayslot, oldgenerationnumber, intersectionnumber, pol1, pol2, pol3} = #1; index1 = index; xunit = 
                       r1*Re[Cos[0.017453292519943295*θ2]] + r2*Re[Sin[0.017453292519943295*θ2]]; yunit = r2*Re[Cos[0.017453292519943295*θ2]] - 
                        1.*r1*Re[Sin[0.017453292519943295*θ2]]; zunit = r3*Re[Cos[0.017453292519943295*θ2]^2 + Sin[0.017453292519943295*θ2]^2]; 
                      xunit = Chop[xunit]; yunit = Chop[yunit]; zunit = Chop[zunit]; magnitude = (xunit^2 + yunit^2 + zunit^2)^2^(-1); 
                      xunit = xunit/magnitude; yunit = yunit/magnitude; zunit = zunit/magnitude; rx = (rex - 1.*Re[xx2])*Re[Cos[0.017453292519943295*
                            θ2]] + (rey - 1.*Re[yy2])*Re[Sin[0.017453292519943295*θ2]]; ry = (rey - 1.*Re[yy2])*Re[Cos[0.017453292519943295*
                            θ2]] - 1.*(rex - 1.*Re[xx2])*Re[Sin[0.017453292519943295*θ2]]; rz = rez*Re[Cos[0.017453292519943295*θ2]^2 + 
                          Sin[0.017453292519943295*θ2]^2]; rx = Chop[rx]; ry = Chop[ry]; rz = Chop[rz]; terminatedeflection = False; 
                      intersections = {{-1., 0., 0.}}; intersections = Block[{Compile`$450, Compile`$460, Compile`$461}, Block[{TempVar, TempVar1, 
                          TempVar2, TempVar3, LocRayL, LocSCx, LocSCy}, TempVar1 = 1.; TempVar2 = 1.; TempVar3 = 1.; TempVar = {{-1., 0., 0.}}; 
                          LocRayL = 1.; LocSCx = 1.; LocSCy = 1.; TempVar1 = rx^2 + ry^2 + rz^2; TempVar2 = rx*xunit + ry*yunit + rz*zunit - 
                            51.872033213443714*xunit; TempVar3 = -TempVar1 + TempVar2^2 + 2*rx*51.872033213443714; TempVar3 = Re[TempVar3]; 
                          If[N[Chop[TempVar3]] >= 0., TempVar3 = Sqrt[TempVar3]; TempVar = {{LocRayL, LocSCx, LocSCy} = (Compile`$450 = TempVar2 + 
                                   TempVar3; {-TempVar2 - TempVar3, ry - Compile`$450*yunit, rz - Compile`$450*zunit}); If[N[Chop[LocRayL]] >= 0. && 
                                 LocSCx^2 + LocSCy^2 <= 625. && Abs[ry + LocRayL*yunit - LocSCx] + Abs[rz + LocRayL*zunit - LocSCy] + 
                                   Abs[-51.872033213443714 + rx + LocRayL*xunit + Sqrt[2690.707829696608 - LocSCx^2 - LocSCy^2]] < 1.*^-7, 
                                {LocRayL, LocSCx, LocSCy}, {-1., 0., 0.}], {LocRayL, LocSCx, LocSCy} = (Compile`$460 = -TempVar2; Compile`$461 = 
                                  Compile`$460 + TempVar3; {Compile`$461, ry + Compile`$461*yunit, rz + Compile`$461*zunit}); If[N[Chop[LocRayL]] >= 
                                  0. && LocSCx^2 + LocSCy^2 <= 625. && Abs[ry + LocRayL*yunit - LocSCx] + Abs[rz + LocRayL*zunit - LocSCy] + 
                                   Abs[-51.872033213443714 + rx + LocRayL*xunit + Sqrt[2690.707829696608 - LocSCx^2 - LocSCy^2]] < 1.*^-7, 
                                {LocRayL, LocSCx, LocSCy}, {-1., 0., 0.}]}; If[TempVar[[1]] == TempVar[[2]], {TempVar[[1]]}, TempVar], 
                           {{-1., 0., 0.}}]]]; AppendTo[intersections, {-1., 0., 0.}]; holeQ = False; If[ !(Chop[surfaceid - 122529.] == 0. || 
                          N[Chop[ray1]] < 0.0001) || generationnumber == 1., minresonateraylength = 0., minresonateraylength = 0.0001]; 
                      Block[{LocRayL, LocSCx, LocSCy}, LocRayL = 1.; LocSCx = 1.; LocSCy = 1.; dummymatrix = Union[Select[intersections, 
                           ({LocRayL, LocSCx, LocSCy} = #1; {LocRayL, LocSCx, LocSCy} == {-1., 0., 0.} || N[Chop[LocRayL]] >= 
                               minresonateraylength) & ]]; intersections = dummymatrix]; unfilteredintersections = intersections; 
                      If[Length[intersections] > 1, dummymatrix = Take[intersections, {2}]; intersections = dummymatrix; 1., 1.]; 
                      AppendTo[intersectionslist, intersections]; If[holeQ, AppendTo[holeQlist, 1], AppendTo[holeQlist, 0]]; 
                      {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, 
                        opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, 
                        rayslot, oldgenerationnumber, intersectionnumber, pol1, pol2, pol3} = #1; index1 = index; xunit = 
                       r1*Re[Cos[0.017453292519943295*θ2]] + r2*Re[Sin[0.017453292519943295*θ2]]; yunit = r2*Re[Cos[0.017453292519943295*θ2]] - 
                        1.*r1*Re[Sin[0.017453292519943295*θ2]]; zunit = r3*Re[Cos[0.017453292519943295*θ2]^2 + Sin[0.017453292519943295*θ2]^2]; 
                      xunit = Chop[xunit]; yunit = Chop[yunit]; zunit = Chop[zunit]; magnitude = (xunit^2 + yunit^2 + zunit^2)^2^(-1); 
                      xunit = xunit/magnitude; yunit = yunit/magnitude; zunit = zunit/magnitude; rx = Re[Cos[0.017453292519943295*θ2]]*
                         (rex - 1.*Re[xx2 + 10.*Cos[0.017453292519943295*θ2]]) + Re[Sin[0.017453292519943295*θ2]]*(rey - 
                          1.*Re[yy2 + 10.*Sin[0.017453292519943295*θ2]]); ry = -1.*(rex - 1.*Re[xx2 + 10.*Cos[0.017453292519943295*θ2]])*
                         Re[Sin[0.017453292519943295*θ2]] + Re[Cos[0.017453292519943295*θ2]]*(rey - 1.*Re[yy2 + 10.*Sin[0.017453292519943295*
                                θ2]]); rz = rez*Re[Cos[0.017453292519943295*θ2]^2 + Sin[0.017453292519943295*θ2]^2]; rx = Chop[rx]; 
                      ry = Chop[ry]; rz = Chop[rz]; terminatedeflection = False; intersections = {{-1., 0., 0.}}; intersections = 
                       Block[{Compile`$79}, If[Chop[xunit] != 0, {(If[N[Chop[#1[[1]]]] >= 0. && #1[[2]]^2 + #1[[3]]^2 <= 625., {#1[[1]], #1[[2]], 
                              #1[[3]]}, {-1., 0., 0.}] & )[Compile`$79 = xunit^(-1); {-(rx*Compile`$79), ry - (rx*yunit)*Compile`$79, 
                             rz - (rx*zunit)*Compile`$79}]}, {{-1., 0., 0.}}]]; AppendTo[intersections, {-1., 0., 0.}]; holeQ = False; 
                      If[ !(Chop[surfaceid - 122530.] == 0. || N[Chop[ray1]] < 0.0001) || generationnumber == 1., minresonateraylength = 0., 
                       minresonateraylength = 0.0001]; Block[{LocRayL, LocSCx, LocSCy}, LocRayL = 1.; LocSCx = 1.; LocSCy = 1.; dummymatrix = 
                         Union[Select[intersections, ({LocRayL, LocSCx, LocSCy} = #1; {LocRayL, LocSCx, LocSCy} == {-1., 0., 0.} || 
                              N[Chop[LocRayL]] >= minresonateraylength) & ]]; intersections = dummymatrix]; unfilteredintersections = intersections; 
                      If[Length[intersections] > 1, dummymatrix = Take[intersections, {2}]; intersections = dummymatrix; 1., 1.]; 
                      AppendTo[intersectionslist, intersections]; If[holeQ, AppendTo[holeQlist, 1], AppendTo[holeQlist, 0]]); holeQlist = 
                      Drop[holeQlist, 1]; Drop[intersectionslist, 1]]); Which[resonatenumber == 1, {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, 
                      r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, 
                      surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, oldgenerationnumber, intersectionnumber, pol1, pol2, 
                      pol3} = #1; index1 = index; xunit = r1*Re[Cos[0.017453292519943295*θ2]] + r2*Re[Sin[0.017453292519943295*θ2]]; 
                    yunit = r2*Re[Cos[0.017453292519943295*θ2]] - 1.*r1*Re[Sin[0.017453292519943295*θ2]]; zunit = 
                     r3*Re[Cos[0.017453292519943295*θ2]^2 + Sin[0.017453292519943295*θ2]^2]; xunit = Chop[xunit]; yunit = Chop[yunit]; 
                    zunit = Chop[zunit]; magnitude = (xunit^2 + yunit^2 + zunit^2)^2^(-1); xunit = xunit/magnitude; yunit = yunit/magnitude; 
                    zunit = zunit/magnitude; rx = (rex - 1.*Re[xx2])*Re[Cos[0.017453292519943295*θ2]] + (rey - 1.*Re[yy2])*
                       Re[Sin[0.017453292519943295*θ2]]; ry = (rey - 1.*Re[yy2])*Re[Cos[0.017453292519943295*θ2]] - 1.*(rex - 1.*Re[xx2])*
                       Re[Sin[0.017453292519943295*θ2]]; rz = rez*Re[Cos[0.017453292519943295*θ2]^2 + Sin[0.017453292519943295*θ2]^2]; 
                    rx = Chop[rx]; ry = Chop[ry]; rz = Chop[rz]; terminatedeflection = False; intersections = {{-1., 0., 0.}}; intersections = 
                     resonateintersections; holeQ = holeQlist[[resonatenumber]] == 1; {newcomponentnumber, newsurfacenumber, newsurfaceid} = 
                     {1., 1., 122529.}; {componentnumber, surfacenumber, surfaceid} = {1., 1., 122529.}; {ray1, ray2, ray3} = intersections[[1]]; 
                    rsx = rex; rsy = rey; rsz = rez; rxhlp = rx + ray1*xunit; ryhlp = ry + ray1*yunit; rzhlp = rz + ray1*zunit; 
                    rex = Re[xx2] + rxhlp*Re[Cos[0.017453292519943295*θ2]] - 1.*ryhlp*Re[Sin[0.017453292519943295*θ2]]; 
                    rey = Re[yy2] + ryhlp*Re[Cos[0.017453292519943295*θ2]] + rxhlp*Re[Sin[0.017453292519943295*θ2]]; 
                    rez = rzhlp*Re[Cos[0.017453292519943295*θ2]^2 + Sin[0.017453292519943295*θ2]^2]; rex = Chop[rex]; rey = Chop[rey]; 
                    rez = Chop[rez]; intersectionnumber = intersectionnumber + 1.; opticallength = opticallength + ray1*index1; completedray = 
                     {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, 
                      opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, 
                      rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}; reportedrayspresent = True; intersectionspresent = True; 
                    deflectedrayspresent = True; localdummyoutrayspresent = True; (surfnorm = 1.*Re[{{1., -(ray2/(2690.707829696608 - ray2^2 - 
                              ray3^2)^2^(-1)), -(ray3/(2690.707829696608 - ray2^2 - ray3^2)^2^(-1))}, {ray2/(2690.707829696608 - ray2^2 - ray3^2)^
                            2^(-1), 1., 0.}, {ray3/(2690.707829696608 - ray2^2 - ray3^2)^2^(-1), 0., 1.}}]; dummymatrix = 
                      ({{Re[Cos[0.017453292519943295*θ2]], -1.*Re[Sin[0.017453292519943295*θ2]], 0.}, {Re[Sin[0.017453292519943295*θ2]], 
                           Re[Cos[0.017453292519943295*θ2]], 0.}, {0., 0., 1.}} . #1 & ) /@ surfnorm; surfnorm = (#1/Sqrt[#1 . #1] & ) /@ 
                       dummymatrix; surfacerotationmatrix = {{Re[Cos[0.017453292519943295*θ2]], Re[Sin[0.017453292519943295*θ2]], 0.}, 
                       {-1.*Re[Sin[0.017453292519943295*θ2]], Re[Cos[0.017453292519943295*θ2]], 0.}, {0., 0., 1.}}; surfacetranslationvector = 
                      {Re[xx2], Re[yy2], 0.}; activeregion = 1.; completedray = {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, 
                       wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, 
                       ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}; 
                     componentnumber = newcomponentnumber; surfacenumber = newsurfacenumber; surfaceid = newsurfaceid; originalray = 
                      {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, 
                       opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, 
                       rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}; ZAxis = First[surfnorm]*Sign[First[surfnorm] . 
                         {r1, r2, r3}]; raytiltz = ZAxis . {r1, r2, r3}; raytiltx = 1 - raytiltz^2; If[raytiltx < 2.220446049250313*^-16, 
                      raytiltx = 0., raytiltx = Sqrt[raytiltx]]; XAxis = {r1, r2, r3} - ZAxis*raytiltz; If[Abs[raytiltx] < 1.*^-7, 
                      XAxis = surfnorm[[2]], XAxis = XAxis/raytiltx]; YAxis = {-(ZAxis[[3]]*XAxis[[2]]) + ZAxis[[2]]*XAxis[[3]], 
                       ZAxis[[3]]*XAxis[[1]] - ZAxis[[1]]*XAxis[[3]], -(ZAxis[[2]]*XAxis[[1]]) + ZAxis[[1]]*XAxis[[2]]}; YAxis = 
                      YAxis/Sqrt[YAxis . YAxis]; PAxis = ZAxis*{r1, r2, r3} . XAxis - XAxis*{r1, r2, r3} . ZAxis; PAxis = PAxis/Sqrt[PAxis . PAxis]; 
                     normalizedpolarization = {pol1, pol2, pol3}[[{1, 2}]]; normalizedpolarization = normalizedpolarization/
                       Sqrt[normalizedpolarization . normalizedpolarization]; allcreatedamplitudes = {{I*0., I*0., I*0.}}; amplitude = 
                      Sqrt[intensity]*(normalizedpolarization[[1]]*{r4, r5, r6} + normalizedpolarization[[2]]*{r7, r8, r9}*
                         Exp[I*pol3*0.017453292519943295]); perpendicularamplitude = amplitude . YAxis; parallelamplitude = amplitude . PAxis; 
                     If[activeregion*(surfacerotationmatrix . {r1, r2, r3})[[1]] > 0, index2 = Sqrt[2.2718929 - 0.010108077*wavelength^2 + 
                          0.010592509/wavelength^2 + 0.00020816965/wavelength^4 - 7.6472538*^-6/wavelength^6 + 4.9240991*^-7/wavelength^8]; 
                       index1 = index; If[Abs[index2 - index1] < 0.01, alternateindex = Sqrt[1.0005281111748805 - 2.364805676582992*^-10*
                             wavelength^2 + 2.9781670902982902*^-6/wavelength^2 + 2.6004099469477187*^-8/wavelength^4 + 3.7787839314048715*^-10/
                             wavelength^6 + 2.830549017940716*^-12/wavelength^8]; If[Abs[alternateindex - index1] > Abs[index2 - index1], 
                          index1 = index2; index2 = alternateindex, index1 = alternateindex], index1 = Sqrt[1.0005281111748805 - 
                           2.364805676582992*^-10*wavelength^2 + 2.9781670902982902*^-6/wavelength^2 + 2.6004099469477187*^-8/wavelength^4 + 
                           3.7787839314048715*^-10/wavelength^6 + 2.830549017940716*^-12/wavelength^8]], index2 = Sqrt[1.0005281111748805 - 
                          2.364805676582992*^-10*wavelength^2 + 2.9781670902982902*^-6/wavelength^2 + 2.6004099469477187*^-8/wavelength^4 + 
                          3.7787839314048715*^-10/wavelength^6 + 2.830549017940716*^-12/wavelength^8]; index1 = index; If[Abs[index2 - index1] < 
                         0.01, alternateindex = Sqrt[2.2718929 - 0.010108077*wavelength^2 + 0.010592509/wavelength^2 + 0.00020816965/wavelength^4 - 
                            7.6472538*^-6/wavelength^6 + 4.9240991*^-7/wavelength^8]; If[Abs[alternateindex - index1] > Abs[index2 - index1], 
                          index1 = index2; index2 = alternateindex, index1 = alternateindex], index1 = Sqrt[2.2718929 - 0.010108077*wavelength^2 + 
                           0.010592509/wavelength^2 + 0.00020816965/wavelength^4 - 7.6472538*^-6/wavelength^6 + 4.9240991*^-7/wavelength^8]]]; 
                     n12 = index1/index2; index = index2; If[1 - n12^2*raytiltx^2 >= 0., dummyvector = n12*{r1, r2, r3} + ZAxis*(-(n12*raytiltz) + 
                           Sqrt[1 - n12^2*raytiltx^2]); {r1, r2, r3} = dummyvector; 1., If[{r1, r2, r3} . surfnorm[[1]] > 0, phaseshift = 
                         phaseshift + 0.5, 1.]; index2 = index1; index = index1; dummyvector = {r1, r2, r3} - 2*ZAxis*ZAxis . {r1, r2, r3}; 
                       {r1, r2, r3} = dummyvector; dummyvector = {pol1, pol2, pol3} + {0, 0, -180}; {pol1, pol2, pol3} = dummyvector; 1.]; 
                     allcreatedrays = {{rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, 
                        ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, 
                        sno3, sno4, rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}}; originalrotation = Partition[Take[originalray, 
                        {7, 15}], 3]; previoustilt = originalrotation[[1]]; transmatrix = MapIndexed[({rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, 
                           r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, 
                           surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, oldgenerationnumber, 
                           intersectionnumber, pol1, pol2, pol3} = #1; rotationaxis = Cross[previoustilt, {r1, r2, r3}]; axismagnitude = 
                          Sqrt[rotationaxis . rotationaxis]; If[Chop[axismagnitude] != 0., rotationaxis = rotationaxis/axismagnitude; axismatrix = 
                            (#1*rotationaxis & ) /@ rotationaxis; crossmatrix = {{0., -rotationaxis[[3]], rotationaxis[[2]]}, {rotationaxis[[3]], 0., 
                              -rotationaxis[[1]]}, {-rotationaxis[[2]], rotationaxis[[1]], 0.}}; cosangle = previoustilt . {r1, r2, r3}; 
                           If[1. - cosangle < 2.220446049250313*^-16, cosangle = 1.]; sinangle = 1 - cosangle^2; If[sinangle < 
                             2.220446049250313*^-16, sinangle = 0., sinangle = Sqrt[sinangle]]; axismatrix + ({{1., 0., 0.}, {0., 1., 0.}, 
                               {0., 0., 1.}} - axismatrix)*cosangle + crossmatrix*sinangle, {{r1, r2, r3}, {r4, r5, r6}, {r7, r8, r9}} = 
                           If[Sign[previoustilt . {r1, r2, r3}] === 1, {{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}, {{-1., 0., 0.}, {0., -1., 0.}, 
                             {0., 0., -1.}}]]) & , allcreatedrays]; allcreatedrays = MapIndexed[({rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, 
                           r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, 
                           surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, oldgenerationnumber, 
                           intersectionnumber, pol1, pol2, pol3} = #1; {{r1, r2, r3}, {r4, r5, r6}, {r7, r8, r9}} = Function[mm, (mm . #1 & ) /@ 
                             originalrotation][transmatrix[[First[#2]]]]; tiltmagnitude = {r1, r2, r3} . {r1, r2, r3}; If[Abs[tiltmagnitude - 1.] > 
                           1.*^-6, dummyvector = {r1, r2, r3}/Sqrt[tiltmagnitude]; {r1, r2, r3} = dummyvector; dummymatrix = (#1/Sqrt[#1 . #1] & ) /@ 
                             {{r1, r2, r3}, {r4, r5, r6}, {r7, r8, r9}}; {{r1, r2, r3}, {r4, r5, r6}, {r7, r8, r9}} = dummymatrix; 1., 1.]; 
                         If[allcreatedamplitudes != {{I*0., I*0., I*0.}}, amplitude = allcreatedamplitudes[[First[#2]]]; hor = {r4, r5, r6} . 
                             amplitude; ver = {r7, r8, r9} . amplitude; If[Abs[hor] > 0. || Abs[ver] > 0., {pol1, pol2, pol3} = {Abs[hor], Abs[ver], 
                               0.}; dummyvector = {pol1, pol2, pol3}/Sqrt[{pol1, pol2, pol3} . {pol1, pol2, pol3}]; {pol1, pol2, pol3} = dummyvector; 
                             dummyvector = ReplacePart[{pol1, pol2, pol3}, (If[ver == 0., 0., Arg[ver]] - If[hor == 0., 0., Arg[hor]])/Degree, 3]; 
                             {pol1, pol2, pol3} = dummyvector]; 1., 1.]; {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, 
                          wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, 
                          ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}) & , 
                       allcreatedrays]); {allcreatedrays, {{1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 1., 1., 
                       1., 1., 0., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}}, {completedray}}, resonatenumber == 2, 
                   {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, 
                      opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, 
                      rayslot, oldgenerationnumber, intersectionnumber, pol1, pol2, pol3} = #1; index1 = index; xunit = 
                     r1*Re[Cos[0.017453292519943295*θ2]] + r2*Re[Sin[0.017453292519943295*θ2]]; yunit = r2*Re[Cos[0.017453292519943295*θ2]] - 
                      1.*r1*Re[Sin[0.017453292519943295*θ2]]; zunit = r3*Re[Cos[0.017453292519943295*θ2]^2 + Sin[0.017453292519943295*θ2]^2]; 
                    xunit = Chop[xunit]; yunit = Chop[yunit]; zunit = Chop[zunit]; magnitude = (xunit^2 + yunit^2 + zunit^2)^2^(-1); 
                    xunit = xunit/magnitude; yunit = yunit/magnitude; zunit = zunit/magnitude; rx = Re[Cos[0.017453292519943295*θ2]]*
                       (rex - 1.*Re[xx2 + 10.*Cos[0.017453292519943295*θ2]]) + Re[Sin[0.017453292519943295*θ2]]*(rey - 
                        1.*Re[yy2 + 10.*Sin[0.017453292519943295*θ2]]); ry = -1.*(rex - 1.*Re[xx2 + 10.*Cos[0.017453292519943295*θ2]])*
                       Re[Sin[0.017453292519943295*θ2]] + Re[Cos[0.017453292519943295*θ2]]*(rey - 1.*Re[yy2 + 10.*Sin[0.017453292519943295*
                              θ2]]); rz = rez*Re[Cos[0.017453292519943295*θ2]^2 + Sin[0.017453292519943295*θ2]^2]; rx = Chop[rx]; 
                    ry = Chop[ry]; rz = Chop[rz]; terminatedeflection = False; intersections = {{-1., 0., 0.}}; intersections = 
                     resonateintersections; holeQ = holeQlist[[resonatenumber]] == 1; {newcomponentnumber, newsurfacenumber, newsurfaceid} = 
                     {1., 2., 122530.}; {componentnumber, surfacenumber, surfaceid} = {1., 2., 122530.}; {ray1, ray2, ray3} = intersections[[1]]; 
                    rsx = rex; rsy = rey; rsz = rez; rxhlp = rx + ray1*xunit; ryhlp = ry + ray1*yunit; rzhlp = rz + ray1*zunit; 
                    rex = rxhlp*Re[Cos[0.017453292519943295*θ2]] + Re[xx2 + 10.*Cos[0.017453292519943295*θ2]] - 1.*ryhlp*
                       Re[Sin[0.017453292519943295*θ2]]; rey = ryhlp*Re[Cos[0.017453292519943295*θ2]] + rxhlp*Re[Sin[0.017453292519943295*
                          θ2]] + Re[yy2 + 10.*Sin[0.017453292519943295*θ2]]; rez = rzhlp*Re[Cos[0.017453292519943295*θ2]^2 + 
                        Sin[0.017453292519943295*θ2]^2]; rex = Chop[rex]; rey = Chop[rey]; rez = Chop[rez]; intersectionnumber = 
                     intersectionnumber + 1.; opticallength = opticallength + ray1*index1; completedray = {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, 
                      r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, 
                      surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, 
                      pol1, pol2, pol3}; reportedrayspresent = True; intersectionspresent = True; deflectedrayspresent = True; 
                    localdummyoutrayspresent = True; (surfnorm = 1.*Re[{{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}]; dummymatrix = 
                      ({{Re[Cos[0.017453292519943295*θ2]], -1.*Re[Sin[0.017453292519943295*θ2]], 0.}, {Re[Sin[0.017453292519943295*θ2]], 
                           Re[Cos[0.017453292519943295*θ2]], 0.}, {0., 0., 1.}} . #1 & ) /@ surfnorm; surfnorm = (#1/Sqrt[#1 . #1] & ) /@ 
                       dummymatrix; surfacerotationmatrix = {{Re[Cos[0.017453292519943295*θ2]], Re[Sin[0.017453292519943295*θ2]], 0.}, 
                       {-1.*Re[Sin[0.017453292519943295*θ2]], Re[Cos[0.017453292519943295*θ2]], 0.}, {0., 0., 1.}}; surfacetranslationvector = 
                      {Re[xx2 + 10.*Cos[0.017453292519943295*θ2]], Re[yy2 + 10.*Sin[0.017453292519943295*θ2]], 0.}; activeregion = -1.; 
                     completedray = {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, 
                       index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, 
                       rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}; componentnumber = newcomponentnumber; surfacenumber = 
                      newsurfacenumber; surfaceid = newsurfaceid; originalray = {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, 
                       wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, 
                       ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}; 
                     ZAxis = First[surfnorm]*Sign[First[surfnorm] . {r1, r2, r3}]; raytiltz = ZAxis . {r1, r2, r3}; raytiltx = 1 - raytiltz^2; 
                     If[raytiltx < 2.220446049250313*^-16, raytiltx = 0., raytiltx = Sqrt[raytiltx]]; XAxis = {r1, r2, r3} - ZAxis*raytiltz; 
                     If[Abs[raytiltx] < 1.*^-7, XAxis = surfnorm[[2]], XAxis = XAxis/raytiltx]; YAxis = {-(ZAxis[[3]]*XAxis[[2]]) + 
                        ZAxis[[2]]*XAxis[[3]], ZAxis[[3]]*XAxis[[1]] - ZAxis[[1]]*XAxis[[3]], -(ZAxis[[2]]*XAxis[[1]]) + ZAxis[[1]]*XAxis[[2]]}; 
                     YAxis = YAxis/Sqrt[YAxis . YAxis]; PAxis = ZAxis*{r1, r2, r3} . XAxis - XAxis*{r1, r2, r3} . ZAxis; PAxis = 
                      PAxis/Sqrt[PAxis . PAxis]; normalizedpolarization = {pol1, pol2, pol3}[[{1, 2}]]; normalizedpolarization = 
                      normalizedpolarization/Sqrt[normalizedpolarization . normalizedpolarization]; allcreatedamplitudes = {{I*0., I*0., I*0.}}; 
                     amplitude = Sqrt[intensity]*(normalizedpolarization[[1]]*{r4, r5, r6} + normalizedpolarization[[2]]*{r7, r8, r9}*
                         Exp[I*pol3*0.017453292519943295]); perpendicularamplitude = amplitude . YAxis; parallelamplitude = amplitude . PAxis; 
                     If[activeregion*(surfacerotationmatrix . {r1, r2, r3})[[1]] > 0, index2 = Sqrt[2.2718929 - 0.010108077*wavelength^2 + 
                          0.010592509/wavelength^2 + 0.00020816965/wavelength^4 - 7.6472538*^-6/wavelength^6 + 4.9240991*^-7/wavelength^8]; 
                       index1 = index; If[Abs[index2 - index1] < 0.01, alternateindex = Sqrt[1.0005281111748805 - 2.364805676582992*^-10*
                             wavelength^2 + 2.9781670902982902*^-6/wavelength^2 + 2.6004099469477187*^-8/wavelength^4 + 3.7787839314048715*^-10/
                             wavelength^6 + 2.830549017940716*^-12/wavelength^8]; If[Abs[alternateindex - index1] > Abs[index2 - index1], 
                          index1 = index2; index2 = alternateindex, index1 = alternateindex], index1 = Sqrt[1.0005281111748805 - 
                           2.364805676582992*^-10*wavelength^2 + 2.9781670902982902*^-6/wavelength^2 + 2.6004099469477187*^-8/wavelength^4 + 
                           3.7787839314048715*^-10/wavelength^6 + 2.830549017940716*^-12/wavelength^8]], index2 = Sqrt[1.0005281111748805 - 
                          2.364805676582992*^-10*wavelength^2 + 2.9781670902982902*^-6/wavelength^2 + 2.6004099469477187*^-8/wavelength^4 + 
                          3.7787839314048715*^-10/wavelength^6 + 2.830549017940716*^-12/wavelength^8]; index1 = index; If[Abs[index2 - index1] < 
                         0.01, alternateindex = Sqrt[2.2718929 - 0.010108077*wavelength^2 + 0.010592509/wavelength^2 + 0.00020816965/wavelength^4 - 
                            7.6472538*^-6/wavelength^6 + 4.9240991*^-7/wavelength^8]; If[Abs[alternateindex - index1] > Abs[index2 - index1], 
                          index1 = index2; index2 = alternateindex, index1 = alternateindex], index1 = Sqrt[2.2718929 - 0.010108077*wavelength^2 + 
                           0.010592509/wavelength^2 + 0.00020816965/wavelength^4 - 7.6472538*^-6/wavelength^6 + 4.9240991*^-7/wavelength^8]]]; 
                     n12 = index1/index2; index = index2; If[1 - n12^2*raytiltx^2 >= 0., dummyvector = n12*{r1, r2, r3} + ZAxis*(-(n12*raytiltz) + 
                           Sqrt[1 - n12^2*raytiltx^2]); {r1, r2, r3} = dummyvector; 1., If[{r1, r2, r3} . surfnorm[[1]] > 0, phaseshift = 
                         phaseshift + 0.5, 1.]; index2 = index1; index = index1; dummyvector = {r1, r2, r3} - 2*ZAxis*ZAxis . {r1, r2, r3}; 
                       {r1, r2, r3} = dummyvector; dummyvector = {pol1, pol2, pol3} + {0, 0, -180}; {pol1, pol2, pol3} = dummyvector; 1.]; 
                     allcreatedrays = {{rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, 
                        ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, 
                        sno3, sno4, rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}}; originalrotation = Partition[Take[originalray, 
                        {7, 15}], 3]; previoustilt = originalrotation[[1]]; transmatrix = MapIndexed[({rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, 
                           r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, 
                           surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, oldgenerationnumber, 
                           intersectionnumber, pol1, pol2, pol3} = #1; rotationaxis = Cross[previoustilt, {r1, r2, r3}]; axismagnitude = 
                          Sqrt[rotationaxis . rotationaxis]; If[Chop[axismagnitude] != 0., rotationaxis = rotationaxis/axismagnitude; axismatrix = 
                            (#1*rotationaxis & ) /@ rotationaxis; crossmatrix = {{0., -rotationaxis[[3]], rotationaxis[[2]]}, {rotationaxis[[3]], 0., 
                              -rotationaxis[[1]]}, {-rotationaxis[[2]], rotationaxis[[1]], 0.}}; cosangle = previoustilt . {r1, r2, r3}; 
                           If[1. - cosangle < 2.220446049250313*^-16, cosangle = 1.]; sinangle = 1 - cosangle^2; If[sinangle < 
                             2.220446049250313*^-16, sinangle = 0., sinangle = Sqrt[sinangle]]; axismatrix + ({{1., 0., 0.}, {0., 1., 0.}, 
                               {0., 0., 1.}} - axismatrix)*cosangle + crossmatrix*sinangle, {{r1, r2, r3}, {r4, r5, r6}, {r7, r8, r9}} = 
                           If[Sign[previoustilt . {r1, r2, r3}] === 1, {{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}, {{-1., 0., 0.}, {0., -1., 0.}, 
                             {0., 0., -1.}}]]) & , allcreatedrays]; allcreatedrays = MapIndexed[({rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, 
                           r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, 
                           surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, oldgenerationnumber, 
                           intersectionnumber, pol1, pol2, pol3} = #1; {{r1, r2, r3}, {r4, r5, r6}, {r7, r8, r9}} = Function[mm, (mm . #1 & ) /@ 
                             originalrotation][transmatrix[[First[#2]]]]; tiltmagnitude = {r1, r2, r3} . {r1, r2, r3}; If[Abs[tiltmagnitude - 1.] > 
                           1.*^-6, dummyvector = {r1, r2, r3}/Sqrt[tiltmagnitude]; {r1, r2, r3} = dummyvector; dummymatrix = (#1/Sqrt[#1 . #1] & ) /@ 
                             {{r1, r2, r3}, {r4, r5, r6}, {r7, r8, r9}}; {{r1, r2, r3}, {r4, r5, r6}, {r7, r8, r9}} = dummymatrix; 1., 1.]; 
                         If[allcreatedamplitudes != {{I*0., I*0., I*0.}}, amplitude = allcreatedamplitudes[[First[#2]]]; hor = {r4, r5, r6} . 
                             amplitude; ver = {r7, r8, r9} . amplitude; If[Abs[hor] > 0. || Abs[ver] > 0., {pol1, pol2, pol3} = {Abs[hor], Abs[ver], 
                               0.}; dummyvector = {pol1, pol2, pol3}/Sqrt[{pol1, pol2, pol3} . {pol1, pol2, pol3}]; {pol1, pol2, pol3} = dummyvector; 
                             dummyvector = ReplacePart[{pol1, pol2, pol3}, (If[ver == 0., 0., Arg[ver]] - If[hor == 0., 0., Arg[hor]])/Degree, 3]; 
                             {pol1, pol2, pol3} = dummyvector]; 1., 1.]; {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, 
                          wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, 
                          ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}) & , 
                       allcreatedrays]); {allcreatedrays, {{1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 1., 1., 
                       1., 1., 0., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}}, {completedray}}, True, 
                   deflecteddummyrayspresent = True; reporteddummyrayspresent = True; localoutrayspresent = True; {{{1., 1., 1., 1., 1., 1., 1., 1., 
                       1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 1., 1., 1., 1., 0., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 
                       1., 1., 1.}}, {{rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, 
                       index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, 
                       rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}}, {{1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 
                       1., 1., -9.87654321*^8, 1., 1., 1., 1., 0., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}}}], 
                 deflecteddummyrayspresent = True; reporteddummyrayspresent = True; localdummyoutrayspresent = True; 
                  {{{1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 1., 1., 1., 1., 0., -1., -1., -1., 1., 1., 1., 
                     1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}}, {{1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 
                     1., 1., 1., 1., 0., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}}, {{1., 1., 1., 1., 1., 1., 1., 1., 
                     1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 1., 1., 1., 1., 0., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 
                     1., 1., 1.}}}]) & ) /@ inrays]; If[deflectedrayspresent && deflecteddummyrayspresent, 
           deflectedlocalrays = Select[deflectedlocalrays, #1[[23]] > 0. & ]*1., 1.]; If[localoutrayspresent, 
           If[localdummyoutrayspresent, localoutrays = Select[localoutrays, #1[[23]] > 0. & ]; 1., 1.]; outrays = Join[outrays, localoutrays]; 1., 
           1.]; If[reportedrayspresent && reporteddummyrayspresent, reportedlocalrays = Select[reportedlocalrays, #1[[23]] > 0. & ]; 1., 1.]); 
         If[reportedrayspresent, raysreported = True; reportedrays = Join[reportedrays, {{-9.87654321*^8, -9.87654321*^8, -9.87654321*^8, 
               -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, 
               -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, 
               -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, 
               -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, 
               -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8}}, reportedlocalrays]; 1., 1.]; inrays = deflectedlocalrays; 
         propagate = generationnumber < generationlimit]; propagatedrays = If[Length[outrays] > 1, Drop[outrays, 1], outrays]; 
       If[generationnumber >= generationlimit && deflectedrayspresent, propagatedrays = Join[propagatedrays, deflectedlocalrays]; 1., 1.]); 
      (inrays = propagatedrays; outrays = {{1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 1., 1., 1., 1., 0., -1., 
          -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}}; deflectedrayspresent = True; reportedrayspresent = False; 
       propagate = True; While[deflectedrayspresent && propagate, generationnumber += 1.; (deflecteddummyrayspresent = False; 
          deflectedrayspresent = False; reportedrayspresent = False; reporteddummyrayspresent = True; localdummyoutrayspresent = False; 
          localoutrayspresent = False; {deflectedlocalrays, localoutrays, reportedlocalrays} = (Flatten[#1, 1] & ) /@ 
            Transpose[(({rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, 
                  opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, 
                  oldgenerationnumber, intersectionnumber, pol1, pol2, pol3} = #1; If[intensity > thresholdintensity && generationnumber < 
                   generationlimit, (resonatenumber = 0; resonateraylength = 9.87654321*^8; resonateintersections = {{-1., 0., 0.}}; 
                   MapIndexed[If[#1[[1,1]] > -1. && #1[[1,1]] < resonateraylength, resonateraylength = #1[[1,1]]; resonateintersections = #1; 
                       resonatenumber = First[#2], resonatenumber] & , intersectionslist = {{{-1., 0., 0.}}}; holeQlist = {0}; 
                     ({rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, 
                        opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, 
                        rayslot, oldgenerationnumber, intersectionnumber, pol1, pol2, pol3} = #1; index1 = index; xunit = -1.*r1; yunit = -1.*r2; 
                      zunit = r3; xunit = Chop[xunit]; yunit = Chop[yunit]; zunit = Chop[zunit]; magnitude = (xunit^2 + yunit^2 + zunit^2)^2^(-1); 
                      xunit = xunit/magnitude; yunit = yunit/magnitude; zunit = zunit/magnitude; rx = -1.*(-100. + rex); ry = -1.*rey; rz = rez; 
                      rx = Chop[rx]; ry = Chop[ry]; rz = Chop[rz]; terminatedeflection = False; intersections = {{-1., 0., 0.}}; intersections = 
                       Block[{Compile`$80}, If[Chop[xunit] != 0, {(If[N[Chop[#1[[1]]]] >= 0. && (#1[[2]] <= 25. && #1[[2]] >= -25. && #1[[3]] >= 
                                -25. && #1[[3]] <= 25.), {#1[[1]], #1[[2]], #1[[3]]}, {-1., 0., 0.}] & )[Compile`$80 = xunit^(-1); 
                            {-(rx*Compile`$80), ry - (rx*yunit)*Compile`$80, rz - (rx*zunit)*Compile`$80}]}, {{-1., 0., 0.}}]]; 
                      AppendTo[intersections, {-1., 0., 0.}]; holeQ = False; If[ !(Chop[surfaceid - 122646.] == 0. || N[Chop[ray1]] < 0.0001) || 
                        generationnumber == 1., minresonateraylength = 0., minresonateraylength = 0.0001]; Block[{LocRayL, LocSCx, LocSCy}, 
                       LocRayL = 1.; LocSCx = 1.; LocSCy = 1.; dummymatrix = Union[Select[intersections, ({LocRayL, LocSCx, LocSCy} = #1; 
                             {LocRayL, LocSCx, LocSCy} == {-1., 0., 0.} || N[Chop[LocRayL]] >= minresonateraylength) & ]]; intersections = 
                         dummymatrix]; unfilteredintersections = intersections; If[Length[intersections] > 1, dummymatrix = Take[intersections, {2}]; 
                        intersections = dummymatrix; 1., 1.]; AppendTo[intersectionslist, intersections]; If[holeQ, AppendTo[holeQlist, 1], 
                       AppendTo[holeQlist, 0]]; {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, 
                        intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, 
                        sno1, sno2, sno3, sno4, rayslot, oldgenerationnumber, intersectionnumber, pol1, pol2, pol3} = #1; index1 = index; xunit = r2; 
                      yunit = -1.*r1; zunit = r3; xunit = Chop[xunit]; yunit = Chop[yunit]; zunit = Chop[zunit]; magnitude = (xunit^2 + yunit^2 + 
                         zunit^2)^2^(-1); xunit = xunit/magnitude; yunit = yunit/magnitude; zunit = zunit/magnitude; rx = 25. + rey; 
                      ry = -1.*(-50. + rex); rz = rez; rx = Chop[rx]; ry = Chop[ry]; rz = Chop[rz]; terminatedeflection = False; intersections = 
                       {{-1., 0., 0.}}; intersections = Block[{Compile`$82}, If[Chop[xunit] != 0, {(If[N[Chop[#1[[1]]]] >= 0. && (#1[[2]] <= 50. && 
                               #1[[2]] >= -50. && #1[[3]] >= -25. && #1[[3]] <= 25.), {#1[[1]], #1[[2]], #1[[3]]}, {-1., 0., 0.}] & )[
                           Compile`$82 = xunit^(-1); {-(rx*Compile`$82), ry - (rx*yunit)*Compile`$82, rz - (rx*zunit)*Compile`$82}]}, 
                         {{-1., 0., 0.}}]]; AppendTo[intersections, {-1., 0., 0.}]; holeQ = False; If[ !(Chop[surfaceid - 122647.] == 0. || 
                          N[Chop[ray1]] < 0.0001) || generationnumber == 1., minresonateraylength = 0., minresonateraylength = 0.0001]; 
                      Block[{LocRayL, LocSCx, LocSCy}, LocRayL = 1.; LocSCx = 1.; LocSCy = 1.; dummymatrix = Union[Select[intersections, 
                           ({LocRayL, LocSCx, LocSCy} = #1; {LocRayL, LocSCx, LocSCy} == {-1., 0., 0.} || N[Chop[LocRayL]] >= 
                               minresonateraylength) & ]]; intersections = dummymatrix]; unfilteredintersections = intersections; 
                      If[Length[intersections] > 1, dummymatrix = Take[intersections, {2}]; intersections = dummymatrix; 1., 1.]; 
                      AppendTo[intersectionslist, intersections]; If[holeQ, AppendTo[holeQlist, 1], AppendTo[holeQlist, 0]]; 
                      {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, 
                        opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, 
                        rayslot, oldgenerationnumber, intersectionnumber, pol1, pol2, pol3} = #1; index1 = index; xunit = r1; yunit = r2; zunit = r3; 
                      xunit = Chop[xunit]; yunit = Chop[yunit]; zunit = Chop[zunit]; magnitude = (xunit^2 + yunit^2 + zunit^2)^2^(-1); 
                      xunit = xunit/magnitude; yunit = yunit/magnitude; zunit = zunit/magnitude; rx = rex; ry = rey; rz = rez; rx = Chop[rx]; 
                      ry = Chop[ry]; rz = Chop[rz]; terminatedeflection = False; intersections = {{-1., 0., 0.}}; intersections = 
                       Block[{Compile`$80}, If[Chop[xunit] != 0, {(If[N[Chop[#1[[1]]]] >= 0. && (#1[[2]] <= 25. && #1[[2]] >= -25. && #1[[3]] >= 
                                -25. && #1[[3]] <= 25.), {#1[[1]], #1[[2]], #1[[3]]}, {-1., 0., 0.}] & )[Compile`$80 = xunit^(-1); 
                            {-(rx*Compile`$80), ry - (rx*yunit)*Compile`$80, rz - (rx*zunit)*Compile`$80}]}, {{-1., 0., 0.}}]]; 
                      AppendTo[intersections, {-1., 0., 0.}]; holeQ = False; If[ !(Chop[surfaceid - 122648.] == 0. || N[Chop[ray1]] < 0.0001) || 
                        generationnumber == 1., minresonateraylength = 0., minresonateraylength = 0.0001]; Block[{LocRayL, LocSCx, LocSCy}, 
                       LocRayL = 1.; LocSCx = 1.; LocSCy = 1.; dummymatrix = Union[Select[intersections, ({LocRayL, LocSCx, LocSCy} = #1; 
                             {LocRayL, LocSCx, LocSCy} == {-1., 0., 0.} || N[Chop[LocRayL]] >= minresonateraylength) & ]]; intersections = 
                         dummymatrix]; unfilteredintersections = intersections; If[Length[intersections] > 1, dummymatrix = Take[intersections, {2}]; 
                        intersections = dummymatrix; 1., 1.]; AppendTo[intersectionslist, intersections]; If[holeQ, AppendTo[holeQlist, 1], 
                       AppendTo[holeQlist, 0]]; {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, 
                        intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, 
                        sno1, sno2, sno3, sno4, rayslot, oldgenerationnumber, intersectionnumber, pol1, pol2, pol3} = #1; index1 = index; 
                      xunit = -1.*r2; yunit = r1; zunit = r3; xunit = Chop[xunit]; yunit = Chop[yunit]; zunit = Chop[zunit]; magnitude = 
                       (xunit^2 + yunit^2 + zunit^2)^2^(-1); xunit = xunit/magnitude; yunit = yunit/magnitude; zunit = zunit/magnitude; 
                      rx = -1.*(-25. + rey); ry = -50. + rex; rz = rez; rx = Chop[rx]; ry = Chop[ry]; rz = Chop[rz]; terminatedeflection = False; 
                      intersections = {{-1., 0., 0.}}; intersections = Block[{Compile`$82}, If[Chop[xunit] != 0, {(If[N[Chop[#1[[1]]]] >= 0. && 
                              (#1[[2]] <= 50. && #1[[2]] >= -50. && #1[[3]] >= -25. && #1[[3]] <= 25.), {#1[[1]], #1[[2]], #1[[3]]}, {-1., 0., 
                              0.}] & )[Compile`$82 = xunit^(-1); {-(rx*Compile`$82), ry - (rx*yunit)*Compile`$82, rz - (rx*zunit)*Compile`$82}]}, 
                         {{-1., 0., 0.}}]]; AppendTo[intersections, {-1., 0., 0.}]; holeQ = False; If[ !(Chop[surfaceid - 122649.] == 0. || 
                          N[Chop[ray1]] < 0.0001) || generationnumber == 1., minresonateraylength = 0., minresonateraylength = 0.0001]; 
                      Block[{LocRayL, LocSCx, LocSCy}, LocRayL = 1.; LocSCx = 1.; LocSCy = 1.; dummymatrix = Union[Select[intersections, 
                           ({LocRayL, LocSCx, LocSCy} = #1; {LocRayL, LocSCx, LocSCy} == {-1., 0., 0.} || N[Chop[LocRayL]] >= 
                               minresonateraylength) & ]]; intersections = dummymatrix]; unfilteredintersections = intersections; 
                      If[Length[intersections] > 1, dummymatrix = Take[intersections, {2}]; intersections = dummymatrix; 1., 1.]; 
                      AppendTo[intersectionslist, intersections]; If[holeQ, AppendTo[holeQlist, 1], AppendTo[holeQlist, 0]]; 
                      {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, 
                        opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, 
                        rayslot, oldgenerationnumber, intersectionnumber, pol1, pol2, pol3} = #1; index1 = index; xunit = r3; yunit = r2; 
                      zunit = -1.*r1; xunit = Chop[xunit]; yunit = Chop[yunit]; zunit = Chop[zunit]; magnitude = (xunit^2 + yunit^2 + zunit^2)^
                        2^(-1); xunit = xunit/magnitude; yunit = yunit/magnitude; zunit = zunit/magnitude; rx = 25. + rez; ry = rey; 
                      rz = -1.*(-50. + rex); rx = Chop[rx]; ry = Chop[ry]; rz = Chop[rz]; terminatedeflection = False; intersections = 
                       {{-1., 0., 0.}}; intersections = Block[{Compile`$81}, If[Chop[xunit] != 0, {(If[N[Chop[#1[[1]]]] >= 0. && (#1[[2]] <= 25. && 
                               #1[[2]] >= -25. && #1[[3]] >= -50. && #1[[3]] <= 50.), {#1[[1]], #1[[2]], #1[[3]]}, {-1., 0., 0.}] & )[
                           Compile`$81 = xunit^(-1); {-(rx*Compile`$81), ry - (rx*yunit)*Compile`$81, rz - (rx*zunit)*Compile`$81}]}, 
                         {{-1., 0., 0.}}]]; AppendTo[intersections, {-1., 0., 0.}]; holeQ = False; If[ !(Chop[surfaceid - 122650.] == 0. || 
                          N[Chop[ray1]] < 0.0001) || generationnumber == 1., minresonateraylength = 0., minresonateraylength = 0.0001]; 
                      Block[{LocRayL, LocSCx, LocSCy}, LocRayL = 1.; LocSCx = 1.; LocSCy = 1.; dummymatrix = Union[Select[intersections, 
                           ({LocRayL, LocSCx, LocSCy} = #1; {LocRayL, LocSCx, LocSCy} == {-1., 0., 0.} || N[Chop[LocRayL]] >= 
                               minresonateraylength) & ]]; intersections = dummymatrix]; unfilteredintersections = intersections; 
                      If[Length[intersections] > 1, dummymatrix = Take[intersections, {2}]; intersections = dummymatrix; 1., 1.]; 
                      AppendTo[intersectionslist, intersections]; If[holeQ, AppendTo[holeQlist, 1], AppendTo[holeQlist, 0]]; 
                      {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, 
                        opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, 
                        rayslot, oldgenerationnumber, intersectionnumber, pol1, pol2, pol3} = #1; index1 = index; xunit = -1.*r3; yunit = -1.*r2; 
                      zunit = -1.*r1; xunit = Chop[xunit]; yunit = Chop[yunit]; zunit = Chop[zunit]; magnitude = (xunit^2 + yunit^2 + zunit^2)^
                        2^(-1); xunit = xunit/magnitude; yunit = yunit/magnitude; zunit = zunit/magnitude; rx = -1.*(-25. + rez); ry = -1.*rey; 
                      rz = -1.*(-50. + rex); rx = Chop[rx]; ry = Chop[ry]; rz = Chop[rz]; terminatedeflection = False; intersections = 
                       {{-1., 0., 0.}}; intersections = Block[{Compile`$81}, If[Chop[xunit] != 0, {(If[N[Chop[#1[[1]]]] >= 0. && (#1[[2]] <= 25. && 
                               #1[[2]] >= -25. && #1[[3]] >= -50. && #1[[3]] <= 50.), {#1[[1]], #1[[2]], #1[[3]]}, {-1., 0., 0.}] & )[
                           Compile`$81 = xunit^(-1); {-(rx*Compile`$81), ry - (rx*yunit)*Compile`$81, rz - (rx*zunit)*Compile`$81}]}, 
                         {{-1., 0., 0.}}]]; AppendTo[intersections, {-1., 0., 0.}]; holeQ = False; If[ !(Chop[surfaceid - 122651.] == 0. || 
                          N[Chop[ray1]] < 0.0001) || generationnumber == 1., minresonateraylength = 0., minresonateraylength = 0.0001]; 
                      Block[{LocRayL, LocSCx, LocSCy}, LocRayL = 1.; LocSCx = 1.; LocSCy = 1.; dummymatrix = Union[Select[intersections, 
                           ({LocRayL, LocSCx, LocSCy} = #1; {LocRayL, LocSCx, LocSCy} == {-1., 0., 0.} || N[Chop[LocRayL]] >= 
                               minresonateraylength) & ]]; intersections = dummymatrix]; unfilteredintersections = intersections; 
                      If[Length[intersections] > 1, dummymatrix = Take[intersections, {2}]; intersections = dummymatrix; 1., 1.]; 
                      AppendTo[intersectionslist, intersections]; If[holeQ, AppendTo[holeQlist, 1], AppendTo[holeQlist, 0]]); holeQlist = 
                      Drop[holeQlist, 1]; Drop[intersectionslist, 1]]); Which[resonatenumber == 1, {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, 
                      r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, 
                      surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, oldgenerationnumber, intersectionnumber, pol1, pol2, 
                      pol3} = #1; index1 = index; xunit = -1.*r1; yunit = -1.*r2; zunit = r3; xunit = Chop[xunit]; yunit = Chop[yunit]; 
                    zunit = Chop[zunit]; magnitude = (xunit^2 + yunit^2 + zunit^2)^2^(-1); xunit = xunit/magnitude; yunit = yunit/magnitude; 
                    zunit = zunit/magnitude; rx = -1.*(-100. + rex); ry = -1.*rey; rz = rez; rx = Chop[rx]; ry = Chop[ry]; rz = Chop[rz]; 
                    terminatedeflection = False; intersections = {{-1., 0., 0.}}; intersections = resonateintersections; 
                    holeQ = holeQlist[[resonatenumber]] == 1; {newcomponentnumber, newsurfacenumber, newsurfaceid} = {2., 1., 122646.}; 
                    {componentnumber, surfacenumber, surfaceid} = {2., 1., 122646.}; {ray1, ray2, ray3} = intersections[[1]]; rsx = rex; rsy = rey; 
                    rsz = rez; rxhlp = rx + ray1*xunit; ryhlp = ry + ray1*yunit; rzhlp = rz + ray1*zunit; rex = 100. - 1.*rxhlp; rey = -1.*ryhlp; 
                    rez = rzhlp; rex = Chop[rex]; rey = Chop[rey]; rez = Chop[rez]; intersectionnumber = intersectionnumber + 1.; 
                    opticallength = opticallength + ray1*index1; completedray = {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, 
                      wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, 
                      ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}; 
                    reportedrayspresent = True; intersectionspresent = True; deflectedrayspresent = True; localdummyoutrayspresent = True; 
                    (surfnorm = 1.*Re[{{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}]; dummymatrix = ({{-1., 0., 0.}, {0., -1., 0.}, {0., 0., 1.}} . 
                         #1 & ) /@ surfnorm; surfnorm = (#1/Sqrt[#1 . #1] & ) /@ dummymatrix; surfacerotationmatrix = {{-1., 0., 0.}, {0., -1., 0.}, 
                       {0., 0., 1.}}; surfacetranslationvector = {100., 0, 0}; activeregion = 0.; completedray = {rsx, rsy, rsz, rex, rey, rez, r1, 
                       r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, 
                       componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, 
                       intersectionnumber, pol1, pol2, pol3}; componentnumber = newcomponentnumber; surfacenumber = newsurfacenumber; 
                     surfaceid = newsurfaceid; originalray = {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, 
                       intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, 
                       frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}; intensity = 0.; 
                     allcreatedrays = {{rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, 
                        ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, 
                        sno3, sno4, rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}}); {allcreatedrays, {{1., 1., 1., 1., 1., 1., 
                       1., 1., 1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 1., 1., 1., 1., 0., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 
                       1., 1., 1., 1., 1.}}, {completedray}}, resonatenumber == 2, {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, 
                      wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, 
                      ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, oldgenerationnumber, intersectionnumber, pol1, pol2, pol3} = #1; 
                    index1 = index; xunit = r2; yunit = -1.*r1; zunit = r3; xunit = Chop[xunit]; yunit = Chop[yunit]; zunit = Chop[zunit]; 
                    magnitude = (xunit^2 + yunit^2 + zunit^2)^2^(-1); xunit = xunit/magnitude; yunit = yunit/magnitude; zunit = zunit/magnitude; 
                    rx = 25. + rey; ry = -1.*(-50. + rex); rz = rez; rx = Chop[rx]; ry = Chop[ry]; rz = Chop[rz]; terminatedeflection = False; 
                    intersections = {{-1., 0., 0.}}; intersections = resonateintersections; holeQ = holeQlist[[resonatenumber]] == 1; 
                    {newcomponentnumber, newsurfacenumber, newsurfaceid} = {2., 2., 122647.}; {componentnumber, surfacenumber, surfaceid} = 
                     {2., 2., 122647.}; {ray1, ray2, ray3} = intersections[[1]]; rsx = rex; rsy = rey; rsz = rez; rxhlp = rx + ray1*xunit; 
                    ryhlp = ry + ray1*yunit; rzhlp = rz + ray1*zunit; rex = 50. - 1.*ryhlp; rey = -25. + rxhlp; rez = rzhlp; rex = Chop[rex]; 
                    rey = Chop[rey]; rez = Chop[rez]; intersectionnumber = intersectionnumber + 1.; opticallength = opticallength + ray1*index1; 
                    completedray = {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, 
                      index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, 
                      rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}; reportedrayspresent = True; intersectionspresent = True; 
                    deflectedrayspresent = True; localdummyoutrayspresent = True; (surfnorm = 1.*Re[{{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}]; 
                     dummymatrix = ({{0., -1., 0.}, {1., 0., 0.}, {0., 0., 1.}} . #1 & ) /@ surfnorm; surfnorm = (#1/Sqrt[#1 . #1] & ) /@ 
                       dummymatrix; surfacerotationmatrix = {{0., 1., 0.}, {-1., 0., 0.}, {0., 0., 1.}}; surfacetranslationvector = {50., -25., 0}; 
                     activeregion = 0.; completedray = {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, 
                       intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, 
                       sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}; componentnumber = 
                      newcomponentnumber; surfacenumber = newsurfacenumber; surfaceid = newsurfaceid; originalray = {rsx, rsy, rsz, rex, rey, rez, 
                       r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, 
                       componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, 
                       intersectionnumber, pol1, pol2, pol3}; intensity = 0.; allcreatedrays = {{rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, 
                        r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, 
                        surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, 
                        pol1, pol2, pol3}}); {allcreatedrays, {{1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 1., 
                       1., 1., 1., 0., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}}, {completedray}}, resonatenumber == 3, 
                   {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, 
                      opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, 
                      rayslot, oldgenerationnumber, intersectionnumber, pol1, pol2, pol3} = #1; index1 = index; xunit = r1; yunit = r2; zunit = r3; 
                    xunit = Chop[xunit]; yunit = Chop[yunit]; zunit = Chop[zunit]; magnitude = (xunit^2 + yunit^2 + zunit^2)^2^(-1); 
                    xunit = xunit/magnitude; yunit = yunit/magnitude; zunit = zunit/magnitude; rx = rex; ry = rey; rz = rez; rx = Chop[rx]; 
                    ry = Chop[ry]; rz = Chop[rz]; terminatedeflection = False; intersections = {{-1., 0., 0.}}; intersections = 
                     resonateintersections; holeQ = holeQlist[[resonatenumber]] == 1; {newcomponentnumber, newsurfacenumber, newsurfaceid} = 
                     {2., 3., 122648.}; {componentnumber, surfacenumber, surfaceid} = {2., 3., 122648.}; {ray1, ray2, ray3} = intersections[[1]]; 
                    rsx = rex; rsy = rey; rsz = rez; rxhlp = rx + ray1*xunit; ryhlp = ry + ray1*yunit; rzhlp = rz + ray1*zunit; rex = rxhlp; 
                    rey = ryhlp; rez = rzhlp; rex = Chop[rex]; rey = Chop[rey]; rez = Chop[rez]; intersectionnumber = intersectionnumber + 1.; 
                    opticallength = opticallength + ray1*index1; completedray = {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, 
                      wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, 
                      ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}; 
                    reportedrayspresent = True; intersectionspresent = True; deflectedrayspresent = True; localdummyoutrayspresent = True; 
                    (surfnorm = 1.*Re[{{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}]; dummymatrix = (#1 & ) /@ surfnorm; surfnorm = 
                      (#1/Sqrt[#1 . #1] & ) /@ dummymatrix; surfacerotationmatrix = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}; surfacetranslationvector = 
                      {0, 0, 0}; activeregion = 0.; completedray = {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, 
                       intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, 
                       frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}; componentnumber = 
                      newcomponentnumber; surfacenumber = newsurfacenumber; surfaceid = newsurfaceid; originalray = {rsx, rsy, rsz, rex, rey, rez, 
                       r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, 
                       componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, 
                       intersectionnumber, pol1, pol2, pol3}; intensity = 0.; allcreatedrays = {{rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, 
                        r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, 
                        surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, 
                        pol1, pol2, pol3}}); {allcreatedrays, {{1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 1., 
                       1., 1., 1., 0., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}}, {completedray}}, resonatenumber == 4, 
                   {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, 
                      opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, 
                      rayslot, oldgenerationnumber, intersectionnumber, pol1, pol2, pol3} = #1; index1 = index; xunit = -1.*r2; yunit = r1; 
                    zunit = r3; xunit = Chop[xunit]; yunit = Chop[yunit]; zunit = Chop[zunit]; magnitude = (xunit^2 + yunit^2 + zunit^2)^2^(-1); 
                    xunit = xunit/magnitude; yunit = yunit/magnitude; zunit = zunit/magnitude; rx = -1.*(-25. + rey); ry = -50. + rex; rz = rez; 
                    rx = Chop[rx]; ry = Chop[ry]; rz = Chop[rz]; terminatedeflection = False; intersections = {{-1., 0., 0.}}; intersections = 
                     resonateintersections; holeQ = holeQlist[[resonatenumber]] == 1; {newcomponentnumber, newsurfacenumber, newsurfaceid} = 
                     {2., 4., 122649.}; {componentnumber, surfacenumber, surfaceid} = {2., 4., 122649.}; {ray1, ray2, ray3} = intersections[[1]]; 
                    rsx = rex; rsy = rey; rsz = rez; rxhlp = rx + ray1*xunit; ryhlp = ry + ray1*yunit; rzhlp = rz + ray1*zunit; rex = 50. + ryhlp; 
                    rey = 25. - 1.*rxhlp; rez = rzhlp; rex = Chop[rex]; rey = Chop[rey]; rez = Chop[rez]; intersectionnumber = intersectionnumber + 
                      1.; opticallength = opticallength + ray1*index1; completedray = {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, 
                      r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, 
                      ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}; 
                    reportedrayspresent = True; intersectionspresent = True; deflectedrayspresent = True; localdummyoutrayspresent = True; 
                    (surfnorm = 1.*Re[{{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}]; dummymatrix = ({{0., 1., 0.}, {-1., 0., 0.}, {0., 0., 1.}} . 
                         #1 & ) /@ surfnorm; surfnorm = (#1/Sqrt[#1 . #1] & ) /@ dummymatrix; surfacerotationmatrix = {{0., -1., 0.}, {1., 0., 0.}, 
                       {0., 0., 1.}}; surfacetranslationvector = {50., 25., 0}; activeregion = 0.; completedray = {rsx, rsy, rsz, rex, rey, rez, r1, 
                       r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, 
                       componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, 
                       intersectionnumber, pol1, pol2, pol3}; componentnumber = newcomponentnumber; surfacenumber = newsurfacenumber; 
                     surfaceid = newsurfaceid; originalray = {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, 
                       intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, 
                       frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}; intensity = 0.; 
                     allcreatedrays = {{rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, 
                        ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, 
                        sno3, sno4, rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}}); {allcreatedrays, {{1., 1., 1., 1., 1., 1., 
                       1., 1., 1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 1., 1., 1., 1., 0., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 
                       1., 1., 1., 1., 1.}}, {completedray}}, resonatenumber == 5, {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, 
                      wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, 
                      ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, oldgenerationnumber, intersectionnumber, pol1, pol2, pol3} = #1; 
                    index1 = index; xunit = r3; yunit = r2; zunit = -1.*r1; xunit = Chop[xunit]; yunit = Chop[yunit]; zunit = Chop[zunit]; 
                    magnitude = (xunit^2 + yunit^2 + zunit^2)^2^(-1); xunit = xunit/magnitude; yunit = yunit/magnitude; zunit = zunit/magnitude; 
                    rx = 25. + rez; ry = rey; rz = -1.*(-50. + rex); rx = Chop[rx]; ry = Chop[ry]; rz = Chop[rz]; terminatedeflection = False; 
                    intersections = {{-1., 0., 0.}}; intersections = resonateintersections; holeQ = holeQlist[[resonatenumber]] == 1; 
                    {newcomponentnumber, newsurfacenumber, newsurfaceid} = {2., 5., 122650.}; {componentnumber, surfacenumber, surfaceid} = 
                     {2., 5., 122650.}; {ray1, ray2, ray3} = intersections[[1]]; rsx = rex; rsy = rey; rsz = rez; rxhlp = rx + ray1*xunit; 
                    ryhlp = ry + ray1*yunit; rzhlp = rz + ray1*zunit; rex = 50. - 1.*rzhlp; rey = ryhlp; rez = -25. + rxhlp; rex = Chop[rex]; 
                    rey = Chop[rey]; rez = Chop[rez]; intersectionnumber = intersectionnumber + 1.; opticallength = opticallength + ray1*index1; 
                    completedray = {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, 
                      index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, 
                      rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}; reportedrayspresent = True; intersectionspresent = True; 
                    deflectedrayspresent = True; localdummyoutrayspresent = True; (surfnorm = 1.*Re[{{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}]; 
                     dummymatrix = ({{0., 0., -1.}, {0., 1., 0.}, {1., 0., 0.}} . #1 & ) /@ surfnorm; surfnorm = (#1/Sqrt[#1 . #1] & ) /@ 
                       dummymatrix; surfacerotationmatrix = {{0., 0., 1.}, {0., 1., 0.}, {-1., 0., 0.}}; surfacetranslationvector = {50., 0, -25.}; 
                     activeregion = 0.; completedray = {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, 
                       intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, 
                       sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}; componentnumber = 
                      newcomponentnumber; surfacenumber = newsurfacenumber; surfaceid = newsurfaceid; originalray = {rsx, rsy, rsz, rex, rey, rez, 
                       r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, 
                       componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, 
                       intersectionnumber, pol1, pol2, pol3}; intensity = 0.; allcreatedrays = {{rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, 
                        r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, 
                        surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, 
                        pol1, pol2, pol3}}); {allcreatedrays, {{1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 1., 
                       1., 1., 1., 0., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}}, {completedray}}, resonatenumber == 6, 
                   {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, 
                      opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, 
                      rayslot, oldgenerationnumber, intersectionnumber, pol1, pol2, pol3} = #1; index1 = index; xunit = -1.*r3; yunit = -1.*r2; 
                    zunit = -1.*r1; xunit = Chop[xunit]; yunit = Chop[yunit]; zunit = Chop[zunit]; magnitude = (xunit^2 + yunit^2 + zunit^2)^2^(-1); 
                    xunit = xunit/magnitude; yunit = yunit/magnitude; zunit = zunit/magnitude; rx = -1.*(-25. + rez); ry = -1.*rey; 
                    rz = -1.*(-50. + rex); rx = Chop[rx]; ry = Chop[ry]; rz = Chop[rz]; terminatedeflection = False; intersections = {{-1., 0., 0.}}; 
                    intersections = resonateintersections; holeQ = holeQlist[[resonatenumber]] == 1; {newcomponentnumber, newsurfacenumber, 
                      newsurfaceid} = {2., 6., 122651.}; {componentnumber, surfacenumber, surfaceid} = {2., 6., 122651.}; {ray1, ray2, ray3} = 
                     intersections[[1]]; rsx = rex; rsy = rey; rsz = rez; rxhlp = rx + ray1*xunit; ryhlp = ry + ray1*yunit; rzhlp = rz + ray1*zunit; 
                    rex = 50. - 1.*rzhlp; rey = -1.*ryhlp; rez = 25. - 1.*rxhlp; rex = Chop[rex]; rey = Chop[rey]; rez = Chop[rez]; 
                    intersectionnumber = intersectionnumber + 1.; opticallength = opticallength + ray1*index1; completedray = {rsx, rsy, rsz, rex, 
                      rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, 
                      componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, 
                      intersectionnumber, pol1, pol2, pol3}; reportedrayspresent = True; intersectionspresent = True; deflectedrayspresent = True; 
                    localdummyoutrayspresent = True; (surfnorm = 1.*Re[{{1., 0., 0.}, {0., 1., 0.}, {0., 0., 1.}}]; dummymatrix = 
                      ({{0., 0., -1.}, {0., -1., 0.}, {-1., 0., 0.}} . #1 & ) /@ surfnorm; surfnorm = (#1/Sqrt[#1 . #1] & ) /@ dummymatrix; 
                     surfacerotationmatrix = {{0., 0., -1.}, {0., -1., 0.}, {-1., 0., 0.}}; surfacetranslationvector = {50., 0, 25.}; 
                     activeregion = 0.; completedray = {rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, 
                       intensityscale, ray1, index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, 
                       sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}; componentnumber = 
                      newcomponentnumber; surfacenumber = newsurfacenumber; surfaceid = newsurfaceid; originalray = {rsx, rsy, rsz, rex, rey, rez, 
                       r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, 
                       componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, 
                       intersectionnumber, pol1, pol2, pol3}; intensity = 0.; allcreatedrays = {{rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, 
                        r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, index, opticallength, phaseshift, componentnumber, 
                        surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, rayslot, generationnumber, intersectionnumber, 
                        pol1, pol2, pol3}}); {allcreatedrays, {{1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 1., 
                       1., 1., 1., 0., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}}, {completedray}}, True, 
                   deflecteddummyrayspresent = True; reporteddummyrayspresent = True; localoutrayspresent = True; {{{1., 1., 1., 1., 1., 1., 1., 1., 
                       1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 1., 1., 1., 1., 0., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 
                       1., 1., 1.}}, {{rsx, rsy, rsz, rex, rey, rez, r1, r2, r3, r4, r5, r6, r7, r8, r9, wavelength, intensity, intensityscale, ray1, 
                       index, opticallength, phaseshift, componentnumber, surfacenumber, surfaceid, ray2, ray3, sid, frontid, sno1, sno2, sno3, sno4, 
                       rayslot, generationnumber, intersectionnumber, pol1, pol2, pol3}}, {{1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 
                       1., 1., -9.87654321*^8, 1., 1., 1., 1., 0., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}}}], 
                 deflecteddummyrayspresent = True; reporteddummyrayspresent = True; localdummyoutrayspresent = True; 
                  {{{1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 1., 1., 1., 1., 0., -1., -1., -1., 1., 1., 1., 
                     1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}}, {{1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 
                     1., 1., 1., 1., 0., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}}, {{1., 1., 1., 1., 1., 1., 1., 1., 
                     1., 1., 1., 1., 1., 1., 1., 1., -9.87654321*^8, 1., 1., 1., 1., 0., -1., -1., -1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 
                     1., 1., 1.}}}]) & ) /@ inrays]; If[deflectedrayspresent && deflecteddummyrayspresent, 
           deflectedlocalrays = Select[deflectedlocalrays, #1[[23]] > 0. & ]*1., 1.]; If[localoutrayspresent, 
           If[localdummyoutrayspresent, localoutrays = Select[localoutrays, #1[[23]] > 0. & ]; 1., 1.]; outrays = Join[outrays, localoutrays]; 1., 
           1.]; If[reportedrayspresent && reporteddummyrayspresent, reportedlocalrays = Select[reportedlocalrays, #1[[23]] > 0. & ]; 1., 1.]); 
         If[reportedrayspresent, raysreported = True; reportedrays = Join[reportedrays, {{-9.87654321*^8, -9.87654321*^8, -9.87654321*^8, 
               -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, 
               -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, 
               -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, 
               -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, 
               -9.87654321*^8, -9.87654321*^8, -9.87654321*^8, -9.87654321*^8}}, reportedlocalrays]; 1., 1.]; inrays = deflectedlocalrays; 
         propagate = generationnumber < generationlimit]; propagatedrays = If[Length[outrays] > 1, Drop[outrays, 1], outrays]; 
       If[generationnumber >= generationlimit && deflectedrayspresent, propagatedrays = Join[propagatedrays, deflectedlocalrays]; 1., 1.]); 
      If[Length[reportedrays] > 2, reportedrays = Drop[reportedrays, 2], reportedrays]; 
      ({#1[[1]], #1[[2]], #1[[3]], #1[[4]], #1[[5]], #1[[6]]} & ) /@ reportedrays], {CompilationTarget -> "MVM", RuntimeAttributes -> {Listable}, 
     Parallelization -> False, RuntimeOptions -> {}}]), TrackedSymbols -> Manipulate, Deployed -> False, AutorunSequencing -> {1, 2, 3, 4, 5, 6}, 
 SynchronousInitialization -> False, SynchronousUpdating -> False, BaseStyle -> {DynamicEvaluationTimeout -> 100}]

How Optica creates CDF content

Optica creates CDF content using the ManipulateSystem function together with the OutputType->NewWindow option.

Optica can create CDF content from other popular optical design packages

Here is an example of Zemax data

lensdata = 
"REM * This ZEMAX lens prescription file was generated by LensVIEW (TM).
REM * ZEMAX is a Trademark of Focus Software, Inc., Tucson, AZ.
REM * LensVIEW is a Trademark of Optical Data Solutions, Inc.
REM
VERS 4.500
NAME U.S.Patent 2671380; Example 1 of 1
UNIT MM
ENPD 28.571420
RAIM
FTYP 0
XFLD 0 0 0
YFLD 0 15.750000 22.500000
FWGT 1 1 1
WAVL 0.4861 0.5876 0.6563
WWGT 1 1 1
PWAV 2
SURF 0
TYPE STANDARD
CURV 0
DISZ INFINITY
SURF 1
TYPE STANDARD
CURV 0.019530922
GLAS XXX 1 0 1.613000 57.2999
DISZ 4.941664859
DIAM 25.059152 1 0
SURF 2
TYPE STANDARD
CURV 0.008197531
DISZ 0.410970100
DIAM 25.059152 1 0
SURF 3
TYPE STANDARD
CURV 0.040670179
GLAS XXX 1 0 1.516000 64.0000
DISZ 13.000688281
DIAM 20.167004 1 0
SURF 4
TYPE STANDARD
CURV 0.059667433
DISZ 6.174575159
DIAM 12.993471 1 0
SURF 5
TYPE STANDARD
CURV 0
GLAS XXX 1 0 1.596000 39.2000
DISZ 2.054850499
DIAM 12.993070 1 0
SURF 6
TYPE STANDARD
CURV 0.070454766
GLAS XXX 1 0 1.613000 58.5997
DISZ 5.352634959
DIAM 10.742458 1 0
SURF 7
TYPE STANDARD
CURV 0
DISZ 2.676317480
DIAM 10.742458 1 0
SURF 8
STOP
TYPE STANDARD
CURV 0
DISZ 2.676317480
DIAM 9.304864 1 0
SURF 9
TYPE STANDARD
CURV 0
GLAS XXX 1 0 1.613000 58.5997
DISZ 4.941664859
DIAM 11.026027 1 0
SURF 10
TYPE STANDARD
CURV -0.041310124
GLAS XXX 1 0 1.596000 39.2000
DISZ 2.465820599
DIAM 11.026027 1 0
SURF 11
TYPE STANDARD
CURV 0
DISZ 6.585545259
DIAM 13.030759 1 0
SURF 12
TYPE STANDARD
CURV -0.053321191
GLAS XXX 1 0 1.516000 64.0000
DISZ 8.500064504
DIAM 13.531942 1 0
SURF 13
TYPE STANDARD
CURV -0.044280492
DISZ 1.443407180
DIAM 18.042590 1 0
SURF 14
TYPE STANDARD
CURV -0.002674924
GLAS XXX 1 0 1.606000 43.9000
DISZ 5.352634959
DIAM 23.054420 1 0
SURF 15
TYPE STANDARD
CURV -0.017047838
DISZ 60.618431335
DIAM 23.054420 1 0
SURF 16
TYPE STANDARD
CURV 0
DISZ 0.000000000
";

ZemaxToOptica is used to import the Zemax data into Optica

compoundlens = ZemaxToOptica[lensdata]

ManipulateSystem[{
	WedgeOfRays[20,NumberOfRays->11,BirthPoint->{-300,0,0},StartAtBirthPoint->False],
	compoundlens,
	Mirror[50,5]},
	OutputType->NewWindow
]

99% of all optical designs are presently made in Zemax and Code V

For every Optica user, there are approximately 100 Zemax users.

No other optical design package supports CDF content or is likely to support it in the foreseeable future.

This presents a huge opportunity for Optica and Mathematica and a paradigm shift for optical modelling in the future.

With CDF it is no longer necessary to “Own” optical modelling packages. With Optica and Mathematica, everything can be built directly over the web and viewed in a Web-Browser.

In fact, Optica already has a JAVA based GUI that can potentially be deployed through any web browser.

Furthermore, by enabling the direct conversion of data content from other optical modeling packages, all of the world’s information on optical designs can potentially be made interactive and assessable through a web portal serviced by Optica and Mathematica.

The world’s optical designs can be converted into CDF content with Optica and Mathematica.

Any optical design can potentially be published with interactive content regardless of the package used to create it in first place.

Cloud computing in optics using CDF content becomes possible.

Through Mathematica, Optica supports parallel computing. Cloud computing can solve optical modelling problems like never before.

A new generation of interactive teaching material becomes possible.

On-line course ware for students.

In the near future: interactive e-books become possible.

Once CDF has been ported to the i-Pad, e-books on Optics will become a phenomenal reading experience.

Examples

Graded Refraction

Code

grinrod = GRINLens[{10,10},10, SymbolicRefractiveModels->{DefaultGRINIndex->(baseIndex-factorX (RSx-xpos)^2-factorY(RSy-ypos)^2)}, SymbolicValues->{baseIndex->2.5,factorX->.001,factorY->.001,xpos->1,ypos->0}];
ManipulateSystem[{Move[Table[Move[SingleRay[],{0,{width*wn,4*wn}}],{wn,-.5,.5,.1}],{{x,10},{y,1}},{th,0}],Move[grinrod,{{glx,15},{gly,0}}], Boundary[40,30]},{DynamicFrame[{PlotType->TopView,PrependGraphics->Hold[Graphics[Dynamic[GeometricTransformation[ContourPlot[(baseIndex-factorX(rsx-xpos)^2-factorY(rsy-ypos)^2),{rsx,-5,5},{rsy,-5,5}][[1]],{{{1,0},{0,1}},{glx+5,gly}}]]]],
AppendGraphics->Hold[Graphics[GeometricTransformation[Locator[Dynamic[{xpos,ypos}],Graphics[{Lighter[Red],Disk[{0,0},.8]},ImageSize->10]],{{{1,0},{0,1}},{glx+5,gly}}]]]}],Dynamic[Plot3D[(baseIndex-factorX(rsx-xpos)^2-factorY(rsy-ypos)^2),{rsx,-5,5},{rsy,-5,5},PlotLabel->"Graded Index Profile",ImageSize->400]]},
	{{factorX,.001,"Horizontal Quadratic Index Factor"},0,.1},
	{{factorY,.001,"Vertical Quadratic Index Factor"},0,.1},
	{{baseIndex,2.5,"Refractive Index Constant"},2.5,10},
	{{width,0,"Beam Width"},0,10},
	SliderLabelReplacements->{"baseIndex"->"Refractive Index Constant","factorX"->"Horizontal Index Quadratic Factor","factorY"->"Vertical Index Quadratic Factor"},
	Deployed->False
]

Chaotic Refraction

Code

positions3 = Apply[Join,
	Table[
		{Random[Real,{-1,1}]+x, Random[Real,{-1,1}]+y},
		{x,-10,10,5},{y,-10,10,5}
	]
];

ManipulateSystem[{Move[LineOfRays[5,NumberOfRays->7],{{x,-14},{y,0}},{th,0}], 
	Resonate[MapIndexed[With[{xsym = ToExpression["x"<>ToString[#2[[1]]]],ysym = ToExpression["y"<>ToString[#2[[1]]]]},
		Move[BallLens[3, FresnelReflections->True],{{xsym,#[[1]]},{ysym,#[[2]]}}]]&,positions3]],Move[Boundary[30],-15]},
		PlotType->TopView, ImageSize->600, SimplifyGraphics->False, Deployed->False]

Lens Snake

Code

lenselements = 15;
defaultspace = 15;
element = BiConvexLens[{focallength,20},12.7,8];
ManipulateSystem[{
	Move[{Move[SingleRay[],0,{θs-div,-5},SymbolicValues->{θs->0,div->5}],Move[SingleRay[],0,{θs,0}],Move[SingleRay[],0,{θs+div,5},SymbolicValues->{θs->0,div->5}]},{{-rs/2,-defaultspace/2},{ys,0}}],
	element,
	Table[Move[element,{{rs Sum[Cos[ix θ Degree],{ix,1,n}],defaultspace n},{rs Sum[Sin[ix θ Degree],{ix,1,n}],0}},{n θ,0},SymbolicValues->{θ->0,rs->defaultspace}],
	{n,1,(lenselements-1)}],
	Move[Move[Screen[25],{screenfactor*rs/2,defaultspace/2},SymbolicValues->{θ->0,rs->defaultspace,screenfactor->1}],{{rs Sum[Cos[ix θ Degree],{ix,1,(lenselements-1)}],(lenselements-1) defaultspace},{rs Sum[Sin[ix θ Degree],{ix,1,(lenselements-1)}],0}},{(lenselements-1) θ,0},SymbolicValues->{θ->0,rs->defaultspace,screenfactor->1,θs->0,div->5}]},{{rs,defaultspace},0,60},{{θ,7},-20,20},{{div,5},0,30},{{focallength,20},10,50},{{screenfactor,2},0,10},{{ys,0},-10,10},ImageSize->600,SliderLabelReplacements->{"rs"->"lens spacing","θ"->"θ snake","div"->"divergence","θs"->"θ source","screenfactor"->"screen position", "ys"->"object position","focallength"->"focal length"}]

Comparison of Lens Types

Code

doublet = DataToOptica[LensDoublet,FocalLength->100,ComponentBoundary->50]

ManipulateSystem[{Table[Move[SingleRay[NumberOfRays->11,WaveLength->lam],{0,{ys*f,0}}],{f,0,1,.1}],
	Move[PlanoConvexLens[100,50,10],{{x1,50},{y-1,-1}}],
	Move[PlanoConvexLens[100,50,10,CurvatureDirection->Back],{{x2,50},{y-100,-100}}],
	Move[SphericalLens[57.36078492440375,-385.36854412733703,50,10],{{x3,50},{y-200,-200}}],
	Move[BiConvexLens[100,50,10],{{x4,50},{y-300,-300}}],
	Move[doublet,{{x5,41.9},{y-400,-400}}],
	Move[PlanoConvexLens[100,50,5,ComponentMedium->Sapphire],{{x7,50},{y-500,-500}}],
	Move[Screen[30],{x,151}],Boundary[175,75,GraphicDesign->False]},{{lam,.532},.4,.7},{{y,1,"Lens Type"},{1->"Plano-Convex (BK7)",100->"Reversed Plano-Convex (BK7)",200->"\"Best Form\" Shape (BK7)",300->"Bi-Convex  (BK7)",400->"Lens Doublet (BK7,F4)",500->"Plano-Convex (Sapphire)"}},Column[{DynamicFrame[PlotType->TopView,PlotLabel->"Comparison of Different Lens Types (Focal Length = 100 mm)",PlotRange->{{-3,175},{-30,30}},Frame->True],
Row[{DynamicFrame[PlotType->TopView,PlotLabel->"Magnified Trace Near Focal Plane",PlotRange->Dynamic[{{x-1,x+1},{-1,1}}],Frame->True,ImageSize->400],DynamicFrame[PlotType->OpticalPathDifference,PlotRange->All,PlotLabel->"Optical Path Difference (wavelengths) vs Trace Position",AspectRatio->1,ReportedSurfaces->7]}]}],SymbolicValues->{ys->10,y->1},SliderLabelReplacements->{"ys"->"Beam Width","lam"->"Wavelength"}]

Optical Sandbox

Code

ManipulateSystem[
{
WedgeOfRays[20,NumberOfRays->11],
Move[PlanoConvexLens[100,50,10,CurvatureDirection->False],{Lens1x, 100}],Move[WedgePrism[-30,{50,50},40],{WPrismX, 150}],
MoveDirected[Screen[50],{235,22},{{ScreenAngle, 175},0}],Move[CylindricalMirror[-100,{60,60},5],{{CylinMirrorX, 300},{CylinMirrorY, 50},0}],Move[BeamSplitter[{50,50},{60,60},10],{{BeamSplitterX,208},{BeamSplitterY,92},0}, {BeamSplitterAngle, 0}],MoveDirected[BiConvexLens[75,100,45],{150,120},{300,50}],ClearBoundary[{80,-50,-100},{310,200,100}],
Boundary[{-60,-70,-100},{350,250,100}]
},
PlotType->TopView
]

SLR Camera

Code

compoundLensTriplet2 =CompoundLens[{(*Radii*) 1.3147, -21.0190, -2.4330, 1.1718, ∞, -31.8418, 1.4005, -1.6991}, {(*Diameter*) 1.54, 1.54, 0.98, .81, .72, .90, 1.16,1.16}, {(*Thicknesses*).329, .223, .081, .194, .125, .082, .278 }, {(*Material*)SK15, IntrinsicMedium, F2, IntrinsicMedium, IntrinsicMedium, K10, SK15, IntrinsicMedium}];

ManipulateSystem[
{Move[LineOfRays[.98, NumberOfRays -> 8], -.930],
Move[compoundLensTriplet2, 0], 
Move[Mirror[1.0], {3, {Mirrory, 0}},{ MirrorAngle,-45}],
Move[Screen[1.16], {3, 1.617}, 90],
Move[SphericalLens[{c11, -21.0},{c12, -2}, 1.16, { t1, .08}], {3, {y1,  1.8}}, 90,  ComponentMedium -> SK15],
Move[PentaPrism[1],{3,2},90], 
Move[SphericalLens[{c21,2.0},{c22,-2}, 1.16,{t2,.2}], {{x2, 4}, 2.5}, ComponentMedium -> SK15],
Move[Screen[1.16], {{xs, 4.617}, 2.5}],
 Move[Screen[1.16], 4.617]}, PlotType -> TopView, Axes -> True]

Conclusion

Code

fimage640=;

imagegraphics = ProjectImage[fimage640,{Move[CustomMirror[Function[3*Sqrt[#1^2+#2^2]],10,SurfaceRayIntersections->Symbol],{0,.001}],Move[Screen[200],3*5]},8,Full3D->True,ReportedSurfaces->{1,2},ImageSize->1000]

Rasterize[Show[imagegraphics,ImageSize->1000],ImageSize->1000,RasterSize->1000]