VTK  9.0.2
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 =========================================================================*/
40 #ifndef vtkArrayListTemplate_h
41 #define vtkArrayListTemplate_h
42 
43 #include "vtkDataArray.h"
44 #include "vtkDataSetAttributes.h"
45 #include "vtkSmartPointer.h"
46 #include "vtkStdString.h"
47 
48 #include <algorithm>
49 #include <vector>
50 
51 // Create a generic class supporting virtual dispatch to type-specific
52 // subclasses.
54 {
56  int NumComp;
58 
59  BaseArrayPair(vtkIdType num, int numComp, vtkDataArray* outArray)
60  : Num(num)
61  , NumComp(numComp)
62  , OutputArray(outArray)
63  {
64  }
65  virtual ~BaseArrayPair() {}
66 
67  virtual void Copy(vtkIdType inId, vtkIdType outId) = 0;
68  virtual void Interpolate(
69  int numWeights, const vtkIdType* ids, const double* weights, vtkIdType outId) = 0;
70  virtual void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId) = 0;
71  virtual void AssignNullValue(vtkIdType outId) = 0;
72  virtual void Realloc(vtkIdType sze) = 0;
73 };
74 
75 // Type specific interpolation on a matched pair of data arrays
76 template <typename T>
77 struct ArrayPair : public BaseArrayPair
78 {
79  T* Input;
80  T* Output;
82 
83  ArrayPair(T* in, T* out, vtkIdType num, int numComp, vtkDataArray* outArray, T null)
84  : BaseArrayPair(num, numComp, outArray)
85  , Input(in)
86  , Output(out)
87  , NullValue(null)
88  {
89  }
90  ~ArrayPair() override // calm down some finicky compilers
91  {
92  }
93 
94  void Copy(vtkIdType inId, vtkIdType outId) override
95  {
96  for (int j = 0; j < this->NumComp; ++j)
97  {
98  this->Output[outId * this->NumComp + j] = this->Input[inId * this->NumComp + j];
99  }
100  }
101 
103  int numWeights, const vtkIdType* ids, const double* weights, vtkIdType outId) override
104  {
105  for (int j = 0; j < this->NumComp; ++j)
106  {
107  double v = 0.0;
108  for (vtkIdType i = 0; i < numWeights; ++i)
109  {
110  v += weights[i] * static_cast<double>(this->Input[ids[i] * this->NumComp + j]);
111  }
112  this->Output[outId * this->NumComp + j] = static_cast<T>(v);
113  }
114  }
115 
116  void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId) override
117  {
118  double v;
119  vtkIdType numComp = this->NumComp;
120  for (int j = 0; j < numComp; ++j)
121  {
122  v = this->Input[v0 * numComp + j] +
123  t * (this->Input[v1 * numComp + j] - this->Input[v0 * numComp + j]);
124  this->Output[outId * numComp + j] = static_cast<T>(v);
125  }
126  }
127 
128  void AssignNullValue(vtkIdType outId) override
129  {
130  for (int j = 0; j < this->NumComp; ++j)
131  {
132  this->Output[outId * this->NumComp + j] = this->NullValue;
133  }
134  }
135 
136  void Realloc(vtkIdType sze) override
137  {
138  this->OutputArray->WriteVoidPointer(0, sze * this->NumComp);
139  this->Output = static_cast<T*>(this->OutputArray->GetVoidPointer(0));
140  }
141 };
142 
143 // Type specific interpolation on a pair of data arrays with different types, where the
144 // output type is expected to be a real type (i.e., float or double).
145 template <typename TInput, typename TOutput>
147 {
148  TInput* Input;
149  TOutput* Output;
150  TOutput NullValue;
151 
153  TInput* in, TOutput* out, vtkIdType num, int numComp, vtkDataArray* outArray, TOutput null)
154  : BaseArrayPair(num, numComp, outArray)
155  , Input(in)
156  , Output(out)
157  , NullValue(null)
158  {
159  }
160  ~RealArrayPair() override // calm down some finicky compilers
161  {
162  }
163 
164  void Copy(vtkIdType inId, vtkIdType outId) override
165  {
166  for (int j = 0; j < this->NumComp; ++j)
167  {
168  this->Output[outId * this->NumComp + j] =
169  static_cast<TOutput>(this->Input[inId * this->NumComp + j]);
170  }
171  }
172 
174  int numWeights, const vtkIdType* ids, const double* weights, vtkIdType outId) override
175  {
176  for (int j = 0; j < this->NumComp; ++j)
177  {
178  double v = 0.0;
179  for (vtkIdType i = 0; i < numWeights; ++i)
180  {
181  v += weights[i] * static_cast<double>(this->Input[ids[i] * this->NumComp + j]);
182  }
183  this->Output[outId * this->NumComp + j] = static_cast<TOutput>(v);
184  }
185  }
186 
187  void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId) override
188  {
189  double v;
190  vtkIdType numComp = this->NumComp;
191  for (int j = 0; j < numComp; ++j)
192  {
193  v = this->Input[v0 * numComp + j] +
194  t * (this->Input[v1 * numComp + j] - this->Input[v0 * numComp + j]);
195  this->Output[outId * numComp + j] = static_cast<TOutput>(v);
196  }
197  }
198 
199  void AssignNullValue(vtkIdType outId) override
200  {
201  for (int j = 0; j < this->NumComp; ++j)
202  {
203  this->Output[outId * this->NumComp + j] = this->NullValue;
204  }
205  }
206 
207  void Realloc(vtkIdType sze) override
208  {
209  this->OutputArray->WriteVoidPointer(0, sze * this->NumComp);
210  this->Output = static_cast<TOutput*>(this->OutputArray->GetVoidPointer(0));
211  }
212 };
213 
214 // Forward declarations. This makes working with vtkTemplateMacro easier.
215 struct ArrayList;
216 
217 template <typename T>
219  ArrayList* list, T* inData, T* outData, vtkIdType numTuples, int numComp, T nullValue);
220 
221 // A list of the arrays to interpolate, and a method to invoke interpolation on the list
222 struct ArrayList
223 {
224  // The list of arrays, and the arrays not to process
225  std::vector<BaseArrayPair*> Arrays;
226  std::vector<vtkDataArray*> ExcludedArrays;
227 
228  // Add the arrays to interpolate here (from attribute data)
230  double nullValue = 0.0, vtkTypeBool promote = true);
231 
232  // Add an array that interpolates from its own attribute values
234  vtkIdType numOutPts, vtkDataSetAttributes* attr, double nullValue = 0.0);
235 
236  // Add a pair of arrays (manual insertion). Returns the output array created,
237  // if any. No array may be created if \c inArray was previously marked as
238  // excluded using ExcludeArray().
239  vtkDataArray* AddArrayPair(vtkIdType numTuples, vtkDataArray* inArray, vtkStdString& outArrayName,
240  double nullValue, vtkTypeBool promote);
241 
242  // Any array excluded here is not added by AddArrays() or AddArrayPair, hence not
243  // processed. Also check whether an array is excluded.
246 
247  // Loop over the array pairs and copy data from one to another
248  void Copy(vtkIdType inId, vtkIdType outId)
249  {
250  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin(); it != Arrays.end(); ++it)
251  {
252  (*it)->Copy(inId, outId);
253  }
254  }
255 
256  // Loop over the arrays and have them interpolate themselves
257  void Interpolate(int numWeights, const vtkIdType* ids, const double* weights, vtkIdType outId)
258  {
259  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin(); it != Arrays.end(); ++it)
260  {
261  (*it)->Interpolate(numWeights, ids, weights, outId);
262  }
263  }
264 
265  // Loop over the arrays perform edge interpolation
266  void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId)
267  {
268  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin(); it != Arrays.end(); ++it)
269  {
270  (*it)->InterpolateEdge(v0, v1, t, outId);
271  }
272  }
273 
274  // Loop over the arrays and assign the null value
276  {
277  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin(); it != Arrays.end(); ++it)
278  {
279  (*it)->AssignNullValue(outId);
280  }
281  }
282 
283  // Extend (realloc) the arrays
284  void Realloc(vtkIdType sze)
285  {
286  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin(); it != Arrays.end(); ++it)
287  {
288  (*it)->Realloc(sze);
289  }
290  }
291 
292  // Only you can prevent memory leaks!
294  {
295  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin(); it != Arrays.end(); ++it)
296  {
297  delete (*it);
298  }
299  }
300 
301  // Return the number of arrays
302  vtkIdType GetNumberOfArrays() { return static_cast<vtkIdType>(Arrays.size()); }
303 };
304 
305 #include "vtkArrayListTemplate.txx"
306 
307 #endif
308 // VTK-HeaderTest-Exclude: vtkArrayListTemplate.h
virtual void * GetVoidPointer(vtkIdType valueIdx)=0
Return a void pointer.
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:50
virtual void * WriteVoidPointer(vtkIdType valueIdx, vtkIdType numValues)=0
Get the address of a particular data index.
represent and manipulate attribute data in a dataset
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:35
void ExcludeArray(vtkDataArray *da)
std::vector< vtkDataArray * > ExcludedArrays
void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId)
void AddSelfInterpolatingArrays(vtkIdType numOutPts, vtkDataSetAttributes *attr, double nullValue=0.0)
void AssignNullValue(vtkIdType outId)
vtkDataArray * AddArrayPair(vtkIdType numTuples, vtkDataArray *inArray, vtkStdString &outArrayName, double nullValue, vtkTypeBool promote)
vtkIdType GetNumberOfArrays()
void Copy(vtkIdType inId, vtkIdType outId)
void AddArrays(vtkIdType numOutPts, vtkDataSetAttributes *inPD, vtkDataSetAttributes *outPD, double nullValue=0.0, vtkTypeBool promote=true)
vtkTypeBool IsExcluded(vtkDataArray *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 Interpolate(int numWeights, const vtkIdType *ids, const double *weights, vtkIdType outId) override
ArrayPair(T *in, T *out, vtkIdType num, int numComp, vtkDataArray *outArray, T null)
void Copy(vtkIdType inId, vtkIdType outId) override
void Realloc(vtkIdType sze) override
~ArrayPair() override
vtkSmartPointer< vtkDataArray > OutputArray
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 void Interpolate(int numWeights, const vtkIdType *ids, const double *weights, vtkIdType outId)=0
virtual void Realloc(vtkIdType sze)=0
virtual ~BaseArrayPair()
BaseArrayPair(vtkIdType num, int numComp, vtkDataArray *outArray)
void Interpolate(int numWeights, const vtkIdType *ids, const double *weights, vtkIdType outId) override
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
~RealArrayPair() override
RealArrayPair(TInput *in, TOutput *out, vtkIdType num, int numComp, vtkDataArray *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:338