package ipp.imse.fusionOptics.apps.imse;

import ipp.imse.fusionOptics.imse.IMSEOptics105_35;
import ipp.imse.fusionOptics.imse.IMSEOptics135_50;
import ipp.imse.fusionOptics.imse.IMSEOptics135_50_rescaled;
import ipp.imse.fusionOptics.imse.IMSEOptics180_50;
import ipp.imse.fusionOptics.imse.IMSEOptics75_25;
import ipp.imse.fusionOptics.imse.IMSEOptics75_25_expanded_filter;
import ipp.imse.fusionOptics.imse.IMSEOptics75_25_imgFielded;
import ipp.imse.fusionOptics.imse.IMSEOptics75_25_imgFielded_fibreFielded;
import ipp.imse.fusionOptics.imse.IMSEOptics75_25_rescaled;
import ipp.imse.fusionOptics.imse.IMSEOptics75_25_silly_second_diag;
import ipp.imse.fusionOptics.mse.AugFaroNov2013;
import ipp.imse.fusionOptics.mse.AugMSESystem;
import ipp.imse.fusionOptics.mse.AugMseFaroData;
import jafama.FastMath;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import cache.common.Cache;
import cache.randomAccessCache.RACacheService;
import fusionOptics.MinervaOpticsSettings;
import fusionOptics.Util;
import fusionOptics.collection.HitsCollector;
import fusionOptics.collection.ImageCollector;
import fusionOptics.collection.IntensityInfo;
import fusionOptics.collection.LensDepolarisationInfoCollector;
import fusionOptics.collection.PlaneAngleInfo;
import fusionOptics.drawing.SVGCylindricalProjection;
import fusionOptics.drawing.SVGRayDrawing;
import fusionOptics.drawing.VRMLDrawer;
import fusionOptics.interfaces.NullInterface;
import fusionOptics.optics.Box;
import fusionOptics.optics.SimpleDoubleConvexLens;
import fusionOptics.optimisation.OptimiseLensCurvature;
import fusionOptics.pointSpread.DualGaussianPSF;
import fusionOptics.pointSpread.PSFGrid;
import fusionOptics.pointSpread.PointSpreadBuilder;
import fusionOptics.pointSpread.PointSpreadFunction;
import fusionOptics.pointSpread.PointsPSF;
import fusionOptics.surfaces.Cylinder;
import fusionOptics.surfaces.Plane;
import fusionOptics.surfaces.Square;
import fusionOptics.tracer.Tracer;
import fusionOptics.types.Element;
import fusionOptics.types.Intersection;
import fusionOptics.types.Optic;
import fusionOptics.types.Pol;
import fusionOptics.types.RaySegment;
import fusionOptics.types.Surface;

import binaryMatrixFile.AsciiMatrixFile;
import binaryMatrixFile.BinaryMatrixFile;
import binaryMatrixFile.BinaryMatrixWriter;

import otherSupport.ColorMaps;
import otherSupport.StatusOutput;


import oneLiners.OneLiners;



/** Generates ray tracing of the FARO (nov2013) MSE LOS points.
 * 
 * 
 * @author oliford
 *
 */
public class DrawMSELosTracing {
	
	final static String outPath = MinervaOpticsSettings.getAppsOutputPath() + "/rayTracing/augImse/mseLOSRays/";
	final static int nRays = 50;
	
	/** What rays to trace */
	public static double minIntensity = 0.01;
	public static boolean traceReflections = false;
	
	/** Optical system setup */
	public static AugMSESystem sys = new AugMSESystem(new IMSEOptics135_50_rescaled());
	
	//we require three optics;
	// 1) The imaging plane, which will collect the light INTENSITY.
	// 2) The polarisation sensitive plane, which collects the light polarisation.
	// 3) The inital target to fire rays at.
	// e.g. for the normal MSE system, 1 is thee fibre plane, 2 is the PEMs and 3 is the main mirror:		
	public static Plane imagePlane = sys.imseOptics.ccd;
	public static Plane polarisationPlane = sys.tubeOptics.PEMsFront;
	public static Element initRaysTarget = sys.mainMirror; //sys.mirrorBox.holeGlassFront;
	
	/** Don't draw rays that don't hit these elements */
	public static Surface mustHitToDraw = sys.tubeOptics.fibreEnds;
	
	final static double wavelen = 653e-9;
	
	public static void main(String[] args) {
		drawMSELosRays();
	}
	
	public static void drawMSELosRays(){
		
		VRMLDrawer vrmlOut = new VRMLDrawer(outPath + "/pictures.vrml", 0.005);
		vrmlOut.setDrawPolarisationFrames(false);
		vrmlOut.addVRML(AugMSESystem.vrmlScaleToAUGDDD);
		
		sys.setupForPulse(351); //Jan2013 setup
		
		double c[] = sys.tubeOptics.fibreEnds.getCentre();
		double n[] = sys.tubeOptics.fibreEnds.getNormal();
		
		AugFaroNov2013 faro = new AugFaroNov2013();
		
		double cols[][] = ColorMaps.jet(10);
		
		StatusOutput stat = new StatusOutput(DrawMSELosTracing.class, 10*6*nRays);		
		for(int ch=0; ch < 10; ch++){ //channel
			for(int f=0; f < 6; f++){ //fibre
			 	//mse-hb-6-5 = hinter beam = between beam and back wall
				//kanal-vb7-06 = vor beam = between beam and mirror box 
			
				double startPos[] = faro.getPos("mse-hb-" + (ch+1) + "-" + (f+1));
							
				int nAttempts = 0, nHit = 0;
				
				for(int i=0; i < nRays; i++){
					 do{
						 stat.doStatus((ch*6 + f)*nRays + i);
							
						Pol.recoverAll();
						
						RaySegment ray = new RaySegment();
						ray.startPos =  startPos.clone();
						ray.dir = Tracer.generateRandomRayTowardSurface(ray.startPos, initRaysTarget);
						
						ray.length = Double.POSITIVE_INFINITY;
						ray.up = Util.cross(Util.reNorm(Util.cross(ray.dir, new double[]{0,0,1})), ray.dir);
						
						ray.E0 = PointSpreadFunction.getInputStatesForMuellerCalc();
						ray.wavelength = wavelen;
						
						Tracer.trace(sys, ray, 100, minIntensity, traceReflections);
						
						List<Intersection> hits = ray.getIntersections(mustHitToDraw); 
										
						if(hits.size() > 0){
							vrmlOut.drawRay(ray, cols[ch]);
							
							nHit++;
							break;
						}
						if(nAttempts > 10000){
							break;
						}
						nAttempts++;
					}while(true);
					 
					System.out.println("\n--------------------------------------- "+ch+" "+f+" ----------------------------------------");
					System.out.println("(ch=" + ch + ", f="+f+"):\t" + nAttempts + "\t" + nHit);
					 
				}
			}
			
			
		}
		
		vrmlOut.drawOptic(sys);
		vrmlOut.addVRML("}"); //end of rotate/transform
		vrmlOut.destroy();
		stat.done();
				
	}

	
		
}
