VTK知识学习(2)-环境搭建

1、c++方案

1.1下载源码编译

官网获取源码。

利用Cmake进行项目构建。

里面要根据实际使用的情况配置相关的模块哟,这个得你自行研究下了。

CMAKEINSTALLPREFIX--这个选项的值表示VTK的安装路径,默认的路径是C:/Program Files/VTK。该选项的值可不作更改,按默认值即可。在配置完 VTK 后生成的工程中会有一个INSTALL项目,编译该项目后即会在CMAKEINSTALL_PREFIX所指定的路径中生成 VTK 相应的头文件、lib 文件和动态链接库 dll 文件。

VTK USE QT-是否使用 Qt。

完成后,会生成一个解决方案,根据配置的情况项目数量是不同。

1.2引入库文件

创建c++项目。

配置相关文件。

VC++目录配置:

链接器,把所有lib名填入。

1.3 编写测试

void TestShow()
{

	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);

	vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
	iren->SetRenderWindow(renWin);

	vtkInteractorStyleTrackballCamera* style =
		vtkInteractorStyleTrackballCamera::New();
	iren->SetInteractorStyle(style);

	vtkBoxWidget* boxWidget = vtkBoxWidget::New();
	boxWidget->SetInteractor(iren);
	boxWidget->SetPlaceFactor(1.25);
	boxWidget->SetProp3D(coneActor);
	boxWidget->PlaceWidget();
	vtkMyCallback* callback = vtkMyCallback::New();
	boxWidget->AddObserver(vtkCommand::InteractionEvent, callback);

	boxWidget->On();

	iren->Initialize();
	iren->Start();

	cone->Delete();
	coneMapper->Delete();
	coneActor->Delete();
	callback->Delete();
	boxWidget->Delete();
	ren1->Delete();
	renWin->Delete();
	iren->Delete();
	style->Delete();
}

2、C#方案

2.1 ActiViz.NET安装

ActiViz - 3D Visualization Library for .Net C# and Unity | Kitware

当然正常情况,你需要在密码上进行注册才能下载。

也可以从我的百度网盘中获取。

链接:https://pan.baidu.com/s/13pHLDSOyqtbWvPJn4mcaCg 
提取码:bkxa

下载成功后,直接运行这个exe到你目录下即可。

ActiViz.NET_9.3_Users_Guide.pdf这个文件你可以阅读下,按照指引你就可以编写一个vtk程序了。

2.2 导入包使用

需要在项目中引用如图所示的两个DLL,然后生成的目录下需要将相关DLL复制到,当然你可以配置环境变量来试试,因为操作系统默认会去环境变量里面找一下看看能不能找到。

2.3编写测试

界面

 <WindowsFormsHost x:Name="windowHost" Grid.Row="1"/>

控件生成

private RenderWindowControl renderWindowControl = new RenderWindowControl();

        public MainWindow()
        {
            InitializeComponent();
            windowHost.Child = renderWindowControl;
            // dd.Dock = DockStyle.Fill;
            // renderWindowControl.Load += renderWindowControl1_Load;
        }

显示一个球和文字

 RenderWindowControl renderWindow = sender as RenderWindowControl;
            if (renderWindow == null)
            {
                return;
            }
            // Create a simple sphere. A pipeline is created. 新建球体,创建“管道pipeline”
            vtkSphereSource sphere = vtkSphereSource.New();  // 新建球
            sphere.SetThetaResolution(8);                     // 设置球纬度参数
            sphere.SetPhiResolution(16);                     // 设置球经度参数
            sphere.SetRadius(0.5);                           // 设置球的半径

            //数据加工 -- "过滤器Filter" -- 收缩
            vtkShrinkPolyData shrink = vtkShrinkPolyData.New(); // 新建数据收缩操作器
            shrink.SetInputConnection(sphere.GetOutputPort());  // 连接管道
            shrink.SetShrinkFactor(0.9);                        // 收缩“面”操作

            //数据制图 -- "制图器Mapper"
            vtkPolyDataMapper mapper = vtkPolyDataMapper.New(); // 新建制图器
            mapper.SetInputConnection(shrink.GetOutputPort());  // 连接管道

            // The actor links the data pipeline to the rendering subsystem
            //创建“角色Actor”,连接“管道pipeline”和“渲染系统rendering subsystem”
            vtkActor actor = vtkActor.New();    // 新建角色
            actor.SetMapper(mapper);          // 传递制图器
            actor.GetProperty().SetColor(1, 0, 0);    // 设置“角色”颜色[RGB]

            // The actor links the data pipeline to the rendering subsystem
            //创建“角色Actor”,连接“管道pipeline”和“渲染系统rendering subsystem”

            // Create components of the rendering subsystem
            //创建渲染--“渲染系统rendering subsystem”

            //(1)新建“渲染器Renderer”和“渲染窗口RenderWindow”   renderWindowControl1控件提供“渲染窗口”
            vtkRenderer ren1 = renderWindow.RenderWindow.GetRenderers().GetFirstRenderer();
            vtkRenderWindow renWin = renderWindow.RenderWindow;

            // Add the actors to the renderer, set the window size
            //将“角色Actor”添加到“渲染器Renderer”并渲染
            ren1.AddViewProp(actor);   // 渲染器添加角色
            renWin.SetSize(250, 250);

            renWin.Render();  // 渲染渲染窗口

总结,后面主要是在C#上编写,当然c++的API更加丰富,毕竟这个C#是在其基础之上进行封装的,如果考虑性能方面肯定有差异的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值