官方教程:

#include <vtkSmartPointer.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkOrientationMarkerWidget.h>
#include <vtkDataSetMapper.h>
#include <vtkProperty.h>
#include <vtkActor.h>
#include <vtkPolyDataMapper.h>
#include <vtkSuperquadricSource.h>
 
int main (int argc, char *argv[] )
{
  // Parse command line arguments
  if(argc != 2)
    {
    std::cerr << "Usage: " << argv[0]
              << " Filename(.vtp)" << std::endl;
    return EXIT_FAILURE;
    }
 
  // Read the polydata for the icon
  vtkSmartPointer<vtkXMLPolyDataReader> reader =
    vtkSmartPointer<vtkXMLPolyDataReader>::New();
  reader->SetFileName(argv[1]);
 
  vtkSmartPointer<vtkDataSetMapper> iconMapper =
    vtkSmartPointer<vtkDataSetMapper>::New();
  iconMapper->SetInputConnection(reader->GetOutputPort());
 
  vtkSmartPointer<vtkActor> iconActor =
    vtkSmartPointer<vtkActor>::New();
  iconActor->SetMapper(iconMapper);
 
  // Set up the renderer, window, and interactor
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderer->SetBackground( 0.0980, 0.0980, 0.4392 );
 
  vtkSmartPointer<vtkRenderWindow> renWin =
    vtkSmartPointer<vtkRenderWindow>::New();
  renWin->AddRenderer( renderer );
  renWin->SetSize( 400, 400 );
 
  vtkSmartPointer<vtkRenderWindowInteractor> iren =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  iren->SetRenderWindow( renWin );
 
  // Set up the widget
  vtkSmartPointer<vtkOrientationMarkerWidget> widget =
    vtkSmartPointer<vtkOrientationMarkerWidget>::New();
  widget->SetOutlineColor( 0.9300, 0.5700, 0.1300 );
  widget->SetOrientationMarker( iconActor );
  widget->SetInteractor( iren );
  widget->SetViewport( 0.0, 0.0, 0.2, 0.2 );
  widget->SetEnabled( 1 );
  widget->InteractiveOn();
 
  // Create a superquadric
  vtkSmartPointer<vtkSuperquadricSource> superquadricSource = 
    vtkSmartPointer<vtkSuperquadricSource>::New();
  superquadricSource->SetPhiRoundness(.2);
  superquadricSource->SetThetaRoundness(.8);
 
  // Create a mapper and actor
  vtkSmartPointer<vtkPolyDataMapper> superquadricMapper = 
    vtkSmartPointer<vtkPolyDataMapper>::New();
  superquadricMapper->SetInputConnection(superquadricSource->GetOutputPort());
 
  vtkSmartPointer<vtkActor> superquadricActor = 
    vtkSmartPointer<vtkActor>::New();
  superquadricActor->SetMapper(superquadricMapper);
  superquadricActor->GetProperty()->SetInterpolationToFlat();
  superquadricActor->GetProperty()->SetDiffuseColor(0.93, 0.57, 0.13);
  superquadricActor->GetProperty()->SetSpecularColor(1.0, 1.0, 1.0);
  superquadricActor->GetProperty()->SetDiffuse(.6);
  superquadricActor->GetProperty()->SetSpecular(.5);
  superquadricActor->GetProperty()->SetSpecularPower(5.0);
 
  renderer->AddActor(superquadricActor);
  renderer->ResetCamera();
 
  renWin->Render();
 
  iren->Initialize();
 
  iren->Start();
 
  return EXIT_SUCCESS;
}  程序运行截图: 

第二个例子:

// Test for use of vtkOrientationMarkerWidget // #include "vtkCylinderSource.h" #include "vtkPolyDataMapper.h" #include "vtkActor.h" #include "vtkRenderer.h" #include "vtkRenderWindow.h" #include "vtkRenderWindowInteractor.h" #include "vtkProperty.h" #include "vtkCamera.h" #include "vtkOrientationMarkerWidget.h" #include "vtkAnnotatedCubeActor.h" #include "vtkAxesActor.h" #include "vtkAssembly.h" int main() { // This creates a polygonal cylinder model with eight circumferential facets. vtkCylinderSource *cylinder = vtkCylinderSource::New(); cylinder->SetResolution(8); // The mapper is responsible for pushing the geometry into the graphics // library. It may also do color mapping, if scalars or other attributes // are defined. vtkPolyDataMapper *cylinderMapper = vtkPolyDataMapper::New(); cylinderMapper->SetInputConnection(cylinder->GetOutputPort()); // The actor is a grouping mechanism: besides the geometry (mapper), it // also has a property, transformation matrix, and/or texture map. // Here we set its color and rotate it -22.5 degrees. vtkActor *cylinderActor = vtkActor::New(); cylinderActor->SetMapper(cylinderMapper); cylinderActor->GetProperty()->SetColor(1.0000, 0.3882, 0.2784); cylinderActor->RotateX(30.0); cylinderActor->RotateY(-45.0); // Create the graphics structure. The renderer renders into the // render window. The render window interactor captures mouse events // and will perform appropriate camera or actor manipulation // depending on the nature of the events. vtkRenderer *ren = vtkRenderer::New(); vtkRenderWindow *renWin = vtkRenderWindow::New(); renWin->AddRenderer(ren); vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New(); iren->SetRenderWindow(renWin); // Add the actors to the renderer, set the background and size ren->AddActor(cylinderActor); ren->SetBackground(0.1, 0.2, 0.4); renWin->SetSize(400, 400); // // Begin of add vtkOrientationMarkerWidget // // Add a vtkOrientationMarkerWidget vtkOrientationMarkerWidget* widget = vtkOrientationMarkerWidget::New(); vtkAnnotatedCubeActor* act1 = vtkAnnotatedCubeActor::New(); act1->SetFaceTextScale(0.5); vtkAxesActor* act2 = vtkAxesActor::New(); vtkAssembly* assemble = vtkAssembly::New(); assemble->AddPart(act1); assemble->AddPart(act2); widget->SetOrientationMarker(assemble); //widget->SetCurrentRenderer(ren); widget->SetInteractor(iren); widget->SetEnabled(1); widget->SetInteractive(0); widget->SetViewport(0., 0., 0.4, 0.4); // // End of add vtkOrientationMarkerWidget // // We'll zoom in a little by accessing the camera and invoking a "Zoom" // method on it. ren->ResetCamera(); ren->GetActiveCamera()->Zoom(1.5); renWin->Render(); // This starts the event loop and as a side effect causes an initial render. iren->Start(); // Exiting from here, we have to delete all the instances that // have been created. assemble->Delete(); act2->Delete(); act1->Delete(); widget->Delete(); cylinder->Delete(); cylinderMapper->Delete(); cylinderActor->Delete(); ren->Delete(); renWin->Delete(); iren->Delete(); return 0; }


程序运行截图: