1.连通区域分析
许多图形数据中,并非只包含一个对象(连通区域)。而在处理这些图形数据时,有时需要对每一个对象单独处理或者让其单独显示。比如,利用MarchingCube方法提取三维图像中的等值面,得到的结果往往是存在多个连通的对象区域,这是就需要对图形数据做连通区域分析,提取每个连通区域并计算其属性信息,以此来得到需要的连通区域。
下面一个例子来分析VTK中如何对图形数据做连通区域分析:
1 #include <vtkAutoInit.h> 2 VTK_MODULE_INIT(vtkRenderingOpenGL); 3 VTK_MODULE_INIT(vtkRenderingFreeType); 4 VTK_MODULE_INIT(vtkInteractionStyle); 5 6 #include <vtkSmartPointer.h> 7 #include <vtkSphereSource.h> 8 #include <vtkConeSource.h> 9 #include <vtkAppendPolyData.h> 10 #include <vtkPolyDataConnectivityFilter.h> 11 #include <vtkPolyDataMapper.h> 12 #include <vtkActor.h> 13 #include <vtkProperty.h> 14 #include <vtkRenderer.h> 15 #include <vtkRenderWindow.h> 16 #include <vtkRenderWindowInteractor.h> 17 int main() 18 { 19 vtkSmartPointer<vtkSphereSource> sphereSource = 20 vtkSmartPointer<vtkSphereSource>::New(); 21 sphereSource->SetRadius(10); 22 sphereSource->SetThetaResolution(10); 23 sphereSource->SetPhiResolution(10); 24 sphereSource->Update(); 25 26 vtkSmartPointer<vtkConeSource> coneSource = 27 vtkSmartPointer<vtkConeSource>::New(); 28 coneSource->SetRadius(5); 29 coneSource->SetHeight(10); 30 coneSource->SetCenter(25, 0, 0); 31 coneSource->Update(); 32 33 vtkSmartPointer<vtkAppendPolyData> appendFilter = 34 vtkSmartPointer<vtkAppendPolyData>::New(); 35 appendFilter->AddInputData(sphereSource->GetOutput()); 36 appendFilter->AddInputData(coneSource->GetOutput()); 37 appendFilter->Update(); 38 39 vtkSmartPointer<vtkPolyDataConnectivityFilter> connectivityFilter = 40 vtkSmartPointer<vtkPolyDataConnectivityFilter>::New(); 41 connectivityFilter->SetInputData(appendFilter->GetOutput()); 42 connectivityFilter->SetExtractionModeToCellSeededRegions(); 43 connectivityFilter->AddSeed(100); 44 connectivityFilter->Update(); 45 46 vtkSmartPointer<vtkPolyDataMapper> originalMapper = 47 vtkSmartPointer<vtkPolyDataMapper>::New(); 48 originalMapper->SetInputConnection(appendFilter->GetOutputPort()); 49 originalMapper->Update(); 50 vtkSmartPointer<vtkActor> originalActor = 51 vtkSmartPointer<vtkActor>::New(); 52 originalActor->SetMapper(originalMapper); 53 54 vtkSmartPointer<vtkPolyDataMapper> extractedMapper = 55 vtkSmartPointer<vtkPolyDataMapper>::New(); 56 extractedMapper->SetInputConnection(connectivityFilter->GetOutputPort()); 57 extractedMapper->Update(); 58 vtkSmartPointer<vtkActor> extractedActor = 59 vtkSmartPointer<vtkActor>::New(); 60 extractedActor->SetMapper(extractedMapper); 61 / 62 double leftViewport[4] = { 0.0, 0.0, 0.5, 1.0 }; 63 double rightViewport[4] = { 0.5, 0.0, 1.0, 1.0 }; 64 65 vtkSmartPointer<vtkRenderer> leftRenderer = 66 vtkSmartPointer<vtkRenderer>::New(); 67 leftRenderer->SetViewport(leftViewport); 68 leftRenderer->AddActor(originalActor); 69 leftRenderer->SetBackground(1, 0, 0); 70 71 vtkSmartPointer<vtkRenderer> rightRenderer = 72 vtkSmartPointer<vtkRenderer>::New(); 73 rightRenderer->SetViewport(rightViewport); 74 rightRenderer->AddActor(extractedActor); 75 rightRenderer->SetBackground(0, 0, 0); 76 77 vtkSmartPointer<vtkRenderWindow> renderWindow = 78 vtkSmartPointer<vtkRenderWindow>::New(); 79 renderWindow->AddRenderer(leftRenderer); 80 renderWindow->AddRenderer(rightRenderer); 81 renderWindow->SetSize(640, 320); 82 renderWindow->Render(); 83 renderWindow->SetWindowName("PolyDataConnectedCompExtract"); 84 85 leftRenderer->ResetCamera(); 86 rightRenderer->SetActiveCamera(leftRenderer->GetActiveCamera()); 87 88 vtkSmartPointer<vtkRenderWindowInteractor> interactor = 89 vtkSmartPointer<vtkRenderWindowInteractor>::New(); 90 interactor->SetRenderWindow(renderWindow); 91 interactor->Initialize(); 92 interactor->Start(); 93 return 0; 94 }
输出结果为:
这个例子够早了一个含有多个连通区域的模型数据。vtkAppendPolyData可以实现vtkPolyData的合并,使用该类可以方便地构造含有多个连通区域的数据,该类型接收两个或者多个vtkPolyData数据输入,合并结果包含输入数据的所有几何和拓扑数据。若输入为两个或者多个数据都含有点属性数据,则将其存储值输出结果中;对于单元属性数据亦是如此。
2.VTKPolyDataConnectivityFilter类解析
vtk中的vtkPolyDataConnectivityFilter类可以用于实现连通区域分析,该类接受vtkPolyData数据作为输入。集体使用如下:
1 vtkSmartPointer<vtkPolyDataConnectivityFilter> connectivityFilter = 2 vtkSmartPointer<vtkPolyDataConnectivityFilter>::New(); 3 connectivityFilter->SetInputData(appendFilter->GetOutput()); 4 connectivityFilter->SetExtractionModeToCellSeededRegions(); 5 connectivityFilter->AddSeed(100); 6 connectivityFilter->Update();
etExtractionModeToLargestRegion():用于提取具有最多点数的连通区域;SetExtractionModeToAllRegions():该模式主要用于连通区域标记,配合函数ColorRegionsOn()使用,在连通区域像是的同时,生成一个名为RegionId的点属性数据。
SetExtractionModeToSpecifiedRegions():该模式用于提取一个或多个连通区域,在该模式下,需要通过AddSpecifiedRegion()来添加西药提取的区域号,区域号从零开始。SetExtractionModeToClosestPointRegion():该模式需要使用SetClosestPoint()函数设置一个空间点坐标,执行结果为离该点最近的连通区域。SetExtractionModeToPointSeededRegions():该模式下需要使用AddSeed()函数添加种子点,提取种子点所在的区域。SetExtractionModeToCellSeededRegions():该模式下需要使用AddSeed()函数调价种子单元,提取种子单元所在的区域。