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;
}
相关推荐
猛码Memmat10 小时前
正六边形:平面密铺图形中,单位面积周长最短的图形
平面·性能优化·最值问题·最优解
ChoSeitaku12 小时前
高数强化NO19|积分学的应用|平面图形|简单几何体|
平面
Evand J14 小时前
【雷达跟踪与滤波|MATLAB例程】平面上的雷达跟踪与UKF(无迹卡尔曼滤波),估计目标轨迹,输出真值、误差曲线、误差特性等
matlab·平面·滤波·ukf·无迹卡尔曼滤波
BlackPercy4 天前
[LaTeX] Tikz 平面绘图
平面
YJlio6 天前
第9章小结(9.19):Sysinternals 安全工具组合拳与脚本清单
java·学习·平面
三维小码9 天前
平面诱导单应性矩阵
平面·矩阵
stormsha12 天前
裸眼3D原理浅析AI如何生成平面裸眼3D图像以科幻战士破框而出为例
人工智能·计算机视觉·平面·3d·ai
报错小能手1 个月前
计算机网络自顶向下方法43——网络层 详解SDN控制平面
计算机网络·平面
Evand J1 个月前
【MATLAB例程】二维平面上,三个雷达对一个目标跟踪,输出观测平均与UKF滤波两种算法的结果对比,附下载链接
matlab·平面·目标跟踪·滤波·卡尔曼滤波
老黄编程1 个月前
三维空间平面方程
线性代数·平面