import java.awt.BorderLayout;

import javax.swing.JFrame;

import vtk.vtkActor;
import vtk.vtkAlgorithmOutput;
import vtk.vtkCanvas;
import vtk.vtkCutter;
import vtk.vtkDataSetMapper;
import vtk.vtkDataSetReader;
import vtk.vtkImplicitPlaneWidget;
import vtk.vtkInteractorStyleTrackballCamera;
import vtk.vtkLookupTable;
import vtk.vtkOutlineFilter;
import vtk.vtkPlane;
import vtk.vtkScalarBarActor;

public class MyCutWidget {
	private vtkImplicitPlaneWidget boxw;
	private vtkCutter cutter;
	private boolean widgetOn;

	public MyCutWidget() {
		vtkCanvas panel = new vtkCanvas();
		//
		vtkDataSetReader reader = new vtkDataSetReader();
		reader.SetFileName("plasma.vtk");
		reader.Update();
		reader.CloseVTKFile();
		widgetOn = true;

		// Pipeline Cutter Plane
		// vtkDataSet data = reader.GetOutput();
		vtkAlgorithmOutput imageOutput = reader.GetOutputPort();
		vtkDataSetMapper mapper = new vtkDataSetMapper();
		vtkActor actor = new vtkActor();

		vtkActor boundActor = new vtkActor();

		//
		cutter = new vtkCutter();
		vtkPlane plan = new vtkPlane();
		plan.SetOrigin(0, 0, 0);
		plan.SetNormal(0, 0, 1);

		cutter.SetCutFunction(plan);
		cutter.SetInputConnection(imageOutput);
		mapper.SetInputConnection(cutter.GetOutputPort());

		actor.SetMapper(mapper);
		
		// Pipeline bounding Box		
		vtkOutlineFilter outlinefilter = new vtkOutlineFilter();
		outlinefilter.SetInputConnection(imageOutput);

		vtkDataSetMapper boundMapper = new vtkDataSetMapper();
		
		boundMapper.SetInputConnection(outlinefilter.GetOutputPort());

		boundActor.SetMapper(boundMapper);
		
		
		// Pipeline Widget
		boxw = new vtkImplicitPlaneWidget();
		boxw.SetInteractor(panel.getIren());
		boxw.SetPlaceFactor(1.0);
		boxw.SetProp3D(boundActor);
		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();
		
		// vtkcubeaxisActor2d => valeur x, y, z
		
		// lookuptable
		vtkLookupTable lookuptable = new vtkLookupTable();
		lookuptable.SetHueRange(0.66667,0);
		lookuptable.Build();
		
		mapper.SetLookupTable(lookuptable);
		mapper.SetScalarRange(reader.GetOutput().GetScalarRange());
		
		vtkScalarBarActor baractor = new vtkScalarBarActor();
		baractor.SetLookupTable(lookuptable);
		
		
		// Rendu...

		panel.GetRenderer().AddActor(actor);
		panel.GetRenderer().AddActor(boundActor);
		panel.GetRenderer().AddActor2D(baractor);
		
		myCallBack();
		
		panel.setInteractorStyle(new vtkInteractorStyleTrackballCamera());		

		//
		JFrame f = new JFrame("Widget 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);

	}

	public void myCallBack() {
		vtkPlane plane = new vtkPlane();
		boxw.GetPlane(plane);
		cutter.SetCutFunction(plane);
		boxw.SetDrawPlane(0);
	}

	public static void main(String[] args) {
		new MyCutWidget();
	}
}
