forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtkPlotHistogram2D.cxx
289 lines (252 loc) · 8.72 KB
/
vtkPlotHistogram2D.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*=========================================================================
Program: Visualization Toolkit
Module: vtk2DHistogramItem.h
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 "vtkPlotHistogram2D.h"
#include "vtkAxis.h"
#include "vtkContext2D.h"
#include "vtkDoubleArray.h"
#include "vtkImageData.h"
#include "vtkMath.h"
#include "vtkScalarsToColors.h"
#include "vtkStringArray.h"
#include "vtkObjectFactory.h"
#include <algorithm>
//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkPlotHistogram2D)
//-----------------------------------------------------------------------------
vtkPlotHistogram2D::vtkPlotHistogram2D()
{
this->TooltipDefaultLabelFormat = "%x, %y: %v";
}
//-----------------------------------------------------------------------------
vtkPlotHistogram2D::~vtkPlotHistogram2D() = default;
void vtkPlotHistogram2D::Update()
{
this->GenerateHistogram();
}
//-----------------------------------------------------------------------------
bool vtkPlotHistogram2D::Paint(vtkContext2D *painter)
{
if (this->Output)
{
if (this->Input)
{
double bounds[4];
this->GetBounds(bounds);
this->Position = vtkRectf(bounds[0], bounds[2],
bounds[1] - bounds[0], bounds[3] - bounds[2]);
}
painter->DrawImage(this->Position, this->Output);
}
return true;
}
//-----------------------------------------------------------------------------
void vtkPlotHistogram2D::SetInputData(vtkImageData *data, vtkIdType)
{
// FIXME: Store the z too, for slices.
this->Input = data;
}
//-----------------------------------------------------------------------------
vtkImageData * vtkPlotHistogram2D::GetInputImageData()
{
return this->Input;
}
//-----------------------------------------------------------------------------
void vtkPlotHistogram2D::SetTransferFunction(vtkScalarsToColors *function)
{
this->TransferFunction = function;
}
//-----------------------------------------------------------------------------
vtkScalarsToColors * vtkPlotHistogram2D::GetTransferFunction()
{
return this->TransferFunction;
}
//-----------------------------------------------------------------------------
void vtkPlotHistogram2D::GetBounds(double bounds[4])
{
if (this->Input)
{
std::copy(this->Input->GetBounds(), this->Input->GetBounds() + 4, bounds);
// Adding a spacing increment is necessary in order to draw in the context 2D correctly
double* spacing = this->Input->GetSpacing();
bounds[1] += spacing[0];
bounds[3] += spacing[1];
}
else
{
std::fill(bounds, bounds + 4, 0.);
}
}
//-----------------------------------------------------------------------------
void vtkPlotHistogram2D::SetPosition(const vtkRectf& pos)
{
this->Position = pos;
}
//-----------------------------------------------------------------------------
vtkRectf vtkPlotHistogram2D::GetPosition()
{
return this->Position;
}
//-----------------------------------------------------------------------------
vtkIdType vtkPlotHistogram2D::GetNearestPoint(const vtkVector2f& point,
const vtkVector2f& tolerance,
vtkVector2f* location,
vtkIdType* vtkNotUsed(segmentId))
{
#ifndef VTK_LEGACY_REMOVE
if (!this->LegacyRecursionFlag)
{
this->LegacyRecursionFlag = true;
vtkIdType ret = this->GetNearestPoint(point, tolerance, location);
this->LegacyRecursionFlag = false;
if (ret != -1)
{
VTK_LEGACY_REPLACED_BODY(
vtkPlotHistogram2D::GetNearestPoint(const vtkVector2f& point, const vtkVector2f& tolerance, vtkVector2f* location),
"VTK 8.3",
vtkPlotHistogram2D::GetNearestPoint(const vtkVector2f& point, const vtkVector2f& tolerance, vtkVector2f* location, vtkIdType* segmentId));
return ret;
}
}
#endif // VTK_LEGACY_REMOVE
if (!this->Input)
{
return -1;
}
(void)tolerance;
double bounds[4];
this->GetBounds(bounds);
double spacing[3];
this->Input->GetSpacing(spacing);
if (point.GetX() < bounds[0] ||
point.GetX() > bounds[1]+spacing[0] ||
point.GetY() < bounds[2] ||
point.GetY() > bounds[3]+spacing[1])
{
return -1;
}
// Can't use vtkImageData::FindPoint() / GetPoint(), as ImageData points are
// rendered as the bottom left corner of a histogram cell, not the center
int locX = vtkMath::Floor( (point.GetX() - bounds[0]) / spacing[0] );
int locY = vtkMath::Floor( (point.GetY() - bounds[2]) / spacing[1] );
int width = this->Input->GetExtent()[1] - this->Input->GetExtent()[0] + 1;
// Discretize to ImageData point values
location->SetX(locX * spacing[0] + bounds[0]);
location->SetY(locY * spacing[1] + bounds[2]);
return (locX + (locY * width));
}
//-----------------------------------------------------------------------------
vtkStdString vtkPlotHistogram2D::GetTooltipLabel(const vtkVector2d &plotPos,
vtkIdType seriesIndex,
vtkIdType)
{
// This does not call the Superclass vtkPlot::GetTooltipLabel(), since the
// format tags internally refer to different values
vtkStdString tooltipLabel;
vtkStdString &format = this->TooltipLabelFormat.empty() ?
this->TooltipDefaultLabelFormat : this->TooltipLabelFormat;
if (!this->Input)
{
return tooltipLabel;
}
double bounds[4];
this->GetBounds(bounds);
int width = this->Input->GetExtent()[1] - this->Input->GetExtent()[0] + 1;
int height = this->Input->GetExtent()[3] - this->Input->GetExtent()[2] + 1;
int pointX = seriesIndex % width + this->Input->GetExtent()[0];
int pointY = seriesIndex / width + this->Input->GetExtent()[2];
// Parse TooltipLabelFormat and build tooltipLabel
bool escapeNext = false;
for (size_t i = 0; i < format.length(); ++i)
{
if (escapeNext)
{
switch (format[i])
{
case 'x':
tooltipLabel += this->GetNumber(plotPos.GetX(), this->XAxis);
break;
case 'y':
tooltipLabel += this->GetNumber(plotPos.GetY(), this->YAxis);
break;
case 'i':
if (this->XAxis->GetTickLabels() &&
pointX >= 0 &&
pointX < this->XAxis->GetTickLabels()->GetNumberOfTuples())
{
tooltipLabel += this->XAxis->GetTickLabels()->GetValue(pointX);
}
break;
case 'j':
if (this->YAxis->GetTickLabels() &&
pointY >= 0 &&
pointY < this->YAxis->GetTickLabels()->GetNumberOfTuples())
{
tooltipLabel += this->YAxis->GetTickLabels()->GetValue(pointY);
}
break;
case 'v':
if (pointX >= 0 && pointX < width && pointY >= 0 && pointY < height)
{
tooltipLabel +=
this->GetNumber(this->Input->GetScalarComponentAsDouble(
pointX, pointY, 0, 0), nullptr);
}
break;
default: // If no match, insert the entire format tag
tooltipLabel += "%";
tooltipLabel += format[i];
break;
}
escapeNext = false;
}
else
{
if (format[i] == '%')
{
escapeNext = true;
}
else
{
tooltipLabel += format[i];
}
}
}
return tooltipLabel;
}
//-----------------------------------------------------------------------------
void vtkPlotHistogram2D::GenerateHistogram()
{
if (!this->Input)
{
return;
}
if (!this->Output)
{
this->Output = vtkSmartPointer<vtkImageData>::New();
}
this->Output->SetExtent(this->Input->GetExtent());
this->Output->AllocateScalars(VTK_UNSIGNED_CHAR, 4);
int dimension = this->Input->GetDimensions()[0] * this->Input->GetDimensions()[1];
void* const input = this->Input->GetScalarPointer();
const int inputType = this->Input->GetScalarType();
unsigned char *output =
reinterpret_cast<unsigned char*>(this->Output->GetScalarPointer());
if (this->TransferFunction)
{
this->TransferFunction->MapScalarsThroughTable2(input, output, inputType,
dimension, 1, 4);
}
}
//-----------------------------------------------------------------------------
void vtkPlotHistogram2D::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}