VTK Learning Twelve - VTK Label Two
Description
使用vtkVectorText
构建标签(Label
),将标签合并成一个vtkPolyData
进行渲染。
Code
#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<vtkRenderWindow.h>
#include<vtkRenderWindowInteractor.h>
#include<vtkRenderer.h>
#include<vtkProperty.h>
#include<vtkNamedColors.h>
#include<vtkAppendPolyData.h>
#include<vtkVectorText.h>
#include<vtkTransform.h>
#include<vtkTransformFilter.h>
int main(){
vtkSmartPointer<vtkPoints> ps = vtkSmartPointer<vtkPoints>::New();
unsigned int GridSize = 10;
vtkSmartPointer<vtkAppendPolyData> appendPolyData = vtkSmartPointer<vtkAppendPolyData>::New();
int count = 0;
for ( int x = 0; x < GridSize; x++)
{
for ( int y = 0; y < GridSize; y++)
{
double z = vtkMath::Random(-.25, .25);
ps->InsertNextPoint(x, y, z);
vtkNew<vtkVectorText> text;
std::string str("T" + std::to_string(count++));
text->SetText(str.c_str());
text->Update();
vtkNew<vtkTransform>transform;
transform->Translate(x, y, z);
transform->RotateX(90);
transform->Scale(.1, .1, .1);
vtkNew<vtkTransformFilter> filter;
filter->SetTransform(transform);
filter->SetInputData(text->GetOutput());
filter->Update();
appendPolyData->AddInputConnection(filter->GetOutputPort());
}
}
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);
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<vtkPolyDataMapper> textMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
textMapper->SetInputConnection(appendPolyData->GetOutputPort());
vtkSmartPointer<vtkActor> txActor = vtkSmartPointer<vtkActor>::New();
txActor->SetMapper(textMapper);
txActor->GetProperty()->SetColor(0.0, 0.0, 0.0);
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->AddActor(txActor);
vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New();
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})