import java.awt.BorderLayout;

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 = 5;
	int _pas = 3;
	
	vtkGeneralTransform _trans = new vtkGeneralTransform();
	
	public HexagoneImplicitFunction() {

	}
	
	public vtkImplicitFunction getFunction() {

        vtkImplicitBoolean func = new vtkImplicitBoolean();
        vtkPlane p = new vtkPlane();
        p.SetOrigin( 0, 0, _halfheight );
        p.SetNormal( 0, 0, 1 );
        func.AddFunction( p );

        p = new vtkPlane();
        p.SetOrigin( 0, 0, - _halfheight );
        p.SetNormal( 0, 0, -1 );
        func.AddFunction( p );
        func.SetOperationType( 1 );

        p = new vtkPlane();
        p.SetOrigin( _pas, 0, 0);
        p.SetNormal( Math.cos(Math.PI/3), Math.sin(Math.PI/3), 0 );
        func.AddFunction( p );
        func.SetOperationType( 1 );
        vtkGeneralTransform t = new vtkGeneralTransform();
        t.Translate( 0, -2 * _pas, 0 );
        p.SetNormal( -Math.cos(Math.PI/3), -Math.sin(Math.PI/3), 0 );
        func.AddFunction( p );
        func.SetOperationType( 1 );

        p = new vtkPlane();
        p.SetOrigin( 0, _pas, 0);
        p.SetNormal( 0, 1, 0 );
        func.AddFunction( p );
        func.SetOperationType( 1 );
        t = new vtkGeneralTransform();
        t.Translate( 0, -2 * _pas, 0 );
        p.SetNormal( 0, -1, 0 );
        func.AddFunction( p );
        func.SetOperationType( 1 );

        p = new vtkPlane();
        p.SetOrigin( _pas, 0, 0);
        p.SetNormal( Math.cos(Math.PI/3), -Math.sin(Math.PI/3), 0 );
        func.AddFunction( p );
        func.SetOperationType( 1 );
        t = new vtkGeneralTransform();
        t.Translate( 0, -2 * _pas, 0 );
        p.SetNormal( -Math.cos(Math.PI/3), Math.sin(Math.PI/3), 0 );
        func.AddFunction( p );
        func.SetOperationType( 1 );

        vtkImplicitBoolean nf = new vtkImplicitBoolean();
        nf.AddFunction( func );
        nf.SetOperationType( 1 );

        t = new vtkGeneralTransform();
        t.SetInput( _trans );
        t.Inverse();
        nf.SetTransform( t );

        return nf;
	}

	public static void main(String[] args) {
		
	vtkPanel panel = new vtkPanel();
   
    vtkImageData data = new vtkImageData();
    data.SetDimensions(10, 10, 20);
    
    HexagoneImplicitFunction hex = new HexagoneImplicitFunction();
    
    vtkCutter cutter = new vtkCutter();
    cutter.SetCutFunction(hex.getFunction());
    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);

}
	
}
