VTK笔记——绘制文本

VTK提供了两种方式用于文本绘制,分别用于二维和三维。二维显示是在三维图形窗口顶部绘制文本,通常会涉及的是在层叠平面上绘制。三维标注是创建三维的多边形数据文本,可以想三维图形对象一样进行变换和显示。

二维文本

绘制二维文本需要用到二维的actor(vtkActor2D或它的子类vtkTextActor)和mapper(vtkMapper2D或它的子类vtkTextMapper)。二维的actor和mapper和三维的类似,不同的是前者是在图形的顶层层叠平面上进行渲染的。
类及具体用法:

vtkTextActor

用vtkTextActor绘制二维文本,可以通过vtkTextProperty设置字体,文本颜色,粗体,斜体以及字体的阴影等效果。

auto textActor =
	vtkSmartPointer<vtkTextActor>::New();
textActor->SetInput(HELLO_TEXT);
textActor->SetPosition2(20, 40);
textActor->GetTextProperty()->SetColor(1.0, 0.0, 0.0);
textActor->GetTextProperty()->SetFontSize(32);
renderer->AddActor2D(textActor);

在这里插入图片描述

三维文本

三维文本绘制的实现是使用vtkVectorText创建文本字符串的多边形表达式,然后把它渲染到三维场景中。放置三维文本的类可以是vtkActor,也可是它的子类vktFollower。区别在于,vtkFollower对象总是面向渲染器当前的相机,因此这种文本总是可见的。

vtkVectorText

创建表达式:

auto textSource =
	vtkSmartPointer<vtkVectorText>::New();
textSource->SetText(HELLO_TEXT);

三维渲染:

vtkActor

用vtkActor渲染:

auto textActor =
	vtkSmartPointer<vtkActor>::New();
textActor->SetMapper(textMapper);
textActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
renderer->AddActor(textActor);

在这里插入图片描述

vtkFollower

用vtkFollower渲染:

auto textFollower =
	vtkSmartPointer<vtkFollower>::New();
textFollower->SetMapper(textMapper);
textFollower->GetProperty()->SetColor(1.0, 0.0, 0.0);
textFollower->SetCamera(renderer->GetActiveCamera());
renderer->AddActor(textFollower);

在这里插入图片描述

DrawText.cxx

#include <vtkSmartPointer.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkTextActor.h>
#include <vtkTextProperty.h>
#include <vtkVectorText.h>
#include <vtkFollower.h>
#include <vtkInteractorStyle.h>

const char* const HELLO_TEXT = "Hello world";

int main(int argc, char* argv[])
{
	if (argc < 2)
	{
		return EXIT_FAILURE;
	}

	auto sphereSource =
		vtkSmartPointer<vtkSphereSource>::New();
	sphereSource->SetRadius(3.0);
	sphereSource->SetCenter(0.0, 0.0, 0.0);

	auto sphereMapper =
		vtkSmartPointer<vtkPolyDataMapper>::New();
	sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
	auto sphereActor =
		vtkSmartPointer<vtkActor>::New();
	sphereActor->SetMapper(sphereMapper);
	sphereActor->GetProperty()->SetOpacity(0.3);

	auto renderer =
		vtkSmartPointer<vtkRenderer>::New();
	renderer->AddActor(sphereActor);
	auto renWindow =
		vtkSmartPointer<vtkRenderWindow>::New();
	renWindow->SetSize(400, 400);
	renWindow->AddRenderer(renderer);
	auto interactor =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	interactor->SetRenderWindow(renWindow);
	
	int dtype = atoi(argv[1]);
	if (dtype == 1)
	{
		auto textActor =
			vtkSmartPointer<vtkTextActor>::New();
		textActor->SetInput(HELLO_TEXT);
		textActor->SetPosition2(20, 40);
		textActor->GetTextProperty()->SetColor(1.0, 0.0, 0.0);
		textActor->GetTextProperty()->SetFontSize(24);
		renderer->AddActor2D(textActor);
	}
	else if (dtype == 2 || dtype == 3)
	{
		auto textSource =
			vtkSmartPointer<vtkVectorText>::New();
		textSource->SetText(HELLO_TEXT);
		auto textMapper =
			vtkSmartPointer<vtkPolyDataMapper>::New();
		textMapper->SetInputConnection(textSource->GetOutputPort());
		if (dtype == 2)
		{
			auto textActor =
				vtkSmartPointer<vtkActor>::New();
			textActor->SetMapper(textMapper);
			textActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
			renderer->AddActor(textActor);
		}
		else if (dtype == 3)
		{
			auto textFollower =
				vtkSmartPointer<vtkFollower>::New();
			textFollower->SetMapper(textMapper);
			textFollower->GetProperty()->SetColor(1.0, 0.0, 0.0);
			textFollower->SetCamera(renderer->GetActiveCamera());
			renderer->AddActor(textFollower);
		}
	}

	renWindow->Render();
	renderer->ResetCamera();
	renWindow->Render();
	interactor->Start();

	return EXIT_SUCCESS;
}

Ref

VTK/Examples/Cxx/Visualization/DrawText
VTK/Examples/Cxx/Visualization/VectorText
VTK/Examples/Cxx/Visualization/Follower
在这里插入图片描述

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
VTK(Visualization Toolkit)是一个用于可视化和图形处理的开源软件包,可用于创建各种光滑、渐变和复杂的3D图形。要使用VTK实现草图绘制,可以按照以下步骤进行: 1. 导入所需的VTK库: 通过在代码开始部分添加必要的VTK库导入语句,如“import vtk”来导入VTK相关的库函数。 2. 创建绘制窗口和渲染器: 使用vtkRenderWindow类创建一个绘制窗口,并使用vtkRenderer类创建一个渲染器对象。将渲染器与绘制窗口相关联,以便可以在窗口中显示渲染结果。 3. 创建草图绘制工具: 使用VTK的一些基本几何图元(如线段、圆等)和交互操作类来创建绘制工具。例如,可以使用vtkLineSource类创建直线,使用vtkCircleSource类创建圆等。 4. 连接绘制工具和渲染器: 将绘制工具添加到渲染器中,以便可以在渲染器中显示绘制的草图。使用vtkActor类将绘制工具与渲染器关联起来,并设置绘制工具在渲染器中显示的属性,例如颜色和线宽等。 5. 设置交互操作: 使用vtkRenderWindowInteractor类创建一个交互操作对象,以便用户可以与绘制窗口进行交互。例如,用户可以使用鼠标或触摸屏输入来绘制草图。 6. 更新渲染: 在设置好交互操作后,调用vtkRenderWindow类的Render方法,更新渲染器并在绘制窗口中显示草图。 通过上述步骤,就可以使用VTK实现草图绘制了。当用户在绘制窗口中进行绘制时,草图绘制工具会生成相应的几何图元,并在渲染器中显示出来。用户可以根据需要修改绘制结果,也可以保存草图为文件,以便后续使用或分析。绘制过程中,交互操作还可以提供一些功能,如平移、缩放和旋转等操作,以便更方便地绘制草图。总之,使用VTK实现草图绘制可以为用户提供一个简单易用、灵活多样的绘图工具。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值