VTK笔记-交互器-vtkXYPlotWidget类

vtkXYPlotWidget

  vtkXYPlotWidget用于操纵XY图表的2D小部件;
  vtkXYPlotWidget提供对鼠标交互操作,可以更改XY图表的位置、大小和方向的支持。可以监听鼠标左键事件和鼠标移动。将根据光标的位置更改光标形状。如果光标位于XY图表的边上,则会将光标形状更改为调整大小的边形状。如果XY打印的位置移动到接近视口四条边之一的中心,则XY打印将更改其方向以与该边对齐。此方向是粘性的,因为它将保持该方向,直到该位置移近另一条边。
在这里插入图片描述

接口

设置vtkXYPlotWidget对应Actor

vtkXYPlotActor* XYPlotActor;
virtual void SetXYPlotActor(vtkXYPlotActor*);
vtkGetObjectMacro(XYPlotActor, vtkXYPlotActor);

设置启用

void SetEnabled(int) override;

代码

  从流体数据中提取数据绘制图表;

#include "pch.h"
#include <iostream>

#include <vtkActor.h>
#include <vtkAppendPolyData.h>
#include <vtkCamera.h>
#include <vtkLineSource.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkMultiBlockPLOT3DReader.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProbeFilter.h>
#include <vtkProperty.h>
#include <vtkProperty2D.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkStructuredGrid.h>
#include <vtkStructuredGridOutlineFilter.h>
#include <vtkTextProperty.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkTubeFilter.h>
#include <vtkXYPlotActor.h>
#include <vtkXYPlotWidget.h>
#include "vtkAutoInit.h" 
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkInteractionStyle);

using namespace std;

int main(){
    vtkNew<vtkMultiBlockPLOT3DReader> pl3d;
    pl3d->SetXYZFileName("E:\\combxyz.bin");
    pl3d->SetQFileName("E:\\combq.bin");
    pl3d->SetScalarFunctionNumber(100);
    pl3d->SetVectorFunctionNumber(202);
    pl3d->Update();

    auto pl3d_output = dynamic_cast<vtkStructuredGrid*>(pl3d->GetOutput()->GetBlock(0));

    // Create three line sources to use for the probe lines.
    vtkNew<vtkLineSource> line;
    line->SetResolution(30);

    // Move the line into place and create the probe filter. For
    // vtkProbeFilter, the probe line is the input, and the underlying data
    // set is the source.
    vtkNew<vtkTransform> transL1;
    transL1->Translate(3.7, 0.0, 28.37);
    transL1->Scale(5, 5, 5);
    transL1->RotateY(90);

    vtkNew<vtkTransformPolyDataFilter> tf;
    tf->SetInputConnection(line->GetOutputPort());
    tf->SetTransform(transL1);

    vtkNew<vtkProbeFilter> probe;
    probe->SetInputConnection(tf->GetOutputPort());
    probe->SetSourceData(pl3d_output);

    // Move the line again and create another probe filter.
    vtkNew<vtkTransform> transL2;
    transL2->Translate(9.2, 0.0, 31.20);
    transL2->Scale(5, 5, 5);
    transL2->RotateY(90);

    vtkNew<vtkTransformPolyDataFilter> tf2;
    tf2->SetInputConnection(line->GetOutputPort());
    tf2->SetTransform(transL2);
    vtkNew<vtkProbeFilter> probe2;
    probe2->SetInputConnection(tf2->GetOutputPort());
    probe2->SetSourceData(pl3d_output);

    // Move the line again and create a third probe filter.
    vtkNew<vtkTransform> transL3;
    transL3->Translate(13.27, 0.0, 33.40);
    transL3->Scale(4.5, 4.5, 4.5);
    transL3->RotateY(90);
    vtkNew<vtkTransformPolyDataFilter> tf3;
    tf3->SetInputConnection(line->GetOutputPort());
    tf3->SetTransform(transL3);
    vtkNew<vtkProbeFilter> probe3;
    probe3->SetInputConnection(tf3->GetOutputPort());
    probe3->SetSourceData(pl3d_output);

    // Create a vtkAppendPolyData to merge the output of the three probe
    // filters into one data set.
    vtkNew<vtkAppendPolyData> appendF;
    appendF->AddInputConnection(probe->GetOutputPort());
    appendF->AddInputConnection(probe2->GetOutputPort());
    appendF->AddInputConnection(probe3->GetOutputPort());

    // Create a tube filter to represent the lines as tubes.  Set up the
    // associated mapper and actor.
    vtkNew<vtkTubeFilter> tuber;
    tuber->SetInputConnection(appendF->GetOutputPort());
    tuber->SetRadius(0.1);
    vtkNew<vtkPolyDataMapper> lineMapper;
    lineMapper->SetInputConnection(tuber->GetOutputPort());
    vtkNew<vtkActor> lineActor;
    lineActor->SetMapper(lineMapper);

    // Create an xy-plot using the output of the 3 probe filters as input.
    // The x-values we are plotting are arc length.
    vtkNew<vtkNamedColors> colors;
    vtkNew<vtkXYPlotActor> xyplot;
    xyplot->AddDataSetInputConnection(probe->GetOutputPort());
    xyplot->AddDataSetInputConnection(probe2->GetOutputPort());
    xyplot->AddDataSetInputConnection(probe3->GetOutputPort());
    xyplot->GetPositionCoordinate()->SetValue(0.0, 0.67, 0);
    xyplot->GetPosition2Coordinate()->SetValue(1.0, 0.33, 0); // relative to Position;
    xyplot->SetXValuesToArcLength();
    xyplot->SetNumberOfXLabels(6);
    xyplot->SetTitle("Pressure vs. Arc Length (Zoomed View)");
    xyplot->SetXTitle("");
    xyplot->SetYTitle("P");
    xyplot->SetXRange(.1, .35);
    xyplot->SetYRange(.2, .4);
    xyplot->GetProperty()->SetColor(colors->GetColor3d("Black").GetData());
    xyplot->GetProperty()->SetLineWidth(2);
    // Set text prop color
    // Assign same object to all text props
    vtkTextProperty* tprop = xyplot->GetTitleTextProperty();
    tprop->SetColor(xyplot->GetProperty()->GetColor());
    xyplot->SetAxisTitleTextProperty(tprop);
    xyplot->SetAxisLabelTextProperty(tprop);

    // Create an xy-plot using the output of the 3 probe filters as input.
    // The x-values we are plotting are normalized arc length.
    vtkNew<vtkXYPlotActor> xyplot2;
    xyplot2->AddDataSetInputConnection(probe->GetOutputPort());
    xyplot2->AddDataSetInputConnection(probe2->GetOutputPort());
    xyplot2->AddDataSetInputConnection(probe3->GetOutputPort());
    xyplot2->GetPositionCoordinate()->SetValue(0.00, 0.33, 0);
    xyplot2->GetPosition2Coordinate()->SetValue(1.0, 0.33, 0); // relative to Position;
    xyplot2->SetXValuesToNormalizedArcLength();
    xyplot2->SetNumberOfXLabels(6);
    xyplot2->SetTitle("Pressure vs. Normalized Arc Length");
    xyplot2->SetXTitle("");
    xyplot2->SetYTitle("P");
    xyplot2->PlotPointsOn();
    xyplot2->PlotLinesOff();
    xyplot2->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());
    xyplot2->GetProperty()->SetPointSize(5);
    // Set text prop color
    // Assign same object to all text props
    tprop = xyplot2->GetTitleTextProperty();
    tprop->SetColor(xyplot2->GetProperty()->GetColor());
    xyplot2->SetAxisTitleTextProperty(tprop);
    xyplot2->SetAxisLabelTextProperty(tprop);

    // Create an xy-plot using the output of the 3 probe filters as input.
    // The x-values we are plotting are the underlying point data values.
    vtkNew<vtkXYPlotActor> xyplot3;
    xyplot3->AddDataSetInputConnection(probe->GetOutputPort());
    xyplot3->AddDataSetInputConnection(probe2->GetOutputPort());
    xyplot3->AddDataSetInputConnection(probe3->GetOutputPort());
    xyplot3->GetPositionCoordinate()->SetValue(0.0, 0.0, 0);
    xyplot3->GetPosition2Coordinate()->SetValue(1.0, 0.33, 0); // relative to Position;
    xyplot3->SetXValuesToIndex();
    xyplot3->SetNumberOfXLabels(6);
    xyplot3->SetTitle("Pressure vs. Point Id");
    xyplot3->SetXTitle("Probe Length");
    xyplot3->SetYTitle("P");
    xyplot3->PlotPointsOn();
    xyplot3->GetProperty()->SetColor(colors->GetColor3d("Peacock").GetData());
    xyplot3->GetProperty()->SetPointSize(3);
    // Set text prop color
    // Assign same object to all text props
    tprop = xyplot3->GetTitleTextProperty();
    tprop->SetColor(xyplot3->GetProperty()->GetColor());
    xyplot3->SetAxisTitleTextProperty(tprop);
    xyplot3->SetAxisLabelTextProperty(tprop);

    // Draw an outline of the PLOT3D data set.
    vtkNew<vtkStructuredGridOutlineFilter> outline;
    outline->SetInputData(pl3d_output);
    vtkNew<vtkPolyDataMapper> outlineMapper;
    outlineMapper->SetInputConnection(outline->GetOutputPort());
    vtkNew<vtkActor> outlineActor;
    outlineActor->SetMapper(outlineMapper);
    outlineActor->GetProperty()->SetColor(colors->GetColor3d("Black").GetData());

    // Create the Renderers, RenderWindow, and RenderWindowInteractor.
    vtkNew<vtkRenderer> renderer;
    vtkNew<vtkRenderer> renderer2;
    vtkNew<vtkRenderWindow> renderWindow;
    renderWindow->AddRenderer(renderer);
    renderWindow->AddRenderer(renderer2);
    vtkNew<vtkRenderWindowInteractor> interactor;
    interactor->SetRenderWindow(renderWindow);
    vtkNew<vtkXYPlotWidget> plotWidget;
    plotWidget->SetInteractor(interactor);
    plotWidget->SetXYPlotActor(xyplot3);
    plotWidget->EnabledOn();
    // Set the background, viewport (necessary because we want to have the
    // renderers draw to different parts of the render window) of the first
    // renderer.  Add the outline and line actors to the renderer.
    renderer->SetBackground(colors->GetColor3d("blue_light").GetData());
    renderer->SetViewport(0, 0, .5, 1);
    renderer->AddActor(outlineActor);
    renderer->AddActor(lineActor);

    // Set the background and viewport of the second renderer.  Add the
    // xy-plot actors to the renderer.  Set the size of the render window.
    renderer2->SetBackground(colors->GetColor3d("silver").GetData());
    renderer2->SetViewport(0.5, 0.0, 1.0, 1.0);
    renderer2->AddActor2D(xyplot);
    renderer2->AddActor2D(xyplot2);
    renderer2->AddActor2D(xyplot3);
    renderWindow->SetSize(800, 600);

    // Set up the camera parameters.
    vtkCamera* cam1 = renderer->GetActiveCamera();
    cam1->SetClippingRange(3.95297, 100);
    cam1->SetFocalPoint(8.88908, 0.595038, 29.3342);
    cam1->SetPosition(-12.3332, 31.7479, 41.2387);
    cam1->SetViewUp(0.060772, -0.319905, 0.945498);

    interactor->Initialize();
    renderWindow->SetWindowName("XYPlot");
    renderWindow->Render();
    interactor->Start();

	system("pause");
	return 0;
}

在这里插入图片描述

参考文献

1.Widget例子

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

黑山老妖的笔记本

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值