vtk-m/vtkm/cont/Field.h

403 lines
11 KiB
C
Raw Normal View History

2015-02-10 17:36:10 +00:00
//============================================================================
// 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.
2015-02-10 17:36:10 +00:00
//
// 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.
//============================================================================
#ifndef vtk_m_cont_Field_h
#define vtk_m_cont_Field_h
2015-02-10 17:36:10 +00:00
#include <vtkm/cont/vtkm_cont_export.h>
#include <vtkm/Range.h>
#include <vtkm/Types.h>
2015-07-16 19:10:01 +00:00
2015-02-10 17:36:10 +00:00
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/ArrayPortalToIterators.h>
2017-05-18 14:51:24 +00:00
#include <vtkm/cont/ArrayRangeCompute.h>
2015-02-10 17:36:10 +00:00
#include <vtkm/cont/DynamicArrayHandle.h>
2017-05-18 14:29:41 +00:00
namespace vtkm
{
namespace cont
{
2015-02-10 17:36:10 +00:00
2017-05-18 14:29:41 +00:00
namespace internal
{
2015-07-16 19:10:01 +00:00
class ComputeRange
2015-07-16 19:10:01 +00:00
{
public:
2017-05-18 14:29:41 +00:00
ComputeRange(ArrayHandle<vtkm::Range>& range)
: Range(&range)
{
}
2015-07-16 19:10:01 +00:00
2017-05-18 14:29:41 +00:00
template <typename ArrayHandleType>
void operator()(const ArrayHandleType& input) const
{
*this->Range = vtkm::cont::ArrayRangeCompute(input);
2015-07-16 19:10:01 +00:00
}
private:
2017-05-18 14:29:41 +00:00
vtkm::cont::ArrayHandle<vtkm::Range>* Range;
2015-07-16 19:10:01 +00:00
};
} // namespace internal
2015-02-10 17:36:10 +00:00
/// A \c Field encapsulates an array on some piece of the mesh, such as
/// the points, a cell set, a point logical dimension, or the whole mesh.
2015-02-10 17:36:10 +00:00
///
class VTKM_CONT_EXPORT Field
2015-02-10 17:36:10 +00:00
{
public:
enum AssociationEnum
{
ASSOC_ANY,
ASSOC_WHOLE_MESH,
ASSOC_POINTS,
ASSOC_CELL_SET,
ASSOC_LOGICAL_DIM
};
/// constructors for points / whole mesh
VTKM_CONT
2017-05-18 14:29:41 +00:00
Field(std::string name, AssociationEnum association, const vtkm::cont::DynamicArrayHandle& data)
: Name(name)
, Association(association)
, AssocCellSetName()
, AssocLogicalDim(-1)
, Data(data)
, Range()
, ModifiedFlag(true)
2015-07-30 14:22:25 +00:00
{
2017-05-18 14:29:41 +00:00
VTKM_ASSERT(this->Association == ASSOC_WHOLE_MESH || this->Association == ASSOC_POINTS);
2015-07-30 14:22:25 +00:00
}
2017-05-18 14:29:41 +00:00
template <typename T, typename Storage>
VTKM_CONT Field(std::string name, AssociationEnum association,
const ArrayHandle<T, Storage>& data)
: Name(name)
, Association(association)
, AssocCellSetName()
, AssocLogicalDim(-1)
, Data(data)
, Range()
, ModifiedFlag(true)
2015-05-11 20:15:58 +00:00
{
2017-05-18 14:29:41 +00:00
VTKM_ASSERT((this->Association == ASSOC_WHOLE_MESH) || (this->Association == ASSOC_POINTS));
2015-05-11 20:15:58 +00:00
}
template <typename T>
2017-05-18 14:29:41 +00:00
VTKM_CONT Field(std::string name, AssociationEnum association, const std::vector<T>& data)
: Name(name)
, Association(association)
, AssocCellSetName()
, AssocLogicalDim(-1)
, Range()
, ModifiedFlag(true)
{
2017-05-18 14:29:41 +00:00
VTKM_ASSERT((this->Association == ASSOC_WHOLE_MESH) || (this->Association == ASSOC_POINTS));
this->CopyData(&data[0], static_cast<vtkm::Id>(data.size()));
}
template <typename T>
2017-05-18 14:29:41 +00:00
VTKM_CONT Field(std::string name, AssociationEnum association, const T* data, vtkm::Id nvals)
: Name(name)
, Association(association)
, AssocCellSetName()
, AssocLogicalDim(-1)
, Range()
, ModifiedFlag(true)
{
2017-05-18 14:29:41 +00:00
VTKM_ASSERT((this->Association == ASSOC_WHOLE_MESH) || (this->Association == ASSOC_POINTS));
this->CopyData(data, nvals);
}
/// constructors for cell set associations
VTKM_CONT
2017-05-18 14:29:41 +00:00
Field(std::string name, AssociationEnum association, const std::string& cellSetName,
const vtkm::cont::DynamicArrayHandle& data)
: Name(name)
, Association(association)
, AssocCellSetName(cellSetName)
, AssocLogicalDim(-1)
, Data(data)
, Range()
, ModifiedFlag(true)
{
VTKM_ASSERT(this->Association == ASSOC_CELL_SET);
}
2015-07-30 14:22:25 +00:00
template <typename T, typename Storage>
2017-05-18 14:29:41 +00:00
VTKM_CONT Field(std::string name, AssociationEnum association, const std::string& cellSetName,
const vtkm::cont::ArrayHandle<T, Storage>& data)
: Name(name)
, Association(association)
, AssocCellSetName(cellSetName)
, AssocLogicalDim(-1)
, Data(data)
, Range()
, ModifiedFlag(true)
2015-05-11 20:15:58 +00:00
{
VTKM_ASSERT(this->Association == ASSOC_CELL_SET);
2015-05-11 20:15:58 +00:00
}
template <typename T>
2017-05-18 14:29:41 +00:00
VTKM_CONT Field(std::string name, AssociationEnum association, const std::string& cellSetName,
const std::vector<T>& data)
: Name(name)
, Association(association)
, AssocCellSetName(cellSetName)
, AssocLogicalDim(-1)
, Range()
, ModifiedFlag(true)
{
VTKM_ASSERT(this->Association == ASSOC_CELL_SET);
this->CopyData(&data[0], static_cast<vtkm::Id>(data.size()));
}
template <typename T>
2017-05-18 14:29:41 +00:00
VTKM_CONT Field(std::string name, AssociationEnum association, const std::string& cellSetName,
const T* data, vtkm::Id nvals)
: Name(name)
, Association(association)
, AssocCellSetName(cellSetName)
, AssocLogicalDim(-1)
, Range()
, ModifiedFlag(true)
{
VTKM_ASSERT(this->Association == ASSOC_CELL_SET);
this->CopyData(data, nvals);
}
/// constructors for logical dimension associations
VTKM_CONT
2017-05-18 14:29:41 +00:00
Field(std::string name, AssociationEnum association, vtkm::IdComponent logicalDim,
const vtkm::cont::DynamicArrayHandle& data)
: Name(name)
, Association(association)
, AssocCellSetName()
, AssocLogicalDim(logicalDim)
, Data(data)
, Range()
, ModifiedFlag(true)
{
VTKM_ASSERT(this->Association == ASSOC_LOGICAL_DIM);
}
2015-07-30 14:22:25 +00:00
template <typename T, typename Storage>
2017-05-18 14:29:41 +00:00
VTKM_CONT Field(std::string name, AssociationEnum association, vtkm::IdComponent logicalDim,
const vtkm::cont::ArrayHandle<T, Storage>& data)
: Name(name)
, Association(association)
, AssocLogicalDim(logicalDim)
, Data(data)
, Range()
, ModifiedFlag(true)
2015-05-11 20:15:58 +00:00
{
VTKM_ASSERT(this->Association == ASSOC_LOGICAL_DIM);
2015-05-11 20:15:58 +00:00
}
template <typename T>
2017-05-18 14:29:41 +00:00
VTKM_CONT Field(std::string name, AssociationEnum association, vtkm::IdComponent logicalDim,
const std::vector<T>& data)
: Name(name)
, Association(association)
, AssocLogicalDim(logicalDim)
, Range()
, ModifiedFlag(true)
{
VTKM_ASSERT(this->Association == ASSOC_LOGICAL_DIM);
this->CopyData(&data[0], static_cast<vtkm::Id>(data.size()));
}
template <typename T>
2017-05-18 14:29:41 +00:00
VTKM_CONT Field(std::string name, AssociationEnum association, vtkm::IdComponent logicalDim,
const T* data, vtkm::Id nvals)
: Name(name)
, Association(association)
, AssocLogicalDim(logicalDim)
, Range()
, ModifiedFlag(true)
{
VTKM_ASSERT(this->Association == ASSOC_LOGICAL_DIM);
CopyData(data, nvals);
}
VTKM_CONT
Field()
2017-05-18 14:29:41 +00:00
: Name()
, Association(ASSOC_ANY)
, AssocCellSetName()
, AssocLogicalDim()
, Data()
, Range()
, ModifiedFlag(true)
{
//Generate an empty field
}
VTKM_CONT
2017-05-18 14:29:41 +00:00
Field& operator=(const vtkm::cont::Field& src) = default;
VTKM_CONT
2017-05-18 14:29:41 +00:00
const std::string& GetName() const { return this->Name; }
2015-05-19 20:21:15 +00:00
VTKM_CONT
2017-05-18 14:29:41 +00:00
AssociationEnum GetAssociation() const { return this->Association; }
2015-05-11 20:15:58 +00:00
VTKM_CONT
2017-05-18 14:29:41 +00:00
std::string GetAssocCellSet() const { return this->AssocCellSetName; }
2015-05-11 20:15:58 +00:00
VTKM_CONT
2017-05-18 14:29:41 +00:00
vtkm::IdComponent GetAssocLogicalDim() const { return this->AssocLogicalDim; }
2015-05-11 20:15:58 +00:00
2017-05-18 14:29:41 +00:00
template <typename TypeList, typename StorageList>
VTKM_CONT const vtkm::cont::ArrayHandle<vtkm::Range>& GetRange(TypeList, StorageList) const
2015-07-16 19:10:01 +00:00
{
VTKM_IS_LIST_TAG(TypeList);
VTKM_IS_LIST_TAG(StorageList);
2015-07-16 19:10:01 +00:00
return this->GetRangeImpl(TypeList(), StorageList());
2015-07-16 19:10:01 +00:00
}
VTKM_CONT
2017-05-18 14:29:41 +00:00
const vtkm::cont::ArrayHandle<vtkm::Range>& GetRange(VTKM_DEFAULT_TYPE_LIST_TAG,
VTKM_DEFAULT_STORAGE_LIST_TAG) const;
2017-05-18 14:29:41 +00:00
template <typename TypeList, typename StorageList>
VTKM_CONT void GetRange(vtkm::Range* range, TypeList, StorageList) const
2015-07-16 19:10:01 +00:00
{
VTKM_IS_LIST_TAG(TypeList);
VTKM_IS_LIST_TAG(StorageList);
this->GetRange(TypeList(), StorageList());
2015-07-16 19:10:01 +00:00
vtkm::Id length = this->Range.GetNumberOfValues();
2015-07-16 19:10:01 +00:00
for (vtkm::Id i = 0; i < length; ++i)
{
range[i] = this->Range.GetPortalConstControl().Get(i);
2015-07-16 19:10:01 +00:00
}
}
2017-05-18 14:29:41 +00:00
template <typename TypeList>
VTKM_CONT const vtkm::cont::ArrayHandle<vtkm::Range>& GetRange(TypeList) const
2015-07-16 19:10:01 +00:00
{
VTKM_IS_LIST_TAG(TypeList);
2017-05-18 14:29:41 +00:00
return this->GetRange(TypeList(), VTKM_DEFAULT_STORAGE_LIST_TAG());
2015-07-16 19:10:01 +00:00
}
2017-05-18 14:29:41 +00:00
template <typename TypeList>
VTKM_CONT void GetRange(vtkm::Range* range, TypeList) const
2015-07-16 19:10:01 +00:00
{
VTKM_IS_LIST_TAG(TypeList);
2017-05-18 14:29:41 +00:00
this->GetRange(range, TypeList(), VTKM_DEFAULT_STORAGE_LIST_TAG());
2015-07-16 19:10:01 +00:00
}
VTKM_CONT
const vtkm::cont::ArrayHandle<vtkm::Range>& GetRange() const;
2015-07-16 19:10:01 +00:00
VTKM_CONT
2017-05-18 14:29:41 +00:00
void GetRange(vtkm::Range* range) const;
2015-07-16 19:10:01 +00:00
2017-05-18 14:29:41 +00:00
const vtkm::cont::DynamicArrayHandle& GetData() const;
2015-07-30 14:22:25 +00:00
2017-05-18 14:29:41 +00:00
vtkm::cont::DynamicArrayHandle& GetData();
2015-02-10 17:36:10 +00:00
2015-05-11 20:15:58 +00:00
template <typename T>
2017-05-18 14:29:41 +00:00
VTKM_CONT void SetData(const vtkm::cont::ArrayHandle<T>& newdata)
{
this->Data = newdata;
this->ModifiedFlag = true;
}
VTKM_CONT
2017-05-18 14:29:41 +00:00
void SetData(const vtkm::cont::DynamicArrayHandle& newdata)
2015-02-10 17:36:10 +00:00
{
this->Data = newdata;
2015-07-16 19:10:01 +00:00
this->ModifiedFlag = true;
2015-02-10 17:36:10 +00:00
}
2015-05-19 20:21:15 +00:00
template <typename T>
2017-05-18 14:29:41 +00:00
VTKM_CONT void CopyData(const T* ptr, vtkm::Id nvals)
2015-02-10 17:36:10 +00:00
{
//allocate main memory using an array handle
vtkm::cont::ArrayHandle<T> tmp;
tmp.Allocate(nvals);
//copy into the memory owned by the array handle
2017-05-18 14:29:41 +00:00
std::copy(ptr, ptr + static_cast<std::size_t>(nvals),
vtkm::cont::ArrayPortalToIteratorBegin(tmp.GetPortalControl()));
//assign to the dynamic array handle
this->Data = tmp;
2015-07-16 19:10:01 +00:00
this->ModifiedFlag = true;
2015-02-10 17:36:10 +00:00
}
2015-05-20 19:26:10 +00:00
VTKM_CONT
2017-05-18 14:29:41 +00:00
virtual void PrintSummary(std::ostream& out) const;
2015-02-10 17:36:10 +00:00
private:
2017-05-18 14:29:41 +00:00
std::string Name; ///< name of field
2015-05-19 20:21:15 +00:00
2017-05-18 14:29:41 +00:00
AssociationEnum Association;
std::string AssocCellSetName; ///< only populate if assoc is cells
vtkm::IdComponent AssocLogicalDim; ///< only populate if assoc is logical dim
2015-05-11 20:15:58 +00:00
vtkm::cont::DynamicArrayHandle Data;
mutable vtkm::cont::ArrayHandle<vtkm::Range> Range;
2015-07-16 19:10:01 +00:00
mutable bool ModifiedFlag;
2017-05-18 14:29:41 +00:00
template <typename TypeList, typename StorageList>
VTKM_CONT const vtkm::cont::ArrayHandle<vtkm::Range>& GetRangeImpl(TypeList, StorageList) const
{
VTKM_IS_LIST_TAG(TypeList);
VTKM_IS_LIST_TAG(StorageList);
if (this->ModifiedFlag)
{
internal::ComputeRange computeRange(this->Range);
2017-05-18 14:29:41 +00:00
this->Data.ResetTypeAndStorageLists(TypeList(), StorageList()).CastAndCall(computeRange);
this->ModifiedFlag = false;
}
return this->Range;
}
2015-02-10 17:36:10 +00:00
};
2017-05-18 14:29:41 +00:00
template <typename Functor>
void CastAndCall(const vtkm::cont::Field& field, const Functor& f)
{
field.GetData().CastAndCall(f);
}
2017-05-18 14:29:41 +00:00
namespace internal
{
template <>
struct DynamicTransformTraits<vtkm::cont::Field>
{
typedef vtkm::cont::internal::DynamicTransformTagCastAndCall DynamicTag;
};
2015-02-10 17:36:10 +00:00
} // namespace internal
2015-02-10 17:36:10 +00:00
} // namespace cont
} // namespace vtkm
#endif //vtk_m_cont_Field_h