VTK Learning Eleven - VTK Label One

VTK Learning Eleven - VTK Label One

Description

属性标签的显示。vtkTextProperty设置文字属性。vtkPointSetToLabelHierarchyvtkLabelPlacementMapper
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})

Result

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kLwejF0k-1586670128177)(images\1.png)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值