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.canvas.IcyCanvas;
022import icy.painter.Overlay;
023import icy.painter.VtkPainter;
024import icy.sequence.Sequence;
025
026import java.awt.Graphics2D;
027
028import plugins.kernel.canvas.VtkCanvas;
029import vtk.vtkActor2D;
030import vtk.vtkCubeSource;
031import vtk.vtkLabeledDataMapper;
032import vtk.vtkProp;
033import vtk.vtkRenderer;
034import vtk.vtkSelectVisiblePoints;
035
036/**
037 * @author stephane
038 */
039public class VtkLabelPainter extends Overlay implements VtkPainter
040{
041    private vtkActor2D pointLabels;
042    private vtkSelectVisiblePoints visPts;
043
044    public VtkLabelPainter()
045    {
046        super("VTK label");
047
048        init();
049    }
050
051    // init vtk objects
052    private void init()
053    {
054        // labellers to display coordinates
055        final vtkCubeSource cubeSource = new vtkCubeSource();
056        cubeSource.SetBounds(0, 50, 100, 150, 300, 200);
057
058        // Create labels for points
059        visPts = new vtkSelectVisiblePoints();
060        visPts.SetInputConnection(cubeSource.GetOutputPort());
061
062        // Create the mapper to display the point ids. Specify the format to
063        // use for the labels. Also create the associated actor.
064        final vtkLabeledDataMapper ldm = new vtkLabeledDataMapper();
065
066        ldm.SetInputConnection(visPts.GetOutputPort());
067        ldm.SetLabelFormat("%g");
068        ldm.SetLabelModeToLabelFieldData();
069
070        pointLabels = new vtkActor2D();
071        pointLabels.SetMapper(ldm);
072    }
073
074    @Override
075    public void paint(Graphics2D g, Sequence sequence, IcyCanvas canvas)
076    {
077        if (canvas instanceof VtkCanvas)
078        {
079            final vtkRenderer renderer = ((VtkCanvas) canvas).getRenderer();
080
081            if (visPts.GetRenderer() == null)
082                visPts.SetRenderer(renderer);
083        }
084    }
085
086    @Override
087    public vtkProp[] getProps()
088    {
089        return new vtkProp[] {pointLabels};
090    }
091
092}