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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/*=========================================================================
Program: ParaView
Module: vtkPVHistogramChartRepresentation.cxx
Copyright (c) Kitware, Inc.
All rights reserved.
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html 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 "vtkPVHistogramChartRepresentation.h"
#include "vtkAnnotationLink.h"
#include "vtkAxis.h"
#include "vtkChartXY.h"
#include "vtkCommand.h"
#include "vtkCompositeDataIterator.h"
#include "vtkCompositeDataSet.h"
#include "vtkContextView.h"
#include "vtkDataArray.h"
#include "vtkDoubleArray.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkNew.h"
#include "vtkPExtractHistogram.h"
#include "vtkPVContextView.h"
#include "vtkSelection.h"
#include "vtkSelectionNode.h"
#include "vtkTable.h"
#include <cmath>
static const char* BIN_EXTENTS = "bin_extents";
static const char* BIN_VALUES = "bin_values";
namespace
{
template <class T>
T vtkRound(double value)
{
return static_cast<T>(std::floor(value + 0.5));
}
}
vtkStandardNewMacro(vtkPVHistogramChartRepresentation);
//----------------------------------------------------------------------------
vtkPVHistogramChartRepresentation::vtkPVHistogramChartRepresentation()
{
this->ExtractHistogram = vtkPExtractHistogram::New();
this->SetChartTypeToBar();
this->SetUseIndexForXAxis(false);
this->SetXAxisSeriesName(BIN_EXTENTS);
this->SetSeriesVisibility(BIN_VALUES, true);
this->SetFieldAssociation(vtkDataObject::FIELD_ASSOCIATION_ROWS);
this->SetHistogramColor(0, 0, 255);
this->AttributeType = vtkDataObject::POINT;
}
//----------------------------------------------------------------------------
vtkPVHistogramChartRepresentation::~vtkPVHistogramChartRepresentation()
{
if (this->ExtractHistogram)
{
this->ExtractHistogram->Delete();
}
}
//----------------------------------------------------------------------------
void vtkPVHistogramChartRepresentation::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
void vtkPVHistogramChartRepresentation::SetBinCount(int bins)
{
if (this->ExtractHistogram->GetBinCount() != bins)
{
this->ExtractHistogram->SetBinCount(bins);
this->MarkModified();
}
}
//----------------------------------------------------------------------------
int vtkPVHistogramChartRepresentation::GetBinCount()
{
return this->ExtractHistogram->GetBinCount();
}
//----------------------------------------------------------------------------
void vtkPVHistogramChartRepresentation::SetUseCustomBinRanges(bool b)
{
if (this->ExtractHistogram->GetUseCustomBinRanges() != b)
{
this->ExtractHistogram->SetUseCustomBinRanges(b);
this->MarkModified();
}
}
//----------------------------------------------------------------------------
bool vtkPVHistogramChartRepresentation::GetUseCustomBinRanges()
{
return this->ExtractHistogram->GetUseCustomBinRanges();
}
//----------------------------------------------------------------------------
void vtkPVHistogramChartRepresentation::SetCustomBinRanges(double min, double max)
{
double curRanges[2];
this->ExtractHistogram->GetCustomBinRanges(curRanges);
if (curRanges[0] != min || curRanges[1] != max)
{
this->ExtractHistogram->SetCustomBinRanges(min, max);
this->MarkModified();
}
}
//----------------------------------------------------------------------------
double* vtkPVHistogramChartRepresentation::GetCustomBinRanges()
{
return this->ExtractHistogram->GetCustomBinRanges();
}
//----------------------------------------------------------------------------
void vtkPVHistogramChartRepresentation::SetComponent(int c)
{
if (this->ExtractHistogram->GetComponent() != c)
{
this->ExtractHistogram->SetComponent(c);
this->MarkModified();
}
}
//----------------------------------------------------------------------------
int vtkPVHistogramChartRepresentation::GetComponent()
{
return this->ExtractHistogram->GetComponent();
}
//----------------------------------------------------------------------------
void vtkPVHistogramChartRepresentation::SetHistogramColor(double r, double g, double b)
{
this->SetColor(BIN_VALUES, r, g, b);
}
//----------------------------------------------------------------------------
void vtkPVHistogramChartRepresentation::SetUseColorMapping(bool colorMapping)
{
this->vtkXYChartRepresentation::SetUseColorMapping(BIN_VALUES, colorMapping);
}
//----------------------------------------------------------------------------
void vtkPVHistogramChartRepresentation::SetLookupTable(vtkScalarsToColors* lut)
{
this->vtkXYChartRepresentation::SetLookupTable(BIN_VALUES, lut);
}
//----------------------------------------------------------------------------
void vtkPVHistogramChartRepresentation::SetHistogramLineStyle(int val)
{
this->SetLineStyle(BIN_VALUES, val);
}
//----------------------------------------------------------------------------
vtkDataObject* vtkPVHistogramChartRepresentation::TransformInputData(
vtkInformationVector** vtkNotUsed(inputVector), vtkDataObject* data)
{
// NOTE: This method gets called in RequestData() and only on
// the server-side, so avoid doing anything that modifies the MTime.
if (this->ArrayName.empty())
{
return NULL;
}
this->ExtractHistogram->SetInputArrayToProcess(0, 0, 0,
this->AttributeType, this->ArrayName.c_str());
this->ExtractHistogram->CalculateAveragesOff();
this->ExtractHistogram->SetInputData(data);
this->ExtractHistogram->Update();
return this->ExtractHistogram->GetOutputDataObject(0);
}
//----------------------------------------------------------------------------
void vtkPVHistogramChartRepresentation::PrepareForRendering()
{
this->Superclass::PrepareForRendering();
vtkChartXY* chart = this->GetChart();
chart->SetBarWidthFraction(1.0);
vtkAxis* axis = chart->GetAxis(vtkAxis::LEFT);
axis->SetMinimum(0);
axis->SetMinimumLimit(0);
}
//----------------------------------------------------------------------------
bool vtkPVHistogramChartRepresentation::MapSelectionToInput(vtkSelection* sel)
{
assert(sel != NULL);
// Now we do the magic: convert chart row selection to threshold selection
vtkNew<vtkDoubleArray> selRanges;
selRanges->SetName(this->ArrayName.c_str());
selRanges->SetNumberOfComponents(1);
vtkTable* table = this->GetLocalOutput();
vtkDoubleArray* binExtents =
vtkDoubleArray::SafeDownCast(table->GetColumnByName(BIN_EXTENTS));
double delta = 1.;
if (binExtents->GetNumberOfTuples() >= 2)
{
delta = (binExtents->GetValue(1) - binExtents->GetValue(0)) * 0.5;
}
vtkIdType nbNodes = sel->GetNumberOfNodes();
for (vtkIdType i = 0; i < nbNodes; i++)
{
vtkSelectionNode* node = sel->GetNode(i);
vtkDataArray* selRows = vtkDataArray::SafeDownCast(node->GetSelectionList());
if (selRows)
{
vtkIdType nbRows = selRows->GetNumberOfTuples();
for (vtkIdType j = 0; j < nbRows; j++)
{
// FIXME: make this smart to collapse adjacent bins and specify a
// single range instead of multiple ones. That'll be faster to execute.
vtkIdType row = (vtkIdType)selRows->GetTuple1(j);
double binExtent = binExtents->GetValue(row);
selRanges->InsertNextTuple1(binExtent - delta);
selRanges->InsertNextTuple1(binExtent + delta);
}
}
}
// Construct threshold selection on input
vtkNew<vtkSelection> newSel;
vtkNew<vtkSelectionNode> selNode;
selNode->SetContentType(vtkSelectionNode::THRESHOLDS);
selNode->SetFieldType(
vtkSelectionNode::ConvertAttributeTypeToSelectionField(this->AttributeType));
newSel->AddNode(selNode.Get());
selNode->SetSelectionList(selRanges.Get());
sel->ShallowCopy(newSel.GetPointer());
return true;
}
//----------------------------------------------------------------------------
bool vtkPVHistogramChartRepresentation::MapSelectionToView(vtkSelection* sel)
{
assert(sel != NULL);
// For vtkPVHistogramChartRepresentation (in the ServerManagerConfiguration
// xml), we set the SelectionRepresentation up so that the original input
// selection is passed to the vtkChartSelectionRepresentation rather than the
// id-based selection. Here, we see if the sel is of type
// vtkSelection::THRESHOLDS and if so, are the thresholds applicable to the
// bins we are showing.
// build a list of vtkSelectionNode instances that are potentially relevant.
int fieldType = vtkSelectionNode::ConvertAttributeTypeToSelectionField(this->AttributeType);
std::vector<vtkSmartPointer<vtkSelectionNode> > nodes;
for (unsigned int cc=0, max=sel->GetNumberOfNodes(); cc < max; ++cc)
{
vtkSelectionNode* node = sel->GetNode(cc);
if (node &&
node->GetFieldType() == fieldType &&
node->GetContentType() == vtkSelectionNode::THRESHOLDS &&
node->GetSelectionList() != NULL &&
node->GetSelectionList()->GetName() != NULL &&
this->ArrayName == node->GetSelectionList()->GetName() &&
node->GetSelectionList()->GetNumberOfTuples() > 0)
{
// potentially applicable selection node.
nodes.push_back(node);
}
}
sel->RemoveAllNodes();
if (nodes.size() == 0)
{
return true;
}
// now, check if the thresholds are applicable to the histogram being shown.
vtkTable* table = this->GetLocalOutput();
vtkDoubleArray* binExtents = table?
vtkDoubleArray::SafeDownCast(table->GetColumnByName(BIN_EXTENTS)) : NULL;
if (binExtents == NULL)
{
// Seems like the vtkPVHistogramChartRepresentation hasn't updated yet and
// the selection is being updated before it. Shouldn't happen, but let's
// handle it.
return false;
}
double delta = 1.;
if (binExtents->GetNumberOfTuples() >= 2)
{
delta = (binExtents->GetValue(1) - binExtents->GetValue(0));
}
const double halfDelta = delta / 2.0;
double dataRange[2];
binExtents->GetRange(dataRange); // this is range of bin-midpoints.
double episilon = delta * 1e-5; // we make this a factor of the delta.
std::set<vtkIdType> selectedBins;
for (size_t cc = 0; cc < nodes.size(); cc++)
{
vtkSelectionNode* node = nodes[cc];
vtkDoubleArray* selectionList = vtkDoubleArray::SafeDownCast(node->GetSelectionList());
for (vtkIdType j=0; (j+1) < selectionList->GetNumberOfTuples(); j+=2)
{
double range[2] = { selectionList->GetValue(j), selectionList->GetValue(j+1) };
// since range is bin-range, convert that to bin-mid-points based range.
range[0] += halfDelta;
range[1] -= halfDelta;
// now get the bin index for this range.
vtkIdType index[2];
index[0] = vtkRound<vtkIdType>((range[0] - dataRange[0])/delta);
index[1] = vtkRound<vtkIdType>((range[1] - dataRange[0])/delta);
// now only accept the range if it nearly matches the bins
if (index[0] >=0 &&
index[0] < binExtents->GetNumberOfTuples() &&
std::abs(range[0]-binExtents->GetValue(index[0])) < episilon &&
index[1] >= 0 &&
index[1] < binExtents->GetNumberOfTuples() &&
std::abs(range[1]-binExtents->GetValue(index[1])) < episilon)
{
for (vtkIdType i=index[0]; i <= index[1]; ++i)
{
selectedBins.insert(i);
}
}
}
}
vtkNew<vtkSelectionNode> node;
node->SetContentType(vtkSelectionNode::INDICES);
node->SetFieldType(vtkSelectionNode::POINT);
vtkNew<vtkIdTypeArray> convertedSelectionList;
convertedSelectionList->SetNumberOfTuples(static_cast<vtkIdType>(selectedBins.size()));
if (selectedBins.size())
{
std::copy(selectedBins.begin(), selectedBins.end(), convertedSelectionList->GetPointer(0));
}
node->SetSelectionList(convertedSelectionList.GetPointer());
sel->AddNode(node.GetPointer());
return true;
}
//----------------------------------------------------------------------------
void vtkPVHistogramChartRepresentation::SetInputArrayToProcess(int idx,
int port, int connection,
int fieldAssociation,
const char *name)
{
this->Superclass::SetInputArrayToProcess(
idx, port, connection, fieldAssociation, name);
if (this->AttributeType != fieldAssociation)
{
this->AttributeType = fieldAssociation;
this->MarkModified();
}
std::string arrayName(name? name : "");
if (this->ArrayName != name)
{
this->ArrayName = name;
this->SetLabel(BIN_VALUES, this->ArrayName.c_str());
this->MarkModified();
}
}
|