vtkLookupTableItem.cxx 3.49 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*=========================================================================

  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.

=========================================================================*/

16
#include "vtkCallbackCommand.h"
17 18 19 20 21
#include "vtkImageData.h"
#include "vtkLookupTable.h"
#include "vtkLookupTableItem.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
22
#include "vtkPoints2D.h"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

#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;
    }
}

62 63 64 65 66 67 68 69 70 71 72 73
//-----------------------------------------------------------------------------
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];
    }
}

74 75 76 77
//-----------------------------------------------------------------------------
void vtkLookupTableItem::SetLookupTable(vtkLookupTable* t)
{
  vtkSetObjectBodyMacro(LookupTable, vtkLookupTable, t);
78 79 80 81
  if (t)
    {
    t->AddObserver(vtkCommand::ModifiedEvent, this->Callback);
    }
82
  this->ScalarsToColorsModified(this->LookupTable, vtkCommand::ModifiedEvent, 0);
83 84 85 86 87 88 89 90 91
}

//-----------------------------------------------------------------------------
void vtkLookupTableItem::ComputeTexture()
{
  if (this->Texture == 0)
    {
    this->Texture = vtkImageData::New();
    }
92 93 94 95 96
  // Could depend of the screen resolution
  const int dimension = 256;
  double values[256];
  // Texture 1D
  this->Texture->SetExtent(0, dimension - 1,
97 98
                           0,0,
                           0,0);
99
  this->Texture->AllocateScalars(VTK_UNSIGNED_CHAR, 4);
100 101 102 103 104 105 106 107 108 109 110 111
  // TODO: Support log scale ?
  double bounds[4];
  this->GetBounds(bounds);
  if (bounds[0] == bounds[1])
    {
    vtkWarningMacro(<< "The lookuptable seems empty");
    return;
    }
  for (int i = 0; i < dimension; ++i)
    {
    values[i] = bounds[0] + i * (bounds[1] - bounds[0]) / (dimension - 1);
    }
112 113
  unsigned char* ptr =
    reinterpret_cast<unsigned char*>(this->Texture->GetScalarPointer(0,0,0));
114
  this->LookupTable->MapScalarsThroughTable2(
115 116 117 118 119 120 121 122 123
    values, ptr, VTK_DOUBLE, dimension, 1, 4);
  if (this->Opacity != 1.)
    {
    for (int i = 0; i < dimension; ++i)
      {
      ptr[3] = static_cast<unsigned char>(this->Opacity * ptr[3]);
      ptr+=4;
      }
    }
124
}