import java.awt.BorderLayout;
import java.util.ArrayList;

import javax.swing.JFrame;

import vtk.vtkActor;
import vtk.vtkCutter;
import vtk.vtkDataSetMapper;
import vtk.vtkGeneralTransform;
import vtk.vtkImageData;
import vtk.vtkImplicitBoolean;
import vtk.vtkImplicitFunction;
import vtk.vtkPanel;
import vtk.vtkPlane;

public class HexagoneImplicitFunction {

	int _halfheight = 2;
	int _pas = 3;

	vtkGeneralTransform _trans = new vtkGeneralTransform();

	public HexagoneImplicitFunction() {

	}

	public vtkImplicitFunction getFunction(int scale) {

		double a = Math.sqrt(3) / 2;
		double b = 0.5;

		ArrayList<double[]> points = new ArrayList<double[]>(8);
		ArrayList<double[]> normal = new ArrayList<double[]>(8);

		points.add(new double[] { 1, 0, 0 });
		points.add(new double[] { 0, 1, 0 });
		points.add(new double[] { -1, 0, 0 });
		points.add(new double[] { -1, 0, 0 });
		points.add(new double[] { 0, -1, 0 });
		points.add(new double[] { 1, 0, 0 });
		points.add(new double[] { 0, 0, _halfheight });
		points.add(new double[] { 0, 0, -_halfheight });
		normal.add(new double[] { a, b, 0 });
		normal.add(new double[] { 0, 1, 0 });
		normal.add(new double[] { -a, b, 0 });
		normal.add(new double[] { -a, -b, 0 });
		normal.add(new double[] { 0, -1, 0 });
		normal.add(new double[] { a, -b, 0 });
		normal.add(new double[] { 0, 0, 1 });
		normal.add(new double[] { 0, 0, -1 });
		

		vtkImplicitBoolean func = new vtkImplicitBoolean();
		vtkPlane p = null;
		for (int i = 0; i < 8; i++) {
			p = new vtkPlane();
			p.SetOrigin(points.get(i));
			p.SetNormal(normal.get(i));
			func.AddFunction(p);
			func.SetOperationType(1);
		}

		return func;
	}

	public static void main(String[] args) {

		vtkPanel panel = new vtkPanel();

		vtkImageData data = new vtkImageData();
		data.SetDimensions(22, 22, 50);
		data.SetOrigin(-1.05, -1.05, -2.5);
		data.SetSpacing(0.1, 0.1, 0.1);

		HexagoneImplicitFunction hex = new HexagoneImplicitFunction();

//		vtkPlane plane = new vtkPlane();
//		plane.SetOrigin(0, 0, 0);
//		plane.SetNormal(0, 0, 1);

		vtkCutter cutter = new vtkCutter();
		cutter.SetCutFunction(hex.getFunction(1));
		cutter.SetInput(data);

		vtkDataSetMapper mapper = new vtkDataSetMapper();
		mapper.SetInput(cutter.GetOutput());

		vtkActor actor = new vtkActor();
		actor.SetMapper(mapper);

		panel.GetRenderer().AddActor(actor);

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

	}

}
