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
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkImageMapper3D.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 "vtkImageMapper3D.h"
#include "vtkRenderer.h"
#include "vtkImageSlice.h"
#include "vtkImageData.h"
#include "vtkImageProperty.h"
#include "vtkScalarsToColors.h"
#include "vtkDataArray.h"
#include "vtkMultiThreader.h"
#include "vtkMath.h"
#include "vtkMatrix4x4.h"
#include "vtkPlane.h"
#include "vtkAbstractTransform.h"
#include "vtkExecutive.h"
#include "vtkGarbageCollector.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkGraphicsFactory.h"
#include "vtkTemplateAliasMacro.h"
//----------------------------------------------------------------------------
vtkImageMapper3D::vtkImageMapper3D()
{
// Default color conversion
this->DefaultLookupTable = vtkScalarsToColors::New();
this->DefaultLookupTable->SetVectorModeToRGBColors();
this->Threader = vtkMultiThreader::New();
this->NumberOfThreads = this->Threader->GetNumberOfThreads();
this->Streaming = 0;
this->Border = 0;
this->Background = 0;
this->SlicePlane = vtkPlane::New();
this->SliceFacesCamera = 0;
this->SliceAtFocalPoint = 0;
this->DataToWorldMatrix = vtkMatrix4x4::New();
this->CurrentProp = 0;
this->CurrentRenderer = 0;
this->MatteEnable = true;
this->ColorEnable = true;
this->DepthEnable = true;
this->DataOrigin[0] = 0.0;
this->DataOrigin[1] = 0.0;
this->DataOrigin[2] = 0.0;
this->DataSpacing[0] = 1.0;
this->DataSpacing[1] = 1.0;
this->DataSpacing[2] = 1.0;
this->DataWholeExtent[0] = 0;
this->DataWholeExtent[1] = 0;
this->DataWholeExtent[2] = 0;
this->DataWholeExtent[3] = 0;
this->DataWholeExtent[4] = 0;
this->DataWholeExtent[5] = 0;
}
//----------------------------------------------------------------------------
vtkImageMapper3D::~vtkImageMapper3D()
{
if (this->DefaultLookupTable)
{
this->DefaultLookupTable->Delete();
}
if (this->Threader)
{
this->Threader->Delete();
}
if (this->SlicePlane)
{
this->SlicePlane->Delete();
}
if (this->DataToWorldMatrix)
{
this->DataToWorldMatrix->Delete();
}
}
//----------------------------------------------------------------------------
void vtkImageMapper3D::SetInputData(vtkImageData *input)
{
this->SetInputDataInternal(0, input);
}
//----------------------------------------------------------------------------
vtkImageData *vtkImageMapper3D::GetInput()
{
if (this->GetNumberOfInputConnections(0) < 1)
{
return 0;
}
return vtkImageData::SafeDownCast(
this->GetExecutive()->GetInputData(0, 0));
}
//----------------------------------------------------------------------------
void vtkImageMapper3D::ReleaseGraphicsResources(vtkWindow *)
{
// see subclass for implementation
}
//----------------------------------------------------------------------------
void vtkImageMapper3D::Render(vtkRenderer *, vtkImageSlice *)
{
// see subclass for implementation
}
//----------------------------------------------------------------------------
int vtkImageMapper3D::ProcessRequest(
vtkInformation* request, vtkInformationVector** inputVector,
vtkInformationVector* outputVector)
{
if (request->Has(vtkStreamingDemandDrivenPipeline::REQUEST_INFORMATION()))
{
vtkInformation *inInfo = inputVector[0]->GetInformationObject(0);
inInfo->Get(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
this->DataWholeExtent);
inInfo->Get(vtkDataObject::SPACING(), this->DataSpacing);
inInfo->Get(vtkDataObject::ORIGIN(), this->DataOrigin);
return 1;
}
return this->Superclass::ProcessRequest(request, inputVector, outputVector);
}
//----------------------------------------------------------------------------
void vtkImageMapper3D::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "SlicePlane: " << this->SlicePlane << "\n";
os << indent << "SliceAtFocalPoint: "
<< (this->SliceAtFocalPoint ? "On\n" : "Off\n");
os << indent << "SliceFacesCamera: "
<< (this->SliceFacesCamera ? "On\n" : "Off\n");
os << indent << "Border: " << (this->Border ? "On\n" : "Off\n");
os << indent << "Background: " << (this->Background ? "On\n" : "Off\n");
os << indent << "NumberOfThreads: " << this->NumberOfThreads << "\n";
os << indent << "Streaming: " << (this->Streaming ? "On\n" : "Off\n");
}
//----------------------------------------------------------------------------
vtkDataObject *vtkImageMapper3D::GetDataObjectInput()
{
if (this->GetNumberOfInputConnections(0) < 1)
{
return 0;
}
return this->GetInputDataObject(0, 0);
}
//----------------------------------------------------------------------------
vtkDataSet *vtkImageMapper3D::GetDataSetInput()
{
if (this->GetNumberOfInputConnections(0) < 1)
{
return 0;
}
return vtkDataSet::SafeDownCast(this->GetInputDataObject(0, 0));
}
//----------------------------------------------------------------------------
int vtkImageMapper3D::FillInputPortInformation(
int vtkNotUsed(port), vtkInformation* info)
{
info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkImageData");
return 1;
}
//----------------------------------------------------------------------------
int vtkImageMapper3D::FillOutputPortInformation(
int vtkNotUsed(port), vtkInformation* info)
{
info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkImageData");
return 1;
}
//----------------------------------------------------------------------------
static
vtkRenderer *vtkImageMapper3DFindRenderer(vtkProp *prop, int &count)
{
vtkRenderer *ren = 0;
int n = prop->GetNumberOfConsumers();
for (int i = 0; i < n; i++)
{
vtkObjectBase *o = prop->GetConsumer(i);
vtkProp3D *a = 0;
if ( (ren = vtkRenderer::SafeDownCast(o)) )
{
count++;
}
else if ( (a = vtkProp3D::SafeDownCast(o)) )
{
ren = vtkImageMapper3DFindRenderer(a, count);
}
}
return ren;
}
//----------------------------------------------------------------------------
static
void vtkImageMapper3DComputeMatrix(vtkProp *prop, double mat[16])
{
vtkMatrix4x4 *propmat = prop->GetMatrix();
vtkMatrix4x4::DeepCopy(mat, propmat);
int n = prop->GetNumberOfConsumers();
for (int i = 0; i < n; i++)
{
vtkObjectBase *o = prop->GetConsumer(i);
vtkProp3D *a = 0;
if ( (a = vtkProp3D::SafeDownCast(o)) )
{
vtkImageMapper3DComputeMatrix(a, mat);
if (a->IsA("vtkAssembly") || a->IsA("vtkImageStack"))
{
vtkMatrix4x4::Multiply4x4(mat, *propmat->Element, mat);
}
}
}
}
//----------------------------------------------------------------------------
vtkRenderer *vtkImageMapper3D::GetCurrentRenderer()
{
vtkImageSlice *prop = this->CurrentProp;
vtkRenderer *ren = this->CurrentRenderer;
int count = 0;
if (ren)
{
return ren;
}
if (!prop)
{
return 0;
}
ren = vtkImageMapper3DFindRenderer(prop, count);
if (count > 1)
{
vtkErrorMacro("Cannot follow camera, mapper is associated with"
"multiple renderers");
ren = 0;
}
return ren;
}
//----------------------------------------------------------------------------
vtkMatrix4x4 *vtkImageMapper3D::GetDataToWorldMatrix()
{
vtkProp3D *prop = this->CurrentProp;
if (prop)
{
if (this->CurrentRenderer)
{
this->DataToWorldMatrix->DeepCopy(prop->GetMatrix());
}
else
{
double mat[16];
vtkImageMapper3DComputeMatrix(prop, mat);
this->DataToWorldMatrix->DeepCopy(mat);
}
}
return this->DataToWorldMatrix;
}
//----------------------------------------------------------------------------
// Convert char data without changing format
static void vtkImageMapperCopy(
const unsigned char *inPtr, unsigned char *outPtr, int ncols, int nrows,
int numComp, vtkIdType inIncX, vtkIdType inIncY, vtkIdType outIncY)
{
// loop through the data and copy it for the texture
if (numComp == 1)
{
for (int idy = 0; idy < nrows; idy++)
{
for (int idx = 0; idx < ncols; idx++)
{
*outPtr = *inPtr;
outPtr++;
inPtr += inIncX;
}
outPtr += outIncY;
inPtr += inIncY;
}
}
else if (numComp == 2)
{
for (int idy = 0; idy < nrows; idy++)
{
for (int idx = 0; idx < ncols; idx++)
{
outPtr[0] = inPtr[0];
outPtr[1] = inPtr[1];
outPtr += 2;
inPtr += inIncX;
}
outPtr += outIncY;
inPtr += inIncY;
}
}
else if (numComp == 3)
{
for (int idy = 0; idy < nrows; idy++)
{
for (int idx = 0; idx < ncols; idx++)
{
outPtr[0] = inPtr[0];
outPtr[1] = inPtr[1];
outPtr[2] = inPtr[2];
outPtr += 3;
inPtr += inIncX;
}
outPtr += outIncY;
inPtr += inIncY;
}
}
else // if (numComp == 4)
{
for (int idy = 0; idy < nrows; idy++)
{
for (int idx = 0; idx < ncols; idx++)
{
outPtr[0] = inPtr[0];
outPtr[1] = inPtr[1];
outPtr[2] = inPtr[2];
outPtr[3] = inPtr[3];
outPtr += 4;
inPtr += inIncX;
}
outPtr += outIncY;
inPtr += inIncY;
}
}
}
//----------------------------------------------------------------------------
// Convert char data to RGBA
static
void vtkImageMapperConvertToRGBA(
const unsigned char *inPtr, unsigned char *outPtr, int ncols, int nrows,
int numComp, vtkIdType inIncX, vtkIdType inIncY, vtkIdType outIncY)
{
unsigned char alpha = 255;
// loop through the data and copy it for the texture
if (numComp == 1)
{
for (int idy = 0; idy < nrows; idy++)
{
for (int idx = 0; idx < ncols; idx++)
{
unsigned char val = inPtr[0];
outPtr[0] = val;
outPtr[1] = val;
outPtr[2] = val;
outPtr[3] = alpha;
outPtr += 4;
inPtr += inIncX;
}
outPtr += outIncY;
inPtr += inIncY;
}
}
else if (numComp == 2)
{
for (int idy = 0; idy < nrows; idy++)
{
for (int idx = 0; idx < ncols; idx++)
{
unsigned char val = inPtr[0];
unsigned char a = inPtr[1];
outPtr[0] = val;
outPtr[1] = val;
outPtr[2] = val;
outPtr[3] = a;
outPtr += 4;
inPtr += inIncX;
}
outPtr += outIncY;
inPtr += inIncY;
}
}
else if (numComp == 3)
{
for (int idy = 0; idy < nrows; idy++)
{
for (int idx = 0; idx < ncols; idx++)
{
outPtr[0] = inPtr[0];
outPtr[1] = inPtr[1];
outPtr[2] = inPtr[2];
outPtr[3] = alpha;
outPtr += 4;
inPtr += inIncX;
}
outPtr += outIncY;
inPtr += inIncY;
}
}
else // if (numComp == 4)
{
for (int idy = 0; idy < nrows; idy++)
{
for (int idx = 0; idx < ncols; idx++)
{
outPtr[0] = inPtr[0];
outPtr[1] = inPtr[1];
outPtr[2] = inPtr[2];
outPtr[3] = inPtr[3];
outPtr += 4;
inPtr += inIncX;
}
outPtr += outIncY;
inPtr += inIncY;
}
}
}
//----------------------------------------------------------------------------
// Convert data to unsigned char
template<class F>
inline F vtkImageMapperClamp(F x, F xmin, F xmax)
{
// do not change this code: it compiles into min/max opcodes
x = (x > xmin ? x : xmin);
x = (x < xmax ? x : xmax);
return x;
}
template<class F, class T>
void vtkImageMapperShiftScale(
const T *inPtr, unsigned char *outPtr, int ncols, int nrows,
int numComp, vtkIdType inIncX, vtkIdType inIncY, vtkIdType outIncY,
F shift, F scale)
{
const F vmin = static_cast<F>(0);
const F vmax = static_cast<F>(255);
unsigned char alpha = 255;
// loop through the data and copy it for the texture
if (numComp == 1)
{
for (int idy = 0; idy < nrows; idy++)
{
for (int idx = 0; idx < ncols; idx++)
{
// Pixel operation
F val = (inPtr[0] + shift)*scale;
val = vtkImageMapperClamp(val, vmin, vmax);
unsigned char cval = static_cast<unsigned char>(val + 0.5);
outPtr[0] = cval;
outPtr[1] = cval;
outPtr[2] = cval;
outPtr[3] = alpha;
outPtr += 4;
inPtr += inIncX;
}
outPtr += outIncY;
inPtr += inIncY;
}
}
else if (numComp == 2)
{
for (int idy = 0; idy < nrows; idy++)
{
for (int idx = 0; idx < ncols; idx++)
{
// Pixel operation
F val = (inPtr[0] + shift)*scale;
val = vtkImageMapperClamp(val, vmin, vmax);
unsigned char cval = static_cast<unsigned char>(val + 0.5);
val = (inPtr[1] + shift)*scale;
val = vtkImageMapperClamp(val, vmin, vmax);
unsigned char aval = static_cast<unsigned char>(val + 0.5);
outPtr[0] = cval;
outPtr[1] = cval;
outPtr[2] = cval;
outPtr[3] = aval;
outPtr += 4;
inPtr += inIncX;
}
outPtr += outIncY;
inPtr += inIncY;
}
}
else if (numComp == 3)
{
for (int idy = 0; idy < nrows; idy++)
{
for (int idx = 0; idx < ncols; idx++)
{
// Pixel operation
F r = (inPtr[0] + shift)*scale;
F g = (inPtr[1] + shift)*scale;
F b = (inPtr[2] + shift)*scale;
r = vtkImageMapperClamp(r, vmin, vmax);
g = vtkImageMapperClamp(g, vmin, vmax);
b = vtkImageMapperClamp(b, vmin, vmax);
outPtr[0] = static_cast<unsigned char>(r + 0.5);
outPtr[1] = static_cast<unsigned char>(g + 0.5);
outPtr[2] = static_cast<unsigned char>(b + 0.5);
outPtr[3] = alpha;
outPtr += 4;
inPtr += inIncX;
}
outPtr += outIncY;
inPtr += inIncY;
}
}
else // if (numComp == 4)
{
for (int idy = 0; idy < nrows; idy++)
{
for (int idx = 0; idx < ncols; idx++)
{
// Pixel operation
F r = (inPtr[0] + shift)*scale;
F g = (inPtr[1] + shift)*scale;
F b = (inPtr[2] + shift)*scale;
F a = (inPtr[3] + shift)*scale;
r = vtkImageMapperClamp(r, vmin, vmax);
g = vtkImageMapperClamp(g, vmin, vmax);
b = vtkImageMapperClamp(b, vmin, vmax);
a = vtkImageMapperClamp(a, vmin, vmax);
outPtr[0] = static_cast<unsigned char>(r + 0.5);
outPtr[1] = static_cast<unsigned char>(g + 0.5);
outPtr[2] = static_cast<unsigned char>(b + 0.5);
outPtr[3] = static_cast<unsigned char>(a + 0.5);
outPtr += 4;
inPtr += inIncX;
}
outPtr += outIncY;
inPtr += inIncY;
}
}
}
//----------------------------------------------------------------------------
static void vtkImageMapperConvertImageScalarsToRGBA(
void *inPtr, unsigned char *outPtr, int ncols, int nrows,
int numComp, vtkIdType inIncX, vtkIdType inIncY, vtkIdType outIncY,
int scalarType, double scalarRange[2])
{
double shift = -scalarRange[0];
double scale = 255.0;
if (scalarRange[0] < scalarRange[1])
{
scale /= (scalarRange[1] - scalarRange[0]);
}
else
{
scale = 1e+32;
}
// Check if the data can be simply copied
if (scalarType == VTK_UNSIGNED_CHAR &&
static_cast<int>(shift*scale) == 0 &&
static_cast<int>((255 + shift)*scale) == 255)
{
vtkImageMapperConvertToRGBA(static_cast<unsigned char *>(inPtr),
outPtr, ncols, nrows, numComp,
inIncX, inIncY, outIncY);
}
else
{
switch (scalarType)
{
vtkTemplateAliasMacro(
vtkImageMapperShiftScale(static_cast<VTK_TT*>(inPtr),
outPtr, ncols, nrows, numComp,
inIncX, inIncY, outIncY, shift, scale));
default:
vtkGenericWarningMacro(
"ConvertImageScalarsToRGBA: Unknown input ScalarType");
}
}
}
//----------------------------------------------------------------------------
template<class T>
void vtkImageMapperMakeContiguous(
const T *inPtr, T *outPtr, int ncols, int numComp, vtkIdType inIncX)
{
if (numComp == 1)
{
for (int idx = 0; idx < ncols; idx++)
{
*outPtr = *inPtr;
outPtr++;
inPtr += inIncX;
}
}
else
{
inIncX -= numComp;
for (int idx = 0; idx < ncols; idx++)
{
int idc = numComp;
do { *outPtr++ = *inPtr++; } while (--idc);
inPtr += inIncX;
}
}
}
static void vtkImageMapperApplyLookupTableToImageScalars(
void *inPtr, unsigned char *outPtr, int ncols, int nrows,
int numComp, vtkIdType inIncX, vtkIdType inIncY, vtkIdType outIncY,
int scalarType, vtkScalarsToColors *lookupTable)
{
// number of values per row of input image
int scalarSize = vtkDataArray::GetDataTypeSize(scalarType);
// convert incs from continuous increments to regular increment
outIncY += 4*ncols;
inIncY += inIncX*ncols;
inIncY *= scalarSize;
// if data not contiguous, make a temporary array
void *newPtr = 0;
if (inIncX > static_cast<vtkIdType>(numComp))
{
newPtr = malloc(scalarSize*numComp*ncols);
}
// loop through the data and copy it for the texture
for (int idy = 0; idy < nrows; idy++)
{
void *tmpPtr = inPtr;
// make contiguous if necessary
if (inIncX > static_cast<vtkIdType>(numComp))
{
tmpPtr = newPtr;
if (scalarSize == 1)
{
vtkImageMapperMakeContiguous(
static_cast<char *>(inPtr), static_cast<char *>(tmpPtr),
ncols, numComp, inIncX);
}
else if (scalarSize == 2)
{
vtkImageMapperMakeContiguous(
static_cast<short *>(inPtr), static_cast<short *>(tmpPtr),
ncols, numComp, inIncX);
}
else if (scalarSize == 4)
{
vtkImageMapperMakeContiguous(
static_cast<float *>(inPtr), static_cast<float *>(tmpPtr),
ncols, numComp, inIncX);
}
else
{
vtkImageMapperMakeContiguous(
static_cast<double *>(inPtr), static_cast<double *>(tmpPtr),
ncols, numComp*(scalarSize >> 3), inIncX*(scalarSize >> 3));
}
}
// pass the data through the lookup table
if (numComp == 1)
{
lookupTable->MapScalarsThroughTable(
tmpPtr, outPtr, scalarType, ncols, numComp, VTK_RGBA);
}
else
{
lookupTable->MapVectorsThroughTable(
tmpPtr, outPtr, scalarType, ncols, numComp, VTK_RGBA);
}
outPtr += outIncY;
inPtr = static_cast<void *>(static_cast<char *>(inPtr) + inIncY);
}
if (newPtr)
{
free(newPtr);
}
}
//----------------------------------------------------------------------------
struct vtkImageMapperThreadStruct
{
void *InputPtr;
unsigned char *OutputPtr;
int ImageSize[2];
int ScalarType;
int NumComp;
vtkIdType InIncX;
vtkIdType InIncY;
vtkIdType OutIncX;
vtkIdType OutIncY;
double Range[2];
vtkScalarsToColors *LookupTable;
};
static VTK_THREAD_RETURN_TYPE vtkImageMapperMapColors(void *arg)
{
vtkMultiThreader::ThreadInfo *mtti =
static_cast<vtkMultiThreader::ThreadInfo *>(arg);
int threadId = mtti->ThreadID;
int threadCount = mtti->NumberOfThreads;
vtkImageMapperThreadStruct *imts =
static_cast<vtkImageMapperThreadStruct *>(mtti->UserData);
int ncols = imts->ImageSize[0];
int nrows = imts->ImageSize[1];
int scalarSize = vtkDataArray::GetDataTypeSize(imts->ScalarType);
// only split in vertical direction
if (threadCount > nrows)
{
threadCount = nrows;
if (threadId >= threadCount)
{
return VTK_THREAD_RETURN_VALUE;
}
}
// adjust pointers
int firstRow = threadId*nrows/threadCount;
int lastRow = (threadId+1)*nrows/threadCount;
void *inputPtr = static_cast<char *>(imts->InputPtr) +
(imts->InIncX*ncols + imts->InIncY)*firstRow*scalarSize;
unsigned char *outputPtr = imts->OutputPtr +
(imts->OutIncX*ncols + imts->OutIncY)*firstRow;
nrows = lastRow - firstRow;
// reformat the data for use as a texture
if (imts->LookupTable)
{
// apply a lookup table
vtkImageMapperApplyLookupTableToImageScalars(
inputPtr, outputPtr, ncols, nrows,
imts->NumComp, imts->InIncX, imts->InIncY, imts->OutIncY,
imts->ScalarType, imts->LookupTable);
}
else
{
// no lookup table, do a shift/scale calculation
vtkImageMapperConvertImageScalarsToRGBA(
inputPtr, outputPtr, ncols, nrows,
imts->NumComp, imts->InIncX, imts->InIncY, imts->OutIncY,
imts->ScalarType, imts->Range);
}
return VTK_THREAD_RETURN_VALUE;
}
//----------------------------------------------------------------------------
// Given an image and an extent that describes a single slice, this method
// will return a contiguous block of unsigned char data that can be loaded
// into a texture.
// The values of xsize, ysize, bytesPerPixel, and reuseTexture must be
// pre-loaded with the current texture size and depth, with subTexture
// set to 1 if only a subTexture is to be generated.
// When the method returns, these values will be set to the dimensions
// of the data that was produced, and subTexture will remain set to 1
// if xsize,ysize describe a subtexture size.
// If subTexture is not set to one upon return, then xsize,ysize will
// describe the full texture size, with the assumption that the full
// texture must be reloaded.
// If reuseData is false upon return, then the returned array must be
// freed after use with delete [].
unsigned char *vtkImageMapper3D::MakeTextureData(
vtkImageProperty *property, vtkImageData *input, int extent[6],
int &xsize, int &ysize, int &bytesPerPixel, bool &reuseTexture,
bool &reuseData)
{
int xdim, ydim;
int imageSize[2];
int textureSize[2];
// compute image size and texture size from extent
this->ComputeTextureSize(
extent, xdim, ydim, imageSize, textureSize);
// number of components
int numComp = input->GetNumberOfScalarComponents();
int scalarType = input->GetScalarType();
int textureBytesPerPixel = 4;
// lookup table and window/level
double colorWindow = 255.0;
double colorLevel = 127.5;
vtkScalarsToColors *lookupTable = 0;
if (property)
{
colorWindow = property->GetColorWindow();
colorLevel = property->GetColorLevel();
lookupTable = property->GetLookupTable();
}
// check if the input is pre-formatted as colors
int inputIsColors = false;
if (lookupTable == 0 && scalarType == VTK_UNSIGNED_CHAR &&
colorLevel == 127.5 && colorWindow == 255.0)
{
inputIsColors = true;
if (reuseData && numComp < 4)
{
textureBytesPerPixel = numComp;
}
}
// reuse texture if texture size has not changed
if (xsize == textureSize[0] && ysize == textureSize[1] &&
bytesPerPixel == textureBytesPerPixel && reuseTexture)
{
// if texture is reused, only reload the image portion
xsize = imageSize[0];
ysize = imageSize[1];
}
else
{
xsize = textureSize[0];
ysize = textureSize[1];
bytesPerPixel = textureBytesPerPixel;
reuseTexture = false;
}
// if the image is already of the desired size and type
if (xsize == imageSize[0] && ysize == imageSize[1])
{
// Check if the data needed for the texture is a contiguous region
// of the input data: this requires that xdim = 0 and ydim = 1
// OR xextent = 1 pixel and xdim = 1 and ydim = 2
// OR xdim = 0 and ydim = 2 and yextent = 1 pixel.
// In addition the corresponding x display extents must match the
// extent of the data
int *dataExtent = input->GetExtent();
if ( (xdim == 0 && ydim == 1 &&
extent[0] == dataExtent[0] && extent[1] == dataExtent[1]) ||
(xdim == 1 && ydim == 2 && dataExtent[0] == dataExtent[1] &&
extent[2] == dataExtent[2] && extent[3] == dataExtent[3]) ||
(xdim == 0 && ydim == 2 && dataExtent[2] == dataExtent[3] &&
extent[0] == dataExtent[0] && extent[1] == dataExtent[1]) )
{
// if contiguous and correct data type, use data as-is
if (inputIsColors && reuseData)
{
return static_cast<unsigned char *>(
input->GetScalarPointerForExtent(extent));
}
}
}
// could not directly use input data, so allocate a new array
reuseData = false;
unsigned char *outPtr = new unsigned char [ysize*xsize*bytesPerPixel];
// output increments
vtkIdType outIncY = bytesPerPixel*(xsize - imageSize[0]);
// input pointer and increments
vtkIdType inInc[3];
void *inPtr = input->GetScalarPointerForExtent(extent);
input->GetIncrements(inInc);
vtkIdType inIncX = inInc[xdim];
vtkIdType inIncY = inInc[ydim] - inInc[xdim]*imageSize[0];
// convert Window/Level to a scalar range
double range[2];
range[0] = colorLevel - 0.5*colorWindow;
range[1] = colorLevel + 0.5*colorWindow;
if (lookupTable && property && !property->GetUseLookupTableScalarRange())
{
// no way to do this without modifying the table
lookupTable->SetRange(range);
}
if (inputIsColors && !lookupTable)
{
// just copy the data
vtkImageMapperCopy(static_cast<unsigned char *>(inPtr), outPtr,
imageSize[0], imageSize[1], numComp,
inIncX, inIncY, outIncY);
}
else
{
// do a multi-threaded conversion
vtkImageMapperThreadStruct imts;
imts.InputPtr = inPtr;
imts.OutputPtr = outPtr;
imts.ImageSize[0] = imageSize[0];
imts.ImageSize[1] = imageSize[1];
imts.ScalarType = scalarType;
imts.NumComp = numComp;
imts.InIncX = inIncX;
imts.InIncY = inIncY;
imts.OutIncX = 4;
imts.OutIncY = outIncY;
imts.Range[0] = range[0];
imts.Range[1] = range[1];
imts.LookupTable = lookupTable;
int numThreads = this->NumberOfThreads;
numThreads = ((numThreads <= imageSize[1]) ? numThreads : imageSize[1]);
this->Threader->SetNumberOfThreads(numThreads);
this->Threader->SetSingleMethod(&vtkImageMapperMapColors, &imts);
this->Threader->SingleMethodExecute();
}
return outPtr;
}
//----------------------------------------------------------------------------
// Compute the coords and tcoords for the image
void vtkImageMapper3D::MakeTextureGeometry(
const int extent[6], double coords[12], double tcoords[8])
{
int xdim, ydim;
int imageSize[2];
int textureSize[2];
// compute image size and texture size from extent
this->ComputeTextureSize(
extent, xdim, ydim, imageSize, textureSize);
// get spacing/origin for the quad coordinates
double *spacing = this->DataSpacing;
double *origin = this->DataOrigin;
int border = this->Border;
// compute the world coordinates of the quad
coords[0] = extent[0]*spacing[0] + origin[0];
coords[1] = extent[2]*spacing[1] + origin[1];
coords[2] = extent[4]*spacing[2] + origin[2];
coords[3] = extent[1]*spacing[0] + origin[0];
coords[4] = extent[2 + (xdim == 1)]*spacing[1] + origin[1];
coords[5] = extent[4]*spacing[2] + origin[2];
coords[6] = extent[1]*spacing[0] + origin[0];
coords[7] = extent[3]*spacing[1] + origin[1];
coords[8] = extent[5]*spacing[2] + origin[2];
coords[9] = extent[0]*spacing[0] + origin[0];
coords[10] = extent[2 + (ydim == 1)]*spacing[1] + origin[1];
coords[11] = extent[5]*spacing[2] + origin[2];
// stretch the geometry one half-pixel
if (border)
{
coords[xdim] -= 0.5*spacing[xdim];
coords[ydim] -= 0.5*spacing[ydim];
coords[3 + xdim] += 0.5*spacing[xdim];
coords[3 + ydim] -= 0.5*spacing[ydim];
coords[6 + xdim] += 0.5*spacing[xdim];
coords[6 + ydim] += 0.5*spacing[ydim];
coords[9 + xdim] -= 0.5*spacing[xdim];
coords[9 + ydim] += 0.5*spacing[ydim];
}
if (tcoords)
{
// compute the tcoords
double textureBorder = 0.5*(border == 0);
tcoords[0] = textureBorder/textureSize[0];
tcoords[1] = textureBorder/textureSize[1];
tcoords[2] = (imageSize[0] - textureBorder)/textureSize[0];
tcoords[3] = tcoords[1];
tcoords[4] = tcoords[2];
tcoords[5] = (imageSize[1] - textureBorder)/textureSize[1];
tcoords[6] = tcoords[0];
tcoords[7] = tcoords[5];
}
}
//----------------------------------------------------------------------------
void vtkImageMapper3D::ComputeTextureSize(
const int extent[6], int &xdim, int &ydim,
int imageSize[2], int textureSize[2])
{
// find dimension indices that will correspond to the
// columns and rows of the 2D texture
xdim = 1;
ydim = 2;
if (extent[0] != extent[1])
{
xdim = 0;
if (extent[2] != extent[3])
{
ydim = 1;
}
}
// compute the image dimensions
imageSize[0] = (extent[xdim*2+1] - extent[xdim*2] + 1);
imageSize[1] = (extent[ydim*2+1] - extent[ydim*2] + 1);
textureSize[0] = imageSize[0];
textureSize[1] = imageSize[1];
}
//----------------------------------------------------------------------------
void vtkImageMapper3D::GetSlicePlaneInDataCoords(
vtkMatrix4x4 *propMatrix, double normal[4])
{
double point[3];
this->SlicePlane->GetNormal(normal);
this->SlicePlane->GetOrigin(point);
// The plane has a transform, though most people forget
vtkAbstractTransform *planeTransform = this->SlicePlane->GetTransform();
if (planeTransform)
{
planeTransform->TransformNormalAtPoint(point, normal, normal);
planeTransform->TransformPoint(point, point);
}
// Convert to a homogeneous normal in data coords
normal[3] = -vtkMath::Dot(point, normal);
// Transform to data coordinates
if (propMatrix)
{
double mat[16];
vtkMatrix4x4::Transpose(*propMatrix->Element, mat);
vtkMatrix4x4::MultiplyPoint(mat, normal, normal);
}
// Normalize the "normal" part for good measure
double l = vtkMath::Norm(normal);
normal[0] /= l;
normal[1] /= l;
normal[2] /= l;
normal[3] /= l;
}
//----------------------------------------------------------------------------
void vtkImageMapper3D::GetBackgroundColor(
vtkImageProperty *property, double color[4])
{
color[0] = 0.0;
color[1] = 0.0;
color[2] = 0.0;
color[3] = 1.0;
if (property)
{
vtkScalarsToColors *table = property->GetLookupTable();
if (table)
{
double v = property->GetColorLevel() - 0.5*property->GetColorWindow();
if (property->GetUseLookupTableScalarRange())
{
v = table->GetRange()[0];
}
table->GetColor(v, color);
color[3] = table->GetOpacity(v);
}
}
}
//----------------------------------------------------------------------------
void vtkImageMapper3D::CheckerboardRGBA(
unsigned char *data, int xsize, int ysize,
double originx, double originy, double spacingx, double spacingy)
{
static double tol = 7.62939453125e-06;
static double maxval = 2147483647;
static double minval = -2147483647;
originx += 1.0 + tol;
originy += 1.0 + tol;
originx = (originx > minval ? originx : minval);
originx = (originx < maxval ? originx : maxval);
originy = (originy > minval ? originy : minval);
originy = (originy < maxval ? originy : maxval);
spacingx = fabs(spacingx);
spacingy = fabs(spacingy);
spacingx = (spacingx < maxval ? spacingx : maxval);
spacingy = (spacingy < maxval ? spacingy : maxval);
spacingx = (spacingx != 0 ? spacingx : maxval);
spacingy = (spacingy != 0 ? spacingy : maxval);
int xn = static_cast<int>(spacingx + tol);
int yn = static_cast<int>(spacingy + tol);
double fx = spacingx - xn;
double fy = spacingy - yn;
int state = 0;
int tmpstate = ~state;
double spacing2x = 2*spacingx;
double spacing2y = 2*spacingy;
originx -= ceil(originx/spacing2x)*spacing2x;
while (originx < 0) { originx += spacing2x; }
originy -= ceil(originy/spacing2y)*spacing2y;
while (originy < 0) { originy += spacing2y; }
double tmporiginx = originx - spacingx;
originx = (tmporiginx < 0 ? originx : tmporiginx);
state = (tmporiginx < 0 ? state : tmpstate);
tmpstate = ~state;
double tmporiginy = originy - spacingy;
originy = (tmporiginy < 0 ? originy : tmporiginy);
state = (tmporiginy < 0 ? state : tmpstate);
int xm = static_cast<int>(originx);
int savexm = xm;
int ym = static_cast<int>(originy);
double gx = originx - xm;
double savegx = gx;
double gy = originy - ym;
int inc = 4;
data += (inc - 1);
for (int j = 0; j < ysize;)
{
double tmpy = gy - 1.0;
gy = (tmpy < 0 ? gy : tmpy);
int yextra = (tmpy >= 0);
ym += yextra;
int ry = ysize - j;
ym = (ym < ry ? ym : ry);
j += ym;
for (; ym; --ym)
{
tmpstate = state;
xm = savexm;
gx = savegx;
for (int i = 0; i < xsize;)
{
double tmpx = gx - 1.0;
gx = (tmpx < 0 ? gx : tmpx);
int xextra = (tmpx >= 0);
xm += xextra;
int rx = xsize - i;
xm = (xm < rx ? xm : rx);
i += xm;
if ( (tmpstate & xm) )
{
do
{
*data = 0;
data += inc;
}
while (--xm);
}
data += inc*xm;
xm = xn;
tmpstate = ~tmpstate;
gx += fx;
}
}
ym = yn;
state = ~state;
gy += fy;
}
}
|