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() VTK_OVERRIDE //calm down some finicky compilers
89  {
90  }
91 
92  void Copy(vtkIdType inId, vtkIdType outId) VTK_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) VTK_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) VTK_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) VTK_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) VTK_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() VTK_OVERRIDE //calm down some finicky compilers
156  {
157  }
158 
159  void Copy(vtkIdType inId, vtkIdType outId) VTK_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) VTK_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) VTK_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) VTK_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) VTK_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  bool 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, bool promote);
238 
239  // Any array excluded here is not added by AddArrays() or AddArrayPair, hence not
240  // processed. Also check whether an array is excluded.
241  void ExcludeArray(vtkDataArray *da);
242  bool IsExcluded(vtkDataArray *da);
243 
244  // Loop over the array pairs and copy data from one to another
245  void Copy(vtkIdType inId, vtkIdType outId)
246  {
247  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin();
248  it != Arrays.end(); ++it)
249  {
250  (*it)->Copy(inId, outId);
251  }
252  }
253 
254  // Loop over the arrays and have them interpolate themselves
255  void Interpolate(int numWeights, const vtkIdType *ids, const double *weights, vtkIdType outId)
256  {
257  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin();
258  it != Arrays.end(); ++it)
259  {
260  (*it)->Interpolate(numWeights, ids, weights, outId);
261  }
262  }
263 
264  // Loop over the arrays perform edge interpolation
265  void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId)
266  {
267  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin();
268  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();
278  it != Arrays.end(); ++it)
279  {
280  (*it)->AssignNullValue(outId);
281  }
282  }
283 
284  // Extend (realloc) the arrays
285  void Realloc(vtkIdType sze)
286  {
287  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin();
288  it != Arrays.end(); ++it)
289  {
290  (*it)->Realloc(sze);
291  }
292  }
293 
294  // Only you can prevent memory leaks!
296  {
297  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin();
298  it != Arrays.end(); ++it)
299  {
300  delete (*it);
301  }
302  }
303 
304  // Return the number of arrays
306  {
307  return static_cast<vtkIdType>(Arrays.size());
308  }
309 
310 };
311 
312 
313 #include "vtkArrayListTemplate.txx"
314 
315 #endif
316 // VTK-HeaderTest-Exclude: vtkArrayListTemplate.h
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:41
~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:345
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)
void AssignNullValue(vtkIdType outId)
vtkIdType GetNumberOfArrays()
void Copy(vtkIdType inId, vtkIdType outId)
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:48
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