其他分享
首页 > 其他分享> > VTK-小例子-圆锥体

VTK-小例子-圆锥体

作者:互联网

圆锥体

文章目录

前言

使用VTK构建一个圆锥体展示;
执行的具体步骤为:
1.创建一个vtkConeSoure类的实例cone;
表示一个圆锥(正棱锥,有棱数目),有三个属性:高度、半径、分辨率(棱数目);
2.创建一个映射器,vtkPolyDataMapper实例;
cone的输出可以作为映射器的输入,
vtk中使用GetOutputPort()作为输出;
VTK常见使用方法:将一个对象的输出当另一个对象的输入;
常见用来作为数据流的处理:将多个对象串联在一起形成一条链,称之为可视化流水线;
3.渲染部分
通过方法SetMapper将vtkActor对象和映射器关联在一起;之后渲染过程暂时省略,在以后的部分单独说明;

一、代码

#include "vtkConeSource.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkAutoInit.h" 
#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);
int main()
{
	vtkConeSource *cone = vtkConeSource::New();
	cone->SetHeight(3.0);
	cone->SetRadius(1.0);
	cone->SetResolution(10);

	vtkPolyDataMapper *coneMapper = vtkPolyDataMapper::New();
	coneMapper->SetInputConnection(cone->GetOutputPort());

	vtkActor *coneActor = vtkActor::New();
	coneActor->SetMapper(coneMapper);

	vtkRenderer *ren1 = vtkRenderer::New();
	ren1->AddActor(coneActor);
	ren1->SetBackground(0.1, 0.2, 0.4);

	vtkRenderWindow *renWin = vtkRenderWindow::New();
	renWin->AddRenderer(ren1);
	renWin->SetSize(300, 300);

	for (size_t i = 0; i < 360; i++)
	{
		renWin->Render();
	}

	cone->Delete();
	coneMapper->Delete();
	coneActor->Delete();
	ren1->Delete();
	renWin->Delete();
}

1.1流程

vtkConeSource vtkPolyDataMapper vtkActor vtkRenderer vtkRenderWindow

二、遇到的问题

1.运行时异常

按照《医学图像编程技术》的代码录入编译成功后,执行是出现异常:
在这里插入图片描述
VTK的错误调试日志窗口显示如下:
在这里插入图片描述
网上找到的回答为https://stackoverflow.com/questions/18642155/no-override-found-for-vtkpolydatamapper
Stackoverflow上的回答为:
I too was getting this error. The error means that the linker can’t find the definition for the vtkPolyDataMapper method. One has to note which vtk rendering backend they used, during build. It will probably be either vtkRenderingOpenGL, or vtkRenderingOpenGL2. Go to your build/lib folder and search for either one of these. I have VS 2015 Community and had the vtkRenderingOpenGL2, with vtk-7.1 built on Windows 8.1, x86_64 Platform, Release configuration.
I fixed the issue by inserting the 3 following lines at the very top of my source files, before any other preprocessor directives:

#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);

This initializes the specified VTK modules. CMake includes these by default, but other compilers such as VS do not.
The last two lines can be combined into the following:

#define vtkRenderingCore_AUTOINIT 2(vtkRenderingOpenGL2, vtkInteractionStyle)

还有另外一个回答:
I had the same issue at my platform; Visual Studio 2015 Windows 7 VTK 6.3
I followed VTK/Build System Migration from Marcus D. Hanwell’s post, and it works. My additonal lines are;

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkInteractionStyle);

on the top of preprocessor. The difference from RestlessC0bra’s post is probably OpenGL version.

采用第一个回答,在main之前增加:
#include “vtkAutoInit.h”
VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
VTK_MODULE_INIT(vtkInteractionStyle);

2.在ThinkPad E530C笔记本上出现的异常

在家里12年的老笔记本Thinpad E530C上运行这个例子,提示
搜索到的资料是“VTK7.0要求OPENGL3.2以上,很多笔记本集成显卡都达不到要求,换成独显就可以了”;
使用台式机(有独立显卡),可以运行不会报错;

3.运行结果

运行结果图如下:
在这里插入图片描述

资料

1.《医学图像编程技术》
2.https://stackoverflow.com/questions/18642155/no-override-found-for-vtkpolydatamapper

标签:VTK,MODULE,vtkRenderingOpenGL2,INIT,例子,圆锥体,include,cone
来源: https://blog.csdn.net/liushao1031177/article/details/115689351