Merge topic 'invert_clip'

9cebcdaa adjusting formatting
0d671bcb adding inverted clip unit tests
2daba821 adding the ability to invert clips

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !1107
This commit is contained in:
Matt Larsen 2018-03-10 03:29:48 +00:00 committed by Kitware Robot
commit 4d14214480
7 changed files with 149 additions and 13 deletions

@ -43,6 +43,9 @@ public:
VTKM_CONT
void SetClipValue(vtkm::Float64 value) { this->ClipValue = value; }
VTKM_CONT
void SetInvertClip(bool invert) { this->Invert = invert; }
VTKM_CONT
vtkm::Float64 GetClipValue() const { return this->ClipValue; }
@ -65,6 +68,7 @@ public:
private:
vtkm::Float64 ClipValue;
vtkm::worklet::Clip Worklet;
bool Invert;
};
template <>

@ -60,6 +60,7 @@ inline VTKM_CONT ClipWithField::ClipWithField()
: vtkm::filter::FilterDataSetWithField<ClipWithField>()
, ClipValue(0)
, Worklet()
, Invert(false)
{
}
@ -87,8 +88,8 @@ inline VTKM_CONT vtkm::filter::Result ClipWithField::DoExecute(
const vtkm::cont::CoordinateSystem& inputCoords =
input.GetCoordinateSystem(this->GetActiveCoordinateSystemIndex());
vtkm::cont::CellSetExplicit<> outputCellSet =
this->Worklet.Run(vtkm::filter::ApplyPolicy(cells, policy), field, this->ClipValue, device);
vtkm::cont::CellSetExplicit<> outputCellSet = this->Worklet.Run(
vtkm::filter::ApplyPolicy(cells, policy), field, this->ClipValue, this->Invert, device);
//create the output data
vtkm::cont::DataSet output;

@ -38,11 +38,15 @@ namespace filter
class ClipWithImplicitFunction : public vtkm::filter::FilterDataSet<ClipWithImplicitFunction>
{
public:
ClipWithImplicitFunction();
void SetImplicitFunction(const vtkm::cont::ImplicitFunctionHandle& func)
{
this->Function = func;
}
void SetInvertClip(bool invert) { this->Invert = invert; }
const vtkm::cont::ImplicitFunctionHandle& GetImplicitFunction() const { return this->Function; }
template <typename DerivedPolicy, typename DeviceAdapter>
@ -62,6 +66,7 @@ public:
private:
vtkm::cont::ImplicitFunctionHandle Function;
vtkm::worklet::Clip Worklet;
bool Invert;
};
}
} // namespace vtkm::filter

@ -54,6 +54,12 @@ struct PointMapHelper
} // end namespace clipwithimplicitfunction
//-----------------------------------------------------------------------------
ClipWithImplicitFunction::ClipWithImplicitFunction()
: Invert(false)
{
}
template <typename DerivedPolicy, typename DeviceAdapter>
inline vtkm::filter::Result ClipWithImplicitFunction::DoExecute(
const vtkm::cont::DataSet& input,
@ -69,7 +75,7 @@ inline vtkm::filter::Result ClipWithImplicitFunction::DoExecute(
input.GetCoordinateSystem(this->GetActiveCoordinateSystemIndex());
vtkm::cont::CellSetExplicit<> outputCellSet = this->Worklet.Run(
vtkm::filter::ApplyPolicy(cells, policy), this->Function, inputCoords, device);
vtkm::filter::ApplyPolicy(cells, policy), this->Function, inputCoords, this->Invert, device);
// compute output coordinates
auto outputCoordsArray = this->Worklet.ProcessPointField(inputCoords.GetData(), device);

@ -93,10 +93,53 @@ void TestClipStructured()
}
}
void TestClipStructuredInverted()
{
std::cout << "Testing ClipWithImplicitFunctionInverted Filter on Structured data" << std::endl;
vtkm::cont::DataSet ds = MakeTestDatasetStructured();
vtkm::Vec<vtkm::FloatDefault, 3> center(1, 1, 0);
vtkm::FloatDefault radius(0.5);
vtkm::filter::Result result;
vtkm::filter::ClipWithImplicitFunction clip;
clip.SetImplicitFunction(vtkm::cont::make_ImplicitFunctionHandle(vtkm::Sphere(center, radius)));
bool invert = true;
clip.SetInvertClip(invert);
result = clip.Execute(ds);
clip.MapFieldOntoOutput(result, ds.GetField("scalars"));
const vtkm::cont::DataSet& outputData = result.GetDataSet();
VTKM_TEST_ASSERT(outputData.GetNumberOfCellSets() == 1,
"Wrong number of cellsets in the output dataset");
VTKM_TEST_ASSERT(outputData.GetNumberOfCoordinateSystems() == 1,
"Wrong number of coordinate systems in the output dataset");
VTKM_TEST_ASSERT(outputData.GetNumberOfFields() == 1,
"Wrong number of fields in the output dataset");
VTKM_TEST_ASSERT(outputData.GetCellSet().GetNumberOfCells() == 4,
"Wrong number of cells in the output dataset");
vtkm::cont::DynamicArrayHandle temp = outputData.GetField("scalars").GetData();
vtkm::cont::ArrayHandle<vtkm::Float32> resultArrayHandle;
temp.CopyTo(resultArrayHandle);
VTKM_TEST_ASSERT(resultArrayHandle.GetNumberOfValues() == 13,
"Wrong number of points in the output dataset");
vtkm::Float32 expected[13] = { 1, 1, 1, 1, 0, 1, 1, 1, 1, 0.25, 0.25, 0.25, 0.25 };
for (int i = 0; i < 13; ++i)
{
VTKM_TEST_ASSERT(test_equal(resultArrayHandle.GetPortalConstControl().Get(i), expected[i]),
"Wrong result for ClipWithImplicitFunction fliter on sturctured quads data");
}
}
void TestClip()
{
//todo: add more clip tests
TestClipStructured();
TestClipStructuredInverted();
}
} // anonymous namespace

@ -196,9 +196,10 @@ public:
typedef void ExecutionSignature(_2, CellShape, PointCount, _3, _4);
VTKM_CONT
ComputeStats(vtkm::Float64 value, const ClipTablesPortal& clipTables)
ComputeStats(vtkm::Float64 value, const ClipTablesPortal& clipTables, bool invert)
: Value(value)
, ClipTables(clipTables)
, Invert(invert)
{
}
@ -215,7 +216,14 @@ public:
vtkm::Id caseId = 0;
for (vtkm::IdComponent i = 0; i < count; ++i)
{
caseId |= (static_cast<vtkm::Float64>(scalars[i]) > this->Value) ? mask[i] : 0;
if (this->Invert)
{
caseId |= (static_cast<vtkm::Float64>(scalars[i]) <= this->Value) ? mask[i] : 0;
}
else
{
caseId |= (static_cast<vtkm::Float64>(scalars[i]) > this->Value) ? mask[i] : 0;
}
}
vtkm::Id idx = this->ClipTables.GetCaseIndex(shape.Id, caseId);
@ -244,6 +252,7 @@ public:
private:
vtkm::Float64 Value;
ClipTablesPortal ClipTables;
bool Invert;
};
template <typename DeviceAdapter>
@ -432,6 +441,7 @@ public:
vtkm::cont::CellSetExplicit<> Run(const vtkm::cont::DynamicCellSetBase<CellSetList>& cellSet,
const ScalarsArrayHandle& scalars,
vtkm::Float64 value,
bool invert,
DeviceAdapter device)
{
using Algorithm = vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter>;
@ -443,7 +453,7 @@ public:
vtkm::cont::ArrayHandle<vtkm::Id> clipTableIdxs;
vtkm::cont::ArrayHandle<ClipStats> stats;
ComputeStats<DeviceAdapter> computeStats(value, clipTablesDevicePortal);
ComputeStats<DeviceAdapter> computeStats(value, clipTablesDevicePortal, invert);
DispatcherMapTopology<ComputeStats<DeviceAdapter>, DeviceAdapter>(computeStats)
.Invoke(cellSet, scalars, clipTableIdxs, stats);
@ -521,10 +531,12 @@ public:
ClipWithImplicitFunction(Clip* clipper,
const DynamicCellSet& cellSet,
const vtkm::ImplicitFunction* function,
const bool invert,
vtkm::cont::CellSetExplicit<>* result)
: Clipper(clipper)
, CellSet(&cellSet)
, Function(function)
, Invert(invert)
, Result(result)
{
}
@ -538,13 +550,15 @@ public:
handle, this->Function);
// Clip at locations where the implicit function evaluates to 0
*this->Result = this->Clipper->Run(*this->CellSet, clipScalars, 0.0, DeviceAdapter());
*this->Result =
this->Clipper->Run(*this->CellSet, clipScalars, 0.0, this->Invert, DeviceAdapter());
}
private:
Clip* Clipper;
const DynamicCellSet* CellSet;
vtkm::ImplicitFunctionValue Function;
bool Invert;
vtkm::cont::CellSetExplicit<>* Result;
};
@ -552,12 +566,13 @@ public:
vtkm::cont::CellSetExplicit<> Run(const vtkm::cont::DynamicCellSetBase<CellSetList>& cellSet,
const vtkm::cont::ImplicitFunctionHandle& clipFunction,
const vtkm::cont::CoordinateSystem& coords,
const bool invert,
DeviceAdapter device)
{
vtkm::cont::CellSetExplicit<> output;
ClipWithImplicitFunction<vtkm::cont::DynamicCellSetBase<CellSetList>, DeviceAdapter> clip(
this, cellSet, clipFunction.PrepareForExecution(device), &output);
this, cellSet, clipFunction.PrepareForExecution(device), invert, &output);
CastAndCall(coords, clip);
return output;

@ -129,8 +129,9 @@ void TestClippingExplicit()
vtkm::cont::DataSet ds = MakeTestDatasetExplicit();
vtkm::worklet::Clip clip;
vtkm::cont::CellSetExplicit<> outputCellSet =
clip.Run(ds.GetCellSet(0), ds.GetField("scalars").GetData(), clipValue, DeviceAdapter());
bool invertClip = false;
vtkm::cont::CellSetExplicit<> outputCellSet = clip.Run(
ds.GetCellSet(0), ds.GetField("scalars").GetData(), clipValue, invertClip, DeviceAdapter());
auto coordsIn = ds.GetCoordinateSystem("coords").GetData();
vtkm::cont::ArrayHandle<Coord3D> coords = clip.ProcessPointField(coordsIn, DeviceAdapter());
@ -183,9 +184,10 @@ void TestClippingStrucutred()
vtkm::cont::DataSet ds = MakeTestDatasetStructured();
bool invertClip = false;
vtkm::worklet::Clip clip;
vtkm::cont::CellSetExplicit<> outputCellSet =
clip.Run(ds.GetCellSet(0), ds.GetField("scalars").GetData(), clipValue, DeviceAdapter());
vtkm::cont::CellSetExplicit<> outputCellSet = clip.Run(
ds.GetCellSet(0), ds.GetField("scalars").GetData(), clipValue, invertClip, DeviceAdapter());
auto coordsIn = ds.GetCoordinateSystem("coords").GetData();
CoordsOutType coords = clip.ProcessPointField(coordsIn, DeviceAdapter());
@ -243,12 +245,13 @@ void TestClippingWithImplicitFunction()
vtkm::FloatDefault radius(0.5);
vtkm::cont::DataSet ds = MakeTestDatasetStructured();
bool invertClip = false;
vtkm::worklet::Clip clip;
vtkm::cont::CellSetExplicit<> outputCellSet =
clip.Run(ds.GetCellSet(0),
vtkm::cont::make_ImplicitFunctionHandle<vtkm::Sphere>(center, radius),
ds.GetCoordinateSystem("coords"),
invertClip,
DeviceAdapter());
auto coordsIn = ds.GetCoordinateSystem("coords").GetData();
@ -297,6 +300,64 @@ void TestClippingWithImplicitFunction()
"Got incorrect cellvar");
}
template <typename DeviceAdapter>
void TestClippingWithImplicitFunctionInverted()
{
vtkm::Vec<vtkm::FloatDefault, 3> center(1, 1, 0);
vtkm::FloatDefault radius(0.5);
vtkm::cont::DataSet ds = MakeTestDatasetStructured();
bool invertClip = true;
vtkm::worklet::Clip clip;
vtkm::cont::CellSetExplicit<> outputCellSet =
clip.Run(ds.GetCellSet(0),
vtkm::cont::make_ImplicitFunctionHandle<vtkm::Sphere>(center, radius),
ds.GetCoordinateSystem("coords"),
invertClip,
DeviceAdapter());
auto coordsIn = ds.GetCoordinateSystem("coords").GetData();
vtkm::cont::ArrayHandle<Coord3D> coords = clip.ProcessPointField(coordsIn, DeviceAdapter());
vtkm::cont::ArrayHandle<vtkm::Float32> scalarsIn;
ds.GetField("scalars").GetData().CopyTo(scalarsIn);
vtkm::cont::ArrayHandle<vtkm::Float32> scalars =
clip.ProcessPointField(scalarsIn, DeviceAdapter());
vtkm::cont::ArrayHandle<vtkm::Float32> cellvarIn;
ds.GetField("cellvar").GetData().CopyTo(cellvarIn);
vtkm::cont::ArrayHandle<vtkm::Float32> cellvar =
clip.ProcessCellField(cellvarIn, DeviceAdapter());
vtkm::Id connectivitySize = 12;
vtkm::Id fieldSize = 13;
vtkm::Id expectedConnectivity[] = { 4, 10, 9, 4, 9, 11, 4, 12, 10, 4, 11, 12 };
Coord3D expectedCoords[] = {
Coord3D(0.0f, 0.0f, 0.0f), Coord3D(1.0f, 0.0f, 0.0f), Coord3D(2.0f, 0.0f, 0.0f),
Coord3D(0.0f, 1.0f, 0.0f), Coord3D(1.0f, 1.0f, 0.0f), Coord3D(2.0f, 1.0f, 0.0f),
Coord3D(0.0f, 2.0f, 0.0f), Coord3D(1.0f, 2.0f, 0.0f), Coord3D(2.0f, 2.0f, 0.0f),
Coord3D(1.0f, 0.75f, 0.0f), Coord3D(0.75f, 1.0f, 0.0f), Coord3D(1.25f, 1.0f, 0.0f),
Coord3D(1.0f, 1.25f, 0.0f),
};
vtkm::Float32 expectedScalars[] = { 1, 1, 1, 1, 0, 1, 1, 1, 1, 0.25, 0.25, 0.25, 0.25 };
std::vector<vtkm::Float32> expectedCellvar = { -100.f, 100.f, 30.f, -30.f };
VTKM_TEST_ASSERT(
TestArrayHandle(outputCellSet.GetConnectivityArray(vtkm::TopologyElementTagPoint(),
vtkm::TopologyElementTagCell()),
expectedConnectivity,
connectivitySize),
"Got incorrect conectivity");
VTKM_TEST_ASSERT(TestArrayHandle(coords, expectedCoords, fieldSize), "Got incorrect coordinates");
VTKM_TEST_ASSERT(TestArrayHandle(scalars, expectedScalars, fieldSize), "Got incorrect scalars");
VTKM_TEST_ASSERT(
TestArrayHandle(cellvar, expectedCellvar.data(), static_cast<vtkm::Id>(expectedCellvar.size())),
"Got incorrect cellvar");
}
template <typename DeviceAdapter>
void TestClipping()
{
@ -306,6 +367,7 @@ void TestClipping()
TestClippingStrucutred<DeviceAdapter>();
std::cout << "Testing clipping with implicit function (sphere):" << std::endl;
TestClippingWithImplicitFunction<DeviceAdapter>();
TestClippingWithImplicitFunctionInverted<DeviceAdapter>();
}
int UnitTestClipping(int, char* [])