001/*
002 * Copyright 2010, 2011 Institut Pasteur.
003 * 
004 * This file is part of ICY.
005 * 
006 * ICY is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * ICY is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with ICY. If not, see <http://www.gnu.org/licenses/>.
018 */
019package plugins.tutorial.vtk;
020
021import icy.painter.Overlay;
022import icy.painter.VtkPainter;
023import icy.vtk.VtkUtil;
024import vtk.vtkActor;
025import vtk.vtkCellArray;
026import vtk.vtkPoints;
027import vtk.vtkPolyData;
028import vtk.vtkPolyDataMapper;
029import vtk.vtkProp;
030
031/**
032 * This plugin shows how use VTK to render a simple cube mesh as a 3D painter
033 * 
034 * @author Stephane
035 */
036public class VtkCubePainter extends Overlay implements VtkPainter
037{
038    private static final double[][] cube_vertex = new double[][] { {-10, -10, -10}, {-10, 10, -10}, {10, 10, -10},
039            {10, -10, -10}, {-10, -10, 10}, {-10, 10, 10}, {10, 10, 10}, {10, -10, 10}};
040    private static final int[][] cube_poly = new int[][] { {0, 1, 2}, {0, 2, 3}, {4, 5, 1}, {4, 1, 0}, {3, 2, 6},
041            {3, 6, 7}, {1, 5, 6}, {1, 6, 2}, {4, 0, 3}, {4, 3, 7}, {7, 6, 5}, {7, 5, 4}};
042
043    private vtkActor cubeActor;
044
045    public VtkCubePainter()
046    {
047        super("VTK cube");
048
049        init();
050    }
051
052    private void init()
053    {
054        // vertex data
055        final vtkPoints points;
056        // polygon data
057        final vtkCellArray cells;
058
059        // fast java data conversion for vertexes
060        points = VtkUtil.getPoints(cube_vertex);
061        // fast java data conversion for cells (polygons)
062        cells = VtkUtil.getCells(12, VtkUtil.prepareCells(cube_poly));
063
064        final vtkPolyData polyData = new vtkPolyData();
065
066        // set polygon
067        polyData.SetPolys(cells);
068        // set vertex
069        polyData.SetPoints(points);
070
071        // add actor to the renderer
072        final vtkPolyDataMapper polyMapper = new vtkPolyDataMapper();
073        polyMapper.SetInputData(polyData);
074
075        cubeActor = new vtkActor();
076        cubeActor.SetMapper(polyMapper);
077    }
078
079    @Override
080    public vtkProp[] getProps()
081    {
082        return new vtkProp[] {cubeActor};
083    }
084}