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 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCellArray.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.
=========================================================================*/
#include "vtkCellArray.h"
#include "vtkArrayDispatch.h"
#include "vtkCellArrayIterator.h"
#include "vtkDataArrayRange.h"
#include "vtkIdTypeArray.h"
#include "vtkIntArray.h"
#include "vtkLongArray.h"
#include "vtkLongLongArray.h"
#include "vtkObjectFactory.h"
#include "vtkSMPThreadLocal.h"
#include "vtkSMPTools.h"
#include <algorithm>
#include <array>
#include <iterator>
namespace
{
// These implementations are for methods that will be deprecated in the future:
namespace deprec
{
struct GetSizeImpl
{
template <typename CellStateT>
vtkIdType operator()(CellStateT& cells) const
{
return (cells.GetOffsets()->GetSize() + cells.GetConnectivity()->GetSize());
}
};
// Given a legacy Location, find the corresponding cellId. The location
// *must* refer to a [numPts] entry in the old connectivity array, or the
// returned CellId will be -1.
struct LocationToCellIdFunctor
{
template <typename CellStateT>
vtkIdType operator()(CellStateT& cells, vtkIdType location) const
{
using ValueType = typename CellStateT::ValueType;
const auto offsets = vtk::DataArrayValueRange<1>(cells.GetOffsets());
// Use a binary-search to find the location:
auto it = this->BinarySearchOffset(
offsets.begin(), offsets.end() - 1, static_cast<ValueType>(location));
const vtkIdType cellId = std::distance(offsets.begin(), it);
if (it == offsets.end() - 1 /* no match found */ ||
(*it + cellId) != location /* `location` not at cell head */)
{ // Location invalid.
return -1;
}
// return the cell id:
return cellId;
}
template <typename IterT>
IterT BinarySearchOffset(const IterT& beginIter, const IterT& endIter,
const typename std::iterator_traits<IterT>::value_type& targetLocation) const
{
using ValueType = typename std::iterator_traits<IterT>::value_type;
using DifferenceType = typename std::iterator_traits<IterT>::difference_type;
DifferenceType roiSize = std::distance(beginIter, endIter);
IterT roiBegin = beginIter;
while (roiSize > 0)
{
IterT it = roiBegin;
const DifferenceType step = roiSize / 2;
std::advance(it, step);
// This differs from a generic binary search in the following line:
// Adding the distance from the start of the array to the current
// iterator will account for the cellSize entries in the old cell array
// format, such that curLocation would be the offset in the old style
// connectivity array.
const ValueType curLocation = *it + std::distance(beginIter, it);
if (curLocation < targetLocation)
{
roiBegin = ++it;
roiSize -= step + 1;
}
else
{
roiSize = step;
}
}
return roiBegin;
}
};
struct CellIdToLocationFunctor
{
template <typename CellStateT>
vtkIdType operator()(CellStateT& cells, vtkIdType cellId) const
{
// Adding the cellId to the offset of that cell id gives us the cell
// location in the old-style vtkCellArray connectivity array.
return static_cast<vtkIdType>(cells.GetOffsets()->GetValue(cellId)) + cellId;
}
};
struct GetInsertLocationImpl
{
template <typename CellStateT>
vtkIdType operator()(CellStateT& cells) const
{
// The insert location used to just be the tail of the connectivity array.
// Compute the equivalent value:
return (
(cells.GetOffsets()->GetNumberOfValues() - 1) + cells.GetConnectivity()->GetNumberOfValues());
}
};
} // end namespace deprec
struct PrintDebugImpl
{
template <typename CellStateT>
void operator()(CellStateT& state, std::ostream& os)
{
using ValueType = typename CellStateT::ValueType;
const vtkIdType numCells = state.GetNumberOfCells();
for (vtkIdType cellId = 0; cellId < numCells; ++cellId)
{
os << "cell " << cellId << ": ";
const auto cellRange = state.GetCellRange(cellId);
for (ValueType ptId : cellRange)
{
os << ptId << " ";
}
os << "\n";
}
}
};
struct InitializeImpl
{
template <typename CellStateT>
void operator()(CellStateT& cells) const
{
cells.GetConnectivity()->Initialize();
cells.GetOffsets()->Initialize();
cells.GetOffsets()->InsertNextValue(0);
}
};
struct SqueezeImpl
{
template <typename CellStateT>
void operator()(CellStateT& cells) const
{
cells.GetConnectivity()->Squeeze();
cells.GetOffsets()->Squeeze();
}
};
struct IsValidImpl
{
template <typename CellStateT>
bool operator()(CellStateT& state) const
{
using ValueType = typename CellStateT::ValueType;
auto* offsetArray = state.GetOffsets();
auto* connArray = state.GetConnectivity();
// Both arrays must be single component
if (offsetArray->GetNumberOfComponents() != 1 || connArray->GetNumberOfComponents() != 1)
{
return false;
}
auto offsets = vtk::DataArrayValueRange<1>(offsetArray);
// Offsets must have at least one value, and the first value must be zero
if (offsets.size() == 0 || *offsets.cbegin() != 0)
{
return false;
}
// Values in offsets must not decrease
auto it = std::adjacent_find(offsets.cbegin(), offsets.cend(),
[](const ValueType a, const ValueType b) -> bool { return a > b; });
if (it != offsets.cend())
{
return false;
}
// The last value in offsets must be the size of the connectivity array.
if (connArray->GetNumberOfValues() != *(offsets.cend() - 1))
{
return false;
}
return true;
}
};
template <typename T>
struct CanConvert
{
template <typename CellStateT>
bool operator()(CellStateT& state) const
{
using ArrayType = typename CellStateT::ArrayType;
using ValueType = typename CellStateT::ValueType;
// offsets are sorted, so just check the last value, but we have to compute
// the full range of the connectivity array.
auto* off = state.GetOffsets();
if (off->GetNumberOfValues() > 0 && !this->CheckValue(off->GetValue(off->GetMaxId())))
{
return false;
}
std::array<ValueType, 2> connRange;
auto* mutConn = const_cast<ArrayType*>(state.GetConnectivity());
if (mutConn->GetNumberOfValues() > 0)
{
mutConn->GetValueRange(connRange.data(), 0);
if (!this->CheckValue(connRange[0]) || !this->CheckValue(connRange[1]))
{
return false;
}
}
return true;
}
template <typename U>
bool CheckValue(const U& val) const
{
return val == static_cast<U>(static_cast<T>(val));
}
};
struct ExtractAndInitialize
{
template <typename CellStateT, typename TargetArrayT>
bool operator()(CellStateT& state, TargetArrayT* offsets, TargetArrayT* conn) const
{
return (
this->Process(state.GetOffsets(), offsets) && this->Process(state.GetConnectivity(), conn));
}
template <typename SourceArrayT, typename TargetArrayT>
bool Process(SourceArrayT* src, TargetArrayT* dst) const
{
// Check that allocation suceeds:
if (!dst->Resize(src->GetNumberOfTuples()))
{
return false;
}
// Copy data:
dst->DeepCopy(src);
// Free old memory:
src->Resize(0);
return true;
}
};
struct IsHomogeneousImpl
{
template <typename CellArraysT>
vtkIdType operator()(CellArraysT& state) const
{
using ValueType = typename CellArraysT::ValueType;
auto* offsets = state.GetOffsets();
const vtkIdType numCells = state.GetNumberOfCells();
if (numCells == 0)
{
return 0;
}
// Initialize using the first cell:
const vtkIdType firstCellSize = state.GetCellSize(0);
// Verify the rest:
auto offsetRange = vtk::DataArrayValueRange<1>(offsets);
auto it = std::adjacent_find(offsetRange.begin() + 1, offsetRange.end(),
[&](const ValueType a, const ValueType b) -> bool { return (b - a != firstCellSize); });
if (it != offsetRange.end())
{ // Found a cell that doesn't match the size of the first cell:
return -1;
}
return firstCellSize;
}
};
struct AllocateExactImpl
{
template <typename CellStateT>
bool operator()(CellStateT& cells, vtkIdType numCells, vtkIdType connectivitySize) const
{
const bool result = (cells.GetOffsets()->Allocate(numCells + 1) &&
cells.GetConnectivity()->Allocate(connectivitySize));
if (result)
{
cells.GetOffsets()->InsertNextValue(0);
}
return result;
}
};
struct ResizeExactImpl
{
template <typename CellStateT>
bool operator()(CellStateT& cells, vtkIdType numCells, vtkIdType connectivitySize) const
{
return (cells.GetOffsets()->SetNumberOfValues(numCells + 1) &&
cells.GetConnectivity()->SetNumberOfValues(connectivitySize));
}
};
struct FindMaxCell // SMP functor
{
vtkCellArray* CellArray;
vtkIdType Result{ 0 };
vtkSMPThreadLocal<vtkIdType> LocalResult;
FindMaxCell(vtkCellArray* array)
: CellArray{ array }
{
}
void Initialize() { this->LocalResult.Local() = 0; }
struct Impl
{
template <typename CellStateT>
vtkIdType operator()(CellStateT& cells, vtkIdType cellId, const vtkIdType endCellId) const
{
vtkIdType result = 0;
for (; cellId < endCellId; ++cellId)
{
result = std::max(result, cells.GetCellSize(cellId));
}
return result;
}
};
void operator()(vtkIdType cellId, vtkIdType endCellId)
{
vtkIdType& lval = this->LocalResult.Local();
lval = std::max(lval, this->CellArray->Visit(Impl{}, cellId, endCellId));
}
void Reduce()
{
for (const vtkIdType lResult : this->LocalResult)
{
this->Result = std::max(this->Result, lResult);
}
}
};
struct GetActualMemorySizeImpl
{
template <typename CellStateT>
unsigned long operator()(CellStateT& cells) const
{
return (
cells.GetOffsets()->GetActualMemorySize() + cells.GetConnectivity()->GetActualMemorySize());
}
};
struct PrintSelfImpl
{
template <typename CellStateT>
void operator()(CellStateT& cells, ostream& os, vtkIndent indent) const
{
os << indent << "Offsets:\n";
cells.GetOffsets()->PrintSelf(os, indent.GetNextIndent());
os << indent << "Connectivity:\n";
cells.GetConnectivity()->PrintSelf(os, indent.GetNextIndent());
}
};
struct GetLegacyDataSizeImpl
{
template <typename CellStateT>
vtkIdType operator()(CellStateT& cells) const
{
return (
(cells.GetOffsets()->GetNumberOfValues() - 1) + cells.GetConnectivity()->GetNumberOfValues());
}
};
struct ReverseCellAtIdImpl
{
template <typename CellStateT>
void operator()(CellStateT& cells, vtkIdType cellId) const
{
auto cellRange = cells.GetCellRange(cellId);
std::reverse(cellRange.begin(), cellRange.end());
}
};
struct ReplaceCellAtIdImpl
{
template <typename CellStateT>
void operator()(
CellStateT& cells, vtkIdType cellId, vtkIdType cellSize, const vtkIdType* cellPoints) const
{
using ValueType = typename CellStateT::ValueType;
auto cellRange = cells.GetCellRange(cellId);
assert(cellRange.size() == cellSize);
for (vtkIdType i = 0; i < cellSize; ++i)
{
cellRange[i] = static_cast<ValueType>(cellPoints[i]);
}
}
};
struct AppendLegacyFormatImpl
{
template <typename CellStateT>
void operator()(
CellStateT& cells, const vtkIdType* data, const vtkIdType len, const vtkIdType ptOffset) const
{
using ValueType = typename CellStateT::ValueType;
ValueType offset = static_cast<ValueType>(cells.GetConnectivity()->GetNumberOfValues());
const vtkIdType* const dataEnd = data + len;
while (data < dataEnd)
{
vtkIdType numPts = *data++;
offset += static_cast<ValueType>(numPts);
cells.GetOffsets()->InsertNextValue(offset);
while (numPts-- > 0)
{
cells.GetConnectivity()->InsertNextValue(static_cast<ValueType>(*data++ + ptOffset));
}
}
}
};
struct AppendImpl
{
// Call this signature:
template <typename DstCellStateT>
void operator()(DstCellStateT& dstcells, vtkCellArray* src, vtkIdType pointOffset) const
{ // dispatch on src:
src->Visit(*this, dstcells, pointOffset);
}
// Above signature calls this operator in Visit:
template <typename SrcCellStateT, typename DstCellStateT>
void operator()(SrcCellStateT& src, DstCellStateT& dst, vtkIdType pointOffsets) const
{
this->AppendArrayWithOffset(
src.GetOffsets(), dst.GetOffsets(), dst.GetConnectivity()->GetNumberOfValues(), true);
this->AppendArrayWithOffset(src.GetConnectivity(), dst.GetConnectivity(), pointOffsets, false);
}
// Assumes both arrays are 1 component. src's data is appended to dst with
// offset added to each value.
template <typename SrcArrayT, typename DstArrayT>
void AppendArrayWithOffset(
SrcArrayT* srcArray, DstArrayT* dstArray, vtkIdType offset, bool skipFirst) const
{
VTK_ASSUME(srcArray->GetNumberOfComponents() == 1);
VTK_ASSUME(dstArray->GetNumberOfComponents() == 1);
using SrcValueType = vtk::GetAPIType<SrcArrayT>;
using DstValueType = vtk::GetAPIType<DstArrayT>;
const vtkIdType srcSize =
skipFirst ? srcArray->GetNumberOfValues() - 1 : srcArray->GetNumberOfValues();
const vtkIdType dstBegin = dstArray->GetNumberOfValues();
const vtkIdType dstEnd = dstBegin + srcSize;
// This extends the allocation of dst to ensure we have enough space
// allocated:
dstArray->InsertValue(dstEnd - 1, 0);
const auto srcRange = vtk::DataArrayValueRange<1>(srcArray, skipFirst ? 1 : 0);
auto dstRange = vtk::DataArrayValueRange<1>(dstArray, dstBegin, dstEnd);
assert(srcRange.size() == dstRange.size());
const DstValueType dOffset = static_cast<DstValueType>(offset);
std::transform(srcRange.cbegin(), srcRange.cend(), dstRange.begin(),
[&](SrcValueType x) -> DstValueType { return static_cast<DstValueType>(x) + dOffset; });
}
};
} // end anon namespace
vtkCellArray::vtkCellArray() = default;
vtkCellArray::~vtkCellArray() = default;
vtkStandardNewMacro(vtkCellArray);
//=================== Begin Legacy Methods ===================================
// These should be deprecated at some point as they are confusing or very slow
//----------------------------------------------------------------------------
vtkIdType vtkCellArray::GetSize()
{
// We can still compute roughly the same result, so go ahead and do that.
return this->Visit(deprec::GetSizeImpl{});
}
//----------------------------------------------------------------------------
vtkIdType vtkCellArray::GetNumberOfConnectivityEntries()
{
// We can still compute roughly the same result, so go ahead and do that.
return this->Visit(GetLegacyDataSizeImpl{});
}
//----------------------------------------------------------------------------
void vtkCellArray::GetCell(vtkIdType loc, vtkIdType& npts, const vtkIdType*& pts)
{
const vtkIdType cellId = this->Visit(deprec::LocationToCellIdFunctor{}, loc);
if (cellId < 0)
{
vtkErrorMacro("Invalid location.");
npts = 0;
pts = nullptr;
return;
}
this->GetCellAtId(cellId, this->TempCell);
npts = this->TempCell->GetNumberOfIds();
pts = this->TempCell->GetPointer(0);
}
//----------------------------------------------------------------------------
void vtkCellArray::GetCell(vtkIdType loc, vtkIdList* pts)
{
const vtkIdType cellId = this->Visit(deprec::LocationToCellIdFunctor{}, loc);
if (cellId < 0)
{
vtkErrorMacro("Invalid location.");
pts->Reset();
return;
}
this->GetCellAtId(cellId, pts);
}
//----------------------------------------------------------------------------
vtkIdType vtkCellArray::GetInsertLocation(int npts)
{
// It looks like the original implementation of this actually returned the
// location of the last cell (of size npts), not the current insert location.
return this->Visit(deprec::GetInsertLocationImpl{}) - npts - 1;
}
//----------------------------------------------------------------------------
vtkIdType vtkCellArray::GetTraversalLocation()
{
return this->Visit(deprec::CellIdToLocationFunctor{}, this->GetTraversalCellId());
}
//----------------------------------------------------------------------------
vtkIdType vtkCellArray::GetTraversalLocation(vtkIdType npts)
{
return this->Visit(deprec::CellIdToLocationFunctor{}, this->GetTraversalCellId()) - npts - 1;
}
//----------------------------------------------------------------------------
void vtkCellArray::SetTraversalLocation(vtkIdType loc)
{
const vtkIdType cellId = this->Visit(deprec::LocationToCellIdFunctor{}, loc);
if (cellId < 0)
{
vtkErrorMacro("Invalid location, ignoring.");
return;
}
this->SetTraversalCellId(cellId);
}
//----------------------------------------------------------------------------
vtkIdType vtkCellArray::EstimateSize(vtkIdType numCells, int maxPtsPerCell)
{
return numCells * (1 + maxPtsPerCell);
}
//----------------------------------------------------------------------------
void vtkCellArray::SetNumberOfCells(vtkIdType)
{
// no-op
}
//----------------------------------------------------------------------------
void vtkCellArray::ReverseCell(vtkIdType loc)
{
const vtkIdType cellId = this->Visit(deprec::LocationToCellIdFunctor{}, loc);
if (cellId < 0)
{
vtkErrorMacro("Invalid location, ignoring.");
return;
}
this->ReverseCellAtId(cellId);
}
//----------------------------------------------------------------------------
void vtkCellArray::ReplaceCell(vtkIdType loc, int npts, const vtkIdType pts[])
{
const vtkIdType cellId = this->Visit(deprec::LocationToCellIdFunctor{}, loc);
if (cellId < 0)
{
vtkErrorMacro("Invalid location, ignoring.");
return;
}
this->ReplaceCellAtId(cellId, static_cast<vtkIdType>(npts), pts);
}
//----------------------------------------------------------------------------
vtkIdTypeArray* vtkCellArray::GetData()
{
this->ExportLegacyFormat(this->LegacyData);
return this->LegacyData;
}
//----------------------------------------------------------------------------
// Specify a group of cells.
void vtkCellArray::SetCells(vtkIdType ncells, vtkIdTypeArray* cells)
{
this->AllocateExact(ncells, cells->GetNumberOfValues() - ncells);
this->ImportLegacyFormat(cells);
}
//=================== End Legacy Methods =====================================
//----------------------------------------------------------------------------
void vtkCellArray::DeepCopy(vtkCellArray* ca)
{
if (ca == this)
{
return;
}
if (ca->Storage.Is64Bit())
{
this->Storage.Use64BitStorage();
auto& srcStorage = ca->Storage.GetArrays64();
auto& dstStorage = this->Storage.GetArrays64();
dstStorage.Offsets->DeepCopy(srcStorage.Offsets);
dstStorage.Connectivity->DeepCopy(srcStorage.Connectivity);
this->Modified();
}
else
{
this->Storage.Use32BitStorage();
auto& srcStorage = ca->Storage.GetArrays32();
auto& dstStorage = this->Storage.GetArrays32();
dstStorage.Offsets->DeepCopy(srcStorage.Offsets);
dstStorage.Connectivity->DeepCopy(srcStorage.Connectivity);
this->Modified();
}
}
//----------------------------------------------------------------------------
void vtkCellArray::ShallowCopy(vtkCellArray* ca)
{
if (ca == this)
{
return;
}
if (ca->Storage.Is64Bit())
{
auto& srcStorage = ca->Storage.GetArrays64();
this->SetData(srcStorage.GetOffsets(), srcStorage.GetConnectivity());
}
else
{
auto& srcStorage = ca->Storage.GetArrays32();
this->SetData(srcStorage.GetOffsets(), srcStorage.GetConnectivity());
}
}
//----------------------------------------------------------------------------
void vtkCellArray::Append(vtkCellArray* src, vtkIdType pointOffset)
{
if (src->GetNumberOfCells() > 0)
{
this->Visit(AppendImpl{}, src, pointOffset);
}
}
//----------------------------------------------------------------------------
void vtkCellArray::Initialize()
{
this->Visit(InitializeImpl{});
this->LegacyData->Initialize();
}
//----------------------------------------------------------------------------
vtkCellArrayIterator* vtkCellArray::NewIterator()
{
vtkCellArrayIterator* iter = vtkCellArrayIterator::New();
iter->SetCellArray(this);
iter->GoToFirstCell();
return iter;
}
//----------------------------------------------------------------------------
void vtkCellArray::SetData(vtkTypeInt32Array* offsets, vtkTypeInt32Array* connectivity)
{
if (offsets->GetNumberOfComponents() != 1 || connectivity->GetNumberOfComponents() != 1)
{
vtkErrorMacro("Only single component arrays may be used for vtkCellArray "
"storage.");
return;
}
this->Storage.Use32BitStorage();
auto& storage = this->Storage.GetArrays32();
// vtkArrayDownCast to ensure this works when ArrayType32 is vtkIdTypeArray.
storage.Offsets = vtkArrayDownCast<ArrayType32>(offsets);
storage.Connectivity = vtkArrayDownCast<ArrayType32>(connectivity);
this->Modified();
}
//----------------------------------------------------------------------------
void vtkCellArray::SetData(vtkTypeInt64Array* offsets, vtkTypeInt64Array* connectivity)
{
if (offsets->GetNumberOfComponents() != 1 || connectivity->GetNumberOfComponents() != 1)
{
vtkErrorMacro("Only single component arrays may be used for vtkCellArray "
"storage.");
return;
}
this->Storage.Use64BitStorage();
auto& storage = this->Storage.GetArrays64();
// vtkArrayDownCast to ensure this works when ArrayType64 is vtkIdTypeArray.
storage.Offsets = vtkArrayDownCast<ArrayType64>(offsets);
storage.Connectivity = vtkArrayDownCast<ArrayType64>(connectivity);
this->Modified();
}
//----------------------------------------------------------------------------
void vtkCellArray::SetData(vtkIdTypeArray* offsets, vtkIdTypeArray* connectivity)
{
#ifdef VTK_USE_64BIT_IDS
vtkNew<vtkTypeInt64Array> o;
vtkNew<vtkTypeInt64Array> c;
o->ShallowCopy(offsets);
c->ShallowCopy(connectivity);
this->SetData(o, c);
#else // VTK_USE_64BIT_IDS
vtkNew<vtkTypeInt32Array> o;
vtkNew<vtkTypeInt32Array> c;
o->ShallowCopy(offsets);
c->ShallowCopy(connectivity);
this->SetData(o, c);
#endif // VTK_USE_64BIT_IDS
}
//----------------------------------------------------------------------------
void vtkCellArray::SetData(
vtkAOSDataArrayTemplate<int>* offsets, vtkAOSDataArrayTemplate<int>* connectivity)
{
#if VTK_SIZEOF_INT == 4
vtkNew<vtkTypeInt32Array> o;
vtkNew<vtkTypeInt32Array> c;
o->ShallowCopy(offsets);
c->ShallowCopy(connectivity);
this->SetData(o, c);
#elif VTK_SIZEOF_INT == 8
vtkNew<vtkTypeInt64Array> o;
vtkNew<vtkTypeInt64Array> c;
o->ShallowCopy(offsets);
c->ShallowCopy(connectivity);
this->SetData(o, c);
#else
vtkErrorMacro("`int` type is neither 32 nor 64 bits.");
#endif
}
//----------------------------------------------------------------------------
void vtkCellArray::SetData(
vtkAOSDataArrayTemplate<long>* offsets, vtkAOSDataArrayTemplate<long>* connectivity)
{
#if VTK_SIZEOF_LONG == 4
vtkNew<vtkTypeInt32Array> o;
vtkNew<vtkTypeInt32Array> c;
o->ShallowCopy(offsets);
c->ShallowCopy(connectivity);
this->SetData(o, c);
#elif VTK_SIZEOF_LONG == 8
vtkNew<vtkTypeInt64Array> o;
vtkNew<vtkTypeInt64Array> c;
o->ShallowCopy(offsets);
c->ShallowCopy(connectivity);
this->SetData(o, c);
#else
vtkErrorMacro("`long` type is neither 32 nor 64 bits.");
#endif
}
//----------------------------------------------------------------------------
void vtkCellArray::SetData(
vtkAOSDataArrayTemplate<long long>* offsets, vtkAOSDataArrayTemplate<long long>* connectivity)
{
#if VTK_SIZEOF_LONG_LONG == 4
vtkNew<vtkTypeInt32Array> o;
vtkNew<vtkTypeInt32Array> c;
o->ShallowCopy(offsets);
c->ShallowCopy(connectivity);
this->SetData(o, c);
#elif VTK_SIZEOF_LONG_LONG == 8
vtkNew<vtkTypeInt64Array> o;
vtkNew<vtkTypeInt64Array> c;
o->ShallowCopy(offsets);
c->ShallowCopy(connectivity);
this->SetData(o, c);
#else
vtkErrorMacro("`long long` type is neither 32 nor 64 bits.");
#endif
}
namespace
{
struct SetDataGenericImpl
{
vtkCellArray* CellArray;
vtkDataArray* ConnDA;
bool ArraysMatch;
template <typename ArrayT>
void operator()(ArrayT* offsets)
{
ArrayT* conn = vtkArrayDownCast<ArrayT>(this->ConnDA);
if (!conn)
{
this->ArraysMatch = false;
return;
}
this->ArraysMatch = true;
this->CellArray->SetData(offsets, conn);
}
};
} // end anon namespace
//----------------------------------------------------------------------------
bool vtkCellArray::SetData(vtkDataArray* offsets, vtkDataArray* connectivity)
{
SetDataGenericImpl worker{ this, connectivity, false };
using SupportedArrays = vtkCellArray::InputArrayList;
using Dispatch = vtkArrayDispatch::DispatchByArray<SupportedArrays>;
if (!Dispatch::Execute(offsets, worker))
{
vtkErrorMacro("Invalid array types passed to SetData: "
<< "offsets=" << offsets->GetClassName() << ", "
<< "connectivity=" << connectivity->GetClassName());
return false;
}
if (!worker.ArraysMatch)
{
vtkErrorMacro("Offsets and Connectivity arrays must have the same type.");
return false;
}
return true;
}
//----------------------------------------------------------------------------
void vtkCellArray::Use32BitStorage()
{
if (!this->Storage.Is64Bit())
{
this->Initialize();
return;
}
this->Storage.Use32BitStorage();
}
//----------------------------------------------------------------------------
void vtkCellArray::Use64BitStorage()
{
if (this->Storage.Is64Bit())
{
this->Initialize();
return;
}
this->Storage.Use64BitStorage();
}
//----------------------------------------------------------------------------
void vtkCellArray::UseDefaultStorage()
{
#ifdef VTK_USE_64BIT_IDS
this->Use64BitStorage();
#else // VTK_USE_64BIT_IDS
this->Use32BitStorage();
#endif // VTK_USE_64BIT_IDS
}
//----------------------------------------------------------------------------
bool vtkCellArray::CanConvertTo32BitStorage() const
{
if (!this->Storage.Is64Bit())
{
return true;
}
return this->Visit(CanConvert<ArrayType32::ValueType>{});
}
//----------------------------------------------------------------------------
bool vtkCellArray::CanConvertTo64BitStorage() const
{
return true;
}
//----------------------------------------------------------------------------
bool vtkCellArray::CanConvertToDefaultStorage() const
{
#ifdef VTK_USE_64BIT_IDS
return this->CanConvertTo64BitStorage();
#else // VTK_USE_64BIT_IDS
return this->CanConvertTo32BitStorage();
#endif // VTK_USE_64BIT_IDS
}
//----------------------------------------------------------------------------
bool vtkCellArray::ConvertTo32BitStorage()
{
if (!this->IsStorage64Bit())
{
return true;
}
vtkNew<ArrayType32> offsets;
vtkNew<ArrayType32> conn;
if (!this->Visit(ExtractAndInitialize{}, offsets.Get(), conn.Get()))
{
return false;
}
this->SetData(offsets, conn);
return true;
}
//----------------------------------------------------------------------------
bool vtkCellArray::ConvertTo64BitStorage()
{
if (this->IsStorage64Bit())
{
return true;
}
vtkNew<ArrayType64> offsets;
vtkNew<ArrayType64> conn;
if (!this->Visit(ExtractAndInitialize{}, offsets.Get(), conn.Get()))
{
return false;
}
this->SetData(offsets, conn);
return true;
}
//----------------------------------------------------------------------------
bool vtkCellArray::ConvertToDefaultStorage()
{
#ifdef VTK_USE_64BIT_IDS
return this->ConvertTo64BitStorage();
#else // VTK_USE_64BIT_IDS
return this->ConvertTo32BitStorage();
#endif // VTK_USE_64BIT_IDS
}
//----------------------------------------------------------------------------
bool vtkCellArray::ConvertToSmallestStorage()
{
if (this->IsStorage64Bit() && this->CanConvertTo32BitStorage())
{
return this->ConvertTo32BitStorage();
}
// Already at the smallest possible.
return true;
}
//----------------------------------------------------------------------------
bool vtkCellArray::AllocateExact(vtkIdType numCells, vtkIdType connectivitySize)
{
return this->Visit(AllocateExactImpl{}, numCells, connectivitySize);
}
//----------------------------------------------------------------------------
bool vtkCellArray::ResizeExact(vtkIdType numCells, vtkIdType connectivitySize)
{
return this->Visit(ResizeExactImpl{}, numCells, connectivitySize);
}
//----------------------------------------------------------------------------
// Returns the size of the largest cell. The size is the number of points
// defining the cell.
int vtkCellArray::GetMaxCellSize()
{
FindMaxCell finder{ this };
// Grain size puts an even number of pages into each instance.
vtkSMPTools::For(0, this->GetNumberOfCells(), finder);
return static_cast<int>(finder.Result);
}
//----------------------------------------------------------------------------
unsigned long vtkCellArray::GetActualMemorySize() const
{
return this->Visit(GetActualMemorySizeImpl{});
}
//----------------------------------------------------------------------------
void vtkCellArray::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "StorageIs64Bit: " << this->Storage.Is64Bit() << "\n";
PrintSelfImpl functor;
this->Visit(functor, os, indent);
}
//----------------------------------------------------------------------------
void vtkCellArray::PrintDebug(std::ostream& os)
{
this->Print(os);
this->Visit(PrintDebugImpl{}, os);
}
//----------------------------------------------------------------------------
vtkIdType vtkCellArray::GetTraversalCellId()
{
return this->TraversalCellId;
}
//----------------------------------------------------------------------------
void vtkCellArray::SetTraversalCellId(vtkIdType cellId)
{
this->TraversalCellId = cellId;
}
//----------------------------------------------------------------------------
void vtkCellArray::ReverseCellAtId(vtkIdType cellId)
{
this->Visit(ReverseCellAtIdImpl{}, cellId);
}
//----------------------------------------------------------------------------
void vtkCellArray::ReplaceCellAtId(vtkIdType cellId, vtkIdList* list)
{
this->Visit(ReplaceCellAtIdImpl{}, cellId, list->GetNumberOfIds(), list->GetPointer(0));
}
//----------------------------------------------------------------------------
void vtkCellArray::ReplaceCellAtId(
vtkIdType cellId, vtkIdType cellSize, const vtkIdType cellPoints[])
{
this->Visit(ReplaceCellAtIdImpl{}, cellId, cellSize, cellPoints);
}
//----------------------------------------------------------------------------
void vtkCellArray::ExportLegacyFormat(vtkIdTypeArray* data)
{
data->Allocate(this->Visit(GetLegacyDataSizeImpl{}));
auto it = vtk::TakeSmartPointer(this->NewIterator());
vtkIdType cellSize;
const vtkIdType* cellPoints;
for (it->GoToFirstCell(); !it->IsDoneWithTraversal(); it->GoToNextCell())
{
it->GetCurrentCell(cellSize, cellPoints);
data->InsertNextValue(cellSize);
for (vtkIdType i = 0; i < cellSize; ++i)
{
data->InsertNextValue(cellPoints[i]);
}
}
}
//----------------------------------------------------------------------------
void vtkCellArray::ImportLegacyFormat(vtkIdTypeArray* data)
{
this->ImportLegacyFormat(data->GetPointer(0), data->GetNumberOfValues());
}
//----------------------------------------------------------------------------
void vtkCellArray::ImportLegacyFormat(const vtkIdType* data, vtkIdType len)
{
this->Reset();
this->AppendLegacyFormat(data, len, 0);
}
//----------------------------------------------------------------------------
void vtkCellArray::AppendLegacyFormat(vtkIdTypeArray* data, vtkIdType ptOffset)
{
this->AppendLegacyFormat(data->GetPointer(0), data->GetNumberOfValues(), ptOffset);
}
//----------------------------------------------------------------------------
void vtkCellArray::AppendLegacyFormat(const vtkIdType* data, vtkIdType len, vtkIdType ptOffset)
{
this->Visit(AppendLegacyFormatImpl{}, data, len, ptOffset);
}
//----------------------------------------------------------------------------
void vtkCellArray::Squeeze()
{
this->Visit(SqueezeImpl{});
// Just delete the legacy buffer.
this->LegacyData->Initialize();
}
//----------------------------------------------------------------------------
bool vtkCellArray::IsValid()
{
return this->Visit(IsValidImpl{});
}
//----------------------------------------------------------------------------
vtkIdType vtkCellArray::IsHomogeneous()
{
return this->Visit(IsHomogeneousImpl{});
}
|