VTK
vtkGenericDataArrayLookupHelper.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkGenericDataArrayLookupHelper.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
22 #ifndef vtkGenericDataArrayLookupHelper_h
23 #define vtkGenericDataArrayLookupHelper_h
24 
25 #include "vtkIdList.h"
26 #include <algorithm>
27 #include <cmath>
28 #include <unordered_map>
29 #include <vector>
30 
31 namespace detail
32 {
33 template <typename T, bool>
34 struct has_NaN;
35 
36 template <typename T>
37 struct has_NaN<T, true>
38 {
39  static bool isnan(T x) { return std::isnan(x); }
40 };
41 
42 template <typename T>
43 struct has_NaN<T, false>
44 {
45  static bool isnan(T) { return false; }
46 };
47 
48 template <typename T>
49 bool isnan(T x)
50 {
51  // Select the correct partially specialized type.
53 }
54 } // namespace detail
55 
56 template <class ArrayTypeT>
58 {
59 public:
60  typedef ArrayTypeT ArrayType;
61  typedef typename ArrayType::ValueType ValueType;
62 
64 
66 
67  void SetArray(ArrayTypeT* array)
68  {
69  if (this->AssociatedArray != array)
70  {
71  this->ClearLookup();
72  this->AssociatedArray = array;
73  }
74  }
75 
77  {
78  this->UpdateLookup();
79  auto indices = FindIndexVec(elem);
80  if (indices == nullptr)
81  {
82  return -1;
83  }
84  return indices->front();
85  }
86 
87  void LookupValue(ValueType elem, vtkIdList* ids)
88  {
89  ids->Reset();
90  this->UpdateLookup();
91  auto indices = FindIndexVec(elem);
92  if (indices)
93  {
94  ids->Allocate(static_cast<vtkIdType>(indices->size()));
95  for (auto index : *indices)
96  {
97  ids->InsertNextId(index);
98  }
99  }
100  }
101 
103 
106  void ClearLookup()
107  {
108  this->ValueMap.clear();
109  this->NanIndices.clear();
110  }
112 
113 private:
115  void operator=(const vtkGenericDataArrayLookupHelper&) = delete;
116 
117  void UpdateLookup()
118  {
119  if (!this->AssociatedArray || (this->AssociatedArray->GetNumberOfTuples() < 1) ||
120  (!this->ValueMap.empty() || !this->NanIndices.empty()))
121  {
122  return;
123  }
124 
125  vtkIdType num = this->AssociatedArray->GetNumberOfValues();
126  this->ValueMap.reserve(num);
127  for (vtkIdType i = 0; i < num; ++i)
128  {
129  auto value = this->AssociatedArray->GetValue(i);
130  if (::detail::isnan(value))
131  {
132  NanIndices.push_back(i);
133  }
134  this->ValueMap[value].push_back(i);
135  }
136  }
137 
138  // Return a pointer to the relevant vector of indices if specified value was
139  // found in the array.
140  std::vector<vtkIdType>* FindIndexVec(ValueType value)
141  {
142  std::vector<vtkIdType>* indices{ nullptr };
143  if (::detail::isnan(value) && !this->NanIndices.empty())
144  {
145  indices = &this->NanIndices;
146  }
147  const auto& pos = this->ValueMap.find(value);
148  if (pos != this->ValueMap.end())
149  {
150  indices = &pos->second;
151  }
152  return indices;
153  }
154 
155  ArrayTypeT* AssociatedArray{ nullptr };
156  std::unordered_map<ValueType, std::vector<vtkIdType> > ValueMap;
157  std::vector<vtkIdType> NanIndices;
158 };
159 
160 #endif
161 // VTK-HeaderTest-Exclude: vtkGenericDataArrayLookupHelper.h
vtkIdList::Allocate
int Allocate(const vtkIdType sz, const int strategy=0)
Allocate a capacity for sz ids in the list and set the number of stored ids in the list to 0.
vtkGenericDataArray::ValueType
ValueTypeT ValueType
Definition: vtkGenericDataArray.h:84
vtkGenericDataArrayLookupHelper
internal class used by vtkGenericDataArray to support LookupValue.
Definition: vtkGenericDataArrayLookupHelper.h:57
vtkGenericDataArrayLookupHelper::SetArray
void SetArray(ArrayTypeT *array)
Definition: vtkGenericDataArrayLookupHelper.h:67
detail::has_NaN< T, false >::isnan
static bool isnan(T)
Definition: vtkGenericDataArrayLookupHelper.h:45
vtkX3D::value
Definition: vtkX3D.h:226
vtkIdType
int vtkIdType
Definition: vtkType.h:349
vtkGenericDataArrayLookupHelper::ValueType
ArrayType::ValueType ValueType
Definition: vtkGenericDataArrayLookupHelper.h:61
detail
Definition: vtkGenericDataArrayLookupHelper.h:31
detail::isnan
bool isnan(T x)
Definition: vtkGenericDataArrayLookupHelper.h:49
vtkIdList::InsertNextId
vtkIdType InsertNextId(const vtkIdType vtkid)
Add the id specified to the end of the list.
Definition: vtkIdList.h:205
vtkGenericDataArrayLookupHelper::~vtkGenericDataArrayLookupHelper
~vtkGenericDataArrayLookupHelper()
Definition: vtkGenericDataArrayLookupHelper.h:65
vtkGenericDataArrayLookupHelper::LookupValue
void LookupValue(ValueType elem, vtkIdList *ids)
Definition: vtkGenericDataArrayLookupHelper.h:87
vtkIdList
list of point or cell ids
Definition: vtkIdList.h:30
vtkGenericDataArrayLookupHelper::LookupValue
vtkIdType LookupValue(ValueType elem)
Definition: vtkGenericDataArrayLookupHelper.h:76
vtkGenericDataArrayLookupHelper::vtkGenericDataArrayLookupHelper
vtkGenericDataArrayLookupHelper()=default
vtkGenericDataArrayLookupHelper::ArrayType
ArrayTypeT ArrayType
Definition: vtkGenericDataArrayLookupHelper.h:60
detail::has_NaN
Definition: vtkGenericDataArrayLookupHelper.h:34
vtkX3D::index
Definition: vtkX3D.h:252
vtkIdList.h
vtkIdList::Reset
void Reset()
Reset to an empty state but retain previously allocated memory.
Definition: vtkIdList.h:128
vtkGenericDataArrayLookupHelper::ClearLookup
void ClearLookup()
Release any allocated memory for internal data-structures.
Definition: vtkGenericDataArrayLookupHelper.h:106
detail::has_NaN< T, true >::isnan
static bool isnan(T x)
Definition: vtkGenericDataArrayLookupHelper.h:39