1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkArrayListTemplate.h
Copyright (c) Kitware, Inc.
All rights reserved.
See LICENSE file for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// .NAME vtkArrayListTemplate - thread-safe and efficient data attribute manipulation
// .SECTION Description
// vtkArrayListTemplate supplements the vtkDataSetAttributes class to provide
// threaded processing of data arrays. It is also more efficient for certain
// interpolation operations. The expectation is that it will be replaced one
// day once vtkPointData, vtkCellData, vtkDataSetAttributes, and vtkFieldData
// properly support multithreading and/or are redesigned. Note that this
// implementation does not support incremental operations (like InsertNext()).
//
// Generally the way this helper class is used is to first invoke
// vtkDataSetAttributes::CopyInterpolate() or InterpolateAllocate() which
// performs the initial magic of constructing input and output arrays. Then
// the input attributes, and output attributes, are passed to initialize the
// internal structures. Essentially these internal structures are pairs of
// arrays of the same type, which can be efficently accessed and
// assigned. The operations on these array pairs (e.g., interpolation) occur
// using a typeless, virtual dispatch base class.
// .SECTION See Also
// vtkFieldData vtkDataSetAttributes vtkPointData vtkCellData
#ifndef vtkArrayListTemplate_h
#define vtkArrayListTemplate_h
#include "vtkDataArray.h"
#include "vtkDataSetAttributes.h"
#include "vtkSmartPointer.h"
#include "vtkStdString.h"
#include <vector>
#include <algorithm>
// Create a generic class supporting virtual dispatch to type-specific
// subclasses.
struct BaseArrayPair
{
vtkIdType Num;
int NumComp;
vtkSmartPointer<vtkDataArray> OutputArray;
BaseArrayPair(vtkIdType num, int numComp, vtkDataArray *outArray) :
Num(num), NumComp(numComp), OutputArray(outArray)
{
}
virtual ~BaseArrayPair()
{
}
virtual void Copy(vtkIdType inId, vtkIdType outId) = 0;
virtual void Interpolate(int numWeights, const vtkIdType *ids,
const double *weights, vtkIdType outId) = 0;
virtual void InterpolateEdge(vtkIdType v0, vtkIdType v1,
double t, vtkIdType outId) = 0;
virtual void AssignNullValue(vtkIdType outId) = 0;
virtual void Realloc(vtkIdType sze) = 0;
};
// Type specific interpolation on a matched pair of data arrays
template <typename T>
struct ArrayPair : public BaseArrayPair
{
T *Input;
T *Output;
T NullValue;
ArrayPair(T *in, T *out, vtkIdType num, int numComp, vtkDataArray *outArray, T null) :
BaseArrayPair(num,numComp,outArray), Input(in), Output(out), NullValue(null)
{
}
virtual ~ArrayPair() //calm down some finicky compilers
{
}
virtual void Copy(vtkIdType inId, vtkIdType outId)
{
for (int j=0; j < this->NumComp; ++j)
{
this->Output[outId*this->NumComp+j] = this->Input[inId*this->NumComp+j];
}
}
virtual void Interpolate(int numWeights, const vtkIdType *ids,
const double *weights, vtkIdType outId)
{
for (int j=0; j < this->NumComp; ++j)
{
double v = 0.0;
for (vtkIdType i=0; i < numWeights; ++i)
{
v += weights[i] * static_cast<double>(this->Input[ids[i]*this->NumComp+j]);
}
this->Output[outId*this->NumComp+j] = static_cast<T>(v);
}
}
virtual void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId)
{
double v;
vtkIdType numComp=this->NumComp;
for (int j=0; j < numComp; ++j)
{
v = this->Input[v0*numComp+j] +
t * (this->Input[v1*numComp+j] - this->Input[v0*numComp+j]);
this->Output[outId*numComp+j] = static_cast<T>(v);
}
}
virtual void AssignNullValue(vtkIdType outId)
{
for (int j=0; j < this->NumComp; ++j)
{
this->Output[outId*this->NumComp+j] = this->NullValue;
}
}
virtual void Realloc(vtkIdType sze)
{
this->OutputArray->WriteVoidPointer(0,sze*this->NumComp);
this->Output = static_cast<T*>(this->OutputArray->GetVoidPointer(0));
}
};
// Type specific interpolation on a pair of data arrays with different types, where the
// output type is expected to be a real type (i.e., float or double).
template <typename TInput, typename TOutput>
struct RealArrayPair : public BaseArrayPair
{
TInput *Input;
TOutput *Output;
TOutput NullValue;
RealArrayPair(TInput *in, TOutput *out, vtkIdType num, int numComp, vtkDataArray *outArray, TOutput null) :
BaseArrayPair(num,numComp,outArray), Input(in), Output(out), NullValue(null)
{
}
virtual ~RealArrayPair() //calm down some finicky compilers
{
}
virtual void Copy(vtkIdType inId, vtkIdType outId)
{
for (int j=0; j < this->NumComp; ++j)
{
this->Output[outId*this->NumComp+j] = static_cast<TOutput>(this->Input[inId*this->NumComp+j]);
}
}
virtual void Interpolate(int numWeights, const vtkIdType *ids,
const double *weights, vtkIdType outId)
{
for (int j=0; j < this->NumComp; ++j)
{
double v = 0.0;
for (vtkIdType i=0; i < numWeights; ++i)
{
v += weights[i] * static_cast<double>(this->Input[ids[i]*this->NumComp+j]);
}
this->Output[outId*this->NumComp+j] = static_cast<TOutput>(v);
}
}
virtual void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId)
{
double v;
vtkIdType numComp=this->NumComp;
for (int j=0; j < numComp; ++j)
{
v = this->Input[v0*numComp+j] +
t * (this->Input[v1*numComp+j] - this->Input[v0*numComp+j]);
this->Output[outId*numComp+j] = static_cast<TOutput>(v);
}
}
virtual void AssignNullValue(vtkIdType outId)
{
for (int j=0; j < this->NumComp; ++j)
{
this->Output[outId*this->NumComp+j] = this->NullValue;
}
}
virtual void Realloc(vtkIdType sze)
{
this->OutputArray->WriteVoidPointer(0,sze*this->NumComp);
this->Output = static_cast<TOutput*>(this->OutputArray->GetVoidPointer(0));
}
};
// Forward declarations. This makes working with vtkTemplateMacro easier.
struct ArrayList;
template <typename T>
void CreateArrayPair(ArrayList *list, T *inData, T *outData,
vtkIdType numPts, int numComp, T nullValue);
// A list of the arrays to interpolate, and a method to invoke interpolation on the list
struct ArrayList
{
// The list of arrays, and the arrays not to process
std::vector<BaseArrayPair*> Arrays;
std::vector<vtkDataArray*> ExcludedArrays;
// Add the arrays to interpolate here (from attribute data)
void AddArrays(vtkIdType numOutPts, vtkDataSetAttributes *inPD,
vtkDataSetAttributes *outPD, double nullValue=0.0,
bool promote=true);
// Add a pair of arrays (manual insertion). Returns the output array created,
// if any. No array may be created if \c inArray was previously marked as
// excluded using ExcludeArray().
vtkDataArray* AddArrayPair(vtkIdType numPts, vtkDataArray *inArray,
vtkStdString &outArrayName, double nullValue, bool promote);
// Any array excluded here is not added by AddArrays() or AddArrayPair, hence not
// processed. Also check whether an array is excluded.
void ExcludeArray(vtkDataArray *da);
bool IsExcluded(vtkDataArray *da);
// Loop over the array pairs and copy data from one to another
void Copy(vtkIdType inId, vtkIdType outId)
{
for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin();
it != Arrays.end(); ++it)
{
(*it)->Copy(inId, outId);
}
}
// Loop over the arrays and have them interpolate themselves
void Interpolate(int numWeights, const vtkIdType *ids, const double *weights, vtkIdType outId)
{
for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin();
it != Arrays.end(); ++it)
{
(*it)->Interpolate(numWeights, ids, weights, outId);
}
}
// Loop over the arrays perform edge interpolation
void InterpolateEdge(vtkIdType v0, vtkIdType v1, double t, vtkIdType outId)
{
for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin();
it != Arrays.end(); ++it)
{
(*it)->InterpolateEdge(v0, v1, t, outId);
}
}
// Loop over the arrays and assign the null value
void AssignNullValue(vtkIdType outId)
{
for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin();
it != Arrays.end(); ++it)
{
(*it)->AssignNullValue(outId);
}
}
// Extend (realloc) the arrays
void Realloc(vtkIdType sze)
{
for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin();
it != Arrays.end(); ++it)
{
(*it)->Realloc(sze);
}
}
// Only you can prevent memory leaks!
~ArrayList()
{
for (std::vector<BaseArrayPair*>::iterator it = Arrays.begin();
it != Arrays.end(); ++it)
{
delete (*it);
}
}
// Return the number of arrays
vtkIdType GetNumberOfArrays()
{
return Arrays.size();
}
};
#include "vtkArrayListTemplate.txx"
#endif
// VTK-HeaderTest-Exclude: vtkArrayListTemplate.h
|