1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkDataArrayRange.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.
=========================================================================*/
/**
* @file vtkDataArrayRange.h
* STL-compatible iterable ranges that provide access to vtkDataArray elements.
*
* @note Since the term 'range' is overloaded, it's worth pointing out that to
* determine the value-range of an array's elements (an unrelated concept to
* the Range objects defined here), see the vtkDataArray::GetRange and
* vtkGenericDataArray::GetValueRange methods.
*/
#ifndef vtkDataArrayRange_h
#define vtkDataArrayRange_h
#include "vtkAOSDataArrayTemplate.h"
#include "vtkDataArray.h"
#include "vtkDataArrayMeta.h"
#include "vtkDataArrayTupleRange_AOS.h"
#include "vtkDataArrayTupleRange_Generic.h"
#include "vtkDataArrayValueRange_AOS.h"
#include "vtkDataArrayValueRange_Generic.h"
#include "vtkMeta.h"
#include "vtkSmartPointer.h"
#include <cassert>
#include <iterator>
#include <type_traits>
/**
* @file vtkDataArrayRange.h
*
* The vtkDataArrayRange.h header provides utilities to convert vtkDataArrays
* into "range" objects that behave like STL ranges. There are two types of
* ranges: TupleRange and ValueRange.
*
* See Testing/Cxx/ExampleDataArrayRangeAPI.cxx for an illustrative example of
* how these ranges and their associated iterators and references are used.
*
* These ranges unify the different memory layouts supported by VTK and provide
* a consistent interface to processing them with high efficiency. Whether a
* range is constructed from a vtkDataArray, vtkFloatArray, or even
* vtkScaledSOADataArrayTemplate, the same range-based algorithm implementation
* can be used to provide the best performance possible using the input array's
* API.
*
* Constructing a range using a derived subclass of vtkDataArray (such as
* vtkFloatArray) will always give better performance than a range constructed
* from a vtkDataArray pointer, since the vtkDataArray API requires virtual
* calls and type conversion. Using a more derived type generally allows the
* compiler to optimize out any function calls and emit assembly that directly
* operates on the array's raw memory buffer(s). See vtkArrayDispatch for
* utilities to convert an unknown vtkDataArray into a more derived type.
* Testing/Cxx/ExampleDataArrayRangeDispatch.cxx demonstrates how ranges may
* be used with the dispatcher system.
*
* # TupleRanges
*
* A TupleRange traverses a vtkDataArray tuple-by-tuple, providing iterators
* and reference objects that refer to conceptual tuples. The tuple references
* themselves may be iterated upon to access individual components.
*
* TupleRanges are created via the function vtk::DataArrayTupleRange. See
* that function's documentation for more information about creating
* TupleRanges.
*
* # ValueRanges
*
* A ValueRange will traverse a vtkDataArray in "value index" order, e.g. as
* if walking a pointer into an AOS layout array:
*
* ```
* Array: {X, X, X}, {X, X, X}, {X, X, X}, ...
* TupleIdx: 0 0 0 1 1 1 2 2 2
* CompIdx: 0 1 2 0 1 2 0 1 2
* ValueIdx: 0 1 2 3 4 5 6 7 8
* ```
*
* ValueRanges are created via the function vtk::DataArrayValueRange. See that
* function's documentation for more information about creating ValueRanges.
*/
VTK_ITER_OPTIMIZE_START
namespace vtk
{
namespace detail
{
VTK_ABI_NAMESPACE_BEGIN
// Internal detail: This utility is not directly needed by users of
// DataArrayRange.
//
// These classes are used to detect when specializations exist for a given
// array type. They are necessary because given:
//
// template <typename ArrayType> class SomeTemplateClass;
// template <typename T> class SomeTemplateClass<vtkAOSDataArrayTemplate<T>>;
//
// SomeTemplateClass<vtkFloatArray> will pick the generic version, as ArrayType
// is a better match than vtkAOSDataArrayTemplate<T>. This class works around
// that by using Declare[Tuple|Value]RangeSpecialization functions that map an
// input ArrayTypePtr and tuple size to a specific version of the appropriate
// Range.
template <typename ArrayTypePtr, ComponentIdType TupleSize>
struct SelectTupleRange
{
private:
// Allow this to work with vtkNew, vtkSmartPointer, etc.
using ArrayType = typename detail::StripPointers<ArrayTypePtr>::type;
static_assert(detail::IsValidTupleSize<TupleSize>::value, "Invalid tuple size.");
static_assert(detail::IsVtkDataArray<ArrayType>::value, "Invalid array type.");
public:
using type =
typename std::decay<decltype(vtk::detail::DeclareTupleRangeSpecialization<ArrayType, TupleSize>(
std::declval<ArrayType*>()))>::type;
};
template <typename ArrayTypePtr, ComponentIdType TupleSize>
struct SelectValueRange
{
private:
// Allow this to work with vtkNew, vtkSmartPointer, etc.
using ArrayType = typename detail::StripPointers<ArrayTypePtr>::type;
static_assert(detail::IsValidTupleSize<TupleSize>::value, "Invalid tuple size.");
static_assert(detail::IsVtkDataArray<ArrayType>::value, "Invalid array type.");
public:
using type =
typename std::remove_reference<decltype(vtk::detail::DeclareValueRangeSpecialization<ArrayType,
TupleSize>(std::declval<ArrayType*>()))>::type;
};
VTK_ABI_NAMESPACE_END
} // end namespace detail
/**
* @brief Generate an stl and for-range compatible range of tuple iterators
* from a vtkDataArray.
*
* This function returns a TupleRange object that is compatible with C++11
* for-range syntax. As an example usage, consider a function that takes some
* instance of vtkDataArray (or a subclass) and prints the magnitude of each
* tuple:
*
* ```
* template <typename ArrayType>
* void PrintMagnitudes(ArrayType *array)
* {
* using T = vtk::GetAPIType<ArrayType>;
*
* for (const auto tuple : vtk::DataArrayTupleRange(array))
* {
* double mag = 0.;
* for (const T comp : tuple)
* {
* mag += static_cast<double>(comp) * static_cast<double>(comp);
* }
* mag = std::sqrt(mag);
* std::cerr << mag < "\n";
* }
* }
* ```
*
* Note that `ArrayType` is generic in the above function. When
* `vtk::DataArrayTupleRange` is given a `vtkDataArray` pointer, the generated
* code produces iterators and reference proxies that rely on the `vtkDataArray`
* API. However, when a more derived `ArrayType` is passed in (for example,
* `vtkFloatArray`), specialized implementations are used that generate highly
* optimized code.
*
* Performance can be further improved when the number of components in the
* array is known. By passing a compile-time-constant integer as a template
* parameter, e.g. `vtk::DataArrayTupleRange<3>(array)`, specializations are
* enabled that allow the compiler to perform additional optimizations.
*
* `vtk::DataArrayTupleRange` takes an additional two arguments that can be used
* to restrict the range of tuples to [start, end).
*
* There is a compiler definition / CMake option called
* `VTK_DEBUG_RANGE_ITERATORS` that enables checks for proper usage of the
* range/iterator/reference classes. This slows things down significantly, but
* is useful for diagnosing problems.
*
* In some situations, developers may want to build in Debug mode while still
* maintaining decent performance for data-heavy computations. For these
* usecases, an additional CMake option `VTK_ALWAYS_OPTIMIZE_ARRAY_ITERATORS`
* may be enabled to force optimization of code using these iterators. This
* option will force inlining and enable -O3 (or equivalent) optimization level
* for iterator code when compiling on platforms that support these features.
* This option has no effect when `VTK_DEBUG_RANGE_ITERATORS` is enabled.
*
* @warning Use caution when using `auto` to hold values or references obtained
* from iterators, as they may not behave as expected. This is a deficiency in
* C++ that affects all proxy iterators (such as those from `vector<bool>`)
* that use a reference object instead of an actual C++ reference type. When in
* doubt, use `std::iterator_traits` (along with decltype) or the typedefs
* listed below to determine the proper value/reference type to use. The
* examples below show how these may be used.
*
*
* To mitigate this, the following types are defined on the range object:
* - `Range::TupleIteratorType`: Iterator that visits tuples.
* - `Range::ConstTupleIteratorType`: Const iterator that visits tuples.
* - `Range::TupleReferenceType`: Mutable tuple proxy reference.
* - `Range::ConstTupleReferenceType`: Const tuple proxy reference.
* - `Range::ComponentIteratorType`: Iterator that visits components in a tuple.
* - `Range::ConstComponentIteratorType`: Const iterator that visits tuple components.
* - `Range::ComponentReferenceType`: Reference proxy to a single tuple component.
* - `Range::ConstComponentReferenceType`: Const reference proxy to a single tuple component.
* - `Range::ComponentType`: `ValueType` of components.
*
* These can be accessed via the range objects, e.g.:
*
* ```
* auto range = vtk::DataArrayTupleRange(array);
*
* using TupleRef = typename decltype(range)::TupleReferenceType;
* using ComponentRef = typename decltype(range)::ComponentReferenceType;
*
* for (TupleRef tuple : range)
* {
* for (ComponentRef comp : tuple)
* {
* comp = comp - 1; // Array is modified.
* }
* }
*
* using ConstTupleRef = typename decltype(range)::ConstTupleReferenceType;
* using ComponentType = typename decltype(range)::ComponentType;
*
* for (ConstTupleRef tuple : range)
* {
* for (ComponentType comp : tuple)
* {
* comp = comp - 1; // Array is not modified.
* }
* }
* ```
*/
VTK_ABI_NAMESPACE_BEGIN
template <ComponentIdType TupleSize = detail::DynamicTupleSize,
typename ArrayTypePtr = vtkDataArray*>
VTK_ITER_INLINE auto DataArrayTupleRange(const ArrayTypePtr& array, TupleIdType start = -1,
TupleIdType end = -1) -> typename detail::SelectTupleRange<ArrayTypePtr, TupleSize>::type
{
// Lookup specializations:
using RangeType = typename detail::SelectTupleRange<ArrayTypePtr, TupleSize>::type;
assert(array);
return RangeType(array, start < 0 ? 0 : start, end < 0 ? array->GetNumberOfTuples() : end);
}
/**
* @brief Generate an stl and for-range compatible range of flat AOS iterators
* from a vtkDataArray.
*
* This function returns a ValueRange object that is compatible with C++11
* for-range syntax. The array is traversed as if calling
* vtkGenericDataArray::GetValue with consecutive, increasing indices. As an
* example usage, consider a function that takes some instance of vtkDataArray
* (or a subclass) and sums the values it contains:
*
* ```
* template <typename ArrayType>
* auto ComputeSum(ArrayType *array) -> vtk::GetAPIType<ArrayType>
* {
* using T = vtk::GetAPIType<ArrayType>;
*
* T sum = 0.;
* for (const T val : vtk::DataArrayValueRange(array))
* {
* sum += val;
* }
* return sum;
* }
* ```
*
* These ranges may also be used with STL algorithms:
*
* ```
* template <typename ArrayType>
* auto ComputeSum(ArrayType *array) -> vtk::GetAPIType<ArrayType>
* {
* const auto range = vtk::DataArrayValueRange(array);
* return std::accumulate(range.begin(), range.end(), 0);
* }
* ```
*
* Note that `ArrayType` is generic in the above function. When
* `vtk::DataArrayValueRange` is given a `vtkDataArray` pointer, the generated
* code produces iterators and reference proxies that rely on the `vtkDataArray`
* API. However, when a more derived `ArrayType` is passed in (for example,
* `vtkFloatArray`), specialized implementations are used that generate highly
* optimized code.
*
* Performance can be further improved when the number of components in the
* array is known. By passing a compile-time-constant integer as a template
* parameter, e.g. `vtk::DataArrayValueRange<3>(array)`, specializations are
* enabled that allow the compiler to perform additional optimizations.
*
* `vtk::DataArrayValueRange` takes an additional two arguments that can be used
* to restrict the range of values to [start, end).
*
* There is a compiler definition / CMake option called
* `VTK_DEBUG_RANGE_ITERATORS` that enables checks for proper usage of the
* range/iterator/reference classes. This slows things down significantly, but
* is useful for diagnosing problems.
*
* In some situations, developers may want to build in Debug mode while still
* maintaining decent performance for data-heavy computations. For these
* usecases, an additional CMake option `VTK_ALWAYS_OPTIMIZE_ARRAY_ITERATORS`
* may be enabled to force optimization of code using these iterators. This
* option will force inlining and enable -O3 (or equivalent) optimization level
* for iterator code when compiling on platforms that support these features.
* This option has no effect when `VTK_DEBUG_RANGE_ITERATORS` is enabled.
*
* @warning Use caution when using `auto` to hold values or references obtained
* from iterators, as they may not behave as expected. This is a deficiency in
* C++ that affects all proxy iterators (such as those from `vector<bool>`)
* that use a reference object instead of an actual C++ reference type. When in
* doubt, use `std::iterator_traits` (along with decltype) or the typedefs
* listed below to determine the proper value/reference type to use. The
* examples below show how these may be used.
*
* To mitigate this, the following types are defined on the range object:
* - `Range::IteratorType`: Iterator that visits values in AOS order.
* - `Range::ConstIteratorType`: Const iterator that visits values in AOS order.
* - `Range::ReferenceType`: Mutable value proxy reference.
* - `Range::ConstReferenceType`: Const value proxy reference.
* - `Range::ValueType`: `ValueType` of array's API.
*
* These can be accessed via the range objects, e.g.:
*
* ```
* auto range = vtk::DataArrayValueRange(array);
*
* using RefType = typename decltype(range)::ReferenceType;
* for (RefType ref : range)
* { // `ref` is a reference (or reference proxy) to the data held by the array.
* ref -= 1; // Array is modified.
* }
*
* using ValueType = typename decltype(range)::ValueType;
* for (ValueType value : range)
* { // implicitly converts from a reference (or proxy) to a local lvalue `value`
* value -= 1; // Array is not modified.
* }
* ```
*/
template <ComponentIdType TupleSize = detail::DynamicTupleSize,
typename ArrayTypePtr = vtkDataArray*>
VTK_ITER_INLINE auto DataArrayValueRange(const ArrayTypePtr& array, ValueIdType start = -1,
ValueIdType end = -1) -> typename detail::SelectValueRange<ArrayTypePtr, TupleSize>::type
{
using RangeType = typename detail::SelectValueRange<ArrayTypePtr, TupleSize>::type;
assert(array);
return RangeType(array, start < 0 ? 0 : start, end < 0 ? array->GetNumberOfValues() : end);
}
VTK_ABI_NAMESPACE_END
} // end namespace vtk
VTK_ITER_OPTIMIZE_END
#endif // vtkDataArrayRange_h
// VTK-HeaderTest-Exclude: vtkDataArrayRange.h
|