Vtk裁剪功能之平面裁剪vtkClipClosedSurface(vtk小记)

1.原理分析

对你的三维图形,使用一个平面切下去,然后保留一半。

确定一个平面:使用法向量和一个三维坐标点可以确定一个平面

原始图像

切一刀

切两刀,又一半

切三刀,又一半

源代码

cpp 复制代码
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkClipClosedSurface.h>
#include <vtkDataSetMapper.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlane.h>
#include <vtkPlaneCollection.h>
#include <vtkPolyData.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkXMLPolyDataReader.h>

int main(int argc, char* argv[])
{
    vtkNew<vtkNamedColors> colors;

    // PolyData to process
    vtkSmartPointer<vtkPolyData> polyData;

    if (argc > 1)
    {
        vtkNew<vtkXMLPolyDataReader> reader;
        reader->SetFileName(argv[1]);
        reader->Update();
        polyData = reader->GetOutput();
    }
    else
    {
        // Create a sphere
        vtkNew<vtkSphereSource> sphereSource;
        sphereSource->SetThetaResolution(20);
        sphereSource->SetPhiResolution(11);
        sphereSource->Update();

        polyData = sphereSource->GetOutput();
    }

    auto center = polyData->GetCenter();
    vtkNew<vtkPlane> plane1;
    plane1->SetOrigin(center[0], center[1], center[2]);
    plane1->SetNormal(0.0, -1.0, 0.0);
    vtkNew<vtkPlane> plane2;
    plane2->SetOrigin(center[0], center[1], center[2]);
    plane2->SetNormal(0.0, 0.0, 1.0);
    vtkNew<vtkPlane> plane3;
    plane3->SetOrigin(center[0], center[1], center[2]);
    plane3->SetNormal(-1.0, 0.0, 0.0);

    vtkNew<vtkPlaneCollection> planes;
    planes->AddItem(plane1);
    planes->AddItem(plane2);
     planes->AddItem(plane3);

    vtkNew<vtkClipClosedSurface> clipper;
    clipper->SetInputData(polyData);
    clipper->SetClippingPlanes(planes);
    clipper->SetActivePlaneId(2);
    clipper->SetScalarModeToColors();
    clipper->SetClipColor(colors->GetColor3d("Banana").GetData());
    clipper->SetBaseColor(colors->GetColor3d("Tomato").GetData());
    clipper->SetActivePlaneColor(colors->GetColor3d("SandyBrown").GetData());

    vtkNew<vtkDataSetMapper> clipMapper;
    clipMapper->SetInputConnection(clipper->GetOutputPort());

    vtkNew<vtkActor> clipActor;
    clipActor->SetMapper(clipMapper);
    clipActor->GetProperty()->SetColor(1.0000, 0.3882, 0.2784);
    clipActor->GetProperty()->SetInterpolationToFlat();

    // Create graphics stuff
    //
    vtkNew<vtkRenderer> ren1;
    ren1->SetBackground(colors->GetColor3d("SteelBlue").GetData());

    vtkNew<vtkRenderWindow> renWin;
    renWin->AddRenderer(ren1);
    renWin->SetSize(512, 512);
    renWin->SetWindowName("ClipClosedSurface");

    vtkNew<vtkRenderWindowInteractor> iren;
    iren->SetRenderWindow(renWin);

    // Add the actors to the renderer, set the background and size
    //
    ren1->AddActor(clipActor);

    // Generate an interesting view
    //
    ren1->ResetCamera();
    ren1->GetActiveCamera()->Azimuth(120);
    ren1->GetActiveCamera()->Elevation(30);
    ren1->GetActiveCamera()->Dolly(1.0);
    ren1->ResetCameraClippingRange();

    renWin->Render();
    iren->Initialize();
    iren->Start();

    return EXIT_SUCCESS;
}
相关推荐
程途拾光1582 天前
一键生成幼儿园安全疏散平面图设计美观合规又省心
论文阅读·安全·平面·毕业设计·流程图·课程设计·论文笔记
刘叨叨趣味运维4 天前
解剖K8s控制平面(上):API Server与etcd如何成为集群的“大脑“与“记忆“?
平面·kubernetes·etcd
Ulyanov6 天前
超越平面:用impress.js打造智能多面棱柱演示器
开发语言·前端·javascript·平面
求真求知的糖葫芦7 天前
微波工程4.3节散射矩阵(S参数矩阵)参考平面移动与广义散射参数学习笔记(下)(自用)
学习·平面·矩阵·射频工程
yongui478349 天前
实现线结构光技术的摄像机标定、光平面标定与三维重建
数码相机·平面
咯哦哦哦哦12 天前
pick_and_place_with_2d_matching_moving_cam.hdev *眼在手上 2D匹配,3D抓取【案例解析】
计算机视觉·平面·3d
Σίσυφος190014 天前
halcon 和PCL 中平面矫正算法原理
平面
Matlab光学21 天前
MATLAB仿真:从平面到立体! 3D 曲线光束塑形技术,微观操控再升级
平面·3d
杀生丸学AI1 个月前
【平面重建】3D高斯平面:混合2D/3D光场重建(NeurIPS2025)
人工智能·平面·3d·大模型·aigc·高斯泼溅·空间智能
fengfuyao9851 个月前
基于MATLAB实现任意平面太阳辐射量计算
算法·matlab·平面