Skip to content
Snippets Groups Projects
Commit ec9bb829 authored by Robert Maynard's avatar Robert Maynard
Browse files

Expose VTK-m ImageConnectivity Filter

parent 8a302e80
No related branches found
No related tags found
No related merge requests found
......@@ -50,6 +50,7 @@ set(impl_classes
vtkmExtractVOI
vtkmGradient
vtkmHistogram
vtkmImageConnectivity
vtkmLevelOfDetail
vtkmNDHistogram
vtkmPointElevation
......
//=============================================================================
//
// 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 "vtkmImageConnectivity.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkImageData.h"
#include "vtkmlib/ArrayConverters.h"
#include "vtkmlib/DataSetConverters.h"
#include "vtkmlib/PolyDataConverter.h"
#include "vtkmlib/Storage.h"
#include "vtkmFilterPolicy.h"
#include <vtkm/filter/ImageConnectivity.h>
vtkStandardNewMacro(vtkmImageConnectivity)
//------------------------------------------------------------------------------
vtkmImageConnectivity::vtkmImageConnectivity()
{
}
//------------------------------------------------------------------------------
vtkmImageConnectivity::~vtkmImageConnectivity()
{
}
//------------------------------------------------------------------------------
void vtkmImageConnectivity::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//------------------------------------------------------------------------------
int vtkmImageConnectivity::RequestData(vtkInformation *,
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
vtkInformation *outInfo = outputVector->GetInformationObject(0);
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
vtkImageData* output = static_cast<vtkImageData *>(
outInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkImageData* input = static_cast<vtkImageData *>(
inInfo->Get(vtkDataObject::DATA_OBJECT()));
// Find the scalar array:
int association = this->GetInputArrayAssociation(0, inputVector);
vtkDataArray *inputArray = this->GetInputArrayToProcess(0, inputVector);
if (association != vtkDataObject::FIELD_ASSOCIATION_POINTS ||
inputArray == nullptr || inputArray->GetName() == nullptr ||
inputArray->GetName()[0] == '\0')
{
vtkErrorMacro("Invalid scalar array; array missing or not a point array.");
return 0;
}
try
{
vtkm::filter::ImageConnectivity filter;
filter.SetActiveField(inputArray->GetName(), vtkm::cont::Field::Association::POINTS);
//the field should be named 'RegionId'
filter.SetOutputFieldName("RegionId");
// explicitly convert just the field we need
auto inData = tovtkm::Convert(input, tovtkm::FieldsFlag::None);
auto inField = tovtkm::Convert(inputArray, association);
inData.AddField(inField);
// don't pass this field
filter.SetFieldsToPass(
vtkm::filter::FieldSelection(vtkm::filter::FieldSelection::MODE_NONE));
vtkm::cont::DataSet result;
vtkmInputFilterPolicy policy;
result = filter.Execute(inData, policy);
//Make sure the output has all the fields / etc that the input has
output->ShallowCopy(input);
// convert back the regionId field to VTK
if (!fromvtkm::ConvertArrays(result, output))
{
vtkWarningMacro(<< "Unable to convert VTKm DataSet back to VTK.\n"
<< "Falling back to serial implementation.");
return 0;
}
}
catch (const vtkm::cont::Error& e)
{
vtkErrorMacro(<< "VTK-m error: " << e.GetMessage());
return 0;
}
return 1;
}
//=============================================================================
//
// 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 vtkmImageConnectivity
* @brief Label regions inside an image by connectivity
*
* vtkmImageConnectivity will identify connected regions within an
* image and label them.
* The filter finds groups of points that have the same field value and are
* connected together through their topology. Any point is considered to be
* connected to its Moore neighborhood:
* - 8 neighboring points for 2D
* - 27 neighboring points for 3D
*
* The active field passed to the filter must be associated with the points.
* The result of the filter is a point field of type vtkIdType.
* Each entry in the point field will be a number that identifies to which
* region it belongs. By default, this output point field is named “component”.
*
* @sa
* vtkConnectivityFilter, vtkImageConnectivityFilter
*/
#ifndef vtkmImageConnectivity_h
#define vtkmImageConnectivity_h
#include "vtkImageAlgorithm.h"
#include "vtkAcceleratorsVTKmModule.h" //required for correct implementation
class VTKACCELERATORSVTKM_EXPORT vtkmImageConnectivity :
public vtkImageAlgorithm
{
public:
vtkTypeMacro(vtkmImageConnectivity,vtkImageAlgorithm)
void PrintSelf(ostream& os, vtkIndent indent) override;
static vtkmImageConnectivity* New();
protected:
vtkmImageConnectivity();
~vtkmImageConnectivity();
virtual int RequestData(vtkInformation*, vtkInformationVector**,
vtkInformationVector*) override;
private:
vtkmImageConnectivity(const vtkmImageConnectivity&) = delete;
void operator=(const vtkmImageConnectivity&) = delete;
};
#endif // vtkmImageConnectivity_h
// VTK-HeaderTest-Exclude: vtkmImageConnectivity.h
......@@ -87,7 +87,7 @@
* of the filter.
*
* @sa
* vtkConnectivityFilter, vtkPolyDataConnectivityFilter
* vtkConnectivityFilter, vtkPolyDataConnectivityFilter, vtkmImageConnectivity
*/
#ifndef vtkImageConnectivityFilter_h
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment