VTK  9.0.1
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
void LookupValue(ValueType elem, vtkIdList *ids)
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.
internal class used by vtkGenericDataArray to support LookupValue.
void Reset()
Reset to an empty state but retain previously allocated memory.
Definition: vtkIdList.h:139
int vtkIdType
Definition: vtkType.h:338
void ClearLookup()
Release any allocated memory for internal data-structures.
list of point or cell ids
Definition: vtkIdList.h:30
vtkIdType InsertNextId(const vtkIdType vtkid)
Add the id specified to the end of the list.
Definition: vtkIdList.h:216