VTK  9.1.20211230
vtkArrayListTemplate.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkArrayListTemplate.h
5 
6  Copyright (c) Kitware, Inc.
7  All rights reserved.
8  See LICENSE file 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 =========================================================================*/
45 #ifndef vtkArrayListTemplate_h
46 #define vtkArrayListTemplate_h
47 
48 #include "vtkAbstractArray.h"
49 #include "vtkDataSetAttributes.h"
50 #include "vtkSmartPointer.h"
51 #include "vtkStdString.h"
52 
53 #include <algorithm>
54 #include <vector>
55 
56 // Create a generic class supporting virtual dispatch to type-specific
57 // subclasses.
59 {
61  int NumComp;
63 
64  BaseArrayPair(vtkIdType num, int numComp, vtkAbstractArray* outArray)
65  : Num(num)
66  , NumComp(numComp)
67  , OutputArray(outArray)
68  {
69  }
70  virtual ~BaseArrayPair() = default;
71 
72  virtual void Copy(vtkIdType inId, vtkIdType outId) = 0;
73  virtual void Interpolate(
74  int numWeights, const vtkIdType* ids, const double* weights, vtkIdType outId) = 0;
75  virtual void Average(int numPts, const vtkIdType* ids, vtkIdType outId) = 0;
76  virtual void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId) = 0;
77  virtual void AssignNullValue(vtkIdType outId) = 0;
78  virtual void Realloc(vtkIdType sze) = 0;
79 };
80 
81 // Type specific interpolation on a matched pair of data arrays
82 template <typename T>
83 struct ArrayPair : public BaseArrayPair
84 {
85  T* Input;
86  T* Output;
88 
89  ArrayPair(T* in, T* out, vtkIdType num, int numComp, vtkAbstractArray* outArray, T null)
90  : BaseArrayPair(num, numComp, outArray)
91  , Input(in)
92  , Output(out)
93  , NullValue(null)
94  {
95  }
96  ~ArrayPair() override = default; // calm down some finicky compilers
97 
98  void Copy(vtkIdType inId, vtkIdType outId) override
99  {
100  for (int j = 0; j < this->NumComp; ++j)
101  {
102  this->Output[outId * this->NumComp + j] = this->Input[inId * this->NumComp + j];
103  }
104  }
105 
107  int numWeights, const vtkIdType* ids, const double* weights, vtkIdType outId) override
108  {
109  for (int j = 0; j < this->NumComp; ++j)
110  {
111  double v = 0.0;
112  for (vtkIdType i = 0; i < numWeights; ++i)
113  {
114  v += weights[i] * static_cast<double>(this->Input[ids[i] * this->NumComp + j]);
115  }
116  this->Output[outId * this->NumComp + j] = static_cast<T>(v);
117  }
118  }
119 
120  void Average(int numPts, const vtkIdType* ids, vtkIdType outId) override
121  {
122  for (int j = 0; j < this->NumComp; ++j)
123  {
124  double v = 0.0;
125  for (vtkIdType i = 0; i < numPts; ++i)
126  {
127  v += static_cast<double>(this->Input[ids[i] * this->NumComp + j]);
128  }
129  v /= static_cast<double>(numPts);
130  this->Output[outId * this->NumComp + j] = static_cast<T>(v);
131  }
132  }
133 
134  void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId) override
135  {
136  double v;
137  vtkIdType numComp = this->NumComp;
138  for (int j = 0; j < numComp; ++j)
139  {
140  v = this->Input[v0 * numComp + j] +
141  t * (this->Input[v1 * numComp + j] - this->Input[v0 * numComp + j]);
142  this->Output[outId * numComp + j] = static_cast<T>(v);
143  }
144  }
145 
146  void AssignNullValue(vtkIdType outId) override
147  {
148  for (int j = 0; j < this->NumComp; ++j)
149  {
150  this->Output[outId * this->NumComp + j] = this->NullValue;
151  }
152  }
153 
154  void Realloc(vtkIdType sze) override
155  {
156  this->OutputArray->Resize(sze);
157  this->OutputArray->SetNumberOfTuples(sze);
158  this->Output = static_cast<T*>(this->OutputArray->GetVoidPointer(0));
159  }
160 };
161 
162 // Type specific interpolation on a pair of data arrays with different types, where the
163 // output type is expected to be a real type (i.e., float or double).
164 template <typename TInput, typename TOutput>
166 {
167  TInput* Input;
168  TOutput* Output;
169  TOutput NullValue;
170 
172  TInput* in, TOutput* out, vtkIdType num, int numComp, vtkAbstractArray* outArray, TOutput null)
173  : BaseArrayPair(num, numComp, outArray)
174  , Input(in)
175  , Output(out)
176  , NullValue(null)
177  {
178  }
179  ~RealArrayPair() override = default; // calm down some finicky compilers
180 
181  void Copy(vtkIdType inId, vtkIdType outId) override
182  {
183  for (int j = 0; j < this->NumComp; ++j)
184  {
185  this->Output[outId * this->NumComp + j] =
186  static_cast<TOutput>(this->Input[inId * this->NumComp + j]);
187  }
188  }
189 
191  int numWeights, const vtkIdType* ids, const double* weights, vtkIdType outId) override
192  {
193  for (int j = 0; j < this->NumComp; ++j)
194  {
195  double v = 0.0;
196  for (vtkIdType i = 0; i < numWeights; ++i)
197  {
198  v += weights[i] * static_cast<double>(this->Input[ids[i] * this->NumComp + j]);
199  }
200  this->Output[outId * this->NumComp + j] = static_cast<TOutput>(v);
201  }
202  }
203 
204  void Average(int numPts, const vtkIdType* ids, vtkIdType outId) override
205  {
206  for (int j = 0; j < this->NumComp; ++j)
207  {
208  double v = 0.0;
209  for (vtkIdType i = 0; i < numPts; ++i)
210  {
211  v += static_cast<double>(this->Input[ids[i] * this->NumComp + j]);
212  }
213  v /= static_cast<double>(numPts);
214  this->Output[outId * this->NumComp + j] = static_cast<TOutput>(v);
215  }
216  }
217 
218  void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId) override
219  {
220  double v;
221  vtkIdType numComp = this->NumComp;
222  for (int j = 0; j < numComp; ++j)
223  {
224  v = this->Input[v0 * numComp + j] +
225  t * (this->Input[v1 * numComp + j] - this->Input[v0 * numComp + j]);
226  this->Output[outId * numComp + j] = static_cast<TOutput>(v);
227  }
228  }
229 
230  void AssignNullValue(vtkIdType outId) override
231  {
232  for (int j = 0; j < this->NumComp; ++j)
233  {
234  this->Output[outId * this->NumComp + j] = this->NullValue;
235  }
236  }
237 
238  void Realloc(vtkIdType sze) override
239  {
240  this->OutputArray->Resize(sze);
241  this->OutputArray->SetNumberOfTuples(sze);
242  this->Output = static_cast<TOutput*>(this->OutputArray->GetVoidPointer(0));
243  }
244 };
245 
246 // Forward declarations. This makes working with vtkTemplateMacro easier.
247 struct ArrayList;
248 
249 template <typename T>
251  ArrayList* list, T* inData, T* outData, vtkIdType numTuples, int numComp, T nullValue);
252 
253 // A list of the arrays to interpolate, and a method to invoke interpolation on the list
254 struct ArrayList
255 {
256  // The list of arrays, and the arrays not to process
257  std::vector<BaseArrayPair*> Arrays;
258  std::vector<vtkAbstractArray*> ExcludedArrays;
259 
260  // Add the arrays to interpolate here (from attribute data). Note that this method is
261  // not thread-safe due to its use of vtkDataSetAttributes.
263  double nullValue = 0.0, vtkTypeBool promote = true);
264 
265  // Add an array that interpolates from its own attribute values
267  vtkIdType numOutPts, vtkDataSetAttributes* attr, double nullValue = 0.0);
268 
269  // Add a pair of arrays (manual insertion). Returns the output array created,
270  // if any. No array may be created if \c inArray was previously marked as
271  // excluded using ExcludeArray().
273  vtkStdString& outArrayName, double nullValue, vtkTypeBool promote);
274 
275  // Any array excluded here is not added by AddArrays() or AddArrayPair, hence not
276  // processed. Also check whether an array is excluded.
279 
280  // Loop over the array pairs and copy data from one to another. This (and the following methods)
281  // can be used within threads.
282  void Copy(vtkIdType inId, vtkIdType outId)
283  {
284  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin(); it != Arrays.end(); ++it)
285  {
286  (*it)->Copy(inId, outId);
287  }
288  }
289 
290  // Loop over the arrays and have them interpolate themselves
291  void Interpolate(int numWeights, const vtkIdType* ids, const double* weights, vtkIdType outId)
292  {
293  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin(); it != Arrays.end(); ++it)
294  {
295  (*it)->Interpolate(numWeights, ids, weights, outId);
296  }
297  }
298 
299  // Loop over the arrays and have them averaged
300  void Average(int numPts, const vtkIdType* ids, vtkIdType outId)
301  {
302  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin(); it != Arrays.end(); ++it)
303  {
304  (*it)->Average(numPts, ids, outId);
305  }
306  }
307 
308  // Loop over the arrays perform edge interpolation
309  void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId)
310  {
311  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin(); it != Arrays.end(); ++it)
312  {
313  (*it)->InterpolateEdge(v0, v1, t, outId);
314  }
315  }
316 
317  // Loop over the arrays and assign the null value
319  {
320  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin(); it != Arrays.end(); ++it)
321  {
322  (*it)->AssignNullValue(outId);
323  }
324  }
325 
326  // Extend (realloc) the arrays
327  void Realloc(vtkIdType sze)
328  {
329  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin(); it != Arrays.end(); ++it)
330  {
331  (*it)->Realloc(sze);
332  }
333  }
334 
335  // Only you can prevent memory leaks!
337  {
338  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin(); it != Arrays.end(); ++it)
339  {
340  delete (*it);
341  }
342  }
343 
344  // Return the number of arrays
345  vtkIdType GetNumberOfArrays() { return static_cast<vtkIdType>(Arrays.size()); }
346 };
347 
348 #include "vtkArrayListTemplate.txx"
349 
350 #endif
351 // VTK-HeaderTest-Exclude: vtkArrayListTemplate.h
Abstract superclass for all arrays.
virtual vtkTypeBool Resize(vtkIdType numTuples)=0
Resize the array to the requested number of tuples and preserve data.
virtual void SetNumberOfTuples(vtkIdType numTuples)=0
Set the number of tuples (a component group) in the array.
virtual void * GetVoidPointer(vtkIdType valueIdx)=0
Return a void pointer.
represent and manipulate attribute data in a dataset
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:105
void Average(int numPts, const vtkIdType *ids, vtkIdType outId)
void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId)
void AddSelfInterpolatingArrays(vtkIdType numOutPts, vtkDataSetAttributes *attr, double nullValue=0.0)
void AssignNullValue(vtkIdType outId)
vtkAbstractArray * AddArrayPair(vtkIdType numTuples, vtkAbstractArray *inArray, vtkStdString &outArrayName, double nullValue, vtkTypeBool promote)
vtkTypeBool IsExcluded(vtkAbstractArray *da)
std::vector< vtkAbstractArray * > ExcludedArrays
vtkIdType GetNumberOfArrays()
void Copy(vtkIdType inId, vtkIdType outId)
void AddArrays(vtkIdType numOutPts, vtkDataSetAttributes *inPD, vtkDataSetAttributes *outPD, double nullValue=0.0, vtkTypeBool promote=true)
void ExcludeArray(vtkAbstractArray *da)
void Realloc(vtkIdType sze)
std::vector< BaseArrayPair * > Arrays
void Interpolate(int numWeights, const vtkIdType *ids, const double *weights, vtkIdType outId)
void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId) override
void AssignNullValue(vtkIdType outId) override
void Average(int numPts, const vtkIdType *ids, vtkIdType outId) override
~ArrayPair() override=default
ArrayPair(T *in, T *out, vtkIdType num, int numComp, vtkAbstractArray *outArray, T null)
void Interpolate(int numWeights, const vtkIdType *ids, const double *weights, vtkIdType outId) override
void Copy(vtkIdType inId, vtkIdType outId) override
void Realloc(vtkIdType sze) override
BaseArrayPair(vtkIdType num, int numComp, vtkAbstractArray *outArray)
virtual void Average(int numPts, const vtkIdType *ids, vtkIdType outId)=0
virtual void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId)=0
virtual void AssignNullValue(vtkIdType outId)=0
virtual void Copy(vtkIdType inId, vtkIdType outId)=0
virtual ~BaseArrayPair()=default
virtual void Interpolate(int numWeights, const vtkIdType *ids, const double *weights, vtkIdType outId)=0
virtual void Realloc(vtkIdType sze)=0
vtkSmartPointer< vtkAbstractArray > OutputArray
void Interpolate(int numWeights, const vtkIdType *ids, const double *weights, vtkIdType outId) override
~RealArrayPair() override=default
void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId) override
void Copy(vtkIdType inId, vtkIdType outId) override
void AssignNullValue(vtkIdType outId) override
void Realloc(vtkIdType sze) override
void Average(int numPts, const vtkIdType *ids, vtkIdType outId) override
RealArrayPair(TInput *in, TOutput *out, vtkIdType num, int numComp, vtkAbstractArray *outArray, TOutput null)
int vtkTypeBool
Definition: vtkABI.h:69
void CreateArrayPair(ArrayList *list, T *inData, T *outData, vtkIdType numTuples, int numComp, T nullValue)
int vtkIdType
Definition: vtkType.h:332