forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkAbstractArray.cxx
810 lines (729 loc) · 24.3 KB
/
vtkAbstractArray.cxx
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAbstractArray.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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.
=========================================================================*/
// Hide VTK_DEPRECATED_IN_9_1_0() warnings for this class.
#define VTK_DEPRECATION_LEVEL 0
#include "vtkAbstractArray.h"
#include "vtkBitArray.h"
#include "vtkCharArray.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkIdList.h"
#include "vtkIdTypeArray.h"
#include "vtkInformation.h"
#include "vtkInformationDoubleVectorKey.h"
#include "vtkInformationInformationVectorKey.h"
#include "vtkInformationIntegerKey.h"
#include "vtkInformationVariantVectorKey.h"
#include "vtkInformationVector.h"
#include "vtkIntArray.h"
#include "vtkLongArray.h"
#include "vtkLongLongArray.h"
#include "vtkMath.h"
#include "vtkMinimalStandardRandomSequence.h"
#include "vtkNew.h"
#include "vtkShortArray.h"
#include "vtkSignedCharArray.h"
#include "vtkStringArray.h"
#include "vtkUnicodeString.h" // for vtkSuperExtraExtendedTemplateMacro
#include "vtkUnicodeStringArray.h"
#include "vtkUnsignedCharArray.h"
#include "vtkUnsignedIntArray.h"
#include "vtkUnsignedLongArray.h"
#include "vtkUnsignedLongLongArray.h"
#include "vtkUnsignedShortArray.h"
#include "vtkVariantArray.h"
#include <algorithm>
#include <cmath>
#include <iterator>
#include <set>
vtkInformationKeyMacro(vtkAbstractArray, GUI_HIDE, Integer);
vtkInformationKeyMacro(vtkAbstractArray, PER_COMPONENT, InformationVector);
vtkInformationKeyMacro(vtkAbstractArray, PER_FINITE_COMPONENT, InformationVector);
vtkInformationKeyMacro(vtkAbstractArray, DISCRETE_VALUES, VariantVector);
vtkInformationKeyRestrictedMacro(
vtkAbstractArray, DISCRETE_VALUE_SAMPLE_PARAMETERS, DoubleVector, 2);
namespace
{
typedef std::vector<vtkStdString*> vtkInternalComponentNameBase;
}
class vtkAbstractArray::vtkInternalComponentNames : public vtkInternalComponentNameBase
{
};
//------------------------------------------------------------------------------
// Construct object with sane defaults.
vtkAbstractArray::vtkAbstractArray()
{
this->Size = 0;
this->MaxId = -1;
this->NumberOfComponents = 1;
this->Name = nullptr;
this->RebuildArray = false;
this->Information = nullptr;
this->ComponentNames = nullptr;
this->MaxDiscreteValues = vtkAbstractArray::MAX_DISCRETE_VALUES; // 32
}
//------------------------------------------------------------------------------
vtkAbstractArray::~vtkAbstractArray()
{
if (this->ComponentNames)
{
for (unsigned int i = 0; i < this->ComponentNames->size(); ++i)
{
delete this->ComponentNames->at(i);
}
this->ComponentNames->clear();
delete this->ComponentNames;
this->ComponentNames = nullptr;
}
this->SetName(nullptr);
this->SetInformation(nullptr);
}
//------------------------------------------------------------------------------
void vtkAbstractArray::SetComponentName(vtkIdType component, const char* name)
{
if (component < 0 || name == nullptr)
{
return;
}
unsigned int index = static_cast<unsigned int>(component);
if (this->ComponentNames == nullptr)
{
// delayed allocate
this->ComponentNames = new vtkAbstractArray::vtkInternalComponentNames();
}
if (index == this->ComponentNames->size())
{
// the array isn't large enough, so we will resize
this->ComponentNames->push_back(new vtkStdString(name));
return;
}
else if (index > this->ComponentNames->size())
{
this->ComponentNames->resize(index + 1, nullptr);
}
// replace an existing element
vtkStdString* compName = this->ComponentNames->at(index);
if (!compName)
{
compName = new vtkStdString(name);
this->ComponentNames->at(index) = compName;
}
else
{
compName->assign(name);
}
}
//------------------------------------------------------------------------------
const char* vtkAbstractArray::GetComponentName(vtkIdType component) const
{
unsigned int index = static_cast<unsigned int>(component);
if (!this->ComponentNames || component < 0 || index >= this->ComponentNames->size())
{
// make sure we have valid vector
return nullptr;
}
vtkStdString* compName = this->ComponentNames->at(index);
return (compName) ? compName->c_str() : nullptr;
}
//------------------------------------------------------------------------------
bool vtkAbstractArray::HasAComponentName() const
{
return this->ComponentNames && !this->ComponentNames->empty();
}
//------------------------------------------------------------------------------
int vtkAbstractArray::CopyComponentNames(vtkAbstractArray* da)
{
if (da && da != this && da->ComponentNames)
{
// clear the vector of the all data
if (!this->ComponentNames)
{
this->ComponentNames = new vtkAbstractArray::vtkInternalComponentNames();
}
// copy the passed in components
for (unsigned int i = 0; i < this->ComponentNames->size(); ++i)
{
delete this->ComponentNames->at(i);
}
this->ComponentNames->clear();
this->ComponentNames->reserve(da->ComponentNames->size());
const char* name;
for (unsigned int i = 0; i < da->ComponentNames->size(); ++i)
{
name = da->GetComponentName(i);
if (name)
{
this->SetComponentName(i, name);
}
}
return 1;
}
return 0;
}
//------------------------------------------------------------------------------
bool vtkAbstractArray::SetNumberOfValues(vtkIdType numValues)
{
vtkIdType numTuples = numValues / this->NumberOfComponents;
if (numValues % this->NumberOfComponents)
{
++numTuples;
}
if (!this->Resize(numTuples))
{
return false;
}
this->MaxId = numValues - 1;
return true;
}
//------------------------------------------------------------------------------
void vtkAbstractArray::SetInformation(vtkInformation* args)
{
// Same as in vtkCxxSetObjectMacro, but no Modified() so that
// this doesn't cause extra pipeline updates.
vtkDebugMacro(<< this->GetClassName() << " (" << this << "): setting Information to " << args);
if (this->Information != args)
{
vtkInformation* tempSGMacroVar = this->Information;
this->Information = args;
if (this->Information != nullptr)
{
this->Information->Register(this);
}
if (tempSGMacroVar != nullptr)
{
tempSGMacroVar->UnRegister(this);
}
}
}
//------------------------------------------------------------------------------
void vtkAbstractArray::GetTuples(vtkIdList* tupleIds, vtkAbstractArray* aa)
{
if (aa->GetNumberOfComponents() != this->GetNumberOfComponents())
{
vtkWarningMacro("Number of components for input and output do not match.");
return;
}
// Here we give the slowest implementation. Subclasses can override
// to use the knowledge about the data.
vtkIdType num = tupleIds->GetNumberOfIds();
for (vtkIdType i = 0; i < num; i++)
{
aa->SetTuple(i, tupleIds->GetId(i), this);
}
}
//------------------------------------------------------------------------------
void vtkAbstractArray::GetTuples(vtkIdType p1, vtkIdType p2, vtkAbstractArray* aa)
{
if (aa->GetNumberOfComponents() != this->GetNumberOfComponents())
{
vtkWarningMacro("Number of components for input and output do not match.");
return;
}
// Here we give the slowest implementation. Subclasses can override
// to use the knowledge about the data.
vtkIdType num = p2 - p1 + 1;
for (vtkIdType i = 0; i < num; i++)
{
aa->SetTuple(i, (p1 + i), this);
}
}
//------------------------------------------------------------------------------
bool vtkAbstractArray::HasStandardMemoryLayout() const
{
return true;
}
//------------------------------------------------------------------------------
void vtkAbstractArray::DeepCopy(vtkAbstractArray* da)
{
if (!da || da == this)
{
return;
}
if (da->HasInformation())
{
this->CopyInformation(da->GetInformation(), /*deep=*/1);
}
else
{
this->SetInformation(nullptr);
}
this->SetName(da->Name);
this->CopyComponentNames(da);
}
//------------------------------------------------------------------------------
void vtkAbstractArray::ExportToVoidPointer(void* dest)
{
if (this->MaxId > 0 && this->GetDataTypeSize() > 0)
{
void* src = this->GetVoidPointer(0);
memcpy(dest, src, ((this->MaxId + 1) * this->GetDataTypeSize()));
}
}
//------------------------------------------------------------------------------
int vtkAbstractArray::CopyInformation(vtkInformation* infoFrom, int deep)
{
// Copy all keys. NOTE: subclasses rely on this.
vtkInformation* myInfo = this->GetInformation();
myInfo->Copy(infoFrom, deep);
// Remove any keys we own that are not to be copied here.
// For now, remove per-component metadata.
myInfo->Remove(PER_COMPONENT());
myInfo->Remove(PER_FINITE_COMPONENT());
myInfo->Remove(DISCRETE_VALUES());
return 1;
}
//------------------------------------------------------------------------------
// call modified on superclass
void vtkAbstractArray::Modified()
{
if (this->HasInformation())
{
vtkInformation* info = this->GetInformation();
// Clear key-value pairs that are now out of date.
info->Remove(PER_COMPONENT());
info->Remove(PER_FINITE_COMPONENT());
}
this->Superclass::Modified();
}
//------------------------------------------------------------------------------
vtkInformation* vtkAbstractArray::GetInformation()
{
if (!this->Information)
{
vtkInformation* info = vtkInformation::New();
this->SetInformation(info);
info->FastDelete();
}
return this->Information;
}
//------------------------------------------------------------------------------
template <class T>
int vtkAbstractArrayGetDataTypeSize(T*)
{
return sizeof(T);
}
int vtkAbstractArray::GetDataTypeSize(int type)
{
switch (type)
{
vtkTemplateMacro(return vtkAbstractArrayGetDataTypeSize(static_cast<VTK_TT*>(nullptr)));
case VTK_BIT:
case VTK_STRING:
case VTK_UNICODE_STRING:
return 0;
default:
vtkGenericWarningMacro(<< "Unsupported data type!");
}
return 1;
}
//------------------------------------------------------------------------------
vtkAbstractArray* vtkAbstractArray::CreateArray(int dataType)
{
switch (dataType)
{
case VTK_BIT:
return vtkBitArray::New();
case VTK_CHAR:
return vtkCharArray::New();
case VTK_SIGNED_CHAR:
return vtkSignedCharArray::New();
case VTK_UNSIGNED_CHAR:
return vtkUnsignedCharArray::New();
case VTK_SHORT:
return vtkShortArray::New();
case VTK_UNSIGNED_SHORT:
return vtkUnsignedShortArray::New();
case VTK_INT:
return vtkIntArray::New();
case VTK_UNSIGNED_INT:
return vtkUnsignedIntArray::New();
case VTK_LONG:
return vtkLongArray::New();
case VTK_UNSIGNED_LONG:
return vtkUnsignedLongArray::New();
case VTK_LONG_LONG:
return vtkLongLongArray::New();
case VTK_UNSIGNED_LONG_LONG:
return vtkUnsignedLongLongArray::New();
case VTK_FLOAT:
return vtkFloatArray::New();
case VTK_DOUBLE:
return vtkDoubleArray::New();
case VTK_ID_TYPE:
return vtkIdTypeArray::New();
case VTK_STRING:
return vtkStringArray::New();
case VTK_UNICODE_STRING:
return vtkUnicodeStringArray::New();
case VTK_VARIANT:
return vtkVariantArray::New();
default:
break;
}
vtkGenericWarningMacro("Unsupported data type: " << dataType << "! Setting to VTK_DOUBLE");
return vtkDoubleArray::New();
}
//------------------------------------------------------------------------------
template <typename T>
vtkVariant vtkAbstractArrayGetVariantValue(T* arr, vtkIdType index)
{
return vtkVariant(arr[index]);
}
//------------------------------------------------------------------------------
vtkVariant vtkAbstractArray::GetVariantValue(vtkIdType valueIdx)
{
vtkVariant val;
switch (this->GetDataType())
{
vtkExtraExtendedTemplateMacro(val = vtkAbstractArrayGetVariantValue(
static_cast<VTK_TT*>(this->GetVoidPointer(0)), valueIdx));
}
return val;
}
//------------------------------------------------------------------------------
void vtkAbstractArray::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
const char* name = this->GetName();
if (name)
{
os << indent << "Name: " << name << "\n";
}
else
{
os << indent << "Name: (none)\n";
}
os << indent << "Data type: " << this->GetDataTypeAsString() << "\n";
os << indent << "Size: " << this->Size << "\n";
os << indent << "MaxId: " << this->MaxId << "\n";
os << indent << "NumberOfComponents: " << this->NumberOfComponents << endl;
if (this->ComponentNames)
{
os << indent << "ComponentNames: " << endl;
vtkIndent nextIndent = indent.GetNextIndent();
for (unsigned int i = 0; i < this->ComponentNames->size(); ++i)
{
os << nextIndent << i << " : " << this->ComponentNames->at(i) << endl;
}
}
os << indent << "Information: " << this->Information << endl;
if (this->Information)
{
this->Information->PrintSelf(os, indent.GetNextIndent());
}
}
//------------------------------------------------------------------------------
void vtkAbstractArray::GetProminentComponentValues(
int comp, vtkVariantArray* values, double uncertainty, double minimumProminence)
{
if (!values || comp < -1 || comp >= this->NumberOfComponents)
{
return;
}
values->Initialize();
values->SetNumberOfComponents(comp < 0 ? this->NumberOfComponents : 1);
bool justCreated = false;
vtkInformation* info = this->GetInformation();
const double* lastParams = info
? (info->Has(DISCRETE_VALUE_SAMPLE_PARAMETERS()) ? info->Get(DISCRETE_VALUE_SAMPLE_PARAMETERS())
: nullptr)
: nullptr;
if (comp >= 0 && info)
{
vtkInformationVector* infoVec = info->Get(PER_COMPONENT());
if (!infoVec || infoVec->GetNumberOfInformationObjects() < this->NumberOfComponents)
{
infoVec = vtkInformationVector::New();
infoVec->SetNumberOfInformationObjects(this->NumberOfComponents);
info->Set(PER_COMPONENT(), infoVec);
infoVec->FastDelete();
justCreated = true;
}
info = infoVec->GetInformationObject(comp);
}
if (info)
{
// Any insane parameter values map to
// deterministic, exhaustive enumeration of all
// distinct values:
if (uncertainty < 0. || uncertainty > 1.)
{
uncertainty = 0.;
}
if (minimumProminence < 0. || minimumProminence > 1.)
{
minimumProminence = 0.;
}
// Are parameter values requesting more certainty in reporting or
// that less-prominent values be reported? If so, recompute.
bool tighterParams = lastParams
? (lastParams[0] > uncertainty || lastParams[1] > minimumProminence ? true : false)
: true;
// Recompute discrete value set when the array has been
// modified since the information was written.
if (!info->Has(DISCRETE_VALUES()) || tighterParams || this->GetMTime() > info->GetMTime() ||
justCreated)
{
this->UpdateDiscreteValueSet(uncertainty, minimumProminence);
}
}
else
{
return;
}
vtkIdType len;
const vtkVariant* vals = info->Get(DISCRETE_VALUES());
if (vals != nullptr)
{
len = info->Length(DISCRETE_VALUES());
values->SetNumberOfTuples(len / values->GetNumberOfComponents());
for (vtkIdType i = 0; i < len; ++i)
{
values->SetVariantValue(i, vals[i]);
}
}
}
//------------------------------------------------------------------------------
namespace
{
template <typename T, bool U = std::numeric_limits<T>::has_quiet_NaN>
struct CompareWithNaN
{
bool operator()(T a, T b) const
{
if (std::isnan(a))
{
return false;
}
if (std::isnan(b))
{
return true;
}
return a < b;
}
};
template <typename T>
struct CompareWithNaN<T, false>
{
bool operator()(T a, T b) const { return a < b; }
};
template <typename T>
bool AccumulateSampleValues(T* array, int nc, vtkIdType begin, vtkIdType end,
std::vector<std::set<T, CompareWithNaN<T>>>& uniques, std::set<std::vector<T>>& tupleUniques,
unsigned int maxDiscreteValues)
{
// number of discrete components remaining (tracked during iteration):
int ndc = nc;
std::pair<typename std::set<T>::iterator, bool> result;
std::pair<typename std::set<std::vector<T>>::iterator, bool> tresult;
std::vector<T> tuple;
tuple.resize(nc);
// Here we iterate over the components and add to their respective lists
// of previously encountered values -- as long as there are not too many
// values already in the list. We also accumulate each component's value
// into a vtkVariantArray named tuple, which is added to the list of
// unique vectors -- again assuming it is not already too long.
for (vtkIdType i = begin; i < end && ndc; ++i)
{
// First, attempt a per-component insert.
for (int j = 0; j < nc; ++j)
{
if (uniques[j].size() > maxDiscreteValues)
continue;
T& val(array[i * nc + j]);
tuple[j] = val;
result = uniques[j].insert(val);
if (result.second)
{
if (uniques[j].size() == maxDiscreteValues + 1)
{
--ndc;
}
}
}
// Now, as long as no component has exceeded maxDiscreteValues unique
// values, it is worth seeing whether the tuple as a whole is unique:
if (nc > 1 && ndc == nc)
{
tresult = tupleUniques.insert(tuple);
(void)tresult; // nice to have when debugging.
}
}
return ndc == 0;
}
//------------------------------------------------------------------------------
template <typename U>
void SampleProminentValues(std::vector<std::vector<vtkVariant>>& uniques, vtkIdType maxId, int nc,
vtkIdType nt, int blockSize, vtkIdType numberOfBlocks, U* ptr, unsigned int maxDiscreteValues)
{
std::vector<std::set<U, CompareWithNaN<U>>> typeSpecificUniques;
std::set<std::vector<U>> typeSpecificUniqueTuples;
typeSpecificUniques.resize(nc);
// I. Accumulate samples for all components plus the tuple,
// either for the full array or a random subset.
if (numberOfBlocks * blockSize > maxId / 2)
{ // Awwww, just do the whole array already!
AccumulateSampleValues(
ptr, nc, 0, nt, typeSpecificUniques, typeSpecificUniqueTuples, maxDiscreteValues);
}
else
{ // Choose random blocks
vtkNew<vtkMinimalStandardRandomSequence> seq;
// test different blocks each time we're called:
seq->SetSeed(static_cast<int>(seq->GetMTime()) ^ 0xdeadbeef);
vtkIdType totalBlockCount = nt / blockSize + (nt % blockSize ? 1 : 0);
std::set<vtkIdType> startTuples;
// Sort the list of blocks we'll search to maintain cache coherence.
for (int i = 0; i < numberOfBlocks; ++i, seq->Next())
{
vtkIdType startTuple = static_cast<vtkIdType>(seq->GetValue() * totalBlockCount) * blockSize;
startTuples.insert(startTuple);
}
// Now iterate over the blocks, accumulating unique values and tuples.
std::set<vtkIdType>::iterator blkIt;
for (blkIt = startTuples.begin(); blkIt != startTuples.end(); ++blkIt)
{
vtkIdType startTuple = *blkIt;
vtkIdType endTuple = startTuple + blockSize;
endTuple = endTuple < nt ? endTuple : nt;
bool endEarly = AccumulateSampleValues(ptr, nc, startTuple, endTuple, typeSpecificUniques,
typeSpecificUniqueTuples, maxDiscreteValues);
if (endEarly)
break;
}
}
// II. Convert type-specific sets of unique values into non-type-specific
// vectors of vtkVariants for storage in array information.
// Handle per-component uniques first
for (int i = 0; i < nc; ++i)
{
std::back_insert_iterator<std::vector<vtkVariant>> bi(uniques[i]);
std::copy(typeSpecificUniques[i].begin(), typeSpecificUniques[i].end(), bi);
}
// Now squash any tuple-wide uniques into
// the final entry of the outer vector.
typename std::set<std::vector<U>>::iterator si;
for (si = typeSpecificUniqueTuples.begin(); si != typeSpecificUniqueTuples.end(); ++si)
{
std::back_insert_iterator<std::vector<vtkVariant>> bi(uniques[nc]);
std::copy(si->begin(), si->end(), bi);
}
}
} // End anonymous namespace.
//------------------------------------------------------------------------------
void vtkAbstractArray::UpdateDiscreteValueSet(double uncertainty, double minimumProminence)
{
// For an array with T tuples and given uncertainty U and mininumum
// prominence P, we sample N blocks of M tuples each, with
// M*N = f(T; P, U) and f some sublinear function of T.
// If every component plus all components taken together each have more than
// MaxDiscreteValues distinct values, then we exit early.
// M is chosen based on the number of bytes per tuple to maximize use of a
// cache line (assuming a 64-byte cache line until kwsys::SystemInformation
// or the like can provide a platform-independent way to query it).
//
// N is chosen to satisfy the requested uncertainty and prominence criteria
// specified.
#define VTK_CACHE_LINE_SIZE 64
#define VTK_SAMPLE_FACTOR 5
// I. Determine the granularity at which the array should be sampled.
int numberOfComponentsWithProminentValues = 0;
int nc = this->NumberOfComponents;
int blockSize = VTK_CACHE_LINE_SIZE / (this->GetDataTypeSize() * nc);
if (!blockSize)
{
blockSize = 4;
}
double logfac = 1.;
vtkIdType nt = this->GetNumberOfTuples();
vtkIdType numberOfSampleTuples = nt;
if (this->MaxId > 0 && minimumProminence > 0.0)
{
logfac = -log(uncertainty * minimumProminence) / minimumProminence;
if (logfac < 0)
{
logfac = -logfac;
}
if (!vtkMath::IsInf(logfac))
{
numberOfSampleTuples = static_cast<vtkIdType>(VTK_SAMPLE_FACTOR * logfac);
}
}
/*
// Theoretically, we should discard values or tuples that recur fewer
// than minFreq times in our sample, but in practice this involves
// counting and communication that slow us down.
vtkIdType minFreq = static_cast<vtkIdType>(
numberOfSampleTuples * minimumProminence / 2);
*/
vtkIdType numberOfBlocks =
numberOfSampleTuples / blockSize + (numberOfSampleTuples % blockSize ? 1 : 0);
if (static_cast<unsigned int>(numberOfBlocks * blockSize) < 2 * this->MaxDiscreteValues)
{
numberOfBlocks =
2 * this->MaxDiscreteValues / blockSize + (2 * this->MaxDiscreteValues % blockSize ? 1 : 0);
}
// II. Sample the array.
std::vector<std::vector<vtkVariant>> uniques(nc > 1 ? nc + 1 : nc);
switch (this->GetDataType())
{
vtkSuperExtraExtendedTemplateMacro(
SampleProminentValues(uniques, this->MaxId, nc, nt, blockSize, numberOfBlocks,
static_cast<VTK_TT*>(this->GetVoidPointer(0)), this->MaxDiscreteValues));
default:
vtkErrorMacro("Array type " << this->GetClassName() << " not supported.");
break;
}
// III. Store the results in the array's vtkInformation.
int c;
vtkInformationVector* iv;
for (c = 0; c < nc; ++c)
{
if (!uniques[c].empty() && uniques[c].size() <= this->MaxDiscreteValues)
{
++numberOfComponentsWithProminentValues;
iv = this->GetInformation()->Get(PER_COMPONENT());
if (!iv)
{
vtkNew<vtkInformationVector> infoVec;
infoVec->SetNumberOfInformationObjects(this->NumberOfComponents);
this->GetInformation()->Set(PER_COMPONENT(), infoVec);
iv = this->GetInformation()->Get(PER_COMPONENT());
}
iv->GetInformationObject(c)->Set(
DISCRETE_VALUES(), &uniques[c][0], static_cast<int>(uniques[c].size()));
}
else
{
iv = this->GetInformation()->Get(PER_COMPONENT());
if (iv)
{
iv->GetInformationObject(c)->Remove(DISCRETE_VALUES());
}
}
}
if (nc > 1 && uniques[nc].size() <= this->MaxDiscreteValues * nc)
{
++numberOfComponentsWithProminentValues;
this->GetInformation()->Set(
DISCRETE_VALUES(), &uniques[nc][0], static_cast<int>(uniques[nc].size()));
}
else
{ // Remove the key
this->GetInformation()->Remove(DISCRETE_VALUES());
}
// Always store the sample parameters; this lets us know not to
// re-run the sampling algorithm.
double params[2];
params[0] = uncertainty;
params[1] = minimumProminence;
this->GetInformation()->Set(DISCRETE_VALUE_SAMPLE_PARAMETERS(), params, 2);
}