Commit a7248eb8 authored by Julien Finet's avatar Julien Finet Committed by Marcus D. Hanwell

ENH: Add the option of drawing a polyline (useful for vtkPiecewiseFunction

and vtkCompositeTransferFunctionItem), specially if the option
MaskAboveCurve is off.
parent 1866c953
......@@ -61,6 +61,18 @@ void vtkColorTransferFunctionItem::PrintSelf(ostream &os, vtkIndent indent)
}
}
//-----------------------------------------------------------------------------
void vtkColorTransferFunctionItem::GetBounds(double* bounds)
{
this->Superclass::GetBounds(bounds);
if (this->ColorTransferFunction)
{
double* range = this->ColorTransferFunction->GetRange();
bounds[0] = range[0];
bounds[1] = range[1];
}
}
//-----------------------------------------------------------------------------
void vtkColorTransferFunctionItem::SetColorTransferFunction(vtkColorTransferFunction* t)
{
......@@ -116,30 +128,3 @@ void vtkColorTransferFunctionItem::ComputeTexture()
}
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]);
this->Shape->Modified();
}
// Internally calls modified to ask for a refresh of the item
this->Superclass::ScalarsToColorsModified(object, eid, calldata);
}
......@@ -21,6 +21,9 @@
class vtkColorTransferFunction;
class vtkImageData;
// Description:
// vtkPlot::Color, vtkPlot::Brush, vtkScalarsToColors::DrawPolyLine,
// vtkScalarsToColors::MaskAboveCurve have no effect here.
class VTK_CHARTS_EXPORT vtkColorTransferFunctionItem: public vtkScalarsToColorsItem
{
public:
......@@ -28,16 +31,18 @@ public:
vtkTypeMacro(vtkColorTransferFunctionItem, vtkScalarsToColorsItem);
virtual void PrintSelf(ostream &os, vtkIndent indent);
// Description:
// Reimplemented to return the range of the lookup table
virtual void GetBounds(double bounds[4]);
void SetColorTransferFunction(vtkColorTransferFunction* t);
vtkGetObjectMacro(ColorTransferFunction, vtkColorTransferFunction);
protected:
vtkColorTransferFunctionItem();
virtual ~vtkColorTransferFunctionItem();
virtual void ComputeTexture();
virtual void ScalarsToColorsModified(vtkObject* object,
unsigned long eid,
void* calldata);
vtkColorTransferFunction* ColorTransferFunction;
private:
vtkColorTransferFunctionItem(const vtkColorTransferFunctionItem&); // Not implemented
......
......@@ -20,6 +20,7 @@
#include "vtkColorTransferFunction.h"
#include "vtkCompositeTransferFunctionItem.h"
#include "vtkObjectFactory.h"
#include "vtkPen.h"
#include "vtkPointData.h"
#include "vtkPoints2D.h"
......@@ -31,8 +32,8 @@ vtkStandardNewMacro(vtkCompositeTransferFunctionItem);
//-----------------------------------------------------------------------------
vtkCompositeTransferFunctionItem::vtkCompositeTransferFunctionItem()
{
this->PolyLinePen->SetLineType(vtkPen::SOLID_LINE);
this->OpacityFunction = 0;
this->MaskAboveCurve = false;
}
//-----------------------------------------------------------------------------
......@@ -62,33 +63,26 @@ void vtkCompositeTransferFunctionItem::PrintSelf(ostream &os, vtkIndent indent)
}
//-----------------------------------------------------------------------------
void vtkCompositeTransferFunctionItem::SetOpacityFunction(vtkPiecewiseFunction* opacity)
void vtkCompositeTransferFunctionItem::GetBounds(double* bounds)
{
vtkSetObjectBodyMacro(OpacityFunction, vtkPiecewiseFunction, opacity);
if (opacity)
this->Superclass::GetBounds(bounds);
if (this->OpacityFunction)
{
opacity->AddObserver(vtkCommand::ModifiedEvent, this->Callback);
double* opacityRange = this->ColorTransferFunction->GetRange();
bounds[0] = bounds[0] < opacityRange[0] ? bounds[0] : opacityRange[0];;
bounds[1] = bounds[1] < opacityRange[1] ? bounds[1] : opacityRange[1];
}
this->ScalarsToColorsModified(this->OpacityFunction, vtkCommand::ModifiedEvent, 0);
}
//-----------------------------------------------------------------------------
void vtkCompositeTransferFunctionItem::SetMaskAboveCurve(bool mask)
void vtkCompositeTransferFunctionItem::SetOpacityFunction(vtkPiecewiseFunction* opacity)
{
if (mask == this->MaskAboveCurve)
{
return;
}
if (mask == false)
vtkSetObjectBodyMacro(OpacityFunction, vtkPiecewiseFunction, opacity);
if (opacity)
{
this->Shape->SetNumberOfPoints(4);
this->Shape->SetPoint(0, 0.f, 0.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);
opacity->AddObserver(vtkCommand::ModifiedEvent, this->Callback);
}
this->MaskAboveCurve = mask;
this->Modified();
this->ScalarsToColorsModified(this->OpacityFunction, vtkCommand::ModifiedEvent, 0);
}
//-----------------------------------------------------------------------------
......@@ -108,7 +102,7 @@ void vtkCompositeTransferFunctionItem::ComputeTexture()
unsigned char* ptr =
reinterpret_cast<unsigned char*>(this->Texture->GetScalarPointer(0,0,0));
// TBD: maybe the shape should be defined somewhere else...
if (MaskAboveCurve)
if (this->MaskAboveCurve || this->PolyLinePen->GetLineType() != vtkPen::SOLID_LINE)
{
this->Shape->SetNumberOfPoints(dimension);
for (int i = 0; i < dimension; ++i)
......@@ -132,36 +126,3 @@ void vtkCompositeTransferFunctionItem::ComputeTexture()
}
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.);
this->Shape->Modified();
}
// Internally calls modified to ask for a refresh of the item
this->vtkScalarsToColorsItem::ScalarsToColorsModified(object, eid, calldata);
}
......@@ -20,6 +20,8 @@
class vtkPiecewiseFunction;
// Description:
// vtkPlot::Color and vtkPlot::Brush have no effect here.
class VTK_CHARTS_EXPORT vtkCompositeTransferFunctionItem: public vtkColorTransferFunctionItem
{
public:
......@@ -27,21 +29,19 @@ public:
vtkTypeMacro(vtkCompositeTransferFunctionItem, vtkColorTransferFunctionItem);
virtual void PrintSelf(ostream &os, vtkIndent indent);
// Description:
// Reimplemented to return the range of the piecewise function
virtual void GetBounds(double bounds[4]);
void SetOpacityFunction(vtkPiecewiseFunction* opacity);
vtkGetObjectMacro(OpacityFunction, vtkPiecewiseFunction);
void SetMaskAboveCurve(bool mask);
vtkGetMacro(MaskAboveCurve, bool);
protected:
vtkCompositeTransferFunctionItem();
virtual ~vtkCompositeTransferFunctionItem();
virtual void ComputeTexture();
virtual void ScalarsToColorsModified(vtkObject* object,
unsigned long eid,
void* calldata);
vtkPiecewiseFunction* OpacityFunction;
bool MaskAboveCurve;
private:
vtkCompositeTransferFunctionItem(const vtkCompositeTransferFunctionItem&); // Not implemented.
......
......@@ -59,6 +59,18 @@ void vtkLookupTableItem::PrintSelf(ostream &os, vtkIndent indent)
}
}
//-----------------------------------------------------------------------------
void vtkLookupTableItem::GetBounds(double* bounds)
{
this->Superclass::GetBounds(bounds);
if (this->LookupTable)
{
double* range = this->LookupTable->GetRange();
bounds[0] = range[0];
bounds[1] = range[1];
}
}
//-----------------------------------------------------------------------------
void vtkLookupTableItem::SetLookupTable(vtkLookupTable* t)
{
......@@ -112,30 +124,3 @@ void vtkLookupTableItem::ComputeTexture()
}
}
}
//-----------------------------------------------------------------------------
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]);
this->Shape->Modified();
}
// Internally calls modified to ask for a refresh of the item
this->Superclass::ScalarsToColorsModified(object, eid, calldata);
}
......@@ -20,6 +20,9 @@
class vtkLookupTable;
// Description:
// vtkPlot::Color, vtkPlot::Brush, vtkScalarsToColors::DrawPolyLine,
// vtkScalarsToColors::MaskAboveCurve have no effect here.
class VTK_CHARTS_EXPORT vtkLookupTableItem: public vtkScalarsToColorsItem
{
public:
......@@ -27,6 +30,10 @@ public:
vtkTypeMacro(vtkLookupTableItem, vtkScalarsToColorsItem);
virtual void PrintSelf(ostream &os, vtkIndent indent);
// Description:
// Reimplemented to return the range of the lookup table
virtual void GetBounds(double bounds[4]);
void SetLookupTable(vtkLookupTable* t);
vtkGetObjectMacro(LookupTable, vtkLookupTable);
......@@ -35,9 +42,6 @@ protected:
virtual ~vtkLookupTableItem();
virtual void ComputeTexture();
virtual void ScalarsToColorsModified(vtkObject* object,
unsigned long eid,
void* calldata);
vtkLookupTable* LookupTable;
private:
......
......@@ -32,9 +32,9 @@ vtkStandardNewMacro(vtkPiecewiseFunctionItem);
//-----------------------------------------------------------------------------
vtkPiecewiseFunctionItem::vtkPiecewiseFunctionItem()
{
this->PolyLinePen->SetLineType(vtkPen::SOLID_LINE);
this->PiecewiseFunction = 0;
this->SetColor(1., 1., 1.);
this->MaskAboveCurve = false;
}
//-----------------------------------------------------------------------------
......@@ -64,34 +64,25 @@ void vtkPiecewiseFunctionItem::PrintSelf(ostream &os, vtkIndent indent)
}
//-----------------------------------------------------------------------------
void vtkPiecewiseFunctionItem::SetPiecewiseFunction(vtkPiecewiseFunction* t)
void vtkPiecewiseFunctionItem::GetBounds(double* bounds)
{
vtkSetObjectBodyMacro(PiecewiseFunction, vtkPiecewiseFunction, t);
if (t)
this->Superclass::GetBounds(bounds);
if (this->PiecewiseFunction)
{
t->AddObserver(vtkCommand::ModifiedEvent, this->Callback);
double* range = this->PiecewiseFunction->GetRange();
bounds[0] = range[0];
bounds[1] = range[1];
}
this->ScalarsToColorsModified(this->PiecewiseFunction, vtkCommand::ModifiedEvent, 0);
}
//-----------------------------------------------------------------------------
void vtkPiecewiseFunctionItem::SetMaskAboveCurve(bool mask)
void vtkPiecewiseFunctionItem::SetPiecewiseFunction(vtkPiecewiseFunction* t)
{
if (mask == this->MaskAboveCurve)
{
return;
}
if (mask == false)
vtkSetObjectBodyMacro(PiecewiseFunction, vtkPiecewiseFunction, t);
if (t)
{
this->Shape->SetNumberOfPoints(4);
this->Shape->SetPoint(0, 0.f, 0.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->Shape->Modified();
t->AddObserver(vtkCommand::ModifiedEvent, this->Callback);
}
this->MaskAboveCurve = mask;
this->Modified();
this->ScalarsToColorsModified(this->PiecewiseFunction, vtkCommand::ModifiedEvent, 0);
}
//-----------------------------------------------------------------------------
......@@ -124,7 +115,7 @@ void vtkPiecewiseFunctionItem::ComputeTexture()
this->PiecewiseFunction->GetTable(bounds[0], bounds[1], dimension, values);
unsigned char* ptr =
reinterpret_cast<unsigned char*>(this->Texture->GetScalarPointer(0,0,0));
if (MaskAboveCurve)
if (this->MaskAboveCurve || this->PolyLinePen->GetLineType() != vtkPen::NO_PEN)
{
this->Shape->SetNumberOfPoints(dimension);
for (int i = 0; i < dimension; ++i)
......@@ -149,30 +140,3 @@ void vtkPiecewiseFunctionItem::ComputeTexture()
}
}
}
//-----------------------------------------------------------------------------
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.);
this->Shape->Modified();
}
// Internally calls modified to ask for a refresh of the item
this->Superclass::ScalarsToColorsModified(object, eid, calldata);
}
......@@ -29,11 +29,13 @@ public:
vtkTypeMacro(vtkPiecewiseFunctionItem, vtkScalarsToColorsItem);
virtual void PrintSelf(ostream &os, vtkIndent indent);
// Description:
// Reimplemented to return the range of the piecewise function
virtual void GetBounds(double bounds[4]);
void SetPiecewiseFunction(vtkPiecewiseFunction* t);
vtkGetObjectMacro(PiecewiseFunction, vtkPiecewiseFunction);
void SetMaskAboveCurve(bool mask);
vtkGetMacro(MaskAboveCurve, bool);
protected:
vtkPiecewiseFunctionItem();
virtual ~vtkPiecewiseFunctionItem();
......@@ -41,12 +43,8 @@ protected:
// Description
// Compute the texture from the PiecewiseFunction
virtual void ComputeTexture();
virtual void ScalarsToColorsModified(vtkObject* object,
unsigned long eid,
void* calldata);
vtkPiecewiseFunction* PiecewiseFunction;
bool MaskAboveCurve;
private:
vtkPiecewiseFunctionItem(const vtkPiecewiseFunctionItem &); // Not implemented.
......
......@@ -17,11 +17,12 @@
#include "vtkCallbackCommand.h"
#include "vtkContext2D.h"
#include "vtkContextScene.h"
#include "vtkFloatArray.h"
#include "vtkImageData.h"
#include "vtkScalarsToColorsItem.h"
#include "vtkObjectFactory.h"
#include "vtkPen.h"
#include "vtkPoints2D.h"
#include "vtkScalarsToColorsItem.h"
#include "vtkSmartPointer.h"
#include "vtkTransform2D.h"
......@@ -30,28 +31,33 @@
//-----------------------------------------------------------------------------
vtkScalarsToColorsItem::vtkScalarsToColorsItem()
{
this->Pen->SetWidth(0.0);
this->Pen->SetLineType(vtkPen::NO_PEN);
this->PolyLinePen = vtkPen::New();
this->PolyLinePen->SetWidth(2.);
this->PolyLinePen->SetColor(64, 64, 72); // Payne's grey, why not
this->PolyLinePen->SetLineType(vtkPen::NO_PEN);
this->Texture = 0;
this->Interpolate = true;
this->Shape = vtkPoints2D::New();
this->Shape->SetDataTypeToFloat();
this->Shape->SetNumberOfPoints(4);
this->Shape->SetPoint(0, 0.f, 0.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->Shape->SetNumberOfPoints(0);
this->Callback = vtkCallbackCommand::New();
this->Callback->SetClientData(this);
this->Callback->SetCallback(
vtkScalarsToColorsItem::OnScalarsToColorsModified);
this->MaskAboveCurve = false;
}
//-----------------------------------------------------------------------------
vtkScalarsToColorsItem::~vtkScalarsToColorsItem()
{
if (this->PolyLinePen)
{
this->PolyLinePen->Delete();
this->PolyLinePen = 0;
}
if (this->Texture)
{
this->Texture->Delete();
......@@ -79,7 +85,10 @@ void vtkScalarsToColorsItem::PrintSelf(ostream &os, vtkIndent indent)
//-----------------------------------------------------------------------------
void vtkScalarsToColorsItem::GetBounds(double bounds[4])
{
this->Shape->GetBounds(bounds);
bounds[0] = 0.;
bounds[1] = 1.;
bounds[2] = 0.;
bounds[3] = 1.;
}
//-----------------------------------------------------------------------------
......@@ -90,7 +99,9 @@ bool vtkScalarsToColorsItem::Paint(vtkContext2D* painter)
{
this->ComputeTexture();
}
painter->ApplyPen(this->Pen);
vtkSmartPointer<vtkPen> transparentPen = vtkSmartPointer<vtkPen>::New();
transparentPen->SetLineType(vtkPen::NO_PEN);
painter->ApplyPen(transparentPen);
painter->GetBrush()->SetColorF(0., 0.,0.,1.);
painter->GetBrush()->SetColorF(1., 1., 1., 1.);
painter->GetBrush()->SetTexture(this->Texture);
......@@ -98,9 +109,14 @@ bool vtkScalarsToColorsItem::Paint(vtkContext2D* painter)
(this->Interpolate ? vtkBrush::Nearest : vtkBrush::Linear) |
vtkBrush::Stretch);
const int size = this->Shape->GetNumberOfPoints();
if (size == 4)
if (!this->MaskAboveCurve || size < 2)
{
painter->DrawPolygon(this->Shape);
double bounds[4];
this->GetBounds(bounds);
painter->DrawQuad(bounds[0], bounds[2],
bounds[0], bounds[3],
bounds[1], bounds[3],
bounds[1], bounds[2]);
}
else
{
......@@ -117,6 +133,13 @@ bool vtkScalarsToColorsItem::Paint(vtkContext2D* painter)
painter->DrawQuadStrip(trapezoids);
trapezoids->Delete();
}
if (this->PolyLinePen->GetLineType() != vtkPen::NO_PEN)
{
painter->ApplyPen(this->PolyLinePen);
painter->DrawPoly(this->Shape);
}
return true;
}
......
......@@ -16,7 +16,6 @@
#ifndef __vtkScalarsToColorsItem_h
#define __vtkScalarsToColorsItem_h
//#include "vtkContextItem.h"
#include "vtkPlot.h"
class vtkCallbackCommand;
......@@ -31,6 +30,15 @@ public:
virtual void GetBounds(double bounds[4]);
virtual bool Paint(vtkContext2D *painter);
// Description:
// Get a pointer to the vtkPen object that controls the was this plot draws
// lines.
vtkGetObjectMacro(PolyLinePen, vtkPen);
vtkSetMacro(MaskAboveCurve, bool);
vtkGetMacro(MaskAboveCurve, bool);
protected:
vtkScalarsToColorsItem();
virtual ~vtkScalarsToColorsItem();
......@@ -43,6 +51,9 @@ protected:
bool Interpolate;
vtkPoints2D* Shape;
vtkCallbackCommand* Callback;
vtkPen* PolyLinePen;
bool MaskAboveCurve;
private:
vtkScalarsToColorsItem(const vtkScalarsToColorsItem &); // Not implemented.
void operator=(const vtkScalarsToColorsItem &); // Not implemented.
......
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