VTK  9.3.20240419
vtkCellArray.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
238 #ifndef vtkCellArray_h
239 #define vtkCellArray_h
240 
241 #include "vtkAbstractCellArray.h"
242 #include "vtkCommonDataModelModule.h" // For export macro
243 #include "vtkWrappingHints.h" // For VTK_MARSHALMANUAL
244 
245 #include "vtkAOSDataArrayTemplate.h" // Needed for inline methods
246 #include "vtkCell.h" // Needed for inline methods
247 #include "vtkDataArrayRange.h" // Needed for inline methods
248 #include "vtkFeatures.h" // for VTK_USE_MEMKIND
249 #include "vtkSmartPointer.h" // For vtkSmartPointer
250 #include "vtkTypeInt32Array.h" // Needed for inline methods
251 #include "vtkTypeInt64Array.h" // Needed for inline methods
252 #include "vtkTypeList.h" // Needed for ArrayList definition
253 
254 #include <cassert> // for assert
255 #include <initializer_list> // for API
256 #include <type_traits> // for std::is_same
257 #include <utility> // for std::forward
258 
279 #define VTK_CELL_ARRAY_V2
280 
281 VTK_ABI_NAMESPACE_BEGIN
283 class vtkIdTypeArray;
284 
285 class VTKCOMMONDATAMODEL_EXPORT VTK_MARSHALMANUAL vtkCellArray : public vtkAbstractCellArray
286 {
287 public:
288  using ArrayType32 = vtkTypeInt32Array;
289  using ArrayType64 = vtkTypeInt64Array;
290 
292 
296  static vtkCellArray* New();
298  void PrintSelf(ostream& os, vtkIndent indent) override;
299  void PrintDebug(ostream& os);
301 
310  using StorageArrayList = vtkTypeList::Create<ArrayType32, ArrayType64>;
311 
323 
332  vtkTypeBool Allocate(vtkIdType sz, vtkIdType vtkNotUsed(ext) = 1000)
333  {
334  return this->AllocateExact(sz, sz) ? 1 : 0;
335  }
336 
346  bool AllocateEstimate(vtkIdType numCells, vtkIdType maxCellSize)
347  {
348  return this->AllocateExact(numCells, numCells * maxCellSize);
349  }
350 
360  bool AllocateExact(vtkIdType numCells, vtkIdType connectivitySize);
361 
372  {
373  return this->AllocateExact(other->GetNumberOfCells(), other->GetNumberOfConnectivityIds());
374  }
375 
385  bool ResizeExact(vtkIdType numCells, vtkIdType connectivitySize);
386 
390  void Initialize() override;
391 
395  void Reset();
396 
402  void Squeeze();
403 
414  bool IsValid();
415 
419  vtkIdType GetNumberOfCells() const override
420  {
421  if (this->Storage.Is64Bit())
422  {
423  return this->Storage.GetArrays64().Offsets->GetNumberOfValues() - 1;
424  }
425  else
426  {
427  return this->Storage.GetArrays32().Offsets->GetNumberOfValues() - 1;
428  }
429  }
430 
435  vtkIdType GetNumberOfOffsets() const override
436  {
437  if (this->Storage.Is64Bit())
438  {
439  return this->Storage.GetArrays64().Offsets->GetNumberOfValues();
440  }
441  else
442  {
443  return this->Storage.GetArrays32().Offsets->GetNumberOfValues();
444  }
445  }
446 
450  vtkIdType GetOffset(vtkIdType cellId) override
451  {
452  if (this->Storage.Is64Bit())
453  {
454  return this->Storage.GetArrays64().Offsets->GetValue(cellId);
455  }
456  else
457  {
458  return this->Storage.GetArrays32().Offsets->GetValue(cellId);
459  }
460  }
461 
469  {
470  if (this->Storage.Is64Bit())
471  {
472  return this->Storage.GetArrays64().Connectivity->GetNumberOfValues();
473  }
474  else
475  {
476  return this->Storage.GetArrays32().Connectivity->GetNumberOfValues();
477  }
478  }
479 
486 
497  void SetData(vtkIdTypeArray* offsets, vtkIdTypeArray* connectivity);
500  void SetData(
502  void SetData(vtkTypeInt32Array* offsets, vtkTypeInt32Array* connectivity);
503  void SetData(vtkTypeInt64Array* offsets, vtkTypeInt64Array* connectivity);
518  bool SetData(vtkDataArray* offsets, vtkDataArray* connectivity);
519 
533  bool SetData(vtkIdType cellSize, vtkDataArray* connectivity);
534 
539  bool IsStorage64Bit() const { return this->Storage.Is64Bit(); }
540 
547  bool IsStorageShareable() const override
548  {
549  if (this->Storage.Is64Bit())
550  {
552  }
553  else
554  {
556  }
557  }
558 
613  {
614  if (this->Storage.Is64Bit())
615  {
616  return this->GetOffsetsArray64();
617  }
618  else
619  {
620  return this->GetOffsetsArray32();
621  }
622  }
634  {
635  if (this->Storage.Is64Bit())
636  {
637  return this->GetConnectivityArray64();
638  }
639  else
640  {
641  return this->GetConnectivityArray32();
642  }
643  }
657 
667  void InitTraversal();
668 
683  int GetNextCell(vtkIdType& npts, vtkIdType const*& pts) VTK_SIZEHINT(pts, npts);
684 
695  int GetNextCell(vtkIdList* pts);
696 
707  void GetCellAtId(vtkIdType cellId, vtkIdType& cellSize, vtkIdType const*& cellPoints,
708  vtkIdList* ptIds) VTK_SIZEHINT(cellPoints, cellSize)
709  VTK_EXPECTS(0 <= cellId && cellId < GetNumberOfCells()) override;
710 
716  void GetCellAtId(vtkIdType cellId, vtkIdList* pts)
717  VTK_EXPECTS(0 <= cellId && cellId < GetNumberOfCells()) override;
718 
726  void GetCellAtId(vtkIdType cellId, vtkIdType& cellSize, vtkIdType* cellPoints) VTK_SIZEHINT(
727  cellPoints, cellSize) VTK_EXPECTS(0 <= cellId && cellId < GetNumberOfCells()) override;
728 
732  vtkIdType GetCellSize(vtkIdType cellId) const override;
733 
737  vtkIdType InsertNextCell(vtkCell* cell);
738 
743  vtkIdType InsertNextCell(vtkIdType npts, const vtkIdType* pts) VTK_SIZEHINT(pts, npts);
744 
749  vtkIdType InsertNextCell(vtkIdList* pts);
750 
758  vtkIdType InsertNextCell(const std::initializer_list<vtkIdType>& cell)
759  {
760  return this->InsertNextCell(static_cast<vtkIdType>(cell.size()), cell.begin());
761  }
762 
769  vtkIdType InsertNextCell(int npts);
770 
775  void InsertCellPoint(vtkIdType id);
776 
781  void UpdateCellCount(int npts);
782 
797  void ReverseCellAtId(vtkIdType cellId) VTK_EXPECTS(0 <= cellId && cellId < GetNumberOfCells());
798 
807  void ReplaceCellAtId(vtkIdType cellId, vtkIdList* list);
808  void ReplaceCellAtId(vtkIdType cellId, vtkIdType cellSize, const vtkIdType* cellPoints)
809  VTK_EXPECTS(0 <= cellId && cellId < GetNumberOfCells()) VTK_SIZEHINT(cellPoints, cellSize);
819  void ReplaceCellPointAtId(vtkIdType cellId, vtkIdType cellPointIndex, vtkIdType newPointId);
820 
828  void ReplaceCellAtId(vtkIdType cellId, const std::initializer_list<vtkIdType>& cell)
829  {
830  return this->ReplaceCellAtId(cellId, static_cast<vtkIdType>(cell.size()), cell.begin());
831  }
832 
837  int GetMaxCellSize() override;
838 
842  void DeepCopy(vtkAbstractCellArray* ca) override;
843 
847  void ShallowCopy(vtkAbstractCellArray* ca) override;
848 
852  void Append(vtkCellArray* src, vtkIdType pointOffset = 0);
853 
865 
893  void AppendLegacyFormat(const vtkIdType* data, vtkIdType len, vtkIdType ptOffset = 0)
894  VTK_SIZEHINT(data, len);
905  unsigned long GetActualMemorySize() const;
906 
907  // The following code is used to support
908 
909  // The wrappers get understandably confused by some of the template code below
910 #ifndef __VTK_WRAP__
911 
912  // Holds connectivity and offset arrays of the given ArrayType.
913  template <typename ArrayT>
914  struct VisitState
915  {
916  using ArrayType = ArrayT;
917  using ValueType = typename ArrayType::ValueType;
918  using CellRangeType = decltype(vtk::DataArrayValueRange<1>(std::declval<ArrayType>()));
919 
920  // We can't just use is_same here, since binary compatible representations
921  // (e.g. int and long) are distinct types. Instead, ensure that ValueType
922  // is a signed integer the same size as vtkIdType.
923  // If this value is true, ValueType pointers may be safely converted to
924  // vtkIdType pointers via reinterpret cast.
925  static constexpr bool ValueTypeIsSameAsIdType = std::is_integral<ValueType>::value &&
926  std::is_signed<ValueType>::value && (sizeof(ValueType) == sizeof(vtkIdType));
927 
928  ArrayType* GetOffsets() { return this->Offsets; }
929  const ArrayType* GetOffsets() const { return this->Offsets; }
930 
931  ArrayType* GetConnectivity() { return this->Connectivity; }
932  const ArrayType* GetConnectivity() const { return this->Connectivity; }
933 
935 
937 
939 
941 
943 
944  friend class vtkCellArray;
945 
946  protected:
948  {
949  this->Connectivity = vtkSmartPointer<ArrayType>::New();
950  this->Offsets = vtkSmartPointer<ArrayType>::New();
951  this->Offsets->InsertNextValue(0);
953  {
954  this->IsInMemkind = true;
955  }
956  }
957  ~VisitState() = default;
958  void* operator new(size_t nSize)
959  {
960  void* r;
961 #ifdef VTK_USE_MEMKIND
963 #else
964  r = malloc(nSize);
965 #endif
966  return r;
967  }
968  void operator delete(void* p)
969  {
970 #ifdef VTK_USE_MEMKIND
971  VisitState* a = static_cast<VisitState*>(p);
972  if (a->IsInMemkind)
973  {
975  }
976  else
977  {
978  free(p);
979  }
980 #else
981  free(p);
982 #endif
983  }
984 
987 
988  private:
989  VisitState(const VisitState&) = delete;
990  VisitState& operator=(const VisitState&) = delete;
991  bool IsInMemkind = false;
992  };
993 
994 private: // Helpers that allow Visit to return a value:
995  template <typename Functor, typename... Args>
996  using GetReturnType = decltype(
997  std::declval<Functor>()(std::declval<VisitState<ArrayType32>&>(), std::declval<Args>()...));
998 
999  template <typename Functor, typename... Args>
1000  struct ReturnsVoid : std::is_same<GetReturnType<Functor, Args...>, void>
1001  {
1002  };
1003 
1004 public:
1074  template <typename Functor, typename... Args,
1075  typename = typename std::enable_if<ReturnsVoid<Functor, Args...>::value>::type>
1076  void Visit(Functor&& functor, Args&&... args)
1077  {
1078  if (this->Storage.Is64Bit())
1079  {
1080  // If you get an error on the next line, a call to Visit(functor, Args...)
1081  // is being called with arguments that do not match the functor's call
1082  // signature. See the Visit documentation for details.
1083  functor(this->Storage.GetArrays64(), std::forward<Args>(args)...);
1084  }
1085  else
1086  {
1087  // If you get an error on the next line, a call to Visit(functor, Args...)
1088  // is being called with arguments that do not match the functor's call
1089  // signature. See the Visit documentation for details.
1090  functor(this->Storage.GetArrays32(), std::forward<Args>(args)...);
1091  }
1092  }
1093 
1094  template <typename Functor, typename... Args,
1095  typename = typename std::enable_if<ReturnsVoid<Functor, Args...>::value>::type>
1096  void Visit(Functor&& functor, Args&&... args) const
1097  {
1098  if (this->Storage.Is64Bit())
1099  {
1100  // If you get an error on the next line, a call to Visit(functor, Args...)
1101  // is being called with arguments that do not match the functor's call
1102  // signature. See the Visit documentation for details.
1103  functor(this->Storage.GetArrays64(), std::forward<Args>(args)...);
1104  }
1105  else
1106  {
1107  // If you get an error on the next line, a call to Visit(functor, Args...)
1108  // is being called with arguments that do not match the functor's call
1109  // signature. See the Visit documentation for details.
1110  functor(this->Storage.GetArrays32(), std::forward<Args>(args)...);
1111  }
1112  }
1113 
1114  template <typename Functor, typename... Args,
1115  typename = typename std::enable_if<!ReturnsVoid<Functor, Args...>::value>::type>
1116  GetReturnType<Functor, Args...> Visit(Functor&& functor, Args&&... args)
1117  {
1118  if (this->Storage.Is64Bit())
1119  {
1120  // If you get an error on the next line, a call to Visit(functor, Args...)
1121  // is being called with arguments that do not match the functor's call
1122  // signature. See the Visit documentation for details.
1123  return functor(this->Storage.GetArrays64(), std::forward<Args>(args)...);
1124  }
1125  else
1126  {
1127  // If you get an error on the next line, a call to Visit(functor, Args...)
1128  // is being called with arguments that do not match the functor's call
1129  // signature. See the Visit documentation for details.
1130  return functor(this->Storage.GetArrays32(), std::forward<Args>(args)...);
1131  }
1132  }
1133  template <typename Functor, typename... Args,
1134  typename = typename std::enable_if<!ReturnsVoid<Functor, Args...>::value>::type>
1135  GetReturnType<Functor, Args...> Visit(Functor&& functor, Args&&... args) const
1136  {
1137  if (this->Storage.Is64Bit())
1138  {
1139  // If you get an error on the next line, a call to Visit(functor, Args...)
1140  // is being called with arguments that do not match the functor's call
1141  // signature. See the Visit documentation for details.
1142  return functor(this->Storage.GetArrays64(), std::forward<Args>(args)...);
1143  }
1144  else
1145  {
1146  // If you get an error on the next line, a call to Visit(functor, Args...)
1147  // is being called with arguments that do not match the functor's call
1148  // signature. See the Visit documentation for details.
1149  return functor(this->Storage.GetArrays32(), std::forward<Args>(args)...);
1150  }
1151  }
1152 
1165 #endif // __VTK_WRAP__
1166 
1167  //=================== Begin Legacy Methods ===================================
1168  // These should be deprecated at some point as they are confusing or very slow
1169 
1177 
1189  vtkIdType EstimateSize(vtkIdType numCells, int maxPtsPerCell);
1190 
1200 
1208 
1218  void GetCell(vtkIdType loc, vtkIdType& npts, const vtkIdType*& pts)
1219  VTK_EXPECTS(0 <= loc && loc < GetNumberOfConnectivityEntries()) VTK_SIZEHINT(pts, npts);
1220 
1227  void GetCell(vtkIdType loc, vtkIdList* pts)
1228  VTK_EXPECTS(0 <= loc && loc < GetNumberOfConnectivityEntries());
1229 
1236  vtkIdType GetInsertLocation(int npts);
1237 
1245  vtkIdType GetTraversalLocation();
1246  vtkIdType GetTraversalLocation(vtkIdType npts);
1247  void SetTraversalLocation(vtkIdType loc);
1257  void ReverseCell(vtkIdType loc) VTK_EXPECTS(0 <= loc && loc < GetNumberOfConnectivityEntries());
1258 
1270  void ReplaceCell(vtkIdType loc, int npts, const vtkIdType pts[])
1271  VTK_EXPECTS(0 <= loc && loc < GetNumberOfConnectivityEntries()) VTK_SIZEHINT(pts, npts);
1272 
1287  void SetCells(vtkIdType ncells, vtkIdTypeArray* cells);
1288 
1300 
1301  //=================== End Legacy Methods =====================================
1302 
1303  friend class vtkCellArrayIterator;
1304 
1305 protected:
1307  ~vtkCellArray() override;
1308 
1309  // Encapsulates storage of the internal arrays as a discriminated union
1310  // between 32-bit and 64-bit storage.
1311  struct Storage
1312  {
1313  // Union type that switches 32 and 64 bit array storage
1314  union ArraySwitch {
1315  ArraySwitch() = default; // handled by Storage
1316  ~ArraySwitch() = default; // handle by Storage
1319  };
1320 
1322  {
1323 #ifdef VTK_USE_MEMKIND
1324  this->Arrays =
1326 #else
1327  this->Arrays = new ArraySwitch;
1328 #endif
1329 
1330  // Default can be changed, to save memory
1332  {
1333  this->Arrays->Int64 = new VisitState<ArrayType64>;
1334  this->StorageIs64Bit = true;
1335  }
1336  else
1337  {
1338  this->Arrays->Int32 = new VisitState<ArrayType32>;
1339  this->StorageIs64Bit = false;
1340  }
1341 
1342 #ifdef VTK_USE_MEMKIND
1344  {
1345  this->IsInMemkind = true;
1346  }
1347 #else
1348  (void)this->IsInMemkind; // comp warning workaround
1349 #endif
1350  }
1351 
1353  {
1354  if (this->StorageIs64Bit)
1355  {
1356  this->Arrays->Int64->~VisitState();
1357  delete this->Arrays->Int64;
1358  }
1359  else
1360  {
1361  this->Arrays->Int32->~VisitState();
1362  delete this->Arrays->Int32;
1363  }
1364 #ifdef VTK_USE_MEMKIND
1365  if (this->IsInMemkind)
1366  {
1368  }
1369  else
1370  {
1371  free(this->Arrays);
1372  }
1373 #else
1374  delete this->Arrays;
1375 #endif
1376  }
1377 
1378  // Switch the internal arrays to be 32-bit. Any old data is lost. Returns
1379  // true if the storage changes.
1381  {
1382  if (!this->StorageIs64Bit)
1383  {
1384  return false;
1385  }
1386 
1387  this->Arrays->Int64->~VisitState();
1388  delete this->Arrays->Int64;
1389  this->Arrays->Int32 = new VisitState<ArrayType32>;
1390  this->StorageIs64Bit = false;
1391 
1392  return true;
1393  }
1394 
1395  // Switch the internal arrays to be 64-bit. Any old data is lost. Returns
1396  // true if the storage changes.
1398  {
1399  if (this->StorageIs64Bit)
1400  {
1401  return false;
1402  }
1403 
1404  this->Arrays->Int32->~VisitState();
1405  delete this->Arrays->Int32;
1406  this->Arrays->Int64 = new VisitState<ArrayType64>;
1407  this->StorageIs64Bit = true;
1408 
1409  return true;
1410  }
1411 
1412  // Returns true if the storage is currently configured to be 64 bit.
1413  bool Is64Bit() const { return this->StorageIs64Bit; }
1414 
1415  // Get the VisitState for 32-bit arrays
1417  {
1418  assert(!this->StorageIs64Bit);
1419  return *this->Arrays->Int32;
1420  }
1421 
1423  {
1424  assert(!this->StorageIs64Bit);
1425  return *this->Arrays->Int32;
1426  }
1427 
1428  // Get the VisitState for 64-bit arrays
1430  {
1431  assert(this->StorageIs64Bit);
1432  return *this->Arrays->Int64;
1433  }
1434 
1436  {
1437  assert(this->StorageIs64Bit);
1438  return *this->Arrays->Int64;
1439  }
1440 
1441  private:
1442  // Access restricted to ensure proper union construction/destruction thru
1443  // API.
1444  ArraySwitch* Arrays;
1445  bool StorageIs64Bit;
1446  bool IsInMemkind = false;
1447  };
1448 
1450  vtkIdType TraversalCellId{ 0 };
1451 
1453 
1455 
1456 private:
1457  vtkCellArray(const vtkCellArray&) = delete;
1458  void operator=(const vtkCellArray&) = delete;
1459 };
1460 
1461 template <typename ArrayT>
1463 {
1464  return this->Offsets->GetNumberOfValues() - 1;
1465 }
1466 
1467 template <typename ArrayT>
1469 {
1470  return static_cast<vtkIdType>(this->Offsets->GetValue(cellId));
1471 }
1472 
1473 template <typename ArrayT>
1475 {
1476  return static_cast<vtkIdType>(this->Offsets->GetValue(cellId + 1));
1477 }
1478 
1479 template <typename ArrayT>
1481 {
1482  return this->GetEndOffset(cellId) - this->GetBeginOffset(cellId);
1483 }
1484 
1485 template <typename ArrayT>
1488 {
1489  return vtk::DataArrayValueRange<1>(
1490  this->GetConnectivity(), this->GetBeginOffset(cellId), this->GetEndOffset(cellId));
1491 }
1492 VTK_ABI_NAMESPACE_END
1493 
1495 {
1496 VTK_ABI_NAMESPACE_BEGIN
1497 
1499 {
1500  // Insert full cell
1501  template <typename CellStateT>
1502  vtkIdType operator()(CellStateT& state, const vtkIdType npts, const vtkIdType pts[])
1503  {
1504  using ValueType = typename CellStateT::ValueType;
1505  auto* conn = state.GetConnectivity();
1506  auto* offsets = state.GetOffsets();
1507 
1508  const vtkIdType cellId = offsets->GetNumberOfValues() - 1;
1509 
1510  offsets->InsertNextValue(static_cast<ValueType>(conn->GetNumberOfValues() + npts));
1511 
1512  for (vtkIdType i = 0; i < npts; ++i)
1513  {
1514  conn->InsertNextValue(static_cast<ValueType>(pts[i]));
1515  }
1516 
1517  return cellId;
1518  }
1519 
1520  // Just update offset table (for incremental API)
1521  template <typename CellStateT>
1522  vtkIdType operator()(CellStateT& state, const vtkIdType npts)
1523  {
1524  using ValueType = typename CellStateT::ValueType;
1525  auto* conn = state.GetConnectivity();
1526  auto* offsets = state.GetOffsets();
1527 
1528  const vtkIdType cellId = offsets->GetNumberOfValues() - 1;
1529 
1530  offsets->InsertNextValue(static_cast<ValueType>(conn->GetNumberOfValues() + npts));
1531 
1532  return cellId;
1533  }
1534 };
1535 
1536 // for incremental API:
1538 {
1539  template <typename CellStateT>
1540  void operator()(CellStateT& state, const vtkIdType npts)
1541  {
1542  using ValueType = typename CellStateT::ValueType;
1543 
1544  auto* offsets = state.GetOffsets();
1545  const ValueType cellBegin = offsets->GetValue(offsets->GetMaxId() - 1);
1546  offsets->SetValue(offsets->GetMaxId(), static_cast<ValueType>(cellBegin + npts));
1547  }
1548 };
1549 
1551 {
1552  template <typename CellStateT>
1553  vtkIdType operator()(CellStateT& state, vtkIdType cellId)
1554  {
1555  return state.GetCellSize(cellId);
1556  }
1557 };
1558 
1560 {
1561  template <typename CellStateT>
1562  void operator()(CellStateT& state, const vtkIdType cellId, vtkIdList* ids)
1563  {
1564  using ValueType = typename CellStateT::ValueType;
1565 
1566  const vtkIdType beginOffset = state.GetBeginOffset(cellId);
1567  const vtkIdType endOffset = state.GetEndOffset(cellId);
1568  const vtkIdType cellSize = endOffset - beginOffset;
1569  const auto cellConnectivity = state.GetConnectivity()->GetPointer(beginOffset);
1570 
1571  // ValueType differs from vtkIdType, so we have to copy into a temporary buffer:
1572  ids->SetNumberOfIds(cellSize);
1573  vtkIdType* idPtr = ids->GetPointer(0);
1574  for (ValueType i = 0; i < cellSize; ++i)
1575  {
1576  idPtr[i] = static_cast<vtkIdType>(cellConnectivity[i]);
1577  }
1578  }
1579 
1580  template <typename CellStateT>
1582  CellStateT& state, const vtkIdType cellId, vtkIdType& cellSize, vtkIdType* cellPoints)
1583  {
1584  using ValueType = typename CellStateT::ValueType;
1585 
1586  const vtkIdType beginOffset = state.GetBeginOffset(cellId);
1587  const vtkIdType endOffset = state.GetEndOffset(cellId);
1588  cellSize = endOffset - beginOffset;
1589  const ValueType* cellConnectivity = state.GetConnectivity()->GetPointer(beginOffset);
1590 
1591  // ValueType differs from vtkIdType, so we have to copy into a temporary buffer:
1592  for (vtkIdType i = 0; i < cellSize; ++i)
1593  {
1594  cellPoints[i] = static_cast<vtkIdType>(cellConnectivity[i]);
1595  }
1596  }
1597 
1598  // SFINAE helper to check if a VisitState's connectivity array's memory
1599  // can be used as a vtkIdType*.
1600  template <typename CellStateT>
1602  {
1603  private:
1604  using ValueType = typename CellStateT::ValueType;
1605  using ArrayType = typename CellStateT::ArrayType;
1607  static constexpr bool ValueTypeCompat = CellStateT::ValueTypeIsSameAsIdType;
1608  static constexpr bool ArrayTypeCompat = std::is_base_of<AOSArrayType, ArrayType>::value;
1609 
1610  public:
1611  static constexpr bool value = ValueTypeCompat && ArrayTypeCompat;
1612  };
1613 
1614  template <typename CellStateT>
1616  CellStateT& state, const vtkIdType cellId, vtkIdType& cellSize, vtkIdType const*& cellPoints,
1617  vtkIdList* vtkNotUsed(temp))
1618  {
1619  const vtkIdType beginOffset = state.GetBeginOffset(cellId);
1620  const vtkIdType endOffset = state.GetEndOffset(cellId);
1621  cellSize = endOffset - beginOffset;
1622  // This is safe, see CanShareConnPtr helper above.
1623  cellPoints = reinterpret_cast<vtkIdType*>(state.GetConnectivity()->GetPointer(beginOffset));
1624  }
1625 
1626  template <typename CellStateT>
1628  CellStateT& state, const vtkIdType cellId, vtkIdType& cellSize, vtkIdType const*& cellPoints,
1629  vtkIdList* temp)
1630  {
1631  using ValueType = typename CellStateT::ValueType;
1632 
1633  const vtkIdType beginOffset = state.GetBeginOffset(cellId);
1634  const vtkIdType endOffset = state.GetEndOffset(cellId);
1635  cellSize = endOffset - beginOffset;
1636  const ValueType* cellConnectivity = state.GetConnectivity()->GetPointer(beginOffset);
1637 
1638  // ValueType differs from vtkIdType, so we have to copy into a temporary buffer:
1639  temp->SetNumberOfIds(cellSize);
1640  vtkIdType* tempPtr = temp->GetPointer(0);
1641  for (vtkIdType i = 0; i < cellSize; ++i)
1642  {
1643  tempPtr[i] = static_cast<vtkIdType>(cellConnectivity[i]);
1644  }
1645 
1646  cellPoints = temp->GetPointer(0);
1647  }
1648 };
1649 
1651 {
1652  template <typename CellStateT>
1653  void operator()(CellStateT& state)
1654  {
1655  state.GetOffsets()->Reset();
1656  state.GetConnectivity()->Reset();
1657  state.GetOffsets()->InsertNextValue(0);
1658  }
1659 };
1660 
1661 VTK_ABI_NAMESPACE_END
1662 } // end namespace vtkCellArray_detail
1663 
1664 VTK_ABI_NAMESPACE_BEGIN
1665 //----------------------------------------------------------------------------
1667 {
1668  this->TraversalCellId = 0;
1669 }
1670 
1671 //----------------------------------------------------------------------------
1672 inline int vtkCellArray::GetNextCell(vtkIdType& npts, vtkIdType const*& pts) VTK_SIZEHINT(pts, npts)
1673 {
1674  if (this->TraversalCellId < this->GetNumberOfCells())
1675  {
1676  this->GetCellAtId(this->TraversalCellId, npts, pts);
1677  ++this->TraversalCellId;
1678  return 1;
1679  }
1680 
1681  npts = 0;
1682  pts = nullptr;
1683  return 0;
1684 }
1685 
1686 //----------------------------------------------------------------------------
1688 {
1689  if (this->TraversalCellId < this->GetNumberOfCells())
1690  {
1691  this->GetCellAtId(this->TraversalCellId, pts);
1692  ++this->TraversalCellId;
1693  return 1;
1694  }
1695 
1696  pts->Reset();
1697  return 0;
1698 }
1699 //----------------------------------------------------------------------------
1701 {
1702  return this->Visit(vtkCellArray_detail::GetCellSizeImpl{}, cellId);
1703 }
1704 
1705 //----------------------------------------------------------------------------
1706 inline void vtkCellArray::GetCellAtId(vtkIdType cellId, vtkIdType& cellSize,
1707  vtkIdType const*& cellPoints, vtkIdList* ptIds) VTK_SIZEHINT(cellPoints, cellSize)
1708 {
1709  this->Visit(vtkCellArray_detail::GetCellAtIdImpl{}, cellId, cellSize, cellPoints, ptIds);
1710 }
1711 
1712 //----------------------------------------------------------------------------
1714 {
1715  this->Visit(vtkCellArray_detail::GetCellAtIdImpl{}, cellId, pts);
1716 }
1717 
1718 //----------------------------------------------------------------------------
1719 inline void vtkCellArray::GetCellAtId(vtkIdType cellId, vtkIdType& cellSize, vtkIdType* cellPoints)
1720 {
1721  this->Visit(vtkCellArray_detail::GetCellAtIdImpl{}, cellId, cellSize, cellPoints);
1722 }
1723 
1724 //----------------------------------------------------------------------------
1726  VTK_SIZEHINT(pts, npts)
1727 {
1728  return this->Visit(vtkCellArray_detail::InsertNextCellImpl{}, npts, pts);
1729 }
1730 
1731 //----------------------------------------------------------------------------
1733 {
1734  return this->Visit(vtkCellArray_detail::InsertNextCellImpl{}, npts);
1735 }
1736 
1737 //----------------------------------------------------------------------------
1739 {
1740  if (this->Storage.Is64Bit())
1741  {
1742  using ValueType = typename ArrayType64::ValueType;
1743  this->Storage.GetArrays64().Connectivity->InsertNextValue(static_cast<ValueType>(id));
1744  }
1745  else
1746  {
1747  using ValueType = typename ArrayType32::ValueType;
1748  this->Storage.GetArrays32().Connectivity->InsertNextValue(static_cast<ValueType>(id));
1749  }
1750 }
1751 
1752 //----------------------------------------------------------------------------
1753 inline void vtkCellArray::UpdateCellCount(int npts)
1754 {
1756 }
1757 
1758 //----------------------------------------------------------------------------
1760 {
1761  return this->Visit(
1763 }
1764 
1765 //----------------------------------------------------------------------------
1767 {
1768  vtkIdList* pts = cell->GetPointIds();
1769  return this->Visit(
1771 }
1772 
1773 //----------------------------------------------------------------------------
1774 inline void vtkCellArray::Reset()
1775 {
1777 }
1778 
1779 VTK_ABI_NAMESPACE_END
1780 #endif // vtkCellArray.h
abstract object to represent cell connectivity
virtual vtkIdType GetNumberOfCells() const =0
Get the number of cells in the array.
void GetCellAtId(vtkIdType cellId, vtkIdType &cellSize, vtkIdType const *&cellPoints)
Return the point ids for the cell at cellId.
virtual vtkIdType GetCellSize(vtkIdType cellId) const =0
Return the size of the cell at cellId.
Encapsulate traversal logic for vtkCellArray.
object to represent cell connectivity
Definition: vtkCellArray.h:286
vtkIdType GetCellSize(vtkIdType cellId) const override
Return the size of the cell at cellId.
void SetData(vtkAOSDataArrayTemplate< int > *offsets, vtkAOSDataArrayTemplate< int > *connectivity)
Set the internal data arrays to the supplied offsets and connectivity arrays.
int GetNextCell(vtkIdType &npts, vtkIdType const *&pts)
vtkIdType IsHomogeneous() override
Check if all cells have the same number of vertices.
vtkIdType GetOffset(vtkIdType cellId) override
Get the offset (into the connectivity) for a specified cell id.
Definition: vtkCellArray.h:450
vtkIdType GetNumberOfConnectivityEntries()
Return the size of the array that would be returned from ExportLegacyFormat().
void SetData(vtkTypeInt32Array *offsets, vtkTypeInt32Array *connectivity)
Set the internal data arrays to the supplied offsets and connectivity arrays.
vtkTypeBool Allocate(vtkIdType sz, vtkIdType vtkNotUsed(ext)=1000)
Allocate memory.
Definition: vtkCellArray.h:332
void UseDefaultStorage()
Initialize internal data structures to use 32- or 64-bit storage.
Storage Storage
bool AllocateCopy(vtkCellArray *other)
Pre-allocate memory in internal data structures to match the used size of the input vtkCellArray.
Definition: vtkCellArray.h:371
void AppendLegacyFormat(vtkIdTypeArray *data, vtkIdType ptOffset=0)
Append an array of data with the legacy vtkCellArray layout, e.g.
static void SetDefaultStorageIs64Bit(bool val)
Control the default internal storage size.
void DeepCopy(vtkAbstractCellArray *ca) override
Perform a deep copy (no reference counting) of the given cell array.
ArrayType32 * GetOffsetsArray32()
Return the array used to store cell offsets.
Definition: vtkCellArray.h:623
bool IsValid()
Check that internal storage is consistent and in a valid state.
void AppendLegacyFormat(const vtkIdType *data, vtkIdType len, vtkIdType ptOffset=0)
Append an array of data with the legacy vtkCellArray layout, e.g.
bool SetData(vtkIdType cellSize, vtkDataArray *connectivity)
Sets the internal arrays to the supported connectivity array with an offsets array automatically gene...
void ShallowCopy(vtkAbstractCellArray *ca) override
Shallow copy ca into this cell array.
vtkIdType GetNumberOfOffsets() const override
Get the number of elements in the offsets array.
Definition: vtkCellArray.h:435
vtkIdType GetTraversalCellId()
Get/Set the current cellId for traversal.
void Visit(Functor &&functor, Args &&... args)
vtkTypeInt32Array ArrayType32
Definition: vtkCellArray.h:288
vtkIdType GetNumberOfCells() const override
Get the number of cells in the array.
Definition: vtkCellArray.h:419
void Reset()
Reuse list.
ArrayType64 * GetOffsetsArray64()
Return the array used to store cell offsets.
Definition: vtkCellArray.h:624
vtkIdType GetNumberOfConnectivityIds() const override
Get the size of the connectivity array that stores the point ids.
Definition: vtkCellArray.h:468
void SetData(vtkAOSDataArrayTemplate< long long > *offsets, vtkAOSDataArrayTemplate< long long > *connectivity)
Set the internal data arrays to the supplied offsets and connectivity arrays.
GetReturnType< Functor, Args... > Visit(Functor &&functor, Args &&... args)
bool AllocateExact(vtkIdType numCells, vtkIdType connectivitySize)
Pre-allocate memory in internal data structures.
bool ResizeExact(vtkIdType numCells, vtkIdType connectivitySize)
ResizeExact() resizes the internal structures to hold numCells total cell offsets and connectivitySiz...
vtkIdType EstimateSize(vtkIdType numCells, int maxPtsPerCell)
Utility routines help manage memory of cell array.
bool AllocateEstimate(vtkIdType numCells, vtkIdType maxCellSize)
Pre-allocate memory in internal data structures.
Definition: vtkCellArray.h:346
vtkTypeInt64Array ArrayType64
Definition: vtkCellArray.h:289
vtkIdType TraversalCellId
void InitTraversal()
bool IsStorageShareable() const override
Definition: vtkCellArray.h:547
void Use64BitStorage()
Initialize internal data structures to use 32- or 64-bit storage.
bool ConvertToDefaultStorage()
Convert internal data structures to use 32- or 64-bit storage.
bool CanConvertToDefaultStorage() const
Check if the existing data can safely be converted to use 32- or 64- bit storage.
static bool DefaultStorageIs64Bit
void GetCell(vtkIdType loc, vtkIdType &npts, const vtkIdType *&pts)
Internal method used to retrieve a cell given a legacy offset location.
bool CanConvertTo32BitStorage() const
Check if the existing data can safely be converted to use 32- or 64- bit storage.
void InsertCellPoint(vtkIdType id)
Used in conjunction with InsertNextCell(npts) to add another point to the list of cells.
void ReplaceCellAtId(vtkIdType cellId, vtkIdList *list)
Replaces the point ids for the specified cell with the supplied list.
void ReverseCellAtId(vtkIdType cellId)
Reverses the order of the point ids for the specified cell.
bool SetData(vtkDataArray *offsets, vtkDataArray *connectivity)
Sets the internal arrays to the supplied offsets and connectivity arrays.
void Initialize() override
Free any memory and reset to an empty state.
void Squeeze()
Reclaim any extra memory while preserving data.
void SetData(vtkIdTypeArray *offsets, vtkIdTypeArray *connectivity)
Set the internal data arrays to the supplied offsets and connectivity arrays.
static vtkCellArray * New()
Standard methods for instantiation, type information, and printing.
ArrayType32 * GetConnectivityArray32()
Return the array used to store the point ids that define the cells' connectivity.
Definition: vtkCellArray.h:644
ArrayType64 * GetConnectivityArray64()
Return the array used to store the point ids that define the cells' connectivity.
Definition: vtkCellArray.h:645
bool ConvertTo32BitStorage()
Convert internal data structures to use 32- or 64-bit storage.
void Visit(Functor &&functor, Args &&... args) const
bool ConvertToSmallestStorage()
Convert internal data structures to use 32- or 64-bit storage.
void ExportLegacyFormat(vtkIdTypeArray *data)
Fill data with the old-style vtkCellArray data layout, e.g.
bool ConvertTo64BitStorage()
Convert internal data structures to use 32- or 64-bit storage.
void SetData(vtkAOSDataArrayTemplate< long > *offsets, vtkAOSDataArrayTemplate< long > *connectivity)
Set the internal data arrays to the supplied offsets and connectivity arrays.
void ImportLegacyFormat(const vtkIdType *data, vtkIdType len)
Import an array of data with the legacy vtkCellArray layout, e.g.
void ImportLegacyFormat(vtkIdTypeArray *data)
Import an array of data with the legacy vtkCellArray layout, e.g.
void Use32BitStorage()
Initialize internal data structures to use 32- or 64-bit storage.
typename vtkTypeList::Unique< vtkTypeList::Create< vtkAOSDataArrayTemplate< int >, vtkAOSDataArrayTemplate< long >, vtkAOSDataArrayTemplate< long long > >>::Result InputArrayList
List of possible ArrayTypes that are compatible with internal storage.
Definition: vtkCellArray.h:322
vtkDataArray * GetConnectivityArray()
Return the array used to store the point ids that define the cells' connectivity.
Definition: vtkCellArray.h:633
void SetTraversalCellId(vtkIdType cellId)
Get/Set the current cellId for traversal.
void SetData(vtkTypeInt64Array *offsets, vtkTypeInt64Array *connectivity)
Set the internal data arrays to the supplied offsets and connectivity arrays.
static bool GetDefaultStorageIs64Bit()
Control the default internal storage size.
int GetMaxCellSize() override
Returns the size of the largest cell.
vtkIdType InsertNextCell(vtkCell *cell)
Insert a cell object.
virtual void SetNumberOfCells(vtkIdType)
Set the number of cells in the array.
vtkNew< vtkIdTypeArray > LegacyData
void PrintSelf(ostream &os, vtkIndent indent) override
Standard methods for instantiation, type information, and printing.
void PrintDebug(ostream &os)
Standard methods for instantiation, type information, and printing.
vtkCellArrayIterator * NewIterator()
NewIterator returns a new instance of vtkCellArrayIterator that is initialized to point at the first ...
void UpdateCellCount(int npts)
Used in conjunction with InsertNextCell(int npts) and InsertCellPoint() to update the number of point...
void GetCellAtId(vtkIdType cellId, vtkIdType &cellSize, vtkIdType const *&cellPoints)
Return the point ids for the cell at cellId.
GetReturnType< Functor, Args... > Visit(Functor &&functor, Args &&... args) const
vtkIdType GetSize()
Get the size of the allocated connectivity array.
vtkDataArray * GetOffsetsArray()
Return the array used to store cell offsets.
Definition: vtkCellArray.h:612
vtkTypeList::Create< ArrayType32, ArrayType64 > StorageArrayList
List of possible array types used for storage.
Definition: vtkCellArray.h:310
vtkIdType InsertNextCell(const std::initializer_list< vtkIdType > &cell)
Overload that allows InsertNextCell({0, 1, 2}) syntax.
Definition: vtkCellArray.h:758
void Append(vtkCellArray *src, vtkIdType pointOffset=0)
Append cells from src into this.
bool IsStorage64Bit() const
Definition: vtkCellArray.h:539
void ReplaceCellAtId(vtkIdType cellId, vtkIdType cellSize, const vtkIdType *cellPoints)
Replaces the point ids for the specified cell with the supplied list.
bool CanConvertTo64BitStorage() const
Check if the existing data can safely be converted to use 32- or 64- bit storage.
abstract class to specify cell behavior
Definition: vtkCell.h:130
vtkIdList * GetPointIds()
Return the list of point ids defining the cell.
Definition: vtkCell.h:228
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:155
list of point or cell ids
Definition: vtkIdList.h:133
void SetNumberOfIds(vtkIdType number)
Specify the number of ids for this object to hold.
vtkIdType GetNumberOfIds() const noexcept
Return the number of id's in the list.
Definition: vtkIdList.h:159
void Reset()
Reset to an empty state but retain previously allocated memory.
Definition: vtkIdList.h:244
vtkIdType * GetPointer(vtkIdType i)
Get a pointer to a particular data index.
Definition: vtkIdList.h:225
dynamic, self-adjusting array of vtkIdType
a simple class to control print indentation
Definition: vtkIndent.h:108
static vtkMallocingFunction GetCurrentMallocFunction()
static vtkFreeingFunction GetAlternateFreeFunction()
static bool GetUsingMemkind()
A global state flag that controls whether vtkObjects are constructed in the usual way (the default) o...
static vtkSmartPointer< T > New()
Create an instance of a VTK object.
vtkSmartPointer< vtkDataArray > GetData(const Ioss::GroupingEntity *entity, const std::string &fieldname, Ioss::Transform *transform=nullptr, Cache *cache=nullptr, const std::string &cachekey=std::string())
Returns a VTK array for a given field (fieldname) on the chosen block (or set) entity.
vtkSmartPointer< vtkCellArray > GetConnectivity(Ioss::GroupingEntity *group_entity, int &vtk_topology_type, Cache *cache=nullptr)
Read connectivity information from the group_entity.
@ value
Definition: vtkX3D.h:220
@ type
Definition: vtkX3D.h:516
@ data
Definition: vtkX3D.h:315
VisitState< ArrayType32 > & GetArrays32()
const VisitState< ArrayType64 > & GetArrays64() const
const VisitState< ArrayType32 > & GetArrays32() const
VisitState< ArrayType64 > & GetArrays64()
ArrayType * GetOffsets()
Definition: vtkCellArray.h:928
vtkIdType GetNumberOfCells() const
vtkIdType GetEndOffset(vtkIdType cellId) const
vtkSmartPointer< ArrayType > Offsets
Definition: vtkCellArray.h:986
vtkSmartPointer< ArrayType > Connectivity
Definition: vtkCellArray.h:985
ArrayType * GetConnectivity()
Definition: vtkCellArray.h:931
decltype(vtk::DataArrayValueRange< 1 >(std::declval< ArrayType >())) CellRangeType
Definition: vtkCellArray.h:918
CellRangeType GetCellRange(vtkIdType cellId)
const ArrayType * GetOffsets() const
Definition: vtkCellArray.h:929
vtkIdType GetBeginOffset(vtkIdType cellId) const
vtkIdType GetCellSize(vtkIdType cellId) const
const ArrayType * GetConnectivity() const
Definition: vtkCellArray.h:932
typename ArrayType::ValueType ValueType
Definition: vtkCellArray.h:917
void operator()(CellStateT &state, const vtkIdType cellId, vtkIdList *ids)
void operator()(CellStateT &state, const vtkIdType cellId, vtkIdType &cellSize, vtkIdType *cellPoints)
std::enable_if<!CanShareConnPtr< CellStateT >::value, void >::type operator()(CellStateT &state, const vtkIdType cellId, vtkIdType &cellSize, vtkIdType const *&cellPoints, vtkIdList *temp)
std::enable_if< CanShareConnPtr< CellStateT >::value, void >::type operator()(CellStateT &state, const vtkIdType cellId, vtkIdType &cellSize, vtkIdType const *&cellPoints, vtkIdList *vtkNotUsed(temp))
vtkIdType operator()(CellStateT &state, vtkIdType cellId)
vtkIdType operator()(CellStateT &state, const vtkIdType npts, const vtkIdType pts[])
vtkIdType operator()(CellStateT &state, const vtkIdType npts)
void operator()(CellStateT &state)
void operator()(CellStateT &state, const vtkIdType npts)
Remove all duplicate types from TypeList TList, storing the new list in Result.
Definition: vtkTypeList.h:110
VisitState< ArrayType32 > * Int32
VisitState< ArrayType64 > * Int64
int vtkTypeBool
Definition: vtkABI.h:64
STL-compatible iterable ranges that provide access to vtkDataArray elements.
int vtkIdType
Definition: vtkType.h:315
#define VTK_SIZEHINT(...)
#define VTK_EXPECTS(x)
#define VTK_MARSHALMANUAL
#define VTK_NEWINSTANCE