很高兴在雪易的CSDN遇见你
VTK技术爱好者 QQ:870202403
本文分享VTKExamples的Meshes模块中的FillHoles样例,涉及vtkFillHolesFilter & vtkPolyDataNormals接口,希望对各位小伙伴有所帮助!
感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!
你的点赞就是我的动力(^U^)ノ~YO
目录
前言
1. VTK自带样例
2. 优化后的样例
结论:
examples.vtk.org/site/Cxx/Meshes/FillHoles/
存在问题:填补的区域的法向量与输入部分的向量不同,需优化
左下角为:将Normals的输出,添加到Renderer后的结果。
右下角为:将输入的PointData_normals数据输入到normals的输出PointData中,结果OK。
void zxExampleMesh::FillHoles()
{
vtkSmartPointer input =
ReadPolyData("F:\\vtk\\VTKExamples-master\\src\\Testing\\Data\\Torso.vtp");
/*vtkSmartPointer input =
ReadPolyData("F:\\MP_Project\\zxVTKLib\\ztInteractionApplication\\TestData\\Mesh\\testFillHoles.stl");*/
vtkSmartPointer colors =
vtkSmartPointer::New();
vtkSmartPointer fillHolesFilter =
vtkSmartPointer::New();
fillHolesFilter->SetInputData(input);
fillHolesFilter->SetHoleSize(100000.0);
fillHolesFilter->Update();
// Make the triangle winding order consistent
vtkSmartPointer normals =
vtkSmartPointer::New();
normals->SetInputData(fillHolesFilter->GetOutput());
normals->ConsistencyOn();
normals->SplittingOff();
normals->Update();
// Visualize
// Define viewport ranges
// (xmin, ymin, xmax, ymax)
double Viewport1[4] = { 0.0, 0.5, 0.5, 1.0 };
double Viewport2[4] = { 0.5, 0.5, 1.0, 1.0 };
double Viewport3[4] = { 0.0, 0.0, 0.5, 0.5 };
double Viewport4[4] = { 0.5, 0.0, 1.0, 0.5 };
// Create a mapper and actor
vtkSmartPointer originalMapper =
vtkSmartPointer::New();
originalMapper->SetInputData(input);
vtkSmartPointer backfaceProp =
vtkSmartPointer::New();
backfaceProp->SetDiffuseColor(1., 0., 0.);
vtkSmartPointer originalActor =
vtkSmartPointer::New();
originalActor->SetMapper(originalMapper);
originalActor->SetBackfaceProperty(backfaceProp);
originalActor->GetProperty()->SetDiffuseColor(
colors->GetColor3d("Flesh").GetData());
vtkSmartPointer filledMapper =
vtkSmartPointer::New();
filledMapper->SetInputData(fillHolesFilter->GetOutput());
vtkSmartPointer filledActor =
vtkSmartPointer::New();
filledActor->SetMapper(filledMapper);
filledActor->GetProperty()->SetDiffuseColor(
colors->GetColor3d("Flesh").GetData());
filledActor->SetBackfaceProperty(backfaceProp);
vtkPolyData* normalPD = vtkPolyData::New();
normalPD->DeepCopy(normals->GetOutput());
vtkSmartPointer normalMapper =
vtkSmartPointer::New();
normalMapper->SetInputData(normalPD);
vtkSmartPointer normalActor =
vtkSmartPointer::New();
normalActor->SetMapper(normalMapper);
normalActor->GetProperty()->SetDiffuseColor(
colors->GetColor3d("Flesh").GetData());
normalActor->SetBackfaceProperty(backfaceProp);
#if 1
// Restore the original normals
normals->GetOutput()->GetPointData()->
SetNormals(input->GetPointData()->GetNormals());
#endif
vtkSmartPointer normalMapper2 =
vtkSmartPointer::New();
normalMapper2->SetInputData(normals->GetOutput());
vtkSmartPointer normalActor2 =
vtkSmartPointer::New();
normalActor2->SetMapper(normalMapper2);
normalActor2->GetProperty()->SetDiffuseColor(
colors->GetColor3d("Flesh").GetData());
normalActor2->SetBackfaceProperty(backfaceProp);
// Create a renderer, render window, and interactor
vtkSmartPointer Renderer1 =
vtkSmartPointer::New();
Renderer1->SetViewport(Viewport1);
vtkSmartPointer Renderer2 =
vtkSmartPointer::New();
Renderer2->SetViewport(Viewport2);
vtkSmartPointer Renderer3 =
vtkSmartPointer::New();
Renderer3->SetViewport(Viewport3);
vtkSmartPointer Renderer4 =
vtkSmartPointer::New();
Renderer4->SetViewport(Viewport4);
vtkSmartPointer renderWindow =
vtkSmartPointer::New();
renderWindow->SetSize(600, 300);
renderWindow->AddRenderer(Renderer1);
renderWindow->AddRenderer(Renderer2);
renderWindow->AddRenderer(Renderer3);
renderWindow->AddRenderer(Renderer4);
vtkSmartPointer renderWindowInteractor =
vtkSmartPointer::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// Add the actor to the scene
Renderer1->AddActor(originalActor);
Renderer2->AddActor(filledActor);
Renderer3->AddActor(normalActor);
Renderer4->AddActor(normalActor2);
Renderer1->SetBackground(colors->GetColor3d("PaleGreen").GetData());
Renderer3->SetBackground(colors->GetColor3d("LightGreen").GetData());
Renderer1->GetActiveCamera()->SetPosition(0, -1, 0);
Renderer1->GetActiveCamera()->SetFocalPoint(0, 0, 0);
Renderer1->GetActiveCamera()->SetViewUp(0, 0, 1);
Renderer1->GetActiveCamera()->Azimuth(30);
Renderer1->GetActiveCamera()->Elevation(30);
Renderer1->ResetCamera();
Renderer2->SetBackground(colors->GetColor3d("LightGreen").GetData());
Renderer4->SetBackground(colors->GetColor3d("PaleGreen").GetData());
// Share the camera
Renderer2->SetActiveCamera(Renderer1->GetActiveCamera());
Renderer3->SetActiveCamera(Renderer1->GetActiveCamera());
Renderer4->SetActiveCamera(Renderer1->GetActiveCamera());
// Render and interact
renderWindow->Render();
renderWindowInteractor->Start();
}
vtkSmartPointer zxExampleMesh::ReadPolyData(const char* fileName)
{
vtkSmartPointer polyData;
std::string extension = vtksys::SystemTools::GetFilenameExtension(std::string(fileName));
if (extension == ".ply")
{
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName(fileName);
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".vtp")
{
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName(fileName);
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".obj")
{
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName(fileName);
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".stl")
{
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName(fileName);
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".vtk")
{
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetFileName(fileName);
reader->Update();
polyData = reader->GetOutput();
}
else if (extension == ".g")
{
vtkSmartPointer reader =
vtkSmartPointer::New();
reader->SetGeometryFileName(fileName);
reader->Update();
polyData = reader->GetOutput();
}
else
{
vtkSmartPointer source =
vtkSmartPointer::New();
source->Update();
polyData = source->GetOutput();
}
return polyData;
}
感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!
你的赞赏是我的最最最最大的动力(^U^)ノ~YO