File: vtkCellGrid.cxx

package info (click to toggle)
vtk9 9.5.2%2Bdfsg4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 206,640 kB
  • sloc: cpp: 2,340,827; ansic: 327,116; python: 114,881; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; javascript: 1,261; makefile: 194; objc: 153; tcl: 59
file content (868 lines) | stat: -rw-r--r-- 22,549 bytes parent folder | download
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkCellGrid.h"

#include "vtkCellAttribute.h"
#include "vtkCellGridBoundsQuery.h"
#include "vtkCellGridCopyQuery.h"
#include "vtkCellMetadata.h"
#include "vtkDataSetAttributes.h"
#include "vtkInformation.h"
#include "vtkInformationDataObjectKey.h"
#include "vtkInformationIntegerVectorKey.h"
#include "vtkInformationVector.h"
#include "vtkObjectFactory.h"
#include "vtkUnsignedCharArray.h"

VTK_ABI_NAMESPACE_BEGIN

vtkStandardNewMacro(vtkCellGrid);
vtkInformationKeyMacro(vtkCellGrid, ARRAY_GROUP_IDS, IntegerVector);

vtkCellGrid::vtkCellGrid() = default;
vtkCellGrid::~vtkCellGrid() = default;

void vtkCellGrid::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);

  vtkIndent i2 = indent.GetNextIndent();
  vtkIndent i3 = i2.GetNextIndent();

  os << indent << "Cells: (" << this->Cells.size() << " types)\n";
  for (auto& cellRec : this->Cells)
  {
    os << i2 << cellRec.second->GetClassName() << " (" << cellRec.first.Data() << ")\n";
    cellRec.second->PrintSelf(os, i3);
  }

  os << indent << "ArrayGroups: (" << this->ArrayGroups.size() << ")\n";
  for (auto it = this->ArrayGroups.begin(); it != this->ArrayGroups.end(); ++it)
  {
    vtkStringToken attToken(static_cast<vtkStringToken::Hash>(it->first));
    auto attName = attToken.HasData() ? attToken.Data() : "";
    if (attName.empty())
    {
      os << i2 << it->first << ": " << it->second << " " << it->second->GetNumberOfArrays()
         << " arrays\n";
    }
    else
    {
      os << i2 << attName << ": " << it->second << " " << it->second->GetNumberOfArrays()
         << " arrays\n";
    }
    it->second->PrintSelf(os, i3);
  }

  os << indent << "Attributes (" << this->Attributes.size() << ")\n";
  for (const auto& attrEntry : this->Attributes)
  {
    os << i2 << attrEntry.second->GetName().Data() << " (" << std::hex << attrEntry.first
       << std::dec << "):\n";
    attrEntry.second->PrintSelf(os, i3);
  }
  os << indent << "HaveShape: " << (this->HaveShape ? "Y" : "N") << "\n";
  if (this->HaveShape)
  {
    os << indent << "ShapeAttribute: " << std::hex << this->ShapeAttribute.GetId() << std::dec
       << " (" << this->ShapeAttribute.Data() << ")\n";
  }
  os << indent << "NextAttribute: " << this->NextAttribute << "\n";
}

void vtkCellGrid::Initialize()
{
  this->Superclass::Initialize();
  this->ArrayGroups.clear();
  this->Attributes.clear();
  this->HaveShape = false;
  this->Cells.clear();
}

unsigned long vtkCellGrid::GetActualMemorySize()
{
  auto result = this->Superclass::GetActualMemorySize();
  for (const auto& dsa : this->ArrayGroups)
  {
    result += dsa.second->GetActualMemorySize();
  }
  result += sizeof(*this) / 1024;
  return result;
}

void vtkCellGrid::ShallowCopy(vtkDataObject* baseSrc)
{
  auto* src = vtkCellGrid::SafeDownCast(baseSrc);
  if (!src)
  {
    vtkErrorMacro("Cannot shallow-copy a null object or object of a different type.");
    return;
  }

  this->Initialize();
  vtkNew<vtkCellGridCopyQuery> copier;
  copier->SetSource(src);
  copier->SetTarget(this);
  copier->CopyOnlyShapeOff();
  copier->AddAllSourceCellAttributeIds();
  copier->CopyCellsOn();
  copier->CopyArraysOn();
  copier->CopyArrayValuesOn();
  copier->DeepCopyArraysOff();
  if (!src->Query(copier))
  {
    vtkErrorMacro("Failed to copy the source " << src);
  }
}

void vtkCellGrid::DeepCopy(vtkDataObject* baseSrc)
{
  auto* src = vtkCellGrid::SafeDownCast(baseSrc);
  if (!src)
  {
    vtkErrorMacro("Cannot shallow-copy a null object or object of a different type.");
    return;
  }

  this->Initialize();
  vtkNew<vtkCellGridCopyQuery> copier;
  copier->SetSource(src);
  copier->SetTarget(this);
  copier->CopyOnlyShapeOff();
  copier->AddAllSourceCellAttributeIds();
  copier->CopyCellsOn();
  copier->CopyArraysOn();
  copier->CopyArrayValuesOn();
  copier->DeepCopyArraysOn();
  if (!src->Query(copier))
  {
    vtkErrorMacro("Failed to copy the source " << src);
  }
}

bool vtkCellGrid::CopyStructure(vtkCellGrid* other, bool byReference)
{
  this->Initialize();
  vtkNew<vtkCellGridCopyQuery> copier;
  copier->SetSource(other);
  copier->SetTarget(this);
  copier->CopyOnlyShapeOn();
  copier->SetDeepCopyArrays(!byReference);
  if (other->Query(copier))
  {
    return true;
  }
  return false;
}

vtkDataSetAttributes* vtkCellGrid::GetAttributes(int type)
{
  auto it = this->ArrayGroups.find(type);
  if (it == this->ArrayGroups.end())
  {
    it =
      this->ArrayGroups.insert(std::make_pair(type, vtkSmartPointer<vtkDataSetAttributes>::New()))
        .first;
  }
  return it->second;
}

vtkDataSetAttributes* vtkCellGrid::GetAttributes(vtkStringToken type)
{
  int key = static_cast<int>(type.GetId());
  return this->GetAttributes(key);
}

vtkDataSetAttributes* vtkCellGrid::FindAttributes(int type) const
{
  auto it = this->ArrayGroups.find(type);
  return it == this->ArrayGroups.end() ? nullptr : it->second;
}

vtkDataSetAttributes* vtkCellGrid::FindAttributes(vtkStringToken type) const
{
  return this->FindAttributes(type.GetId());
}

void vtkCellGrid::MapArrayLocations(
  std::unordered_map<vtkAbstractArray*, vtkStringToken>& arrayLocations) const
{
  for (const auto& entry : this->ArrayGroups)
  {
    auto groupToken = entry.first;
    for (vtkIdType ii = 0; ii < entry.second->GetNumberOfArrays(); ++ii)
    {
      auto* arr = entry.second->GetAbstractArray(ii);
      if (arr)
      {
        arrayLocations[arr] = groupToken;
      }
    }
  }
}

vtkUnsignedCharArray* vtkCellGrid::GetGhostArray(int type)
{
  vtkUnsignedCharArray* result = nullptr;
  auto* dsa = this->FindAttributes(type);
  if (!dsa)
  {
    return result;
  }
  result =
    vtkArrayDownCast<vtkUnsignedCharArray>(dsa->GetArray(vtkDataSetAttributes::GhostArrayName()));
  return result;
}

//------------------------------------------------------------------------------
bool vtkCellGrid::SupportsGhostArray(int type)
{
  if (type == CELL)
  {
    return true;
  }
  return false;
}

int vtkCellGrid::GetAttributeTypeForArray(vtkAbstractArray* arr)
{
  // First, see if the array is marked with a group for fast lookup.
  if (arr->HasInformation())
  {
    auto* info = arr->GetInformation();
    if (info->Has(ARRAY_GROUP_IDS()))
    {
      int numGroups = info->Length(vtkCellGrid::ARRAY_GROUP_IDS());
      int* groupIds = info->Get(vtkCellGrid::ARRAY_GROUP_IDS());
      for (int gg = 0; gg < numGroups; ++gg)
      {
        if (auto* group = this->FindAttributes(groupIds[gg]))
        {
          if (auto* array = group->GetAbstractArray(arr->GetName()))
          {
            if (array != arr)
            {
              // NB: We might update info by rewriting ARRAY_GROUP_IDS to
              //     exclude groupIds[gg], but it is possible – because
              //     arrays are shallow-copied – that they may end up in
              //     multiple groups across multiple instances of
              //     vtkCellGrid and we do not necessarily want to de-index
              //     arr across all cell-grids.
              continue;
            }
            return groupIds[gg];
          }
        }
      }
    }
  }

  // Next, search through DOF arrays:
  for (auto it = this->ArrayGroups.begin(); it != this->ArrayGroups.end(); ++it)
  {
    for (int ii = 0; ii < it->second->GetNumberOfArrays(); ++ii)
    {
      if (it->second->GetAbstractArray(ii) == arr)
      {
        // Accelerate next lookup by adding the result.
        arr->GetInformation()->Append(ARRAY_GROUP_IDS(), it->first);
        return it->first;
      }
    }
  }

  // If not a DOF array, perhaps it is field data:
  for (int ii = 0; ii < this->FieldData->GetNumberOfArrays(); ++ii)
  {
    if (this->FieldData->GetAbstractArray(ii) == arr)
    {
      return FIELD;
    }
  }
  // No such array is owned by this vtkCellGrid:
  return -1;
}

vtkIdType vtkCellGrid::GetNumberOfElements(int type)
{
  auto* dsa = this->FindAttributes(type);
  if (type == CELL)
  {
    return this->GetNumberOfCells();
  }
  else if (!dsa && type == FIELD)
  {
    return this->FieldData->GetNumberOfTuples();
  }
  else if (dsa)
  {
    return dsa->GetNumberOfTuples();
  }
  return 0;
}

vtkIdType vtkCellGrid::GetNumberOfCells()
{
  vtkIdType result = 0;
  for (const auto& cellType : this->Cells)
  {
    result += cellType.second->GetNumberOfCells();
  }
  return result;
}

void vtkCellGrid::GetBounds(double bounds[6])
{
  if (this->CachedBoundsTime < this->GetMTime())
  {
    this->ComputeBoundsInternal();
  }
  for (int ii = 0; ii < 6; ++ii)
  {
    bounds[ii] = this->CachedBounds[ii];
  }
}

vtkCellMetadata* vtkCellGrid::AddCellMetadata(vtkCellMetadata* cellType)
{
  if (!cellType)
  {
    return cellType;
  }
  auto it = this->Cells.find(cellType->Hash());
  if (it != this->Cells.end())
  {
    // Do not take ownership of cellType; return the instance we own.
    return it->second;
  }
  // OK, we don't already have this type... insert it.
  vtkSmartPointer<vtkCellMetadata> owner = cellType;
  this->Cells[owner->Hash()] = owner;
  owner->SetCellGrid(this);
  // Because we have added cells, clear any cell-attribute ranges cached.
  this->RangeCache.clear();
  return cellType;
}

vtkCellMetadata* vtkCellGrid::AddCellMetadata(vtkStringToken cellTypeName)
{
  // See if we have this type already
  auto* metadata = this->GetCellType(cellTypeName);
  if (metadata)
  {
    return metadata;
  }

  // Create a new instance and add it.
  if (!cellTypeName.IsValid())
  {
    return metadata;
  }
  metadata = vtkCellMetadata::NewInstance(cellTypeName, this);
  return metadata;
}

int vtkCellGrid::AddAllCellMetadata()
{
  int numAdded = 0;
  vtkIdType numNewCells = 0;
  auto metadataTypeNames = vtkCellMetadata::CellTypes();
  for (const auto& metadataTypeName : metadataTypeNames)
  {
    auto metadata = vtkCellMetadata::NewInstance(metadataTypeName, this);
    if (metadata)
    {
      ++numAdded;
      numNewCells += metadata->GetNumberOfCells();
    }
  }
  if (numNewCells > 0)
  {
    // Because we have added cells, clear any cell-attribute ranges cached.
    this->RangeCache.clear();
  }
  return numAdded;
}

bool vtkCellGrid::RemoveCellMetadata(vtkCellMetadata* cellType)
{
  if (!cellType)
  {
    return false;
  }
  auto it = this->Cells.find(cellType->Hash());
  if (it == this->Cells.end())
  {
    return false;
  }
  this->Cells.erase(it);
  // Because we have removed cells, clear any cell-attribute ranges cached.
  this->RangeCache.clear();
  return true;
}

int vtkCellGrid::RemoveUnusedCellMetadata()
{
  int numRemoved = 0;
  std::set<vtkCellMetadata*> unused;
  for (const auto& cellEntry : this->Cells)
  {
    if (cellEntry.second->GetNumberOfCells() == 0)
    {
      unused.insert(cellEntry.second);
    }
  }
  for (const auto& cellType : unused)
  {
    if (this->RemoveCellMetadata(cellType))
    {
      ++numRemoved;
    }
  }
  return numRemoved;
}

std::vector<vtkStringToken> vtkCellGrid::CellTypeArray() const
{
#if defined(_MSC_VER) && _MSC_VER >= 1930 && _MSC_VER < 1940 /*17.4+*/
  // MSVC 2022 shows LNK1161 when an exported method uses thread_local in its implementation.
  // See https://github.com/pytorch/pytorch/issues/87957 for more. We omit the
  // thread_local here, which makes this method non-threadsafe on Windows, which
  // should be OK in most cases.
  static std::vector<vtkStringToken> cellTypes;
#else
  static thread_local std::vector<vtkStringToken> cellTypes;
#endif
  cellTypes.clear();
  this->CellTypes(cellTypes);
  return cellTypes;
}

std::vector<std::string> vtkCellGrid::GetCellTypes() const
{
  auto cta = this->CellTypeArray();
  std::vector<std::string> result;
  result.reserve(cta.size());
  for (const auto& cellTypeToken : cta)
  {
    result.push_back(cellTypeToken.Data());
  }
  return result;
}

const vtkCellMetadata* vtkCellGrid::GetCellType(vtkStringToken cellTypeName) const
{
  auto it = this->Cells.find(cellTypeName);
  if (it == this->Cells.end())
  {
    return nullptr;
  }
  return it->second;
}

vtkCellMetadata* vtkCellGrid::GetCellType(vtkStringToken cellTypeName)
{
  auto it = this->Cells.find(cellTypeName);
  if (it == this->Cells.end())
  {
    return nullptr;
  }
  return it->second;
}

bool vtkCellGrid::AddCellAttribute(vtkCellAttribute* attribute)
{
  if (!attribute)
  {
    return false;
  }
  auto it = this->Attributes.find(attribute->GetHash());
  if (it != this->Attributes.end())
  {
    // Either we have a hash collision or the attribute already exists.
    if (it->second != attribute)
    {
      vtkWarningMacro("Attempting to add attribute "
        << attribute << " (" << attribute->GetName().Data() << "), but " << it->second << " ("
        << it->second->GetName().Data() << ") already exists with the same hash "
        << it->second->GetHash() << ". Ignoring.");
    }
    return false;
  }
  this->Attributes[attribute->GetHash()] = attribute;
  attribute->SetId(this->NextAttribute++);
  return true;
}

bool vtkCellGrid::RemoveCellAttribute(vtkCellAttribute* attribute)
{
  if (!attribute)
  {
    return false;
  }
  // Do not allow the shape attribute to be removed:
  if (this->ShapeAttribute.GetId() == attribute->GetHash())
  {
    return false;
  }
  auto it = this->Attributes.find(attribute->GetHash());
  if (it == this->Attributes.end())
  {
    return false;
  }
  // Remove any cache for this cell-attribute's ranges.
  this->RangeCache.erase(attribute);
  // Now unhook the cell-attribute:
  this->Attributes.erase(it);
  return true;
}

bool vtkCellGrid::GetCellAttributeRange(
  vtkCellAttribute* attribute, int componentIndex, double range[2], bool finiteRange) const
{
  // Invalidate the range so early returns indicate we could not compute one.
  range[0] = 1.;
  range[1] = 0.;

  if (!attribute || componentIndex < -2 || componentIndex >= attribute->GetNumberOfComponents())
  {
    return false;
  }

  // If attribute does not belong to this vtkCellGrid, we cannot proceed.
  auto attIt = this->Attributes.find(attribute->GetHash());
  if (attIt == this->Attributes.end())
  {
    return false;
  }

  // If the cache does not exist, is not up to date, or is not the right size
  // (i.e., because someone forgot to call Modified() on the attribute), then
  // recompute the range.
  auto cacheIt = this->RangeCache.find(attribute);
  if (cacheIt == this->RangeCache.end() ||
    cacheIt->second.size() <= static_cast<std::size_t>(componentIndex + 2) ||
    (finiteRange && cacheIt->second[componentIndex + 2].FiniteRangeTime < attribute->GetMTime()) ||
    (!finiteRange && cacheIt->second[componentIndex + 2].EntireRangeTime < attribute->GetMTime()))
  {
    if (!this->ComputeRangeInternal(attribute, componentIndex, finiteRange))
    {
      return false;
    }
    cacheIt = this->RangeCache.find(attribute);
    if (cacheIt == this->RangeCache.end())
    {
      return false;
    }
  }

  // Copy the cache into our result.
  if (finiteRange)
  {
    for (int ii = 0; ii < 2; ++ii)
    {
      range[ii] = cacheIt->second[componentIndex + 2].FiniteRange[ii];
    }
  }
  else
  {
    for (int ii = 0; ii < 2; ++ii)
    {
      range[ii] = cacheIt->second[componentIndex + 2].EntireRange[ii];
    }
  }
  return true;
}

void vtkCellGrid::ClearRangeCache(const std::string& attributeName)
{
  if (attributeName.empty())
  {
    this->RangeCache.clear();
    return;
  }
  auto* att = this->GetCellAttributeByName(attributeName);
  if (!att)
  {
    return;
  }
  auto it = this->RangeCache.find(att);
  if (it == this->RangeCache.end())
  {
    return;
  }
  this->RangeCache.erase(it);
}

std::set<int> vtkCellGrid::GetCellAttributeIds() const
{
  std::set<int> attributeIds;
  for (const auto& entry : this->Attributes)
  {
    attributeIds.insert(entry.second->GetId());
  }
  return attributeIds;
}

std::vector<int> vtkCellGrid::GetUnorderedCellAttributeIds() const
{
  std::set<int> attributeIds;
  for (const auto& entry : this->Attributes)
  {
    attributeIds.insert(entry.second->GetId());
  }
  std::vector<int> result(attributeIds.begin(), attributeIds.end());
  return result;
}

std::vector<vtkSmartPointer<vtkCellAttribute>> vtkCellGrid::GetCellAttributeList() const
{
  std::vector<vtkSmartPointer<vtkCellAttribute>> result;
  result.reserve(this->Attributes.size());
  for (const auto& entry : this->Attributes)
  {
    result.emplace_back(entry.second);
  }
  return result;
}

vtkCellAttribute* vtkCellGrid::GetCellAttribute(vtkStringToken::Hash hash)
{
  auto it = this->Attributes.find(hash);
  if (it == this->Attributes.end())
  {
    return nullptr;
  }
  return it->second;
}

vtkCellAttribute* vtkCellGrid::GetCellAttributeById(int attributeId)
{
  for (const auto& entry : this->Attributes)
  {
    if (entry.second->GetId() == attributeId)
    {
      return entry.second;
    }
  }
  return nullptr;
}

vtkCellAttribute* vtkCellGrid::GetCellAttributeByName(const std::string& name)
{
  for (const auto& entry : this->Attributes)
  {
    if (entry.second->GetName() == name)
    {
      return entry.second;
    }
  }
  return nullptr;
}

vtkCellAttribute* vtkCellGrid::GetShapeAttribute()
{
  if (!this->HaveShape)
  {
    return nullptr;
  }
  auto it = this->Attributes.find(this->ShapeAttribute.GetId());
  if (it == this->Attributes.end())
  {
    return nullptr;
  }
  return it->second.GetPointer();
}

bool vtkCellGrid::SetShapeAttribute(vtkCellAttribute* shape)
{
  if (!shape)
  {
    if (this->HaveShape)
    {
      this->HaveShape = false;
      this->Modified();
      return true;
    }
    return false;
  }
  if (shape->GetHash() == this->ShapeAttribute.GetId() && this->HaveShape)
  {
    return false; // No change.
  }
  // If we don't already own this attribute, add it to this->Attributes:
  auto it = this->Attributes.find(shape->GetHash());
  if (it == this->Attributes.end())
  {
    this->Attributes[shape->GetHash()] = shape;
  }
  else if (it->second != shape)
  {
    vtkErrorMacro("Hash collision for shape attribute. Ignoring call to SetShapeAttribute().");
    return false;
  }
  this->HaveShape = true;
  this->ShapeAttribute = shape->GetHash();
  this->Modified();
  return true;
}

bool vtkCellGrid::Query(vtkCellGridQuery* query)
{
  if (!query)
  {
    return false;
  }

  bool ok = true;
  if (!query->Initialize())
  {
    ok = false;
    return ok;
  }
  do
  {
    query->StartPass();
    for (const auto& cellType : this->Cells)
    {
      ok &= cellType.second->Query(query);
    }
  } while (query->IsAnotherPassRequired());
  bool didFinalize = query->Finalize();
  ok &= didFinalize;
  return ok;
}

void vtkCellGrid::SetSchema(vtkStringToken name, vtkTypeUInt32 version)
{
  if (name == this->SchemaName && version == this->SchemaVersion)
  {
    return;
  }
  this->Modified();
  this->SchemaName = name;
  this->SchemaVersion = version;
}

vtkCellGrid* vtkCellGrid::GetData(vtkInformation* info)
{
  return info ? vtkCellGrid::SafeDownCast(info->Get(DATA_OBJECT())) : nullptr;
}

vtkCellGrid* vtkCellGrid::GetData(vtkInformationVector* v, int i)
{
  return vtkCellGrid::GetData(v->GetInformationObject(i));
}

vtkDataArray* vtkCellGrid::CorrespondingArray(
  vtkCellGrid* gridA, vtkDataArray* arrayA, vtkCellGrid* gridB)
{
  vtkDataArray* arrayB = nullptr;
  if (!gridA || !gridB || !arrayA || !arrayA->GetName() || !arrayA->GetName()[0])
  {
    return arrayB;
  }

  const char* arrayName = arrayA->GetName();

  // If we have ARRAY_GROUP_IDS, look there first.
  auto* infoA = arrayA->HasInformation() ? arrayA->GetInformation() : nullptr;
  if (infoA && infoA->Has(ARRAY_GROUP_IDS()))
  {
    int numGroups = infoA->Length(vtkCellGrid::ARRAY_GROUP_IDS());
    int* groupIds = infoA->Get(vtkCellGrid::ARRAY_GROUP_IDS());
    for (int gg = 0; gg < numGroups; ++gg)
    {
      if (auto* groupA = gridA->FindAttributes(groupIds[gg]))
      {
        if (auto* array = groupA->GetArray(arrayName))
        {
          if (array != arrayA)
          {
            continue;
          }
          if (auto* groupB = gridB->FindAttributes(groupIds[gg]))
          {
            arrayB = groupB->GetArray(arrayName);
            if (arrayB)
            {
              return arrayB;
            }
          }
        }
      }
    }
  }

  // We don't currently index arrays by their parent group.
  // Just iterate groups until we find a match.
  for (const auto& groupEntry : gridA->ArrayGroups)
  {
    auto* array = groupEntry.second->GetArray(arrayName);
    if (array != arrayA)
    {
      continue;
    }
    // The input array was not marked with a group but was present in a group; add it:
    arrayA->GetInformation()->Append(ARRAY_GROUP_IDS(), groupEntry.first);
    auto* groupB = gridB->FindAttributes(groupEntry.first);
    if (!groupB)
    {
      return arrayB;
    }
    arrayB = groupB->GetArray(arrayName);
    if (arrayB)
    {
      if (!arrayB->HasInformation() || !arrayB->GetInformation()->Has(ARRAY_GROUP_IDS()))
      {
        // Mark arrayB for fast lookup.
        arrayB->GetInformation()->Append(ARRAY_GROUP_IDS(), groupEntry.first);
      }
      return arrayB;
    }
    // Continue, hoping arrayA is in multiple array groups…
  }
  return arrayB;
}

bool vtkCellGrid::ComputeBoundsInternal()
{
  if (!this->HaveShape || !this->GetShapeAttribute())
  {
    vtkMath::UninitializeBounds(this->CachedBounds.data());
    this->CachedBoundsTime.Modified();
  }
  vtkNew<vtkCellGridBoundsQuery> bq;
  if (this->Query(bq))
  {
    bq->GetBounds(this->CachedBounds.data());
    this->CachedBoundsTime.Modified();
    return true;
  }
  return false;
}

bool vtkCellGrid::ComputeRangeInternal(
  vtkCellAttribute* attribute, int component, bool finiteRange) const
{
  auto* self = const_cast<vtkCellGrid*>(this);
  auto& cache = this->RangeCache[attribute]; // This inserts a blank entry if none exists.
  // Ensure the range vectors are the proper size:
  if (cache.size() != static_cast<std::size_t>(attribute->GetNumberOfComponents() + 2))
  {
    cache.resize(attribute->GetNumberOfComponents() + 2);
  }
  vtkNew<vtkCellGridRangeQuery> rangeQuery;
  rangeQuery->SetComponent(component);
  rangeQuery->SetFiniteRange(finiteRange);
  rangeQuery->SetCellGrid(self);
  rangeQuery->SetCellAttribute(attribute);
  if (!self->Query(rangeQuery))
  {
    vtkWarningMacro("Range computation for \"" << attribute->GetName().Data() << "\" "
                                               << "(" << component << ") was partial at best.");
  }
  return true;
}

VTK_ABI_NAMESPACE_END