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 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkDataArray.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 "vtkDataArray.h"
#include "vtkDataArrayPrivate.txx"
#include "vtkBitArray.h"
#include "vtkCharArray.h"
#include "vtkDataArrayIteratorMacro.h"
#include "vtkDoubleArray.h"
#include "vtkFloatArray.h"
#include "vtkInformation.h"
#include "vtkInformationDoubleVectorKey.h"
#include "vtkInformationInformationVectorKey.h"
#include "vtkInformationVector.h"
#include "vtkIdTypeArray.h"
#include "vtkIntArray.h"
#include "vtkIdList.h"
#include "vtkLookupTable.h"
#include "vtkLongArray.h"
#include "vtkMath.h"
#include "vtkShortArray.h"
#include "vtkSignedCharArray.h"
#include "vtkTypedDataArrayIterator.h"
#include "vtkTypeTraits.h"
#include "vtkUnsignedCharArray.h"
#include "vtkUnsignedIntArray.h"
#include "vtkUnsignedLongArray.h"
#include "vtkUnsignedShortArray.h"
#include <algorithm> // for min(), max()
namespace {
//----------------------------------------------------------------------------
template <class InputIterator>
void vtkDeepCopySwitchOnOutput(InputIterator begin, InputIterator end,
vtkDataArray *outputArray)
{
switch (outputArray->GetDataType())
{
vtkDataArrayIteratorMacro(outputArray,
std::copy(begin, end, vtkDABegin));
default:
vtkGenericWarningMacro("Unsupported data type "
<< outputArray->GetDataTypeAsString() << "!");
}
}
//--------------------------------------------------------------------------
template <class T>
inline void vtkDataArrayRoundIfNecessary(double val, T* retVal)
{
val = std::max(val, static_cast<double>(vtkTypeTraits<T>::Min()));
val = std::min(val, static_cast<double>(vtkTypeTraits<T>::Max()));
*retVal = static_cast<T>((val>=0.0)?(val + 0.5):(val - 0.5));
}
//--------------------------------------------------------------------------
VTK_TEMPLATE_SPECIALIZE
inline void vtkDataArrayRoundIfNecessary(double val, double* retVal)
{
*retVal = val;
}
//--------------------------------------------------------------------------
VTK_TEMPLATE_SPECIALIZE
inline void vtkDataArrayRoundIfNecessary(double val, float* retVal)
{
*retVal = static_cast<float>(val);
}
//--------------------------------------------------------------------------
template <class Scalar, class Iterator>
void vtkDataArrayInterpolateTuple(Iterator from, Scalar *to, int numComp,
vtkIdType* ids, vtkIdType numIds, double* weights)
{
for(int i=0; i < numComp; ++i)
{
double c = 0;
for(vtkIdType j=0; j < numIds; ++j)
{
c += weights[j] * static_cast<double>(from[ids[j]*numComp+i]);
}
// Round integer types. Don't round floating point types.
vtkDataArrayRoundIfNecessary(c, to);
++to;
}
}
//----------------------------------------------------------------------------
template <class Scalar, class Iterator>
void vtkDataArrayInterpolateTuple(Iterator from1, Iterator from2, Scalar* to,
int numComp, double t)
{
const double oneMinusT = 1.0 - t;
while (numComp-- > 0)
{
*(to++) = static_cast<Scalar>(oneMinusT * (*(from1++)) + t * (*(from2++)));
}
}
//----------------------------------------------------------------------------
template <class InputIterator, class OutputIterator>
void vtkDataArrayGetTuplesTemplate2(vtkIdType* ids, vtkIdType* idsEnd,
InputIterator inIter,
OutputIterator outIter,
int numComps)
{
InputIterator inPt;
while (ids != idsEnd)
{
inPt = inIter + (*(ids++) * numComps);
outIter = std::copy(inPt, inPt + numComps, outIter);
}
}
//----------------------------------------------------------------------------
template <class InputIterator>
void vtkDataArrayGetTuplesTemplate1(vtkIdType* ids, vtkIdType* idsEnd,
InputIterator inIter,
vtkDataArray *outArray,
int numComps)
{
switch (outArray->GetDataType())
{
vtkDataArrayIteratorMacro(outArray,
vtkDataArrayGetTuplesTemplate2(ids, idsEnd, inIter, vtkDABegin, numComps)
);
default:
vtkGenericWarningMacro("vtkDataArray::GetTuples: "
"Unsupported output type.");
return;
}
}
//----------------------------------------------------------------------------
template <class IT, class OT>
void vtkCopyTuples(IT* input, OT* output, int nComp,
vtkIdType p1, vtkIdType p2)
{
vtkIdType i;
int j;
vtkIdType num=p2-p1+1;
for (i=0; i<num; i++)
{
for (j=0; j<nComp; j++)
{
output[i*nComp+j] = static_cast<OT>(input[(p1+i)*nComp+j]);
}
}
}
//----------------------------------------------------------------------------
template <class IT>
void vtkCopyTuples1(IT* input, vtkDataArray* output,
vtkIdType p1, vtkIdType p2)
{
switch (output->GetDataType())
{
vtkTemplateMacro(vtkCopyTuples( input,
static_cast<VTK_TT *>(output->GetVoidPointer(0)),
output->GetNumberOfComponents(), p1, p2) );
default:
vtkGenericWarningMacro("Sanity check failed: Unsupported data type "
<< output->GetDataType() << ".");
return;
}
}
template<typename InfoType, typename KeyType>
bool hasValidKey(InfoType info, KeyType key,
unsigned long mtime, double range[2] )
{
if ( info->Has( key ) )
{
if ( mtime <= info->GetMTime() )
{
info->Get( key, range );
return true;
}
}
return false;
}
template<typename InfoType, typename KeyType, typename ComponentKeyType>
bool hasValidKey(InfoType info, KeyType key, ComponentKeyType ckey,
unsigned long mtime, double range[2], int comp )
{
if ( info->Has( key ) )
{
if ( mtime <= info->GetMTime() )
{
info->Get( key )->GetInformationObject(comp)->Get( ckey, range );
return true;
}
}
return false;
}
} // end anon namespace
vtkInformationKeyRestrictedMacro(vtkDataArray, COMPONENT_RANGE, DoubleVector, 2);
vtkInformationKeyRestrictedMacro(vtkDataArray, L2_NORM_RANGE, DoubleVector, 2);
//----------------------------------------------------------------------------
// Construct object with default tuple dimension (number of components) of 1.
vtkDataArray::vtkDataArray()
{
this->LookupTable = NULL;
this->Range[0] = 0;
this->Range[1] = 0;
}
//----------------------------------------------------------------------------
vtkDataArray::~vtkDataArray()
{
if ( this->LookupTable )
{
this->LookupTable->Delete();
}
this->SetName(0);
}
//----------------------------------------------------------------------------
void vtkDataArray::DeepCopy(vtkAbstractArray* aa)
{
if ( aa == NULL )
{
return;
}
vtkDataArray *da = vtkDataArray::FastDownCast(aa);
if (da == NULL)
{
vtkErrorMacro(<< "Input array is not a vtkDataArray ("
<< aa->GetClassName() << ")");
return;
}
this->DeepCopy(da);
}
//----------------------------------------------------------------------------
//Normally subclasses will do this when the input and output type of the
//DeepCopy are the same. When they are not the same, then we use the
//templated code below.
void vtkDataArray::DeepCopy(vtkDataArray *da)
{
// Match the behavior of the old AttributeData
if ( da == NULL )
{
return;
}
if ( this != da )
{
this->Superclass::DeepCopy( da ); // copy Information object
vtkIdType numTuples = da->GetNumberOfTuples();
this->NumberOfComponents = da->NumberOfComponents;
this->SetNumberOfTuples(numTuples);
if (numTuples > 0)
{
switch (da->GetDataType())
{
vtkDataArrayIteratorMacro(
da, vtkDeepCopySwitchOnOutput(vtkDABegin, vtkDAEnd, this)
);
case VTK_BIT:
{//bit not supported, using generic double API
for (vtkIdType i=0; i < numTuples; i++)
{
this->SetTuple(i, da->GetTuple(i));
}
break;
}
default:
vtkErrorMacro("Unsupported data type " << da->GetDataType() << "!");
}
}
this->SetLookupTable(0);
if (da->LookupTable)
{
this->LookupTable = da->LookupTable->NewInstance();
this->LookupTable->DeepCopy(da->LookupTable);
}
}
this->Squeeze();
}
//----------------------------------------------------------------------------
// These can be overridden for more efficiency
double vtkDataArray::GetComponent(vtkIdType i, int j)
{
double *tuple=new double[this->NumberOfComponents], c;
this->GetTuple(i,tuple);
c = tuple[j];
delete [] tuple;
return c;
}
//----------------------------------------------------------------------------
void vtkDataArray::SetComponent(vtkIdType i, int j, double c)
{
double *tuple=new double[this->NumberOfComponents];
if ( i < this->GetNumberOfTuples() )
{
this->GetTuple(i,tuple);
}
else
{
for (int k=0; k<this->NumberOfComponents; k++)
{
tuple[k] = 0.0;
}
}
tuple[j] = c;
this->SetTuple(i,tuple);
delete [] tuple;
}
//----------------------------------------------------------------------------
void vtkDataArray::InsertComponent(vtkIdType i, int j, double c)
{
double *tuple=new double[this->NumberOfComponents];
if ( i < this->GetNumberOfTuples() )
{
this->GetTuple(i,tuple);
}
else
{
for (int k=0; k<this->NumberOfComponents; k++)
{
tuple[k] = 0.0;
}
}
tuple[j] = c;
this->InsertTuple(i,tuple);
delete [] tuple;
}
//----------------------------------------------------------------------------
void vtkDataArray::GetData(vtkIdType tupleMin, vtkIdType tupleMax, int compMin,
int compMax, vtkDoubleArray* data)
{
int i;
vtkIdType j;
int numComp=this->GetNumberOfComponents();
double *tuple=new double[numComp];
double *ptr=data->WritePointer(0,(tupleMax-tupleMin+1)*(compMax-compMin+1));
for (j=tupleMin; j <= tupleMax; j++)
{
this->GetTuple(j,tuple);
for (i=compMin; i <= compMax; i++)
{
*ptr++ = tuple[i];
}
}
delete [] tuple;
}
//----------------------------------------------------------------------------
// Interpolate array value from other array value given the
// indices and associated interpolation weights.
// This method assumes that the two arrays are of the same time.
void vtkDataArray::InterpolateTuple(vtkIdType i, vtkIdList *ptIndices,
vtkAbstractArray* source, double* weights)
{
if (this->GetDataType() != source->GetDataType())
{
vtkErrorMacro("Cannot InterpolateValue from array of type "
<< source->GetDataTypeAsString());
return;
}
vtkDataArray* fromData = vtkDataArray::FastDownCast(source);
if (fromData)
{
int numComp = fromData->GetNumberOfComponents();
vtkIdType j, numIds=ptIndices->GetNumberOfIds();
vtkIdType *ids=ptIndices->GetPointer(0);
vtkIdType idx= i*numComp;
double c;
// Note that we must call WriteVoidPointer before GetVoidPointer
// in case WriteVoidPointer reallocates memory and fromData ==
// this. The vtkBitArray implementation doesn't use pointers, so skip
// the resizing in this case.
int dataType = fromData->GetDataType();
void* vto = dataType != VTK_BIT ?
this->WriteVoidPointer(idx, numComp) : 0;
switch (dataType)
{
case VTK_BIT:
{
vtkBitArray *from=static_cast<vtkBitArray *>(fromData);
vtkBitArray *to=static_cast<vtkBitArray *>(this);
for (int k=0; k<numComp; k++)
{
for (c=0, j=0; j<numIds; j++)
{
c += weights[j]*from->GetValue(ids[j]*numComp+k);
}
to->InsertValue(idx+k, static_cast<int>(c));
}
}
break;
vtkDataArrayIteratorMacro(fromData,
vtkDataArrayInterpolateTuple(vtkDABegin,
static_cast<vtkDAValueType*>(vto),
numComp, ids, numIds, weights)
);
default:
vtkErrorMacro("Unsupported data type " << fromData->GetDataType()
<< " during interpolation!");
}
}
}
//----------------------------------------------------------------------------
// Interpolate value from the two values, p1 and p2, and an
// interpolation factor, t. The interpolation factor ranges from (0,1),
// with t=0 located at p1. This method assumes that the three arrays are of
// the same type. p1 is value at index id1 in fromArray1, while, p2 is
// value at index id2 in fromArray2.
void vtkDataArray::InterpolateTuple(vtkIdType i,
vtkIdType id1, vtkAbstractArray* source1,
vtkIdType id2, vtkAbstractArray* source2, double t)
{
int type = this->GetDataType();
if (type != source1->GetDataType() || type != source2->GetDataType())
{
vtkErrorMacro("All arrays to InterpolateValue must be of same type.");
return;
}
int k, numComp = source1->GetNumberOfComponents();
double c;
vtkIdType loc = i * numComp;
switch (type)
{
case VTK_BIT:
{
vtkBitArray *from1 = static_cast<vtkBitArray *>(source1);
vtkBitArray *from2 = static_cast<vtkBitArray *>(source2);
vtkBitArray *to = static_cast<vtkBitArray *>(this);
for (k=0; k<numComp; k++)
{
c = from1->GetValue(id1) + t * (from2->GetValue(id2) - from1->GetValue(id1));
to->InsertValue(loc + k, static_cast<int>(c));
}
}
break;
// Note that we must call WriteVoidPointer before GetVoidPointer/creating
// iterators in case WriteVoidPointer reallocates memory and
// fromData1==this or fromData2==this.
vtkTemplateMacro(
// If either of the source arrays are mapped, use iterators. Otherwise,
// void pointers are safe.
if (source1->HasStandardMemoryLayout() &&
source2->HasStandardMemoryLayout())
{
// Use pointers:
void *vto = this->WriteVoidPointer(loc, numComp);
void *vfrom1 = source1->GetVoidPointer(id1 * numComp);
void *vfrom2 = source2->GetVoidPointer(id2 * numComp);
vtkDataArrayInterpolateTuple<VTK_TT>(static_cast<VTK_TT*>(vfrom1),
static_cast<VTK_TT*>(vfrom2),
static_cast<VTK_TT*>(vto),
numComp, t);
}
else
{
vtkTypedDataArray<VTK_TT> *tfrom1 =
vtkTypedDataArray<VTK_TT>::FastDownCast(source1);
vtkTypedDataArray<VTK_TT> *tfrom2 =
vtkTypedDataArray<VTK_TT>::FastDownCast(source2);
if (!tfrom1 || !tfrom2)
{
vtkErrorMacro(<<"Cannot call this function with non-standard arrays "
"unless all arrays are vtkTypedDataArray subclasses.");
return;
}
VTK_TT *vto = static_cast<VTK_TT*>(
this->WriteVoidPointer(loc, numComp));
vtkDataArrayInterpolateTuple<VTK_TT>(
vtkTypedDataArrayIterator<VTK_TT>(tfrom1, id1 * numComp),
vtkTypedDataArrayIterator<VTK_TT>(tfrom2, id2 * numComp),
vto, numComp, t);
}
);
default:
vtkErrorMacro("Unsupported data type " << type
<< " during interpolation!");
}
}
//----------------------------------------------------------------------------
void vtkDataArray::CreateDefaultLookupTable()
{
if ( this->LookupTable )
{
this->LookupTable->UnRegister(this);
}
this->LookupTable = vtkLookupTable::New();
// make sure it is built
// otherwise problems with InsertScalar trying to map through
// non built lut
this->LookupTable->Build();
}
//----------------------------------------------------------------------------
void vtkDataArray::SetLookupTable(vtkLookupTable* lut)
{
if ( this->LookupTable != lut )
{
if ( this->LookupTable )
{
this->LookupTable->UnRegister(this);
}
this->LookupTable = lut;
if ( this->LookupTable )
{
this->LookupTable->Register(this);
}
this->Modified();
}
}
//----------------------------------------------------------------------------
double* vtkDataArray::GetTupleN(vtkIdType i, int n)
{
int numComp = this->GetNumberOfComponents();
if (numComp != n)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != " << n);
}
return this->GetTuple(i);
}
//----------------------------------------------------------------------------
double vtkDataArray::GetTuple1(vtkIdType i)
{
int numComp = this->GetNumberOfComponents();
if (numComp != 1)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != 1");
}
return *(this->GetTuple(i));
}
//----------------------------------------------------------------------------
double* vtkDataArray::GetTuple2(vtkIdType i)
{
return this->GetTupleN(i, 2);
}
//----------------------------------------------------------------------------
double* vtkDataArray::GetTuple3(vtkIdType i)
{
return this->GetTupleN(i, 3);
}
//----------------------------------------------------------------------------
double* vtkDataArray::GetTuple4(vtkIdType i)
{
return this->GetTupleN(i, 4);
}
//----------------------------------------------------------------------------
double* vtkDataArray::GetTuple6(vtkIdType i)
{
return this->GetTupleN(i, 6);
}
//----------------------------------------------------------------------------
double* vtkDataArray::GetTuple9(vtkIdType i)
{
return this->GetTupleN(i, 9);
}
//----------------------------------------------------------------------------
void vtkDataArray::SetTuple1(vtkIdType i, double value)
{
int numComp = this->GetNumberOfComponents();
if (numComp != 1)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != 1");
}
this->SetTuple(i, &value);
}
//----------------------------------------------------------------------------
void vtkDataArray::SetTuple2(vtkIdType i, double val0, double val1)
{
double tuple[2];
int numComp = this->GetNumberOfComponents();
if (numComp != 2)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != 2");
}
tuple[0] = val0;
tuple[1] = val1;
this->SetTuple(i, tuple);
}
//----------------------------------------------------------------------------
void vtkDataArray::SetTuple3(vtkIdType i, double val0, double val1,
double val2)
{
double tuple[3];
int numComp = this->GetNumberOfComponents();
if (numComp != 3)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != 3");
}
tuple[0] = val0;
tuple[1] = val1;
tuple[2] = val2;
this->SetTuple(i, tuple);
}
//----------------------------------------------------------------------------
void vtkDataArray::SetTuple4(vtkIdType i, double val0, double val1,
double val2, double val3)
{
double tuple[4];
int numComp = this->GetNumberOfComponents();
if (numComp != 4)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != 4");
}
tuple[0] = val0;
tuple[1] = val1;
tuple[2] = val2;
tuple[3] = val3;
this->SetTuple(i, tuple);
}
//----------------------------------------------------------------------------
void vtkDataArray::SetTuple6(vtkIdType i, double val0, double val1,
double val2, double val3,
double val4, double val5)
{
double tuple[6];
int numComp = this->GetNumberOfComponents();
if (numComp != 6)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != 6");
}
tuple[0] = val0;
tuple[1] = val1;
tuple[2] = val2;
tuple[3] = val3;
tuple[4] = val4;
tuple[5] = val5;
this->SetTuple(i, tuple);
}
//----------------------------------------------------------------------------
void vtkDataArray::SetTuple9(vtkIdType i, double val0, double val1,
double val2, double val3, double val4,
double val5, double val6, double val7, double val8)
{
double tuple[9];
int numComp = this->GetNumberOfComponents();
if (numComp != 9)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != 9");
}
tuple[0] = val0;
tuple[1] = val1;
tuple[2] = val2;
tuple[3] = val3;
tuple[4] = val4;
tuple[5] = val5;
tuple[6] = val6;
tuple[7] = val7;
tuple[8] = val8;
this->SetTuple(i, tuple);
}
//----------------------------------------------------------------------------
void vtkDataArray::InsertTuple1(vtkIdType i, double value)
{
int numComp = this->GetNumberOfComponents();
if (numComp != 1)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != 1");
}
this->InsertTuple(i, &value);
}
//----------------------------------------------------------------------------
void vtkDataArray::InsertTuple2(vtkIdType i, double val0, double val1)
{
double tuple[2];
int numComp = this->GetNumberOfComponents();
if (numComp != 2)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != 2");
}
tuple[0] = val0;
tuple[1] = val1;
this->InsertTuple(i, tuple);
}
//----------------------------------------------------------------------------
void vtkDataArray::InsertTuple3(vtkIdType i, double val0, double val1,
double val2)
{
double tuple[3];
int numComp = this->GetNumberOfComponents();
if (numComp != 3)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != 3");
}
tuple[0] = val0;
tuple[1] = val1;
tuple[2] = val2;
this->InsertTuple(i, tuple);
}
//----------------------------------------------------------------------------
void vtkDataArray::InsertTuple4(vtkIdType i, double val0, double val1,
double val2, double val3)
{
double tuple[4];
int numComp = this->GetNumberOfComponents();
if (numComp != 4)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != 4");
}
tuple[0] = val0;
tuple[1] = val1;
tuple[2] = val2;
tuple[3] = val3;
this->InsertTuple(i, tuple);
}
//----------------------------------------------------------------------------
void vtkDataArray::InsertTuple9(vtkIdType i, double val0, double val1,
double val2, double val3, double val4,
double val5, double val6,double val7, double val8)
{
double tuple[9];
int numComp = this->GetNumberOfComponents();
if (numComp != 9)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != 9");
}
tuple[0] = val0;
tuple[1] = val1;
tuple[2] = val2;
tuple[3] = val3;
tuple[4] = val4;
tuple[5] = val5;
tuple[6] = val6;
tuple[7] = val7;
tuple[8] = val8;
this->InsertTuple(i, tuple);
}
//----------------------------------------------------------------------------
void vtkDataArray::InsertNextTuple1(double value)
{
int numComp = this->GetNumberOfComponents();
if (numComp != 1)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != 1");
}
this->InsertNextTuple(&value);
}
//----------------------------------------------------------------------------
void vtkDataArray::InsertNextTuple2(double val0, double val1)
{
double tuple[2];
int numComp = this->GetNumberOfComponents();
if (numComp != 2)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != 2");
}
tuple[0] = val0;
tuple[1] = val1;
this->InsertNextTuple(tuple);
}
//----------------------------------------------------------------------------
void vtkDataArray::InsertNextTuple3(double val0, double val1,
double val2)
{
double tuple[3];
int numComp = this->GetNumberOfComponents();
if (numComp != 3)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != 3");
}
tuple[0] = val0;
tuple[1] = val1;
tuple[2] = val2;
this->InsertNextTuple(tuple);
}
//----------------------------------------------------------------------------
void vtkDataArray::InsertNextTuple4(double val0, double val1,
double val2, double val3)
{
double tuple[4];
int numComp = this->GetNumberOfComponents();
if (numComp != 4)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != 4");
}
tuple[0] = val0;
tuple[1] = val1;
tuple[2] = val2;
tuple[3] = val3;
this->InsertNextTuple(tuple);
}
//----------------------------------------------------------------------------
void vtkDataArray::InsertNextTuple9(double val0, double val1,
double val2, double val3, double val4,
double val5, double val6,double val7,
double val8)
{
double tuple[9];
int numComp = this->GetNumberOfComponents();
if (numComp != 9)
{
vtkErrorMacro("The number of components do not match the number requested: "
<< numComp << " != 9");
}
tuple[0] = val0;
tuple[1] = val1;
tuple[2] = val2;
tuple[3] = val3;
tuple[4] = val4;
tuple[5] = val5;
tuple[6] = val6;
tuple[7] = val7;
tuple[8] = val8;
this->InsertNextTuple(tuple);
}
//----------------------------------------------------------------------------
unsigned long vtkDataArray::GetActualMemorySize()
{
vtkIdType numPrims;
double size;
// The allocated array may be larger than the number of primitives used.
//numPrims = this->GetNumberOfTuples() * this->GetNumberOfComponents();
numPrims = this->GetSize();
size = vtkDataArray::GetDataTypeSize(this->GetDataType());
// kibibytes
return static_cast<unsigned long>(ceil((size*static_cast<double>(numPrims)
)/1024.0));
}
//----------------------------------------------------------------------------
vtkDataArray* vtkDataArray::CreateDataArray(int dataType)
{
vtkAbstractArray* aa = vtkAbstractArray::CreateArray(dataType);
vtkDataArray* da = vtkDataArray::SafeDownCast(aa);
if (!da && aa)
{
// Requested array is not a vtkDataArray. Delete the allocated array.
aa->Delete();
}
return da;
}
//----------------------------------------------------------------------------
void vtkDataArray::GetTuples(vtkIdList *ptIds, vtkAbstractArray *aa)
{
vtkDataArray *outArray = vtkDataArray::FastDownCast(aa);
if (!outArray)
{
vtkWarningMacro("Input is not a vtkDataArray.");
return;
}
if ((outArray->GetNumberOfComponents() != this->GetNumberOfComponents()))
{
vtkWarningMacro("Number of components for input and output do not match");
return;
}
vtkIdType* ids = ptIds->GetPointer(0);
vtkIdType* idsEnd = ptIds->GetPointer(ptIds->GetNumberOfIds());
switch (this->GetDataType())
{
vtkDataArrayIteratorMacro(this,
vtkDataArrayGetTuplesTemplate1(ids, idsEnd, vtkDABegin, outArray,
this->NumberOfComponents)
);
default: // Fallback to the double interface
vtkIdType num=ptIds->GetNumberOfIds();
for (vtkIdType i=0; i<num; i++)
{
outArray->SetTuple(i, this->GetTuple(ptIds->GetId(i)));
}
break;
}
}
//----------------------------------------------------------------------------
void vtkDataArray::GetTuples(vtkIdType p1, vtkIdType p2, vtkAbstractArray *aa)
{
vtkDataArray* da = vtkDataArray::SafeDownCast(aa);
if (!da)
{
vtkWarningMacro("Input is not a vtkDataArray.");
return;
}
if ((da->GetNumberOfComponents() != this->GetNumberOfComponents()))
{
vtkWarningMacro("Number of components for input and output do not match");
return;
}
switch (this->GetDataType())
{
vtkTemplateMacro(vtkCopyTuples1( static_cast<VTK_TT *>(this->GetVoidPointer(0)), da,
p1, p2 ) );
// This is not supported by the template macro.
// Switch to using the double interface.
case VTK_BIT:
{
vtkIdType num=p2-p1+1;
for (vtkIdType i=0; i<num; i++)
{
da->SetTuple(i,this->GetTuple(p1+i));
}
}
break;
default:
vtkErrorMacro("Sanity check failed: Unsupported data type "
<< this->GetDataType() << ".");
return;
}
}
//----------------------------------------------------------------------------
void vtkDataArray::FillComponent(int j, double c)
{
if (j < 0 || j >= this->GetNumberOfComponents())
{
vtkErrorMacro(<< "Specified component " << j << " is not in [0, "
<< this->GetNumberOfComponents() << ")" );
return;
}
vtkIdType i;
for (i = 0; i < this->GetNumberOfTuples(); i++)
{
this->SetComponent(i, j, c);
}
}
//----------------------------------------------------------------------------
void vtkDataArray::CopyComponent(int j, vtkDataArray *from,
int fromComponent)
{
if (this->GetNumberOfTuples() != from->GetNumberOfTuples())
{
vtkErrorMacro(<< "Number of tuples in 'from' ("
<< from->GetNumberOfTuples() << ") and 'to' ("
<< this->GetNumberOfTuples() << ") do not match.");
return;
}
if (j < 0 || j >= this->GetNumberOfComponents())
{
vtkErrorMacro(<< "Specified component " << j << " in 'to' array is not in [0, "
<< this->GetNumberOfComponents() << ")" );
return;
}
if (fromComponent < 0 || fromComponent >= from->GetNumberOfComponents())
{
vtkErrorMacro(<< "Specified component " << fromComponent << " in 'from' array is not in [0, "
<< from->GetNumberOfComponents() << ")" );
return;
}
vtkIdType i;
for (i = 0; i < this->GetNumberOfTuples(); i++)
{
this->SetComponent(i, j, from->GetComponent(i, fromComponent));
}
}
//----------------------------------------------------------------------------
double vtkDataArray::GetMaxNorm()
{
vtkIdType i;
double norm, maxNorm;
int nComponents = this->GetNumberOfComponents();
maxNorm = 0.0;
for (i=0; i<this->GetNumberOfTuples(); i++)
{
norm = vtkMath::Norm(this->GetTuple(i), nComponents);
if ( norm > maxNorm )
{
maxNorm = norm;
}
}
return maxNorm;
}
//----------------------------------------------------------------------------
int vtkDataArray::CopyInformation(vtkInformation* infoFrom, int deep)
{
// Copy everything + give base classes a chance to
// Exclude keys which they don't want copied.
this->Superclass::CopyInformation(infoFrom,deep);
// Remove any keys we own that are not to be copied here.
vtkInformation *myInfo=this->GetInformation();
// Range:
if (myInfo->Has( L2_NORM_RANGE() ))
{
myInfo->Remove( L2_NORM_RANGE() );
}
return 1;
}
//----------------------------------------------------------------------------
void vtkDataArray::ComputeRange(double range[2], int comp)
{
//this method needs a large refactoring to be way easier to read
if ( comp >= this->NumberOfComponents )
{ // Ignore requests for nonexistent components.
return;
}
// If we got component -1 on a vector array, compute vector magnitude.
if (comp < 0 && this->NumberOfComponents == 1)
{
comp = 0;
}
range[0] = vtkTypeTraits<double>::Max();
range[1] = vtkTypeTraits<double>::Min();
vtkInformation* info = this->GetInformation();
vtkInformationDoubleVectorKey* rkey;
if ( comp < 0 )
{
rkey = L2_NORM_RANGE();
//hasValidKey will update range to the cached value if it exists.
if( !hasValidKey(info,rkey,this->GetMTime(),range) )
{
this->ComputeVectorRange(range);
info->Set( rkey, range, 2 );
}
return;
}
else
{
rkey = COMPONENT_RANGE();
//hasValidKey will update range to the cached value if it exists.
if( !hasValidKey(info, PER_COMPONENT(), rkey,
this->GetMTime(), range, comp))
{
double* allCompRanges = new double[this->NumberOfComponents*2];
const bool computed = this->ComputeScalarRange(allCompRanges);
if(computed)
{
//construct the keys and add them to the info object
vtkInformationVector* infoVec = vtkInformationVector::New();
info->Set( PER_COMPONENT(), infoVec );
infoVec->SetNumberOfInformationObjects( this->NumberOfComponents );
for ( int i = 0; i < this->NumberOfComponents; ++i )
{
infoVec->GetInformationObject( i )->Set( rkey,
allCompRanges+(i*2),
2 );
}
infoVec->FastDelete();
//update the range passed in since we have a valid range.
range[0] = allCompRanges[comp*2];
range[1] = allCompRanges[(comp*2)+1];
}
delete[] allCompRanges;
}
}
}
//----------------------------------------------------------------------------
bool vtkDataArray::ComputeScalarRange(double* ranges)
{
bool computed = false;
switch (this->GetDataType())
{
vtkDataArrayIteratorMacro(this,
computed = vtkDataArrayPrivate::DoComputeScalarRange<vtkDAValueType>(
vtkDABegin, vtkDAEnd,
this->GetNumberOfComponents(),
ranges)
);
default:
break;
}
return computed;
}
//-----------------------------------------------------------------------------
bool vtkDataArray::ComputeVectorRange(double range[2])
{
bool computed = false;
switch (this->GetDataType())
{
vtkDataArrayIteratorMacro(this,
computed = vtkDataArrayPrivate::DoComputeVectorRange<vtkDAValueType>(
vtkDABegin, vtkDAEnd,
this->GetNumberOfComponents(),
range)
);
default:
break;
}
return computed;
}
//----------------------------------------------------------------------------
void vtkDataArray::GetDataTypeRange(double range[2])
{
vtkDataArray::GetDataTypeRange(this->GetDataType(), range);
}
//----------------------------------------------------------------------------
double vtkDataArray::GetDataTypeMin()
{
return vtkDataArray::GetDataTypeMin(this->GetDataType());
}
//----------------------------------------------------------------------------
double vtkDataArray::GetDataTypeMax()
{
return vtkDataArray::GetDataTypeMax(this->GetDataType());
}
//----------------------------------------------------------------------------
void vtkDataArray::GetDataTypeRange(int type, double range[2])
{
range[0] = vtkDataArray::GetDataTypeMin(type);
range[1] = vtkDataArray::GetDataTypeMax(type);
}
//----------------------------------------------------------------------------
double vtkDataArray::GetDataTypeMin(int type)
{
switch (type)
{
case VTK_BIT: return static_cast<double>(VTK_BIT_MIN);
case VTK_SIGNED_CHAR: return static_cast<double>(VTK_SIGNED_CHAR_MIN);
case VTK_UNSIGNED_CHAR: return static_cast<double>(VTK_UNSIGNED_CHAR_MIN);
case VTK_CHAR: return static_cast<double>(VTK_CHAR_MIN);
case VTK_UNSIGNED_SHORT: return static_cast<double>(VTK_UNSIGNED_SHORT_MIN);
case VTK_SHORT: return static_cast<double>(VTK_SHORT_MIN);
case VTK_UNSIGNED_INT: return static_cast<double>(VTK_UNSIGNED_INT_MIN);
case VTK_INT: return static_cast<double>(VTK_INT_MIN);
case VTK_UNSIGNED_LONG: return static_cast<double>(VTK_UNSIGNED_LONG_MIN);
case VTK_LONG: return static_cast<double>(VTK_LONG_MIN);
#if defined(VTK_TYPE_USE_LONG_LONG)
case VTK_UNSIGNED_LONG_LONG: return static_cast<double>(VTK_UNSIGNED_LONG_LONG_MIN);
case VTK_LONG_LONG: return static_cast<double>(VTK_LONG_LONG_MIN);
#endif
#if defined(VTK_TYPE_USE___INT64)
case VTK___INT64: return static_cast<double>(VTK___INT64_MIN);
# if defined(VTK_TYPE_CONVERT_UI64_TO_DOUBLE)
case VTK_UNSIGNED___INT64: return static_cast<double>(VTK_UNSIGNED___INT64_MIN);
# endif
#endif
case VTK_FLOAT: return static_cast<double>(VTK_FLOAT_MIN);
case VTK_DOUBLE: return static_cast<double>(VTK_DOUBLE_MIN);
case VTK_ID_TYPE: return static_cast<double>(VTK_ID_MIN);
default: return 0;
}
}
//----------------------------------------------------------------------------
double vtkDataArray::GetDataTypeMax(int type)
{
switch (type)
{
case VTK_BIT: return static_cast<double>(VTK_BIT_MAX);
case VTK_SIGNED_CHAR: return static_cast<double>(VTK_SIGNED_CHAR_MAX);
case VTK_UNSIGNED_CHAR: return static_cast<double>(VTK_UNSIGNED_CHAR_MAX);
case VTK_CHAR: return static_cast<double>(VTK_CHAR_MAX);
case VTK_UNSIGNED_SHORT: return static_cast<double>(VTK_UNSIGNED_SHORT_MAX);
case VTK_SHORT: return static_cast<double>(VTK_SHORT_MAX);
case VTK_UNSIGNED_INT: return static_cast<double>(VTK_UNSIGNED_INT_MAX);
case VTK_INT: return static_cast<double>(VTK_INT_MAX);
case VTK_UNSIGNED_LONG: return static_cast<double>(VTK_UNSIGNED_LONG_MAX);
case VTK_LONG: return static_cast<double>(VTK_LONG_MAX);
#if defined(VTK_TYPE_USE_LONG_LONG)
case VTK_UNSIGNED_LONG_LONG: return static_cast<double>(VTK_UNSIGNED_LONG_LONG_MAX);
case VTK_LONG_LONG: return static_cast<double>(VTK_LONG_LONG_MAX);
#endif
#if defined(VTK_TYPE_USE___INT64)
case VTK___INT64: return static_cast<double>(VTK___INT64_MAX);
# if defined(VTK_TYPE_CONVERT_UI64_TO_DOUBLE)
case VTK_UNSIGNED___INT64: return static_cast<double>(VTK_UNSIGNED___INT64_MAX);
# endif
#endif
case VTK_FLOAT: return static_cast<double>(VTK_FLOAT_MAX);
case VTK_DOUBLE: return static_cast<double>(VTK_DOUBLE_MAX);
case VTK_ID_TYPE: return static_cast<double>(VTK_ID_MAX);
default: return 1;
}
}
//----------------------------------------------------------------------------
void vtkDataArray::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
const char* name = this->GetName();
if (name)
{
os << indent << "Name: " << name << "\n";
}
else
{
os << indent << "Name: (none)\n";
}
os << indent << "Number Of Components: " << this->NumberOfComponents << "\n";
os << indent << "Number Of Tuples: " << this->GetNumberOfTuples() << "\n";
os << indent << "Size: " << this->Size << "\n";
os << indent << "MaxId: " << this->MaxId << "\n";
if ( this->LookupTable )
{
os << indent << "Lookup Table:\n";
this->LookupTable->PrintSelf(os,indent.GetNextIndent());
}
else
{
os << indent << "LookupTable: (none)\n";
}
}
|