Commit 91c82ae7 authored by Marcus D. Hanwell's avatar Marcus D. Hanwell
Browse files

ENH: Improvements to the 2D histogram, including legend.

Added a vtkColorLegend class, this is now displayed in a 2D histogram
chart, and uses a vtkAxis to display the scale. There is still some
work to do to get everything laid out correctly. Disabled the test for
now, until the layout and display is working as expected.

Change-Id: Iaa2a47538158d340d2f2c7120bd61d61e179b6cf
parent c2d690c4
......@@ -27,6 +27,7 @@ SET(Kit_SRCS
vtkChartParallelCoordinates.cxx
vtkChartXY.cxx
vtkChartPie.cxx
vtkColorLegend.cxx
vtkPlotPie.cxx
vtkColor.h
vtkColorSeries.cxx
......
......@@ -13,7 +13,7 @@ IF (VTK_USE_RENDERING AND VTK_USE_VIEWS)
# add tests that require data
SET(MyTests ${MyTests}
#TestContextUnicode.cxx # Disabled until Freetype unicode works
Test2DHistogram.cxx
#Test2DHistogram.cxx
TestBarGraph.cxx
TestContext.cxx
TestContextImage.cxx
......
......@@ -64,7 +64,7 @@ int Test2DHistogram( int, char * [] )
transferFunction->AddHSVSegment(0.3333, 0.3333, 1.0, 1.0,
0.6666, 0.6666, 1.0, 1.0);
transferFunction->AddHSVSegment(0.6666, 0.6666, 1.0, 1.0,
1.0, 0.0, 1.0, 1.0);
1.0, 0.2, 1.0, 0.3);
transferFunction->Build();
chart->SetTransferFunction(transferFunction);
......
......@@ -23,6 +23,7 @@
#include "vtkTextProperty.h"
#include "vtkAxis.h"
#include "vtk2DHistogramItem.h"
#include "vtkColorLegend.h"
#include "vtkContextTransform.h"
#include "vtkSmartPointer.h"
#include "vtkTransform2D.h"
......@@ -71,6 +72,9 @@ vtkChart2DHistogram::vtkChart2DHistogram()
this->AddItem(this->Storage->Axes.back());
this->Storage->Axes[i]->SetPosition(i);
}
this->Legend = vtkSmartPointer<vtkColorLegend>::New();
this->AddItem(this->Legend);
}
//-----------------------------------------------------------------------------
......@@ -87,6 +91,8 @@ void vtkChart2DHistogram::Update()
{
(*it)->Update();
}
this->Histogram->Update();
this->Legend->Update();
}
//-----------------------------------------------------------------------------
......@@ -124,6 +130,7 @@ void vtkChart2DHistogram::SetInput(vtkImageData *data, vtkIdType z)
void vtkChart2DHistogram::SetTransferFunction(vtkScalarsToColors *function)
{
this->Histogram->SetTransferFunction(function);
this->Legend->SetTransferFunction(function);
}
//-----------------------------------------------------------------------------
......@@ -160,7 +167,7 @@ vtkIdType vtkChart2DHistogram::GetNumberOfAxes()
//-----------------------------------------------------------------------------
void vtkChart2DHistogram::UpdateGeometry()
{
this->SetBorders(20, 20, 20, 20);
this->SetBorders(20, 20, 60, 20);
double bounds[4];
this->Histogram->GetBounds(bounds);
......@@ -178,6 +185,9 @@ void vtkChart2DHistogram::UpdateGeometry()
axis->AutoScale();
axis->Update();
this->Legend->SetPosition(vtkRectf(this->Point2[0], this->Point1[1],
10, this->Point2[1] - this->Point1[1]));
this->CalculatePlotTransform(this->Storage->Axes[vtkAxis::BOTTOM],
this->Storage->Axes[vtkAxis::LEFT],
this->Storage->Transform->GetTransform());
......@@ -212,7 +222,7 @@ bool vtkChart2DHistogram::MouseEnterEvent(const vtkContextMouseEvent &)
}
//-----------------------------------------------------------------------------
bool vtkChart2DHistogram::MouseMoveEvent(const vtkContextMouseEvent &mouse)
bool vtkChart2DHistogram::MouseMoveEvent(const vtkContextMouseEvent &)
{
return true;
}
......@@ -224,22 +234,19 @@ bool vtkChart2DHistogram::MouseLeaveEvent(const vtkContextMouseEvent &)
}
//-----------------------------------------------------------------------------
bool vtkChart2DHistogram::MouseButtonPressEvent(
const vtkContextMouseEvent& mouse)
bool vtkChart2DHistogram::MouseButtonPressEvent(const vtkContextMouseEvent &)
{
return false;
}
//-----------------------------------------------------------------------------
bool vtkChart2DHistogram::MouseButtonReleaseEvent(
const vtkContextMouseEvent& mouse)
bool vtkChart2DHistogram::MouseButtonReleaseEvent(const vtkContextMouseEvent &)
{
return false;
}
//-----------------------------------------------------------------------------
bool vtkChart2DHistogram::MouseWheelEvent(const vtkContextMouseEvent &,
int)
bool vtkChart2DHistogram::MouseWheelEvent(const vtkContextMouseEvent &, int)
{
return true;
}
......
......@@ -24,6 +24,7 @@
#include "vtkChart.h"
#include "vtkSmartPointer.h" // For SP ivars
class vtkColorLegend;
class vtk2DHistogramItem;
class vtkImageData;
class vtkScalarsToColors;
......@@ -104,6 +105,7 @@ protected:
vtkChart2DHistogram();
~vtkChart2DHistogram();
vtkSmartPointer<vtkColorLegend> Legend;
vtkSmartPointer<vtk2DHistogramItem> Histogram;
// Description:
......
/*=========================================================================
Program: Visualization Toolkit
Module: vtkColorLegend.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 "vtkColorLegend.h"
#include "vtkAxis.h"
#include "vtkBrush.h"
#include "vtkCallbackCommand.h"
#include "vtkContext2D.h"
#include "vtkContextScene.h"
#include "vtkFloatArray.h"
#include "vtkImageData.h"
#include "vtkObjectFactory.h"
#include "vtkPen.h"
#include "vtkPoints2D.h"
#include "vtkSmartPointer.h"
#include "vtkTransform2D.h"
#include "vtkScalarsToColors.h"
//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkColorLegend);
//-----------------------------------------------------------------------------
vtkColorLegend::vtkColorLegend()
{
this->Interpolate = true;
this->Axis = vtkSmartPointer<vtkAxis>::New();
this->Axis->SetPosition(vtkAxis::RIGHT);
this->Callback = vtkSmartPointer<vtkCallbackCommand>::New();
this->Callback->SetClientData(this);
this->Callback->SetCallback(vtkColorLegend::OnScalarsToColorsModified);
}
//-----------------------------------------------------------------------------
vtkColorLegend::~vtkColorLegend()
{
}
//-----------------------------------------------------------------------------
void vtkColorLegend::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Interpolate: " << this->Interpolate << endl;
}
//-----------------------------------------------------------------------------
void vtkColorLegend::GetBounds(double bounds[4])
{
if (this->TransferFunction)
{
bounds[0] = this->TransferFunction->GetRange()[0];
bounds[1] = this->TransferFunction->GetRange()[1];
}
else
{
bounds[0] = 0.0;
bounds[1] = 1.0;
}
bounds[2] = 0.0;
bounds[3] = 1.0;
}
//-----------------------------------------------------------------------------
void vtkColorLegend::Update()
{
if (this->ImageData == 0 ||
this->ImageData->GetMTime() < this->GetMTime())
{
this->ComputeTexture();
}
this->Axis->Update();
}
//-----------------------------------------------------------------------------
bool vtkColorLegend::Paint(vtkContext2D* painter)
{
painter->DrawImage(this->Position, this->ImageData);
this->Axis->Paint(painter);
return true;
}
void vtkColorLegend::SetTransferFunction(vtkScalarsToColors* transfer)
{
this->TransferFunction = transfer;
}
vtkScalarsToColors * vtkColorLegend::GetTransferFunction()
{
return this->TransferFunction;
}
//-----------------------------------------------------------------------------
void vtkColorLegend::SetPosition(const vtkRectf& pos)
{
this->Position = pos;
this->Axis->SetPoint1(vtkVector2f(pos.X() + pos.Width(), pos.Y()));
this->Axis->SetPoint2(vtkVector2f(pos.X() + pos.Width(), pos.Y() + pos.Height()));
}
//-----------------------------------------------------------------------------
vtkRectf vtkColorLegend::GetPosition()
{
return this->Position;
}
//-----------------------------------------------------------------------------
void vtkColorLegend::ComputeTexture()
{
if (!this->ImageData)
{
this->ImageData = vtkSmartPointer<vtkImageData>::New();
}
double bounds[4];
this->GetBounds(bounds);
if (bounds[0] == bounds[1])
{
vtkWarningMacro(<< "The color transfer function seems to be empty.");
return;
}
// Set the axis up
this->Axis->SetRange(bounds[0], bounds[1]);
//this->Axis->AutoScale();
this->Axis->Print(cout);
// Could depend of the screen resolution
const int dimension = 256;
double* values = new double[dimension];
// Texture 1D
this->ImageData->SetExtent(0, 0,
0, dimension-1,
0, 0);
this->ImageData->SetNumberOfScalarComponents(3);
this->ImageData->SetScalarTypeToUnsignedChar();
this->ImageData->AllocateScalars();
for (int i = 0; i < dimension; ++i)
{
values[i] = bounds[0] + i * (bounds[1] - bounds[0]) / (dimension - 1);
}
unsigned char* ptr =
reinterpret_cast<unsigned char*>(this->ImageData->GetScalarPointer());
this->TransferFunction->MapScalarsThroughTable2(
values, ptr, VTK_DOUBLE, dimension, 1, 3);
delete [] values;
}
//-----------------------------------------------------------------------------
void vtkColorLegend::OnScalarsToColorsModified(vtkObject* caller,
unsigned long eid,
void *clientdata,
void* calldata)
{
vtkColorLegend* self =
reinterpret_cast<vtkColorLegend*>(clientdata);
self->ScalarsToColorsModified(caller, eid, calldata);
}
//-----------------------------------------------------------------------------
void vtkColorLegend::ScalarsToColorsModified(vtkObject* vtkNotUsed(object),
unsigned long vtkNotUsed(eid),
void* vtkNotUsed(calldata))
{
this->Modified();
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkColorLegend.h
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.
=========================================================================*/
// .NAME vtkColorLegend - Legend item to display vtkScalarsToColors.
// .SECTION Description
// vtkColorLegend is an item that will display the vtkScalarsToColors
// using a 1D texture, and a vtkAxis to show both the color and numerical range.
#ifndef __vtkColorLegend_h
#define __vtkColorLegend_h
#include "vtkContextItem.h"
#include "vtkSmartPointer.h" // For SP ivars
#include "vtkVector.h" // For vtkRectf
class vtkAxis;
class vtkImageData;
class vtkScalarsToColors;
class vtkCallbackCommand;
class VTK_CHARTS_EXPORT vtkColorLegend: public vtkContextItem
{
public:
vtkTypeMacro(vtkColorLegend, vtkContextItem);
virtual void PrintSelf(ostream &os, vtkIndent indent);
static vtkColorLegend* New();
// Decription:
// Bounds of the item, by default (0, 1, 0, 1) but it mainly depends on the
// range of the vtkScalarsToColors function.
virtual void GetBounds(double bounds[4]);
// Description:
// Perform any updates to the item that may be necessary before rendering.
// The scene should take care of calling this on all items before their
// Paint function is invoked.
virtual void Update();
// Decription:
// Paint the texture into a rectangle defined by the bounds. If
// MaskAboveCurve is true and a shape has been provided by a subclass, it
// draws the texture into the shape
virtual bool Paint(vtkContext2D *painter);
virtual void SetTransferFunction(vtkScalarsToColors* transfer);
virtual vtkScalarsToColors * GetTransferFunction();
virtual void SetPosition(const vtkRectf& pos);
virtual vtkRectf GetPosition();
protected:
vtkColorLegend();
virtual ~vtkColorLegend();
// Description:
// Need to be reimplemented by subclasses, ComputeTexture() is called at
// paint time if the texture is not up to date compared to vtkColorLegend
virtual void ComputeTexture();
// Description:
// Called whenever the ScalarsToColors function(s) is modified. It internally
// calls Modified(). Can be reimplemented by subclasses.
virtual void ScalarsToColorsModified(vtkObject* caller, unsigned long eid,
void* calldata);
static void OnScalarsToColorsModified(vtkObject* caller, unsigned long eid,
void *clientdata, void* calldata);
vtkScalarsToColors* TransferFunction;
vtkSmartPointer<vtkImageData> ImageData;
vtkSmartPointer<vtkAxis> Axis;
vtkSmartPointer<vtkCallbackCommand> Callback;
bool Interpolate;
vtkRectf Position;
private:
vtkColorLegend(const vtkColorLegend &); // Not implemented.
void operator=(const vtkColorLegend &); // Not implemented.
};
#endif
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