Commit 1050b901 authored by Julien Finet's avatar Julien Finet

ENH: Add transfer function 2D items

parent 2e4d2810
/*=========================================================================
Program: Visualization Toolkit
Module: vtkColorTransferFunctionItem.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 "vtkBrush.h"
#include "vtkContext2D.h"
#include "vtkImageData.h"
#include "vtkColorTransferFunction.h"
#include "vtkColorTransferFunctionItem.h"
#include "vtkObjectFactory.h"
#include "vtkPen.h"
#include "vtkPointData.h"
#include <cassert>
//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkColorTransferFunctionItem);
//-----------------------------------------------------------------------------
vtkColorTransferFunctionItem::vtkColorTransferFunctionItem()
{
this->ColorTransferFunction = 0;
}
//-----------------------------------------------------------------------------
vtkColorTransferFunctionItem::~vtkColorTransferFunctionItem()
{
if (this->ColorTransferFunction)
{
this->ColorTransferFunction->Delete();
this->ColorTransferFunction = 0;
}
}
//-----------------------------------------------------------------------------
void vtkColorTransferFunctionItem::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "ColorTransferFunction: ";
if (this->ColorTransferFunction)
{
os << endl;
this->ColorTransferFunction->PrintSelf(os, indent.GetNextIndent());
}
else
{
os << "(none)" << endl;
}
}
//-----------------------------------------------------------------------------
void vtkColorTransferFunctionItem::SetColorTransferFunction(vtkColorTransferFunction* t)
{
vtkSetObjectBodyMacro(ColorTransferFunction, vtkColorTransferFunction, t);
}
//-----------------------------------------------------------------------------
void vtkColorTransferFunctionItem::ComputeTexture()
{
if (this->Texture == 0)
{
this->Texture = vtkImageData::New();
}
const int dimension = 256;
double values[256];
// should depends on the true size on screen
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;
ptr+=4;
}
this->ColorTransferFunction->MapScalarsThroughTable2(
values,
reinterpret_cast<unsigned char*>(this->Texture->GetScalarPointer(0,0,0)),
VTK_DOUBLE,dimension,1,4);
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkColorTransferFunctionItem.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.
=========================================================================*/
#ifndef __vtkColorTransferFunctionItem_h
#define __vtkColorTransferFunctionItem_h
#include "vtkScalarsToColorsItem.h"
class vtkColorTransferFunction;
class vtkImageData;
class VTK_CHARTS_EXPORT vtkColorTransferFunctionItem: public vtkScalarsToColorsItem
{
public:
static vtkColorTransferFunctionItem* New();
vtkTypeMacro(vtkColorTransferFunctionItem, vtkScalarsToColorsItem);
virtual void PrintSelf(ostream &os, vtkIndent indent);
void SetColorTransferFunction(vtkColorTransferFunction* t);
vtkGetObjectMacro(ColorTransferFunction, vtkColorTransferFunction);
protected:
vtkColorTransferFunctionItem();
virtual ~vtkColorTransferFunctionItem();
virtual void ComputeTexture();
vtkColorTransferFunction* ColorTransferFunction;
};
#endif
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCompositeTransferFunctionItem.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 "vtkImageData.h"
#include "vtkPiecewiseFunction.h"
#include "vtkCompositeTransferFunctionItem.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include <cassert>
//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkCompositeTransferFunctionItem);
//-----------------------------------------------------------------------------
vtkCompositeTransferFunctionItem::vtkCompositeTransferFunctionItem()
{
this->OpacityFunction = 0;
}
//-----------------------------------------------------------------------------
vtkCompositeTransferFunctionItem::~vtkCompositeTransferFunctionItem()
{
if (this->OpacityFunction)
{
this->OpacityFunction->Delete();
this->OpacityFunction = 0;
}
}
//-----------------------------------------------------------------------------
void vtkCompositeTransferFunctionItem::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "CompositeTransferFunction: ";
if (this->OpacityFunction)
{
os << endl;
this->OpacityFunction->PrintSelf(os, indent.GetNextIndent());
}
else
{
os << "(none)" << endl;
}
}
//-----------------------------------------------------------------------------
void vtkCompositeTransferFunctionItem::SetOpacityFunction(vtkPiecewiseFunction* opacity)
{
vtkSetObjectBodyMacro(OpacityFunction, vtkPiecewiseFunction, opacity);
}
//-----------------------------------------------------------------------------
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])
{
vtkWarningMacro(<< "The piecewise function seems empty");
return;
}
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)
{
ptr[3] = static_cast<unsigned char>(values[i] * this->Opacity * 255);
ptr+=4;
}
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCompositeTransferFunctionItem.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.
=========================================================================*/
#ifndef __vtkCompositeTransferFunctionItem_h
#define __vtkCompositeTransferFunctionItem_h
#include "vtkColorTransferFunctionItem.h"
class vtkPiecewiseFunction;
class VTK_CHARTS_EXPORT vtkCompositeTransferFunctionItem: public vtkColorTransferFunctionItem
{
public:
static vtkCompositeTransferFunctionItem* New();
vtkTypeMacro(vtkCompositeTransferFunctionItem, vtkColorTransferFunctionItem);
virtual void PrintSelf(ostream &os, vtkIndent indent);
void SetOpacityFunction(vtkPiecewiseFunction* opacity);
vtkGetObjectMacro(OpacityFunction, vtkPiecewiseFunction);
protected:
vtkCompositeTransferFunctionItem();
virtual ~vtkCompositeTransferFunctionItem();
virtual void ComputeTexture();
vtkPiecewiseFunction* OpacityFunction;
};
#endif
/*=========================================================================
Program: Visualization Toolkit
Module: vtkLookupTableItem.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 "vtkImageData.h"
#include "vtkLookupTable.h"
#include "vtkLookupTableItem.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include <cassert>
//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkLookupTableItem);
//-----------------------------------------------------------------------------
vtkLookupTableItem::vtkLookupTableItem()
{
this->Interpolate = false;
this->LookupTable = 0;
}
//-----------------------------------------------------------------------------
vtkLookupTableItem::~vtkLookupTableItem()
{
if (this->LookupTable)
{
this->LookupTable->Delete();
this->LookupTable = 0;
}
}
//-----------------------------------------------------------------------------
void vtkLookupTableItem::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "LookupTable: ";
if (this->LookupTable)
{
os << endl;
this->LookupTable->PrintSelf(os, indent.GetNextIndent());
}
else
{
os << "(none)" << endl;
}
}
//-----------------------------------------------------------------------------
void vtkLookupTableItem::SetLookupTable(vtkLookupTable* t)
{
vtkSetObjectBodyMacro(LookupTable, vtkLookupTable, t);
}
//-----------------------------------------------------------------------------
void vtkLookupTableItem::ComputeTexture()
{
if (this->Texture == 0)
{
this->Texture = vtkImageData::New();
}
this->Texture->SetExtent(0, this->LookupTable->GetNumberOfTableValues() - 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());
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkLookupTableItem.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.
=========================================================================*/
#ifndef __vtkLookupTableItem_h
#define __vtkLookupTableItem_h
#include "vtkScalarsToColorsItem.h"
class vtkLookupTable;
class VTK_CHARTS_EXPORT vtkLookupTableItem: public vtkScalarsToColorsItem
{
public:
static vtkLookupTableItem* New();
vtkTypeMacro(vtkLookupTableItem, vtkScalarsToColorsItem);
virtual void PrintSelf(ostream &os, vtkIndent indent);
void SetLookupTable(vtkLookupTable* t);
vtkGetObjectMacro(LookupTable, vtkLookupTable);
protected:
vtkLookupTableItem();
virtual ~vtkLookupTableItem();
virtual void ComputeTexture();
vtkLookupTable* LookupTable;
};
#endif
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPiecewiseFunctionItem.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 "vtkBrush.h"
#include "vtkContext2D.h"
#include "vtkImageData.h"
#include "vtkPiecewiseFunction.h"
#include "vtkPiecewiseFunctionItem.h"
#include "vtkObjectFactory.h"
#include "vtkPen.h"
#include "vtkPointData.h"
#include <cassert>
//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkPiecewiseFunctionItem);
//-----------------------------------------------------------------------------
vtkPiecewiseFunctionItem::vtkPiecewiseFunctionItem()
{
this->PiecewiseFunction = 0;
this->Color[0] = 255; this->Color[1] = 255; this->Color[2] = 255;
}
//-----------------------------------------------------------------------------
vtkPiecewiseFunctionItem::~vtkPiecewiseFunctionItem()
{
if (this->PiecewiseFunction)
{
this->PiecewiseFunction->Delete();
this->PiecewiseFunction = 0;
}
}
//-----------------------------------------------------------------------------
void vtkPiecewiseFunctionItem::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "PiecewiseFunction: ";
if (this->PiecewiseFunction)
{
os << endl;
this->PiecewiseFunction->PrintSelf(os, indent.GetNextIndent());
}
else
{
os << "(none)" << endl;
}
}
//-----------------------------------------------------------------------------
void vtkPiecewiseFunctionItem::SetPiecewiseFunction(vtkPiecewiseFunction* t)
{
vtkSetObjectBodyMacro(PiecewiseFunction, vtkPiecewiseFunction, t);
}
//-----------------------------------------------------------------------------
void vtkPiecewiseFunctionItem::ComputeTexture()
{
if (this->Texture == 0)
{
this->Texture = vtkImageData::New();
}
const int dimension = 256;
double values[256];
// should depends on the true size on screen
this->Texture->SetExtent(0, dimension-1,
0, 0,
0, 0);
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);
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)
{
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;
}
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPiecewiseFunctionItem.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.
=========================================================================*/
#ifndef __vtkPiecewiseFunctionItem_h
#define __vtkPiecewiseFunctionItem_h
#include "vtkScalarsToColorsItem.h"
class vtkPiecewiseFunction;
class vtkImageData;
class VTK_CHARTS_EXPORT vtkPiecewiseFunctionItem: public vtkScalarsToColorsItem
{
public:
static vtkPiecewiseFunctionItem* New();
vtkTypeMacro(vtkPiecewiseFunctionItem, vtkScalarsToColorsItem);
virtual void PrintSelf(ostream &os, vtkIndent indent);
void SetPiecewiseFunction(vtkPiecewiseFunction* t);
vtkGetObjectMacro(PiecewiseFunction, vtkPiecewiseFunction);
vtkSetVector3Macro(Color, unsigned char);
vtkGetVector3Macro(Color, unsigned char);
protected:
vtkPiecewiseFunctionItem();
virtual ~vtkPiecewiseFunctionItem();
// Description
// Compute the texture from the PiecewiseFunction
virtual void ComputeTexture();
vtkPiecewiseFunction* PiecewiseFunction;
unsigned char Color[3];
};
#endif
/*=========================================================================
Program: Visualization Toolkit
Module: vtkScalarsToColorsItem.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 "vtkBrush.h"
#include "vtkContext2D.h"
#include "vtkImageData.h"
#include "vtkScalarsToColorsItem.h"
#include "vtkObjectFactory.h"
#include "vtkPen.h"
#include <cassert>
//-----------------------------------------------------------------------------
vtkScalarsToColorsItem::vtkScalarsToColorsItem()
{
this->Texture = 0;
this->Interpolate = true;
}
//-----------------------------------------------------------------------------
vtkScalarsToColorsItem::~vtkScalarsToColorsItem()
{
if (this->Texture)
{
this->Texture->Delete();
this->Texture = 0;
}
}
//-----------------------------------------------------------------------------
void vtkScalarsToColorsItem::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Interpolate: " << this->Interpolate << endl;
}
//-----------------------------------------------------------------------------
bool vtkScalarsToColorsItem::Paint(vtkContext2D* painter)
{
if (this->Texture == 0 ||
this->Texture->GetMTime() < this->GetMTime())
{
this->ComputeTexture();
}
painter->GetPen()->SetLineType(vtkPen::NO_PEN);
painter->GetBrush()->SetColorF(1., 1., 1., 1.);
painter->GetBrush()->SetTexture(this->Texture);
painter->GetBrush()->SetTextureProperties(
(this->Interpolate ? vtkBrush::Nearest : vtkBrush::Linear) |
vtkBrush::Stretch);
painter->DrawRect(0.f,0.f,100.f,100.f);
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkScalarsToColorsItem.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.
=========================================================================*/
#ifndef __vtkScalarsToColorsItem_h
#define __vtkScalarsToColorsItem_h
#include "vtkContextItem.h"
class vtkImageData;
class VTK_CHARTS_EXPORT vtkScalarsToColorsItem: public vtkContextItem
{
public:
vtkTypeMacro(vtkScalarsToColorsItem, vtkContextItem);
virtual void PrintSelf(ostream &os, vtkIndent indent);
virtual bool Paint(vtkContext2D *painter);
protected:
vtkScalarsToColorsItem();
virtual ~vtkScalarsToColorsItem();
virtual void ComputeTexture() = 0;
vtkImageData* Texture;
bool Interpolate;
private:
vtkScalarsToColorsItem(const vtkScalarsToColorsItem &); // Not implemented.
void operator=(const vtkScalarsToColorsItem &); // 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