Commit ebd46030 authored by Haocheng LIU's avatar Haocheng LIU

Expose vtkm histogram filter

parent 803ca045
......@@ -39,6 +39,7 @@ set(headers
vtkmContour.h
vtkmExternalFaces.h
vtkmExtractVOI.h
vtkmHistogram.h
vtkmThreshold.h
vtkmLevelOfDetail.h
vtkmGradient.h
......@@ -56,6 +57,7 @@ set(cpu_accelerator_srcs
vtkmContour.cxx
vtkmExternalFaces.cxx
vtkmExtractVOI.cxx
vtkmHistogram.cxx
vtkmThreshold.cxx
vtkmLevelOfDetail.cxx
vtkmCellSetExplicit.cxx
......@@ -77,6 +79,7 @@ set(cuda_accelerator_srcs
vtkmContour.cu
vtkmExternalFaces.cu
vtkmExtractVOI.cu
vtkmHistogram.cu
vtkmThreshold.cu
vtkmLevelOfDetail.cu
vtkmAverageToCells.cu
......
......@@ -2,10 +2,11 @@ vtk_add_test_cxx(vtkAcceleratorsVtkmCxxTests tests
TestVTKMCleanGrid.cxx
TestVTKMClip.cxx
TestVTKMClipWithImplicitFunction.cxx
TestVTKMGradient.cxx,NO_VALID
TestVTKMGradientAndVorticity.cxx,NO_VALID
TestVTKMExternalFaces.cxx
TestVTKMExtractVOI.cxx
TestVTKMGradient.cxx,NO_VALID
TestVTKMGradientAndVorticity.cxx,NO_VALID
TestVTKMHistogram.cxx,NO_VALID
TestVTKMLevelOfDetail.cxx
TestVTKMMarchingCubes.cxx
TestVTKMMarchingCubes2.cxx
......
This diff is collapsed.
//=============================================================================
//
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt 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.
//
// Copyright 2012 Sandia Corporation.
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
//=============================================================================
#include "vtkmHistogram.cxx"
/*=========================================================================
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));
}
}
//=============================================================================
//
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt 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.
//
// Copyright 2012 Sandia Corporation.
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
//=============================================================================
/**
* @class vtkmHistogram
* @brief generate a histogram out of a scalar data
*
* vtkmHistogram is a filter that generates a histogram out of a scalar data.
* The histogram consists of a certain number of bins specified by the user, and
* the user can fetch the range and bin delta after completion.
*
*/
#ifndef vtkmHistogram_h
#define vtkmHistogram_h
#include "vtkAcceleratorsVTKmModule.h" //required for correct export
#include "vtkTableAlgorithm.h"
class vtkDoubleArray;
class VTKACCELERATORSVTKM_EXPORT vtkmHistogram : public vtkTableAlgorithm
{
public:
vtkTypeMacro(vtkmHistogram, vtkTableAlgorithm)
void PrintSelf(ostream& os, vtkIndent indent) override;
static vtkmHistogram* New();
//@{
/**
* Specify number of bins. Default is 10.
*/
vtkSetMacro(NumberOfBins,size_t);
vtkGetMacro(NumberOfBins,size_t);
//@}
//@{
/**
* Specify the range to use to generate the histogram. They are only used when
* UseCustomBinRanges is set to true.
*/
vtkSetVector2Macro(CustomBinRange,double);
vtkGetVector2Macro(CustomBinRange,double);
//@}
//@{
/**
* When set to true, CustomBinRanges will be used instead of using the full
* range for the selected array. By default, set to false.
*/
vtkSetMacro(UseCustomBinRanges, bool);
vtkGetMacro(UseCustomBinRanges, bool);
vtkBooleanMacro(UseCustomBinRanges, bool);
//@}
//@{
/**
* Get/Set if first and last bins must be centered around the min and max
* data. This is only used when UseCustomBinRanges is set to false.
* Default is false.
*/
vtkSetMacro(CenterBinsAroundMinAndMax, bool);
vtkGetMacro(CenterBinsAroundMinAndMax, bool);
vtkBooleanMacro(CenterBinsAroundMinAndMax, bool);
//@}
//@{
/**
* Return the range used to generate the histogram.
*/
vtkGetVectorMacro(ComputedRange,double, 2);
//@}
//@{
/**
* Return the bin delta of the computed field.
*/
vtkGetMacro(BinDelta, double);
//@}
protected:
vtkmHistogram();
~vtkmHistogram();
virtual int RequestData(vtkInformation*,
vtkInformationVector**, vtkInformationVector*) override;
int FillInputPortInformation(int port, vtkInformation* info) override;
private:
vtkmHistogram(const vtkmHistogram&) = delete;
void operator=(const vtkmHistogram&) = delete;
void FillBinExtents(vtkDoubleArray* binExtents);
size_t NumberOfBins;
double BinDelta;
double CustomBinRange[2];
bool UseCustomBinRanges;
bool CenterBinsAroundMinAndMax;
double ComputedRange[2];
};
#endif // vtkmHistogram_h
// VTK-HeaderTest-Exclude: vtkmHistogram.h
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment