1.连通区域分析
许多图形数据中,并非只包含一个对象(连通区域)。而在处理这些图形数据时,有时需要对每一个对象单独处理或者让其单独显示。比如,利用MarchingCube方法提取三维图像中的等值面,得到的结果往往是存在多个连通的对象区域,这是就需要对图形数据做连通区域分析,提取每个连通区域并计算其属性信息,以此来得到需要的连通区域。
下面一个例子来分析VTK中如何对图形数据做连通区域分析:
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkConeSource.h>
#include <vtkAppendPolyData.h>
#include <vtkPolyDataConnectivityFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
int main()
{
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
sphereSource->SetRadius(10);
sphereSource->SetThetaResolution(10);
sphereSource->SetPhiResolution(10);
sphereSource->Update();
vtkSmartPointer<vtkConeSource> coneSource =
vtkSmartPointer<vtkConeSource>::New();
coneSource->SetRadius(5);
coneSource->SetHeight(10);
coneSource->SetCenter(25, 0, 0);
coneSource->Update();
vtkSmartPointer<vtkAppendPolyData> appendFilter =
vtkSmartPointer<vtkAppendPolyData>::New();
appendFilter->AddInputData(sphereSource->GetOutput());
appendFilter->AddInputData(coneSource->GetOutput());
appendFilter->Update();
vtkSmartPointer<vtkPolyDataConnectivityFilter> connectivityFilter =
vtkSmartPointer<vtkPolyDataConnectivityFilter>::New();
connectivityFilter->SetInputData(appendFilter->GetOutput());
connectivityFilter->SetExtractionModeToCellSeededRegions();
connectivityFilter->AddSeed(100);
connectivityFilter->Update();
vtkSmartPointer<vtkPolyDataMapper> originalMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
originalMapper->SetInputConnection(appendFilter->GetOutputPort());
originalMapper->Update();
vtkSmartPointer<vtkActor> originalActor =
vtkSmartPointer<vtkActor>::New();
originalActor->SetMapper(originalMapper);
vtkSmartPointer<vtkPolyDataMapper> extractedMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
extractedMapper->SetInputConnection(connectivityFilter->GetOutputPort());
extractedMapper->Update();
vtkSmartPointer<vtkActor> extractedActor =
vtkSmartPointer<vtkActor>::New();
extractedActor->SetMapper(extractedMapper);
/
double leftViewport[4] = { 0.0, 0.0, 0.5, 1.0 };
double rightViewport[4] = { 0.5, 0.0, 1.0, 1.0 };
vtkSmartPointer<vtkRenderer> leftRenderer =
vtkSmartPointer<vtkRenderer>::New();
leftRenderer->SetViewport(leftViewport);
leftRenderer->AddActor(originalActor);
leftRenderer->SetBackground(1, 0, 0);
vtkSmartPointer<vtkRenderer> rightRenderer =
vtkSmartPointer<vtkRenderer>::New();
rightRenderer->SetViewport(rightViewport);
rightRenderer->AddActor(extractedActor);
rightRenderer->SetBackground(0, 0, 0);
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(leftRenderer);
renderWindow->AddRenderer(rightRenderer);
renderWindow->SetSize(640, 320);
renderWindow->Render();
renderWindow->SetWindowName("PolyDataConnectedCompExtract");
leftRenderer->ResetCamera();
rightRenderer->SetActiveCamera(leftRenderer->GetActiveCamera());
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
interactor->SetRenderWindow(renderWindow);
interactor->Initialize();
interactor->Start();
return 0;
}
输出结果为:
这个例子够早了一个含有多个连通区域的模型数据。vtkAppendPolyData可以实现vtkPolyData的合并,使用该类可以方便地构造含有多个连通区域的数据,该类型接收两个或者多个vtkPolyData数据输入,合并结果包含输入数据的所有几何和拓扑数据。若输入为两个或者多个数据都含有点属性数据,则将其存储值输出结果中;对于单元属性数据亦是如此。
2.VTKPolyDataConnectivityFilter类解析
vtk中的vtkPolyDataConnectivityFilter类可以用于实现连通区域分析,该类接受vtkPolyData数据作为输入。集体使用如下:
vtkSmartPointer<vtkPolyDataConnectivityFilter> connectivityFilter =
vtkSmartPointer<vtkPolyDataConnectivityFilter>::New();
connectivityFilter->SetInputData(appendFilter->GetOutput());
connectivityFilter->SetExtractionModeToCellSeededRegions();
connectivityFilter->AddSeed(100);
connectivityFilter->Update();
SetExtractionModeToLargestRegion():用于提取具有最多点数的连通区域;
SetExtractionModeToAllRegions():该模式主要用于连通区域标记,配合函数ColorRegionsOn()使用,在连通区域像是的同时,生成一个名为RegionId的点属性数据。
SetExtractionModeToSpecifiedRegions():该模式用于提取一个或多个连通区域,在该模式下,需要通过AddSpecifiedRegion()来添加西药提取的区域号,区域号从零开始。
SetExtractionModeToClosestPointRegion():该模式需要使用SetClosestPoint()函数设置一个空间点坐标,执行结果为离该点最近的连通区域。
SetExtractionModeToPointSeededRegions():该模式下需要使用AddSeed()函数添加种子点,提取种子点所在的区域。
SetExtractionModeToCellSeededRegions():该模式下需要使用AddSeed()函数调价种子单元,提取种子单元所在的区域。