Commit 1b267bab authored by Julien Finet's avatar Julien Finet

ENH: Allow vtkScalarsToColorsItem to draw an arbitrary shape

vtkPiecewiseFunctionItem and vtkCompositeTransferFunctionItem can mask
pixels above the alpha value.
parent 02a45620
......@@ -73,13 +73,15 @@ int TestScalarsToColors( int argc, char * argv [] )
item3->SetColorTransferFunction(colorTransferFunction);
item3->SetOpacityFunction(opacityFunction);
item3->SetOpacity(0.2);
//view->GetScene()->AddItem(item3);
item3->SetMaskAboveCurve(true);
view->GetScene()->AddItem(item3);
vtkSmartPointer<vtkPiecewiseFunctionItem> item4 =
vtkSmartPointer<vtkPiecewiseFunctionItem>::New();
item4->SetPiecewiseFunction(opacityFunction);
item4->SetColor(255,0,0);
view->GetScene()->AddItem(item4);
//item4->SetMaskAboveCurve(true);
//view->GetScene()->AddItem(item4);
//Finally render the scene and compare the image to a reference image
//view->GetRenderWindow()->SetMultiSamples(0);
......
......@@ -18,6 +18,7 @@
#include "vtkCompositeTransferFunctionItem.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints2D.h"
#include <cassert>
......@@ -28,6 +29,7 @@ vtkStandardNewMacro(vtkCompositeTransferFunctionItem);
vtkCompositeTransferFunctionItem::vtkCompositeTransferFunctionItem()
{
this->OpacityFunction = 0;
this->MaskAboveCurve = false;
}
//-----------------------------------------------------------------------------
......@@ -62,6 +64,25 @@ void vtkCompositeTransferFunctionItem::SetOpacityFunction(vtkPiecewiseFunction*
vtkSetObjectBodyMacro(OpacityFunction, vtkPiecewiseFunction, opacity);
}
//-----------------------------------------------------------------------------
void vtkCompositeTransferFunctionItem::SetMaskAboveCurve(bool mask)
{
if (mask == this->MaskAboveCurve)
{
return;
}
if (mask == false)
{
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->MaskAboveCurve = mask;
this->Modified();
}
//-----------------------------------------------------------------------------
void vtkCompositeTransferFunctionItem::ComputeTexture()
{
......@@ -80,9 +101,28 @@ void vtkCompositeTransferFunctionItem::ComputeTexture()
this->OpacityFunction->GetTable(range[0], range[1], dimension, values);
unsigned char* ptr =
reinterpret_cast<unsigned char*>(this->Texture->GetScalarPointer(0,0,0));
for (int i = 0; i < dimension; ++i)
if (MaskAboveCurve)
{
this->Shape->SetNumberOfPoints(dimension+2);
this->Shape->SetPoint(0, 0.f, 0.f);
this->Shape->SetPoint(dimension + 1, 100.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);
ptr+=4;
}
}
else
{
ptr[3] = static_cast<unsigned char>(values[i] * this->Opacity * 255);
ptr+=4;
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.);
ptr+=4;
}
}
}
......@@ -30,6 +30,8 @@ public:
void SetOpacityFunction(vtkPiecewiseFunction* opacity);
vtkGetObjectMacro(OpacityFunction, vtkPiecewiseFunction);
void SetMaskAboveCurve(bool mask);
vtkGetMacro(MaskAboveCurve, bool);
protected:
vtkCompositeTransferFunctionItem();
virtual ~vtkCompositeTransferFunctionItem();
......@@ -37,6 +39,7 @@ protected:
virtual void ComputeTexture();
vtkPiecewiseFunction* OpacityFunction;
bool MaskAboveCurve;
};
#endif
......@@ -21,6 +21,7 @@
#include "vtkObjectFactory.h"
#include "vtkPen.h"
#include "vtkPointData.h"
#include "vtkPoints2D.h"
#include <cassert>
......@@ -32,6 +33,7 @@ vtkPiecewiseFunctionItem::vtkPiecewiseFunctionItem()
{
this->PiecewiseFunction = 0;
this->Color[0] = 255; this->Color[1] = 255; this->Color[2] = 255;
this->MaskAboveCurve = false;
}
//-----------------------------------------------------------------------------
......@@ -66,6 +68,25 @@ void vtkPiecewiseFunctionItem::SetPiecewiseFunction(vtkPiecewiseFunction* t)
vtkSetObjectBodyMacro(PiecewiseFunction, vtkPiecewiseFunction, t);
}
//-----------------------------------------------------------------------------
void vtkPiecewiseFunctionItem::SetMaskAboveCurve(bool mask)
{
if (mask == this->MaskAboveCurve)
{
return;
}
if (mask == false)
{
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->MaskAboveCurve = mask;
this->Modified();
}
//-----------------------------------------------------------------------------
void vtkPiecewiseFunctionItem::ComputeTexture()
{
......@@ -93,13 +114,32 @@ void vtkPiecewiseFunctionItem::ComputeTexture()
this->PiecewiseFunction->GetTable(range[0], range[1], dimension, values);
unsigned char* ptr =
reinterpret_cast<unsigned char*>(this->Texture->GetScalarPointer(0,0,0));
unsigned char color[3] = {255, 255, 255};
for (int i = 0; i < dimension; ++i)
if (MaskAboveCurve)
{
this->Shape->SetNumberOfPoints(dimension + 2 );
this->Shape->SetPoint(0, 0.f, 0.f);
this->Shape->SetPoint(dimension + 1, 100.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);
assert(values[i] <= 1. && values[i] >= 0.);
this->Shape->SetPoint(i +1, static_cast<float>(i) * 100.f / dimension, values[i] * 100.f);
ptr+=4;
}
}
else
{
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+=4;
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);
assert(values[i] <= 1. && values[i] >= 0.);
ptr+=4;
}
}
}
......@@ -33,6 +33,9 @@ public:
vtkSetVector3Macro(Color, unsigned char);
vtkGetVector3Macro(Color, unsigned char);
void SetMaskAboveCurve(bool mask);
vtkGetMacro(MaskAboveCurve, bool);
protected:
vtkPiecewiseFunctionItem();
virtual ~vtkPiecewiseFunctionItem();
......@@ -42,7 +45,8 @@ protected:
virtual void ComputeTexture();
vtkPiecewiseFunction* PiecewiseFunction;
unsigned char Color[3];
unsigned char Color[3];
bool MaskAboveCurve;
};
#endif
......@@ -19,6 +19,7 @@
#include "vtkScalarsToColorsItem.h"
#include "vtkObjectFactory.h"
#include "vtkPen.h"
#include "vtkPoints2D.h"
#include <cassert>
......@@ -27,6 +28,13 @@ vtkScalarsToColorsItem::vtkScalarsToColorsItem()
{
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, 100.f, 0.f);
this->Shape->SetPoint(2, 100.f, 100.f);
this->Shape->SetPoint(3, 0.f, 100.f);
}
//-----------------------------------------------------------------------------
......@@ -37,6 +45,11 @@ vtkScalarsToColorsItem::~vtkScalarsToColorsItem()
this->Texture->Delete();
this->Texture = 0;
}
if (this->Shape)
{
this->Shape->Delete();
this->Shape = 0;
}
}
//-----------------------------------------------------------------------------
......@@ -60,5 +73,5 @@ bool vtkScalarsToColorsItem::Paint(vtkContext2D* painter)
painter->GetBrush()->SetTextureProperties(
(this->Interpolate ? vtkBrush::Nearest : vtkBrush::Linear) |
vtkBrush::Stretch);
painter->DrawRect(0.f,0.f,100.f,100.f);
painter->DrawPolygon(this->Shape);
}
......@@ -19,6 +19,7 @@
#include "vtkContextItem.h"
class vtkImageData;
class vtkPoints2D;
class VTK_CHARTS_EXPORT vtkScalarsToColorsItem: public vtkContextItem
{
......@@ -35,6 +36,7 @@ protected:
vtkImageData* Texture;
bool Interpolate;
vtkPoints2D* Shape;
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