vtkLookupTableItem.cxx 3.59 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

#include <cassert>

//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkLookupTableItem);

//-----------------------------------------------------------------------------
vtkLookupTableItem::vtkLookupTableItem()
{
  this->Interpolate = false;
33
  this->LookupTable = nullptr;
34 35 36 37 38 39
}

//-----------------------------------------------------------------------------
vtkLookupTableItem::~vtkLookupTableItem()
{
  if (this->LookupTable)
40
  {
41
    this->LookupTable->Delete();
42
    this->LookupTable = nullptr;
43
  }
44 45 46 47 48 49 50 51
}

//-----------------------------------------------------------------------------
void vtkLookupTableItem::PrintSelf(ostream &os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "LookupTable: ";
  if (this->LookupTable)
52
  {
53 54
    os << endl;
    this->LookupTable->PrintSelf(os, indent.GetNextIndent());
55
  }
56
  else
57
  {
58
    os << "(none)" << endl;
59
  }
60 61
}

62
//-----------------------------------------------------------------------------
63
void vtkLookupTableItem::ComputeBounds(double* bounds)
64
{
65
  this->Superclass::ComputeBounds(bounds);
66
  if (this->LookupTable)
67
  {
68 69 70
    double* range = this->LookupTable->GetRange();
    bounds[0] = range[0];
    bounds[1] = range[1];
71
  }
72 73
}

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

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