A large Merge request is about to land in VTK master that changes several core parts of VTK quite a bit. Any feedback would be more than welcome.
vtkCellArray: Support offsets/connectivity as vtkDataArray including vtkAffineArray for Offsets
The vtkCellArray
used to be able to support storing connectivity and offsets that are either vtkTypedInt32Array
or
vtkTypedInt64Array
. This was limiting because it did not allow using other vtkDataArray
subclasses, therefore,
many times a copy to the supported types was needed.
A user can now store the connectivity and offsets as any vtkDataArray
subclass using
vtkCellArray::SetData(vtkDataArray* offsets, vtkDataArray* conn)
. This is useful because it allows
storing any user given array without worrying whether VTK will deep copy the data arrays or not.
vtkCellArray
used to have 2 storage types but now has the following 5 :
Int64
(Old): OffsetsvtkTypeInt64Array
, ConnectivityvtkTypeInt64Array
Int32
(Old): OffsetsvtkTypeInt32Array
, ConnectivityvtkTypeInt32Array
FixedSizeInt32
(New): OffsetsvtkAffineArray<vtkTypeInt32>
, ConnectivityvtkTypeInt32Array
FixedSizeInt64
(New): OffsetsvtkAffineArray<vtkTypeInt64>
, ConnectivityvtkTypeInt64Array
Generic
(New): OffsetsvtkDataArray
, ConnectivityvtkDataArray
vtkAffineArray
has become a first class citizen in vtkCellArray
to support creating a cell array with cells
of constant size without needing to store the offsets explicitly, and therefore saving memory.
The following functions can be used to interact with the new storage types:
bool IsStorage64Bit()
bool IsStorage32Bit()
bool IsStorageFixedSize64Bit()
bool IsStorageFixedSize32Bit()
bool IsStorageFixedSize()
bool IsStorageGeneric()
void Use64BitStorage()
void Use32BitStorage()
void UseFixedSize32BitStorage(int size)
void UseFixedSize64BitStorage(int size)
bool CanConvertTo64BitStorage()
bool CanConvertTo32BitStorage()
bool CanConvertToDefaultStorage()
bool CanConvertToFixedSize32BitStorage()
bool CanConvertToFixedSize64BitStorage()
bool CanConvertToFixedSizeStorage()
void ConvertTo64BitStorage()
void ConvertTo32BitStorage()
void ConvertToDefaultStorage()
void ConvertToFixedSize64BitStorage()
void ConvertToFixedSize32BitStorage()
void ConvertToFixedSizeStorage()
bool ConvertToStorageType(int type)
vtkTypeInt64Array* GetOffsetsArray64()
vtkTypeInt32Array* GetOffsetsArray32()
vtkAffineArray<vtkTypeInt32>* GetOffsetsAffineArray32()
vtkAffineArray<vtkTypeInt64>* GetOffsetsAffineArray64()
vtkDataArray* GetOffsetsArray()
vtkTypeInt64Array* GetConnectivityArray64()
vtkTypeInt32Array* GetConnectivityArray32()
vtkDataArray* GetConnectivityArray()
int GetStorageType()
When the storage type is Int64
, Int32
, FixedSizeInt64
or FixedSizeInt32
then direct access to the array is
available through the Visit
functor. When the storage type is Generic
, then the Visit
functor will provide the
arrays as vtkDataArray
, therefore the user can no longer assume the arrays will be a subclass of
vtkAOSDataArrayTemplate
. To solve this problem, the array access calls shown in the following Visit
functor:
struct MyVisitor
{
template <typename CellStateT>
void operator()(CellStateT& state)
{
using ArrayType = typename CellStateT::ArrayType;
using ValueType = typename CellStateT::ValueType;
auto* conn = state.GetConnectivity();
auto connRange = vtk::DataArrayValueRange<1>(conn);
ValueType connValue = conn->GetValue(0);
conn->SetValue(0, connValue);
ValueType* connPtr = conn->GetPointer(0);
conn->InsertNextValue(connValue);
vtkIdType numCells = state.GetNumberOfCells();
vtkIdType beginOffset = state.GetBeginOffset(0);
vtkIdType endOffset = state.GetEndOffset(0);
vtkIdType cellSize = state.GetCellSize(0);
auto cellRange = state.GetCellRange(0);
}
};
cells->Visit(MyVisitor{});
should be replaced with:
struct MyVisitor : public vtkCellArray::DispatchUtilities
{
template <class OffsetsT, class ConnectivityT>
void operator()(OffsetsT* offsets, ConnectivityT* conn)
{
using AccessorType = vtkDataArrayAccessor<ConnectivityT>;
using ValueType = GetAPIType<ConnectivityT>; // The result of GetAPIType<OffsetsT> is the same
auto connRange = GetRange(conn); // or vtk::DataArrayValueRange<1, vtkIdType>(conn)
ValueType connValue = connRange[0];
connRange[0] = connValue;
auto connPtr = connRange.begin();
AccessorType accessor(conn);
accessor.InsertNext(connValue);
vtkIdType numCells = GetNumberOfCells(offsets);
vtkIdType beginOffset = GetBeginOffset(offsets, 0);
vtkIdType endOffset = GetEndOffset(offsets, 0);
vtkIdType cellSize = GetCellSize(offsets, 0);
auto cellRange = GetCellRange(offsets, conn, 0);
}
};
cells->Dispatch(MyVisitor{});
// or also
if (!vtkArrayDispatch::Dispatch2ByArrayWithSameValueType<vtkCellArray::StorageOffsetsArrays,
vtkCellArray::StorageConnectivityArrays>::Execute(cells->GetOffsetsArray(),
cells->GetConnectivityArray(), MyVisitor{}))
{
MyVisitor{}(cells->GetOffsetsArray(), cells->GetConnectivityArray());
}
Due to this new requirement, all Visit
functors in VTK have been updated to use the new Dispatch
API.
It should be noted that due to this change, offsets and connectivity arrays generated by a VTKm filter can now also
be stored in vtkCellArray
as vtkmDataArray
without needing to copy the data to vtkTypeInt32Array
or
vtkTypeInt64Array
.
Additionally, the majority of VTK filters that were generating constant size cells have been updated to use
FixedSizeInt32/64
storage type to save memory.
Finally, the following methods that are associated with the legacy format of vtkCellArray
have been deprecated:
vtkTypeBool Allocate(vtkIdType sz, vtkIdType vtkNotUsed(ext) = 1000)
virtual void SetNumberOfCells(vtkIdType)
vtkIdType EstimateSize(vtkIdType numCells, int maxPtsPerCell)
vtkIdType GetSize()
vtkIdType GetNumberOfConnectivityEntries()
void GetCell(vtkIdType loc, vtkIdType& npts, const vtkIdType*& pts)
void GetCell(vtkIdType loc, vtkIdList* pts)
vtkIdType GetInsertLocation(int npts)
vtkIdType GetTraversalLocation()
vtkIdType GetTraversalLocation(vtkIdType npts)
void SetTraversalLocation(vtkIdType loc)
void ReverseCell(vtkIdType loc)
void ReplaceCell(vtkIdType loc, int npts, const vtkIdType pts[])
void SetCells(vtkIdType ncells, vtkIdTypeArray* cells)
vtkIdTypeArray* GetData()
vtkDataSet: GetCellTypes updates
vtkDataSet
’s method void GetCellTypes(vtkCellTypes* types)
, has been deprecated in favor of
void GetDistinctCellTypes(vtkCellTypes* types)
.
vtkCartesianGrid
subclasses (vtkImageData
, vtkRectilinearGrid
) and vtkStructuredGrid
’s method
vtkConstantArray<int>* GetCellTypesArray()
has been deprecated in favor of vtkConstantUnsignedCharArray GetCellTypes()
vtkUnstructuredGrid
’s method vtkUnsignedCharArray* GetCellTypesArray()
has been deprecated in favor of
vtkDataArray* GetCellTypes()
, which is templated to allow returning other arrays, such as
vtkUnsignedCharArray* GetCellTypes<vtkUnsignedCharArray>()
or
vtkConstantUnsignedCharArray* GetCellTypes<vtkConstantUnsignedCharArray>()
.
This was done to enable storing the cell types of meshes with only 1 cell type as vtkConstantUnsignedCharArray
instead
of vtkUnsignedCharArray
, which saves memory.
It should be noted that due to this change, a cell types array generated by a VTKm filter can now also be stored in
vtkUnstructuredGrid
as vtkmDataArray
without needing to copy the data to vtkUnsignedCharArray
.
Additionally, the majority of VTK filters that were generating single cell type cells have been updated to use vtkConstantUnsignedCharArray
to save memory.