1.点云重建
虽然Delaunay三角剖分算法可以实现网格曲面重建,但是其应用主要在二维剖分,在三维空间网格生成中遇到了问题。因为在三维点云曲面重建中,Delaunay条件不在满足,不仅基于最大最小角判断的对角线交换准则不在成立,而且基于外接圆判据的Delaunay三角化也不能保证网格质量。VTKSurfaceReconstructionFilter则实现了一种隐式曲面重建方法,即将曲面看做一个符号距离函数的等值面,曲面内外的距离值得符号相反,而零等值面即为所求的曲面。该方法需要对点云数据进行网格划分,然后估算每个点的切平面和方向,并以每个点与最近的切平面距离来近似表面距离。这样即可得到一个符号距离的体数据。这样,我们就可以利用VTKContourFilter来提取零等值面即可得到相应的网格。利用人脸点云数据进行人脸网格曲面重建实例如下: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 <vtkPolyDataReader.h> 8 #include <vtkPolyData.h> 9 #include <vtkSurfaceReconstructionFilter.h> 10 #include <vtkContourFilter.h> 11 #include <vtkVertexGlyphFilter.h> 12 #include <vtkPolyDataMapper.h> 13 #include <vtkActor.h> 14 #include <vtkRenderer.h> 15 #include <vtkCamera.h> 16 #include <vtkRenderWindow.h> 17 #include <vtkRenderWindowInteractor.h> 18 #include <vtkProperty.h> 19 20 int main() 21 { 22 vtkSmartPointer<vtkPolyDataReader> reader = 23 vtkSmartPointer<vtkPolyDataReader>::New(); 24 reader->SetFileName("fran_cut.vtk"); 25 reader->Update(); 26 27 vtkSmartPointer<vtkPolyData> points = 28 vtkSmartPointer<vtkPolyData>::New(); 29 points->SetPoints(reader->GetOutput()->GetPoints()); //获得网格模型中的几何数据:点集 30 31 vtkSmartPointer<vtkSurfaceReconstructionFilter> surf = 32 vtkSmartPointer<vtkSurfaceReconstructionFilter>::New(); 33 surf->SetInputData(points); 34 surf->SetNeighborhoodSize(20); 35 surf->SetSampleSpacing(0.005); 36 surf->Update(); 37 38 vtkSmartPointer<vtkContourFilter> contour = 39 vtkSmartPointer<vtkContourFilter>::New(); 40 contour->SetInputConnection(surf->GetOutputPort()); 41 contour->SetValue(0, 0.0); 42 contour->Update(); 43 // 44 vtkSmartPointer <vtkVertexGlyphFilter> vertexGlyphFilter = 45 vtkSmartPointer<vtkVertexGlyphFilter>::New(); 46 vertexGlyphFilter->AddInputData(points); 47 vertexGlyphFilter->Update(); 48 vtkSmartPointer<vtkPolyDataMapper> pointMapper = 49 vtkSmartPointer<vtkPolyDataMapper>::New(); 50 pointMapper->SetInputData(vertexGlyphFilter->GetOutput()); 51 pointMapper->ScalarVisibilityOff(); 52 53 vtkSmartPointer<vtkActor> pointActor = 54 vtkSmartPointer<vtkActor>::New(); 55 pointActor->SetMapper(pointMapper); 56 pointActor->GetProperty()->SetColor(1, 0, 0); 57 pointActor->GetProperty()->SetPointSize(4); 58 59 vtkSmartPointer<vtkPolyDataMapper> contourMapper = 60 vtkSmartPointer<vtkPolyDataMapper>::New(); 61 contourMapper->SetInputData(contour->GetOutput()); 62 vtkSmartPointer<vtkActor> contourActor = 63 vtkSmartPointer<vtkActor>::New(); 64 contourActor->SetMapper(contourMapper); 65 /// 66 double pointView[4] = { 0, 0, 0.5, 1 }; 67 double contourView[4] = { 0.5, 0, 1, 1 }; 68 69 vtkSmartPointer<vtkRenderer> pointRender = 70 vtkSmartPointer<vtkRenderer>::New(); 71 pointRender->AddActor(pointActor); 72 pointRender->SetViewport(pointView); 73 pointRender->SetBackground(1, 1, 1); 74 75 vtkSmartPointer<vtkRenderer> contourRender = 76 vtkSmartPointer<vtkRenderer>::New(); 77 contourRender->AddActor(contourActor); 78 contourRender->SetViewport(contourView); 79 contourRender->SetBackground(0, 1, 0); 80 81 pointRender->GetActiveCamera()->SetPosition(0, -1, 0); 82 pointRender->GetActiveCamera()->SetFocalPoint(0, 0, 0); 83 pointRender->GetActiveCamera()->SetViewUp(0,0,1); 84 pointRender->GetActiveCamera()->Azimuth(30); 85 pointRender->GetActiveCamera()->Elevation(30); 86 pointRender->ResetCamera(); 87 contourRender->SetActiveCamera(pointRender->GetActiveCamera()); 88 89 vtkSmartPointer<vtkRenderWindow> rw = 90 vtkSmartPointer<vtkRenderWindow>::New(); 91 rw->AddRenderer(pointRender); 92 rw->AddRenderer(contourRender); 93 rw->SetSize(640, 320); 94 rw->SetWindowName("3D Surface Reconstruction "); 95 rw->Render(); 96 97 vtkSmartPointer<vtkRenderWindowInteractor> rwi = 98 vtkSmartPointer<vtkRenderWindowInteractor>::New(); 99 rwi->SetRenderWindow(rw); 100 rwi->Initialize(); 101 rwi->Start(); 102 103 return 0; 104 }
使用VTKSurfaceReconstructionFilter时,主要涉及两个参数,分别使用函数SetNeighborhoodSize()和SetSampleSpacing()进行设置。
SetNeighborhoodSize:设置邻域点的个数;而这些邻域点则用来估计每个点的局部切平面。邻域点的个数默认为20,能够处理大多数重建问题。个数设置越多,计算消耗时间越长。当点云分布严重不均匀情况下,可以考虑增加该值。SetSampleSpacing:用于设置划分网格的网格间距,间距与小,网格越密集,一般采用默认值0.05.该例的输出结果如下图所示: