Commit 18e42307 authored by Julien Finet's avatar Julien Finet

ENH: vtkScalarsToColorsItem now derive from vtkPlot

They can be added into any vtkChartXY. Their size depend on the
vtkScalarsToColors (vtkLookupTable or vtkColorTransferFunction) range.
parent 28f524a8
......@@ -13,6 +13,10 @@
=========================================================================*/
#include "vtkChartXY.h"
#include "vtkPlot.h"
#include "vtkTable.h"
#include "vtkChartXY.h"
#include "vtkColorTransferFunction.h"
#include "vtkColorTransferFunctionItem.h"
#include "vtkCompositeTransferFunctionItem.h"
......@@ -29,6 +33,11 @@
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkSmartPointer.h"
#include "vtkIntArray.h"
#define VTK_CREATE(type, name) \
vtkSmartPointer<type> name = vtkSmartPointer<type>::New()
//----------------------------------------------------------------------------
int TestScalarsToColors( int argc, char * argv [] )
......@@ -38,20 +47,13 @@ int TestScalarsToColors( int argc, char * argv [] )
vtkSmartPointer<vtkContextView>::New();
view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
view->GetRenderWindow()->SetSize(400, 300);
//vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();
//view->GetScene()->AddItem(chart);
vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();
chart->SetTitle("Chart");
view->GetScene()->AddItem(chart);
vtkSmartPointer<vtkLookupTable> lookupTable =
vtkSmartPointer<vtkLookupTable>::New();
lookupTable->Build();
//vtkSmartPointer<vtkLookupTableChart> lChart =
// vtkSmartPointer<vtkLookupTableChart>::New();
//lChart->SetLookupTable(lookupTable);
//lChart->SetChart(chart);
vtkSmartPointer<vtkLookupTableItem> item =
vtkSmartPointer<vtkLookupTableItem>::New();
item->SetLookupTable(lookupTable);
//view->GetScene()->AddItem(item);
vtkSmartPointer<vtkColorTransferFunction> colorTransferFunction =
vtkSmartPointer<vtkColorTransferFunction>::New();
......@@ -60,37 +62,28 @@ int TestScalarsToColors( int argc, char * argv [] )
colorTransferFunction->AddHSVSegment(0.6666,0.6666,1.,1.,1.,0.,1.,1.);
colorTransferFunction->Build();
vtkSmartPointer<vtkColorTransferFunctionItem> item2 =
vtkSmartPointer<vtkColorTransferFunctionItem>::New();
item2->SetColorTransferFunction(colorTransferFunction);
//view->GetScene()->AddItem(item2);
vtkSmartPointer<vtkPiecewiseFunction> opacityFunction =
vtkSmartPointer<vtkPiecewiseFunction>::New();
opacityFunction->AddPoint(0.,0.);
opacityFunction->AddPoint(0.5,0.5);
opacityFunction->AddPoint(1.,1.);
vtkSmartPointer<vtkCompositeTransferFunctionItem> item3 =
vtkSmartPointer<vtkCompositeTransferFunctionItem>::New();
vtkCompositeTransferFunctionItem* item3 =
vtkCompositeTransferFunctionItem::New();
item3->SetColorTransferFunction(colorTransferFunction);
item3->SetOpacityFunction(opacityFunction);
item3->SetOpacity(0.2);
item3->SetMaskAboveCurve(true);
//view->GetScene()->AddItem(item3);
vtkSmartPointer<vtkPiecewiseFunctionItem> item4 =
vtkSmartPointer<vtkPiecewiseFunctionItem>::New();
item4->SetPiecewiseFunction(opacityFunction);
item4->SetColor(255,0,0);
item4->SetMaskAboveCurve(true);
view->GetScene()->AddItem(item4);
chart->AddPlot(item3);
vtkSmartPointer<vtkPiecewiseControlPointsItem> item5 =
vtkSmartPointer<vtkPiecewiseControlPointsItem>::New();
vtkPiecewiseControlPointsItem* item5 =
vtkPiecewiseControlPointsItem::New();
item5->SetPiecewiseFunction(opacityFunction);
view->GetScene()->AddItem(item5);
chart->AddPlot(item5);
//Finally render the scene and compare the image to a reference image
//view->GetRenderWindow()->SetMultiSamples(0);
view->GetRenderWindow()->SetMultiSamples(0);
int retVal = vtkRegressionTestImage(view->GetRenderWindow());
if(retVal == vtkRegressionTester::DO_INTERACTOR)
{
......
......@@ -14,6 +14,7 @@
=========================================================================*/
#include "vtkBrush.h"
#include "vtkCallbackCommand.h"
#include "vtkContext2D.h"
#include "vtkImageData.h"
#include "vtkColorTransferFunction.h"
......@@ -21,6 +22,7 @@
#include "vtkObjectFactory.h"
#include "vtkPen.h"
#include "vtkPointData.h"
#include "vtkPoints2D.h"
#include <cassert>
......@@ -63,6 +65,10 @@ void vtkColorTransferFunctionItem::PrintSelf(ostream &os, vtkIndent indent)
void vtkColorTransferFunctionItem::SetColorTransferFunction(vtkColorTransferFunction* t)
{
vtkSetObjectBodyMacro(ColorTransferFunction, vtkColorTransferFunction, t);
if (t)
{
t->AddObserver(vtkCommand::ModifiedEvent, this->Callback);
}
}
//-----------------------------------------------------------------------------
......@@ -73,33 +79,62 @@ void vtkColorTransferFunctionItem::ComputeTexture()
{
this->Texture = vtkImageData::New();
}
double bounds[4];
this->GetBounds(bounds);
if (bounds[0] == bounds[1])
{
vtkWarningMacro(<< "The color transfer function seems empty");
return;
}
// Could depend of the screen resolution
const int dimension = 256;
double values[256];
// should depends on the true size on screen
double* values = new double[dimension];
// Texture 1D
this->Texture->SetExtent(0, dimension-1,
0, 0,
0, 0);
this->Texture->SetNumberOfScalarComponents(4);
this->Texture->SetScalarTypeToUnsignedChar();
this->Texture->AllocateScalars();
double range[2];
this->ColorTransferFunction->GetRange(range);
if (range[0] == range[1])
{
vtkWarningMacro(<< "The color transfer function seems empty");
return;
}
unsigned char* ptr =
reinterpret_cast<unsigned char*>(this->Texture->GetScalarPointer(0,0,0));
for (int i = 0; i < dimension; ++i)
{
values[i] = range[0] + i * (range[1] - range[0]) / dimension;
ptr[3] = this->Opacity * 255;
values[i] = bounds[0] + i * (bounds[1] - bounds[0]) / (dimension - 1);
ptr[3] = static_cast<unsigned char>(this->Opacity * 255 + 0.5);
ptr+=4;
}
this->ColorTransferFunction->MapScalarsThroughTable2(
values,
reinterpret_cast<unsigned char*>(this->Texture->GetScalarPointer(0,0,0)),
VTK_DOUBLE,dimension,1,4);
delete values;
}
//-----------------------------------------------------------------------------
void vtkColorTransferFunctionItem::ScalarsToColorsModified(vtkObject* object,
unsigned long eid,
void* calldata)
{
if (object != this->ColorTransferFunction)
{
vtkErrorMacro("The callback sender object is not the lookup table object ");
return;
}
//Update shape based on the potentially new range.
double* range = this->ColorTransferFunction->GetRange();
double bounds[4];
this->GetBounds(bounds);
if (bounds[0] != range[0] || bounds[1] != range[1])
{
this->Shape->SetNumberOfPoints(4);
this->Shape->SetPoint(0, range[0], bounds[2]);
this->Shape->SetPoint(1, range[0], bounds[3]);
this->Shape->SetPoint(2, range[1], bounds[3]);
this->Shape->SetPoint(3, range[1], bounds[2]);
}
// Internally calls modified to ask for a refresh of the item
this->Superclass::ScalarsToColorsModified(object, eid, calldata);
}
......@@ -35,7 +35,9 @@ protected:
virtual ~vtkColorTransferFunctionItem();
virtual void ComputeTexture();
virtual void ScalarsToColorsModified(vtkObject* object,
unsigned long eid,
void* calldata);
vtkColorTransferFunction* ColorTransferFunction;
};
......
......@@ -13,8 +13,11 @@
=========================================================================*/
#include "vtkCallbackCommand.h"
#include "vtkCommand.h"
#include "vtkImageData.h"
#include "vtkPiecewiseFunction.h"
#include "vtkColorTransferFunction.h"
#include "vtkCompositeTransferFunctionItem.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
......@@ -62,6 +65,10 @@ void vtkCompositeTransferFunctionItem::PrintSelf(ostream &os, vtkIndent indent)
void vtkCompositeTransferFunctionItem::SetOpacityFunction(vtkPiecewiseFunction* opacity)
{
vtkSetObjectBodyMacro(OpacityFunction, vtkPiecewiseFunction, opacity);
if (opacity)
{
opacity->AddObserver(vtkCommand::ModifiedEvent, this->Callback);
}
}
//-----------------------------------------------------------------------------
......@@ -75,9 +82,9 @@ void vtkCompositeTransferFunctionItem::SetMaskAboveCurve(bool mask)
{
this->Shape->SetNumberOfPoints(4);
this->Shape->SetPoint(0, 0.f, 0.f);
this->Shape->SetPoint(1, 100.f, 0.f);
this->Shape->SetPoint(2, 100.f, 100.f);
this->Shape->SetPoint(3, 0.f, 100.f);
this->Shape->SetPoint(1, 1.f, 0.f);
this->Shape->SetPoint(2, 1.f, 1.f);
this->Shape->SetPoint(3, 0.f, 1.f);
}
this->MaskAboveCurve = mask;
this->Modified();
......@@ -87,32 +94,31 @@ void vtkCompositeTransferFunctionItem::SetMaskAboveCurve(bool mask)
void vtkCompositeTransferFunctionItem::ComputeTexture()
{
this->Superclass::ComputeTexture();
// TODO: get the dimension from superclass
const int dimension = 256;
double values[256];
// TBD: it doesn't makes much sense here
double range[2];
this->OpacityFunction->GetRange(range);
if (range[0] == range[1])
double bounds[4];
this->GetBounds(bounds);
if (bounds[0] == bounds[1])
{
vtkWarningMacro(<< "The piecewise function seems empty");
return;
}
this->OpacityFunction->GetTable(range[0], range[1], dimension, values);
const int dimension = this->Texture->GetExtent()[1] + 1;
double* values = new double[dimension];
this->OpacityFunction->GetTable(bounds[0], bounds[1], dimension, values);
unsigned char* ptr =
reinterpret_cast<unsigned char*>(this->Texture->GetScalarPointer(0,0,0));
// TBD: maybe the shape should be defined somewhere else...
if (MaskAboveCurve)
{
this->Shape->SetNumberOfPoints(dimension+2);
this->Shape->SetPoint(0, 0.f, 0.f);
this->Shape->SetPoint(dimension + 1, 100.f, 0.f);
this->Shape->SetPoint(dimension + 1, 1.f, 0.f);
for (int i = 0; i < dimension; ++i)
{
ptr[3] = static_cast<unsigned char>(values[i] * this->Opacity * 255);
assert(values[i] <= 1. && values[i] >= 0.);
this->Shape->SetPoint(i+1,
static_cast<float>(i) * 100.f / dimension,
values[i] * 100.f);
static_cast<float>(i) * 1.f / (dimension - 1),
values[i] * 1.f);
ptr+=4;
}
}
......@@ -125,4 +131,37 @@ void vtkCompositeTransferFunctionItem::ComputeTexture()
ptr+=4;
}
}
delete values;
}
//-----------------------------------------------------------------------------
void vtkCompositeTransferFunctionItem::ScalarsToColorsModified(vtkObject* object,
unsigned long eid,
void* calldata)
{
if (object != this->ColorTransferFunction &&
object != this->OpacityFunction)
{
vtkErrorMacro("The callback sender object is not the color transfer "
"function nor the opacity function");
return;
}
//Update shape based on the potentially new range.
double* range = this->ColorTransferFunction->GetRange();
double* opacityRange = this->ColorTransferFunction->GetRange();
double bounds[4];
this->GetBounds(bounds);
double newRange[2];
newRange[0] = range[0] < opacityRange[0] ? range[0] : opacityRange[0];
newRange[1] = range[1] < opacityRange[1] ? range[1] : opacityRange[1];
if (bounds[0] != newRange[0] || bounds[1] != newRange[1])
{
this->Shape->SetNumberOfPoints(4);
this->Shape->SetPoint(0, newRange[0], 0.);
this->Shape->SetPoint(1, newRange[0], 1.);
this->Shape->SetPoint(2, newRange[1], 1.);
this->Shape->SetPoint(3, newRange[1], 0.);
}
// Internally calls modified to ask for a refresh of the item
this->vtkScalarsToColorsItem::ScalarsToColorsModified(object, eid, calldata);
}
......@@ -37,7 +37,9 @@ protected:
virtual ~vtkCompositeTransferFunctionItem();
virtual void ComputeTexture();
virtual void ScalarsToColorsModified(vtkObject* object,
unsigned long eid,
void* calldata);
vtkPiecewiseFunction* OpacityFunction;
bool MaskAboveCurve;
};
......
......@@ -16,6 +16,7 @@
#include "vtkBrush.h"
#include "vtkCallbackCommand.h"
#include "vtkContext2D.h"
#include "vtkContextScene.h"
#include "vtkControlPointsItem.h"
#include "vtkObjectFactory.h"
#include "vtkPen.h"
......@@ -28,6 +29,11 @@
//-----------------------------------------------------------------------------
vtkControlPointsItem::vtkControlPointsItem()
{
this->Pen->SetLineType(vtkPen::SOLID_LINE);
this->Pen->SetWidth(1.);
this->Pen->SetColorF(1., 1., 1.);
this->Brush->SetColorF(0.85, 0.85, 1., 0.75);
this->Points = vtkPoints2D::New();
this->HighlightPoints = vtkPoints2D::New();
this->Callback = vtkCallbackCommand::New();
......@@ -62,21 +68,26 @@ void vtkControlPointsItem::PrintSelf(ostream &os, vtkIndent indent)
this->Superclass::PrintSelf(os, indent);
}
//-----------------------------------------------------------------------------
void vtkControlPointsItem::GetBounds(double bounds[4])
{
this->Points->GetBounds(bounds);
}
//-----------------------------------------------------------------------------
bool vtkControlPointsItem::Paint(vtkContext2D* painter)
{
if (this->Points->GetNumberOfPoints())
{
painter->GetPen()->SetLineType(vtkPen::SOLID_LINE);
painter->GetPen()->SetColorF(1., 1., 1.);
painter->GetBrush()->SetColorF(0.85, 0.85, 0.85, 0.85);
painter->ApplyPen(this->Pen);
painter->ApplyBrush(this->Brush);
this->DrawPoints(painter, this->Points);
}
if (this->HighlightPoints->GetNumberOfPoints())
{
painter->GetPen()->SetLineType(vtkPen::SOLID_LINE);
painter->GetPen()->SetColorF(0.9, 0.9, 1.);
painter->GetBrush()->SetColorF(0.65, 0.65, 0.95, 0.95);
painter->GetPen()->SetColorF(0.87, 0.87, 1.);
painter->GetBrush()->SetColorF(0.65, 0.65, 0.95, 0.55);
this->DrawPoints(painter, this->HighlightPoints);
}
}
......@@ -94,25 +105,31 @@ void vtkControlPointsItem::CallComputePoints(
//-----------------------------------------------------------------------------
void vtkControlPointsItem::ComputePoints()
{
this->Modified();
}
//-----------------------------------------------------------------------------
void vtkControlPointsItem::DrawPoints(vtkContext2D* painter, vtkPoints2D* points)
{
const int count = points->GetNumberOfPoints();
double point[2];
vtkTransform2D* sceneTransform = painter->GetTransform();
vtkSmartPointer<vtkTransform2D> translation =
vtkSmartPointer<vtkTransform2D>::New();
double point[2];
double transformedPoint[2];
const int count = points->GetNumberOfPoints();
for (int i = 0; i < count; ++i)
{
points->GetPoint(i,point);
points->GetPoint(i, point);
sceneTransform->TransformPoints(point, transformedPoint, 1);
painter->PushMatrix();
translation->Identity();
// TODO, use world coordinates, not local coordinates
translation->Translate(point[0]*100.f, point[1]*100.f);
translation->Translate(transformedPoint[0], transformedPoint[1]);
painter->SetTransform(translation);
painter->DrawWedge(0.f, 0.f, 5.f, 0.f, 0.f, 360.f);
painter->DrawArc(0.f, 0.f, 5.f, 0.f, 360.f);
painter->DrawWedge(0.f, 0.f, 6.f, 0.f, 0.f, 360.f);
painter->DrawArc(0.f, 0.f, 6.f, 0.f, 360.f);
painter->PopMatrix();
}
}
......@@ -16,19 +16,21 @@
#ifndef __vtkControlPointsItem_h
#define __vtkControlPointsItem_h
#include "vtkContextItem.h"
#include "vtkPlot.h"
class vtkCallbackCommand;
class vtkContext2D;
class vtkPoints2D;
class VTK_CHARTS_EXPORT vtkControlPointsItem: public vtkContextItem
class VTK_CHARTS_EXPORT vtkControlPointsItem: public vtkPlot
{
public:
vtkTypeMacro(vtkControlPointsItem, vtkContextItem);
vtkTypeMacro(vtkControlPointsItem, vtkPlot);
virtual void PrintSelf(ostream &os, vtkIndent indent);
virtual void GetBounds(double bounds[4]);
virtual bool Paint(vtkContext2D *painter);
protected:
vtkControlPointsItem();
virtual ~vtkControlPointsItem();
......
......@@ -13,11 +13,13 @@
=========================================================================*/
#include "vtkCallbackCommand.h"
#include "vtkImageData.h"
#include "vtkLookupTable.h"
#include "vtkLookupTableItem.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints2D.h"
#include <cassert>
......@@ -61,6 +63,10 @@ void vtkLookupTableItem::PrintSelf(ostream &os, vtkIndent indent)
void vtkLookupTableItem::SetLookupTable(vtkLookupTable* t)
{
vtkSetObjectBodyMacro(LookupTable, vtkLookupTable, t);
if (t)
{
t->AddObserver(vtkCommand::ModifiedEvent, this->Callback);
}
}
//-----------------------------------------------------------------------------
......@@ -70,13 +76,60 @@ void vtkLookupTableItem::ComputeTexture()
{
this->Texture = vtkImageData::New();
}
this->Texture->SetExtent(0, this->LookupTable->GetNumberOfTableValues() - 1,
// Could depend of the screen resolution
const int dimension = 256;
double values[256];
// Texture 1D
this->Texture->SetExtent(0, dimension - 1,
0,0,
0,0);
this->Texture->SetNumberOfScalarComponents(4);
this->Texture->SetScalarTypeToUnsignedChar();
// this->Texture will share the same scalar array than this->LookupTable
// TODO: Support log scale
this->Texture->GetPointData()->SetScalars(this->LookupTable->GetTable());
this->Texture->AllocateScalars();
// TODO: Support log scale ?
double bounds[4];
this->GetBounds(bounds);
if (bounds[0] == bounds[1])
{
vtkWarningMacro(<< "The lookuptable seems empty");
return;
}
unsigned char* ptr =
reinterpret_cast<unsigned char*>(this->Texture->GetScalarPointer(0,0,0));
for (int i = 0; i < dimension; ++i)
{
values[i] = bounds[0] + i * (bounds[1] - bounds[0]) / (dimension - 1);
ptr[3] = static_cast<unsigned char>(this->Opacity * 255 + 0.5);
ptr+=4;
}
this->LookupTable->MapScalarsThroughTable2(
values,
reinterpret_cast<unsigned char*>(this->Texture->GetScalarPointer(0,0,0)),
VTK_DOUBLE, dimension, 1, 4);
}
//-----------------------------------------------------------------------------
void vtkLookupTableItem::ScalarsToColorsModified(vtkObject* object,
unsigned long eid,
void* calldata)
{
if (object != this->LookupTable)
{
vtkErrorMacro("The callback sender object is not the lookup table object ");
return;
}
//Update shape based on the potentially new range.
double* range = this->LookupTable->GetRange();
double bounds[4];
this->GetBounds(bounds);
if (bounds[0] != range[0] || bounds[1] != range[1])
{
this->Shape->SetNumberOfPoints(4);
this->Shape->SetPoint(0, range[0], bounds[2]);
this->Shape->SetPoint(1, range[0], bounds[3]);
this->Shape->SetPoint(2, range[1], bounds[3]);
this->Shape->SetPoint(3, range[1], bounds[2]);
}
// Internally calls modified to ask for a refresh of the item
this->Superclass::ScalarsToColorsModified(object, eid, calldata);
}
......@@ -35,7 +35,9 @@ protected:
virtual ~vtkLookupTableItem();
virtual void ComputeTexture();
virtual void ScalarsToColorsModified(vtkObject* object,
unsigned long eid,
void* calldata);
vtkLookupTable* LookupTable;
};
......
......@@ -14,6 +14,7 @@
=========================================================================*/
#include "vtkBrush.h"
#include "vtkCallbackCommand.h"
#include "vtkContext2D.h"
#include "vtkImageData.h"
#include "vtkPiecewiseFunction.h"
......@@ -66,6 +67,10 @@ void vtkPiecewiseFunctionItem::PrintSelf(ostream &os, vtkIndent indent)
void vtkPiecewiseFunctionItem::SetPiecewiseFunction(vtkPiecewiseFunction* t)
{
vtkSetObjectBodyMacro(PiecewiseFunction, vtkPiecewiseFunction, t);
if (t)
{
t->AddObserver(vtkCommand::ModifiedEvent, this->Callback);
}
}
//-----------------------------------------------------------------------------
......@@ -79,9 +84,9 @@ void vtkPiecewiseFunctionItem::SetMaskAboveCurve(bool mask)
{
this->Shape->SetNumberOfPoints(4);
this->Shape->SetPoint(0, 0.f, 0.f);
this->Shape->SetPoint(1, 100.f, 0.f);
this->Shape->SetPoint(2, 100.f, 100.f);
this->Shape->SetPoint(3, 0.f, 100.f);
this->Shape->SetPoint(1, 1.f, 0.f);
this->Shape->SetPoint(2, 1.f, 1.f);
this->Shape->SetPoint(3, 0.f, 1.f);
}
this->MaskAboveCurve = mask;
this->Modified();
......@@ -95,8 +100,17 @@ void vtkPiecewiseFunctionItem::ComputeTexture()
{
this->Texture = vtkImageData::New();
}
double bounds[4];
this->GetBounds(bounds);
if (bounds[0] == bounds[1])
{
vtkWarningMacro(<< "The piecewise function seems empty");
return;
}
const int dimension = 256;
double values[256];
double* values = new double[dimension];
// should depends on the true size on screen
this->Texture->SetExtent(0, dimension-1,
0, 0,
......@@ -104,29 +118,24 @@ void vtkPiecewiseFunctionItem::ComputeTexture()
this->Texture->SetNumberOfScalarComponents(4);
this->Texture->SetScalarTypeToUnsignedChar();
this->Texture->AllocateScalars();
double range[2];
this->PiecewiseFunction->GetRange(range);
if (range[0] == range[1])
{
vtkWarningMacro(<< "The piecewise function seems empty");
return;
}
this->PiecewiseFunction->GetTable(range[0], range[1], dimension, values);
this->PiecewiseFunction->GetTable(bounds[0], bounds[1], dimension, values);
unsigned char* ptr =
reinterpret_cast<unsigned char*>(this->Texture->GetScalarPointer(0,0,0));
if (MaskAboveCurve)
{
this->Shape->SetNumberOfPoints(dimension + 2 );
this->Shape->SetPoint(0, 0.f, 0.f);
this->Shape->SetPoint(dimension + 1, 100.f, 0.f);
this->Shape->SetPoint(dimension + 1, 1.f, 0.f);
for (int i = 0; i < dimension; ++i)
{
ptr[0] = this->Color[0];
ptr[1] = this->Color[1];
ptr[2] = this->Color[2];
ptr[3] = static_cast<unsigned char>(values[i] * this->Opacity * 255);
ptr[3] = static_cast<unsigned char>(values[i] * this->Opacity * 255 + 0.5);
assert(values[i] <= 1. && values[i] >= 0.);
this->Shape->SetPoint(i +1, static_cast<float>(i) * 100.f / dimension, values[i] * 100.f);
this->Shape->SetPoint(i + 1, static_cast<float>(i) * 1.f / (dimension-1),
values[i] * 1.f);
ptr+=4;
}
}
......@@ -137,9 +146,35 @@ void vtkPiecewiseFunctionItem::ComputeTexture()
ptr[0] = this->Color[0];
ptr[1] = this->Color[1];
ptr[2] = this->Color[2];
ptr[3] = static_cast<unsigned char>(values[i] * this->Opacity * 255);
ptr[3] = static_cast<unsigned char>(values[i] * this->Opacity * 255 + 0.5);
assert(values[i] <= 1. && values[i] >= 0.);
ptr+=4;
}
}
}
//-----------------------------------------------------------------------------
void vtkPiecewiseFunctionItem::ScalarsToColorsModified(vtkObject* object,
unsigned long eid,
void* calldata)
{
if (object != this->PiecewiseFunction)
{
vtkErrorMacro("The callback sender object is not the lookup table object ");
return;
}
//Update shape based on the potentially new range.
double* range = this->PiecewiseFunction->GetRange();
double bounds[4];
this->GetBounds(bounds);
if (bounds[0] != range[0] || bounds[1] != range[1])
{
this->Shape->SetNumberOfPoints(4);
this->Shape->SetPoint(0, range[0], 0.);
this->Shape->SetPoint(1, range[0], 1.);
this->Shape->SetPoint(2, range[1], 1.);
this->Shape->SetPoint(3, range[1], 0.);
}
// Internally calls modified to ask for a refresh of the item
this->Superclass::ScalarsToColorsModified(object, eid, calldata);
}
......@@ -43,6 +43,9 @@ protected:
// Description
// Compute the texture from the PiecewiseFunction
virtual void ComputeTexture();
virtual void ScalarsToColorsModified(vtkObject* object,
unsigned long eid,
void* calldata);
vtkPiecewiseFunction* PiecewiseFunction;
unsigned char Color[3];
......
......@@ -14,27 +14,39 @@
=========================================================================*/
#include "vtkBrush.h"
#include "vtkCallbackCommand.h"
#include "vtkContext2D.h"
#include "vtkContextScene.h"
#include "vtkImageData.h"
#include "vtkScalarsToColorsItem.h"
#include "vtkObjectFactory.h"
#include "vtkPen.h"
#include "vtkPoints2D.h"
#include "vtkSmartPointer.h"
#include "vtkTransform2D.h"
#include <cassert>
//-----------------------------------------------------------------------------
vtkScalarsToColorsItem::vtkScalarsToColorsItem()
{
this->Pen->SetWidth(0.0);
this->Pen->SetLineType(vtkPen::NO_PEN);
this->Texture = 0;