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

  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"
16
#include "vtkCallbackCommand.h"
17 18 19 20 21 22 23
#include "vtkContext2D.h"
#include "vtkImageData.h"
#include "vtkColorTransferFunction.h"
#include "vtkColorTransferFunctionItem.h"
#include "vtkObjectFactory.h"
#include "vtkPen.h"
#include "vtkPointData.h"
24
#include "vtkPoints2D.h"
25 26

#include <cassert>
27
#include <cmath>
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkColorTransferFunctionItem);

//-----------------------------------------------------------------------------
vtkColorTransferFunctionItem::vtkColorTransferFunctionItem()
{
  this->ColorTransferFunction = 0;
}

//-----------------------------------------------------------------------------
vtkColorTransferFunctionItem::~vtkColorTransferFunctionItem()
{
  if (this->ColorTransferFunction)
    {
43
    this->ColorTransferFunction->RemoveObserver(this->Callback);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    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;
    }
}

65
//-----------------------------------------------------------------------------
66
void vtkColorTransferFunctionItem::ComputeBounds(double* bounds)
67
{
68
  this->Superclass::ComputeBounds(bounds);
69 70 71 72 73 74 75 76
  if (this->ColorTransferFunction)
    {
    double* range = this->ColorTransferFunction->GetRange();
    bounds[0] = range[0];
    bounds[1] = range[1];
    }
}

77 78 79
//-----------------------------------------------------------------------------
void vtkColorTransferFunctionItem::SetColorTransferFunction(vtkColorTransferFunction* t)
{
80 81 82 83 84 85 86 87
  if (t == this->ColorTransferFunction)
    {
    return;
    }
  if (this->ColorTransferFunction)
    {
    this->ColorTransferFunction->RemoveObserver(this->Callback);
    }
88
  vtkSetObjectBodyMacro(ColorTransferFunction, vtkColorTransferFunction, t);
89 90 91 92
  if (t)
    {
    t->AddObserver(vtkCommand::ModifiedEvent, this->Callback);
    }
93
  this->ScalarsToColorsModified(t, vtkCommand::ModifiedEvent, 0);
94 95 96 97 98
}

//-----------------------------------------------------------------------------
void vtkColorTransferFunctionItem::ComputeTexture()
{
99 100
  double bounds[4];
  this->GetBounds(bounds);
101 102
  if (bounds[0] == bounds[1]
      || !this->ColorTransferFunction)
103 104 105
    {
    return;
    }
106 107 108 109 110
  if (this->Texture == 0)
    {
    this->Texture = vtkImageData::New();
    }

111
  // Could depend of the screen resolution
112
  const int dimension = this->GetTextureWidth();
113 114
  double* values = new double[dimension];
  // Texture 1D
115 116 117
  this->Texture->SetExtent(0, dimension-1,
                           0, 0,
                           0, 0);
118
  this->Texture->AllocateScalars(VTK_UNSIGNED_CHAR, 4);
119 120 121 122
  bool isLogTable = this->UsingLogScale();
  double logBoundsMin = bounds[0] > 0.0 ? log10(bounds[0]) : 0.0;
  double logBoundsDelta = (bounds[0] > 0.0 && bounds[1] > 0.0)?
    (log10(bounds[1])-log10(bounds[0])) : 0.0;
123 124
  for (int i = 0; i < dimension; ++i)
    {
125 126 127 128 129 130 131 132 133 134
    if (isLogTable)
      {
      double normVal = i/(dimension-1.0);
      double lval = logBoundsMin + normVal*logBoundsDelta;
      values[i] = pow(10.0, lval);
      }
    else
      {
      values[i] = bounds[0] + i * (bounds[1] - bounds[0]) / (dimension - 1);
      }
135
    }
136 137
  unsigned char* ptr =
    reinterpret_cast<unsigned char*>(this->Texture->GetScalarPointer(0,0,0));
138
  this->ColorTransferFunction->MapScalarsThroughTable2(
139
    values, ptr, VTK_DOUBLE, dimension, VTK_LUMINANCE, VTK_RGBA);
140 141 142 143 144 145 146 147
  if (this->Opacity != 1.0)
    {
    for (int i = 0; i < dimension; ++i)
      {
      ptr[3] = static_cast<unsigned char>(this->Opacity * ptr[3]);
      ptr+=4;
      }
    }
148
  delete [] values;
149
  return;
150
}
151 152 153 154 155 156 157

//-----------------------------------------------------------------------------
bool vtkColorTransferFunctionItem::UsingLogScale()
{
  return this->ColorTransferFunction?
    (this->ColorTransferFunction->UsingLogScale() != 0) : false;
}