Commit fb87d303 authored by Haocheng LIU's avatar Haocheng LIU

Expose vtkm point transform filter

parent ee13bc40
......@@ -43,6 +43,7 @@ set(headers
vtkmLevelOfDetail.h
vtkmNDHistogram.h
vtkmPointElevation.h
vtkmPointTransform.h
vtkmPolyDataNormals.h
vtkmProbe.h
vtkmThreshold.h
......@@ -68,6 +69,7 @@ set(cpu_accelerator_srcs
vtkmLevelOfDetail.cxx
vtkmNDHistogram.cxx
vtkmPointElevation.cxx
vtkmPointTransform.cxx
vtkmPolyDataNormals.cxx
vtkmProbe.cxx
vtkmThreshold.cxx
......@@ -96,6 +98,7 @@ set(cuda_accelerator_srcs
vtkmLevelOfDetail.cu
vtkmNDHistogram.cu
vtkmPointElevation.cu
vtkmPointTransform.cu
vtkmPolyDataNormals.cu
vtkmProbe.cu
vtkmThreshold.cu
......
......@@ -12,6 +12,7 @@ vtk_add_test_cxx(vtkAcceleratorsVtkmCxxTests tests
TestVTKMMarchingCubes2.cxx
TestVTKMNDHistogram.cxx,NO_VALID
TestVTKMPointElevation.cxx
TestVTKMPointTransform.cxx
TestVTKMProbe.cxx,NO_VALID
TestVTKMPolyDataNormals.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 "vtkActor.h"
#include "vtkCamera.h"
#include "vtkDataSetSurfaceFilter.h"
#include "vtkPlaneSource.h"
#include "vtkmPointTransform.h"
#include "vtkFloatArray.h"
#include "vtkImageData.h"
#include "vtkMath.h"
#include "vtkNew.h"
#include "vtkPoints.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkTransform.h"
#include "vtkTriangleFilter.h"
#include "vtkRegressionTestImage.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
namespace {
int RunVTKPipeline(vtkPlaneSource* plane, int argc, char* argv[])
{
vtkNew<vtkRenderer> ren;
vtkNew<vtkRenderWindow> renWin;
vtkNew<vtkRenderWindowInteractor> iren;
renWin->AddRenderer(ren);
iren->SetRenderWindow(renWin);
vtkNew<vtkTriangleFilter> tf;
tf->SetInputConnection(plane->GetOutputPort());
tf->Update();
vtkNew<vtkPolyData> pd;
pd->CopyStructure(tf->GetOutput());
vtkIdType numPts = pd->GetNumberOfPoints();
vtkPoints* oldPts = tf->GetOutput()->GetPoints();
vtkNew<vtkPoints> newPts;
newPts->SetNumberOfPoints(numPts);
for (vtkIdType i = 0; i < numPts; i++)
{
auto pt = oldPts->GetPoint(i);
auto r = sqrt(pow(pt[0],2) + pow(pt[1], 2));
auto z = 1.5 * cos (2* r);
newPts->SetPoint(i, pt[0], pt[1], z);
}
pd->SetPoints(newPts);
vtkNew<vtkmPointTransform> pf;
pf->SetInputData(pd);
vtkNew<vtkTransform> transformMatrix;
transformMatrix->RotateX(30);
transformMatrix->RotateY(60);
transformMatrix->RotateZ(90);
pf->SetTransform(transformMatrix);
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(pf->GetOutputPort());
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
ren->AddActor(actor);
ren->SetBackground(0.0, 0.0, 0.0);
renWin->Render();
int retVal = vtkRegressionTestImage(renWin);
if(retVal == vtkRegressionTester::DO_INTERACTOR)
{
iren->Start();
retVal = vtkRegressionTester::PASSED;
}
return (!retVal);
}
}
int TestVTKMPointTransform(int argc, char* argv[])
{
vtkNew<vtkPlaneSource> plane;
int res = 300;
plane->SetXResolution(res);
plane->SetYResolution(res);
plane->SetOrigin(-10.0, -10.0, 0.0);
plane->SetPoint1(10.0, -10.0, 0.0);
plane->SetPoint2(-10.0, 10.0, 0.0);
return RunVTKPipeline(plane, argc, argv);
}
46e175d5ae08aab1e49209df20d4ea2be38456211c89e0dd41a0aad372c26c34c1ef6b581ed3715c07c1be31e670efdf3665a8706de7b053c18ef0362b578547
//=============================================================================
//
// 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 "vtkmPointTransform.cxx"
/*=========================================================================
Program: Visualization Toolkit
Module: vtkTransformFilter.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 "vtkmPointTransform.h"
#include "vtkCellData.h"
#include "vtkHomogeneousTransform.h"
#include "vtkImageData.h"
#include "vtkInformationVector.h"
#include "vtkInformation.h"
#include "vtkObjectFactory.h"
#include "vtkMatrix4x4.h"
#include "vtkNew.h"
#include "vtkPointData.h"
#include "vtkRectilinearGrid.h"
#include "vtkRectilinearGridToPointSet.h"
#include "vtkSmartPointer.h"
#include "vtkStructuredGrid.h"
#include "vtkmlib/ArrayConverters.h"
#include "vtkmlib/DataSetConverters.h"
#include "vtkmlib/Storage.h"
#include "vtkm/cont/Error.h"
#include "vtkm/cont/Error.h"
#include "vtkm/filter/PointTransform.h"
#include "vtkmFilterPolicy.h"
vtkStandardNewMacro(vtkmPointTransform);
vtkCxxSetObjectMacro(vtkmPointTransform,Transform,vtkHomogeneousTransform);
//------------------------------------------------------------------------------
vtkmPointTransform::vtkmPointTransform()
{
this->Transform = nullptr;
}
//------------------------------------------------------------------------------
vtkmPointTransform::~vtkmPointTransform()
{
this->SetTransform(nullptr);
}
//------------------------------------------------------------------------------
int vtkmPointTransform::FillInputPortInformation(int vtkNotUsed(port),
vtkInformation* info)
{
info->Remove(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE());
info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPointSet");
info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkImageData");
info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkRectilinearGrid");
return 1;
}
//------------------------------------------------------------------------------
int vtkmPointTransform::RequestDataObject(vtkInformation* request,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
vtkImageData* inImage = vtkImageData::GetData(inputVector[0]);
vtkRectilinearGrid* inRect = vtkRectilinearGrid::GetData(inputVector[0]);
if (inImage || inRect)
{
vtkStructuredGrid* output = vtkStructuredGrid::GetData(outputVector);
if (!output)
{
vtkNew<vtkStructuredGrid> newOutput;
outputVector->GetInformationObject(0)->Set(
vtkDataObject::DATA_OBJECT(), newOutput);
}
return 1;
}
else
{
return this->Superclass::RequestDataObject(request, inputVector, outputVector);
}
}
//------------------------------------------------------------------------------
int vtkmPointTransform::RequestData(vtkInformation* vtkNotUsed(request),
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
vtkSmartPointer<vtkPointSet> input = vtkPointSet::GetData(inputVector[0]);
vtkSmartPointer<vtkPointSet> output = vtkPointSet::GetData(outputVector);
if (!input)
{
// Try converting rectilinear grid
vtkRectilinearGrid* inRect = vtkRectilinearGrid::GetData(inputVector[0]);
if (inRect)
{
vtkNew<vtkRectilinearGridToPointSet> rectToPoints;
rectToPoints->SetInputData(inRect);
rectToPoints->Update();
input = rectToPoints->GetOutput();
}
}
if (!input)
{
vtkErrorMacro(<< "Invalid or missing input");
return 0;
}
output->CopyStructure(input);
vtkPoints* inPts = input->GetPoints();
if (!inPts || !this->Transform)
{
vtkDebugMacro(<< "Miss input points or transform matrix");
return 0;
}
try
{
vtkm::cont::DataSet in = tovtkm::Convert(input, tovtkm::FieldsFlag::PointsAndCells);
vtkMatrix4x4* matrix = this->Transform->GetMatrix();
vtkm::Matrix<vtkm::FloatDefault, 4, 4> vtkmMatrix;
for (int i = 0; i < 4; i++)
{
for(int j = 0;j < 4; j++)
{
vtkmMatrix[i][j] = static_cast<vtkm::FloatDefault>(matrix->GetElement(i, j));
}
}
vtkm::filter::PointTransform<vtkm::FloatDefault> pointTransform;
pointTransform.SetUseCoordinateSystemAsField(true);
pointTransform.SetTransform(vtkmMatrix);
vtkmInputFilterPolicy policy;
auto result = pointTransform.Execute(in, policy);
vtkDataArray* pointTransformResult = fromvtkm::Convert(result.GetField("transform",
vtkm::cont::Field::Association::POINTS));
vtkPoints* newPts = vtkPoints::New();
// Update points
newPts->SetNumberOfPoints(pointTransformResult->GetNumberOfTuples());
newPts->SetData(pointTransformResult);
output->SetPoints(newPts);
newPts->Delete();
pointTransformResult->FastDelete();
}
catch(const vtkm::cont::Error& e)
{
vtkErrorMacro(<< "VTK-m error: " << e.GetMessage());
}
// Update ourselves and release memory
output->GetPointData()->CopyNormalsOff(); // distorted geometry
output->GetPointData()->PassData(input->GetPointData());
output->GetCellData()->CopyNormalsOff(); // distorted geometry
output->GetCellData()->PassData(input->GetCellData());
return 1;
}
//------------------------------------------------------------------------------
void vtkmPointTransform::PrintSelf(std::ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Transform: " << this->Transform << "\n";
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkTransformFilter.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.
=========================================================================*/
/**
* @class vtkmPointTransform
* @brief transform points via vtkm PointTransform filter
*
* vtkmPointTransform is a filter to transform point coordinates. For now it
* does not support transforming associated point normals and vectors, as well
* as cell normals and vectors with the point coordinates.
*/
#ifndef vtkmPointTransform_h
#define vtkmPointTransform_h
#include "vtkAcceleratorsVTKmModule.h" // For export macro
#include "vtkPointSetAlgorithm.h"
class vtkHomogeneousTransform;
class VTKACCELERATORSVTKM_EXPORT vtkmPointTransform : public vtkPointSetAlgorithm
{
public:
vtkTypeMacro(vtkmPointTransform, vtkPointSetAlgorithm)
void PrintSelf(ostream& os, vtkIndent indent) override;
static vtkmPointTransform *New();
//@{
/**
* Specify the transform object used to transform the points
*/
void SetTransform(vtkHomogeneousTransform* tf);
vtkGetObjectMacro(Transform,vtkHomogeneousTransform);
//@}
int FillInputPortInformation(int port, vtkInformation *info) override;
protected:
vtkmPointTransform();
~vtkmPointTransform() override;
int RequestDataObject(vtkInformation* request,
vtkInformationVector** inputVector,
vtkInformationVector* outputVector) override;
int RequestData(vtkInformation*,
vtkInformationVector**,
vtkInformationVector*) override;
vtkHomogeneousTransform* Transform;
private:
vtkmPointTransform(const vtkmPointTransform&) = delete;
void operator=(const vtkmPointTransform&) = delete;
};
#endif
// VTK-HeaderTest-Exclude: vtkmPointTransform.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