vtkPolyData 生成轮廓线

本文详细介绍了如何使用VTK库中的类如vtkPolyData、vtkCutter和vtkMapper在给定的PolyData对象上创建切片并显示轮廓,以实现三维数据的可视化。
摘要由CSDN通过智能技术生成

 PolyData 的轮廓用法实战

#include <vtkActor.h>
#include <vtkCutter.h>
#include <vtkMath.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlane.h>
#include <vtkPolyDataMapper.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;

    vtkSmartPointer<vtkPolyData> inputPolyData;
    if (argc > 1)
    {
        vtkNew<vtkXMLPolyDataReader> reader;
        reader->SetFileName(argv[1]);
        reader->Update();
        inputPolyData = reader->GetOutput();
    }
    else
    {
        vtkNew<vtkSphereSource> sphereSource;
        sphereSource->SetThetaResolution(30);
        sphereSource->SetPhiResolution(15);
        sphereSource->Update();
        inputPolyData = sphereSource->GetOutput();
    }

    vtkNew<vtkPolyDataMapper> inputMapper;
    inputMapper->SetInputData(inputPolyData);

    // Create a plane to cut
    vtkNew<vtkPlane> plane;
    plane->SetOrigin(inputPolyData->GetCenter());
    plane->SetNormal(1, 1, 1);

    double minBound[3];
    minBound[0] = inputPolyData->GetBounds()[0];
    minBound[1] = inputPolyData->GetBounds()[2];
    minBound[2] = inputPolyData->GetBounds()[4];

    double maxBound[3];
    maxBound[0] = inputPolyData->GetBounds()[1];
    maxBound[1] = inputPolyData->GetBounds()[3];
    maxBound[2] = inputPolyData->GetBounds()[5];

    double center[3];
    center[0] = inputPolyData->GetCenter()[0];
    center[1] = inputPolyData->GetCenter()[1];
    center[2] = inputPolyData->GetCenter()[2];

    double distanceMin = sqrt(vtkMath::Distance2BetweenPoints(minBound, center));
    double distanceMax = sqrt(vtkMath::Distance2BetweenPoints(maxBound, center));

    // Create cutter.
    vtkNew<vtkCutter> cutter;
    cutter->SetCutFunction(plane);
    cutter->SetInputData(inputPolyData);

    cutter->GenerateValues(20, -distanceMin, distanceMax);
    vtkNew<vtkPolyDataMapper> cutterMapper;
    cutterMapper->SetInputConnection(cutter->GetOutputPort());
    cutterMapper->ScalarVisibilityOff();

    // Create plane actor.
    vtkNew<vtkActor> planeActor;
    planeActor->GetProperty()->SetColor(
        colors->GetColor3d("Deep_pink").GetData());
    planeActor->GetProperty()->SetLineWidth(5);
    planeActor->SetMapper(cutterMapper);

    // Create input actor.
    vtkNew<vtkActor> inputActor;
    inputActor->GetProperty()->SetColor(colors->GetColor3d("Bisque").GetData());
    inputActor->SetMapper(inputMapper);

    // Create renderers and add actors of plane and cube.
    vtkNew<vtkRenderer> renderer;
    renderer->AddActor(planeActor); // Display the rectangle resulting from the
                                    // cut.
    renderer->AddActor(inputActor); // Display the cube.

    // Add renderer to renderwindow and render
    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->AddRenderer(renderer);
    renderWindow->SetWindowName("ContoursFromPolyData");
    renderWindow->SetSize(600, 600);

    vtkNew<vtkRenderWindowInteractor> interactor;
    interactor->SetRenderWindow(renderWindow);
    renderer->SetBackground(colors->GetColor3d("Slate_grey").GetData());
    renderWindow->Render();

    interactor->Start();

    return EXIT_SUCCESS;
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恋恋西风

up up up

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值