【VTKExamples::Meshes】第一期 FillHoles

发布时间:2024年01月03日

很高兴在雪易的CSDN遇见你?

VTK技术爱好者 QQ:870202403


前言

本文分享VTKExamples的Meshes模块中的FillHoles样例,涉及vtkFillHolesFilter & vtkPolyDataNormals接口,希望对各位小伙伴有所帮助!

感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!

你的点赞就是我的动力(^U^)ノ~YO


目录

前言

1. VTK自带样例

2. 优化后的样例

结论:


1. VTK自带样例

????????examples.vtk.org/site/Cxx/Meshes/FillHoles/

?????????

? ? ? ? 存在问题:填补的区域的法向量与输入部分的向量不同,需优化?

2. 优化后的样例

????????

?左下角为:将Normals的输出,添加到Renderer后的结果。

右下角为:将输入的PointData_normals数据输入到normals的输出PointData中,结果OK。

void zxExampleMesh::FillHoles()
{
    vtkSmartPointer<vtkPolyData> input =
        ReadPolyData("F:\\vtk\\VTKExamples-master\\src\\Testing\\Data\\Torso.vtp");
    /*vtkSmartPointer<vtkPolyData> input =
        ReadPolyData("F:\\MP_Project\\zxVTKLib\\ztInteractionApplication\\TestData\\Mesh\\testFillHoles.stl");*/

    vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();

    vtkSmartPointer<vtkFillHolesFilter> fillHolesFilter =
        vtkSmartPointer<vtkFillHolesFilter>::New();
    fillHolesFilter->SetInputData(input);
    fillHolesFilter->SetHoleSize(100000.0);
    fillHolesFilter->Update();

    // Make the triangle winding order consistent
    vtkSmartPointer<vtkPolyDataNormals> normals =
        vtkSmartPointer<vtkPolyDataNormals>::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<vtkPolyDataMapper> originalMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    originalMapper->SetInputData(input);

    vtkSmartPointer<vtkProperty> backfaceProp =
        vtkSmartPointer<vtkProperty>::New();
    backfaceProp->SetDiffuseColor(1., 0., 0.);

    vtkSmartPointer<vtkActor> originalActor =
        vtkSmartPointer<vtkActor>::New();
    originalActor->SetMapper(originalMapper);
    originalActor->SetBackfaceProperty(backfaceProp);
    originalActor->GetProperty()->SetDiffuseColor(
        colors->GetColor3d("Flesh").GetData());

    vtkSmartPointer<vtkPolyDataMapper> filledMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    filledMapper->SetInputData(fillHolesFilter->GetOutput());

    vtkSmartPointer<vtkActor> filledActor =
        vtkSmartPointer<vtkActor>::New();
    filledActor->SetMapper(filledMapper);
    filledActor->GetProperty()->SetDiffuseColor(
        colors->GetColor3d("Flesh").GetData());
    filledActor->SetBackfaceProperty(backfaceProp);

    vtkPolyData* normalPD = vtkPolyData::New();
    normalPD->DeepCopy(normals->GetOutput());
    vtkSmartPointer<vtkPolyDataMapper> normalMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    normalMapper->SetInputData(normalPD);

    vtkSmartPointer<vtkActor> normalActor =
        vtkSmartPointer<vtkActor>::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<vtkPolyDataMapper> normalMapper2 =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    normalMapper2->SetInputData(normals->GetOutput());

    vtkSmartPointer<vtkActor> normalActor2 =
        vtkSmartPointer<vtkActor>::New();
    normalActor2->SetMapper(normalMapper2);
    normalActor2->GetProperty()->SetDiffuseColor(
        colors->GetColor3d("Flesh").GetData());
    normalActor2->SetBackfaceProperty(backfaceProp);

    // Create a renderer, render window, and interactor
    vtkSmartPointer<vtkRenderer> Renderer1 =
        vtkSmartPointer<vtkRenderer>::New();
    Renderer1->SetViewport(Viewport1);

    vtkSmartPointer<vtkRenderer> Renderer2 =
        vtkSmartPointer<vtkRenderer>::New();
    Renderer2->SetViewport(Viewport2);

    vtkSmartPointer<vtkRenderer> Renderer3 =
        vtkSmartPointer<vtkRenderer>::New();
    Renderer3->SetViewport(Viewport3);

    vtkSmartPointer<vtkRenderer> Renderer4 =
        vtkSmartPointer<vtkRenderer>::New();
    Renderer4->SetViewport(Viewport4);

    vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->SetSize(600, 300);

    renderWindow->AddRenderer(Renderer1);
    renderWindow->AddRenderer(Renderer2);
    renderWindow->AddRenderer(Renderer3);
    renderWindow->AddRenderer(Renderer4);

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
        vtkSmartPointer<vtkRenderWindowInteractor>::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<vtkPolyData> zxExampleMesh::ReadPolyData(const char* fileName)
{
    vtkSmartPointer<vtkPolyData> polyData;
    std::string extension = vtksys::SystemTools::GetFilenameExtension(std::string(fileName));
    if (extension == ".ply")
    {
        vtkSmartPointer<vtkPLYReader> reader =
            vtkSmartPointer<vtkPLYReader>::New();
        reader->SetFileName(fileName);
        reader->Update();
        polyData = reader->GetOutput();
    }
    else if (extension == ".vtp")
    {
        vtkSmartPointer<vtkXMLPolyDataReader> reader =
            vtkSmartPointer<vtkXMLPolyDataReader>::New();
        reader->SetFileName(fileName);
        reader->Update();
        polyData = reader->GetOutput();
    }
    else if (extension == ".obj")
    {
        vtkSmartPointer<vtkOBJReader> reader =
            vtkSmartPointer<vtkOBJReader>::New();
        reader->SetFileName(fileName);
        reader->Update();
        polyData = reader->GetOutput();
    }
    else if (extension == ".stl")
    {
        vtkSmartPointer<vtkSTLReader> reader =
            vtkSmartPointer<vtkSTLReader>::New();
        reader->SetFileName(fileName);
        reader->Update();
        polyData = reader->GetOutput();
    }
    else if (extension == ".vtk")
    {
        vtkSmartPointer<vtkPolyDataReader> reader =
            vtkSmartPointer<vtkPolyDataReader>::New();
        reader->SetFileName(fileName);
        reader->Update();
        polyData = reader->GetOutput();
    }
    else if (extension == ".g")
    {
        vtkSmartPointer<vtkBYUReader> reader =
            vtkSmartPointer<vtkBYUReader>::New();
        reader->SetGeometryFileName(fileName);
        reader->Update();
        polyData = reader->GetOutput();
    }
    else
    {
        vtkSmartPointer<vtkSphereSource> source =
            vtkSmartPointer<vtkSphereSource>::New();
        source->Update();
        polyData = source->GetOutput();
    }
    return polyData;
}

结论:

感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!

你的赞赏是我的最最最最大的动力(^U^)ノ~YO

文章来源:https://blog.csdn.net/qq_40041064/article/details/135359867
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。