package seed.minerva.apps.mse;

import jafama.FastMath;
import otherSupport.ColorMaps;
import seed.minerva.MinervaOpticsSettings;
import seed.minerva.aug.mse.AugMSESystem;
import seed.minerva.optics.drawing.VRMLDrawer;
import seed.minerva.optics.types.Optic;
import seed.minerva.optics.types.Surface;

/** Interpolate the beam axis drawn on the image between 3D points on the backwall to find
 * the point 'behind' the beam axis, then draw a line from that out to see if/where they hit the mirror(box)
 *  
 * @author oliford
 */
public class BeamAxisTransformBacktraceVRML {
	private static final int beamIdx = AugMSESystem.BEAM_Q3;
	private static final double R0 = 1.50; //just past core
	private static final double R1 = 2.10;
	private static final double fwhm = 0.02;//0.16;
	private static final double dL = 0.05;
	private static final double[][] cols = ColorMaps.jet(20);
	
	public static void main(String[] args) {
		String outPath = MinervaOpticsSettings.getAppsOutputPath();
		makeBeamsRadialColoured(outPath + "/augBeams-colourByR.vrml");
		makeBeamsAllGreen(outPath + "/augBeams-allGreen.vrml");
		makeBeamsPINIColoured(outPath + "/augBeams-colourByPini.vrml");
	}	
	
	private static void makeBeamsAllGreen(String fileName){
			
		VRMLDrawer vrmlOut = new VRMLDrawer(fileName, 0.005);
		vrmlOut.addVRML(AugMSESystem.vrmlScaleToAUGDDD);
		vrmlOut.setDrawPolarisationFrames(false);
		vrmlOut.drawOptic(AugMSESystem.makeAllBeamCylds());
		vrmlOut.addVRML("}");
		vrmlOut.destroy();
	}
	
	private static void makeBeamsPINIColoured(String fileName){
		
		VRMLDrawer vrmlOut = new VRMLDrawer(fileName, 0.005);
		vrmlOut.addVRML(AugMSESystem.vrmlScaleToAUGDDD);
		vrmlOut.setDrawPolarisationFrames(false);
		
		vrmlOut.addMat("beam_Q1", "1 1 0", 0.7);  
		vrmlOut.addMat("beam_Q2", "0 1 0", 0.7);  
		vrmlOut.addMat("beam_Q3", "0 0 1", 0.7);  
		vrmlOut.addMat("beam_Q4", "1 0 0", 0.7);  
				
		vrmlOut.drawOptic(AugMSESystem.makeBeamCylds(AugMSESystem.BEAM_Q1, dL, fwhm), "beam_Q1");
		vrmlOut.drawOptic(AugMSESystem.makeBeamCylds(AugMSESystem.BEAM_Q2, dL, fwhm), "beam_Q2");
		vrmlOut.drawOptic(AugMSESystem.makeBeamCylds(AugMSESystem.BEAM_Q3, dL, fwhm), "beam_Q3");
		vrmlOut.drawOptic(AugMSESystem.makeBeamCylds(AugMSESystem.BEAM_Q4, dL, fwhm), "beam_Q4");
		
		vrmlOut.addVRML("}");
		vrmlOut.destroy();
	}	
	
	private static void makeBeamsRadialColoured(String fileName){
		VRMLDrawer vrmlOut = new VRMLDrawer(fileName, 0.005);
		for(int i=0; i < cols.length; i++)
			vrmlOut.addMat("beamR" + i, 
					cols[i][0] + " " + cols[i][1] + " " + cols[i][2],
					0.3);
		
		vrmlOut.addMat("beamOutOfRange", "0 0 0" , 0.8);
	
		vrmlOut.addVRML(AugMSESystem.vrmlScaleToAUGDDD);
		vrmlOut.setDrawPolarisationFrames(false);
		drawRadialColoured(vrmlOut, AugMSESystem.makeAllBeamCylds());
		vrmlOut.addVRML("}");
		vrmlOut.destroy();
	}
	
	private static void drawRadialColoured(VRMLDrawer vrmlOut, Optic optic){
		
		for(Optic subOptic : optic.getSubOptics()){
			drawRadialColoured(vrmlOut, subOptic);
		}
		
		for(Surface surf : optic.getSurfaces()){
			double pos[] = surf.getBoundarySphereCentre();
			double R = FastMath.sqrt(pos[0]*pos[0] + pos[1]*pos[1]);
			
			int colIdx = (int)(cols.length * (R - R0) / (R1 - R0));
			
			if(colIdx >= 0 && colIdx < cols.length)
				vrmlOut.drawSurface(surf, "beamR" + colIdx);
			else
				vrmlOut.drawSurface(surf, "beamOutOfRange");
			
		}
	}
}
