VTK显示中文

博客介绍了VTK中文显示相关内容。vtk 5.x版本显示中文困难,需修改底层代码,而从vtk 6.1开始,中文支持问题不大,关键是文字用utf - 8格式,对vtkTextProperty指定中文字体文件,还给出示例代码重点演示中文显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基本概念

        vtk 5.x版本显示中文是一个比较困难的问题,基本上需要修改底层代码。从vtk 6.1开始,其实中文支持已经不是大问题了。VTK开始支持使用指定的ttf字体文件进行显示了。关键是两个问题:文字使用utf-8格式,比如textActor.SetInput();对vtkTextProperty直接指定使用中文字体文件,比如,simhei.ttf 。例如

vtkNew<vtkTextProperty> tprop;
tprop->SetFontFamily(VTK_FONT_FILE); 
tprop->SetFontFile(“/path/to/some/font/file.ttf”); 

示例演示

        我们绘制不同要求的文字,重点演示如何显示中文。代码如下。

/**********************************************************************

Copyright (c) Mr.Bin. All rights reserved.
For more information visit: http://blog.youkuaiyun.com/webzhuce

**********************************************************************/
#include "vtkTextRenderer.h"
#include "vtkNew.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkStdString.h"
#include "vtkTextActor.h"
#include "vtkTextProperty.h"
#include "vtkSmartPointer.h"
#include "vtkUnicodeString.h"
#include <windows.h>
#include <iostream>
#include <string>

int main(int argc, char *argv[])
{
	if (argc < 2)
	{
		cerr << "Missing font filename." << endl;
		return EXIT_FAILURE;
	}

	std::string uncodeFontFile(argv[1]);

	vtkSmartPointer<vtkTextRenderer> tren = vtkSmartPointer<vtkTextRenderer>::New();
	if (tren == nullptr)
	{
		std::cerr << "Object factory cannot find vtkTextRenderer override.\n";
		return EXIT_FAILURE;
	}

	if (strcmp(tren->GetClassName(), "vtkMathTextFreeTypeTextRenderer") != 0)
	{
		std::cerr << "Object factory returning unrecognized vtkTextRenderer "
			"override: " << tren->GetClassName() << std::endl;
		return EXIT_FAILURE;
	}

	std::string str = "Sample multiline\ntext rendered\nusing FreeTypeTools.";
	vtkSmartPointer<vtkTextActor> actor1 = vtkSmartPointer<vtkTextActor>::New();
	actor1->GetTextProperty()->SetFontSize(20);
	actor1->GetTextProperty()->SetColor(1.0, 0.0, 0.0);
	actor1->GetTextProperty()->SetJustificationToLeft();
	actor1->GetTextProperty()->SetVerticalJustificationToTop();
	actor1->GetTextProperty()->SetFontFamilyToTimes();
	actor1->SetInput(str.c_str());
	actor1->SetPosition(10, 590);

	vtkSmartPointer<vtkTextActor> actor2 = vtkSmartPointer<vtkTextActor>::New();
	actor2->GetTextProperty()->SetFontSize(20);
	actor2->GetTextProperty()->SetColor(0.0, 1.0, 0.0);
	actor2->GetTextProperty()->SetJustificationToRight();
	actor2->GetTextProperty()->SetVerticalJustificationToTop();
	actor2->GetTextProperty()->SetFontFamilyToCourier();
	actor2->SetInput(str.c_str());
	actor2->SetPosition(590, 590);

	vtkSmartPointer<vtkTextActor> actor3 = vtkSmartPointer<vtkTextActor>::New();
	actor3->GetTextProperty()->SetFontSize(20);
	actor3->GetTextProperty()->SetColor(0.0, 0.0, 1.0);
	actor3->GetTextProperty()->SetJustificationToLeft();
	actor3->GetTextProperty()->SetVerticalJustificationToBottom();
	actor3->GetTextProperty()->SetItalic(1);
	actor3->SetInput(str.c_str());
	actor3->SetPosition(10, 10);

	vtkSmartPointer<vtkTextActor> actor4 = vtkSmartPointer<vtkTextActor>::New();
	actor4->GetTextProperty()->SetFontSize(20);
	actor4->GetTextProperty()->SetColor(0.3, 0.4, 0.5);
	actor4->GetTextProperty()->SetJustificationToRight();
	actor4->GetTextProperty()->SetVerticalJustificationToBottom();
	actor4->GetTextProperty()->SetBold(1);
	actor4->GetTextProperty()->SetShadow(1);
	actor4->GetTextProperty()->SetShadowOffset(-3, 2);
	actor4->SetInput(str.c_str());
	actor4->SetPosition(590, 10);

	vtkSmartPointer<vtkTextActor> actor5 = vtkSmartPointer<vtkTextActor>::New();
	actor5->GetTextProperty()->SetFontSize(20);
	actor5->GetTextProperty()->SetColor(1.0, 1.0, 0.0);
	actor5->GetTextProperty()->SetJustificationToCentered();
	actor5->GetTextProperty()->SetVerticalJustificationToCentered();
	actor5->GetTextProperty()->SetBold(1);
	actor5->GetTextProperty()->SetItalic(1);
	actor5->GetTextProperty()->SetShadow(1);
	actor5->GetTextProperty()->SetShadowOffset(5, -8);
	actor5->SetInput(str.c_str());
	actor5->SetPosition(300, 300);

	vtkSmartPointer<vtkTextActor> actor6 = vtkSmartPointer<vtkTextActor>::New();
	actor6->GetTextProperty()->SetFontSize(16);
	actor6->GetTextProperty()->SetColor(1.0, 0.5, 0.2);
	actor6->GetTextProperty()->SetJustificationToCentered();
	actor6->GetTextProperty()->SetVerticalJustificationToCentered();
	actor6->GetTextProperty()->SetOrientation(45);
	actor6->SetInput(str.c_str());
	actor6->SetPosition(300, 450);

	vtkSmartPointer<vtkTextActor> actor7 = vtkSmartPointer<vtkTextActor>::New();
	actor7->GetTextProperty()->SetFontSize(16);
	actor7->GetTextProperty()->SetColor(0.5, 0.2, 1.0);
	actor7->GetTextProperty()->SetJustificationToLeft();
	actor7->GetTextProperty()->SetVerticalJustificationToCentered();
	actor7->GetTextProperty()->SetOrientation(45);
	actor7->SetInput(str.c_str());
	actor7->SetPosition(100, 156);

	vtkSmartPointer<vtkTextActor> actor8 = vtkSmartPointer<vtkTextActor>::New();
	actor8->GetTextProperty()->SetFontSize(16);
	actor8->GetTextProperty()->SetColor(0.8, 1.0, 0.3);
	actor8->GetTextProperty()->SetJustificationToRight();
	actor8->GetTextProperty()->SetVerticalJustificationToCentered();
	actor8->GetTextProperty()->SetOrientation(45);
	actor8->SetInput(str.c_str());
	actor8->SetPosition(500, 249);

	// Mathtext tests

	// Test that escaped "$" are passed through to freetype:
	vtkSmartPointer<vtkTextActor> actor9 = vtkSmartPointer<vtkTextActor>::New();
	actor9->GetTextProperty()->SetFontSize(12);
	actor9->GetTextProperty()->SetColor(0.2, 0.5, 1.0);
	actor9->SetInput("Escaped dollar signs:\n\\$10, \\$20");
	actor9->SetPosition(100, 450);

	vtkSmartPointer<vtkTextActor> actor10 = vtkSmartPointer<vtkTextActor>::New();
	actor10->GetTextProperty()->SetFontSize(16);
	actor10->GetTextProperty()->SetColor(0.5, 0.2, 1.0);
	actor10->GetTextProperty()->SetJustificationToRight();
	actor10->GetTextProperty()->SetOrientation(45);
	actor10->SetInput("Test MathText $\\int_0^\\infty\\frac{2\\pi}"
		"{x - \\frac{z}{4}}\\,dx$");
	actor10->SetPosition(588, 433);

	// Invalid latex markup -- should fallback to freetype.
	vtkSmartPointer<vtkTextActor> actor11 = vtkSmartPointer<vtkTextActor>::New();
	actor11->GetTextProperty()->SetFontSize(15);
	actor11->GetTextProperty()->SetColor(1.0, 0.5, 0.2);
	actor11->SetInput("Test FreeType fallback:\n$\\asdf$");
	actor11->SetPosition(10, 350);

	// Both $...$ and \\$
	vtkSmartPointer<vtkTextActor> actor12 = vtkSmartPointer<vtkTextActor>::New();
	actor12->GetTextProperty()->SetFontSize(18);
	actor12->GetTextProperty()->SetColor(0.0, 1.0, 0.7);
	actor12->SetInput("Test MathText '\\$' $\\$\\sqrt[3]{8}$");
	actor12->SetPosition(10, 300);

	// $...$ without other text.
	vtkSmartPointer<vtkTextActor> actor13 = vtkSmartPointer<vtkTextActor>::New();
	actor13->GetTextProperty()->SetFontSize(18);
	actor13->GetTextProperty()->SetColor(0.2, 1.0, 1.0);
	actor13->SetInput("$A = \\pi r^2$");
	actor13->SetPosition(10, 250);

	// Numbers, using courier, Text that gets 'cut off'
	vtkSmartPointer<vtkTextActor> actor14 = vtkSmartPointer<vtkTextActor>::New();
	actor14->GetTextProperty()->SetFontSize(21);
	actor14->GetTextProperty()->SetColor(1.0, 0.0, 0.0);
	actor14->GetTextProperty()->SetJustificationToCentered();
	actor14->GetTextProperty()->SetVerticalJustificationToCentered();
	actor14->GetTextProperty()->SetBold(1);
	actor14->GetTextProperty()->SetItalic(1);
	actor14->GetTextProperty()->SetFontFamilyToCourier();
	actor14->SetInput("4.0");
	actor14->SetPosition(500, 400);

	// UTF-8 freetype handling:
	vtkSmartPointer<vtkTextActor> actor15 = vtkSmartPointer<vtkTextActor>::New();
	actor15->GetTextProperty()->SetFontFamily(VTK_FONT_FILE);
	actor15->GetTextProperty()->SetFontFile(uncodeFontFile.c_str());
	actor15->GetTextProperty()->SetJustificationToCentered();
	actor15->GetTextProperty()->SetVerticalJustificationToCentered();
	actor15->GetTextProperty()->SetFontSize(18);
	actor15->GetTextProperty()->SetColor(0.0, 1.0, 0.7);
	actor15->SetInput(u8"UTF-8 FreeType: 显示中文\n换行显示");
	actor15->SetPosition(300, 110);

	// Boring rendering setup....

	vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();
	ren->SetBackground(0.1, 0.1, 0.1);
	vtkSmartPointer<vtkRenderWindow> win = vtkSmartPointer<vtkRenderWindow>::New();
	win->SetSize(600, 600);
	win->AddRenderer(ren);
	vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
	iren->SetRenderWindow(win);

	ren->AddActor(actor1);
	ren->AddActor(actor2);
	ren->AddActor(actor3);
	ren->AddActor(actor4);
	ren->AddActor(actor5);
	ren->AddActor(actor6);
	ren->AddActor(actor7);
	ren->AddActor(actor8);
	ren->AddActor(actor9);
	ren->AddActor(actor10);
	ren->AddActor(actor11);
	ren->AddActor(actor12);
	ren->AddActor(actor13);
	ren->AddActor(actor14);
	ren->AddActor(actor15);

	win->SetMultiSamples(0);
	win->Render();
	win->GetInteractor()->Initialize();
	win->GetInteractor()->Start();

	return EXIT_SUCCESS;
}

运行结果

在这里插入图片描述

参考资料

VTK用户指南 版本4.0 William J. Schroeder 1998-2000 第一部分 VTK 介绍 第1章 欢迎 机构-----------------------------------------------------------------------------------------------8 怎样使用VTK----------------------------------------------------------------------------------8 附加资源-----------------------------------------------------------------------------------------8 第2章 安装 2.1 概述-----------------------------------------------------------------------------------------------9 2.2 安装VTK到Windows9x/NT/ME/2000/XP------------------------------------------------9 二进制安装-------------------------------------------------------------------------------------9 源代码安装-------------------------------------------------------------------------------------9 2.3 安装VTK到Unix操作系统 源代码安装------------------------------------------------------------------------------------10 运行CMake------------------------------------------------------------------------------------11 编译源代码 建立VTK多平台 安装VTK 第3章 系统概述 3.1 系统设计---------------------------------------------------------------------------------------12 图形模型--------------------------------------------------------------------------------------13 可视化模型-----------------------------------------------------------------------------------15 3.2 创建一个应用---------------------------------------------------------------------------------19 用户方法、对象和命令--------------------------------------------------------------------19 Tcl----------------------------------------------------------------------------------------------19 C++---------------------------------------------------------------------------------------------20 Java Phthon Visual Basic/COM/ActiveX 3.3 在两种语言间转换 第二部分 通过例子学习VTK 第4章 基础 4.1 创建1个简单的模型-------------------------------------------------------------------------24 程序化源对象---------------------------------------------------------------------------------24 读取源对象------------------------------------------------------------------------------------26 4.2 使用VTK交互器-----------------------------------------------------------------------------27 vtk绘制窗口交互器 交互风格 4.3 滤波数据---------------------------------------------------------------------------------------29 4.4 控制相机---------------------------------------------------------------------------------------30 安装相机 简单操作方法 控制视角方向 透视与正交视 保存与恢复相机状态 4.5 控制光线---------------------------------------------------------------------------------------32 位置光 4.6 控制3D道具-----------------------------------------------------------------------------------32 指定vtk道具3D位置 演员 演员的详细级 装配 体 vtk装载3D道具 4.7 作用纹理---------------------------------------------------------------------------------------37 4.8 拾取---------------------------------------------------------------------------------------------38 vtk装配路线 例子 4.9 vtk坐标和坐标系---------------------------------------------------------------------------40 4.10 控制vtk演员2D----------------------------------------------------------------------------41 4.11 注释--------------------------------------------------------------------------------------------41 2D注释 3D注释和vtk跟踪 4.12 特殊绘图类-----------------------------------------------------------------------------------44 尺度棒 X-Y绘制 边界盒轴 标记数据 4.13 变换数据--------------------------------------------------------------------------------------48 高级变换 第5章 可视化技术 5.1 可视化VTK数据集vtkDataSet(和子类) -------------------------------------------------50 使用数据属性进行工作 颜色映射 轮廓化 浮雕化 流线图 流线表面 剪裁 融合数据 附加数据 用另外一个尺度给等值面赋颜色 抽取单元格子集 抽取单元格作为多边形数据 5.2 可视化多边形数据---------------------------------------------------------------------------67 手工产生多边形数据 产生表面当量 十比一抽取 平滑网格 粘贴数据 产生纹理坐标 5.3 可视化结构网格-----------------------------------------------------------------------------74 手工产生结构化网格 抽取计算平面 结构网格子样化 5.4 可视化直线网格-----------------------------------------------------------------------------76 手工产生VTK直线网格 抽取计算平面 5.5 可视化非结构网格--------------------------------------------------------------------------77 手工产生VTK非结构网格 抽取部分网格 非结构网格轮廓化 第6章 可视化图像和体数据 6.1 VTK结构化点的历史表示-----------------------------------------------------------------80 6.2 手工产生VTK图像数据-------------------------------------------------------------------80 6.3 抽取图像数据子样--------------------------------------------------------------------------81 6.4 基于尺度值的弯曲--------------------------------------------------------------------------83 6.5 图像显示--------------------------------------------------------------------------------------83 图像观察者 图像演员 6.6 图像源-----------------------------------------------------------------------------------------85 2D帆布图像源 3D椭圆体图像源 高斯图像源 网格图像源 噪声图像源 正弦曲线源 6.7 图像处理--------------------------------------------------------------------------------------88 梯度化 高斯平滑 直方图 图像逻辑 重新切片 6.8 体绘制-----------------------------------------------------------------------------------------92 一个简单的例子 为什么会有多种体绘制技术? 产生一个VTK体 使用片层化函数 使用颜色变换函数 在一个体属性中控制颜色和透明度 在一个体属性中控制阴影 产生一个体映射 裁剪一个体 粘贴一个体 对一个体应用3D纹理 控制标准编码 体素光线计算 2D纹理映射 VolumePro绘制硬件 速度和精确度交替使用 使用vtkLODProp3D改善性能 可行性/局限性技术 第7章 建立模型 7.1 隐模型----------------------------------------------------------------------------------------114 定义隐函数 对隐函数进行抽样 7.2 挤压-------------------------------------------------------------------------------------------117 7.3 构建表面-------------------------------------------------------------------------------------119 Delaunay三角形化 高斯油彩 无组织点产生表面 第三部分 VTK研发者指南 第8章 数据接口和其他 8.1 读入器----------------------------------------------------------------------------------------130 多边形数据读入器 图像和体素读入器 数据集读入器 结构化网格读入器 线性网格读入器 非结构化网格读入器 8.2 写入器----------------------------------------------------------------------------------------131 多边形数据读入器 图像和体素读入器 结构化网格读入器 线性网格读入器 非结构化网格读入器 8.3 输入者----------------------------------------------------------------------------------------132 8.4 输出者----------------------------------------------------------------------------------------132 8.5 创建硬拷贝----------------------------------------------------------------------------------132 保存图像 保存大(高分辨率)图像 8.6 产生动画(使用样条) -----------------------------------------------------------------------134 8.7 使用现场数据工作--------------------------------------------------------------------------136 第9章 贡献编码 9.1 编码补偿--------------------------------------------------------------------------------------141 为VTK贡献编码的条件 编码风格 如何贡献编码 9.2 标准方法: 创建和消除对象---------------------------------------------------------------142 9.3 拷贝对象和受保护的方法------------------------------------------------------------------143 9.4 写一个VTK类: 综述-----------------------------------------------------------------------144 找到一个相似类 识别一个超类 单个类Per.h 文件 必需的方法 文档编码 使用SetGet宏 向VTK中添加类 9.5 对象工厂--------------------------------------------------------------------------------------145 综述 如何写一个工厂 如何安装一个工厂 例子工厂 第10章 流水线执行管理 10.1 执行过程--------------------------------------------------------------------------------------151 概述和术语 更新信息通道 传播更新扩展通道 触发异步更新通道 更新数据通道 10.2 使用流---------------------------------------------------------------------------------------162 第11章 VTK数据对象接口 11.1 数据组---------------------------------------------------------------------------------------166 方法 11.2 数据集---------------------------------------------------------------------------------------169 11.3 VTK数据集接口---------------------------------------------------------------------------170 方法 例子 11.4 VTK图像数据接口-----------------------------------------------------------------------174 方法 例子 11.5 VTK点集接口-----------------------------------------------------------------------------176 方法 例子 11.6 VTK结构化网格接口---------------------------------------------------------------------178 方法 例子 11.7 VTK线性网格接口-----------------------------------------------------------------------178 方法 例子 11.8 VTK多边形数据接口---------------------------------------------------------------------179 方法 例子 11.9 VTK非结构化网格接口-----------------------------------------------------------------184 方法 例子 11.10 单元格接口(VTK单元格子类) ------------------------------------------------------185 11.11 其他接口----------------------------------------------------------------------------------187 点 单元格数组 单元格类型 单元格连接 11.12 现场和属性数据接口------------------------------------------------------------------193 现场数据方法 数据集属性方法 第12章 如何写一个过程方法 12.1 概述----------------------------------------------------------------------------------------196 永远不要修改输入数据 参考计数数据 使用Debug宏 回收/删除截入的内在 修改时间 过程事件和异常终止执行 12.2 如何写一个绘图过滤器---------------------------------------------------------------199 概述 简单过滤器 复杂过滤器和流水线执行 抽取绘图过滤器 程序过滤器 重载流水执行方法 12.3 如何写一个图像过滤器---------------------------------------------------------------210 实现一个图像过滤器 第13章 用窗口系统集成 13.1 绘制窗口交互风格--------------------------------------------------------------------------216 13.2 GUI交互的总指导线------------------------------------------------------------------------217 13.3 X Window, Xt, and Motif--------------------------------------------------------------------221 13.4 MS Windows/Microsoft Foundation Classes---------------------------------------------226 13.5 Tcl/Tk-------------------------------------------------------------------------------------------227 13.6 Java 第14章 编码资源 14.1 对象图表--------------------------------------------------------------------------------------230 基础 单元格 数据集 流水线 源 过滤器 映射器 图形 体绘制 成像 OpenGL绘制器 拾取 变换塔形结构 14.2 过滤器总结-----------------------------------------------------------------------------------237 可视化过滤器 映射者对象 演员对象 14.3 VTK文件格式--------------------------------------------------------------------------------244 二进制文件 数据集属性格式 例子 第15章 光盘 15.1 源代码 15.2 例子代码 15.3 Window 9x/NT/ME/2000/XP 预编译二进制 15.4 数据 15.5 文档 15.6 退化测试图像 15.7 Kitware 应用
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值