Adding CellLocatorUniformGrid

- Adding a cell locator to locate points in a uniform grid
- Adding unit tests for the new cell locator
This commit is contained in:
ayenpure 2018-08-29 19:25:01 -07:00
parent b56d1789b0
commit 594d1934d4
10 changed files with 452 additions and 0 deletions

@ -0,0 +1,97 @@
#ifndef vtkm_cont_celllocatoruniformgrid_h
#define vtkm_cont_celllocatoruniformgrid_h
#include <vtkm/cont/CellLocator.h>
#include <vtkm/cont/CellSetStructured.h>
#include <vtkm/cont/DeviceAdapterAlgorithm.h>
#include <vtkm/cont/ErrorBadDevice.h>
#include <vtkm/exec/CellLocatorUniformGrid.h>
namespace vtkm
{
namespace cont
{
class CellLocatorUniformGrid : public vtkm::cont::CellLocator
{
public:
VTKM_CONT
void Build() override
{
vtkm::cont::CoordinateSystem coords = this->GetCoordinates();
vtkm::cont::DynamicCellSet cellSet = this->GetCellSet();
if (!coords.GetData().IsType<UniformType>())
throw vtkm::cont::ErrorInternal("CellSet is not 3D Structured Type");
if (!cellSet.IsSameType(StructuredType()))
throw vtkm::cont::ErrorInternal("CellSet is not 3D Structured Type");
Bounds = coords.GetBounds();
Dims = cellSet.Cast<StructuredType>().GetSchedulingRange(vtkm::TopologyElementTagPoint());
RangeTransform[0] =
static_cast<vtkm::FloatDefault>((Dims[0] - 1.0l) / (Bounds.X.Max - Bounds.X.Min));
RangeTransform[1] =
static_cast<vtkm::FloatDefault>((Dims[1] - 1.0l) / (Bounds.Y.Max - Bounds.Y.Min));
RangeTransform[2] =
static_cast<vtkm::FloatDefault>((Dims[2] - 1.0l) / (Bounds.Z.Max - Bounds.Z.Min));
// Since we are calculating the cell Id, and the number of cells is
// 1 less than the number of points in each direction, the -1 from Dims
// is necessary.
PlaneSize = (Dims[0] - 1) * (Dims[1] - 1);
RowSize = (Dims[0] - 1);
}
struct PrepareForExecutionFunctor
{
template <typename DeviceAdapter>
VTKM_CONT bool operator()(DeviceAdapter,
const vtkm::cont::CellLocatorUniformGrid& contLocator,
HandleType& execLocator) const
{
using ExecutionType = vtkm::exec::CellLocatorUniformGrid<DeviceAdapter>;
ExecutionType* execObject =
new ExecutionType(contLocator.Bounds,
contLocator.Dims,
contLocator.RangeTransform,
contLocator.PlaneSize,
contLocator.RowSize,
contLocator.GetCellSet().template Cast<StructuredType>(),
contLocator.GetCoordinates().GetData(),
DeviceAdapter());
execLocator.Reset(execObject);
return true;
}
};
VTKM_CONT
const HandleType PrepareForExecutionImpl(
const vtkm::cont::DeviceAdapterId deviceId) const override
{
const bool success = vtkm::cont::TryExecuteOnDevice(
deviceId, PrepareForExecutionFunctor(), *this, this->ExecHandle);
if (!success)
{
throwFailedRuntimeDeviceTransfer("CellLocatorUniformGrid", deviceId);
}
return this->ExecHandle;
}
private:
using UniformType = vtkm::cont::ArrayHandleUniformPointCoordinates;
using StructuredType = vtkm::cont::CellSetStructured<3>;
vtkm::Bounds Bounds;
vtkm::Vec<vtkm::Id, 3> Dims;
vtkm::Vec<vtkm::FloatDefault, 3> RangeTransform;
vtkm::Id PlaneSize;
vtkm::Id RowSize;
mutable HandleType ExecHandle;
};
}
}
#endif //vtkm_cont_celllocatoruniformgrid_h

@ -23,6 +23,7 @@ set(unit_tests
UnitTestCudaArrayHandleFancy.cu
UnitTestCudaArrayHandleVirtualCoordinates.cu
UnitTestCudaCellLocatorTwoLevelUniformGrid.cu
UnitTestCudaCellLocatorUniformGrid.cu
UnitTestCudaComputeRange.cu
UnitTestCudaColorTable.cu
UnitTestCudaDataSetExplicit.cu

@ -0,0 +1,34 @@
//============================================================================
// 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 2017 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2017 UT-Battelle, LLC.
// Copyright 2017 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
// Make sure that the tested code is using the device adapter specified. This
// is important in the long run so we don't, for example, use the CUDA device
// for a part of an operation where the TBB device was specified.
#define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_ERROR
#include <vtkm/cont/testing/TestingCellLocatorUniformGrid.h>
int UnitTestCudaCellLocatorUniformGrid(int, char* [])
{
auto tracker = vtkm::cont::GetGlobalRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagCuda{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorUniformGrid<vtkm::cont::DeviceAdapterTagCuda>());
}

@ -22,6 +22,7 @@ set(unit_tests
UnitTestSerialArrayHandle.cxx
UnitTestSerialArrayHandleFancy.cxx
UnitTestSerialArrayHandleVirtualCoordinates.cxx
UnitTestSerialCellLocatorUniformGrid.cxx
UnitTestSerialCellLocatorTwoLevelUniformGrid.cxx
UnitTestSerialComputeRange.cxx
UnitTestSerialColorTable.cxx

@ -0,0 +1,34 @@
//============================================================================
// 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 2017 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2017 UT-Battelle, LLC.
// Copyright 2017 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
// Make sure that the tested code is using the device adapter specified. This
// is important in the long run so we don't, for example, use the CUDA device
// for a part of an operation where the TBB device was specified.
#define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_ERROR
#include <vtkm/cont/testing/TestingCellLocatorUniformGrid.h>
int UnitTestSerialCellLocatorUniformGrid(int, char* [])
{
auto tracker = vtkm::cont::GetGlobalRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagSerial{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorUniformGrid<vtkm::cont::DeviceAdapterTagSerial>());
}

@ -23,6 +23,7 @@ set(unit_tests
UnitTestTBBArrayHandleFancy.cxx
UnitTestTBBArrayHandleVirtualCoordinates.cxx
UnitTestTBBCellLocatorTwoLevelUniformGrid.cxx
UnitTestTBBCellLocatorUniformGrid.cxx
UnitTestTBBColorTable.cxx
UnitTestTBBComputeRange.cxx
UnitTestTBBDataSetExplicit.cxx

@ -0,0 +1,34 @@
//============================================================================
// 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 2017 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2017 UT-Battelle, LLC.
// Copyright 2017 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
// Make sure that the tested code is using the device adapter specified. This
// is important in the long run so we don't, for example, use the CUDA device
// for a part of an operation where the TBB device was specified.
#define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_ERROR
#include <vtkm/cont/testing/TestingCellLocatorUniformGrid.h>
int UnitTestTBBCellLocatorUniformGrid(int, char* [])
{
auto tracker = vtkm::cont::GetGlobalRuntimeDeviceTracker();
tracker.ForceDevice(vtkm::cont::DeviceAdapterTagTBB{});
return vtkm::cont::testing::Testing::Run(
TestingCellLocatorUniformGrid<vtkm::cont::DeviceAdapterTagTBB>());
}

@ -25,6 +25,7 @@ set(headers
TestingArrayHandles.h
TestingArrayHandleVirtualCoordinates.h
TestingCellLocatorTwoLevelUniformGrid.h
TestingCellLocatorUniformGrid.h
TestingColorTable.h
TestingComputeRange.h
TestingDeviceAdapter.h

@ -0,0 +1,153 @@
//============================================================================
// 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 2017 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2017 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#ifndef vtk_m_cont_testing_TestingCellLocatorUniformGrid_h
#define vtk_m_cont_testing_TestingCellLocatorUniformGrid_h
#include <random>
#include <string>
#include <vtkm/cont/CellLocatorUniformGrid.h>
#include <vtkm/cont/DataSet.h>
#include <vtkm/cont/testing/MakeTestDataSet.h>
#include <vtkm/cont/testing/Testing.h>
#include <vtkm/exec/CellLocatorUniformGrid.h>
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/WorkletMapField.h>
class LocatorWorklet : public vtkm::worklet::WorkletMapField
{
public:
LocatorWorklet(vtkm::Bounds& bounds_, vtkm::Vec<vtkm::Id, 3>& dims_)
: bounds(bounds_)
, dims(dims_)
{
}
using ControlSignature = void(FieldIn<> pointIn,
ExecObject locator,
FieldOut<> cellId,
FieldOut<> parametric,
FieldOut<> match);
using ExecutionSignature = void(_1, _2, _3, _4, _5);
template <typename PointType>
VTKM_EXEC vtkm::Id CalculateCellId(const PointType& point) const
{
if (!bounds.Contains(point))
return -1;
vtkm::Vec<vtkm::Id, 3> logical;
logical[0] = static_cast<vtkm::Id>(vtkm::Floor((point[0] / bounds.X.Length()) * (dims[0] - 1)));
logical[1] = static_cast<vtkm::Id>(vtkm::Floor((point[1] / bounds.Y.Length()) * (dims[1] - 1)));
logical[2] = static_cast<vtkm::Id>(vtkm::Floor((point[2] / bounds.Z.Length()) * (dims[2] - 1)));
return logical[2] * (dims[0] - 1) * (dims[1] - 1) + logical[1] * (dims[0] - 1) + logical[0];
}
template <typename PointType, typename LocatorType>
VTKM_EXEC void operator()(const PointType& pointIn,
const LocatorType& locator,
vtkm::Id& cellId,
PointType& parametric,
bool& match) const
{
vtkm::Id calculated = CalculateCellId(pointIn);
locator->FindCell(pointIn, cellId, parametric, (*this));
match = (calculated == cellId);
}
private:
vtkm::Bounds bounds;
vtkm::Vec<vtkm::Id, 3> dims;
};
template <typename DeviceAdapter>
class TestingCellLocatorUniformGrid
{
public:
using Algorithm = vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter>;
void TestTest() const
{
vtkm::cont::DataSet dataset = vtkm::cont::testing::MakeTestDataSet().Make3DUniformDataSet1();
vtkm::cont::CoordinateSystem coords = dataset.GetCoordinateSystem();
vtkm::cont::DynamicCellSet cellSet = dataset.GetCellSet();
vtkm::Bounds bounds = coords.GetBounds();
std::cout << "X bounds : " << bounds.X.Min << " to " << bounds.X.Max << std::endl;
std::cout << "Y bounds : " << bounds.Y.Min << " to " << bounds.Y.Max << std::endl;
std::cout << "Z bounds : " << bounds.Z.Min << " to " << bounds.Z.Max << std::endl;
using StructuredType = vtkm::cont::CellSetStructured<3>;
vtkm::Vec<vtkm::Id, 3> dims =
cellSet.Cast<StructuredType>().GetSchedulingRange(vtkm::TopologyElementTagPoint());
std::cout << "Dimensions of dataset : " << dims << std::endl;
vtkm::cont::CellLocatorUniformGrid locator;
locator.SetCoordinates(coords);
locator.SetCellSet(cellSet);
locator.Update();
// Generate some sample points.
using PointType = vtkm::Vec<vtkm::FloatDefault, 3>;
std::vector<PointType> pointsVec;
std::default_random_engine dre;
std::uniform_real_distribution<vtkm::Float32> inBounds(0.0f, 4.0f);
for (size_t i = 0; i < 10; i++)
{
PointType point = vtkm::make_Vec(inBounds(dre), inBounds(dre), inBounds(dre));
pointsVec.push_back(point);
}
std::uniform_real_distribution<vtkm::Float32> outBounds(4.0f, 5.0f);
for (size_t i = 0; i < 5; i++)
{
PointType point = vtkm::make_Vec(outBounds(dre), outBounds(dre), outBounds(dre));
pointsVec.push_back(point);
}
vtkm::cont::ArrayHandle<PointType> points = vtkm::cont::make_ArrayHandle(pointsVec);
// Query the points using the locators.
vtkm::cont::ArrayHandle<vtkm::Id> cellIds;
vtkm::cont::ArrayHandle<PointType> parametric;
vtkm::cont::ArrayHandle<bool> match;
LocatorWorklet worklet(bounds, dims);
vtkm::worklet::DispatcherMapField<LocatorWorklet, DeviceAdapter> dispather(worklet);
dispather.Invoke(points, locator, cellIds, parametric, match);
auto matchPortal = match.GetPortalConstControl();
for (vtkm::Id index = 0; index < match.GetNumberOfValues(); index++)
{
VTKM_TEST_ASSERT(matchPortal.Get(index), "Points do not match");
}
std::cout << "Test finished successfully." << std::endl;
}
void operator()() const
{
vtkm::cont::GetGlobalRuntimeDeviceTracker().ForceDevice(DeviceAdapter());
this->TestTest();
}
};
#endif

@ -0,0 +1,96 @@
#ifndef vtkm_exec_celllocatoruniformgrid_h
#define vtkm_exec_celllocatoruniformgrid_h
#include <vtkm/Bounds.h>
#include <vtkm/TopologyElementTag.h>
#include <vtkm/Types.h>
#include <vtkm/VecFromPortalPermute.h>
#include <vtkm/cont/CellSetStructured.h>
#include <vtkm/exec/CellInside.h>
#include <vtkm/exec/CellLocator.h>
#include <vtkm/exec/ParametricCoordinates.h>
namespace vtkm
{
namespace exec
{
template <typename DeviceAdapter>
class CellLocatorUniformGrid : public vtkm::exec::CellLocator
{
public:
VTKM_CONT
CellLocatorUniformGrid(const vtkm::Bounds& bounds,
const vtkm::Vec<vtkm::Id, 3>& dims,
const vtkm::Vec<vtkm::FloatDefault, 3> rangeTransform,
const vtkm::Id planeSize,
const vtkm::Id rowSize,
const vtkm::cont::CellSetStructured<3>& cellSet,
const vtkm::cont::ArrayHandleVirtualCoordinates& coords,
DeviceAdapter)
: Bounds(bounds)
, Dims(dims)
, RangeTransform(rangeTransform)
, PlaneSize(planeSize)
, RowSize(rowSize)
{
CellSet = cellSet.PrepareForInput(DeviceAdapter(), FromType(), ToType());
Coords = coords.PrepareForInput(DeviceAdapter());
}
VTKM_EXEC
void FindCell(const vtkm::Vec<vtkm::FloatDefault, 3>& point,
vtkm::Id& cellId,
vtkm::Vec<vtkm::FloatDefault, 3>& parametric,
const vtkm::exec::FunctorBase& worklet) const override
{
if (!Bounds.Contains(point))
{
cellId = -1;
return;
}
// Get the Cell Id from the point.
vtkm::Vec<vtkm::FloatDefault, 3> relative;
relative[0] = static_cast<vtkm::FloatDefault>((point[0] - Bounds.X.Min) * RangeTransform[0]);
relative[1] = static_cast<vtkm::FloatDefault>((point[1] - Bounds.Y.Min) * RangeTransform[1]);
relative[2] = static_cast<vtkm::FloatDefault>((point[2] - Bounds.Z.Min) * RangeTransform[2]);
vtkm::Vec<vtkm::Id, 3> logicalCell;
logicalCell[0] = static_cast<vtkm::Id>(vtkm::Floor(relative[0]));
logicalCell[1] = static_cast<vtkm::Id>(vtkm::Floor(relative[1]));
logicalCell[2] = static_cast<vtkm::Id>(vtkm::Floor(relative[2]));
// Get the actual cellId, from the logical cell index of the cell
cellId = logicalCell[2] * PlaneSize + logicalCell[1] * RowSize + logicalCell[0];
bool success = false;
using IndicesType = typename CellSetPortal::IndicesType;
IndicesType cellPointIndices = CellSet.GetIndices(cellId);
vtkm::VecFromPortalPermute<IndicesType, CoordsPortal> cellPoints(&cellPointIndices, Coords);
auto cellShape = CellSet.GetCellShape(cellId);
// Get Parametric Coordinates from the cell, for the point.
parametric = vtkm::exec::WorldCoordinatesToParametricCoordinates(
cellPoints, point, cellShape, success, worklet);
}
private:
using FromType = vtkm::TopologyElementTagPoint;
using ToType = vtkm::TopologyElementTagCell;
using CellSetPortal = typename vtkm::cont::CellSetStructured<
3>::template ExecutionTypes<DeviceAdapter, FromType, ToType>::ExecObjectType;
using CoordsPortal = typename vtkm::cont::ArrayHandleVirtualCoordinates::template ExecutionTypes<
DeviceAdapter>::PortalConst;
vtkm::Bounds Bounds;
vtkm::Vec<vtkm::Id, 3> Dims;
vtkm::Vec<vtkm::FloatDefault, 3> RangeTransform;
vtkm::Id PlaneSize;
vtkm::Id RowSize;
CellSetPortal CellSet;
CoordsPortal Coords;
};
}
}
#endif //vtkm_exec_celllocatoruniformgrid_h