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 a pair of arrays (manual insertion). Returns the output array created,
230  // if any. No array may be created if \c inArray was previously marked as
231  // excluded using ExcludeArray().
232  vtkDataArray* AddArrayPair(vtkIdType numTuples, vtkDataArray *inArray,
233  vtkStdString &outArrayName, double nullValue, bool promote);
234 
235  // Any array excluded here is not added by AddArrays() or AddArrayPair, hence not
236  // processed. Also check whether an array is excluded.
237  void ExcludeArray(vtkDataArray *da);
238  bool IsExcluded(vtkDataArray *da);
239 
240  // Loop over the array pairs and copy data from one to another
241  void Copy(vtkIdType inId, vtkIdType outId)
242  {
243  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin();
244  it != Arrays.end(); ++it)
245  {
246  (*it)->Copy(inId, outId);
247  }
248  }
249 
250  // Loop over the arrays and have them interpolate themselves
251  void Interpolate(int numWeights, const vtkIdType *ids, const double *weights, vtkIdType outId)
252  {
253  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin();
254  it != Arrays.end(); ++it)
255  {
256  (*it)->Interpolate(numWeights, ids, weights, outId);
257  }
258  }
259 
260  // Loop over the arrays perform edge interpolation
261  void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId)
262  {
263  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin();
264  it != Arrays.end(); ++it)
265  {
266  (*it)->InterpolateEdge(v0, v1, t, outId);
267  }
268  }
269 
270  // Loop over the arrays and assign the null value
272  {
273  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin();
274  it != Arrays.end(); ++it)
275  {
276  (*it)->AssignNullValue(outId);
277  }
278  }
279 
280  // Extend (realloc) the arrays
281  void Realloc(vtkIdType sze)
282  {
283  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin();
284  it != Arrays.end(); ++it)
285  {
286  (*it)->Realloc(sze);
287  }
288  }
289 
290  // Only you can prevent memory leaks!
292  {
293  for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin();
294  it != Arrays.end(); ++it)
295  {
296  delete (*it);
297  }
298  }
299 
300  // Return the number of arrays
302  {
303  return Arrays.size();
304  }
305 
306 };
307 
308 
309 #include "vtkArrayListTemplate.txx"
310 
311 #endif
312 // VTK-HeaderTest-Exclude: vtkArrayListTemplate.h
void AddArrays(vtkIdType numOutPts, vtkDataSetAttributes *inPD, vtkDataSetAttributes *outPD, double nullValue=0.0, bool promote=true)
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:47
bool IsExcluded(vtkDataArray *da)
~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:287
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:54
void Realloc(vtkIdType sze)
represent and manipulate attribute data in a dataset
virtual void Copy(vtkIdType inId, vtkIdType outId)=0
vtkDataArray * AddArrayPair(vtkIdType numTuples, vtkDataArray *inArray, vtkStdString &outArrayName, double nullValue, bool promote)
void ExcludeArray(vtkDataArray *da)
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