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;
}
相关推荐
Lao A(zhou liang)的菜园5 天前
Oracle双平面适用场景讨论会议
数据库·平面·oracle
flytalei12 天前
理解 Kubernetes 的架构与控制平面组件运行机制
平面·架构·kubernetes
MechMaster17 天前
Halcon计算点到平面的距离没有那么简单
平面
ICT系统集成阿祥20 天前
华为云stack网络平面有哪些?作用及技术实现介绍!
网络·平面·华为云
liang_202622 天前
【HT周赛】T3.二维平面 题解(分块:矩形chkmax,求矩形和)
数据结构·笔记·学习·算法·平面·总结
猎板阿权1 个月前
出于PCB设计层面考虑,连排半孔需要注意哪些事项?
单片机·物联网·平面
惊鸿一博1 个月前
几何_平面方程表示_点+向量形式
平面
光电大美美-见合八方中国芯1 个月前
【平面波导外腔激光器专题系列】1064nm单纵模平面波导外腔激光器‌
网络·数据库·人工智能·算法·平面·性能优化
青花瓷1 个月前
空间内任意点到直线和平面的距离推导
数学·平面·解析几何
yuanpan1 个月前
平面坐标系中判断点P是否在线段上AB上的常用方法总结
开发语言·python·平面·点线关系