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;
}
相关推荐
EQ-雪梨蛋花汤16 小时前
【裸眼3D原理浅析】使用AI生成平面裸眼3D图像——“科幻战士破框而出”的裸眼3D图背后的原理与技巧
平面·3d
Evand J4 天前
【MATLAB例程】到达角度定位(AOA),平面环境多锚点定位(自适应基站数量),动态轨迹使用EKF滤波优化。附代码下载链接
开发语言·matlab·平面·滤波·aoa·到达角度
夜星辰20235 天前
RK3568 MIPI 摄像头驱动的 V4L2 多平面视频格式解析
平面·摄像头格式·nv12
鼓掌MVP11 天前
图生3D技术解析:从二维平面到立体世界的智能飞跃
平面·3d
老歌老听老掉牙12 天前
使用 OpenCASCADE 提取布尔运算后平面图形的外轮廓
c++·平面·opencascade
Kingsdesigner15 天前
从平面到“货架”:Illustrator与Substance Stager的包装设计可视化工作流
前端·平面·illustrator·设计师·substance 3d·平面设计·产品渲染
xinxiangwangzhi_15 天前
多视图几何--密集匹配--视差平面推导
平面
lqjun082715 天前
平面的方程公式
线性代数·机器学习·平面
Evand J20 天前
组合导航的MATLAB例程,二维平面上的CKF滤波,融合IMU和GNSS数据,仿真,观测为X和Y轴的坐标,附代码下载链接
开发语言·matlab·平面·imu·组合导航
Xvisio诠视科技1 个月前
超越平面交互:SLAM技术如何驱动MR迈向空间计算时代?诠视科技以算法引领变革
平面·mr·空间计算