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

  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"
17
#include "vtkCallbackCommand.h"
18
#include "vtkContext2D.h"
19
#include "vtkContextDevice2D.h"
20
#include "vtkContextScene.h"
21
#include "vtkFloatArray.h"
22
#include "vtkImageData.h"
23
#include "vtkNew.h"
24 25
#include "vtkObjectFactory.h"
#include "vtkPen.h"
26
#include "vtkPoints2D.h"
27
#include "vtkScalarsToColorsItem.h"
28 29
#include "vtkSmartPointer.h"
#include "vtkTransform2D.h"
30 31 32 33 34 35

#include <cassert>

//-----------------------------------------------------------------------------
vtkScalarsToColorsItem::vtkScalarsToColorsItem()
{
36 37 38 39
  this->PolyLinePen = vtkPen::New();
  this->PolyLinePen->SetWidth(2.);
  this->PolyLinePen->SetColor(64, 64, 72); // Payne's grey, why not
  this->PolyLinePen->SetLineType(vtkPen::NO_PEN);
40

41 42
  this->Texture = 0;
  this->Interpolate = true;
43 44
  this->Shape = vtkPoints2D::New();
  this->Shape->SetDataTypeToFloat();
45
  this->Shape->SetNumberOfPoints(0);
46 47 48 49 50

  this->Callback = vtkCallbackCommand::New();
  this->Callback->SetClientData(this);
  this->Callback->SetCallback(
    vtkScalarsToColorsItem::OnScalarsToColorsModified);
51 52

  this->MaskAboveCurve = false;
53

54 55
  this->UserBounds[0] = this->UserBounds[2] = 0.0;
  this->UserBounds[1] = this->UserBounds[3] = -1.0;
56 57 58 59 60
}

//-----------------------------------------------------------------------------
vtkScalarsToColorsItem::~vtkScalarsToColorsItem()
{
61 62 63 64 65
  if (this->PolyLinePen)
    {
    this->PolyLinePen->Delete();
    this->PolyLinePen = 0;
    }
66 67 68 69 70
  if (this->Texture)
    {
    this->Texture->Delete();
    this->Texture = 0;
    }
71 72 73 74 75
  if (this->Shape)
    {
    this->Shape->Delete();
    this->Shape = 0;
    }
76 77 78 79 80
  if (this->Callback)
    {
    this->Callback->Delete();
    this->Callback = 0;
    }
81 82 83 84 85 86 87 88 89
}

//-----------------------------------------------------------------------------
void vtkScalarsToColorsItem::PrintSelf(ostream &os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  os << indent << "Interpolate: " << this->Interpolate << endl;
}

90 91
//-----------------------------------------------------------------------------
void vtkScalarsToColorsItem::GetBounds(double bounds[4])
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
{
  if (this->UserBounds[1] > this->UserBounds[0] &&
      this->UserBounds[3] > this->UserBounds[2])
    {
    bounds[0] = this->UserBounds[0];
    bounds[1] = this->UserBounds[1];
    bounds[2] = this->UserBounds[2];
    bounds[3] = this->UserBounds[3];
    return;
    }
  this->ComputeBounds(bounds);
}

//-----------------------------------------------------------------------------
void vtkScalarsToColorsItem::ComputeBounds(double bounds[4])
107
{
108 109 110 111
  bounds[0] = 0.;
  bounds[1] = 1.;
  bounds[2] = 0.;
  bounds[3] = 1.;
112 113
}

114 115 116
//-----------------------------------------------------------------------------
bool vtkScalarsToColorsItem::Paint(vtkContext2D* painter)
{
117
  this->TextureWidth = this->GetScene()->GetViewWidth();
118 119 120 121 122
  if (this->Texture == 0 ||
      this->Texture->GetMTime() < this->GetMTime())
    {
    this->ComputeTexture();
    }
123 124 125 126
  if (this->Texture == 0)
    {
    return false;
    }
127 128 129
  vtkSmartPointer<vtkPen> transparentPen = vtkSmartPointer<vtkPen>::New();
  transparentPen->SetLineType(vtkPen::NO_PEN);
  painter->ApplyPen(transparentPen);
130
  painter->GetBrush()->SetColorF(0., 0.,0.,1.);
131 132 133 134 135
  painter->GetBrush()->SetColorF(1., 1., 1., 1.);
  painter->GetBrush()->SetTexture(this->Texture);
  painter->GetBrush()->SetTextureProperties(
    (this->Interpolate ? vtkBrush::Nearest : vtkBrush::Linear) |
    vtkBrush::Stretch);
136
  const int size = this->Shape->GetNumberOfPoints();
137
  if (!this->MaskAboveCurve || size < 2)
138
    {
139 140 141 142 143 144 145 146 147 148 149 150 151 152
    double dbounds[4];
    this->GetBounds(dbounds);

    // shift/scale to scale from data space to rendering space.
    const vtkRectd& ss = this->ShiftScale;
    float fbounds[4];
    fbounds[0] = static_cast<float>((dbounds[0] + ss[0]) * ss[2]);
    fbounds[1] = static_cast<float>((dbounds[1] + ss[0]) * ss[2]);
    fbounds[2] = static_cast<float>((dbounds[2] + ss[1]) * ss[3]);
    fbounds[3] = static_cast<float>((dbounds[3] + ss[1]) * ss[3]);
    painter->DrawQuad(fbounds[0], fbounds[2],
                      fbounds[0], fbounds[3],
                      fbounds[1], fbounds[3],
                      fbounds[1], fbounds[2]);
153 154 155
    }
  else
    {
156 157
    const vtkRectd& ss = this->ShiftScale;

158 159 160 161 162 163 164
    vtkPoints2D* trapezoids = vtkPoints2D::New();
    trapezoids->SetNumberOfPoints(2*size);
    double point[2];
    vtkIdType j = -1;
    for (vtkIdType i = 0; i < size; ++i)
      {
      this->Shape->GetPoint(i, point);
165 166 167 168

      // shift/scale to scale from data space to rendering space.
      point[0] = (point[0] + ss[0]) * ss[2];
      point[1] = (point[1] + ss[1]) * ss[3];
169 170 171 172 173 174
      trapezoids->SetPoint(++j, point[0], 0.);
      trapezoids->SetPoint(++j, point);
      }
    painter->DrawQuadStrip(trapezoids);
    trapezoids->Delete();
    }
175 176 177

  if (this->PolyLinePen->GetLineType() != vtkPen::NO_PEN)
    {
178 179 180 181 182 183 184 185 186 187 188 189 190
    const vtkRectd& ss = this->ShiftScale;

    vtkNew<vtkPoints2D> transformedShape;
    transformedShape->SetNumberOfPoints(size);
    for (vtkIdType i = 0; i < size; ++i)
      {
      double point[2];
      this->Shape->GetPoint(i, point);
      // shift/scale to scale from data space to rendering space.
      point[0] = (point[0] + ss[0]) * ss[2];
      point[1] = (point[1] + ss[1]) * ss[3];
      transformedShape->SetPoint(i, point);
      }
191
    painter->ApplyPen(this->PolyLinePen);
192
    painter->DrawPoly(transformedShape.GetPointer());
193 194
    }

195
  return true;
196
}
197 198 199 200 201 202 203 204 205 206 207 208 209

//-----------------------------------------------------------------------------
void vtkScalarsToColorsItem::OnScalarsToColorsModified(vtkObject* caller,
                                                       unsigned long eid,
                                                       void *clientdata,
                                                       void* calldata)
{
  vtkScalarsToColorsItem* self =
    reinterpret_cast<vtkScalarsToColorsItem*>(clientdata);
  self->ScalarsToColorsModified(caller, eid, calldata);
}

//-----------------------------------------------------------------------------
210 211 212
void vtkScalarsToColorsItem::ScalarsToColorsModified(vtkObject* vtkNotUsed(object),
                                                     unsigned long vtkNotUsed(eid),
                                                     void* vtkNotUsed(calldata))
213 214 215
{
  this->Modified();
}