Skip to content

OrientationMarkerWidget

Repository source: OrientationMarkerWidget

Description

vtkOrientationMarkerWidget object provides support for interactively manipulating the position, size, and apparent orientation of a prop that represents an orientation marker.

This class works by adding its internal renderer to an external "parent" renderer on a different layer. The input orientation marker is rendered as an overlay on the parent renderer and, thus, appears superposed over all props in the parent's scene.

The camera view of the orientation the marker is made to match that of the parent's by means of an observer mechanism, giving the illusion that the orientation of the marker reflects that of the prop(s) in the parent's scene.

The widget listens to left mouse button and mouse movement events. It will change the cursor shape based on its location. If the cursor is over the overlay renderer, it will change the cursor shape to a SIZEALL shape or to a resize corner shape (e.g., SIZENW) if the cursor is near a corner. If the left mouse button is pressed and held down while moving, the overlay renderer, and hence, the orientation marker, is resized or moved. In the case of a resize operation, releasing the left mouse button causes the widget to enforce its renderer to be square. The diagonally opposite corner to the one moved is repositioned such that all edges of the renderer have the same length: the minimum.

Other languages

See (Cxx), (Python)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

OrientationMarkerWidget.java

import vtk.vtkNativeLibrary;
import vtk.vtkRenderWindow;
import vtk.vtkRenderWindowInteractor;
import vtk.vtkRenderer;
import vtk.vtkActor;
import vtk.vtkNamedColors;
import vtk.vtkPolyDataMapper;
import vtk.vtkXMLPolyDataReader;
import vtk.vtkDataSetMapper;
import vtk.vtkOrientationMarkerWidget;
import vtk.vtkSuperquadricSource;


public class OrientationMarkerWidget 
{
  // -----------------------------------------------------------------
  // Load VTK library and print which library was not properly loaded
  static 
  {
    if (!vtkNativeLibrary.LoadAllNativeLibraries()) 
    {
      for (vtkNativeLibrary lib : vtkNativeLibrary.values()) 
      {
        if (!lib.IsLoaded()) 
        {
          System.out.println(lib.GetLibraryName() + " not loaded");
        }
      }
    }
    vtkNativeLibrary.DisableOutputWindow(null);
  }
  // -----------------------------------------------------------------

  public static void main(String args[]) 
  {

    //parse command line arguments
    if (args.length != 1) 
    {
      System.err.println("Usage: java -classpath ... Filename(.vtp) e.g Bunny.vtp");
      return;
    }

    vtkNamedColors Color = new vtkNamedColors(); 

    //For icon Actor Color
    double iconActorColor[] = new double[4];
    //For superquadricActor Color
    double superquadricActorColor[] = new double[4];
    //Renderer Background Color
    double BgColor[] = new double[4];

    //Change Color Name to Use your own Color for Change icon Actor Color
    Color.GetColor("Silver",iconActorColor);
    //Change Color Name to Use your own Color for Change icon superquadricActorColor
    Color.GetColor("Carrot",superquadricActorColor);
    //Change Color Name to Use your own Color for Renderer Background
    Color.GetColor("SlateGray",BgColor);

    //Read the polydata for the icon
    vtkXMLPolyDataReader reader =  new vtkXMLPolyDataReader();
    reader.SetFileName(args[0]);

    vtkDataSetMapper iconMapper = new vtkDataSetMapper();
    iconMapper.SetInputConnection(reader.GetOutputPort());

    vtkActor iconActor = new vtkActor();
    iconActor.SetMapper(iconMapper);
    iconActor.GetProperty().SetColor(iconActorColor);

    //Create the renderer, render window and interactor.
    vtkRenderer ren = new vtkRenderer();
    vtkRenderWindow renWin = new vtkRenderWindow();
    renWin.AddRenderer(ren);
    vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor();
    iren.SetRenderWindow(renWin);

    //Set up the widget
    vtkOrientationMarkerWidget widget = new vtkOrientationMarkerWidget();
    widget.SetOrientationMarker( iconActor );
    widget.SetInteractor( iren );
    widget.SetViewport( 0.0, 0.0, 0.2, 0.2 );
    widget.SetEnabled( 1 );
    widget.InteractiveOn();

    //Create a superquadric
    vtkSuperquadricSource superquadricSource = new vtkSuperquadricSource();
    superquadricSource.SetPhiRoundness(0.2);
    superquadricSource.SetThetaRoundness(0.8);

    //Create a mapper and actor
    vtkPolyDataMapper superquadricMapper = new vtkPolyDataMapper();
    superquadricMapper.SetInputConnection(superquadricSource.GetOutputPort());

    vtkActor superquadricActor = new vtkActor();
    superquadricActor.SetMapper(superquadricMapper);
    superquadricActor.GetProperty().SetInterpolationToFlat();
    superquadricActor.GetProperty().SetDiffuseColor(superquadricActorColor);
    superquadricActor.GetProperty().SetSpecularColor(1.0, 1.0, 1.0);
    superquadricActor.GetProperty().SetDiffuse(.6);
    superquadricActor.GetProperty().SetSpecular(.5);
    superquadricActor.GetProperty().SetSpecularPower(5.0);

    ren.AddActor(superquadricActor);
    ren.SetBackground(BgColor);
    ren.ResetCamera();

    renWin.SetSize(300,300);
    renWin.Render();

    iren.Initialize();
    iren.Start();

  }
}