import java.awt.BorderLayout;

import javax.swing.JFrame;

import vtk.vtkActor;
import vtk.vtkActor2D;
import vtk.vtkDataSetMapper;
import vtk.vtkDataSetWriter;
import vtk.vtkExtractGrid;
import vtk.vtkIdList;
import vtk.vtkLabeledDataMapper;
import vtk.vtkPanel;
import vtk.vtkPoints;
import vtk.vtkUnstructuredGrid;

public class Hexagone {
	private vtkUnstructuredGrid pdata;

	public Hexagone() {

		pdata = new vtkUnstructuredGrid();
		vtkPoints points = new vtkPoints();

		points.InsertNextPoint(-1, 0, -5);
		points.InsertNextPoint(-0.5, Math.sqrt(3) / 2, -5);
		points.InsertNextPoint(0.5, Math.sqrt(3) / 2, -5);
		points.InsertNextPoint(1, 0, -5);
		points.InsertNextPoint(0.5, -Math.sqrt(3) / 2, -5);
		points.InsertNextPoint(-0.5, -Math.sqrt(3) / 2, -5);

		points.InsertNextPoint(0, 0, -5);

		points.InsertNextPoint(-1, 0, 5);
		points.InsertNextPoint(-0.5, Math.sqrt(3) / 2, 5);
		points.InsertNextPoint(0.5, Math.sqrt(3) / 2, 5);
		points.InsertNextPoint(1, 0, 5);
		points.InsertNextPoint(0.5, -Math.sqrt(3) / 2, 5);
		points.InsertNextPoint(-0.5, -Math.sqrt(3) / 2, 5);

		points.InsertNextPoint(0, 0, 5);

		pdata.SetPoints(points);

		int VTK_TRIANGLE = 5;
		int VTK_TRIANGLE_STRIP = 6;
		vtkIdList idlist = new vtkIdList();
		idlist.InsertNextId(0);
		idlist.InsertNextId(5);
		idlist.InsertNextId(6);
		pdata.InsertNextCell(VTK_TRIANGLE, idlist);

		idlist = new vtkIdList();
		idlist.InsertNextId(5);
		idlist.InsertNextId(4);
		idlist.InsertNextId(6);
		pdata.InsertNextCell(VTK_TRIANGLE, idlist);

		idlist = new vtkIdList();
		idlist.InsertNextId(4);
		idlist.InsertNextId(3);
		idlist.InsertNextId(6);
		pdata.InsertNextCell(VTK_TRIANGLE, idlist);

		idlist = new vtkIdList();
		idlist.InsertNextId(2);
		idlist.InsertNextId(6);
		idlist.InsertNextId(3);
		pdata.InsertNextCell(VTK_TRIANGLE, idlist);

		idlist = new vtkIdList();
		idlist.InsertNextId(2);
		idlist.InsertNextId(1);
		idlist.InsertNextId(6);
		pdata.InsertNextCell(VTK_TRIANGLE, idlist);

		idlist = new vtkIdList();
		idlist.InsertNextId(1);
		idlist.InsertNextId(0);
		idlist.InsertNextId(6);
		pdata.InsertNextCell(VTK_TRIANGLE, idlist);

		idlist = new vtkIdList();
		idlist.InsertNextId(0);
		idlist.InsertNextId(7);
		idlist.InsertNextId(5);
		idlist.InsertNextId(12);
		idlist.InsertNextId(4);
		idlist.InsertNextId(11);
		idlist.InsertNextId(3);
		idlist.InsertNextId(10);
		idlist.InsertNextId(2);
		idlist.InsertNextId(9);
		idlist.InsertNextId(1);
		idlist.InsertNextId(8);
		idlist.InsertNextId(0);
		idlist.InsertNextId(7);
		pdata.InsertNextCell(VTK_TRIANGLE_STRIP, idlist);

		idlist = new vtkIdList();
		idlist.InsertNextId(7);
		idlist.InsertNextId(13);
		idlist.InsertNextId(12);
		pdata.InsertNextCell(VTK_TRIANGLE, idlist);

		idlist = new vtkIdList();
		idlist.InsertNextId(12);
		idlist.InsertNextId(13);
		idlist.InsertNextId(11);
		pdata.InsertNextCell(VTK_TRIANGLE, idlist);

		idlist = new vtkIdList();
		idlist.InsertNextId(11);
		idlist.InsertNextId(13);
		idlist.InsertNextId(10);
		pdata.InsertNextCell(VTK_TRIANGLE, idlist);

		idlist = new vtkIdList();
		idlist.InsertNextId(10);
		idlist.InsertNextId(13);
		idlist.InsertNextId(9);
		pdata.InsertNextCell(VTK_TRIANGLE, idlist);

		idlist = new vtkIdList();
		idlist.InsertNextId(9);
		idlist.InsertNextId(13);
		idlist.InsertNextId(8);
		pdata.InsertNextCell(VTK_TRIANGLE, idlist);

		idlist = new vtkIdList();
		idlist.InsertNextId(8);
		idlist.InsertNextId(13);
		idlist.InsertNextId(7);
		pdata.InsertNextCell(VTK_TRIANGLE, idlist);

	}

	public vtkUnstructuredGrid getPdata() {
		return pdata;
	}

	public static void main(String[] args) {

		vtkPanel panel = new vtkPanel();

		Hexagone hex = new Hexagone();
		vtkUnstructuredGrid pdata = hex.getPdata();

		vtkLabeledDataMapper mapperNode = new vtkLabeledDataMapper();
		mapperNode.SetInput(pdata);

		vtkDataSetMapper mapper = new vtkDataSetMapper();
		vtkActor actor = new vtkActor();
		vtkActor2D nodeactor = new vtkActor2D();

		/*
		 * vtkDelaunay3D delaunay3D = new vtkDelaunay3D();
		 * delaunay3D.SetInput(pdata); delaunay3D.SetTolerance(0.01);
		 * vtkPolyData data = delaunay3D.GetOutput();
		 */
		vtkExtractGrid extractGrid = new vtkExtractGrid();
		extractGrid.SetInput(pdata);
		extractGrid.SetSampleRate(2, 2, 2);
		mapper.SetInput(pdata);

		actor.SetMapper(mapper);
		nodeactor.SetMapper(mapperNode);
		panel.GetRenderer().AddActor(actor);
		// panel.GetRenderer().AddActor(nodeactor);
		
		// Writer
//		vtkDataSetWriter writer = new vtkDataSetWriter();
//		writer.SetFileName("hexagone.vtk");
//		writer.SetFileTypeToASCII();
//		writer.SetInput(pdata);
//		writer.Update();

		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);

	}

}
