VTk显示样式设置



显示样式

  在vtk中,显示样式是通过vtkProperty来控制的。这里介绍三种基本的属性设置方式:点方式,网格方式和面方式。其设置方法为

  • actor->GetProperty()->SetRepresentationToPoints()
  • actor->GetProperty()->SetRepresentationToWireframe()
  • actor->GetProperty()->SetRepresentationToSurface()

示例说明

CMakeLists.txt文件代码如下:

CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT( Representation )
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
ADD_EXECUTABLE(Representation Representation.cpp)
TARGET_LINK_LIBRARIES(Representation ${VTK_LIBRARIES})

Representation.cpp文件代码如下:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

int main(int argc, char *argv[])
{
    vtkSmartPointer coneSource =
        vtkSmartPointer::New();
    coneSource->Update();

    vtkSmartPointer cone = coneSource->GetOutput();
    int nPoints = cone->GetNumberOfPoints();
    int nCells  = cone->GetNumberOfCells();

    std::cout<<"Points number:"<std::endl;
    std::cout<<"Cells number:"<std::endl;

    vtkSmartPointer mapper =
        vtkSmartPointer::New();
    mapper->SetInputData(cone);

    vtkSmartPointer actor =
        vtkSmartPointer::New();
    actor->SetMapper(mapper);
    actor->GetProperty()->SetColor(1.0, 0, 0);  //设置颜色属性
    actor->GetProperty()->SetRepresentationToPoints();      //点方式
    //actor->GetProperty()->SetRepresentationToWireframe(); //网格方式
    //actor->GetProperty()->SetRepresentationToSurface(); //默认设置,面方式

    vtkSmartPointer renderer =
        vtkSmartPointer::New();
    renderer->AddActor(actor);
    renderer->SetBackground(0.0,0.0,0.0);

    vtkSmartPointer renderWindow =
        vtkSmartPointer::New();
    renderWindow->AddRenderer(renderer);
    renderWindow->SetSize( 640, 480 );
    renderWindow->Render();
    renderWindow->SetWindowName("Representation");

    vtkSmartPointer renderWindowInteractor =
        vtkSmartPointer::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderWindow->Render();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

运行结果:

这里写图片描述

这里写图片描述

这里写图片描述