Commit 7ef9d7a4 authored by Zack Galbreath's avatar Zack Galbreath

refactor vtkChartXYZ

It now follows the API of the 2D charts more closely.  vtkChartXYZ is
responsible for the axes, while the subclasses of vtkPlot3D handle
displaying the actual data.  As part of this effort, I've included
optional interactivity into vtkChartXYZ.  This eliminates the need for a
separate vtkInteractiveChartXYZ class.

This change also introduces new functionality.  vtkPlotSurface allows us
to visualize a table as a 3D surface plot.

I've updated vtkScatterPlotMatrix so that it correctly uses the new API
of vtkChartXYZ for animation.

All affected tests were updated as well.
Change-Id: Ic8406c99758a98851949c6153129f7704784e31a
parent f2327f6f
......@@ -16,12 +16,12 @@ set(Module_SRCS
vtkCompositeTransferFunctionItem.cxx
vtkContextPolygon.cxx
vtkControlPointsItem.cxx
vtkInteractiveChartXYZ.cxx
vtkLookupTableItem.cxx
vtkPiecewiseControlPointsItem.cxx
vtkPiecewiseFunctionItem.cxx
vtkPiecewisePointHandleItem.cxx
vtkPlot.cxx
vtkPlot3D.cxx
vtkPlotBar.cxx
vtkPlotGrid.cxx
vtkPlotHistogram2D.cxx
......@@ -29,7 +29,9 @@ set(Module_SRCS
vtkPlotParallelCoordinates.cxx # This adds a vtkInfovisCore dep for one class...
vtkPlotPie.cxx
vtkPlotPoints.cxx
vtkPlotPoints3D.cxx
vtkPlotStacked.cxx
vtkPlotSurface.cxx
vtkScalarsToColorsItem.cxx
vtkScatterPlotMatrix.cxx
)
......
......@@ -41,6 +41,7 @@
TestScientificPlot.cxx
TestStackedBarGraph.cxx
TestStackedPlot.cxx
TestSurfacePlot.cxx
)
# Set the tolerance higher for a few tests that need it
set(TestGLSLError 12)
......
......@@ -17,6 +17,7 @@
#include "vtkContextView.h"
#include "vtkContextScene.h"
#include "vtkFloatArray.h"
#include "vtkPlotPoints3D.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
......@@ -51,10 +52,15 @@ int TestChartXYZ(int , char * [])
{
// Now the chart
vtkNew<vtkChartXYZ> chart;
chart->SetAutoRotate(true);
chart->SetFitToScene(false);
chart->SetDecorateAxes(false);
vtkNew<vtkContextView> view;
view->GetRenderWindow()->SetSize(400, 300);
view->GetScene()->AddItem(chart.GetPointer());
vtkNew<vtkChartXYZ> chart2;
chart2->SetFitToScene(false);
chart->SetDecorateAxes(false);
view->GetScene()->AddItem(chart2.GetPointer());
chart->SetGeometry(vtkRectf(75.0, 20.0, 250, 260));
......@@ -85,14 +91,14 @@ int TestChartXYZ(int , char * [])
//chart->SetAroundX(true);
// Add the three dimensions we are interested in visualizing.
chart->SetInput(table.GetPointer(), "X Axis", "Sine", "Cosine");
chart->RecalculateBounds();
chart->RecalculateTransform();
vtkNew<vtkPlotPoints3D> plot;
plot->SetInputData(table.GetPointer(), "X Axis", "Sine", "Cosine");
chart->AddPlot(plot.GetPointer());
// We want a duplicate, that does not move.
chart2->SetInput(table.GetPointer(), "X Axis", "Sine", "Cosine");
chart2->RecalculateBounds();
chart2->RecalculateTransform();
vtkNew<vtkPlotPoints3D> plot2;
plot2->SetInputData(table.GetPointer(), "X Axis", "Sine", "Cosine");
chart2->AddPlot(plot2.GetPointer());
view->GetRenderWindow()->SetMultiSamples(0);
view->GetInteractor()->Initialize();
......
......@@ -13,12 +13,13 @@
=========================================================================*/
#include "vtkInteractiveChartXYZ.h"
#include "vtkChartXYZ.h"
#include "vtkContextMouseEvent.h"
#include "vtkContextView.h"
#include "vtkContextScene.h"
#include "vtkFloatArray.h"
#include "vtkNew.h"
#include "vtkPlotPoints3D.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
......@@ -30,7 +31,7 @@
int TestInteractiveChartXYZ(int , char * [])
{
// Now the chart
vtkNew<vtkInteractiveChartXYZ> chart;
vtkNew<vtkChartXYZ> chart;
vtkNew<vtkContextView> view;
view->GetRenderWindow()->SetSize(400, 300);
view->GetScene()->AddItem(chart.GetPointer());
......@@ -65,9 +66,9 @@ int TestInteractiveChartXYZ(int , char * [])
}
// Add the dimensions we are interested in visualizing.
chart->SetInput(table.GetPointer(), "X Axis", "Sine", "Cosine", "Color");
chart->RecalculateBounds();
chart->RecalculateTransform();
vtkNew<vtkPlotPoints3D> plot;
plot->SetInputData(table.GetPointer(), "X Axis", "Sine", "Cosine", "Color");
chart->AddPlot(plot.GetPointer());
view->GetRenderWindow()->SetMultiSamples(0);
view->GetInteractor()->Initialize();
......
......@@ -74,6 +74,16 @@ int TestScatterPlotMatrix(int, char * [])
matrix->GetMainChart()->SetActionToButton(vtkChart::SELECT_POLYGON,
vtkContextMouseEvent::RIGHT_BUTTON);
// Test animation by releasing a right click on subchart (1,2)
vtkContextMouseEvent mouseEvent;
mouseEvent.SetInteractor(view->GetInteractor());
vtkVector2f pos;
mouseEvent.SetButton(vtkContextMouseEvent::RIGHT_BUTTON);
pos.Set(245, 301);
mouseEvent.SetPos(pos);
matrix->MouseButtonReleaseEvent(mouseEvent);
//Finally render the scene and compare the image to a reference image
view->GetRenderWindow()->SetMultiSamples(0);
view->GetInteractor()->Initialize();
......
/*=========================================================================
Program: Visualization Toolkit
Module: TestSurfacePlot.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 "vtkChartXYZ.h"
#include "vtkContextMouseEvent.h"
#include "vtkContextView.h"
#include "vtkContextScene.h"
#include "vtkFloatArray.h"
#include "vtkNew.h"
#include "vtkPlotSurface.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkTable.h"
#include "vtkRegressionTestImage.h"
#include "vtkUnsignedCharArray.h"
#include "vtkVector.h"
int TestSurfacePlot(int , char * [])
{
vtkNew<vtkChartXYZ> chart;
vtkNew<vtkPlotSurface> plot;
vtkNew<vtkContextView> view;
view->GetRenderWindow()->SetSize(400, 300);
view->GetScene()->AddItem(chart.GetPointer());
chart->SetGeometry(vtkRectf(75.0, 20.0, 250, 260));
// Create a surface
vtkNew<vtkTable> table;
float numPoints = 70;
float inc = 9.424778 / (numPoints - 1);
for (float i = 0; i < numPoints; ++i)
{
vtkNew<vtkFloatArray> arr;
table->AddColumn(arr.GetPointer());
}
table->SetNumberOfRows(numPoints);
for (float i = 0; i < numPoints; ++i)
{
float x = i * inc;
for (float j = 0; j < numPoints; ++j)
{
float y = j * inc;
table->SetValue(i, j, sin(sqrt(x*x + y*y)));
}
}
// Set up the surface plot we wish to visualize and add it to the chart.
plot->SetXRange(0, 9.424778);
plot->SetYRange(0, 9.424778);
plot->SetInputData(table.GetPointer());
chart->AddPlot(plot.GetPointer());
view->GetRenderWindow()->SetMultiSamples(0);
view->GetInteractor()->Initialize();
view->GetRenderWindow()->Render();
// rotate
vtkContextMouseEvent mouseEvent;
mouseEvent.SetInteractor(view->GetInteractor());
vtkVector2i pos;
vtkVector2i lastPos;
mouseEvent.SetButton(vtkContextMouseEvent::LEFT_BUTTON);
lastPos.Set(100, 50);
mouseEvent.SetLastScreenPos(lastPos);
pos.Set(150, 100);
mouseEvent.SetScreenPos(pos);
vtkVector2d sP(pos.Cast<double>().GetData());
vtkVector2d lSP(lastPos.Cast<double>().GetData());
vtkVector2d screenPos(mouseEvent.GetScreenPos().Cast<double>().GetData());
vtkVector2d lastScreenPos(mouseEvent.GetLastScreenPos().Cast<double>().GetData());
chart->MouseMoveEvent(mouseEvent);
view->GetInteractor()->Start();
return EXIT_SUCCESS;
}
This diff is collapsed.
......@@ -30,11 +30,14 @@ using std::vector;
class vtkAnnotationLink;
class vtkAxis;
class vtkChartXYZPrivate;
class vtkPlot;
class vtkContext3D;
class vtkContextMouseEvent;
class vtkPen;
class vtkPlane;
class vtkPlot3D;
class vtkTable;
class vtkTransform;
class vtkPen;
class vtkUnsignedCharArray;
class VTKCHARTSCORE_EXPORT vtkChartXYZ : public vtkContextItem
{
......@@ -45,22 +48,19 @@ public:
static vtkChartXYZ * New();
// Description:
// Update any data as necessary before drawing the chart.
void Update();
// Description:
// Paint event for the chart, called whenever the chart needs to be drawn
bool Paint(vtkContext2D *painter);
// Set the geometry in pixel coordinates (origin and width/height).
// This method also sets up the end points of the axes of the chart.
// For this reason, if you call SetAroundX(), you should call SetGeometry()
// afterwards.
void SetGeometry(const vtkRectf &bounds);
// Description:
// Add a plot to the chart, defaults to using the name of the y column
virtual vtkPlot* AddPlot(int type);
// Set the rotation angle for the chart (AutoRotate mode only).
void SetAngle(double angle);
// Description:
// Set the input for the chart, this should be done in the plot, but keeping
// things simple while I get everything working...
virtual void SetInput(vtkTable *input, const vtkStdString &x,
const vtkStdString &y, const vtkStdString &z);
// Set whether or not we're rotating about the X axis.
void SetAroundX(bool isX);
// Description:
// Set the vtkAnnotationLink for the chart.
......@@ -70,21 +70,87 @@ public:
// Get the x (0), y (1) or z (2) axis.
vtkAxis * GetAxis(int axis);
void RecalculateTransform();
// Description:
// Set whether or not we're using this chart to rotate on a timer.
// Default value is false.
void SetAutoRotate(bool b);
// Description:
// Set whether or not axes labels & tick marks should be drawn.
// Default value is true.
void SetDecorateAxes(bool b);
// Description:
// Set whether or not the chart should automatically resize itself to fill
// the scene. Default value is true.
void SetFitToScene(bool b);
// Description:
// Paint event for the chart, called whenever the chart needs to be drawn.
virtual bool Paint(vtkContext2D *painter);
// Description:
// Adds a plot to the chart.
virtual vtkIdType AddPlot(vtkPlot3D* plot);
// Description:
// Remove all the plots from this chart.
void ClearPlots();
// Description:
// Returns true if we should recalculate what points have been clipped.
bool ShouldCheckClipping();
// Description:
// Determine whether an individual data point falls within the bounds of the
// chart axes.
bool PointShouldBeClipped(vtkVector3f point);
// Description:
// Determine the XYZ bounds of the plots within this chart.
// This information is then used to set the range of the axes.
void RecalculateBounds();
// Description:
// Set the geometry in pixel coordinates (origin and width/height).
void SetGeometry(const vtkRectf &bounds);
// Use this chart's Geometry to set the endpoints of its axes.
// This method also sets up a transformation that is used to
// properly render the data within the chart.
void RecalculateTransform();
void SetAngle(double angle);
void SetAroundX(bool isX);
//BTX
// Description:
// Returns true if the transform is interactive, false otherwise.
virtual bool Hit(const vtkContextMouseEvent &mouse);
// Description:
// Mouse press event. Keep track of zoom anchor position.
virtual bool MouseButtonPressEvent(const vtkContextMouseEvent &mouse);
// Description:
// Mouse move event. Perform pan or zoom as specified by the mouse bindings.
virtual bool MouseMoveEvent(const vtkContextMouseEvent &mouse);
// Description:
// Mouse wheel event. Zooms in or out.
virtual bool MouseWheelEvent(const vtkContextMouseEvent &mouse, int delta);
// Description:
// Key press event. This allows the user to snap the chart to one of three
// different 2D views. "x" changes the view so we're looking down the X axis.
// Similar behavior occurs for "y" or "z".
virtual bool KeyPressEvent(const vtkContextKeyEvent &key);
//ETX
protected:
vtkChartXYZ();
~vtkChartXYZ();
// Description:
// Calculate the transformation matrices used to draw data points and axes
// in the scene. This function also sets up clipping planes that determine
// whether or not a data point is within range.
virtual void CalculateTransforms();
// Description:
// Given the x, y and z vtkAxis, and a transform, calculate the transform that
// the points in a chart would need to be drawn within the axes. This assumes
......@@ -93,29 +159,271 @@ protected:
bool CalculatePlotTransform(vtkAxis *x, vtkAxis *y, vtkAxis *z,
vtkTransform *transform);
virtual void CalculateTransforms();
// Description:
// Rotate the chart in response to a mouse movement.
bool Rotate(const vtkContextMouseEvent &mouse);
// Description:
// Pan the data within the chart in response to a mouse movement.
bool Pan(const vtkContextMouseEvent &mouse);
// Description:
// Zoom in or out on the data in response to a mouse movement.
bool Zoom(const vtkContextMouseEvent &mouse);
// Description:
// Spin the chart in response to a mouse movement.
bool Spin(const vtkContextMouseEvent &mouse);
// Description:
// Adjust the rotation of the chart so that we are looking down the X axis.
void LookDownX();
// Description:
// Adjust the rotation of the chart so that we are looking down the Y axis.
void LookDownY();
// Description:
// Adjust the rotation of the chart so that we are looking down the Z axis.
void LookDownZ();
// Description:
// Adjust the rotation of the chart so that we are looking up the X axis.
void LookUpX();
// Description:
// Adjust the rotation of the chart so that we are looking up the Y axis.
void LookUpY();
// Description:
// Adjust the rotation of the chart so that we are looking up the Z axis.
void LookUpZ();
// Description:
// Check to see if the scene changed size since the last render.
bool CheckForSceneResize();
// Description:
// Scale the axes up or down in response to a scene resize.
void RescaleAxes();
// Description:
// Scale up the axes when the scene gets larger.
void ScaleUpAxes();
// Description:
// Scale down the axes when the scene gets smaller.
void ScaleDownAxes();
// Description:
// Change the scaling of the axes by a specified amount.
void ZoomAxes(int delta);
// Description:
// Initialize a list of "test points". These are used to determine whether
// or not the chart fits completely within the bounds of the current scene.
void InitializeAxesBoundaryPoints();
// Description:
// Initialize the "future box" transform. This transform is a duplicate of
// the Box transform, which dictates how the chart's axes should be drawn.
// In ScaleUpAxes() and ScaleDownAxes(), we incrementally change the scaling
// of the FutureBox transform to determine how much we need to zoom in or
// zoom out to fit the chart within the newly resized scene. Using a
// separate transform for this process allows us to resize the Box in a
// single step.
void InitializeFutureBox();
// Description:
// Compute a bounding box for the data that is rendered within the axes.
void ComputeDataBounds();
// Description:
// Draw the cube axes of this chart.
void DrawAxes(vtkContext3D *context);
// Description:
// For each of the XYZ dimensions, find the axis line that is furthest
// from the rendered data.
void DetermineWhichAxesToLabel();
// Description:
// Draw tick marks and tick mark labels along the axes.
void DrawTickMarks(vtkContext2D *painter);
// Description:
// Label the axes.
void DrawAxesLabels(vtkContext2D *painter);
// Description:
// Compute how some text should be offset from an axis. The parameter
// bounds contains the bounding box of the text to be rendered. The
// result is stored in the parameter offset.
void GetOffsetForAxisLabel(int axis, float *bounds, float *offset);
// Description:
// Calculate the next "nicest" numbers above and below the current minimum.
// \return the "nice" spacing of the numbers.
// This function was mostly copied from vtkAxis.
double CalculateNiceMinMax(double &min, double &max, int axis);
// Description:
// The size and position of this chart.
vtkRectf Geometry;
// Description:
// The 3 axes of this chart.
vector< vtkSmartPointer<vtkAxis> > Axes;
// Description:
// This boolean indicates whether or not we're using this chart to rotate
// on a timer.
bool AutoRotate;
// Description:
// When we're in AutoRotate mode, this boolean tells us if we should rotate
// about the X axis or the Y axis.
bool IsX;
// Description:
// When we're in AutoRotate mode, this value tells the chart how much it
// should be rotated.
double Angle;
// Description:
// This boolean indicates whether or not we should draw tick marks
// and axes labels.
bool DrawAxesDecoration;
// Description:
// This boolean is used internally to keep track of when clipping might have
// changed.
bool CheckClipping;
// Description:
// This boolean indicates whether or not we should automatically resize the
// chart so that it snugly fills up the scene.
bool FitToScene;
// Description:
// This is the transform that is applied when rendering data from the plots.
vtkNew<vtkTransform> ContextTransform;
// Description:
// This transform translates and scales the plots' data points so that they
// appear within the axes of this chart. It is one of the factors that
// makes up the ContextTransform.
vtkNew<vtkTransform> PlotTransform;
// Description:
// This is the transform that is applied when rendering data from the plots.
vtkNew<vtkTransform> Box;
// Description:
// This transform keeps track of how the chart has been rotated.
vtkNew<vtkTransform> Rotation;
// Description:
// This transform keeps track of how the data points have been panned within
// the chart.
vtkNew<vtkTransform> Translation;
// Description:
// This transform keeps track of how the data points have been scaled
// (zoomed in or zoomed out) within the chart.
vtkNew<vtkTransform> Scale;
// Description:
// This transform keeps track of how the axes have been scaled
// (zoomed in or zoomed out).
vtkNew<vtkTransform> BoxScale;
// Description:
// This transform is initialized as a copy of Box. It is used within
// ScaleUpAxes() and ScaleDownAxes() to figure out how much we need to
// zoom in or zoom out to fit our chart within the newly resized scene.
vtkNew<vtkTransform> FutureBox;
// Description:
// This transform keeps track of the Scale of the FutureBox transform.
vtkNew<vtkTransform> FutureBoxScale;
// Description:
// This is the pen that is used to draw data from the plots.
vtkNew<vtkPen> Pen;
vtkNew<vtkPen> SelectedPen;
// Description:
// This is the pen that is used to draw the axes.
vtkNew<vtkPen> AxisPen;
// Description:
// This link is used to share selected points with other classes.
vtkSmartPointer<vtkAnnotationLink> Link;
vector<vtkVector3f> Points;
vtkTimeStamp PointsBuildTime;
vector<vtkVector3f> SelectedPoints;
vtkTimeStamp SelectedPointsBuildTime;
// Description:
// The plots that are drawn within this chart.
vector<vtkPlot3D *> Plots;
vector< vtkSmartPointer<vtkAxis> > Axes;
vtkNew<vtkTransform> Transform;
vtkNew<vtkTransform> Translation;
vtkNew<vtkTransform> ContextTransform;
vtkNew<vtkTransform> Rotation;
vtkNew<vtkTransform> Box;
double Angle;
bool IsX;
// Description:
// The label for the X Axis.
std::string XAxisLabel;
// Description:
// The label for the Y Axis.
std::string YAxisLabel;
// Description:
// The label for the Z Axis.
std::string ZAxisLabel;
// Description:
// The six planes that define the bounding cube of our 3D axes.
vtkNew<vtkPlane> Face1;
vtkNew<vtkPlane> Face2;
vtkNew<vtkPlane> Face3;
vtkNew<vtkPlane> Face4;
vtkNew<vtkPlane> Face5;
vtkNew<vtkPlane> Face6;
// Description:
// Points used to determine whether the axes will fit within the scene as
// currently sized, regardless of rotation.
float AxesBoundaryPoints[14][3];
// Description: