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;
}
相关推荐
PHOSKEY9 天前
光子精密3D工业相机陶瓷基片平面度检测!赋能电子制造质控升级
平面·3d·3d工业相机
csuzhucong12 天前
平面平铺
平面
AI科技星15 天前
时空的几何动力学:基于光速螺旋运动公设的速度上限定理求导与全维度验证
人工智能·线性代数·算法·机器学习·平面
Evand J17 天前
【UWB与IMU紧耦合定位,MATLAB例程】UWB的TOA定位方法,与IMU紧耦合,对目标轨迹定位并输出误差统计。适用于二维平面的高精度定位导航
开发语言·matlab·平面·uwb·组合导航
zVGKKTvT21 天前
嘿,今天来跟大家分享一下我做的 FPGA 以太网多通道实时同步采集系统,这可是个挺实用且有趣的项目呢
平面
求真求知的糖葫芦21 天前
巴伦学习(三.一)一种可以实现阻抗变换的平面Marchand巴伦的公式推导学习笔记(中)(自用)
笔记·学习·平面·射频工程
芯片SIPI设计21 天前
地平面不连续性对100G PAM4以太网信号完整性的影响
数学建模·平面
求真求知的糖葫芦21 天前
巴伦学习(三.一)一种可以实现阻抗变换的平面Marchand巴伦的公式推导学习笔记(下)(自用)
笔记·学习·平面
AI科技星23 天前
从复平面旋转到三维螺旋:欧拉公式在张祥前统一场论中的几何角色与运动合成
线性代数·算法·机器学习·平面·矩阵·概率论
求真求知的糖葫芦23 天前
巴伦学习(三.一)一种可以实现阻抗变换的平面Marchand巴伦的公式推导学习笔记(上)(自用)
笔记·学习·平面·射频工程