vtkDataArray.cxx 39.6 KB
Newer Older
1
2
3
4
5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkDataArray.cxx

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7
8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9

10
11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
     PURPOSE.  See the above copyright notice for more information.
13
14
15

=========================================================================*/
#include "vtkDataArray.h"
16
#include "vtkDataArrayPrivate.txx"
17
18
#include "vtkBitArray.h"
#include "vtkCharArray.h"
19
#include "vtkDataArrayIteratorMacro.h"
20
#include "vtkDoubleArray.h"
Ken Martin's avatar
Ken Martin committed
21
#include "vtkFloatArray.h"
22
23
24
25
#include "vtkInformation.h"
#include "vtkInformationDoubleVectorKey.h"
#include "vtkInformationInformationVectorKey.h"
#include "vtkInformationVector.h"
26
#include "vtkIdTypeArray.h"
27
#include "vtkIntArray.h"
28
#include "vtkIdList.h"
Ken Martin's avatar
Ken Martin committed
29
30
#include "vtkLookupTable.h"
#include "vtkLongArray.h"
31
#include "vtkMath.h"
Ken Martin's avatar
Ken Martin committed
32
#include "vtkShortArray.h"
33
#include "vtkSignedCharArray.h"
34
#include "vtkTypedDataArrayIterator.h"
35
#include "vtkTypeTraits.h"
Ken Martin's avatar
Ken Martin committed
36
37
38
39
#include "vtkUnsignedCharArray.h"
#include "vtkUnsignedIntArray.h"
#include "vtkUnsignedLongArray.h"
#include "vtkUnsignedShortArray.h"
40

41
#include <algorithm> // for min(), max()
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
namespace {

//----------------------------------------------------------------------------
template <class InputIterator>
void vtkDeepCopySwitchOnOutput(InputIterator begin, InputIterator end,
                               vtkDataArray *outputArray)
{
  switch (outputArray->GetDataType())
    {
    vtkDataArrayIteratorMacro(outputArray,
                              std::copy(begin, end, vtkDABegin));

    default:
      vtkGenericWarningMacro("Unsupported data type "
                             << outputArray->GetDataTypeAsString() << "!");
    }
}

//--------------------------------------------------------------------------
template <class T>
inline void vtkDataArrayRoundIfNecessary(double val, T* retVal)
{
  val = std::max(val, static_cast<double>(vtkTypeTraits<T>::Min()));
  val = std::min(val, static_cast<double>(vtkTypeTraits<T>::Max()));
  *retVal = static_cast<T>((val>=0.0)?(val + 0.5):(val - 0.5));
}

//--------------------------------------------------------------------------
VTK_TEMPLATE_SPECIALIZE
inline void vtkDataArrayRoundIfNecessary(double val, double* retVal)
{
  *retVal = val;
}

//--------------------------------------------------------------------------
VTK_TEMPLATE_SPECIALIZE
inline void vtkDataArrayRoundIfNecessary(double val, float* retVal)
{
  *retVal = static_cast<float>(val);
}

//--------------------------------------------------------------------------
template <class Scalar, class Iterator>
void vtkDataArrayInterpolateTuple(Iterator from, Scalar *to, int numComp,
  vtkIdType* ids, vtkIdType numIds, double* weights)
{
  for(int i=0; i < numComp; ++i)
    {
    double c = 0;
    for(vtkIdType j=0; j < numIds; ++j)
      {
      c += weights[j] * static_cast<double>(from[ids[j]*numComp+i]);
      }
    // Round integer types. Don't round floating point types.
    vtkDataArrayRoundIfNecessary(c, to);
    ++to;
    }
}

//----------------------------------------------------------------------------
template <class Scalar, class Iterator>
void vtkDataArrayInterpolateTuple(Iterator from1, Iterator from2, Scalar* to,
  int numComp, double t)
{
  const double oneMinusT = 1.0 - t;
  while (numComp-- > 0)
    {
109
110
111
    double c = oneMinusT * (*(from1++)) + t * (*(from2++));
    // Round integer types. Don't round floating point types.
    vtkDataArrayRoundIfNecessary(c, &(*(to++)));
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
    }
}

//----------------------------------------------------------------------------
template <class InputIterator, class OutputIterator>
void vtkDataArrayGetTuplesTemplate2(vtkIdType* ids, vtkIdType* idsEnd,
                                    InputIterator inIter,
                                    OutputIterator outIter,
                                    int numComps)
{
  InputIterator inPt;
  while (ids != idsEnd)
    {
    inPt = inIter + (*(ids++) * numComps);
    outIter = std::copy(inPt, inPt + numComps, outIter);
    }
}

//----------------------------------------------------------------------------
template <class InputIterator>
void vtkDataArrayGetTuplesTemplate1(vtkIdType* ids, vtkIdType* idsEnd,
                                    InputIterator inIter,
                                    vtkDataArray *outArray,
                                    int numComps)
{
  switch (outArray->GetDataType())
    {
    vtkDataArrayIteratorMacro(outArray,
      vtkDataArrayGetTuplesTemplate2(ids, idsEnd, inIter, vtkDABegin, numComps)
      );
    default:
      vtkGenericWarningMacro("vtkDataArray::GetTuples: "
                             "Unsupported output type.");
      return;
    }
}

//----------------------------------------------------------------------------
template <class IT, class OT>
void vtkCopyTuples(IT* input, OT* output, int nComp,
                   vtkIdType p1, vtkIdType p2)
{
  vtkIdType i;
  int j;
  vtkIdType num=p2-p1+1;
  for (i=0; i<num; i++)
    {
    for (j=0; j<nComp; j++)
      {
      output[i*nComp+j] = static_cast<OT>(input[(p1+i)*nComp+j]);
      }
    }
}

//----------------------------------------------------------------------------
template <class IT>
void vtkCopyTuples1(IT* input, vtkDataArray* output,
                    vtkIdType p1, vtkIdType p2)
{
  switch (output->GetDataType())
    {
    vtkTemplateMacro(vtkCopyTuples( input,
                                    static_cast<VTK_TT *>(output->GetVoidPointer(0)),
                                    output->GetNumberOfComponents(), p1, p2) );

    default:
      vtkGenericWarningMacro("Sanity check failed: Unsupported data type "
                             << output->GetDataType() << ".");
      return;
    }
}

184
185
186
template<typename InfoType, typename KeyType>
bool hasValidKey(InfoType info, KeyType key,
                   unsigned long mtime, double range[2] )
187
{
188
  if ( info->Has( key ) )
189
    {
190
    if ( mtime <= info->GetMTime() )
191
      {
192
193
      info->Get( key, range );
      return true;
194
195
      }
    }
196
  return false;
197
198
}

199
200
201
template<typename InfoType, typename KeyType, typename ComponentKeyType>
bool hasValidKey(InfoType info, KeyType key, ComponentKeyType ckey,
                   unsigned long mtime, double range[2], int comp )
202
{
203
  if ( info->Has( key ) )
204
    {
205
    if ( mtime <= info->GetMTime() )
206
      {
207
208
      info->Get( key )->GetInformationObject(comp)->Get( ckey, range );
      return true;
209
210
      }
    }
211
  return false;
212
213
214
}

} // end anon namespace
215

216
217
vtkInformationKeyRestrictedMacro(vtkDataArray, COMPONENT_RANGE, DoubleVector, 2);
vtkInformationKeyRestrictedMacro(vtkDataArray, L2_NORM_RANGE, DoubleVector, 2);
218

219
//----------------------------------------------------------------------------
220
// Construct object with default tuple dimension (number of components) of 1.
221
vtkDataArray::vtkDataArray()
222
{
223
  this->LookupTable = NULL;
224
225
  this->Range[0] = 0;
  this->Range[1] = 0;
226
227
}

228
//----------------------------------------------------------------------------
229
230
231
232
233
234
vtkDataArray::~vtkDataArray()
{
  if ( this->LookupTable )
    {
    this->LookupTable->Delete();
    }
235
  this->SetName(0);
236
237
}

Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
238
239
240
241
242
243
244
245
//----------------------------------------------------------------------------
void vtkDataArray::DeepCopy(vtkAbstractArray* aa)
{
  if ( aa == NULL )
    {
    return;
    }

246
  vtkDataArray *da = vtkDataArray::FastDownCast(aa);
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
247
248
  if (da == NULL)
    {
249
250
    vtkErrorMacro(<< "Input array is not a vtkDataArray ("
                  << aa->GetClassName() << ")");
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
251
252
253
254
255
256
    return;
    }

  this->DeepCopy(da);
}

257
//----------------------------------------------------------------------------
258
259
260
261
262
//Normally subclasses will do this when the input and output type of the
//DeepCopy are the same. When they are not the same, then we use the
//templated code below.
void vtkDataArray::DeepCopy(vtkDataArray *da)
{
263
264
265
266
267
268
  // Match the behavior of the old AttributeData
  if ( da == NULL )
    {
    return;
    }

269
  if ( this != da )
270
    {
271
272
    this->Superclass::DeepCopy( da ); // copy Information object

273
    vtkIdType numTuples = da->GetNumberOfTuples();
274
    this->NumberOfComponents = da->NumberOfComponents;
275
276
    this->SetNumberOfTuples(numTuples);

277
    if (numTuples > 0)
278
      {
279
280
281
282
283
284
285
286
287
288
289
290
291
      switch (da->GetDataType())
        {
        vtkDataArrayIteratorMacro(
          da, vtkDeepCopySwitchOnOutput(vtkDABegin, vtkDAEnd, this)
          );

        case VTK_BIT:
          {//bit not supported, using generic double API
          for (vtkIdType i=0; i < numTuples; i++)
            {
            this->SetTuple(i, da->GetTuple(i));
            }
          break;
292
293
          }

294
295
296
        default:
          vtkErrorMacro("Unsupported data type " << da->GetDataType() << "!");
        }
297
      }
Berk Geveci's avatar
Berk Geveci committed
298
299
300
301
302
303
304

    this->SetLookupTable(0);
    if (da->LookupTable)
      {
      this->LookupTable = da->LookupTable->NewInstance();
      this->LookupTable->DeepCopy(da->LookupTable);
      }
305
    }
306
307

  this->Squeeze();
308
309
}

310
//----------------------------------------------------------------------------
311
// These can be overridden for more efficiency
Ken Martin's avatar
Ken Martin committed
312
double vtkDataArray::GetComponent(vtkIdType i, int j)
313
{
Ken Martin's avatar
Ken Martin committed
314
  double *tuple=new double[this->NumberOfComponents], c;
315
316
317
318
319
320
321
322

  this->GetTuple(i,tuple);
  c =  tuple[j];
  delete [] tuple;

  return c;
}

323
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
324
void vtkDataArray::SetComponent(vtkIdType i, int j, double c)
325
{
Ken Martin's avatar
Ken Martin committed
326
  double *tuple=new double[this->NumberOfComponents];
327
328
329
330
331
332
333

  if ( i < this->GetNumberOfTuples() )
    {
    this->GetTuple(i,tuple);
    }
  else
    {
Jim Miller's avatar
Jim Miller committed
334
335
336
337
    for (int k=0; k<this->NumberOfComponents; k++)
      {
      tuple[k] = 0.0;
      }
338
339
340
341
342
343
344
345
    }

  tuple[j] = c;
  this->SetTuple(i,tuple);

  delete [] tuple;
}

346
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
347
void vtkDataArray::InsertComponent(vtkIdType i, int j, double c)
348
{
Ken Martin's avatar
Ken Martin committed
349
  double *tuple=new double[this->NumberOfComponents];
350
351
352
353
354
355
356

  if ( i < this->GetNumberOfTuples() )
    {
    this->GetTuple(i,tuple);
    }
  else
    {
Jim Miller's avatar
Jim Miller committed
357
358
359
360
    for (int k=0; k<this->NumberOfComponents; k++)
      {
      tuple[k] = 0.0;
      }
361
362
363
364
365
366
367
368
    }

  tuple[j] = c;
  this->InsertTuple(i,tuple);

  delete [] tuple;
}

369
//----------------------------------------------------------------------------
370
void vtkDataArray::GetData(vtkIdType tupleMin, vtkIdType tupleMax, int compMin,
Ken Martin's avatar
Ken Martin committed
371
                           int compMax, vtkDoubleArray* data)
372
{
373
374
  int i;
  vtkIdType j;
375
  int numComp=this->GetNumberOfComponents();
Ken Martin's avatar
Ken Martin committed
376
377
  double *tuple=new double[numComp];
  double *ptr=data->WritePointer(0,(tupleMax-tupleMin+1)*(compMax-compMin+1));
378

379
380
381
382
383
384
385
386
  for (j=tupleMin; j <= tupleMax; j++)
    {
    this->GetTuple(j,tuple);
    for (i=compMin; i <= compMax; i++)
      {
      *ptr++ = tuple[i];
      }
    }
Will Schroeder's avatar
Will Schroeder committed
387
388
389
  delete [] tuple;
}

Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
390
391
392
393
394
395
396
397
398
//----------------------------------------------------------------------------
// Interpolate array value from other array value given the
// indices and associated interpolation weights.
// This method assumes that the two arrays are of the same time.
void vtkDataArray::InterpolateTuple(vtkIdType i, vtkIdList *ptIndices,
  vtkAbstractArray* source,  double* weights)
{
  if (this->GetDataType() != source->GetDataType())
    {
399
    vtkErrorMacro("Cannot InterpolateValue from array of type "
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
400
401
402
      << source->GetDataTypeAsString());
    return;
    }
403

404
  vtkDataArray* fromData = vtkDataArray::FastDownCast(source);
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
405
406
407
408
409
410
411
412
  if (fromData)
    {
    int numComp = fromData->GetNumberOfComponents();
    vtkIdType j, numIds=ptIndices->GetNumberOfIds();
    vtkIdType *ids=ptIndices->GetPointer(0);
    vtkIdType idx= i*numComp;
    double c;

413
414
415
416
    // Note that we must call WriteVoidPointer before GetVoidPointer
    // in case WriteVoidPointer reallocates memory and fromData ==
    // this. The vtkBitArray implementation doesn't use pointers, so skip
    // the resizing in this case.
417
418
    int dataType = fromData->GetDataType();
    void* vto = dataType != VTK_BIT ?
419
420
          this->WriteVoidPointer(idx, numComp) : 0;

421
    switch (dataType)
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
422
423
424
      {
    case VTK_BIT:
        {
425
426
        vtkBitArray *from=static_cast<vtkBitArray *>(fromData);
        vtkBitArray *to=static_cast<vtkBitArray *>(this);
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
427
428
429
430
431
432
        for (int k=0; k<numComp; k++)
          {
          for (c=0, j=0; j<numIds; j++)
            {
            c += weights[j]*from->GetValue(ids[j]*numComp+k);
            }
433
          to->InsertValue(idx+k, static_cast<int>(c));
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
434
435
436
          }
        }
      break;
437
438
439
440
        vtkDataArrayIteratorMacro(fromData,
          vtkDataArrayInterpolateTuple(vtkDABegin,
                                       static_cast<vtkDAValueType*>(vto),
                                       numComp, ids, numIds, weights)
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
441
442
443
444
445
446
447
448
449
      );
    default:
      vtkErrorMacro("Unsupported data type " << fromData->GetDataType()
        << " during interpolation!");
      }
    }
}

//----------------------------------------------------------------------------
450
451
452
// Interpolate value from the two values, p1 and p2, and an
// interpolation factor, t. The interpolation factor ranges from (0,1),
// with t=0 located at p1. This method assumes that the three arrays are of
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
453
454
// the same type. p1 is value at index id1 in fromArray1, while, p2 is
// value at index id2 in fromArray2.
455
456
void vtkDataArray::InterpolateTuple(vtkIdType i,
  vtkIdType id1, vtkAbstractArray* source1,
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
457
458
459
  vtkIdType id2, vtkAbstractArray* source2, double t)
{
  int type = this->GetDataType();
460

Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
461
462
463
464
  if (type != source1->GetDataType() || type != source2->GetDataType())
    {
    vtkErrorMacro("All arrays to InterpolateValue must be of same type.");
    return;
465
    }
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
466

467
  int k, numComp = source1->GetNumberOfComponents();
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
468
469
470
  double c;
  vtkIdType loc = i * numComp;

471
  switch (type)
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
472
473
474
    {
    case VTK_BIT:
      {
475
476
477
      vtkBitArray *from1 = static_cast<vtkBitArray *>(source1);
      vtkBitArray *from2 = static_cast<vtkBitArray *>(source2);
      vtkBitArray *to = static_cast<vtkBitArray *>(this);
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
478
479
480
      for (k=0; k<numComp; k++)
        {
        c = from1->GetValue(id1) + t * (from2->GetValue(id2) - from1->GetValue(id1));
481
        to->InsertValue(loc + k, static_cast<int>(c));
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
482
483
484
        }
      }
      break;
485
486
487
      // Note that we must call WriteVoidPointer before GetVoidPointer/creating
      // iterators in case WriteVoidPointer reallocates memory and
      // fromData1==this or fromData2==this.
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
488
    vtkTemplateMacro(
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
      // If either of the source arrays are mapped, use iterators. Otherwise,
      // void pointers are safe.
      if (source1->HasStandardMemoryLayout() &&
          source2->HasStandardMemoryLayout())
        {
        // Use pointers:
        void *vto = this->WriteVoidPointer(loc, numComp);
        void *vfrom1 = source1->GetVoidPointer(id1 * numComp);
        void *vfrom2 = source2->GetVoidPointer(id2 * numComp);
        vtkDataArrayInterpolateTuple<VTK_TT>(static_cast<VTK_TT*>(vfrom1),
                                             static_cast<VTK_TT*>(vfrom2),
                                             static_cast<VTK_TT*>(vto),
                                             numComp, t);
        }
      else
        {
        vtkTypedDataArray<VTK_TT> *tfrom1 =
            vtkTypedDataArray<VTK_TT>::FastDownCast(source1);
        vtkTypedDataArray<VTK_TT> *tfrom2 =
            vtkTypedDataArray<VTK_TT>::FastDownCast(source2);
        if (!tfrom1 || !tfrom2)
          {
          vtkErrorMacro(<<"Cannot call this function with non-standard arrays "
                        "unless all arrays are vtkTypedDataArray subclasses.");
          return;
          }
        VTK_TT *vto = static_cast<VTK_TT*>(
            this->WriteVoidPointer(loc, numComp));
        vtkDataArrayInterpolateTuple<VTK_TT>(
            vtkTypedDataArrayIterator<VTK_TT>(tfrom1, id1 * numComp),
            vtkTypedDataArrayIterator<VTK_TT>(tfrom2, id2 * numComp),
            vto, numComp, t);
        }
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
522
523
      );
    default:
524
      vtkErrorMacro("Unsupported data type " << type
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
525
526
527
528
529
                    << " during interpolation!");
    }

}

530
//----------------------------------------------------------------------------
531
532
533
534
535
536
537
void vtkDataArray::CreateDefaultLookupTable()
{
  if ( this->LookupTable )
    {
    this->LookupTable->UnRegister(this);
    }
  this->LookupTable = vtkLookupTable::New();
538
539
  // make sure it is built
  // otherwise problems with InsertScalar trying to map through
540
541
542
543
  // non built lut
  this->LookupTable->Build();
}

544
//----------------------------------------------------------------------------
545
546
void vtkDataArray::SetLookupTable(vtkLookupTable* lut)
{
547
  if ( this->LookupTable != lut )
548
549
550
551
552
553
    {
    if ( this->LookupTable )
      {
      this->LookupTable->UnRegister(this);
      }
    this->LookupTable = lut;
Sean McBride's avatar
Sean McBride committed
554
555
556
557
    if ( this->LookupTable )
      {
      this->LookupTable->Register(this);
      }
558
559
560
561
    this->Modified();
    }
}

562
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
563
double* vtkDataArray::GetTupleN(vtkIdType i, int n)
564
565
566
567
568
{
  int numComp = this->GetNumberOfComponents();
  if (numComp != n)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
569
                  << numComp << " != " << n);
570
571
572
573
    }
  return this->GetTuple(i);
}

574
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
575
double vtkDataArray::GetTuple1(vtkIdType i)
576
577
578
579
580
{
  int numComp = this->GetNumberOfComponents();
  if (numComp != 1)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
581
                  << numComp << " != 1");
582
583
584
    }
  return *(this->GetTuple(i));
}
585
586

//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
587
double* vtkDataArray::GetTuple2(vtkIdType i)
588
{
589
  return this->GetTupleN(i, 2);
590
}
591
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
592
double* vtkDataArray::GetTuple3(vtkIdType i)
593
{
594
  return this->GetTupleN(i, 3);
595
}
596
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
597
double* vtkDataArray::GetTuple4(vtkIdType i)
598
{
599
  return this->GetTupleN(i, 4);
600
}
601
//----------------------------------------------------------------------------
Sebastien Jourdain's avatar
Sebastien Jourdain committed
602
603
604
605
606
double* vtkDataArray::GetTuple6(vtkIdType i)
{
  return this->GetTupleN(i, 6);
}
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
607
double* vtkDataArray::GetTuple9(vtkIdType i)
608
{
609
  return this->GetTupleN(i, 9);
610
611
}

612
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
613
void vtkDataArray::SetTuple1(vtkIdType i, double value)
614
615
616
617
618
{
  int numComp = this->GetNumberOfComponents();
  if (numComp != 1)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
619
                  << numComp << " != 1");
620
621
622
    }
  this->SetTuple(i, &value);
}
623
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
624
void vtkDataArray::SetTuple2(vtkIdType i, double val0, double val1)
625
{
Ken Martin's avatar
Ken Martin committed
626
  double tuple[2];
627
628
629
630
  int numComp = this->GetNumberOfComponents();
  if (numComp != 2)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
631
                  << numComp << " != 2");
632
633
    }
  tuple[0] = val0;
Berk Geveci's avatar
Typo.    
Berk Geveci committed
634
  tuple[1] = val1;
635
636
  this->SetTuple(i, tuple);
}
637
//----------------------------------------------------------------------------
638
void vtkDataArray::SetTuple3(vtkIdType i, double val0, double val1,
639
                             double val2)
640
{
Ken Martin's avatar
Ken Martin committed
641
  double tuple[3];
642
643
644
645
  int numComp = this->GetNumberOfComponents();
  if (numComp != 3)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
646
                  << numComp << " != 3");
647
648
649
650
651
652
    }
  tuple[0] = val0;
  tuple[1] = val1;
  tuple[2] = val2;
  this->SetTuple(i, tuple);
}
653
//----------------------------------------------------------------------------
654
void vtkDataArray::SetTuple4(vtkIdType i, double val0, double val1,
Ken Martin's avatar
Ken Martin committed
655
                             double val2, double val3)
656
{
Ken Martin's avatar
Ken Martin committed
657
  double tuple[4];
658
659
660
661
  int numComp = this->GetNumberOfComponents();
  if (numComp != 4)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
662
                  << numComp << " != 4");
663
664
665
666
667
668
669
    }
  tuple[0] = val0;
  tuple[1] = val1;
  tuple[2] = val2;
  tuple[3] = val3;
  this->SetTuple(i, tuple);
}
670
//----------------------------------------------------------------------------
Sebastien Jourdain's avatar
Sebastien Jourdain committed
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
void vtkDataArray::SetTuple6(vtkIdType i, double val0, double val1,
                             double val2, double val3,
                             double val4, double val5)
{
  double tuple[6];
  int numComp = this->GetNumberOfComponents();
  if (numComp != 6)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
                  << numComp << " != 6");
    }
  tuple[0] = val0;
  tuple[1] = val1;
  tuple[2] = val2;
  tuple[3] = val3;
  tuple[4] = val4;
  tuple[5] = val5;
  this->SetTuple(i, tuple);
}
//----------------------------------------------------------------------------
691
692
void vtkDataArray::SetTuple9(vtkIdType i, double val0, double val1,
                             double val2, double val3, double val4,
693
                             double val5, double val6, double val7, double val8)
694
{
Ken Martin's avatar
Ken Martin committed
695
  double tuple[9];
696
697
698
699
  int numComp = this->GetNumberOfComponents();
  if (numComp != 9)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
700
                  << numComp << " != 9");
701
702
703
704
705
706
707
708
709
710
711
712
713
    }
  tuple[0] = val0;
  tuple[1] = val1;
  tuple[2] = val2;
  tuple[3] = val3;
  tuple[4] = val4;
  tuple[5] = val5;
  tuple[6] = val6;
  tuple[7] = val7;
  tuple[8] = val8;
  this->SetTuple(i, tuple);
}

714
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
715
void vtkDataArray::InsertTuple1(vtkIdType i, double value)
716
717
718
719
720
{
  int numComp = this->GetNumberOfComponents();
  if (numComp != 1)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
721
                  << numComp << " != 1");
722
723
724
    }
  this->InsertTuple(i, &value);
}
725
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
726
void vtkDataArray::InsertTuple2(vtkIdType i, double val0, double val1)
727
{
Ken Martin's avatar
Ken Martin committed
728
  double tuple[2];
729
730
731
732
  int numComp = this->GetNumberOfComponents();
  if (numComp != 2)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
733
                  << numComp << " != 2");
734
735
    }
  tuple[0] = val0;
Berk Geveci's avatar
Typo.    
Berk Geveci committed
736
  tuple[1] = val1;
737
738
  this->InsertTuple(i, tuple);
}
739
//----------------------------------------------------------------------------
740
void vtkDataArray::InsertTuple3(vtkIdType i, double val0, double val1,
Ken Martin's avatar
Ken Martin committed
741
                                double val2)
742
{
Ken Martin's avatar
Ken Martin committed
743
  double tuple[3];
744
745
746
747
  int numComp = this->GetNumberOfComponents();
  if (numComp != 3)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
748
                  << numComp << " != 3");
749
750
751
752
753
754
    }
  tuple[0] = val0;
  tuple[1] = val1;
  tuple[2] = val2;
  this->InsertTuple(i, tuple);
}
755
//----------------------------------------------------------------------------
756
void vtkDataArray::InsertTuple4(vtkIdType i, double val0, double val1,
Ken Martin's avatar
Ken Martin committed
757
                                double val2, double val3)
758
{
Ken Martin's avatar
Ken Martin committed
759
  double tuple[4];
760
761
762
763
  int numComp = this->GetNumberOfComponents();
  if (numComp != 4)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
764
                  << numComp << " != 4");
765
766
767
768
769
770
771
    }
  tuple[0] = val0;
  tuple[1] = val1;
  tuple[2] = val2;
  tuple[3] = val3;
  this->InsertTuple(i, tuple);
}
772
//----------------------------------------------------------------------------
773
774
void vtkDataArray::InsertTuple9(vtkIdType i, double val0, double val1,
                                double val2,  double val3, double val4,
Ken Martin's avatar
Ken Martin committed
775
                                double val5, double val6,double val7, double val8)
776
{
Ken Martin's avatar
Ken Martin committed
777
  double tuple[9];
778
779
780
781
  int numComp = this->GetNumberOfComponents();
  if (numComp != 9)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
782
                  << numComp << " != 9");
783
784
785
786
787
788
789
790
791
792
793
794
795
    }
  tuple[0] = val0;
  tuple[1] = val1;
  tuple[2] = val2;
  tuple[3] = val3;
  tuple[4] = val4;
  tuple[5] = val5;
  tuple[6] = val6;
  tuple[7] = val7;
  tuple[8] = val8;
  this->InsertTuple(i, tuple);
}

796
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
797
void vtkDataArray::InsertNextTuple1(double value)
798
799
800
801
802
{
  int numComp = this->GetNumberOfComponents();
  if (numComp != 1)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
803
                  << numComp << " != 1");
804
805
806
    }
  this->InsertNextTuple(&value);
}
807
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
808
void vtkDataArray::InsertNextTuple2(double val0, double val1)
809
{
Ken Martin's avatar
Ken Martin committed
810
  double tuple[2];
811
812
813
814
  int numComp = this->GetNumberOfComponents();
  if (numComp != 2)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
815
                  << numComp << " != 2");
816
817
    }
  tuple[0] = val0;
Ken Martin's avatar
Ken Martin committed
818
  tuple[1] = val1;
819
820
  this->InsertNextTuple(tuple);
}
821
//----------------------------------------------------------------------------
822
void vtkDataArray::InsertNextTuple3(double val0, double val1,
Ken Martin's avatar
Ken Martin committed
823
                                    double val2)
824
{
Ken Martin's avatar
Ken Martin committed
825
  double tuple[3];
826
827
828
829
  int numComp = this->GetNumberOfComponents();
  if (numComp != 3)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
830
                  << numComp << " != 3");
831
832
833
834
835
836
    }
  tuple[0] = val0;
  tuple[1] = val1;
  tuple[2] = val2;
  this->InsertNextTuple(tuple);
}
837
//----------------------------------------------------------------------------
838
void vtkDataArray::InsertNextTuple4(double val0, double val1,
Ken Martin's avatar
Ken Martin committed
839
                                    double val2, double val3)
840
{
Ken Martin's avatar
Ken Martin committed
841
  double tuple[4];
842
843
844
845
  int numComp = this->GetNumberOfComponents();
  if (numComp != 4)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
846
                  << numComp << " != 4");
847
848
849
850
851
852
853
    }
  tuple[0] = val0;
  tuple[1] = val1;
  tuple[2] = val2;
  tuple[3] = val3;
  this->InsertNextTuple(tuple);
}
854
//----------------------------------------------------------------------------
855
856
857
void vtkDataArray::InsertNextTuple9(double val0, double val1,
                                    double val2,  double val3, double val4,
                                    double val5, double val6,double val7,
Ken Martin's avatar
Ken Martin committed
858
                                    double val8)
859
{
Ken Martin's avatar
Ken Martin committed
860
  double tuple[9];
861
862
863
864
  int numComp = this->GetNumberOfComponents();
  if (numComp != 9)
    {
    vtkErrorMacro("The number of components do not match the number requested: "
865
                  << numComp << " != 9");
866
867
868
869
870
871
872
873
874
875
876
877
878
    }
  tuple[0] = val0;
  tuple[1] = val1;
  tuple[2] = val2;
  tuple[3] = val3;
  tuple[4] = val4;
  tuple[5] = val5;
  tuple[6] = val6;
  tuple[7] = val7;
  tuple[8] = val8;
  this->InsertNextTuple(tuple);
}

879
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
880
881
unsigned long vtkDataArray::GetActualMemorySize()
{
882
  vtkIdType numPrims;
883
  double size;
Kyle Lutz's avatar
Kyle Lutz committed
884
  // The allocated array may be larger than the number of primitives used.
Ken Martin's avatar
Ken Martin committed
885
886
887
888
  //numPrims = this->GetNumberOfTuples() * this->GetNumberOfComponents();
  numPrims = this->GetSize();

  size = vtkDataArray::GetDataTypeSize(this->GetDataType());
Will Schroeder's avatar
Will Schroeder committed
889

890
  // kibibytes
891
892
  return static_cast<unsigned long>(ceil((size*static_cast<double>(numPrims)
                                           )/1024.0));
Will Schroeder's avatar
Will Schroeder committed
893
894
}

895
//----------------------------------------------------------------------------
896
897
vtkDataArray* vtkDataArray::CreateDataArray(int dataType)
{
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
898
899
900
  vtkAbstractArray* aa = vtkAbstractArray::CreateArray(dataType);
  vtkDataArray* da = vtkDataArray::SafeDownCast(aa);
  if (!da && aa)
901
    {
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
902
903
    // Requested array is not a vtkDataArray. Delete the allocated array.
    aa->Delete();
904
    }
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
905
  return da;
906
907
}

908
//----------------------------------------------------------------------------
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
909
void vtkDataArray::GetTuples(vtkIdList *ptIds, vtkAbstractArray *aa)
910
{
911
912
  vtkDataArray *outArray = vtkDataArray::FastDownCast(aa);
  if (!outArray)
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
913
914
915
916
    {
    vtkWarningMacro("Input is not a vtkDataArray.");
    return;
    }
917

918
  if ((outArray->GetNumberOfComponents() != this->GetNumberOfComponents()))
919
920
921
922
923
    {
    vtkWarningMacro("Number of components for input and output do not match");
    return;
    }

924
925
  vtkIdType* ids = ptIds->GetPointer(0);
  vtkIdType* idsEnd = ptIds->GetPointer(ptIds->GetNumberOfIds());
926

927
928
  switch (this->GetDataType())
    {
929
930
931
932
933
    vtkDataArrayIteratorMacro(this,
      vtkDataArrayGetTuplesTemplate1(ids, idsEnd, vtkDABegin, outArray,
                                     this->NumberOfComponents)
      );
    default: // Fallback to the double interface
934
935
936
      vtkIdType num=ptIds->GetNumberOfIds();
      for (vtkIdType i=0; i<num; i++)
        {
937
        outArray->SetTuple(i, this->GetTuple(ptIds->GetId(i)));
938
939
        }
      break;
940
941
942
    }
}

943
//----------------------------------------------------------------------------
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
944
void vtkDataArray::GetTuples(vtkIdType p1, vtkIdType p2, vtkAbstractArray *aa)
945
{
Utkarsh Ayachit's avatar
ENH:    
Utkarsh Ayachit committed
946
947
948
949
950
951
  vtkDataArray* da = vtkDataArray::SafeDownCast(aa);
  if (!da)
    {
    vtkWarningMacro("Input is not a vtkDataArray.");
    return;
    }
952
953
954
955
956
957
958
959
960

  if ((da->GetNumberOfComponents() != this->GetNumberOfComponents()))
    {
    vtkWarningMacro("Number of components for input and output do not match");
    return;
    }

  switch (this->GetDataType())
    {
961
    vtkTemplateMacro(vtkCopyTuples1( static_cast<VTK_TT *>(this->GetVoidPointer(0)), da,
962
                                     p1, p2 ) );
963
    // This is not supported by the template macro.
Ken Martin's avatar
Ken Martin committed
964
    // Switch to using the double interface.
965
966
967
968
969
970
971
972
973
974
    case VTK_BIT:
      {
      vtkIdType num=p2-p1+1;
      for (vtkIdType i=0; i<num; i++)
        {
        da->SetTuple(i,this->GetTuple(p1+i));
        }
      }
      break;
    default:
975
976
      vtkErrorMacro("Sanity check failed: Unsupported data type "
                    << this->GetDataType() << ".");
977
978
979
      return;
    }
}
980

981
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
982
void vtkDataArray::FillComponent(int j, double c)
983
984
985
986
987
988
989
{
  if (j < 0 || j >= this->GetNumberOfComponents())
    {
    vtkErrorMacro(<< "Specified component " << j << " is not in [0, "
    << this->GetNumberOfComponents() << ")" );
    return;
    }
990

991
992
993
994
995
996
997
998
999
  vtkIdType i;

  for (i = 0; i < this->GetNumberOfTuples(); i++)
    {
    this->SetComponent(i, j, c);
    }
}


1000
//----------------------------------------------------------------------------
1001
1002
void vtkDataArray::CopyComponent(int j, vtkDataArray *from,
                                 int fromComponent)
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
{
  if (this->GetNumberOfTuples() != from->GetNumberOfTuples())
    {
    vtkErrorMacro(<< "Number of tuples in 'from' ("
    << from->GetNumberOfTuples() << ") and 'to' ("
    << this->GetNumberOfTuples() << ") do not match.");
    return;
    }

  if (j < 0 || j >= this->GetNumberOfComponents())
    {
    vtkErrorMacro(<< "Specified component " << j << " in 'to' array is not in [0, "
    << this->GetNumberOfComponents() << ")" );
    return;
    }

  if (fromComponent < 0 || fromComponent >= from->GetNumberOfComponents())
    {
    vtkErrorMacro(<< "Specified component " << fromComponent << " in 'from' array is not in [0, "
    << from->GetNumberOfComponents() << ")" );
    return;
    }

  vtkIdType i;
  for (i = 0; i < this->GetNumberOfTuples(); i++)
    {
    this->SetComponent(i, j, from->GetComponent(i, fromComponent));
    }
}

1033
//----------------------------------------------------------------------------
Ken Martin's avatar
Ken Martin committed
1034
double vtkDataArray::GetMaxNorm()
1035
1036
{
  vtkIdType i;
Ken Martin's avatar
Ken Martin committed
1037
  double norm, maxNorm;
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
  int nComponents = this->GetNumberOfComponents();

  maxNorm = 0.0;
  for (i=0; i<this->GetNumberOfTuples(); i++)
    {
    norm = vtkMath::Norm(this->GetTuple(i), nComponents);
    if ( norm > maxNorm )
      {
      maxNorm = norm;
      }
    }

  return maxNorm;
}

Burlen Loring's avatar
   
Burlen Loring committed
1053
//----------------------------------------------------------------------------
1054
int vtkDataArray::CopyInformation(vtkInformation* infoFrom, int deep)
Burlen Loring's avatar
   
Burlen Loring committed
1055
1056
{
  // Copy everything + give base classes a chance to
1057
  // Exclude keys which they don't want copied.
Burlen Loring's avatar
   
Burlen Loring committed
1058
1059
  this->Superclass::CopyInformation(infoFrom,deep);

1060
  // Remove any keys we own that are not to be copied here.
Burlen Loring's avatar
   
Burlen Loring committed
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
  vtkInformation *myInfo=this->GetInformation();
  // Range:
  if (myInfo->Has( L2_NORM_RANGE() ))
    {
    myInfo->Remove( L2_NORM_RANGE() );
    }

  return 1;
}

1071
//----------------------------------------------------------------------------
1072
void vtkDataArray::ComputeRange(double range[2], int comp)
1073
{
1074
1075
  //this method needs a large refactoring to be way easier to read

Burlen Loring's avatar
   
Burlen Loring committed
1076
1077
1078
1079
1080
  if ( comp >= this->NumberOfComponents )
    { // Ignore requests for nonexistent components.
    return;
    }
  // If we got component -1 on a vector array, compute vector magnitude.
1081
1082
1083
1084
1085
  if (comp < 0 && this->NumberOfComponents == 1)
    {
    comp = 0;
    }

1086
1087
1088
  range[0] = vtkTypeTraits<double>::Max();
  range[1] = vtkTypeTraits<double>::Min();

Burlen Loring's avatar
   
Burlen Loring committed
1089
  vtkInformation* info = this->GetInformation();
1090
1091
  vtkInformationDoubleVectorKey* rkey;
  if ( comp < 0 )
1092
    {
1093
    rkey = L2_NORM_RANGE();
1094
1095
1096
1097
1098
1099
1100
1101
    //hasValidKey will update range to the cached value if it exists.
    if( !hasValidKey(info,rkey,this->GetMTime(),range) )
      {

      this->ComputeVectorRange(range);
      info->Set( rkey, range, 2 );
      }
    return;
1102
1103
1104
1105
1106
    }
  else
    {
    rkey = COMPONENT_RANGE();

1107
1108
1109
    //hasValidKey will update range to the cached value if it exists.
    if( !hasValidKey(info, PER_COMPONENT(), rkey,
                       this->GetMTime(), range, comp))
1110
      {
1111
1112
1113
      double* allCompRanges = new double[this->NumberOfComponents*2];
      const bool computed = this->ComputeScalarRange(allCompRanges);
      if(computed)
1114
        {
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
        //construct the keys and add them to the info object
        vtkInformationVector* infoVec = vtkInformationVector::New();
        info->Set( PER_COMPONENT(), infoVec );

        infoVec->SetNumberOfInformationObjects( this->NumberOfComponents );
        for ( int i = 0; i < this->NumberOfComponents; ++i )
          {
          infoVec->GetInformationObject( i )->Set( rkey,
                                                   allCompRanges+(i*2),
                                                   2 );
          }
        infoVec->FastDelete();

        //update the range passed in since we have a valid range.
        range[0] = allCompRanges[comp*2];
        range[1] = allCompRanges[(comp*2)+1];
1131
        }
1132
      delete[] allCompRanges;
1133
1134
      }
    }
Burlen Loring's avatar
   
Burlen Loring committed
1135
}
1136

1137
//----------------------------------------------------------------------------
1138
bool vtkDataArray::ComputeScalarRange(double* ranges)
1139
{
1140
  bool computed = false;
1141
1142
1143
  switch (this->GetDataType())
      {
      vtkDataArrayIteratorMacro(this,
1144
        computed = vtkDataArrayPrivate::DoComputeScalarRange<vtkDAValueType>(
1145
                                         vtkDABegin, vtkDAEnd,
1146
1147
                                         this->GetNumberOfComponents(),
                                         ranges)
1148
1149
1150
1151
      );
      default:
        break;
      }
1152
  return computed;
Burlen Loring's avatar
   
Burlen Loring committed
1153
1154
}

1155
//-----------------------------------------------------------------------------
1156
bool vtkDataArray::ComputeVectorRange(double range[2])