Commit 813ba231 authored by Sujin Philip's avatar Sujin Philip

Add vtkmExternalFaces Filter

parent 81a718e4
......@@ -34,6 +34,7 @@ set(lib_srcs
set(headers
vtkmClip.h
vtkmContour.h
vtkmExternalFaces.h
vtkmThreshold.h
vtkmLevelOfDetail.h
vtkmCellAverage.h
......@@ -44,6 +45,7 @@ set(headers
set(cpu_accelerator_srcs
vtkmClip.cxx
vtkmContour.cxx
vtkmExternalFaces.cxx
vtkmThreshold.cxx
vtkmLevelOfDetail.cxx
vtkmCellAverage.cxx
......@@ -58,6 +60,7 @@ set(cpu_accelerator_srcs
set(cuda_accelerator_srcs
vtkmClip.cu
vtkmContour.cu
vtkmExternalFaces.cu
vtkmThreshold.cu
vtkmLevelOfDetail.cu
vtkmCellAverage.cu
......
......@@ -5,6 +5,7 @@ include_directories(${VTKm_INCLUDE_DIRS})
vtk_add_test_cxx(${vtk-module}CxxTests tests
TestVTKMClip.cxx
# TestVTKMGradientAndVorticity.cxx,NO_VALID
TestVTKMExternalFaces.cxx
TestVTKMLevelOfDetail.cxx
TestVTKMMarchingCubes.cxx
TestVTKMThreshold.cxx
......
//=============================================================================
//
// 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 "vtkmExternalFaces.h"
#include "vtkActor.h"
#include "vtkCell.h"
#include "vtkCylinder.h"
#include "vtkNew.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkRegressionTestImage.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRTAnalyticSource.h"
#include "vtkSphere.h"
#include "vtkTableBasedClipDataSet.h"
#include "vtkTransform.h"
#include "vtkTransformFilter.h"
#include "vtkUnstructuredGrid.h"
#include "vtkPointData.h"
#include "vtkDataArray.h"
namespace {
bool Convert2DUnstructuredGridToPolyData(vtkUnstructuredGrid *in,
vtkPolyData *out)
{
out->Allocate();
out->SetPoints(in->GetPoints());
vtkIdType numCells = in->GetNumberOfCells();
for (vtkIdType i = 0; i < numCells; ++i)
{
vtkCell *cell = in->GetCell(i);
if (cell->GetCellType() != VTK_TRIANGLE && cell->GetCellType() != VTK_QUAD)
{
std::cout << "Error: Unexpected cell type: " << cell->GetCellType()
<< "\n";
return false;
}
out->InsertNextCell(cell->GetCellType(), cell->GetPointIds());
}
out->GetPointData()->PassData(in->GetPointData());
return true;
}
} // anonymous namespace
int TestVTKMExternalFaces(int argc, char* argv[])
{
// create pipeline
vtkNew<vtkRTAnalyticSource> wavelet;
wavelet->SetWholeExtent(-16, 16, -16, 16, -16, 16);
wavelet->SetCenter(0, 0, 0);
vtkNew<vtkCylinder> cylinder;
cylinder->SetCenter(0, 0, 0);
cylinder->SetRadius(15);
cylinder->SetAxis(0, 1, 0);
vtkNew<vtkTableBasedClipDataSet> clipCyl;
clipCyl->SetInputConnection(wavelet->GetOutputPort());
clipCyl->SetClipFunction(cylinder.GetPointer());
clipCyl->InsideOutOn();
vtkNew<vtkSphere> sphere;
sphere->SetCenter(0, 0, 4);
sphere->SetRadius(12);
vtkNew<vtkTableBasedClipDataSet> clipSphr;
clipSphr->SetInputConnection(clipCyl->GetOutputPort());
clipSphr->SetClipFunction(sphere.GetPointer());
vtkNew<vtkTransform> transform;
transform->RotateZ(45);
vtkNew<vtkTransformFilter> transFilter;
transFilter->SetInputConnection(clipSphr->GetOutputPort());
transFilter->SetTransform(transform.GetPointer());
vtkNew<vtkmExternalFaces> externalFaces;
externalFaces->SetInputConnection(transFilter->GetOutputPort());
// execute pipeline
externalFaces->Update();
vtkIdType numInputPoints = externalFaces->GetOutput()->GetNumberOfPoints();
externalFaces->CompactPointsOn();
externalFaces->Update();
if (externalFaces->GetOutput()->GetNumberOfPoints() >= numInputPoints)
{
std::cout << "Expecting the number of points in the output to be less "
<< "than the input ("
<< externalFaces->GetOutput()->GetNumberOfPoints() << ">="
<< numInputPoints << ")\n";
return 1;
}
vtkNew<vtkPolyData> polydata;
if (!Convert2DUnstructuredGridToPolyData(externalFaces->GetOutput(),
polydata.GetPointer()))
{
std::cout << "Error converting result to polydata\n";
return 1;
}
// render results
double scalarRange[2];
polydata->GetPointData()->GetArray("RTData")->GetRange(scalarRange);
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputData(polydata.GetPointer());
mapper->SetScalarRange(scalarRange);
vtkNew<vtkActor> actor;
actor->SetMapper(mapper.GetPointer());
vtkNew<vtkRenderer> renderer;
renderer->AddActor(actor.GetPointer());
renderer->ResetCamera();
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(renderer.GetPointer());
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin.GetPointer());
iren->Initialize();
renWin->Render();
int retVal = vtkRegressionTestImage(renWin.GetPointer());
if (retVal == vtkRegressionTester::DO_INTERACTOR)
{
iren->Start();
}
return !retVal;
}
......@@ -15,5 +15,6 @@ vtk_module(vtkAcceleratorsVTKm
vtkIOXML
vtkImagingHybrid
vtkImagingSources
vtkInteractionStyle
EXCLUDE_FROM_ALL
)
//=============================================================================
//
// 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 "vtkmExternalFaces.cxx"
//=============================================================================
//
// 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 "vtkmExternalFaces.h"
#include "vtkCellData.h"
#include "vtkDemandDrivenPipeline.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkUnsignedCharArray.h"
#include "vtkUnstructuredGrid.h"
#include "vtkmlib/ArrayConverters.h"
#include "vtkmlib/DataSetConverters.h"
#include "vtkmlib/Storage.h"
#include "vtkmlib/CellSetConverters.h"
#include "vtkmlib/UnstructuredGridConverter.h"
#include "vtkmCellSetExplicit.h"
#include "vtkmCellSetSingleType.h"
#include "vtkmFilterPolicy.h"
#include <vtkm/filter/ExternalFaces.h>
vtkStandardNewMacro(vtkmExternalFaces)
//------------------------------------------------------------------------------
vtkmExternalFaces::vtkmExternalFaces()
: CompactPoints(false)
{
this->SetNumberOfInputPorts(1);
this->SetNumberOfOutputPorts(1);
}
//------------------------------------------------------------------------------
vtkmExternalFaces::~vtkmExternalFaces()
{
}
//------------------------------------------------------------------------------
void vtkmExternalFaces::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//------------------------------------------------------------------------------
void vtkmExternalFaces::SetInputData(vtkUnstructuredGrid *ds)
{
this->SetInputDataObject(0, ds);
}
//------------------------------------------------------------------------------
vtkUnstructuredGrid* vtkmExternalFaces::GetOutput()
{
return vtkUnstructuredGrid::SafeDownCast(this->GetOutputDataObject(0));
}
//------------------------------------------------------------------------------
int vtkmExternalFaces::FillInputPortInformation(int, vtkInformation* info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkUnstructuredGrid");
return 1;
}
//------------------------------------------------------------------------------
int vtkmExternalFaces::FillOutputPortInformation(int vtkNotUsed(port),
vtkInformation *info)
{
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkUnstructuredGrid");
return 1;
}
//------------------------------------------------------------------------------
int vtkmExternalFaces::ProcessRequest(vtkInformation* request,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
// generate the data
if(request->Has(vtkDemandDrivenPipeline::REQUEST_DATA()))
{
return this->RequestData(request, inputVector, outputVector);
}
return this->Superclass::ProcessRequest(request, inputVector, outputVector);
}
//------------------------------------------------------------------------------
int vtkmExternalFaces::RequestData(vtkInformation* request,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
vtkInformation* inInfo = inputVector[0]->GetInformationObject(0);
vtkInformation* outInfo = outputVector->GetInformationObject(0);
vtkUnstructuredGrid* input =
vtkUnstructuredGrid::SafeDownCast(inInfo->Get(vtkDataObject::DATA_OBJECT()));
vtkUnstructuredGrid* output =
vtkUnstructuredGrid::SafeDownCast(outInfo->Get(vtkDataObject::DATA_OBJECT()));
// convert the input dataset to a vtkm::cont::DataSet
vtkm::cont::DataSet in = tovtkm::Convert(input);
if (in.GetNumberOfCoordinateSystems() <= 0 || in.GetNumberOfCellSets() <= 0)
{
vtkErrorMacro(<< "Could not convert vtk dataset to vtkm dataset");
return 0;
}
vtkmInputFilterPolicy policy;
// apply the filter
vtkm::filter::ExternalFaces filter;
filter.SetCompactPoints(this->CompactPoints);
vtkm::filter::ResultDataSet result = filter.Execute(in, policy);
if (!result.IsValid())
{
vtkErrorMacro(<< "VTKm ExternalFaces algorithm failed to run");
return 0;
}
if (this->CompactPoints)
{
// map each point array
vtkPointData* pd = input->GetPointData();
for (vtkIdType i = 0; i < pd->GetNumberOfArrays(); i++)
{
vtkDataArray* array = pd->GetArray(i);
if (array == NULL)
{
continue;
}
vtkm::cont::Field pfield =
tovtkm::Convert(array, vtkDataObject::FIELD_ASSOCIATION_POINTS);
try
{
filter.MapFieldOntoOutput(result, pfield, policy);
}
catch (vtkm::cont::Error&)
{
vtkWarningMacro(<< "Unable to use VTKm to convert field ("
<< array->GetName() << ") to the output");
}
}
// convert back to vtkDataSet (vtkUnstructuredGrid)
if (!fromvtkm::Convert(result.GetDataSet(), output, input))
{
vtkErrorMacro(<< "Unable to convert VTKm DataSet back to VTK");
return 0;
}
}
else
{
// convert just the cellset from vtkm to vtk
vtkNew<vtkCellArray> cells;
vtkNew<vtkUnsignedCharArray> types;
vtkNew<vtkIdTypeArray> locations;
vtkm::cont::DynamicCellSet cellSet = result.GetDataSet().GetCellSet();
if (!fromvtkm::Convert(cellSet, cells.GetPointer(), types.GetPointer(),
locations.GetPointer()))
{
vtkErrorMacro(<< "Unable to convert VTKm DataSet back to VTK");
return 0;
}
// copy points from input to output
output->SetPoints(input->GetPoints());
// add the new cellset to output
output->SetCells(types.GetPointer(), locations.GetPointer(),
cells.GetPointer());
// copy the point data from input to output
output->GetPointData()->PassData(input->GetPointData());
}
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.
//
//=============================================================================
#ifndef vtkmExternalFaces_h
#define vtkmExternalFaces_h
#include "vtkAlgorithm.h"
#include "vtkAcceleratorsVTKmModule.h" //required for correct implementation
class vtkUnstructuredGrid;
class VTKACCELERATORSVTKM_EXPORT vtkmExternalFaces : public vtkAlgorithm
{
public:
vtkTypeMacro(vtkmExternalFaces, vtkAlgorithm)
void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE;
static vtkmExternalFaces* New();
/**
* Set the input DataSet
*/
void SetInputData(vtkUnstructuredGrid *ds);
/**
* Get the resulr DataSet
*/
vtkUnstructuredGrid* GetOutput();
//@{
/**
* Get/Set if the points from the input that are unused in the output should
* be removed. This will take extra time but the result dataset may use
* less memory. Off by default.
*/
vtkSetMacro(CompactPoints, bool);
vtkGetMacro(CompactPoints, bool);
vtkBooleanMacro(CompactPoints, bool);
//@}
protected:
vtkmExternalFaces();
~vtkmExternalFaces();
int FillInputPortInformation(int, vtkInformation *) VTK_OVERRIDE;
int FillOutputPortInformation(int, vtkInformation *) VTK_OVERRIDE;
int ProcessRequest(vtkInformation*, vtkInformationVector**,
vtkInformationVector*) VTK_OVERRIDE;
virtual int RequestData(vtkInformation *, vtkInformationVector **,
vtkInformationVector *);
bool CompactPoints;
private:
vtkmExternalFaces(const vtkmExternalFaces&) VTK_DELETE_FUNCTION;
void operator=(const vtkmExternalFaces&) VTK_DELETE_FUNCTION;
};
#endif // vtkmExternalFaces_h
// VTK-HeaderTest-Exclude: vtkmExternalFaces.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