import java.awt.BorderLayout;

import javax.swing.JFrame;

import vtk.vtkActor;
import vtk.vtkAlgorithmOutput;
import vtk.vtkAppendFilter;
import vtk.vtkCanvas;
import vtk.vtkCutter;
import vtk.vtkDataSet;
import vtk.vtkDataSetMapper;
import vtk.vtkDataSetReader;
import vtk.vtkImplicitPlaneWidget;
import vtk.vtkInteractorStyleTrackballCamera;
import vtk.vtkLookupTable;
import vtk.vtkPanel;
import vtk.vtkPlane;
import vtk.vtkScalarBarActor;
import vtk.vtkUnstructuredGrid;


public class vtkDoubleCutter {
	
	private vtkImplicitPlaneWidget boxw;
	private vtkCutter cutter;
	vtkActor actor;
	
	public vtkDoubleCutter(vtkCanvas panel) {
		vtkDataSetReader reader = new vtkDataSetReader();
		reader.SetFileName("plasma.vtk");
		reader.Update();
		reader.CloseVTKFile();
		
		//vtkDataSet data = reader.GetOutput();
		vtkDataSet output = reader.GetOutput();
		vtkDataSet output2 = reader.GetOutput();
		
		FiltreGeo filtregeo= new FiltreGeo();
		filtregeo.SetInput(output2);
		filtregeo.GetOutput().Update();
		
		// Attention : append agrège des données de même type !!!
		vtkAppendFilter append = new vtkAppendFilter();
		append.AddInput(output);
		append.AddInput(filtregeo.GetOutput());
		append.GetOutput().Update();
		
		//
		cutter = new vtkCutter();
		vtkPlane plan = new vtkPlane();
		plan.SetOrigin(0,0,0);
		plan.SetNormal(0,0,1);
		cutter.SetCutFunction(plan);		
		cutter.SetInput(append.GetOutput());
		cutter.GetOutput().Update();
		
		vtkDataSetMapper mapper = new vtkDataSetMapper();
		actor = new vtkActor();
		
		vtkDataSetMapper mapperw = new vtkDataSetMapper();
		vtkActor actorw = new vtkActor();
		
		mapperw.SetInput(append.GetOutput());
		actorw.SetMapper(mapperw);
		
		// Pipeline Widget
		boxw = new vtkImplicitPlaneWidget();
		boxw.SetInteractor(panel.getIren());
		boxw.SetPlaceFactor(1.0);
		boxw.SetProp3D(actorw);
		boxw.PlaceWidget();
		boxw.SetOutlineTranslation(0);

		// EndInteractionEvent : pour afficher la coupe après le déplacement du plan de coupe
		// i.e. appel myCallBack après déplacement du plan.
		//boxw.AddObserver("EndInteractionEvent", this, "myCallBack");
		boxw.AddObserver("InteractionEvent", this, "myCallBack");

		boxw.On();
		
		
		vtkLookupTable lookuptable = new vtkLookupTable();
		lookuptable.SetHueRange(0.66667,0);
		//lookuptable.SetRange(reader.GetOutput().GetScalarRange());
		lookuptable.Build();
		
		mapper.SetLookupTable(lookuptable);
		mapper.SetScalarRange(append.GetOutput().GetScalarRange());
		
		vtkScalarBarActor baractor = new vtkScalarBarActor();
		baractor.SetLookupTable(lookuptable);
		
		//
		mapper.SetInputConnection(cutter.GetOutputPort());
		actor.SetMapper(mapper);		
		
		panel.setInteractorStyle(new vtkInteractorStyleTrackballCamera());
		panel.GetRenderer().AddActor(actor);
		panel.GetRenderer().AddActor2D(baractor);		
	}
	
	
	public void myCallBack() {
		vtkPlane plane = new vtkPlane();
		boxw.GetPlane(plane);
		cutter.SetCutFunction(plane);
		boxw.SetDrawPlane(0);
	}
	
	
	public static void main(String[] args) {
		vtkCanvas panel = new vtkCanvas();
		//
		vtkDoubleCutter doublecut = new vtkDoubleCutter(panel);
		//

		
		//
		JFrame f = new JFrame("Cutting test");
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.setSize(500, 500);
		f.setLocationRelativeTo(null);
		f.getContentPane().setLayout(new BorderLayout());
		f.getContentPane().add(panel, BorderLayout.CENTER);
		f.setVisible(true);
	}

}
