VTK 显示3D模型的网格线(线框/wireframe)
在VTK9.1.0在Windows10+VS2019+Qt 5.15.2环境下编译安装的Qt例子中,想显示球体表面的网格线(线框/wireframe),设置actor的EdgeVisibilityOn属性。
sphereActor->GetProperty()->EdgeVisibilityOn();
全部代码
QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat()); vtkNew<vtkRenderer> renderer; vtkNew<vtkNamedColors> colors; vtkNew<vtkSphereSource> sphereSource; vtkNew<vtkPolyDataMapper> sphereMapper; sphereMapper->SetInputConnection(sphereSource->GetOutputPort()); vtkNew<vtkActor> sphereActor; sphereActor->SetMapper(sphereMapper); sphereActor->GetProperty()->SetColor(colors->GetColor4d("Tomato").GetData()); sphereActor->GetProperty()->EdgeVisibilityOn(); renderer->AddActor(sphereActor); renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData()); vtkNew<vtkGenericOpenGLRenderWindow> renderWindow; renderWindow->AddRenderer(renderer); renderWindow->SetWindowName("RenderWindowNoUIFile"); ui.vtk_main->setRenderWindow(renderWindow);
还可以通过vtkExtractEdges来实现显示全部网格线:
QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat()); vtkNew<vtkRenderer> renderer; vtkNew<vtkNamedColors> colors; vtkNew<vtkSphereSource> sphereSource; { //球体 vtkNew<vtkPolyDataMapper> sphereMapper; sphereMapper->SetInputConnection(sphereSource->GetOutputPort()); vtkNew<vtkActor> sphereActor; sphereActor->SetMapper(sphereMapper); sphereActor->GetProperty()->SetColor(colors->GetColor4d("Tomato").GetData()); renderer->AddActor(sphereActor); } { //网格线 vtkNew<vtkExtractEdges> extractEdges; extractEdges->SetInputConnection(sphereSource->GetOutputPort()); vtkNew<vtkPolyDataMapper> sphereMapper; sphereMapper->SetInputConnection(extractEdges->GetOutputPort()); vtkNew<vtkActor> sphereActor; sphereActor->SetMapper(sphereMapper); sphereActor->GetProperty()->SetColor(colors->GetColor4d("Black").GetData()); renderer->AddActor(sphereActor); } renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData()); vtkNew<vtkGenericOpenGLRenderWindow> renderWindow; renderWindow->AddRenderer(renderer); renderWindow->SetWindowName("RenderWindowNoUIFile"); ui.vtk_main->setRenderWindow(renderWindow);
编译显示vtkExtractEdges未定义,引入vtkExtractEdges的头文件vtkExtractEdges.h,任何时候vtk类没有定义,头文件是vtk类.h
如下图所示:
还可以通过设置actor的SetRepresentationToWireframe属性来显示网格线:
sphereActor->GetProperty()->SetRepresentationToWireframe();
通过SetRepresentationToWireframe只能显示网格线,不能面和线同时显示。
最后,还可以通过vtkFeatureEdges来显示特定的网格线:
1.边界(boundary):只被一个多边形使用的边,或者直线单元;
2.非流行(non-manifold):被三个以上的多边形共用的边;
3.特征边(feature edges):被两个三角形使用,并且两个三角形之间的夹角大于特征角度(可自行设置此角度);
4.流行边(manifold edges):只被两个多边形使用的边。