Merge topic 'indexedArrays'

5b4f6f64b7 review(IndexedArray): enforce final for backend + optimization
bd2f9b2d6a add(changelog): for `vtkIndexedArrays`
ec20199665 add(IndexedArray): data array based on `vtkIndexedImplicitBackend`
6a49775c3f add(IndexedBackend): implementation of indexed backend

Acked-by: Kitware Robot <kwrobot@kitware.com>
Tested-by: buildbot <buildbot@kitware.com>
Merge-request: !9703
This commit is contained in:
Julien Fausty 2022-11-30 17:46:12 +00:00 committed by Kitware Robot
commit d638614c6d
12 changed files with 757 additions and 0 deletions

View File

@ -1,11 +1,13 @@
option(VTK_DISPATCH_AFFINE_ARRAYS "Include implicit vtkDataArray subclasses based on an affine function backend in dispatcher" OFF)
option(VTK_DISPATCH_COMPOSITE_ARRAYS "Include implicit vtkDataArray subclasses based on a composite binary tree backend in dispatcher" OFF)
option(VTK_DISPATCH_CONSTANT_ARRAYS "Include implicit vtkDataArray subclasses based on a constant backend in dispatcher" OFF)
option(VTK_DISPATCH_INDEXED_ARRAYS "Include implicit vtkDataArray subclasses based on an index referencing backend in dispatcher" OFF)
option(VTK_DISPATCH_STD_FUNCTION_ARRAYS "Include implicit vtkDataArray subclasses based on std::function in dispatcher" OFF)
mark_as_advanced(
VTK_DISPATCH_AFFINE_ARRAYS
VTK_DISPATCH_COMPOSITE_ARRAYS
VTK_DISPATCH_CONSTANT_ARRAYS
VTK_DISPATCH_INDEXED_ARRAYS
VTK_DISPATCH_STD_FUNCTION_ARRAYS
)
@ -36,6 +38,8 @@ foreach (INSTANTIATION_VALUE_TYPE IN LISTS vtkArrayDispatchImplicit_all_types)
list(APPEND _list "vtkCompositeArrayInstantiate")
list(APPEND _list "vtkCompositeImplicitBackendInstantiate")
list(APPEND _list "vtkConstantArrayInstantiate")
list(APPEND _list "vtkIndexedArrayInstantiate")
list(APPEND _list "vtkIndexedImplicitBackendInstantiate")
list(APPEND _list "vtkStdFunctionArrayInstantiate")
# generate cxx file to instantiate template with this type
@ -56,6 +60,7 @@ set(nowrap_headers
vtkConstantArray.h
vtkConstantImplicitBackend.h
vtkImplicitArrayTraits.h
vtkIndexedArray.h
vtkStdFunctionArray.h
"${CMAKE_CURRENT_BINARY_DIR}/vtkVTK_DISPATCH_IMPLICIT_ARRAYS.h"
"${CMAKE_CURRENT_BINARY_DIR}/vtkArrayDispatchImplicitArrayList.h"
@ -64,6 +69,7 @@ set(nowrap_headers
set(nowrap_template_classes
vtkImplicitArray
vtkCompositeImplicitBackend
vtkIndexedImplicitBackend
)
set(sources

View File

@ -6,6 +6,8 @@ vtk_add_test_cxx(vtkCommonImplicitArrayCxxTests tests
TestConstantArray.cxx
TestImplicitArraysBase.cxx
TestImplicitArrayTraits.cxx
TestIndexedArray.cxx
TestIndexedImplicitBackend.cxx
TestStdFunctionArray.cxx
)

View File

@ -0,0 +1,133 @@
/*=========================================================================
Program: Visualization Toolkit
Module: TestIndexedArray.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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.
=========================================================================*/
#include "vtkIndexedArray.h"
#include "vtkDataArrayRange.h"
#include "vtkIdList.h"
#include "vtkIntArray.h"
#include "vtkVTK_DISPATCH_IMPLICIT_ARRAYS.h"
#ifdef VTK_DISPATCH_INDEXED_ARRAYS
#include "vtkArrayDispatch.h"
#include "vtkArrayDispatchImplicitArrayList.h"
#endif // VTK_DISPATCH_INDEXED_ARRAYS
#include <cstdlib>
#include <numeric>
#include <random>
#ifdef VTK_DISPATCH_INDEXED_ARRAYS
namespace
{
struct ScaleWorker
{
template <typename SrcArray, typename DstArray>
void operator()(SrcArray* srcArr, DstArray* dstArr, double scale)
{
using SrcType = vtk::GetAPIType<SrcArray>;
using DstType = vtk::GetAPIType<DstArray>;
const auto srcRange = vtk::DataArrayValueRange(srcArr);
auto dstRange = vtk::DataArrayValueRange(dstArr);
if (srcRange.size() != dstRange.size())
{
std::cout << "Different array sizes in ScaleWorker" << std::endl;
return;
}
auto dstIter = dstRange.begin();
for (SrcType srcVal : srcRange)
{
*dstIter++ = static_cast<DstType>(srcVal * scale);
}
}
};
}
#endif // VTK_DISPATCH_INDEXED_ARRAYS
int TestIndexedArray(int vtkNotUsed(argc), char* vtkNotUsed(argv)[])
{
int res = EXIT_SUCCESS;
vtkNew<vtkIntArray> baseArray;
baseArray->SetNumberOfComponents(1);
baseArray->SetNumberOfTuples(1000);
auto range = vtk::DataArrayValueRange<1>(baseArray);
std::iota(range.begin(), range.end(), 0);
vtkNew<vtkIdList> handles;
handles->SetNumberOfIds(100);
std::random_device randdev;
std::mt19937 generator(randdev());
auto index_rand = std::bind(std::uniform_int_distribution<vtkIdType>(0, 999), generator);
for (vtkIdType idx = 0; idx < 100; idx++)
{
handles->SetId(idx, index_rand());
}
vtkNew<vtkIndexedArray<int>> indexed;
indexed->SetBackend(std::make_shared<vtkIndexedImplicitBackend<int>>(handles, baseArray));
indexed->SetNumberOfComponents(1);
indexed->SetNumberOfTuples(100);
for (vtkIdType iArr = 0; iArr < 100; iArr++)
{
if (indexed->GetValue(iArr) != static_cast<int>(handles->GetId(iArr)))
{
res = EXIT_FAILURE;
std::cout << "get value failed with vtkIndexedArray" << std::endl;
}
}
int iArr = 0;
for (auto val : vtk::DataArrayValueRange<1>(indexed))
{
if (val != static_cast<int>(handles->GetId(iArr)))
{
res = EXIT_FAILURE;
std::cout << "range iterator failed with vtkIndexedArray" << std::endl;
}
iArr++;
}
#ifdef VTK_DISPATCH_INDEXED_ARRAYS
std::cout << "vtkIndexedArray: performing dispatch tests" << std::endl;
vtkNew<vtkIntArray> destination;
destination->SetNumberOfTuples(100);
destination->SetNumberOfComponents(1);
using Dispatcher =
vtkArrayDispatch::Dispatch2ByArray<vtkArrayDispatch::ReadOnlyArrays, vtkArrayDispatch::Arrays>;
::ScaleWorker worker;
if (!Dispatcher::Execute(indexed, destination, worker, 3.0))
{
res = EXIT_FAILURE;
std::cout << "vtkArrayDispatch failed with vtkIndexedArray" << std::endl;
worker(indexed.Get(), destination.Get(), 3.0);
}
iArr = 0;
for (auto val : vtk::DataArrayValueRange<1>(destination))
{
if (val != 3 * static_cast<int>(handles->GetId(iArr)))
{
res = EXIT_FAILURE;
std::cout << "dispatch failed to populate the array with the correct values" << std::endl;
}
iArr++;
}
#endif // VTK_DISPATCH_INDEXED_ARRAYS
return res;
};

View File

@ -0,0 +1,169 @@
/*=========================================================================
Program: Visualization Toolkit
Module: TestIndexedImplicitBackend.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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.
=========================================================================*/
#include "vtkIndexedImplicitBackend.h"
#include "vtkDataArrayRange.h"
#include "vtkIdList.h"
#include "vtkIntArray.h"
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <random>
namespace
{
int LoopAndTest(vtkIdList* handles, vtkIndexedImplicitBackend<int>& backend)
{
for (int idx = 0; idx < handles->GetNumberOfIds(); idx++)
{
if (backend(idx) != static_cast<int>(handles->GetId(idx)))
{
std::cout << "Indexed backend evaluation failed with: " << backend(idx)
<< " != " << handles->GetId(idx) << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
int TestWithIDList()
{
vtkNew<vtkIntArray> baseArray;
baseArray->SetNumberOfComponents(1);
baseArray->SetNumberOfTuples(100);
auto range = vtk::DataArrayValueRange<1>(baseArray);
std::iota(range.begin(), range.end(), 0);
vtkNew<vtkIdList> handles;
handles->SetNumberOfIds(100);
{
std::vector<vtkIdType> buffer(100);
std::iota(buffer.begin(), buffer.end(), 0);
std::random_device randdev;
std::mt19937 generator(randdev());
std::shuffle(buffer.begin(), buffer.end(), generator);
for (vtkIdType idx = 0; idx < 100; idx++)
{
handles->SetId(idx, buffer[idx]);
}
}
vtkIndexedImplicitBackend<int> backend(handles, baseArray);
int res = LoopAndTest(handles, backend);
vtkNew<vtkIntArray> baseMultiArray;
baseMultiArray->SetNumberOfComponents(3);
baseMultiArray->SetNumberOfTuples(100);
auto multiRange = vtk::DataArrayValueRange<3>(baseMultiArray);
std::iota(multiRange.begin(), multiRange.end(), 0);
vtkNew<vtkIdList> multiHandles;
multiHandles->SetNumberOfIds(300);
{
std::vector<vtkIdType> buffer(100);
std::iota(buffer.begin(), buffer.end(), 0);
std::random_device randdev;
std::mt19937 generator(randdev());
std::shuffle(buffer.begin(), buffer.end(), generator);
for (vtkIdType idx = 0; idx < 100; idx++)
{
for (vtkIdType comp = 0; comp < 3; comp++)
{
multiHandles->SetId(3 * idx + comp, 3 * buffer[idx] + comp);
}
}
}
vtkIndexedImplicitBackend<int> multiBackend(multiHandles, baseMultiArray);
return LoopAndTest(multiHandles, multiBackend) == EXIT_SUCCESS ? res : EXIT_FAILURE;
}
int LoopAndTest(vtkIntArray* handles, vtkIndexedImplicitBackend<int>& backend)
{
for (int idx = 0; idx < handles->GetNumberOfTuples(); idx++)
{
if (backend(idx) != static_cast<int>(handles->GetValue(idx)))
{
std::cout << "Indexed backend evaluation failed with: " << backend(idx)
<< " != " << handles->GetValue(idx) << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
int TestWithDataArrayIndexing()
{
vtkNew<vtkIntArray> baseArray;
baseArray->SetNumberOfComponents(1);
baseArray->SetNumberOfTuples(100);
auto range = vtk::DataArrayValueRange<1>(baseArray);
std::iota(range.begin(), range.end(), 0);
vtkNew<vtkIntArray> handles;
handles->SetNumberOfComponents(1);
handles->SetNumberOfTuples(100);
{
std::vector<int> buffer(100);
std::iota(buffer.begin(), buffer.end(), 0);
std::random_device randdev;
std::mt19937 generator(randdev());
std::shuffle(buffer.begin(), buffer.end(), generator);
auto handleRange = vtk::DataArrayValueRange<1>(handles);
std::copy(buffer.begin(), buffer.end(), handleRange.begin());
}
vtkIndexedImplicitBackend<int> backend(handles, baseArray);
int res = LoopAndTest(handles, backend);
// and with multi component entries
vtkNew<vtkIntArray> baseMultiArray;
baseMultiArray->SetNumberOfComponents(3);
baseMultiArray->SetNumberOfTuples(100);
auto multiRange = vtk::DataArrayValueRange<3>(baseMultiArray);
std::iota(multiRange.begin(), multiRange.end(), 0);
vtkNew<vtkIntArray> multiHandles;
multiHandles->SetNumberOfComponents(1);
multiHandles->SetNumberOfTuples(300);
{
std::vector<vtkIdType> buffer(100);
std::iota(buffer.begin(), buffer.end(), 0);
std::random_device randdev;
std::mt19937 generator(randdev());
std::shuffle(buffer.begin(), buffer.end(), generator);
for (vtkIdType idx = 0; idx < 100; idx++)
{
for (vtkIdType comp = 0; comp < 3; comp++)
{
multiHandles->SetValue(3 * idx + comp, 3 * buffer[idx] + comp);
}
}
}
vtkIndexedImplicitBackend<int> multiBackend(multiHandles, baseMultiArray);
return LoopAndTest(multiHandles, multiBackend) == EXIT_SUCCESS ? res : EXIT_FAILURE;
}
}
int TestIndexedImplicitBackend(int, char*[])
{
int res = ::TestWithIDList();
return ::TestWithDataArrayIndexing() == EXIT_SUCCESS ? res : EXIT_FAILURE;
}

View File

@ -137,6 +137,14 @@ if (VTK_DISPATCH_COMPOSITE_ARRAYS)
)
endif()
if (VTK_DISPATCH_INDEXED_ARRAYS)
list(APPEND vtkArrayDispatchImplicit_containers vtkIndexedArray)
set(vtkArrayDispatchImplicit_vtkIndexedArray_header vtkIndexedArray.h)
set(vtkArrayDispatchImplicit_vtkIndexedArray_types
${vtkArrayDispatchImplicit_all_types}
)
endif()
endmacro()
# Create a header that declares the vtkArrayDispatch::Arrays TypeList.

View File

@ -316,6 +316,8 @@ template <typename ValueType>
class vtkCompositeImplicitBackend;
template <typename ValueType>
struct vtkConstantImplicitBackend;
template <typename ValueType>
class vtkIndexedImplicitBackend;
VTK_ABI_NAMESPACE_END
#include <functional>
@ -337,6 +339,8 @@ VTK_ABI_NAMESPACE_END
vtkImplicitArray<vtkCompositeImplicitBackend<ValueType>>, ValueType) \
VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE( \
vtkImplicitArray<vtkConstantImplicitBackend<ValueType>>, ValueType) \
VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE( \
vtkImplicitArray<vtkIndexedImplicitBackend<ValueType>>, ValueType) \
VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<std::function<ValueType(int)>>, ValueType)
#elif defined(VTK_USE_EXTERN_TEMPLATE) // VTK_IMPLICIT_VALUERANGE_INSTANTIATING
@ -357,6 +361,8 @@ template <typename ValueType>
class vtkCompositeImplicitBackend;
template <typename ValueType>
struct vtkConstantImplicitBackend;
template <typename ValueType>
class vtkIndexedImplicitBackend;
VTK_ABI_NAMESPACE_END
#include <functional>
@ -391,6 +397,8 @@ VTK_ABI_NAMESPACE_END
vtkImplicitArray<vtkCompositeImplicitBackend<ValueType>>, ValueType) \
VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
vtkImplicitArray<vtkConstantImplicitBackend<ValueType>>, ValueType) \
VTK_DECLARE_VALUERANGE_ARRAYTYPE( \
vtkImplicitArray<vtkIndexedImplicitBackend<ValueType>>, ValueType) \
VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<std::function<ValueType(int)>>, ValueType)
#define VTK_DECLARE_VALUERANGE_IMPLICIT_BACKENDTYPE(BackendT) \
@ -414,6 +422,7 @@ VTK_ABI_NAMESPACE_BEGIN
VTK_DECLARE_VALUERANGE_IMPLICIT_BACKENDTYPE(vtkAffineImplicitBackend)
VTK_DECLARE_VALUERANGE_IMPLICIT_BACKENDTYPE(vtkConstantImplicitBackend)
VTK_DECLARE_VALUERANGE_IMPLICIT_BACKENDTYPE(vtkCompositeImplicitBackend)
VTK_DECLARE_VALUERANGE_IMPLICIT_BACKENDTYPE(vtkIndexedImplicitBackend)
VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<std::function<float(int)>>, double)
VTK_DECLARE_VALUERANGE_ARRAYTYPE(vtkImplicitArray<std::function<double(int)>>, double)

View File

@ -0,0 +1,90 @@
/*=========================================================================
Program: Visualization Toolkit
Module: vtkIndexedArray.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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.
=========================================================================*/
#ifndef vtkIndexedArray_h
#define vtkIndexedArray_h
#ifdef VTK_INDEXED_ARRAY_INSTANTIATING
#define VTK_IMPLICIT_VALUERANGE_INSTANTIATING
#include "vtkDataArrayPrivate.txx"
#endif
#include "vtkCommonImplicitArraysModule.h" // for export macro
#include "vtkImplicitArray.h"
#include "vtkIndexedImplicitBackend.h" // for the array backend
#ifdef VTK_INDEXED_ARRAY_INSTANTIATING
#undef VTK_IMPLICIT_VALUERANGE_INSTANTIATING
#endif
#include <vector>
/**
* \var vtkIndexedArray
* \brief A utility alias for creating a wrapper array around an existing array and reindexing its
* components
*
* In order to be usefully included in the dispatchers, these arrays need to be instantiated at the
* vtk library compile time.
*
* An example of potential usage:
* ```
* vtkNew<vtkIntArray> baseArray;
* baseArray->SetNumberOfComponents(1);
* baseArray->SetNumberOfTuples(100);
* auto range = vtk::DataArrayValueRange<1>(baseArray);
* std::iota(range.begin(), range.end(), 0);
*
* vtkNew<vtkIdList> handles;
* handles->SetNumberOfIds(100);
* for (vtkIdType idx = 0; idx < 100; idx++)
* {
* handles->SetId(idx, 99-idx);
* }
*
* vtkNew<vtkIndexed<int>> indexedArr;
* indexedArr->SetBackend(std::make_shared<vtkIndexedImplicitBackend<int>>(handles, baseArray));
* indexedArr->SetNumberOfComponents(1);
* indexedArr->SetNumberOfTuples(100);
* CHECK(indexedArr->GetValue(57) == 42); // always true
* ```
*
* @sa
* vtkImplicitArray vtkIndexedImplicitBackend
*/
VTK_ABI_NAMESPACE_BEGIN
template <typename T>
using vtkIndexedArray = vtkImplicitArray<vtkIndexedImplicitBackend<T>>;
VTK_ABI_NAMESPACE_END
#endif // vtkIndexedArray_h
#ifdef VTK_INDEXED_ARRAY_INSTANTIATING
#define VTK_INSTANTIATE_INDEXED_ARRAY(ValueType) \
VTK_ABI_NAMESPACE_BEGIN \
template class VTKCOMMONIMPLICITARRAYS_EXPORT \
vtkImplicitArray<vtkIndexedImplicitBackend<ValueType>>; \
VTK_ABI_NAMESPACE_END \
namespace vtkDataArrayPrivate \
{ \
VTK_ABI_NAMESPACE_BEGIN \
VTK_INSTANTIATE_VALUERANGE_ARRAYTYPE( \
vtkImplicitArray<vtkIndexedImplicitBackend<ValueType>>, double) \
VTK_ABI_NAMESPACE_END \
}
#endif

View File

@ -0,0 +1,18 @@
/*=========================================================================
Program: Visualization Toolkit
Module: vtkIndexedArray.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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.
=========================================================================*/
#define VTK_INDEXED_ARRAY_INSTANTIATING
#include "vtkIndexedArray.h"
VTK_INSTANTIATE_INDEXED_ARRAY(@INSTANTIATION_VALUE_TYPE@)

View File

@ -0,0 +1,97 @@
/*=========================================================================
Program: Visualization Toolkit
Module: vtkIndexedImplicitBackend.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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.
=========================================================================*/
#ifndef vtkIndexedImplicitBackend_h
#define vtkIndexedImplicitBackend_h
/**
* \class vtkIndexedImplicitBackend
*
* A backend for the `vtkImplicitArray` framework allowing one to use a subset of a given data
* array, by providing a `vtkIdList` or `vtkDataArray` of indexes as indirection, as another
* `vtkDataArray` without any excess memory consumption.
*
* This structure can be classified as a closure and can be called using syntax similar to a
* function call.
*
* An example of potential usage in a `vtkImplicitArray`:
* ```
* vtkNew<vtkIntArray> baseArray;
* baseArray->SetNumberOfComponents(1);
* baseArray->SetNumberOfTuples(100);
* auto range = vtk::DataArrayValueRange<1>(baseArray);
* std::iota(range.begin(), range.end(), 0);
*
* vtkNew<vtkIdList> handles;
* handles->SetNumberOfIds(100);
* for (vtkIdType idx = 0; idx < 100; idx++)
* {
* handles->SetId(idx, 99-idx);
* }
*
* vtkNew<vtkImplicitArray<vtkIndexedImplicitBackend<int>>> indexedArr; // More compact with
* // `vtkIndexedArray<int>`
* // if available
* indexedArr->SetBackend(std::make_shared<vtkIndexedImplicitBackend<int>>(handles, baseArray));
* indexedArr->SetNumberOfComponents(1);
* indexedArr->SetNumberOfTuples(100);
* CHECK(indexedArr->GetValue(57) == 42);
* ```
*
* @sa
* vtkImplicitArray, vtkIndexedArray
*/
#include "vtkCommonImplicitArraysModule.h"
#include <memory>
VTK_ABI_NAMESPACE_BEGIN
class vtkDataArray;
class vtkIdList;
template <typename ValueType>
class vtkIndexedImplicitBackend final
{
public:
///@{
/**
* Constructor
* @param indexes list of indexes to use for indirection of the array
* @param array base array of interest
*/
vtkIndexedImplicitBackend(vtkIdList* indexes, vtkDataArray* array);
vtkIndexedImplicitBackend(vtkDataArray* indexes, vtkDataArray* array);
///@}
~vtkIndexedImplicitBackend();
/**
* Indexing operation for the indexed array respecting the backend expectations of
* `vtkImplicitArray`
*/
ValueType operator()(int idx) const;
private:
struct Internals;
std::unique_ptr<Internals> Internal;
};
VTK_ABI_NAMESPACE_END
#endif // vtkIndexedImplicitBackend_h
#ifdef VTK_INDEXED_BACKEND_INSTANTIATING
#define VTK_INSTANTIATE_INDEXED_BACKEND(ValueType) \
VTK_ABI_NAMESPACE_BEGIN \
template class VTKCOMMONIMPLICITARRAYS_EXPORT vtkIndexedImplicitBackend<ValueType>; \
VTK_ABI_NAMESPACE_END
#endif

View File

@ -0,0 +1,204 @@
/*=========================================================================
Program: Visualization Toolkit
Module: vtkIndexedImplicitBackend.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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.
=========================================================================*/
#include "vtkIndexedImplicitBackend.h"
#include "vtkArrayDispatch.h"
#include "vtkArrayDispatchImplicitArrayList.h"
#include "vtkDataArrayRange.h"
#include "vtkIdList.h"
#include "vtkImplicitArray.h"
#include "vtkTypeList.h"
#include <memory>
namespace
{
//-----------------------------------------------------------------------
template <typename ValueType>
struct TypedArrayCache
{
virtual ValueType GetValue(int idx) const = 0;
virtual ~TypedArrayCache() = default;
};
template <typename ValueType, typename ArrayT>
struct SpecializedCache : public TypedArrayCache<ValueType>
{
public:
SpecializedCache(ArrayT* arr)
: Array(arr)
{
}
ValueType GetValue(int idx) const override
{
return static_cast<ValueType>(this->Array->GetValue(idx));
}
private:
vtkSmartPointer<ArrayT> Array;
};
template <typename ValueType>
struct SpecializedCache<ValueType, vtkDataArray> : public TypedArrayCache<ValueType>
{
public:
SpecializedCache(vtkDataArray* arr)
: Array(arr)
{
}
ValueType GetValue(int idx) const override
{
const int nComps = this->Array->GetNumberOfComponents();
const int iTup = idx / nComps;
const int iComp = idx - iTup * nComps;
return static_cast<ValueType>(this->Array->GetComponent(iTup, iComp));
}
private:
vtkSmartPointer<vtkDataArray> Array;
};
//-----------------------------------------------------------------------
template <typename ValueType>
struct CacheDispatchWorker
{
template <typename ArrayT>
void operator()(ArrayT* arr, std::shared_ptr<TypedArrayCache<ValueType>>& cache)
{
cache = std::make_shared<SpecializedCache<ValueType, ArrayT>>(arr);
}
};
//-----------------------------------------------------------------------
template <typename ArrayList, typename ValueType>
struct TypedCacheWrapper
{
TypedCacheWrapper(vtkDataArray* arr)
{
CacheDispatchWorker<ValueType> worker;
if (!Dispatcher::Execute(arr, worker, this->Cache))
{
worker(arr, this->Cache);
}
}
ValueType operator()(int idx) const { return this->Cache->GetValue(idx); }
private:
using Dispatcher = vtkArrayDispatch::DispatchByArray<ArrayList>;
std::shared_ptr<TypedArrayCache<ValueType>> Cache = nullptr;
};
//-----------------------------------------------------------------------
struct IdListWrapper
{
IdListWrapper(vtkIdList* indexes)
: Handles(indexes)
{
}
vtkIdType operator()(int idx) const { return this->Handles->GetId(idx); }
vtkSmartPointer<vtkIdList> Handles;
};
}
VTK_ABI_NAMESPACE_BEGIN
//-----------------------------------------------------------------------
template <typename ValueType>
struct vtkIndexedImplicitBackend<ValueType>::Internals
{
using InternalArrayList = vtkTypeList::Append<vtkArrayDispatch::AllArrays,
vtkTypeList::Create<vtkImplicitArray<::IdListWrapper>>>::Result;
Internals(vtkIdList* indexes, vtkDataArray* array)
{
if (!indexes || !array)
{
vtkErrorWithObjectMacro(nullptr, "Either index array or array itself is nullptr");
return;
}
vtkNew<vtkImplicitArray<::IdListWrapper>> newHandles;
newHandles->SetBackend(std::make_shared<IdListWrapper>(indexes));
newHandles->SetNumberOfComponents(1);
newHandles->SetNumberOfTuples(indexes->GetNumberOfIds());
this->Handles = this->TypeCacheArray<vtkIdType>(newHandles);
this->Array = this->TypeCacheArray<ValueType>(array);
}
Internals(vtkDataArray* indexes, vtkDataArray* array)
{
if (!indexes || !array)
{
vtkErrorWithObjectMacro(nullptr, "Either index array or array itself is nullptr");
return;
}
if (indexes->GetNumberOfComponents() != 1)
{
vtkErrorWithObjectMacro(nullptr,
"Passed a vtkDataArray with multiple components as indexing array to vtkIndexedArray");
return;
}
this->Handles = this->TypeCacheArray<vtkIdType>(indexes);
this->Array = this->TypeCacheArray<ValueType>(array);
}
template <typename VT>
static vtkSmartPointer<vtkImplicitArray<::TypedCacheWrapper<InternalArrayList, VT>>>
TypeCacheArray(vtkDataArray* da)
{
vtkNew<vtkImplicitArray<::TypedCacheWrapper<InternalArrayList, VT>>> wrapped;
wrapped->SetBackend(std::make_shared<::TypedCacheWrapper<InternalArrayList, VT>>(da));
wrapped->SetNumberOfComponents(1);
wrapped->SetNumberOfTuples(da->GetNumberOfTuples() * da->GetNumberOfComponents());
return wrapped;
}
vtkSmartPointer<vtkImplicitArray<::TypedCacheWrapper<InternalArrayList, ValueType>>> Array;
vtkSmartPointer<vtkImplicitArray<::TypedCacheWrapper<InternalArrayList, vtkIdType>>> Handles;
};
//-----------------------------------------------------------------------
template <typename ValueType>
vtkIndexedImplicitBackend<ValueType>::vtkIndexedImplicitBackend(
vtkIdList* indexes, vtkDataArray* array)
: Internal(std::unique_ptr<Internals>(new Internals(indexes, array)))
{
}
//-----------------------------------------------------------------------
template <typename ValueType>
vtkIndexedImplicitBackend<ValueType>::vtkIndexedImplicitBackend(
vtkDataArray* indexes, vtkDataArray* array)
: Internal(std::unique_ptr<Internals>(new Internals(indexes, array)))
{
}
//-----------------------------------------------------------------------
template <typename ValueType>
vtkIndexedImplicitBackend<ValueType>::~vtkIndexedImplicitBackend()
{
}
//-----------------------------------------------------------------------
template <typename ValueType>
ValueType vtkIndexedImplicitBackend<ValueType>::operator()(int idx) const
{
return this->Internal->Array->GetValue(this->Internal->Handles->GetValue(idx));
}
VTK_ABI_NAMESPACE_END

View File

@ -0,0 +1,19 @@
/*=========================================================================
Program: Visualization Toolkit
Module: vtkIndexedImplicitBackend.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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.
=========================================================================*/
#define VTK_INDEXED_BACKEND_INSTANTIATING
#include "vtkIndexedImplicitBackend.h"
#include "vtkIndexedImplicitBackend.txx"
VTK_INSTANTIATE_INDEXED_BACKEND(@INSTANTIATION_VALUE_TYPE@)

View File

@ -22,6 +22,8 @@
#cmakedefine VTK_DISPATCH_COMPOSITE_ARRAYS
// defined if VTK dispatches the vtkConstantArray class
#cmakedefine VTK_DISPATCH_CONSTANT_ARRAYS
// defined if VTK dispatches the vtkIndexedArray class
#cmakedefine VTK_DISPATCH_INDEXED_ARRAYS
// defined if VTK dispatches the vtkStdFunctionArray class
#cmakedefine VTK_DISPATCH_STD_FUNCTION_ARRAYS