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