From a12181f9416a93359da4184ba34ce97b418afac3 Mon Sep 17 00:00:00 2001 From: Sujin Philip Date: Wed, 14 Jun 2017 12:56:49 -0400 Subject: [PATCH] Add vtkmPolyDataNormals filter --- Accelerators/Vtkm/CMakeLists.txt | 3 + Accelerators/Vtkm/Testing/Cxx/CMakeLists.txt | 1 + .../Testing/Cxx/TestVTKMPolyDataNormals.cxx | 153 ++++++++++++++++++ .../Baseline/TestVTKMPolyDataNormals.png.md5 | 1 + Accelerators/Vtkm/vtkmFilterPolicy.h | 1 + Accelerators/Vtkm/vtkmPolyDataNormals.cu | 15 ++ Accelerators/Vtkm/vtkmPolyDataNormals.cxx | 135 ++++++++++++++++ Accelerators/Vtkm/vtkmPolyDataNormals.h | 41 +++++ 8 files changed, 350 insertions(+) create mode 100644 Accelerators/Vtkm/Testing/Cxx/TestVTKMPolyDataNormals.cxx create mode 100644 Accelerators/Vtkm/Testing/Data/Baseline/TestVTKMPolyDataNormals.png.md5 create mode 100644 Accelerators/Vtkm/vtkmPolyDataNormals.cu create mode 100644 Accelerators/Vtkm/vtkmPolyDataNormals.cxx create mode 100644 Accelerators/Vtkm/vtkmPolyDataNormals.h diff --git a/Accelerators/Vtkm/CMakeLists.txt b/Accelerators/Vtkm/CMakeLists.txt index edfb0a27f3..2164dc8a0f 100644 --- a/Accelerators/Vtkm/CMakeLists.txt +++ b/Accelerators/Vtkm/CMakeLists.txt @@ -43,6 +43,7 @@ set(headers vtkmLevelOfDetail.h vtkmAverageToCells.h vtkmGradient.h + vtkmPolyDataNormals.h ) #implementation of the algorithms for cpu accelerators @@ -60,6 +61,7 @@ set(cpu_accelerator_srcs vtkmCellSetSingleType.cxx vtkmConnectivityExec.cxx vtkmGradient.cxx + vtkmPolyDataNormals.cxx vtkmlib/Portals.cxx vtkmlib/ImplicitFunctionConverter.cxx ) @@ -79,6 +81,7 @@ set(cuda_accelerator_srcs vtkmCellSetSingleType.cu vtkmConnectivityExec.cu vtkmGradient.cu + vtkmPolyDataNormals.cu vtkmlib/Portals.cu vtkmlib/ImplicitFunctionConverter.cu ) diff --git a/Accelerators/Vtkm/Testing/Cxx/CMakeLists.txt b/Accelerators/Vtkm/Testing/Cxx/CMakeLists.txt index ac4f8d8eb5..85e7936206 100644 --- a/Accelerators/Vtkm/Testing/Cxx/CMakeLists.txt +++ b/Accelerators/Vtkm/Testing/Cxx/CMakeLists.txt @@ -12,6 +12,7 @@ vtk_add_test_cxx(${vtk-module}CxxTests tests TestVTKMLevelOfDetail.cxx TestVTKMMarchingCubes.cxx TestVTKMMarchingCubes2.cxx + TestVTKMPolyDataNormals.cxx TestVTKMThreshold.cxx TestVTKMThreshold2.cxx ) diff --git a/Accelerators/Vtkm/Testing/Cxx/TestVTKMPolyDataNormals.cxx b/Accelerators/Vtkm/Testing/Cxx/TestVTKMPolyDataNormals.cxx new file mode 100644 index 0000000000..a60e5ed6f5 --- /dev/null +++ b/Accelerators/Vtkm/Testing/Cxx/TestVTKMPolyDataNormals.cxx @@ -0,0 +1,153 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: TestVTKMExtractVOI.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 "vtkmPolyDataNormals.h" + +#include "vtkActor.h" +#include "vtkArrowSource.h" +#include "vtkCamera.h" +#include "vtkCellCenters.h" +#include "vtkCellData.h" +#include "vtkCleanPolyData.h" +#include "vtkCylinderSource.h" +#include "vtkGlyph3D.h" +#include "vtkNew.h" +#include "vtkPointData.h" +#include "vtkPolyDataMapper.h" +#include "vtkProperty.h" +#include "vtkRegressionTestImage.h" +#include "vtkRenderer.h" +#include "vtkRenderWindow.h" +#include "vtkRenderWindowInteractor.h" +#include "vtkSmartPointer.h" +#include "vtkTriangleFilter.h" + + +namespace +{ + +void MakeInputDataSet(vtkPolyData *ds) +{ + vtkNew cylinder; + cylinder->SetRadius(1.0); + cylinder->SetResolution(8); + cylinder->CappingOn(); + + vtkNew triangle; + triangle->SetInputConnection(cylinder->GetOutputPort()); + + vtkNew clean; + clean->SetInputConnection(triangle->GetOutputPort()); + + clean->Update(); + + ds->ShallowCopy(clean->GetOutput()); + ds->GetPointData()->Initialize(); + ds->GetCellData()->Initialize(); +} + +} + +int TestVTKMPolyDataNormals(int argc, char* argv[]) +{ + vtkNew input; + MakeInputDataSet(input.GetPointer()); + + vtkNew normals; + normals->SetInputData(input.GetPointer()); + normals->ComputePointNormalsOn(); + normals->ComputeCellNormalsOn(); + + + // cylinder mapper and actor + vtkNew cylinderMapper; + cylinderMapper->SetInputData(input.GetPointer()); + + vtkNew cylinderActor; + cylinderActor->SetMapper(cylinderMapper.GetPointer()); + vtkSmartPointer cylinderProperty; + cylinderProperty.TakeReference(cylinderActor->MakeProperty()); + cylinderProperty->SetRepresentationToWireframe(); + cylinderProperty->SetColor(0.3, 0.3, 0.3); + cylinderActor->SetProperty(cylinderProperty.GetPointer()); + + + vtkNew arrow; + + // point normals + vtkNew pnGlyphs; + pnGlyphs->SetInputConnection(normals->GetOutputPort()); + pnGlyphs->SetSourceConnection(arrow->GetOutputPort()); + pnGlyphs->SetScaleFactor(0.5); + pnGlyphs->OrientOn(); + pnGlyphs->SetVectorModeToUseNormal(); + + vtkNew pnMapper; + pnMapper->SetInputConnection(pnGlyphs->GetOutputPort()); + + vtkNew pnActor; + pnActor->SetMapper(pnMapper.GetPointer()); + + vtkNew pnRenderer; + pnRenderer->AddActor(cylinderActor.GetPointer()); + pnRenderer->AddActor(pnActor.GetPointer()); + pnRenderer->ResetCamera(); + pnRenderer->GetActiveCamera()->SetPosition(0.0, 4.5, 7.5); + + + // cell normals + vtkNew cells; + cells->SetInputConnection(normals->GetOutputPort()); + + vtkNew cnGlyphs; + cnGlyphs->SetInputConnection(cells->GetOutputPort()); + cnGlyphs->SetSourceConnection(arrow->GetOutputPort()); + cnGlyphs->SetScaleFactor(0.5); + cnGlyphs->OrientOn(); + cnGlyphs->SetVectorModeToUseNormal(); + + vtkNew cnMapper; + cnMapper->SetInputConnection(cnGlyphs->GetOutputPort()); + + vtkNew cnActor; + cnActor->SetMapper(cnMapper.GetPointer()); + + vtkNew cnRenderer; + cnRenderer->AddActor(cylinderActor.GetPointer()); + cnRenderer->AddActor(cnActor.GetPointer()); + cnRenderer->ResetCamera(); + cnRenderer->GetActiveCamera()->SetPosition(0.0, 8.0, 0.1); + + + // render + vtkNew renWin; + renWin->SetSize(600, 300); + pnRenderer->SetViewport(0.0, 0.0, 0.5, 1.0); + renWin->AddRenderer(pnRenderer.GetPointer()); + cnRenderer->SetViewport(0.5, 0.0, 1.0, 1.0); + renWin->AddRenderer(cnRenderer.GetPointer()); + + vtkNew iren; + iren->SetRenderWindow(renWin.GetPointer()); + iren->Initialize(); + + renWin->Render(); + int retVal = vtkRegressionTestImage(renWin.GetPointer()); + if (retVal == vtkRegressionTester::DO_INTERACTOR) + { + iren->Start(); + } + + return !retVal; +} diff --git a/Accelerators/Vtkm/Testing/Data/Baseline/TestVTKMPolyDataNormals.png.md5 b/Accelerators/Vtkm/Testing/Data/Baseline/TestVTKMPolyDataNormals.png.md5 new file mode 100644 index 0000000000..83f11d68a6 --- /dev/null +++ b/Accelerators/Vtkm/Testing/Data/Baseline/TestVTKMPolyDataNormals.png.md5 @@ -0,0 +1 @@ +27479f1dc3745f0a8cec5bf401a429cf diff --git a/Accelerators/Vtkm/vtkmFilterPolicy.h b/Accelerators/Vtkm/vtkmFilterPolicy.h index 7cd7a2aabe..e5c325e92e 100644 --- a/Accelerators/Vtkm/vtkmFilterPolicy.h +++ b/Accelerators/Vtkm/vtkmFilterPolicy.h @@ -203,6 +203,7 @@ struct CellListUnstructuredInVTK struct CellListUnstructuredOutVTK : vtkm::ListTagBase< vtkm::cont::CellSetExplicit<>, vtkm::cont::CellSetSingleType<>, + vtkm::cont::vtkmCellSetExplicitAOS, vtkm::cont::vtkmCellSetSingleType, vtkm::cont::CellSetPermutation, vtkm::cont::CellSetPermutation> { diff --git a/Accelerators/Vtkm/vtkmPolyDataNormals.cu b/Accelerators/Vtkm/vtkmPolyDataNormals.cu new file mode 100644 index 0000000000..79a807dce4 --- /dev/null +++ b/Accelerators/Vtkm/vtkmPolyDataNormals.cu @@ -0,0 +1,15 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkmPolyDataNormals.cu + + 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 "vtkmPolyDataNormals.cxx" diff --git a/Accelerators/Vtkm/vtkmPolyDataNormals.cxx b/Accelerators/Vtkm/vtkmPolyDataNormals.cxx new file mode 100644 index 0000000000..b4202a8845 --- /dev/null +++ b/Accelerators/Vtkm/vtkmPolyDataNormals.cxx @@ -0,0 +1,135 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkmPolyDataNormals.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 "vtkmPolyDataNormals.h" + +#include "vtkCellData.h" +#include "vtkInformation.h" +#include "vtkInformationVector.h" +#include "vtkPointData.h" +#include "vtkPolyData.h" +#include "vtkSmartPointer.h" + +#include "vtkmlib/ArrayConverters.h" +#include "vtkmlib/PolyDataConverter.h" +#include "vtkmlib/Storage.h" + +#include "vtkmCellSetExplicit.h" +#include "vtkmCellSetSingleType.h" +#include "vtkmFilterPolicy.h" + +#include "vtkm/filter/SurfaceNormals.h" + + +vtkStandardNewMacro(vtkmPolyDataNormals) + +//------------------------------------------------------------------------------ +void vtkmPolyDataNormals::PrintSelf(ostream& os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os, indent); +} + +//------------------------------------------------------------------------------ +vtkmPolyDataNormals::vtkmPolyDataNormals() +{ + // change defaults from parent + this->Splitting = 0; + this->Consistency = 0; + this->FlipNormals = 0; + this->ComputePointNormals = 1; + this->ComputeCellNormals = 0; + this->AutoOrientNormals = 0; +} + +//------------------------------------------------------------------------------ +vtkmPolyDataNormals::~vtkmPolyDataNormals() = default; + +//------------------------------------------------------------------------------ +int vtkmPolyDataNormals::RequestData( + vtkInformation *request, + vtkInformationVector **inputVector, + vtkInformationVector *outputVector) +{ + // get the info objects + vtkInformation *inInfo = inputVector[0]->GetInformationObject(0); + vtkInformation *outInfo = outputVector->GetInformationObject(0); + + // get the input and output + vtkPolyData *input = vtkPolyData::SafeDownCast( + inInfo->Get(vtkDataObject::DATA_OBJECT())); + vtkPolyData *output = vtkPolyData::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; + vtkm::cont::DataSet out; + + // check for flags that vtkm filter cannot handle + bool unsupported = this->Splitting || this->Consistency || this->FlipNormals; + bool vtkmSuccess = false; + if (!unsupported) + { + vtkm::filter::SurfaceNormals filter; + filter.SetGenerateCellNormals(this->ComputeCellNormals); + filter.SetCellNormalsName("Normals"); + filter.SetGeneratePointNormals(this->ComputePointNormals); + filter.SetPointNormalsName("Normals"); + auto result = filter.Execute(in, policy); + + if (result.IsValid()) + { + out = result.GetDataSet(); + vtkmSuccess = true; + } + } + + if (!vtkmSuccess) + { + vtkWarningMacro(<< "VTKm SurfaceNormals algorithm failed to run" + << (unsupported ? ": unsupported settings." : ".") + << "Falling back to vtkPolyDataNormals."); + return this->Superclass::RequestData(request, inputVector, outputVector); + } + + if (!fromvtkm::Convert(out, output, input)) + { + vtkErrorMacro(<< "Unable to convert VTKm DataSet back to VTK"); + return 0; + } + vtkSmartPointer pointNormals = output->GetPointData()->GetArray("Normals"); + vtkSmartPointer cellNormals = output->GetCellData()->GetArray("Normals"); + + output->GetPointData()->CopyNormalsOff(); + output->GetPointData()->PassData(input->GetPointData()); + output->GetCellData()->CopyNormalsOff(); + output->GetCellData()->PassData(input->GetPointData()); + + if (pointNormals) + { + output->GetPointData()->SetNormals(pointNormals.GetPointer()); + } + if (cellNormals) + { + output->GetCellData()->SetNormals(cellNormals.GetPointer()); + } + + return 1; +} diff --git a/Accelerators/Vtkm/vtkmPolyDataNormals.h b/Accelerators/Vtkm/vtkmPolyDataNormals.h new file mode 100644 index 0000000000..1fd0a27509 --- /dev/null +++ b/Accelerators/Vtkm/vtkmPolyDataNormals.h @@ -0,0 +1,41 @@ +/*========================================================================= + + Program: Visualization Toolkit + Module: vtkmPolyDataNormals.h + + 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. + +=========================================================================*/ +#ifndef vtkmPolyDataNormals_h +#define vtkmPolyDataNormals_h + +#include "vtkPolyDataNormals.h" +#include "vtkAcceleratorsVTKmModule.h" // for export macro + +class VTKACCELERATORSVTKM_EXPORT vtkmPolyDataNormals : public vtkPolyDataNormals +{ +public: + vtkTypeMacro(vtkmPolyDataNormals, vtkPolyDataNormals) + void PrintSelf(ostream& os, vtkIndent indent) VTK_OVERRIDE; + static vtkmPolyDataNormals* New(); + +protected: + vtkmPolyDataNormals(); + ~vtkmPolyDataNormals(); + + int RequestData(vtkInformation*, vtkInformationVector**, + vtkInformationVector*) VTK_OVERRIDE; + +private: + vtkmPolyDataNormals(const vtkmPolyDataNormals&) VTK_DELETE_FUNCTION; + void operator=(const vtkmPolyDataNormals&) VTK_DELETE_FUNCTION; +}; + +#endif // vtkmPolyDataNormals_h +// VTK-HeaderTest-Exclude: vtkmPolyDataNormals.h -- GitLab