forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtkmHistogram.cxx
175 lines (149 loc) · 6.14 KB
/
vtkmHistogram.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkElevationFilter.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 "vtkmHistogram.h"
#include "vtkCellData.h"
#include "vtkDataSet.h"
#include "vtkDoubleArray.h"
#include "vtkFieldData.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkPointData.h"
#include "vtkTable.h"
#include "vtkUnstructuredGrid.h"
#include "vtkmlib/ArrayConverters.h"
#include "vtkmlib/DataSetConverters.h"
#include "vtkmlib/Storage.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkSmartPointer.h"
#include "vtkmFilterPolicy.h"
#include <vtkm/filter/Histogram.h>
#include <vtkm/cont/ArrayRangeCompute.hxx>
vtkStandardNewMacro(vtkmHistogram)
//------------------------------------------------------------------------------
vtkmHistogram::vtkmHistogram()
{
this->CustomBinRange[0] = 0;
this->CustomBinRange[0] = 100;
this->UseCustomBinRanges = false;
this->CenterBinsAroundMinAndMax = false;
this->NumberOfBins = 10;
}
//------------------------------------------------------------------------------
vtkmHistogram::~vtkmHistogram()
{
}
//-----------------------------------------------------------------------------
int vtkmHistogram::FillInputPortInformation(int port, vtkInformation* info)
{
this->Superclass::FillInputPortInformation(port, info);
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkDataObject");
return 1;
}
//------------------------------------------------------------------------------
int vtkmHistogram::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
vtkDataSet* input =
vtkDataSet::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkTable* output = vtkTable::GetData(outputVector, 0);
output->Initialize();
// These are the mid-points for each of the bins
vtkSmartPointer<vtkDoubleArray> binExtents = vtkSmartPointer<vtkDoubleArray>::New();
binExtents->SetNumberOfComponents(1);
binExtents->SetNumberOfTuples(static_cast<vtkIdType>(this->NumberOfBins));
binExtents->SetName("bin_extents");
binExtents->FillComponent(0, 0.0);
// Grab the input array to process to determine the field we want to apply histogram
int association = this->GetInputArrayAssociation(0, inputVector);
auto fieldArray = this->GetInputArrayToProcess(0, inputVector);
if ((association != vtkDataObject::FIELD_ASSOCIATION_POINTS &&
association != vtkDataObject::FIELD_ASSOCIATION_CELLS) ||
fieldArray == nullptr ||
fieldArray->GetName() == nullptr || fieldArray->GetName()[0] == '\0')
{
vtkErrorMacro(<< "Invalid field: Requires a point or cell field with a valid name.");
return 0;
}
const char* fieldName = fieldArray->GetName();
try
{
vtkm::cont::DataSet in = tovtkm::Convert(input);
auto field = tovtkm::Convert(fieldArray, association);
in.AddField(field);
vtkmInputFilterPolicy policy;
vtkm::filter::Histogram filter;
filter.SetNumberOfBins(static_cast<vtkm::Id>(this->NumberOfBins));
filter.SetActiveField(fieldName, field.GetAssociation());
if (this->UseCustomBinRanges)
{
if (this->CustomBinRange[0] > this->CustomBinRange[1])
{
vtkWarningMacro("Custom bin range adjusted to keep min <= max value");
double min = this->CustomBinRange[1];
double max = this->CustomBinRange[0];
this->CustomBinRange[0] = min;
this->CustomBinRange[1] = max;
}
filter.SetRange(vtkm::Range(this->CustomBinRange[0], this->CustomBinRange[1]));
}
auto result = filter.Execute(in, policy);
this->BinDelta = filter.GetBinDelta();
this->ComputedRange[0] = filter.GetComputedRange().Min;
this->ComputedRange[1] = filter.GetComputedRange().Max;
// Convert the result back
vtkDataArray* resultingArray =
fromvtkm::Convert(result.GetField("histogram"));
resultingArray->SetName("bin_values");
if (resultingArray == nullptr)
{
vtkErrorMacro(<<"Unable to convert result array from VTK-m to VTK");
return 0;
}
this->FillBinExtents(binExtents);
output->GetRowData()->AddArray(binExtents);
output->GetRowData()->AddArray(resultingArray);
resultingArray->FastDelete();
}
catch (const vtkm::cont::Error& e)
{
vtkErrorMacro(<< "VTK-m error: " << e.GetMessage());
return 0;
}
return 1;
}
//------------------------------------------------------------------------------
void vtkmHistogram::PrintSelf(std::ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "NumberOfBins: " << NumberOfBins << "\n";
os << indent << "UseCustomBinRanges: " <<UseCustomBinRanges << "\n";
os << indent << "CenterBinsAroundMinAndMax: " << CenterBinsAroundMinAndMax << "\n";
os << indent << "CustomBinRange: " <<CustomBinRange[0] <<
", " << CustomBinRange[1] << "\n";
}
//------------------------------------------------------------------------------
void vtkmHistogram::FillBinExtents(vtkDoubleArray* binExtents)
{
binExtents->SetNumberOfComponents(1);
binExtents->SetNumberOfTuples(static_cast<vtkIdType>(this->NumberOfBins));
double binDelta = this->CenterBinsAroundMinAndMax ?
((this->ComputedRange[1] - this->ComputedRange[0]) / (this->NumberOfBins -1))
: this->BinDelta;
double halfBinDelta = binDelta / 2.0;
for (vtkIdType i = 0; i < static_cast<vtkIdType>(this->NumberOfBins); i++)
{
binExtents->SetValue(i, this->ComputedRange[0] + (i*binDelta) +
(this->CenterBinsAroundMinAndMax ? 0.0 : halfBinDelta));
}
}