VTK Learning Eleven - VTK Label One
Description
属性标签的显示。vtkTextProperty
设置文字属性。vtkPointSetToLabelHierarchy
、vtkLabelPlacementMapper
和vtkActor2D
渲染文字。
Code
#include<vtkRenderWindow.h>
#include<vtkSmartPointer.h>
#include<vtkPoints.h>
#include<vtkMath.h>
#include<vtkPolyData.h>
#include<vtkCellArray.h>
#include<vtkVertex.h>
#include<vtkPolyDataMapper.h>
#include<vtkActor.h>
#include<vtkRenderWindowInteractor.h>
#include<vtkRenderer.h>
#include<vtkProperty.h>
#include<vtkNamedColors.h>
#include<vtkStringArray.h>
#include<vtkPointData.h>
#include<vtkTextProperty.h>
#include<vtkPointSetToLabelHierarchy.h>
#include<vtkLabelPlacementMapper.h>
#include<vtkActor2D.h>
int main(){
vtkSmartPointer<vtkPoints> ps = vtkSmartPointer<vtkPoints>::New();
unsigned int GridSize = 10;
vtkSmartPointer<vtkStringArray>names = vtkSmartPointer<vtkStringArray>::New();
names->SetName("names");
int count = 0;
for ( int x = 0; x < GridSize; x++)
{
for ( int y = 0; y < GridSize; y++)
{
ps->InsertNextPoint(x, y, vtkMath::Random(-.25, .25));
names->InsertNextValue("T"+ std::to_string(count++));
}
}
vtkSmartPointer<vtkVertex> vertex =vtkSmartPointer<vtkVertex>::New();
vertex->GetPointIds()->SetNumberOfIds(ps->GetNumberOfPoints());
for (int i = 0; i<ps->GetNumberOfPoints(); i++) {
vertex->GetPointIds()->SetId(i, i);
}
vtkSmartPointer<vtkCellArray> vertices =
vtkSmartPointer<vtkCellArray>::New();
vertices->InsertNextCell(vertex);
vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New();
polydata->SetPoints(ps);
polydata->SetVerts(vertices);
polydata->GetPointData()->AddArray(names);
vtkSmartPointer<vtkTextProperty>textProp = vtkSmartPointer<vtkTextProperty>::New();
textProp->SetFontSize(20);
textProp->SetColor(0, 0, 0);
vtkSmartPointer<vtkPointSetToLabelHierarchy>labels = vtkSmartPointer<vtkPointSetToLabelHierarchy>::New();
labels->SetInputData(polydata);
labels->SetLabelArrayName("names");
labels->SetTargetLabelCount(polydata->GetNumberOfPoints());
labels->SetTextProperty(textProp);
vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New();
vtkSmartPointer<vtkPolyDataMapper> pointMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
pointMapper->SetInputData(polydata);
vtkSmartPointer<vtkActor> pointActor = vtkSmartPointer<vtkActor>::New();
pointActor->GetProperty()->SetColor(1, 0, 0);
pointActor->GetProperty()->SetPointSize(5);
pointActor->SetMapper(pointMapper);
vtkSmartPointer<vtkLabelPlacementMapper> placeMapper = vtkSmartPointer<vtkLabelPlacementMapper>::New();
placeMapper->SetInputConnection(labels->GetOutputPort());
vtkSmartPointer<vtkActor2D> txActor = vtkSmartPointer<vtkActor2D>::New();
txActor->SetMapper(placeMapper);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
renderer->AddActor(pointActor);
renderer->AddActor2D(txActor);
renderer->SetBackground(colors->GetColor3d("Mint").GetData());
renderWindow->Render();
renderWindowInteractor->Initialize();
renderWindowInteractor->Start();
return 0;
}
CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 3.3 FATAL_ERROR)
PROJECT(Text)
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
ADD_EXECUTABLE(Text Text.cxx)
TARGET_LINK_LIBRARIES(Text ${VTK_LIBRARIES})