Commit d65f6e4d authored by Karthik Krishnan's avatar Karthik Krishnan
Browse files

ENH: Add functionality to be able to display both opacity and scalar values for

    a scalar bar. The opacity is turned off by default. If ON, it displays the opacity values in the
    lookup table by varying the opacity of the color. A texture grid is placed in the background, so
    you can see something if things are totally invisible
parent 7f2e2d42
......@@ -25,8 +25,14 @@
#include "vtkViewport.h"
#include "vtkWindow.h"
#include "vtkLookupTable.h"
vtkCxxRevisionMacro(vtkScalarBarActor, "1.57");
#include "vtkFloatArray.h"
#include "vtkPointData.h"
#include "vtkTexture.h"
#include "vtkImageData.h"
#include "vtkRenderer.h"
#include "vtkProperty2D.h"
vtkCxxRevisionMacro(vtkScalarBarActor, "1.58");
vtkStandardNewMacro(vtkScalarBarActor);
vtkCxxSetObjectMacro(vtkScalarBarActor,LookupTable,vtkScalarsToColors);
......@@ -84,6 +90,67 @@ vtkScalarBarActor::vtkScalarBarActor()
this->LastOrigin[1] = 0;
this->LastSize[0] = 0;
this->LastSize[1] = 0;
// If opacity is on, a jail like texture is displayed behind it..
this->UseOpacity = 0;
this->TextureGridWidth = 10.0;
this->TexturePolyData = vtkPolyData::New();
vtkPolyDataMapper2D * textureMapper = vtkPolyDataMapper2D::New();
textureMapper->SetInput(this->TexturePolyData);
this->TextureActor = vtkActor2D::New();
this->TextureActor->SetMapper(textureMapper);
textureMapper->Delete();
this->TextureActor->GetPositionCoordinate()->
SetReferenceCoordinate(this->PositionCoordinate);
vtkFloatArray* tc = vtkFloatArray::New();
tc->SetNumberOfComponents(2);
tc->SetNumberOfTuples(4);
tc->InsertComponent(0,0, 0.0);
tc->InsertComponent(0,1, 0.0);
tc->InsertComponent(1,1, 0.0);
tc->InsertComponent(3,0, 0.0);
this->TexturePolyData->GetPointData()->SetTCoords(tc);
tc->Delete();
vtkCellArray* polys2 = vtkCellArray::New();
polys2->InsertNextCell(4);
polys2->InsertCellPoint(0);
polys2->InsertCellPoint(1);
polys2->InsertCellPoint(2);
polys2->InsertCellPoint(3);
this->TexturePolyData->SetPolys(polys2);
polys2->Delete();
vtkProperty2D *imageProperty = vtkProperty2D::New();
imageProperty->SetOpacity(0.08);
this->TextureActor->SetProperty(imageProperty);
imageProperty->Delete();
// Create the default texture. Just a "Jail" like grid
const unsigned int dim = 128;
vtkImageData *image = vtkImageData::New();
image->SetDimensions(dim, dim, 1);
image->SetScalarTypeToUnsignedChar();
image->AllocateScalars();
for (unsigned int y = 0; y < dim; y++)
{
unsigned char *ptr =
static_cast< unsigned char * >(image->GetScalarPointer(0, y, 0));
for (unsigned int x = 0; x < dim; x++)
{
*ptr = ((x == y) || (x == (dim-y-1))) ? 255 : 0;
++ptr;
}
}
this->Texture = vtkTexture::New();
this->Texture->SetInput( image );
this->Texture->RepeatOn();
image->Delete();
}
//----------------------------------------------------------------------------
......@@ -139,6 +206,9 @@ vtkScalarBarActor::~vtkScalarBarActor()
this->SetLookupTable(NULL);
this->SetLabelTextProperty(NULL);
this->SetTitleTextProperty(NULL);
this->Texture->Delete();
this->TextureActor->Delete();
this->TexturePolyData->Delete();
}
//----------------------------------------------------------------------------
......@@ -164,6 +234,12 @@ int vtkScalarBarActor::RenderOverlay(vtkViewport *viewport)
renderedSomething += this->TextActors[i]->RenderOverlay(viewport);
}
if (this->UseOpacity)
{
this->Texture->Render(vtkRenderer::SafeDownCast(viewport));
renderedSomething += this->TextureActor->RenderOverlay(viewport);
}
renderedSomething = (renderedSomething > 0)?(1):(0);
return renderedSomething;
......@@ -270,7 +346,9 @@ int vtkScalarBarActor::RenderOpaqueGeometry(vtkViewport *viewport)
vtkCellArray *polys = vtkCellArray::New();
polys->Allocate(polys->EstimateSize(numColors,4));
vtkUnsignedCharArray *colors = vtkUnsignedCharArray::New();
colors->SetNumberOfComponents(3);
unsigned int nComponents = ((this->UseOpacity) ? 4 : 3);
colors->SetNumberOfComponents( nComponents );
colors->SetNumberOfTuples(numColors);
this->ScalarBarActor->SetProperty(this->GetProperty());
......@@ -373,10 +451,14 @@ int vtkScalarBarActor::RenderOpaqueGeometry(vtkViewport *viewport)
((double)i /(numColors-1.0)));
}
rgb = colors->GetPointer(3*i); //write into array directly
rgb = colors->GetPointer( nComponents * i); //write into array directly
rgb[0] = rgba[0];
rgb[1] = rgba[1];
rgb[2] = rgba[2];
if (this->UseOpacity)
{
rgb[3] = rgba[3];
}
}
// Now position everything properly
......@@ -424,6 +506,23 @@ int vtkScalarBarActor::RenderOpaqueGeometry(vtkViewport *viewport)
}
}
// Set the texture points
//
vtkPoints *texturePoints = vtkPoints::New();
texturePoints->SetNumberOfPoints(4);
this->TexturePolyData->SetPoints(texturePoints);
texturePoints->SetPoint(0, 0.0, 0.0, 0.0);
texturePoints->SetPoint(1, barWidth, 0.0, 0.0);
texturePoints->SetPoint(2, barWidth, barHeight, 0.0);
texturePoints->SetPoint(3, 0.0, barHeight, 0.0);
texturePoints->Delete();
vtkDataArray * tc = this->TexturePolyData->GetPointData()->GetTCoords();
tc->SetTuple2(1, barWidth / this->TextureGridWidth, 0.0);
tc->SetTuple2(2, barWidth / this->TextureGridWidth,
barHeight / this->TextureGridWidth);
tc->SetTuple2(3, 0.0, barHeight / this->TextureGridWidth);
this->BuildTime.Modified();
}
......@@ -503,6 +602,11 @@ void vtkScalarBarActor::PrintSelf(ostream& os, vtkIndent indent)
}
os << indent << "Label Format: " << this->LabelFormat << "\n";
os << indent << "UseOpacity: " << this->UseOpacity << "\n";
if (this->UseOpacity)
{
os << indent << "TextureGridWidth: " << this->TextureGridWidth << "\n";
}
}
//----------------------------------------------------------------------------
......
......@@ -60,6 +60,7 @@ class vtkPolyDataMapper2D;
class vtkScalarsToColors;
class vtkTextMapper;
class vtkTextProperty;
class vtkTexture;
#define VTK_ORIENT_HORIZONTAL 0
#define VTK_ORIENT_VERTICAL 1
......@@ -99,6 +100,16 @@ public:
virtual void SetLookupTable(vtkScalarsToColors*);
vtkGetObjectMacro(LookupTable,vtkScalarsToColors);
// Description:
// Should be display the opacity as well. This is displayed by changing
// the opacity of the scalar bar in accordance with the opacity of the
// given color. For clarity, a texture grid is placed in the background
// if Opacity is ON. You might also want to play with SetTextureGridWith
// in that case. [Default: off]
vtkSetMacro( UseOpacity, int );
vtkGetMacro( UseOpacity, int );
vtkBooleanMacro( UseOpacity, int );
// Description:
// Set/Get the maximum number of scalar bar segments to show. This may
// differ from the number of colors in the lookup table, in which case
......@@ -144,6 +155,15 @@ public:
// Shallow copy of a scalar bar actor. Overloads the virtual vtkProp method.
void ShallowCopy(vtkProp *prop);
// Description:
// Set the width of the texture grid. Used only if UseOpacity is ON.
vtkSetMacro( TextureGridWidth, double );
vtkGetMacro( TextureGridWidth, double );
// Description:
// Get the texture actor.. you may want to change some properties on it
vtkGetObjectMacro( TextureActor, vtkActor2D );
protected:
vtkScalarBarActor();
~vtkScalarBarActor();
......@@ -158,6 +178,8 @@ protected:
int Orientation;
char *Title;
char *LabelFormat;
int UseOpacity; // off by default
double TextureGridWidth;
vtkTextMapper **TextMappers;
virtual void AllocateAndSizeLabels(int *labelSize, int *size,
......@@ -173,6 +195,10 @@ private:
vtkPolyDataMapper2D *ScalarBarMapper;
vtkActor2D *ScalarBarActor;
vtkPolyData *TexturePolyData;
vtkTexture *Texture;
vtkActor2D *TextureActor;
vtkTimeStamp BuildTime;
int LastSize[2];
int LastOrigin[2];
......
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