parent
6a49775c3f
commit
ec20199665
@ -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;
|
||||
};
|
@ -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
|
@ -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@)
|
Loading…
Reference in new issue