vtkScalarsToColorsItem.cxx 5.64 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
23
24
#include "vtkImageData.h"
#include "vtkObjectFactory.h"
#include "vtkPen.h"
25
#include "vtkPoints2D.h"
26
#include "vtkScalarsToColorsItem.h"
27
28
#include "vtkSmartPointer.h"
#include "vtkTransform2D.h"
29
30
31
32
33
34

#include <cassert>

//-----------------------------------------------------------------------------
vtkScalarsToColorsItem::vtkScalarsToColorsItem()
{
35
36
37
38
  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);
39

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

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

  this->MaskAboveCurve = false;
52
53
54

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

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

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

89
90
//-----------------------------------------------------------------------------
void vtkScalarsToColorsItem::GetBounds(double bounds[4])
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
{
  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])
106
{
107
108
109
110
  bounds[0] = 0.;
  bounds[1] = 1.;
  bounds[2] = 0.;
  bounds[3] = 1.;
111
112
}

113
114
115
//-----------------------------------------------------------------------------
bool vtkScalarsToColorsItem::Paint(vtkContext2D* painter)
{
116
  this->TextureWidth = this->GetScene()->GetViewWidth();
117
118
119
120
121
  if (this->Texture == 0 ||
      this->Texture->GetMTime() < this->GetMTime())
    {
    this->ComputeTexture();
    }
122
123
124
125
  if (this->Texture == 0)
    {
    return false;
    }
126
127
128
  vtkSmartPointer<vtkPen> transparentPen = vtkSmartPointer<vtkPen>::New();
  transparentPen->SetLineType(vtkPen::NO_PEN);
  painter->ApplyPen(transparentPen);
129
  painter->GetBrush()->SetColorF(0., 0.,0.,1.);
130
131
132
133
134
  painter->GetBrush()->SetColorF(1., 1., 1., 1.);
  painter->GetBrush()->SetTexture(this->Texture);
  painter->GetBrush()->SetTextureProperties(
    (this->Interpolate ? vtkBrush::Nearest : vtkBrush::Linear) |
    vtkBrush::Stretch);
135
  const int size = this->Shape->GetNumberOfPoints();
136
  if (!this->MaskAboveCurve || size < 2)
137
    {
138
139
140
141
142
143
    double bounds[4];
    this->GetBounds(bounds);
    painter->DrawQuad(bounds[0], bounds[2],
                      bounds[0], bounds[3],
                      bounds[1], bounds[3],
                      bounds[1], bounds[2]);
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    }
  else
    {
    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);
      trapezoids->SetPoint(++j, point[0], 0.);
      trapezoids->SetPoint(++j, point);
      }
    painter->DrawQuadStrip(trapezoids);
    trapezoids->Delete();
    }
160
161
162
163
164
165
166

  if (this->PolyLinePen->GetLineType() != vtkPen::NO_PEN)
    {
    painter->ApplyPen(this->PolyLinePen);
    painter->DrawPoly(this->Shape);
    }

167
  return true;
168
}
169
170
171
172
173
174
175
176
177
178
179
180
181

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

//-----------------------------------------------------------------------------
182
183
184
void vtkScalarsToColorsItem::ScalarsToColorsModified(vtkObject* vtkNotUsed(object),
                                                     unsigned long vtkNotUsed(eid),
                                                     void* vtkNotUsed(calldata))
185
186
187
{
  this->Modified();
}