Commit 89335720 authored by Haocheng LIU's avatar Haocheng LIU

Expose vtkmWarpScalar filter into vtk

It uses the same api as vtkWarpScalar filter.
parent f3dd33e8
......@@ -48,6 +48,7 @@ set(headers
vtkmProbe.h
vtkmThreshold.h
vtkmTriangleMeshPointNormals.h
vtkmWarpScalar.h
)
#implementation of the algorithms for cpu accelerators
......@@ -71,6 +72,7 @@ set(cpu_accelerator_srcs
vtkmProbe.cxx
vtkmThreshold.cxx
vtkmTriangleMeshPointNormals.cxx
vtkmWarpScalar.cxx
vtkmlib/Portals.cxx
vtkmlib/ImplicitFunctionConverter.cxx
)
......@@ -96,6 +98,7 @@ set(cuda_accelerator_srcs
vtkmProbe.cu
vtkmThreshold.cu
vtkmTriangleMeshPointNormals.cu
vtkmWarpScalar.cu
vtkmlib/Portals.cu
vtkmlib/ImplicitFunctionConverter.cu
)
......
......@@ -17,6 +17,7 @@ vtk_add_test_cxx(vtkAcceleratorsVtkmCxxTests tests
TestVTKMThreshold.cxx
TestVTKMThreshold2.cxx
TestVTKMTriangleMeshPointNormals.cxx
TestVTKMWarpScalar.cxx
)
vtk_test_cxx_executable(vtkAcceleratorsVtkmCxxTests tests
RENDERING_FACTORY
......
//=============================================================================
//
// 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 "vtkmWarpScalar.h"
#include "vtkActor.h"
#include "vtkCamera.h"
#include "vtkCellData.h"
#include "vtkDataSet.h"
#include "vtkDataSetMapper.h"
#include "vtkDataArray.h"
#include "vtkFloatArray.h"
#include "vtkImageData.h"
#include "vtkNew.h"
#include "vtkPointData.h"
#include "vtkPointSet.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRegressionTestImage.h"
#include "vtkRTAnalyticSource.h"
#include "vtkSphereSource.h"
int TestVTKMWarpScalar(int argc, char* argv[])
{
vtkNew<vtkRenderer> xyplaneRen, dataNormalRen, customNormalRen;
vtkNew<vtkRenderWindow> renWin;
renWin->SetSize(900, 300);
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
// Define viewport ranges
// (xmin, ymin, xmax, ymax)
double leftViewport[4] = {0.0, 0.0, 0.33, 1.0};
double centerViewport[4] = {0.33, 0.0, .66, 1.0};
double rightViewport[4] = {0.66, 0.0, 1.0, 1.0};
/// First window - xy plane
vtkSmartPointer<vtkRTAnalyticSource> xySource =
vtkSmartPointer<vtkRTAnalyticSource>::New();
xySource->SetWholeExtent(-100, 100, -100, 100, 1, 1);
xySource->SetCenter(0, 0, 0);
xySource->SetMaximum(255);
xySource->SetStandardDeviation(.5);
xySource->SetXFreq(60);
xySource->SetYFreq(30);
xySource->SetZFreq(40);
xySource->SetXMag(10);
xySource->SetYMag(18);
xySource->SetZMag(5);
xySource->SetSubsampleRate(1);
vtkNew<vtkmWarpScalar> xyWarpScalar;
xyWarpScalar->SetScaleFactor(2);
xyWarpScalar->XYPlaneOn();
xyWarpScalar->SetNormal(1, 0, 0); // should be igored
xyWarpScalar->SetInputConnection(xySource->GetOutputPort());
xyWarpScalar->Update();
vtkPointSet* points = xyWarpScalar->GetOutput();
for (vtkIdType i = 0; i < points->GetNumberOfPoints(); i++)
{
assert (points->GetPoint(i)[2] == 3.0);
if (points->GetPoint(i)[2] != 3.0)
{
std::cout << "XYPlane result is wrong at i=" << i <<std::endl;
}
}
vtkNew<vtkDataSetMapper> xyplaneMapper;
xyplaneMapper->SetInputConnection(xyWarpScalar->GetOutputPort());
vtkNew<vtkActor> xyplaneActor;
xyplaneActor->SetMapper(xyplaneMapper);
renWin->AddRenderer(xyplaneRen);
xyplaneRen->SetViewport(leftViewport);
xyplaneRen->SetBackground(0.5, 0.4, 0.3);
xyplaneRen->AddActor(xyplaneActor);
/// Second window - data normal
vtkSmartPointer<vtkSphereSource> dataNormalSource =
vtkSmartPointer<vtkSphereSource>::New();
dataNormalSource->SetRadius(100);
dataNormalSource->SetThetaResolution(20);
dataNormalSource->SetPhiResolution(20);
dataNormalSource->Update();
// Create a scalar array
auto dataNormalSourceOutput = dataNormalSource->GetOutput();
vtkNew<vtkFloatArray> scalarArray;
scalarArray->SetName("scalarfactor");
scalarArray->SetNumberOfValues(dataNormalSourceOutput->GetNumberOfPoints());
for (vtkIdType i = 0; i < dataNormalSourceOutput->GetNumberOfPoints(); i++)
{
scalarArray->SetValue(i, 2);
}
dataNormalSourceOutput->GetPointData()->AddArray(scalarArray);
vtkNew<vtkmWarpScalar> dataNormalWarpScalar;
dataNormalWarpScalar->SetScaleFactor(2);
dataNormalWarpScalar->SetInputData(dataNormalSource->GetOutput());
dataNormalWarpScalar->SetInputArrayToProcess(0,0,0,
vtkDataObject::FIELD_ASSOCIATION_POINTS, "scalarfactor");
vtkNew<vtkDataSetMapper> dataNormalMapper;
dataNormalMapper->SetInputConnection(dataNormalWarpScalar->GetOutputPort());
vtkNew<vtkActor> dataNormalActor;
dataNormalActor->SetMapper(dataNormalMapper);
renWin->AddRenderer(dataNormalRen);
dataNormalRen->SetViewport(centerViewport);
dataNormalRen->SetBackground(0.0, 0.7, 0.2);
dataNormalRen->AddActor(dataNormalActor);
/// Third window - custom normal
vtkSmartPointer<vtkRTAnalyticSource> customNormalSource =
vtkSmartPointer<vtkRTAnalyticSource>::New();
customNormalSource->SetWholeExtent(-100, 100, -100, 100, 1, 1);
customNormalSource->SetCenter(0, 0, 0);
customNormalSource->SetMaximum(255);
customNormalSource->SetStandardDeviation(.5);
customNormalSource->SetXFreq(60);
customNormalSource->SetYFreq(30);
customNormalSource->SetZFreq(40);
customNormalSource->SetXMag(10);
customNormalSource->SetYMag(18);
customNormalSource->SetZMag(5);
customNormalSource->SetSubsampleRate(1);
vtkNew<vtkmWarpScalar> customNormalWarpScalar;
customNormalWarpScalar->SetScaleFactor(2);
customNormalWarpScalar->SetNormal(0.333, 0.333, 0.333);
customNormalWarpScalar->SetInputConnection(customNormalSource->GetOutputPort());
vtkNew<vtkDataSetMapper> customNormalMapper;
customNormalMapper->SetInputConnection(customNormalWarpScalar->GetOutputPort());
vtkNew<vtkActor> customNormalActor;
customNormalActor->SetMapper(customNormalMapper);
renWin->AddRenderer(customNormalRen);
customNormalRen->SetViewport(rightViewport);
customNormalRen->SetBackground(0.3, 0.2, 0.5);
customNormalRen->AddActor(customNormalActor);
xyplaneRen->ResetCamera();
dataNormalRen->ResetCamera();
customNormalRen->ResetCamera();
renWin->Render();
int retVal = vtkRegressionTestImage(renWin);
if(retVal == vtkRegressionTester::DO_INTERACTOR)
{
iren->Start();
retVal = vtkRegressionTester::PASSED;
}
return (!retVal);
}
6ff8298127c1dc7d83aebd8def970ffb0f33ab684357c80e4bdd0f276493c30f63d9f39c97eff7c34d96ddb6444f64fc51e1aca7517c9774febadce7810fd4ee
//=============================================================================
//
// 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 "vtkmWarpScalar.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 "vtkmWarpScalar.h"
#include "vtkmConfig.h"
#include "vtkCellData.h"
#include "vtkDataSet.h"
#include "vtkImageData.h"
#include "vtkImageDataToPointSet.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkPointData.h"
#include "vtkRectilinearGrid.h"
#include "vtkRectilinearGridToPointSet.h"
#include "vtkUnstructuredGrid.h"
#include "vtkmlib/ArrayConverters.h"
#include "vtkmlib/DataSetConverters.h"
#include "vtkmlib/Storage.h"
#include "vtkmFilterPolicy.h"
#include "vtkm/cont/DataSetFieldAdd.h"
#include <vtkm/filter/WarpScalar.h>
vtkStandardNewMacro(vtkmWarpScalar)
namespace {
// This class is pretty similar to vtkmInputFilterPolicy except that it adds
// the support for ArrayHandle and ArrayHandleConstant
class vtkmWarpScalarFilterPolicy : public
vtkm::filter::PolicyBase<vtkmWarpScalarFilterPolicy>
{
public:
using FieldTypeList = tovtkm::FieldTypeInVTK;
using vecType = vtkm::Vec<vtkm::FloatDefault, 3>;
struct TypeListVTKMWarpScalarTags : vtkm::ListTagBase<
vtkm::cont::ArrayHandle<vtkm::FloatDefault>::StorageTag,
vtkm::cont::ArrayHandleConstant<vecType>::StorageTag,
vtkm::cont::ArrayHandleConstant<vtkm::FloatDefault>::StorageTag,
#if defined(VTKM_FILTER_INCLUDE_AOS)
tovtkm::vtkAOSArrayContainerTag
#endif
#if defined(VTKM_FILTER_INCLUDE_SOA)
,tovtkm::vtkSOAArrayContainerTag
#endif
>
{
};
using FieldStorageList = TypeListVTKMWarpScalarTags;
using StructuredCellSetList = tovtkm::CellListStructuredInVTK;
using UnstructuredCellSetList = tovtkm::CellListUnstructuredInVTK;
using AllCellSetList = tovtkm::CellListAllInVTK;
using CoordinateTypeList = vtkm::TypeListTagFieldVec3;
using CoordinateStorageList = tovtkm::PointListInVTK;
using DeviceAdapterList = vtkm::filter::PolicyDefault::DeviceAdapterList;
};
}
//------------------------------------------------------------------------------
vtkmWarpScalar::vtkmWarpScalar() : vtkWarpScalar()
{
}
//------------------------------------------------------------------------------
vtkmWarpScalar::~vtkmWarpScalar()
{
}
//------------------------------------------------------------------------------
int vtkmWarpScalar::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 image data.
vtkImageData *inImage = vtkImageData::GetData(inputVector[0]);
if (inImage)
{
vtkNew<vtkImageDataToPointSet> image2points;
image2points->SetInputData(inImage);
image2points->Update();
input = image2points->GetOutput();
}
}
if (!input)
{
// Try converting rectilinear grid.
vtkRectilinearGrid *inRect = vtkRectilinearGrid::GetData(inputVector[0]);
if (inRect)
{
vtkNew<vtkRectilinearGridToPointSet> rect2points;
rect2points->SetInputData(inRect);
rect2points->Update();
input = rect2points->GetOutput();
}
}
if (!input)
{
vtkErrorMacro(<< "Invalid or missing input");
return 0;
}
output->CopyStructure(input);
// Get the scalar field info
vtkDataArray* inScalars = this->GetInputArrayToProcess(0, inputVector);
int inScalarsAssociation = this->GetInputArrayAssociation(0, inputVector);
// Get the normal field info
vtkDataArray* inNormals = input->GetPointData()->GetNormals();
vtkPoints* inPts = input->GetPoints();
// InScalars is not used when XYPlane is on
if (!inPts || (!inScalars && !this->XYPlane))
{
vtkDebugMacro( << "No data to warp");
return 1;
}
try
{
vtkm::cont::DataSet in = tovtkm::Convert(input, tovtkm::FieldsFlag::PointsAndCells);
if (inScalars)
{
auto scalarFactor = tovtkm::Convert(inScalars, inScalarsAssociation);
in.AddField(scalarFactor);
}
vtkm::Id numberOfPoints = in.GetCoordinateSystem().GetData().GetNumberOfValues();
// ScaleFactor in vtk is the scalarAmount in vtk-m.
vtkm::filter::WarpScalar warpScalar(this->ScaleFactor);
warpScalar.SetUseCoordinateSystemAsField(true);
// Get/generate the normal field
if (inNormals && !this->UseNormal)
{ // DataNormal
auto inNormalsField = tovtkm::Convert(inNormals,
vtkDataObject::FIELD_ASSOCIATION_POINTS);
in.AddField(inNormalsField);
warpScalar.SetNormalField(inNormals->GetName());
}
else if (this->XYPlane)
{
using vecType = vtkm::Vec<vtkm::FloatDefault, 3>;
vecType normal = vtkm::make_Vec<vtkm::FloatDefault>(0.0, 0.0, 1.0);
vtkm::cont::ArrayHandleConstant<vecType> vectorAH =
vtkm::cont::make_ArrayHandleConstant(normal,
numberOfPoints);
vtkm::cont::DataSetFieldAdd::AddPointField(in, "zNormal", vectorAH);
warpScalar.SetNormalField("zNormal");
}
else
{
using vecType = vtkm::Vec<vtkm::FloatDefault, 3>;
vecType normal = vtkm::make_Vec<vtkm::FloatDefault>(this->Normal[0],
this->Normal[1], this->Normal[2]);
vtkm::cont::ArrayHandleConstant<vecType> vectorAH =
vtkm::cont::make_ArrayHandleConstant(normal,
numberOfPoints);
vtkm::cont::DataSetFieldAdd::AddPointField(in, "instanceNormal", vectorAH);
warpScalar.SetNormalField("instanceNormal");
}
if (this->XYPlane)
{ // Just use the z value to warp the surface. Ignore the input scalars.
std::vector<vtkm::FloatDefault> zValues;
zValues.reserve(static_cast<size_t>(input->GetNumberOfPoints()));
for (vtkIdType i = 0; i < input->GetNumberOfPoints(); i++)
{
zValues.push_back(input->GetPoints()->GetPoint(i)[2]);
}
vtkm::cont::DataSetFieldAdd::AddPointField(in, "scalarfactor", zValues);
warpScalar.SetScarlarFactorField("scalarfactor");
}
else
{
warpScalar.SetScarlarFactorField(std::string(inScalars->GetName()));
}
vtkmWarpScalarFilterPolicy policy;
auto result = warpScalar.Execute(in, policy);
vtkDataArray* warpScalarResult = fromvtkm::Convert(result.GetField("warpscalar",
vtkm::cont::Field::Association::POINTS));
vtkPoints* newPts = vtkPoints::New();
// Update points
newPts->SetNumberOfPoints(warpScalarResult->GetNumberOfTuples());
newPts->SetData(warpScalarResult);
output->SetPoints(newPts);
newPts->Delete();
warpScalarResult->FastDelete();
}
catch (const vtkm::cont::Error& e)
{
vtkErrorMacro(<< "VTK-m error: " << e.GetMessage());
return 0;
}
// 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 vtkmWarpScalar::PrintSelf(std::ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//=============================================================================
//
// 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 vtkmWarpScalar
* @brief deform geometry with scalar data
*
* vtkmWarpScalar is a filter that modifies point coordinates by moving points
* along point normals by the scalar amount times the scalar factor with vtkm
* as its backend.
* Useful for creating carpet or x-y-z plots.
*
* If normals are not present in data, the Normal instance variable will
* be used as the direction along which to warp the geometry. If normals are
* present but you would like to use the Normal instance variable, set the
* UseNormal boolean to true.
*
* If XYPlane boolean is set true, then the z-value is considered to be
* a scalar value (still scaled by scale factor), and the displacement is
* along the z-axis. If scalars are also present, these are copied through
* and can be used to color the surface.
*
* Note that the filter passes both its point data and cell data to
* its output, except for normals, since these are distorted by the
* warping.
*/
#ifndef vtkmWarpScalar_h
#define vtkmWarpScalar_h
#include "vtkWarpScalar.h"
#include "vtkAcceleratorsVTKmModule.h" // required for correct export
class VTKACCELERATORSVTKM_EXPORT vtkmWarpScalar : public vtkWarpScalar
{
public:
vtkTypeMacro(vtkmWarpScalar, vtkWarpScalar)
void PrintSelf(ostream& os, vtkIndent indent) override;
static vtkmWarpScalar* New();
protected:
vtkmWarpScalar();
~vtkmWarpScalar();
int RequestData(vtkInformation* , vtkInformationVector**,
vtkInformationVector*) override;
private:
vtkmWarpScalar(const vtkmWarpScalar&) = delete;
void operator=(const vtkmWarpScalar&) = delete;
};
#endif // vtkmWarpScalar_h
// VTK-HeaderTest-Exclude: vtkmWarpScalar.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