Commit dc73d916 authored by Marcus D. Hanwell's avatar Marcus D. Hanwell

Added scaling to the vtkPlotBar class (and a test)

Added awareness of the scaling factor to the vtkPlotBar class, and a
test to exercise this functionality (with mixed plotting of points, line
and bar).

Change-Id: I55e157012e2618d5d59986ef828f80036188f4c7
parent 043de2d1
......@@ -11,6 +11,7 @@ vtk_add_test_cxx(
TestBarGraph.cxx
TestBarGraphHorizontal.cxx
TestColorTransferFunction.cxx,-E80
TestChartDouble.cxx
TestChartMatrix.cxx
TestChartsOn3D.cxx,-E16
TestChartXYZ.cxx
......
/*=========================================================================
Program: Visualization Toolkit
Module: TestChartDouble.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 "vtkDoubleArray.h"
#include "vtkContextView.h"
#include "vtkContextScene.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkNew.h"
#include "vtkAxis.h"
//----------------------------------------------------------------------------
int TestChartDouble(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());
// Create a table with some points in it...
vtkNew<vtkTable> table;
vtkNew<vtkDoubleArray> arrX;
arrX->SetName("X");
table->AddColumn(arrX.GetPointer());
vtkNew<vtkDoubleArray> arrC;
arrC->SetName("f1");
table->AddColumn(arrC.GetPointer());
vtkNew<vtkDoubleArray> arrS;
arrS->SetName("f2");
table->AddColumn(arrS.GetPointer());
vtkNew<vtkDoubleArray> arrS2;
arrS2->SetName("f3");
table->AddColumn(arrS2.GetPointer());
// Test charting with a few more points...
int numPoints = 69;
float inc = 7.5 / (numPoints - 1);
table->SetNumberOfRows(numPoints);
for (int i = 0; i < numPoints; ++i)
{
double x(i * inc + 0.2);
table->SetValue(i, 0, x);
table->SetValue(i, 1, 1.0e-80 * (cos(x - 1.0) + sin(x - 3.14 / 4.0)));
table->SetValue(i, 2, 1.0e-80 * sin(x) * 1e-12);
table->SetValue(i, 3, 1.0e-80 * sin(x - 1.0));
}
// Add multiple line plots, setting the colors etc
vtkPlot *line = chart->AddPlot(vtkChart::POINTS);
line->SetInputData(table.GetPointer(), 0, 1);
line = chart->AddPlot(vtkChart::LINE);
line->SetInputData(table.GetPointer(), 0, 2);
// Put this plot in a different corner - it is orders of magnitude smaller.
chart->SetPlotCorner(line, 1);
line = chart->AddPlot(vtkChart::BAR);
line->SetInputData(table.GetPointer(), 0, 3);
chart->GetAxis(vtkAxis::LEFT)->SetTitle("A tiny range");
chart->GetAxis(vtkAxis::BOTTOM)->SetTitle("A normal range");
chart->GetAxis(vtkAxis::RIGHT)->SetTitle("An even tinier range");
// Render the scene and compare the image to a reference image
view->GetRenderWindow()->SetMultiSamples(0);
view->GetInteractor()->Initialize();
view->GetInteractor()->Start();
return EXIT_SUCCESS;
}
......@@ -32,6 +32,7 @@
#include "vtkSmartPointer.h"
#include "vtkColorSeries.h"
#include "vtkStringArray.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
......@@ -46,7 +47,7 @@ namespace {
// Copy the two arrays into the points array
template<class A, class B>
void CopyToPoints(vtkPoints2D *points, vtkPoints2D *previousPoints, A *a, B *b,
int n, int logScale)
int n, int logScale, const vtkVector2d &scale)
{
points->SetNumberOfPoints(n);
float* data = static_cast<float*>(points->GetVoidPointer(0));
......@@ -62,19 +63,21 @@ void CopyToPoints(vtkPoints2D *points, vtkPoints2D *previousPoints, A *a, B *b,
{
prev = prevData[2 * i + 1];
}
A tmpA(a[i] * scale[0]);
B tmpB(b[i] * scale[1]);
data[2 * i] = static_cast<float>((logScale & 1) ?
log10(static_cast<double>(a[i]))
: a[i]);
log10(static_cast<double>(tmpA))
: tmpA);
data[2 * i + 1] = static_cast<float>((logScale & 2) ?
log10(static_cast<double>(b[i] + prev))
: (b[i] + prev));
log10(static_cast<double>(tmpB + prev))
: (tmpB + prev));
}
}
// Copy one array into the points array, use the index of that array as x
template<class A>
void CopyToPoints(vtkPoints2D *points, vtkPoints2D *previousPoints, A *a, int n,
int logScale)
int logScale, const vtkVector2d &scale)
{
points->SetNumberOfPoints(n);
float* data = static_cast<float*>(points->GetVoidPointer(0));
......@@ -90,25 +93,28 @@ void CopyToPoints(vtkPoints2D *points, vtkPoints2D *previousPoints, A *a, int n,
{
prev = prevData[2 * i + 1];
}
A tmpA(a[i] * scale[1]);
data[2 * i] = static_cast<float>((logScale & 1) ?
log10(static_cast<double>(i + 1.0))
: i);
data[2 * i + 1] = static_cast<float>((logScale & 2) ?
log10(static_cast<double>(a[i] + prev))
: (a[i] + prev));
log10(static_cast<double>(tmpA + prev))
: (tmpA + prev));
}
}
// Copy the two arrays into the points array
template<class A>
void CopyToPointsSwitch(vtkPoints2D *points, vtkPoints2D *previousPoints, A *a,
vtkDataArray *b, int n, int logScale)
vtkDataArray *b, int n, int logScale,
const vtkVector2d &scale)
{
switch(b->GetDataType())
{
vtkTemplateMacro(
CopyToPoints(points,previousPoints, a,
static_cast<VTK_TT*>(b->GetVoidPointer(0)), n, logScale));
static_cast<VTK_TT*>(b->GetVoidPointer(0)), n, logScale,
scale));
}
}
......@@ -174,9 +180,8 @@ class vtkPlotBarSegment : public vtkObject {
delete this->SelectionSet;
}
void Configure(
vtkPlotBar* bar, vtkDataArray* xArray, vtkDataArray* yArray,
vtkAxis* xAxis, vtkAxis* yAxis, vtkPlotBarSegment* prev)
void Configure(vtkPlotBar* bar, vtkDataArray* xArray, vtkDataArray* yArray,
vtkAxis* xAxis, vtkAxis* yAxis, vtkPlotBarSegment* prev)
{
this->Bar = bar;
this->Previous = prev;
......@@ -188,9 +193,8 @@ class vtkPlotBarSegment : public vtkObject {
delete this->Sorted;
delete this->SelectionSet;
int logScale =
(xAxis->GetLogScaleActive() ? 1 : 0) +
(yAxis->GetLogScaleActive() ? 2 : 0);
int logScale = (xAxis->GetLogScaleActive() ? 1 : 0) +
(yAxis->GetLogScaleActive() ? 2 : 0);
if (xArray)
{
switch (xArray->GetDataType())
......@@ -198,7 +202,8 @@ class vtkPlotBarSegment : public vtkObject {
vtkTemplateMacro(
CopyToPointsSwitch(this->Points,this->Previous ? this->Previous->Points : 0,
static_cast<VTK_TT*>(xArray->GetVoidPointer(0)),
yArray, xArray->GetNumberOfTuples(), logScale));
yArray, xArray->GetNumberOfTuples(), logScale,
this->Bar->GetScalingFactor()));
}
}
else
......@@ -208,7 +213,8 @@ class vtkPlotBarSegment : public vtkObject {
vtkTemplateMacro(
CopyToPoints(this->Points, this->Previous ? this->Previous->Points : 0,
static_cast<VTK_TT*>(yArray->GetVoidPointer(0)),
yArray->GetNumberOfTuples(), logScale));
yArray->GetNumberOfTuples(), logScale,
this->Bar->GetScalingFactor()));
}
}
}
......@@ -409,6 +415,7 @@ class vtkPlotBarSegment : public vtkObject {
vtkPlotBar *Bar;
VectorPIMPL* Sorted;
std::set<vtkIdType>* SelectionSet;
vtkVector2d ScalingFactor;
};
vtkStandardNewMacro(vtkPlotBarSegment);
......@@ -427,11 +434,10 @@ public:
vtkDataArray *xArray, vtkDataArray *yArray,
vtkAxis* xAxis, vtkAxis* yAxis, vtkPlotBarSegment *prev = 0)
{
vtkSmartPointer<vtkPlotBarSegment> segment =
vtkSmartPointer<vtkPlotBarSegment>::New();
vtkNew<vtkPlotBarSegment> segment;
segment->Configure(this->Bar, xArray, yArray, xAxis, yAxis, prev);
this->Segments.push_back(segment);
return segment;
this->Segments.push_back(segment.GetPointer());
return segment.GetPointer();
}
void PaintSegments(vtkContext2D *painter, vtkColorSeries *colorSeries,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment