12、VTK用X、Z平面去切割一个圆锥

红色是留下的部分,透明蓝色是切去的部分

cpp 复制代码
#include "MainWindow.h"

#include <QApplication>
#include <vtkConeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkCamera.h>
#include <vtkProperty.h>
#include <vtkClipPolyData.h>
#include <vtkPlane.h>
#include <vtkAxesActor.h>

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)

int main(int argc, char *argv[])
{
    double h = 1.0f;

    auto cone = vtkSmartPointer<vtkConeSource>::New();
    cone->SetRadius(0.3);
    cone->SetHeight(h);
    cone->SetResolution(100);
    cone->SetDirection(0, 1, 0);

    auto plane = vtkSmartPointer<vtkPlane>::New();
    plane->SetNormal(0, 1, 0); // 希望用X、Z平面去切割,则法向量是Y轴方向
    plane->SetOrigin(0, 0, 0); // 平面过原点

    auto clip = vtkSmartPointer<vtkClipPolyData>::New();
    clip->SetInputConnection(cone->GetOutputPort());
    clip->SetClipFunction(plane);
    clip->SetValue(0);
    clip->GenerateClippedOutputOn(); // 生成切去部分
    clip->Update();

    auto mapperRemain = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapperRemain->SetInputConnection(clip->GetOutputPort()); // 切割留下的部分

    auto actorRemain = vtkSmartPointer<vtkActor>::New();
    actorRemain->SetMapper(mapperRemain);
    actorRemain->GetProperty()->SetColor(1, 0, 0);


    auto mapperClip = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapperClip->SetInputConnection(clip->GetClippedOutputPort()); // 切去的部分

    auto actorClip = vtkSmartPointer<vtkActor>::New();
    actorClip->SetMapper(mapperClip);
    actorClip->GetProperty()->SetColor(0, 0, 1);
    actorClip->GetProperty()->SetOpacity(0.3);


    auto actorAxes = vtkSmartPointer<vtkAxesActor>::New();

    auto render = vtkSmartPointer<vtkRenderer>::New();
    render->SetBackground(0.1, 0.2, 0.3);
    render->AddActor(actorRemain);
    render->AddActor(actorClip);
    render->AddActor(actorAxes);

    render->GetActiveCamera()->SetFocalPoint(0, 0, 0);
    render->GetActiveCamera()->SetPosition(1, 5, 5);

    auto wd = vtkSmartPointer<vtkRenderWindow>::New();
    wd->AddRenderer(render);

    auto interator = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    wd->SetInteractor(interator);
    wd->SetSize(800, 600);
    wd->SetPosition(200, 200);

    wd->Render();
    interator->Start();

    return 0;
}
相关推荐
老歌老听老掉牙8 天前
圆柱端面四平面交线及其在yoz面投影的几何解析与 Python 实现
开发语言·python·平面
无言听风雨丶9 天前
Kubernetes 控制平面深度剖析:从 kubectl apply 到 etcd 写入的全链路
平面·kubernetes·etcd
Solange__11 天前
AI世界模型、3D场景生成和游戏引擎怎么选?按5类任务比较交互、物理与导出
开发语言·人工智能·游戏·平面·3d·自然语言处理·游戏引擎
国科安芯12 天前
商业光学遥感载荷焦平面供电链的低噪声稳压器研究综述——以国科安芯ASP7A84AS型3A低压差线性稳压器为器件级参照
大数据·网络·嵌入式硬件·平面·卫星·商业航天·抗辐射
希和202712 天前
考研高数综合题|微分方程 + 平面面积 + 旋转体体积
考研·平面
beibeix201512 天前
VTK 渲染全流程与 OpenGL 上下文适配
vtk
Evand J14 天前
【MATLAB例程】平面下,5基站对待定位点的定位。基于距离辅助下的AOA。完整代码下载链接
开发语言·matlab·平面·定位·导航·测距·aoa
网络小江19 天前
组网第21课:初登交换机——管理平面三板斧
平面·ssh·console·华为交换机
天人合一peng20 天前
Catia数控加工平面铣削
平面
Suprman21 天前
从 B/S 到 Electron:平面排版套料系统桌面版开发实战
javascript·平面·electron·排序算法