【VTKExamples::Visualization】第一期 Arbitrary3DCursor

发布时间:2024年01月07日

很高兴在雪易的CSDN遇见你?

VTK技术爱好者 QQ:870202403


前言

本文分享Example中Visualization模块中的Arbitrary3DCursor样例,主要解析vtkProbefileter,希望对各位小伙伴有所帮助!

感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!

你的点赞就是我的动力(^U^)ノ~YO


1. vtkProbeFilter

? ? ? ? ?在指定的点位置采样数据值。

????????vtkProbeFilter是一个过滤器,在指定的点位置计算点属性(例如,标量,向量等)。过滤器有两个输入: Input和Source。输入的几何结构通过过滤器。输入点位置的Point属性通过插值到Source数据。例如:我们可以通过Volume(Source数据)计算平面上的数据值(平面指定为输入)。Source数据中的Cell数据根据每个输入点所在的源单元格复制到输出。如果在源的点数据和单元数据中都存在同名的数组,则只探测来自点数据的数组。

????????此过滤器可用于重新采样数据,或将一个数据集表单转换为另一个数据集表单。例如,一个非结构化网格(vtkUnstructuredGrid)可以用一个体(三维vtkImageData)来探测,然后体渲染技术可以用来可视化结果。另一个例子:一条线或曲线可以用来探测数据,从而沿着这条线或曲线生成x-y图。

2. 应用示例

??

?

// This does the actual work: updates the probe.
// Callback for the interaction
class vtkmyPWCallback : public vtkCommand
{
public:
    vtkmyPWCallback() = default;

    static vtkmyPWCallback* New()
    {
        return new vtkmyPWCallback;
    }
    virtual void Execute(vtkObject* caller, unsigned long, void*)
    {
        vtkPointWidget* pointWidget = reinterpret_cast<vtkPointWidget*>(caller);
        pointWidget->GetPolyData(this->PolyData);
        double position[3];
        pointWidget->GetPosition(position);
        std::ostringstream text;
        text << "cursor: " << std::fixed << std::setprecision(4) << position[0]
            << ", " << position[1] << ", " << position[2];
        this->PositionActor->SetInput(text.str().c_str());
        this->CursorActor->VisibilityOn();
    }

    vtkPolyData* PolyData = nullptr;
    vtkActor* CursorActor = nullptr;
    vtkTextActor* PositionActor = nullptr;
};

void QtWidgetsApplication1::on_arbitrary3DCursorAct_triggered()
{
    vtkSmartPointer<vtkPolyData> inputPolyData;



    auto line = vtkSmartPointer<vtkLineSource>::New();
    line->SetResolution(2);
    line->SetPoint1(0., 0., 0.);
    line->SetPoint2(10, 0, 0);
    line->Update();
    inputPolyData = line->GetOutput();

    /*auto plane = vtkSmartPointer<vtkPlaneSource>::New();
    plane->Update();
    inputPolyData = plane->GetOutput();*/

    /*auto sphereSource =
        vtkSmartPointer<vtkSphereSource>::New();
    sphereSource->SetPhiResolution(15);
    sphereSource->SetThetaResolution(15);
    sphereSource->Update();
    inputPolyData = sphereSource->GetOutput();*/

    auto colors =
        vtkSmartPointer<vtkNamedColors>::New();

    auto point =
        vtkSmartPointer<vtkPolyData>::New();

    auto probe =
        vtkSmartPointer<vtkProbeFilter>::New();
    probe->SetInputData(point);
    probe->SetSourceData(inputPolyData);

    // create glyph
    auto cone =
        vtkSmartPointer<vtkConeSource>::New();
    cone->SetResolution(16);

    auto glyph =
        vtkSmartPointer<vtkGlyph3D>::New();
    glyph->SetInputConnection(probe->GetOutputPort());
    glyph->SetSourceConnection(cone->GetOutputPort());
    glyph->SetVectorModeToUseVector();
    glyph->SetScaleModeToDataScalingOff();
    glyph->SetScaleFactor(inputPolyData->GetLength() * 0.1);

    auto glyphMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    glyphMapper->SetInputConnection(glyph->GetOutputPort());

    auto glyphActor =
        vtkSmartPointer<vtkActor>::New();
    glyphActor->SetMapper(glyphMapper);
    glyphActor->GetProperty()->SetColor(1.,0.,0.);
    glyphActor->VisibilityOn();

    auto mapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputData(inputPolyData);

    auto actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    actor->GetProperty()->SetRepresentationToWireframe();
    actor->GetProperty()->SetColor(colors->GetColor3d("gold").GetData());

    auto textActor =
        vtkSmartPointer<vtkTextActor>::New();
    textActor->GetTextProperty()->SetFontSize(12);
    textActor->SetPosition(10, 20);
    textActor->SetInput("cursor:");
    textActor->GetTextProperty()->SetColor(0.,1.,0.);

    // Create the RenderWindow, Render1er and both Actors
    //
    auto ren1 =
        vtkSmartPointer<vtkRenderer>::New();
    auto renWin =
        vtkSmartPointer<vtkRenderWindow>::New();
    renWin->AddRenderer(ren1);

    auto iren =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    iren->SetRenderWindow(renWin);

    // The SetInteractor method is how 3D widgets are associated with the render
    // window interactor. Internally, SetInteractor sets up a bunch of callbacks
    // using the Command/Observer mechanism (AddObserver()).
    auto myCallback =
        vtkSmartPointer<vtkmyPWCallback>::New();
    myCallback->PolyData = point;
    myCallback->CursorActor = glyphActor;
    myCallback->PositionActor = textActor;

    // The point widget is used probe the dataset.
    //
    auto pointWidget =
        vtkSmartPointer<vtkPointWidget>::New();
    pointWidget->SetInteractor(iren);
    pointWidget->SetInputData(inputPolyData);
    pointWidget->AllOff();
    pointWidget->PlaceWidget();
    pointWidget->AddObserver(vtkCommand::InteractionEvent, myCallback);

    ren1->AddActor(glyphActor);
    ren1->AddActor(actor);
    ren1->AddActor2D(textActor);

    // Add the actors to the renderer, set the background and size
    //
    ren1->GradientBackgroundOn();
    ren1->SetBackground(colors->GetColor3d("SlateGray").GetData());
    ren1->SetBackground2(colors->GetColor3d("Wheat").GetData());

    renWin->SetSize(300, 300);
    renWin->Render();
    pointWidget->On();
    // render the image
    //
    iren->Initialize();
    renWin->Render();

    iren->Start();
}

结论:

感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!

你的赞赏是我的最最最最大的动力(^U^)ノ~YO

文章来源:https://blog.csdn.net/qq_40041064/article/details/135442941
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。