VTK修炼之道56:图形基本操作进阶_表面重建技术(三维点云曲面重建)

1.点云重建

虽然Delaunay三角剖分算法可以实现网格曲面重建,但是其应用主要在二维剖分,在三维空间网格生成中遇到了问题。因为在三维点云曲面重建中,Delaunay条件不在满足,不仅基于最大最小角判断的对角线交换准则不在成立,而且基于外接圆判据的Delaunay三角化也不能保证网格质量。
VTKSurfaceReconstructionFilter则实现了一种隐式曲面重建方法,即将曲面看做一个符号距离函数的等值面,曲面内外的距离值得符号相反,而零等值面即为所求的曲面。该方法需要对点云数据进行网格划分,然后估算每个点的切平面和方向,并以每个点与最近的切平面距离来近似表面距离。这样即可得到一个符号距离的体数据。这样,我们就可以利用VTKContourFilter来提取零等值面即可得到相应的网格。
利用人脸点云数据进行人脸网格曲面重建实例如下:
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);

#include <vtkSmartPointer.h>
#include <vtkPolyDataReader.h>
#include <vtkPolyData.h>
#include <vtkSurfaceReconstructionFilter.h>
#include <vtkContourFilter.h>
#include <vtkVertexGlyphFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkCamera.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>

int main()
{
	vtkSmartPointer<vtkPolyDataReader> reader =
		vtkSmartPointer<vtkPolyDataReader>::New();
	reader->SetFileName("fran_cut.vtk");
	reader->Update();

	vtkSmartPointer<vtkPolyData> points =
		vtkSmartPointer<vtkPolyData>::New();
	points->SetPoints(reader->GetOutput()->GetPoints()); //获得网格模型中的几何数据:点集

	vtkSmartPointer<vtkSurfaceReconstructionFilter> surf =
		vtkSmartPointer<vtkSurfaceReconstructionFilter>::New();
	surf->SetInputData(points);
	surf->SetNeighborhoodSize(20);
	surf->SetSampleSpacing(0.005);
	surf->Update();

	vtkSmartPointer<vtkContourFilter> contour =
		vtkSmartPointer<vtkContourFilter>::New();
	contour->SetInputConnection(surf->GetOutputPort());
	contour->SetValue(0, 0.0);
	contour->Update();
	//
	vtkSmartPointer <vtkVertexGlyphFilter> vertexGlyphFilter =
		vtkSmartPointer<vtkVertexGlyphFilter>::New();
	vertexGlyphFilter->AddInputData(points);
	vertexGlyphFilter->Update();
	vtkSmartPointer<vtkPolyDataMapper> pointMapper =
		vtkSmartPointer<vtkPolyDataMapper>::New();
	pointMapper->SetInputData(vertexGlyphFilter->GetOutput());
	pointMapper->ScalarVisibilityOff();

	vtkSmartPointer<vtkActor> pointActor =
		vtkSmartPointer<vtkActor>::New();
	pointActor->SetMapper(pointMapper);
	pointActor->GetProperty()->SetColor(1, 0, 0);
	pointActor->GetProperty()->SetPointSize(4);

	vtkSmartPointer<vtkPolyDataMapper> contourMapper =
		vtkSmartPointer<vtkPolyDataMapper>::New();
	contourMapper->SetInputData(contour->GetOutput());
	vtkSmartPointer<vtkActor> contourActor =
		vtkSmartPointer<vtkActor>::New();
	contourActor->SetMapper(contourMapper);
	///
	double pointView[4] = { 0, 0, 0.5, 1 };
	double contourView[4] = { 0.5, 0, 1, 1 };

	vtkSmartPointer<vtkRenderer> pointRender =
		vtkSmartPointer<vtkRenderer>::New();
	pointRender->AddActor(pointActor);
	pointRender->SetViewport(pointView);
	pointRender->SetBackground(1, 1, 1);

	vtkSmartPointer<vtkRenderer> contourRender =
		vtkSmartPointer<vtkRenderer>::New();
	contourRender->AddActor(contourActor);
	contourRender->SetViewport(contourView);
	contourRender->SetBackground(0, 1, 0);

	pointRender->GetActiveCamera()->SetPosition(0, -1, 0);
	pointRender->GetActiveCamera()->SetFocalPoint(0, 0, 0);
	pointRender->GetActiveCamera()->SetViewUp(0,0,1);
	pointRender->GetActiveCamera()->Azimuth(30);
	pointRender->GetActiveCamera()->Elevation(30);
	pointRender->ResetCamera();
	contourRender->SetActiveCamera(pointRender->GetActiveCamera());

	vtkSmartPointer<vtkRenderWindow> rw =
		vtkSmartPointer<vtkRenderWindow>::New();
	rw->AddRenderer(pointRender);
	rw->AddRenderer(contourRender);
	rw->SetSize(640, 320);
	rw->SetWindowName("3D Surface Reconstruction ");
	rw->Render();

	vtkSmartPointer<vtkRenderWindowInteractor> rwi =
		vtkSmartPointer<vtkRenderWindowInteractor>::New();
	rwi->SetRenderWindow(rw);
	rwi->Initialize();
	rwi->Start();

	return 0;
}

使用VTKSurfaceReconstructionFilter时,主要涉及两个参数,分别使用函数SetNeighborhoodSize()和SetSampleSpacing()进行设置。
SetNeighborhoodSize:设置邻域点的个数;而这些邻域点则用来估计每个点的局部切平面。邻域点的个数默认为20,能够处理大多数重建问题。个数设置越多,计算消耗时间越长。当点云分布严重不均匀情况下,可以考虑增加该值。
SetSampleSpacing:用于设置划分网格的网格间距,间距与小,网格越密集,一般采用默认值0.05.
该例的输出结果如下图所示:

2.参看资料

1.《C++ primer》
2.《The VTK User’s Guide – 11thEdition》
3.  张晓东, 罗火灵. VTK图形图像开发进阶[M]. 机械工业出版社, 2015.
相关推荐
©️2020 CSDN 皮肤主题: 代码科技 设计师:Amelia_0503 返回首页