[vtkusers] vtkTextActor versus vtkTextMapper: whose vtkTextProperty wins?

Steve M. Robbins steven.robbins at videotron.ca
Thu Aug 25 16:42:23 EDT 2005


Hi,

I'm using VTK 4.2, but a quick peek at CVS leads me to believe that
this particular code hasn't changed.  I didn't compile any code against
cvs VTK, though, so I could be wrong.

I'm trying to position multi-line text in one of two ways; either:
(1) specify the lower left corner and the text is left and bottom justified, or
(2) specify the top right corner and the text is right and top justified.

The vtkTextActor has a vtkTextProperty member.
The vtkTextActor's vtkTextMapper also has a vtkTextProperty member.
I'm confused as to what each of these control.

By setting both of these vtkTextProperies to the same object, I managed
to achieve what I wanted; see example code below.

Unfortunately, it breaks if I enable scaled text.  It looks like the
"right top" actor is now being positioned according to the bottom left
of its bounding box.  At any rate, it appears in the top, right
quadrant of the display, overlapping the "bottom left" actor.  How can
I achieve what I need using scaled text?

Thanks,
-Steve


import java.awt.BorderLayout;

import javax.swing.JComponent;
import javax.swing.JFrame;

import vtk.vtkActor2D;
import vtk.vtkCellArray;
import vtk.vtkCoordinate;
import vtk.vtkPanel;
import vtk.vtkPoints;
import vtk.vtkPolyData;
import vtk.vtkPolyDataMapper2D;
import vtk.vtkRenderer;
import vtk.vtkTextActor;
import vtk.vtkTextMapper;
import vtk.vtkTextProperty;
import vtk.util.VtkPanelUtil;

public class TextScaling extends JComponent
{
    private vtkPanel renWin;
    
    public TextScaling() 
    {
        // Setup VTK rendering panel
        renWin = new vtkPanel();
        vtkRenderer ren = renWin.GetRenderer();
        
        ren.AddActor2D( createBottomLeft() );
        ren.AddActor2D( createTopRight() );
        
        ren.AddActor2D( createGrid() );
        
        VtkPanelUtil.setSize(renWin, 500, 300);
        ren.GetActiveCamera().Zoom(1.5);
        
        // Place renWin in the center of this panel
        setLayout(new BorderLayout());
        add(renWin, BorderLayout.CENTER);
    }

    private vtkActor2D createBottomLeft()
    {
        vtkTextProperty textProperty = new vtkTextProperty();
        textProperty.SetVerticalJustificationToBottom();
        textProperty.SetJustificationToLeft();
        return createTextActor( textProperty );
    }

    private vtkActor2D createTopRight()
    {
        vtkTextProperty textProperty = new vtkTextProperty();
        textProperty.SetVerticalJustificationToTop();
        textProperty.SetJustificationToRight();
        return createTextActor( textProperty );
    }

    private vtkTextActor createTextActor( vtkTextProperty textProperty )
    {
        vtkTextMapper mapper = new vtkTextMapper();
        mapper.SetTextProperty( textProperty );
        
        vtkTextActor actor = new vtkTextActor();
        actor.SetMapper( mapper );
        actor.SetTextProperty( textProperty );

        initializeTextActor( textProperty, actor );
        return actor;
    }

    private void initializeTextActor( vtkTextProperty textProperty, vtkTextActor actor )
    {
        actor.ScaledTextOff();  // Breaks if I turn on scaled text!!
        actor.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay();
        
        actor.SetInput( "Horizontal justification: " + textProperty.GetJustificationAsString() + "\n" + 
                        "Vertical justification: " + textProperty.GetVerticalJustificationAsString() );

        actor.SetPosition( 0.5, 0.5 );
        actor.SetPosition2( 0.5, 0.2 );
    }

    private void initializeTextActor( vtkTextProperty textProperty, vtkTextMapper mapper, vtkActor2D actor )
    {
        actor.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay();
        
        mapper.SetInput( "Horizontal justification: " + textProperty.GetJustificationAsString() + "\n" + 
                         "Vertical justification: " + textProperty.GetVerticalJustificationAsString() );

        actor.SetPosition( 0.5, 0.5 );
        actor.SetPosition2( 0.5, 0.2 );
    }

    /**
     * @return a grid to demonstrate the placement of the text.
     */
    private vtkActor2D createGrid()
    {
        // Set up the necessary points.
        vtkPoints pts = new vtkPoints();
        pts.InsertNextPoint(0.5, 0.0, 0.0);
        pts.InsertNextPoint(0.5, 1.0, 0.0);
        pts.InsertNextPoint(0.0, 0.5, 0.0);
        pts.InsertNextPoint(1.0, 0.5, 0.0);

        // Set up the lines that use these points.
        vtkCellArray lines = new vtkCellArray();
        lines.InsertNextCell(2);
        lines.InsertCellPoint(0);
        lines.InsertCellPoint(1);
        lines.InsertNextCell(2);
        lines.InsertCellPoint(2);
        lines.InsertCellPoint(3);

        // Create a grid that uses these points and lines.
        vtkPolyData grid = new vtkPolyData();
        grid.SetPoints(pts);
        grid.SetLines(lines);

        // Set up the coordinate system.
        vtkCoordinate normCoords = new vtkCoordinate();
        normCoords.SetCoordinateSystemToNormalizedViewport();
        
        // Set up the mapper and actor (2D); for the grid.
        vtkPolyDataMapper2D mapper = new vtkPolyDataMapper2D();
        mapper.SetInput(grid);
        mapper.SetTransformCoordinate(normCoords);

        vtkActor2D gridActor = new vtkActor2D();
        gridActor.SetMapper(mapper);
        gridActor.GetProperty().SetColor( 1, 1, 1 );
        return gridActor;
    }
    
    public static void main(String s[]) 
    {
        TextScaling panel = new TextScaling();
        
        JFrame frame = new JFrame("MultiLineText");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add("Center", panel);
        frame.pack();
        frame.setVisible(true);
    }
}




More information about the vtkusers mailing list