TestZoomAxis.cxx 3.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    TestZoomAxis.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

#include "vtkRenderWindow.h"
#include "vtkSmartPointer.h"
#include "vtkChartXY.h"
#include "vtkPlot.h"
#include "vtkTable.h"
#include "vtkFloatArray.h"
#include "vtkContextView.h"
#include "vtkContextScene.h"
#include "vtkContextMouseEvent.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkAnnotationLink.h"
#include "vtkNew.h"

//----------------------------------------------------------------------------
int TestZoomAxis(int, char * [])
{
  // Set up a 2D scene, add an XY chart to it
  vtkNew<vtkContextView> view;
  view->GetRenderWindow()->SetSize(400, 300);
  vtkNew<vtkChartXY> chart;
  view->GetScene()->AddItem(chart.GetPointer());
  vtkNew<vtkAnnotationLink> link;
  chart->SetAnnotationLink(link.GetPointer());
  chart->SetActionToButton(vtkChart::ZOOM_AXIS,
                           vtkContextMouseEvent::LEFT_BUTTON);
  chart->SetSelectionMethod(vtkChart::SELECTION_PLOTS);

  // Create a table with some points in it...
  vtkNew<vtkTable> table;
  vtkNew<vtkFloatArray> arrX;
  arrX->SetName("X Axis");
  table->AddColumn(arrX.GetPointer());
  vtkNew<vtkFloatArray> arrS;
  arrS->SetName("Sine");
  table->AddColumn(arrS.GetPointer());
  // Test charting with a few more points...
  int numPoints = 100;
  float inc = 9.5f / (numPoints-1);
  table->SetNumberOfRows(numPoints);
  for (int i = 0; i < numPoints; ++i)
    {
    table->SetValue(i, 0, i * inc);
    table->SetValue(i, 1, sin(i * inc));
    }

  // Add multiple line plots, setting the colors etc
  vtkPlot *plot = chart->AddPlot(vtkChart::POINTS);
  plot->SetInputData(table.GetPointer(), 0, 1);
  plot->SetColor(0, 255, 0, 255);
  plot->SetWidth(1.0);

  view->Update();
  view->Render();

  // Inject some mouse events to perform zooming
  vtkContextMouseEvent event;
72 73 74 75 76
  event.SetLastPos(vtkVector2f(0.0f));
  event.SetPos(vtkVector2f(0.0f));
  event.SetLastScenePos(vtkVector2f(0.0f));
  event.SetScenePos(vtkVector2f(0.0f));
  event.SetLastScreenPos(vtkVector2i(0));
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  event.SetInteractor(view->GetInteractor());
  event.SetButton(vtkContextMouseEvent::LEFT_BUTTON);
  event.SetScreenPos(vtkVector2i(350, 250));
  chart->MouseButtonPressEvent(event);
  event.SetLastScreenPos(event.GetScreenPos());
  event.SetScreenPos(vtkVector2i(10, 10));
  chart->MouseMoveEvent(event);
  chart->MouseButtonReleaseEvent(event);

  //Finally render the scene and compare the image to a reference image
  view->GetRenderWindow()->SetMultiSamples(0);
  view->GetInteractor()->Initialize();
  view->GetInteractor()->Start();
  return EXIT_SUCCESS;
}