程序示例精选
C++ VTK网格模型补洞填孔
如需安装运行环境或远程调试,见文章底部个人QQ名片,由专业技术人员远程协助!
前言
这篇博客针对<<C++ VTK网格模型补洞填孔>>编写代码,代码整洁,规则,易读。 学习与应用推荐首选。
文章目录
一、所需工具软件
二、使用步骤
1. 引入库
2. 代码实现
3. 运行结果
三、在线协助
一、所需工具软件
1. VS
2. C++
二、使用步骤
1.引入库
#include <vtkSTLReader.h>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCleanPolyData.h>
#include <vtkDataSetSurfaceFilter.h>
#include <vtkExtractSelection.h>
#include <vtkFillHolesFilter.h>
#include <vtkIdTypeArray.h>
#include <vtkInformation.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkPolyDataNormals.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSelection.h>
#include <vtkSelectionNode.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkUnstructuredGrid.h>
#include <vtkBYUReader.h>
#include <vtkOBJReader.h>
#include <vtkPLYReader.h>
#include <vtkPolyDataReader.h>
#include <vtkSTLReader.h>
#include <vtkSphereSource.h>
#include <vtkXMLPolyDataReader.h>
#include <vtksys/SystemTools.hxx>
#include <vtkSTLWriter.h>
#include <vtkPLYWriter.h>
2. 代码实现
代码如下:
namespace {
vtkSmartPointer<vtkPolyData> ReadPolyData(const char* fileName);
}
int main(int argc, char* argv[])
{
vtkNew<vtkNamedColors> colors;
//auto input = ReadPolyData(argc > 1 ? argv[1] : "");
auto input = ReadPolyData("triangles.ply");
#if 0
// Restore the original normals
normals->GetOutput()->GetPointData()->
SetNormals(input->GetPointData()->GetNormals());
#endif
// Visualize
// Define viewport ranges
// (xmin, ymin, xmax, ymax)
double leftViewport[4] = { 0.0, 0.0, 0.5, 1.0 };
double rightViewport[4] = { 0.5, 0.0, 1.0, 1.0 };
// Create a mapper and actor
vtkNew<vtkPolyDataMapper> originalMapper;
originalMapper->SetInputData(input);
vtkNew<vtkProperty> backfaceProp;
backfaceProp->SetDiffuseColor(colors->GetColor3d("Banana").GetData());
vtkNew<vtkActor> originalActor;
originalActor->SetMapper(originalMapper);
originalActor->SetBackfaceProperty(backfaceProp);
originalActor->GetProperty()->SetDiffuseColor(
colors->GetColor3d("NavajoWhite").GetData());
vtkNew<vtkPolyDataMapper> filledMapper;
filledMapper->SetInputData(fillHolesFilter->GetOutput());
vtkNew<vtkActor> filledActor;
filledActor->SetMapper(filledMapper);
filledActor->GetProperty()->SetDiffuseColor(
colors->GetColor3d("NavajoWhite").GetData());
filledActor->SetBackfaceProperty(backfaceProp);
// Create a renderer, render window, and interactor
vtkNew<vtkRenderer> leftRenderer;
leftRenderer->SetViewport(leftViewport);
vtkNew<vtkRenderer> rightRenderer;
rightRenderer->SetViewport(rightViewport);
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(600, 300);
renderWindow->SetWindowName("FillHoles");
renderWindow->AddRenderer(leftRenderer);
renderWindow->AddRenderer(rightRenderer);
vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add the actor to the scene
leftRenderer->AddActor(originalActor);
rightRenderer->AddActor(filledActor);
leftRenderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
leftRenderer->GetActiveCamera()->SetPosition(0, -1, 0);
leftRenderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
leftRenderer->GetActiveCamera()->SetViewUp(0, 0, 1);
leftRenderer->GetActiveCamera()->Azimuth(30);
leftRenderer->GetActiveCamera()->Elevation(30);
leftRenderer->ResetCamera();
rightRenderer->SetBackground(colors->GetColor3d("LightSlateGray").GetData());
// Share the camera
rightRenderer->SetActiveCamera(leftRenderer->GetActiveCamera());
// Render and interact
renderWindow->Render();
renderWindowInteractor->Start();
return EXIT_SUCCESS;
}
void GenerateData(vtkPolyData* input)
{
// Create a sphere
vtkNew<vtkSphereSource> sphereSource;
sphereSource->Update();
// Remove some cells
vtkNew<vtkIdTypeArray> ids;
ids->SetNumberOfComponents(1);
// Set values
ids->InsertNextValue(2);
ids->InsertNextValue(10);
vtkNew<vtkSelectionNode> selectionNode;
selectionNode->SetFieldType(vtkSelectionNode::CELL);
selectionNode->SetContentType(vtkSelectionNode::INDICES);
selectionNode->SetSelectionList(ids);
selectionNode->GetProperties()->Set(vtkSelectionNode::INVERSE(),
1); // invert the selection
vtkNew<vtkSelection> selection;
selection->AddNode(selectionNode);
vtkNew<vtkExtractSelection> extractSelection;
extractSelection->SetInputConnection(0, sphereSource->GetOutputPort());
extractSelection->SetInputData(1, selection);
extractSelection->Update();
// In selection
vtkNew<vtkDataSetSurfaceFilter> surfaceFilter;
surfaceFilter->SetInputConnection(extractSelection->GetOutputPort());
surfaceFilter->Update();
input->ShallowCopy(surfaceFilter->GetOutput());
}
// Snippets
namespace {
vtkSmartPointer<vtkPolyData> ReadPolyData(const char* fileName)
{
vtkSmartPointer<vtkPolyData> polyData;
std::string extension =
vtksys::SystemTools::GetFilenameExtension(std::string(fileName));
if (extension == ".ply")
{
vtkNew<vtkPLYReader> reader;
reader->SetFileName(fileName);
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".vtp")
{
vtkNew<vtkXMLPolyDataReader> reader;
reader->SetFileName(fileName);
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".obj")
{
vtkNew<vtkOBJReader> reader;
reader->SetFileName(fileName);
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".stl")
{
vtkNew<vtkSTLReader> reader;
reader->SetFileName(fileName);
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".vtk")
{
vtkNew<vtkPolyDataReader> reader;
reader->SetFileName(fileName);
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".g")
{
vtkNew<vtkBYUReader> reader;
reader->SetGeometryFileName(fileName);
reader->Update();
polyData = reader->GetOutput();
}
else
{
vtkNew<vtkSphereSource> source;
source->Update();
polyData = source->GetOutput();
}
return polyData;
}
} // namespace
3. 运行结果
三、在线协助:
如需安装运行环境或远程调试,见文章底部个人 QQ 名片,由专业技术人员远程协助!
1)远程安装运行环境,代码调试
2)Qt, C++, Python入门指导
3)界面美化
4)软件制作
博主推荐文章:python人脸识别统计人数qt窗体-CSDN博客
博主推荐文章:Python Yolov5火焰烟雾识别源码分享-CSDN博客
Python OpenCV识别行人入口进出人数统计_python识别人数-CSDN博客
个人博客主页:alicema1111的博客_CSDN博客-Python,C++,网页领域博主
博主所有文章点这里:alicema1111的博客_CSDN博客-Python,C++,网页领域博主