VTK學習(一)SetInputData()和SetInputConnection()替換SetInput()
阿新 • • 發佈:2018-12-12
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());
例子2
- 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.csdn.net/www_doling_net (東靈工作室)
- **********************************************************************/
- /**********************************************************************
- 檔名: 2.3_Viewport.cpp
- Copyright (c) 張曉東, 羅火靈. All rights reserved.
- 更多資訊請訪問:
- http://www.vtkchina.org (VTK中國)
- http://blog.csdn.net/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/