VTK6引入了一些向后不兼容的更改。这里更详细地描述了这些变化背后的原因。其中一个更改是使用SetInputData()和SetInputConnection()替换SetInput()。
VTK4中管道对象连接连接
someFilter - > SetInput ( someReader - > GetOutput ());
VTK5中更改为
someFilter - > SetInputConnection ( someReader - > GetOutputPort ());
SetInput()和相关方法被保留为向后兼容性。
VTK6中 此方法和所有相关功能(如SetSource)在VTK 6中已被删除。这背后的主要原因是在将数据对象与管道对象分离(参见此处有详细信息)时,不可能保留此方法。在VTK 5中,SetInput()是使用SetInputConnection()实现的,但这需要访问该算法及其输出端口给定一个数据对象。在VTK 6中,由于数据对象不再引用生成它的算法,所以不可能建立仅给出数据对象的流水线连接。
为了便于将独立数据对象分配给算法的输入,我们在VTK 6中引入了一组方便的功能。下面是一个例子:
someFilter - > SetInputData ( aDataObject );
请注意,即使下面的代码将被编译,它不会创建一个管道连接,也不应该用于代替SetInputConnection()。
someFilter - > SetInputData ( someReader - > GetOutput ());
将数据对象与管道对象分离的另一个优点是开发人员不再需要创建输入副本,以便使用内部过滤器。以前,这是算法正常运行所必需的:
现在可以用以下代替
void MyFilter::RequestData(…) { vtkDataObject* input = inInfo->Get(vtkDataObject::DATA_OBJECT()); vtkDataObject* copy = input->NewInstance(); copy->ShallowCopy(input); this->InternalFilter->SetInput(copy); this->InternalFilter->Update(); … }
- void MyFilter::RequestData(…)
- {
- vtkDataObject* input = inInfo->Get(vtkDataObject::DATA_OBJECT());
- this->InternalFilter->SetInputData(input);
- this->InternalFilter->Update();
- …
- }
另一个优点是,此更改会从管道中删除一些循环引用,从而无需使用垃圾回收器。这对使用大型管道时VTK的性能有明显的影响。
这种变化可能对VTK社区产生最大的影响。同时,这是最简单的解决方案。这是一个经验法则:
- 如果要建立管道连接,请使用SetInputConnection
- 如果要处理独立数据集,请使用SetInputData
以下是一些具体的例子。注意,即使我们在所有这些示例中使用SetInput(),类似的方法,如SetSource()也应该遵循相同的模式。查找SetSourceConnection()或SetSourceData()为例。此外,我们忽略这些示例中的端口号,但大多数应该使用端口号。
例子1anotherFilter->SetInput(aFilter->GetOutput());
anotherFilter->SetInputConnection(aFilter->GetOutputPort());
vtkDataObject* output = aFilter->GetOutput(); anotherFilter->SetInput(output);
anotherFilter->SetInputConnection(aFilter->GetOutputPort());
例子3
vtkPolyData *pd = vtkPolyData::New(); aFilter->SetInput(pd);
例子4
vtkPolyData *pd = vtkPolyData::New(); aFilter->SetInputData(pd);
vtkDataObject* output = aFilter->GetOutput(); aFilter->Update(); anotherFilter->SetInput(output);
例子5
vtkDataObject* output = aFilter->GetOutput(); aFilter->Update(); anotherFilter->SetInputData(output);
- void myfunction(vtkDataObject* dobj)
- {
- vtkAFilter* aFilter = vtkAFilter::New();
- aFilter->SetInput(dobj);
- aFilter->Update();
- // …
- }
This example is also ambiguous. You need to trace it up to find the origin of dobj. If this is the common use:myfunction(aFilter->GetOutput());
you will have to refactor myfunction to take an algorithm and an output port. For example:
- void myfunction(vtkAlgorithm* alg, int port)
- {
- vtkAFilter* aFilter = vtkAFilter::New();
- aFilter->SetInputConnection(alg->GetOutputPort(port));
- aFilter->Update();
- // …
- }
-
- myfunction(aFilter, 0);
If this is the common use:
- vtkPolyData* pd = vtkPolyData::New();
- // fill pd
- myfunction(pd);
then replacing SetInput() with SetInputData() would work.https://www.vtk.org/Wiki/VTK/VTK_6_Migration/Replacement_of_SetInput
例子
- #include "vtkAutoInit.h"
- VTK_MODULE_INIT(vtkRenderingOpenGL2); // VTK was built with vtkRenderingOpenGL2
- VTK_MODULE_INIT(vtkInteractionStyle);
- /**********************************************************************
- 文件名: 2.3_Viewport.cpp
- Copyright (c) 张晓东, 罗火灵. All rights reserved.
- 更多信息请访问:
- http://www.vtkchina.org (VTK中国)
- http://blog.youkuaiyun.com/www_doling_net (东灵工作室)
- **********************************************************************/
-
- /**********************************************************************
- 文件名: 2.3_Viewport.cpp
- Copyright (c) 张晓东, 罗火灵. All rights reserved.
- 更多信息请访问:
- http://www.vtkchina.org (VTK中国)
- http://blog.youkuaiyun.com/www_doling_net (东灵工作室)
- **********************************************************************/
-
- #include <vtkConeSource.h>
- #include <vtkCubeSource.h>
- #include <vtkCylinderSource.h>
- #include <vtkSphereSource.h>
- #include <vtkPolyDataMapper.h>
- #include <vtkRenderer.h>
- #include <vtkRenderWindow.h>
- #include <vtkActor.h>
- #include <vtkRenderWindowInteractor.h>
- #include <vtkSmartPointer.h>
-
- int main()
- {
- vtkSmartPointer<vtkConeSource> cone = vtkSmartPointer<vtkConeSource>::New();
- vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New();
- vtkSmartPointer<vtkCylinderSource> cylinder = vtkSmartPointer<vtkCylinderSource>::New();
- vtkSmartPointer<vtkSphereSource> sphere = vtkSmartPointer<vtkSphereSource>::New();
-
- vtkSmartPointer<vtkPolyDataMapper> coneMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
- coneMapper->SetInputData(cone->GetOutput());//SetInput改为SetInputData
- vtkSmartPointer<vtkPolyDataMapper> cubeMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
- cubeMapper->SetInputData(cube->GetOutput());
- vtkSmartPointer<vtkPolyDataMapper> cylinderMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
- cylinderMapper->SetInputData(cylinder->GetOutput());
- vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
- sphereMapper->SetInputData(sphere->GetOutput());
-
- vtkSmartPointer<vtkActor> coneActor = vtkSmartPointer<vtkActor>::New();
- coneActor->SetMapper(coneMapper);
- vtkSmartPointer<vtkActor> cubeActor = vtkSmartPointer<vtkActor>::New();
- cubeActor->SetMapper(cubeMapper);
- vtkSmartPointer<vtkActor> cylinderActor = vtkSmartPointer<vtkActor>::New();
- cylinderActor->SetMapper(cylinderMapper);
- vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New();
- sphereActor->SetMapper(sphereMapper);
-
-
- vtkSmartPointer<vtkRenderer> renderer1 = vtkSmartPointer<vtkRenderer>::New();
- renderer1->AddActor(coneActor);
- renderer1->SetBackground(1.0, 0.0, 0.0);
- renderer1->SetViewport(0.0, 0.0, 0.5, 0.5);
- vtkSmartPointer<vtkRenderer> renderer2 = vtkSmartPointer<vtkRenderer>::New();
- renderer2->AddActor(cubeActor);
- renderer2->SetBackground(0, 0, 0);
- renderer2->SetViewport(0.5, 0.0, 1.0, 0.5);
- vtkSmartPointer<vtkRenderer> renderer3 = vtkSmartPointer<vtkRenderer>::New();
- renderer3->AddActor(cylinderActor);
- renderer3->SetBackground(0.0, 0.0, 1.0);
- renderer3->SetViewport(0.0, 0.5, 0.5, 1.0);
- vtkSmartPointer<vtkRenderer> renderer4 = vtkSmartPointer<vtkRenderer>::New();
- renderer4->AddActor(sphereActor);
- renderer4->SetBackground(1.0, 1.0, 0.0);
- renderer4->SetViewport(0.5, 0.5, 1.0, 1.0);
-
- vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
- renWin->AddRenderer(renderer1);
- renWin->AddRenderer(renderer2);
- renWin->AddRenderer(renderer3);
- renWin->AddRenderer(renderer4);
- renWin->SetSize(640, 480);
- renWin->Render();
- renWin->SetWindowName("Viewport");
-
- vtkSmartPointer<vtkRenderWindowInteractor> interactor =
- vtkSmartPointer<vtkRenderWindowInteractor>::New();
- interactor->SetRenderWindow(renWin);
-
- renWin->Render();
- interactor->Initialize();
- interactor->Start();
-
- return EXIT_SUCCESS;
- }

运行在运行《VTK图形图像进阶》第五章5.3_ImageResliceExample.cpp时,会提示MetaImage cannot read data from file或者显示窗口是空白,后来搜索了VTKExample中的例子,发现可以这样解决:
imgActor->SetInput(colorMap->GetOutput());
改为imgActor->GetMapper()->SetInputConnection(colorMap->GetOutputPort());
同时添加头文件
#include <vtkImageMapper3D.h>
参考链接https://heliublog.com/2017/05/10/VTK%E6%8F%90%E7%A4%BAMetaImage%20cannot%20read%20data%20from%20file/