VTK学习笔记(十五)曲线图

一般的曲线图都是没有填充,这个填充了。 

挺有意思的,也很有用的一个功能,在画一些对比图的时候很有用。

#include <vtkChartLegend.h>
#include <vtkChartXY.h>
#include <vtkColor.h>
#include <vtkColorSeries.h>
#include <vtkContextScene.h>
#include <vtkContextView.h>
#include <vtkDoubleArray.h>
#include <vtkLookupTable.h>
#include <vtkMath.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPen.h>
#include <vtkPlotFunctionalBag.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkStringArray.h>
#include <vtkTable.h>

#include <sstream>

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

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  // Creates an input table
  const int numCols = 7;
  const int numVals = 100;

  vtkNew<vtkTable> inputTable;

  vtkNew<vtkDoubleArray> arr[numCols];
  for (int i = 0; i < numCols; i++)
  {
    std::stringstream ss;
    ss << "Y" << i;
    arr[i]->SetName(ss.str().c_str());
    arr[i]->SetNumberOfValues(numVals);
    for (int j = 0; j < numVals; j++)
    {
      arr[i]->SetValue(j,
                       (i + 1) *
                               fabs(sin((j * 2.f * vtkMath::Pi()) /
                                        static_cast<float>(numVals))) *
                               j +
                           i * 20);
    }
    inputTable->AddColumn(arr[i]);
  }

  // Create a X-axis column
  vtkNew<vtkDoubleArray> xArr;
  xArr->SetName("X");
  xArr->SetNumberOfValues(numVals);
  for (int j = 0; j < numVals; j++)
  {
    xArr->SetValue(j, j * 2.0);
  }
  inputTable->AddColumn(xArr);

  // Create the bag columns
  vtkNew<vtkDoubleArray> q3Arr;
  q3Arr->SetName("Q3");
  q3Arr->SetNumberOfComponents(2);
  q3Arr->SetNumberOfTuples(numVals);
  vtkNew<vtkDoubleArray> q2Arr;
  q2Arr->SetName("Q2");
  q2Arr->SetNumberOfComponents(2);
  q2Arr->SetNumberOfTuples(numVals);

  for (int i = 0; i < numVals; i++)
  {
    double v0, v1;
    v0 = arr[1]->GetVariantValue(i).ToFloat();
    v1 = arr[5]->GetVariantValue(i).ToFloat();
    q3Arr->SetTuple2(i, v0, v1);

    v0 = arr[2]->GetVariantValue(i).ToFloat();
    v1 = arr[4]->GetVariantValue(i).ToFloat();
    q2Arr->SetTuple2(i, v0, v1);
  }

  inputTable->AddColumn(q3Arr);
  inputTable->AddColumn(q2Arr);

  // Set up a 2D scene and add an XY chart to it
  vtkNew<vtkContextView> view;
  view->GetRenderWindow()->SetSize(640, 480);
  view->GetRenderWindow()->SetMultiSamples(0);
  view->GetRenderWindow()->SetWindowName("FunctionalBagPlot");

  vtkNew<vtkChartXY> chart;
  chart->SetShowLegend(true);
  chart->GetLegend()->SetHorizontalAlignment(vtkChartLegend::LEFT);
  chart->GetLegend()->SetVerticalAlignment(vtkChartLegend::TOP);

  view->GetScene()->AddItem(chart);

  // Create the functional bag plots
  vtkColor3d color3d = colors->GetColor3d("Tomato");
  vtkNew<vtkPlotFunctionalBag> q3Plot;
  q3Plot->SetColor(color3d.GetRed(), color3d.GetGreen(), color3d.GetBlue());
  q3Plot->SetInputData(inputTable, "X", "Q3");
  chart->AddPlot(q3Plot);

  color3d = colors->GetColor3d("Banana");
  vtkNew<vtkPlotFunctionalBag> q2Plot;
  q2Plot->SetColor(color3d.GetRed(), color3d.GetGreen(), color3d.GetBlue());
  q2Plot->SetInputData(inputTable, "X", "Q2");
  chart->AddPlot(q2Plot);

  vtkNew<vtkColorSeries> colorSeries;
  colorSeries->SetColorScheme(vtkColorSeries::BREWER_QUALITATIVE_SET3);
  ;

  vtkNew<vtkLookupTable> lookup;
  lookup->SetNumberOfColors(numCols);
  lookup->SetRange(0, numCols - 1);
  for (int j = 0; j < numCols; j++)
  {
    vtkNew<vtkPlotFunctionalBag> plot;
    vtkColor3ub color = colorSeries->GetColorRepeating(j);
    lookup->SetTableValue(j, color.GetRed() / 255., color.GetGreen() / 255.,
                          color.GetBlue() / 255., 1.);
    double rgb[3];
    lookup->GetColor(j, rgb);
    plot->SetColor(rgb[0], rgb[1], rgb[2]);
    plot->SetInputData(inputTable, "X", inputTable->GetColumn(j)->GetName());
    plot->GetPen()->SetWidth(3.0);
    chart->AddPlot(plot);
  }

  view->GetRenderer()->SetBackground(colors->GetColor3d("SlateGray").GetData());

  // Render the scene
  view->GetRenderWindow()->Render();
  view->GetInteractor()->Initialize();
  view->GetInteractor()->Start();

  return EXIT_SUCCESS;
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值