vtk-m/vtkm/worklet/testing/UnitTestPointElevation.cxx
Kenneth Moreland 9f624f0a36 Have CoordinateSystem inherit from Field
Previously, coordinate systems in a DataSet simply pointed to field data
specifying the coordinate information (although the ability to get that
back out of the DataSet was missing). This makes sense since point
coordinates are in fact just fields with a particular semantic meaning
to them.

However, there is an issue with this approach. It turns out that there
are special representations that are very common for point coordinates
and very uncommon for other types of fields. For example, a uniform
(a.k.a. regular or image) grid has point coordinates that are easily
derived from the point index, but such fields are quite uncommon
elsewhere.

Representing this kind of structure in the Field list of a DataSet is
problematic. Either all fields have to check to see if they are this
type, which will cause an explosion of unnecessary generated code, or
you will have to actually write out the coordinates in memory, which is
really wasteful but what was done previously.

However, by storing fields representing coordinate systems in a separate
batch, we can use these special types without the stated explosion.
2015-08-25 14:38:41 -06:00

116 lines
3.9 KiB
C++

//============================================================================
// 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 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 Los Alamos National Security.
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// 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.
//============================================================================
#include <vtkm/worklet/PointElevation.h>
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/cont/CellSetExplicit.h>
#include <vtkm/cont/DataSet.h>
#include <vtkm/cont/testing/Testing.h>
#include <vector>
namespace {
vtkm::cont::DataSet MakePointElevationTestDataSet()
{
vtkm::cont::DataSet dataSet;
std::vector<vtkm::Vec<vtkm::Float32,3> > coordinates;
const vtkm::Id dim = 5;
for (vtkm::Id j = 0; j < dim; ++j)
{
vtkm::Float32 z = static_cast<vtkm::Float32>(j) /
static_cast<vtkm::Float32>(dim - 1);
for (vtkm::Id i = 0; i < dim; ++i)
{
vtkm::Float32 x = static_cast<vtkm::Float32>(i) /
static_cast<vtkm::Float32>(dim - 1);
vtkm::Float32 y = (x*x + z*z)/2.0f;
coordinates.push_back(vtkm::make_Vec(x,y,z));
}
}
vtkm::Id numCells = (dim - 1) * (dim - 1);
dataSet.AddCoordinateSystem(
vtkm::cont::CoordinateSystem("coordinates", 1, coordinates));
vtkm::cont::CellSetExplicit<> cellSet("cells", 3);
cellSet.PrepareToAddCells(numCells, numCells * 4);
for (vtkm::Id j = 0; j < dim - 1; ++j)
{
for (vtkm::Id i = 0; i < dim - 1; ++i)
{
cellSet.AddCell(vtkm::VTKM_QUAD,
4,
vtkm::make_Vec<vtkm::Id>(j * dim + i,
j * dim + i + 1,
(j + 1) * dim + i + 1,
(j + 1) * dim + i));
}
}
cellSet.CompleteAddingCells();
dataSet.AddCellSet(cellSet);
return dataSet;
}
}
void TestPointElevation()
{
std::cout << "Testing PointElevation Worklet" << std::endl;
vtkm::cont::DataSet dataSet = MakePointElevationTestDataSet();
dataSet.AddField(vtkm::cont::Field("elevation", 1, vtkm::cont::Field::ASSOC_POINTS,
vtkm::Float32()));
vtkm::worklet::PointElevation pointElevationWorklet;
pointElevationWorklet.SetLowPoint(vtkm::make_Vec<vtkm::Float64>(0.0, 0.0, 0.0));
pointElevationWorklet.SetHighPoint(vtkm::make_Vec<vtkm::Float64>(0.0, 1.0, 0.0));
pointElevationWorklet.SetRange(0.0, 2.0);
vtkm::worklet::DispatcherMapField<vtkm::worklet::PointElevation>
dispatcher(pointElevationWorklet);
dispatcher.Invoke(dataSet.GetCoordinateSystem().GetData(),
dataSet.GetField("elevation").GetData());
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::Float32,3> > coordinates =
dataSet.GetCoordinateSystem().GetData().
CastToArrayHandle(vtkm::Vec<vtkm::Float32,3>(),VTKM_DEFAULT_STORAGE_TAG());
vtkm::cont::ArrayHandle<vtkm::Float32> result =
dataSet.GetField("elevation").GetData().
CastToArrayHandle(vtkm::Float32(),VTKM_DEFAULT_STORAGE_TAG());
for (vtkm::Id i = 0; i < result.GetNumberOfValues(); ++i)
{
VTKM_TEST_ASSERT(test_equal(coordinates.GetPortalConstControl().Get(i)[1] * 2.0,
result.GetPortalConstControl().Get(i)),
"Wrong result for PointElevation worklet");
}
}
int UnitTestPointElevation(int, char *[])
{
return vtkm::cont::testing::Testing::Run(TestPointElevation);
}