/usr/include/ITK-4.5/itkVTKPolyDataMeshIO.h is in libinsighttoolkit4-dev 4.5.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef __itkVTKPolyDataMeshIO_h
#define __itkVTKPolyDataMeshIO_h
#include "ITKIOMeshExport.h"
#include "itkByteSwapper.h"
#include "itkMetaDataObject.h"
#include "itkMeshIOBase.h"
#include "itkVectorContainer.h"
#include "itkNumberToString.h"
#include <fstream>
#include <vector>
namespace itk
{
/** \class VTKPolyDataMeshIO
* \brief This class defines how to read and write vtk legacy file format.
*
* \author Wanlin Zhu. Uviversity of New South Wales, Australia.
* \ingroup IOFilters
* \ingroup ITKIOMesh
*/
class ITKIOMesh_EXPORT VTKPolyDataMeshIO:public MeshIOBase
{
public:
/** Standard "Self" typedef. */
typedef VTKPolyDataMeshIO Self;
typedef MeshIOBase Superclass;
typedef SmartPointer< Self > Pointer;
typedef SmartPointer< const Self > ConstPointer;
typedef Superclass::SizeValueType SizeValueType;
typedef std::string StringType;
typedef std::vector< StringType > StringVectorType;
typedef std::stringstream StringStreamType;
typedef std::vector< SizeValueType > PointIdVector;
typedef VectorContainer< SizeValueType, PointIdVector > PolylinesContainerType;
typedef PolylinesContainerType::Pointer PolylinesContainerPointer;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro(VTKPolyDataMeshIO, MeshIOBase);
/**-------- This part of the interfaces deals with reading data. ----- */
/** Determine if the file can be read with this MeshIO implementation.
* \param FileNameToRead The name of the file to test for reading.
* \post Sets classes MeshIOBase::m_FileName variable to be FileNameToWrite
* \return Returns true if this MeshIO can read the file specified.
*/
virtual bool CanReadFile(const char *FileNameToRead);
/** Set the spacing and dimension information for the set filename. */
virtual void ReadMeshInformation();
/** Reads the data from disk into the memory buffer provided. */
virtual void ReadPoints(void *buffer);
virtual void ReadCells(void *buffer);
virtual void ReadPointData(void *buffer);
virtual void ReadCellData(void *buffer);
/*-------- This part of the interfaces deals with writing data. ----- */
/** Determine if the file can be written with this MeshIO implementation.
* \param FileNameToWrite The name of the file to test for writing.
* \post Sets classes MeshIOBase::m_FileName variable to be FileNameToWrite
* \return Returns true if this MeshIO can write the file specified.
*/
virtual bool CanWriteFile(const char *FileNameToWrite);
/** Set the spacing and dimension information for the set filename. */
virtual void WriteMeshInformation();
/** Writes the data to disk from the memory buffer provided. Make sure
* that the IORegions has been set properly. */
virtual void WritePoints(void *buffer);
virtual void WriteCells(void *buffer);
virtual void WritePointData(void *buffer);
virtual void WriteCellData(void *buffer);
virtual void Write();
protected:
VTKPolyDataMeshIO();
virtual ~VTKPolyDataMeshIO() {}
void PrintSelf(std::ostream & os, Indent indent) const;
template< typename T >
void UpdateCellInformation(T *buffer)
{
unsigned int numberOfVertices = 0;
unsigned int numberOfVertexIndices = 0;
unsigned int numberOfLines = 0;
unsigned int numberOfLineIndices = 0;
unsigned int numberOfPolygons = 0;
unsigned int numberOfPolygonIndices = 0;
SizeValueType index = 0;
for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
{
MeshIOBase::CellGeometryType cellType = static_cast< MeshIOBase::CellGeometryType >( static_cast< int >( buffer[index++] ) );
unsigned int nn = static_cast< unsigned int >( buffer[index++] );
switch ( cellType )
{
case VERTEX_CELL:
numberOfVertices++;
numberOfVertexIndices += nn + 1;
break;
case LINE_CELL:
numberOfLines++;
numberOfLineIndices += nn + 1;
break;
case TRIANGLE_CELL:
numberOfPolygons++;
numberOfPolygonIndices += nn + 1;
break;
case POLYGON_CELL:
numberOfPolygons++;
numberOfPolygonIndices += nn + 1;
break;
case QUADRILATERAL_CELL:
numberOfPolygons++;
numberOfPolygonIndices += nn + 1;
break;
default:
itkExceptionMacro(<< "Currently we dont support this cell type");
}
index += nn;
}
MetaDataDictionary & metaDic = this->GetMetaDataDictionary();
EncapsulateMetaData< unsigned int >(metaDic, "numberOfVertices", numberOfVertices);
EncapsulateMetaData< unsigned int >(metaDic, "numberOfVertexIndices", numberOfVertexIndices);
EncapsulateMetaData< unsigned int >(metaDic, "numberOfLines", numberOfLines);
EncapsulateMetaData< unsigned int >(metaDic, "numberOfLineIndices", numberOfLineIndices);
EncapsulateMetaData< unsigned int >(metaDic, "numberOfPolygons", numberOfPolygons);
EncapsulateMetaData< unsigned int >(metaDic, "numberOfPolygonIndices", numberOfPolygonIndices);
return;
}
template< typename T >
void ReadPointsBufferAsASCII(std::ifstream & inputFile, T *buffer)
{
std::string line;
while ( !inputFile.eof() )
{
std::getline(inputFile, line, '\n');
if ( line.find("POINTS") != std::string::npos )
{
/** Load the point coordinates into the itk::Mesh */
SizeValueType numberOfComponents = this->m_NumberOfPoints * this->m_PointDimension;
for ( SizeValueType ii = 0; ii < numberOfComponents; ii++ )
{
inputFile >> buffer[ii];
}
}
}
}
template< typename T >
void ReadPointsBufferAsBINARY(std::ifstream & inputFile, T *buffer)
{
std::string line;
while ( !inputFile.eof() )
{
std::getline(inputFile, line, '\n');
if ( line.find("POINTS") != std::string::npos )
{
/** Load the point coordinates into the itk::Mesh */
SizeValueType numberOfComponents = this->m_NumberOfPoints * this->m_PointDimension;
inputFile.read( reinterpret_cast< char * >( buffer ), numberOfComponents * sizeof( T ) );
if ( itk::ByteSwapper< T >::SystemIsLittleEndian() )
{
itk::ByteSwapper< T >::SwapRangeFromSystemToBigEndian(buffer, numberOfComponents);
}
}
}
}
void ReadCellsBufferAsASCII(std::ifstream & inputFile, void *buffer);
void ReadCellsBufferAsBINARY(std::ifstream & inputFile, void *buffer);
template< typename T >
void ReadPointDataBufferAsASCII(std::ifstream & inputFile, T *buffer)
{
StringType line;
while ( !inputFile.eof() )
{
std::getline(inputFile, line, '\n');
if ( line.find("POINT_DATA") != std::string::npos )
{
if ( !inputFile.eof() )
{
std::getline(inputFile, line, '\n');
}
else
{
itkExceptionMacro("UnExpected end of line while trying to read POINT_DATA");
}
/** For scalars we have to read the next line of LOOKUP_TABLE */
if ( line.find("SCALARS") != std::string::npos && line.find("COLOR_SCALARS") == std::string::npos )
{
if ( !inputFile.eof() )
{
std::getline(inputFile, line, '\n');
if ( line.find("LOOKUP_TABLE") == std::string::npos )
{
itkExceptionMacro("UnExpected end of line while trying to read LOOKUP_TABLE");
}
}
else
{
itkExceptionMacro("UnExpected end of line while trying to read LOOKUP_TABLE");
}
}
/** for VECTORS or NORMALS or TENSORS, we could read them directly */
SizeValueType numberOfComponents = this->m_NumberOfPointPixels * this->m_NumberOfPointPixelComponents;
for ( SizeValueType ii = 0; ii < numberOfComponents; ii++ )
{
inputFile >> buffer[ii];
}
}
}
}
template< typename T >
void ReadPointDataBufferAsBINARY(std::ifstream & inputFile, T *buffer)
{
StringType line;
while ( !inputFile.eof() )
{
std::getline(inputFile, line, '\n');
if ( line.find("POINT_DATA") != std::string::npos )
{
if ( !inputFile.eof() )
{
std::getline(inputFile, line, '\n');
}
else
{
itkExceptionMacro("UnExpected end of line while trying to read POINT_DATA");
}
/** For scalars we have to read the next line of LOOKUP_TABLE */
if ( line.find("SCALARS") != std::string::npos && line.find("COLOR_SCALARS") == std::string::npos )
{
if ( !inputFile.eof() )
{
std::getline(inputFile, line, '\n');
if ( line.find("LOOKUP_TABLE") == std::string::npos )
{
itkExceptionMacro("UnExpected end of line while trying to read LOOKUP_TABLE");
}
}
else
{
itkExceptionMacro("UnExpected end of line while trying to read LOOKUP_TABLE");
}
}
/** for VECTORS or NORMALS or TENSORS, we could read them directly */
SizeValueType numberOfComponents = this->m_NumberOfPointPixels * this->m_NumberOfPointPixelComponents;
inputFile.read( reinterpret_cast< char * >( buffer ), numberOfComponents * sizeof( T ) );
if ( itk::ByteSwapper< T >::SystemIsLittleEndian() )
{
itk::ByteSwapper< T >::SwapRangeFromSystemToBigEndian(buffer, numberOfComponents);
}
}
}
}
template< typename T >
void ReadCellDataBufferAsASCII(std::ifstream & inputFile, T *buffer)
{
StringType line;
while ( !inputFile.eof() )
{
std::getline(inputFile, line, '\n');
if ( line.find("CELL_DATA") != std::string::npos )
{
if ( !inputFile.eof() )
{
std::getline(inputFile, line, '\n');
}
else
{
itkExceptionMacro("UnExpected end of line while trying to read CELL_DATA");
}
/** For scalars we have to read the next line of LOOKUP_TABLE */
if ( line.find("SCALARS") != std::string::npos && line.find("COLOR_SCALARS") == std::string::npos )
{
if ( !inputFile.eof() )
{
std::getline(inputFile, line, '\n');
if ( line.find("LOOKUP_TABLE") == std::string::npos )
{
itkExceptionMacro("UnExpected end of line while trying to read LOOKUP_TABLE");
}
}
else
{
itkExceptionMacro("UnExpected end of line while trying to read LOOKUP_TABLE");
}
}
/** for VECTORS or NORMALS or TENSORS, we could read them directly */
SizeValueType numberOfComponents = this->m_NumberOfCellPixels * this->m_NumberOfCellPixelComponents;
for ( SizeValueType ii = 0; ii < numberOfComponents; ii++ )
{
inputFile >> buffer[ii];
}
}
}
}
template< typename T >
void ReadCellDataBufferAsBINARY(std::ifstream & inputFile, T *buffer)
{
StringType line;
while ( !inputFile.eof() )
{
std::getline(inputFile, line, '\n');
if ( line.find("POINT_DATA") != std::string::npos )
{
if ( !inputFile.eof() )
{
std::getline(inputFile, line, '\n');
}
else
{
itkExceptionMacro("UnExpected end of line while trying to read POINT_DATA");
}
/** For scalars we have to read the next line of LOOKUP_TABLE */
if ( line.find("SCALARS") != std::string::npos && line.find("COLOR_SCALARS") == std::string::npos )
{
if ( !inputFile.eof() )
{
std::getline(inputFile, line, '\n');
if ( line.find("LOOKUP_TABLE") == std::string::npos )
{
itkExceptionMacro("UnExpected end of line while trying to read LOOKUP_TABLE");
}
}
else
{
itkExceptionMacro("UnExpected end of line while trying to read LOOKUP_TABLE");
}
}
/** For VECTORS or NORMALS or TENSORS, we could read them directly */
SizeValueType numberOfComponents = this->m_NumberOfCellPixels * this->m_NumberOfCellPixelComponents;
inputFile.read( reinterpret_cast< char * >( buffer ), numberOfComponents * sizeof( T ) );
if ( itk::ByteSwapper< T >::SystemIsLittleEndian() )
{
itk::ByteSwapper< T >::SwapRangeFromSystemToBigEndian(buffer, numberOfComponents);
}
}
}
}
template< typename T >
void WritePointsBufferAsASCII(std::ofstream & outputFile, T *buffer, const StringType & pointComponentType)
{
NumberToString<T> convert;
/** 1. Write number of points */
outputFile << "POINTS " << this->m_NumberOfPoints;
outputFile << pointComponentType << '\n';
for ( SizeValueType ii = 0; ii < this->m_NumberOfPoints; ii++ )
{
for ( unsigned int jj = 0; jj < this->m_PointDimension - 1; jj++ )
{
outputFile << convert(buffer[ii * this->m_PointDimension + jj]) << " ";
}
outputFile << convert(buffer[ii * this->m_PointDimension + this->m_PointDimension - 1]) << '\n';
}
return;
}
template< typename T >
void WritePointsBufferAsBINARY(std::ofstream & outputFile, T *buffer, const StringType & pointComponentType)
{
/** 1. Write number of points */
outputFile << "POINTS " << this->m_NumberOfPoints;
outputFile << pointComponentType << "\n";
itk::ByteSwapper< T >::SwapWriteRangeFromSystemToBigEndian(buffer, this->m_NumberOfPoints * this->m_PointDimension, &outputFile);
outputFile << "\n";
return;
}
template< typename T >
void WriteCellsBufferAsASCII(std::ofstream & outputFile, T *buffer)
{
MetaDataDictionary & metaDic = this->GetMetaDataDictionary();
unsigned int numberOfVertices = 0;
unsigned int numberOfVertexIndices = 0;
unsigned int numberOfLines = 0;
unsigned int numberOfLineIndices = 0;
unsigned int numberOfPolygons = 0;
unsigned int numberOfPolygonIndices = 0;
/** Write vertices */
SizeValueType index = 0;
ExposeMetaData< unsigned int >(metaDic, "numberOfVertices", numberOfVertices);
if ( numberOfVertices )
{
ExposeMetaData< unsigned int >(metaDic, "numberOfVertexIndices", numberOfVertexIndices);
outputFile << "VERTICES " << numberOfVertices << " " << numberOfVertexIndices << '\n';
for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
{
MeshIOBase::CellGeometryType cellType = static_cast< MeshIOBase::CellGeometryType >( static_cast< int >( buffer[index++] ) );
unsigned int nn = static_cast< unsigned int >( buffer[index++] );
if ( cellType == VERTEX_CELL )
{
outputFile << nn;
for ( unsigned int jj = 0; jj < nn; jj++ )
{
outputFile << " " << buffer[index++];
}
outputFile << '\n';
}
else
{
index += nn;
}
}
}
/** Write lines */
index = 0;
ExposeMetaData< unsigned int >(metaDic, "numberOfLines", numberOfLines);
if ( numberOfLines )
{
numberOfLineIndices = 0;
SizeValueType numberOfPolylines = 0;
PolylinesContainerPointer polylines = PolylinesContainerType::New();
PointIdVector pointIds;
for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
{
MeshIOBase::CellGeometryType cellType = static_cast< MeshIOBase::CellGeometryType >( static_cast< int >( buffer[index++] ) );
unsigned int nn = static_cast< unsigned int >( buffer[index++] );
if ( cellType == LINE_CELL )
{
if ( pointIds.size() >= nn )
{
SizeValueType id = pointIds.back();
if ( id == static_cast< SizeValueType >( buffer[index] ) )
{
pointIds.push_back( static_cast< SizeValueType >( buffer[index + 1] ) );
}
else if ( id == static_cast< SizeValueType >( buffer[index + 1] ) )
{
pointIds.push_back( static_cast< SizeValueType >( buffer[index] ) );
}
else
{
polylines->InsertElement(numberOfPolylines++, pointIds);
numberOfLineIndices += pointIds.size();
pointIds.clear();
for ( unsigned int jj = 0; jj < nn; jj++ )
{
pointIds.push_back( static_cast< SizeValueType >( buffer[index + jj] ) );
}
}
}
else
{
for ( unsigned int jj = 0; jj < nn; jj++ )
{
pointIds.push_back( static_cast< SizeValueType >( buffer[index + jj] ) );
}
}
}
index += nn;
}
polylines->InsertElement(numberOfPolylines++, pointIds);
numberOfLineIndices += pointIds.size();
pointIds.clear();
numberOfLines = polylines->Size();
numberOfLineIndices += numberOfLines;
EncapsulateMetaData< unsigned int >(metaDic, "numberOfLines", numberOfLines);
EncapsulateMetaData< unsigned int >(metaDic, "numberOfLineIndices", numberOfLineIndices);
outputFile << "LINES " << numberOfLines << " " << numberOfLineIndices << '\n';
for ( SizeValueType ii = 0; ii < polylines->Size(); ++ii )
{
unsigned int nn = polylines->ElementAt(ii).size();
outputFile << nn;
for ( unsigned int jj = 0; jj < nn; ++jj )
{
outputFile << " " << polylines->ElementAt(ii)[jj];
}
outputFile << '\n';
}
}
/** Write polygons */
index = 0;
ExposeMetaData< unsigned int >(metaDic, "numberOfPolygons", numberOfPolygons);
if ( numberOfPolygons )
{
ExposeMetaData< unsigned int >(metaDic, "numberOfPolygonIndices", numberOfPolygonIndices);
outputFile << "POLYGONS " << numberOfPolygons << " " << numberOfPolygonIndices << '\n';
for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
{
MeshIOBase::CellGeometryType cellType = static_cast< MeshIOBase::CellGeometryType >( static_cast< int >( buffer[index++] ) );
unsigned int nn = static_cast< unsigned int >( buffer[index++] );
if ( cellType == POLYGON_CELL ||
cellType == TRIANGLE_CELL ||
cellType == QUADRILATERAL_CELL )
{
outputFile << nn;
for ( unsigned int jj = 0; jj < nn; jj++ )
{
outputFile << " " << buffer[index++];
}
outputFile << '\n';
}
else
{
index += nn;
}
}
}
}
template< typename T >
void WriteCellsBufferAsBINARY(std::ofstream & outputFile, T *buffer)
{
MetaDataDictionary & metaDic = this->GetMetaDataDictionary();
unsigned int numberOfVertices = 0;
unsigned int numberOfVertexIndices = 0;
unsigned int numberOfLines = 0;
unsigned int numberOfLineIndices = 0;
unsigned int numberOfPolygons = 0;
unsigned int numberOfPolygonIndices = 0;
/** Write vertices */
SizeValueType index = 0;
ExposeMetaData< unsigned int >(metaDic, "numberOfVertices", numberOfVertices);
if ( numberOfVertices )
{
ExposeMetaData< unsigned int >(metaDic, "numberOfVertexIndices", numberOfVertexIndices);
outputFile << "VERTICES " << numberOfVertices << " " << numberOfVertexIndices << '\n';
unsigned int *data = new unsigned int[numberOfVertexIndices];
ReadCellsBuffer(buffer, data);
itk::ByteSwapper< unsigned int >::SwapWriteRangeFromSystemToBigEndian(data, numberOfVertexIndices, &outputFile);
outputFile << "\n";
delete[] data;
}
/** Write lines */
index = 0;
ExposeMetaData< unsigned int >(metaDic, "numberOfLines", numberOfLines);
if ( numberOfLines )
{
numberOfLineIndices = 0;
SizeValueType numberOfPolylines = 0;
PolylinesContainerPointer polylines = PolylinesContainerType::New();
PointIdVector pointIds;
for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
{
MeshIOBase::CellGeometryType cellType = static_cast< MeshIOBase::CellGeometryType >( static_cast< int >( buffer[index++] ) );
unsigned int nn = static_cast< unsigned int >( buffer[index++] );
if ( cellType == LINE_CELL )
{
if ( pointIds.size() >= nn )
{
SizeValueType id = pointIds.back();
if ( id == static_cast< SizeValueType >( buffer[index] ) )
{
pointIds.push_back( static_cast< SizeValueType >( buffer[index + 1] ) );
}
else if ( id == static_cast< SizeValueType >( buffer[index + 1] ) )
{
pointIds.push_back( static_cast< SizeValueType >( buffer[index] ) );
}
else
{
polylines->InsertElement(numberOfPolylines++, pointIds);
numberOfLineIndices += pointIds.size();
pointIds.clear();
for ( unsigned int jj = 0; jj < nn; jj++ )
{
pointIds.push_back( static_cast< SizeValueType >( buffer[index + jj] ) );
}
}
}
else
{
for ( unsigned int jj = 0; jj < nn; jj++ )
{
pointIds.push_back( static_cast< SizeValueType >( buffer[index + jj] ) );
}
}
}
index += nn;
}
polylines->InsertElement(numberOfPolylines++, pointIds);
numberOfLineIndices += pointIds.size();
pointIds.clear();
numberOfLines = polylines->Size();
numberOfLineIndices += numberOfLines;
EncapsulateMetaData< unsigned int >(metaDic, "numberOfLines", numberOfLines);
EncapsulateMetaData< unsigned int >(metaDic, "numberOfLineIndices", numberOfLineIndices);
outputFile << "LINES " << numberOfLines << " " << numberOfLineIndices << '\n';
unsigned int *data = new unsigned int[numberOfLineIndices];
unsigned long outputIndex = 0;
for ( SizeValueType ii = 0; ii < polylines->Size(); ++ii )
{
unsigned int nn = polylines->ElementAt(ii).size();
data[outputIndex++] = nn;
for ( unsigned int jj = 0; jj < nn; ++jj )
{
data[outputIndex++] = polylines->ElementAt(ii)[jj];
}
}
itk::ByteSwapper< unsigned int >::SwapWriteRangeFromSystemToBigEndian(data, numberOfLineIndices, &outputFile);
outputFile << "\n";
delete[] data;
}
/** Write polygons */
index = 0;
ExposeMetaData< unsigned int >(metaDic, "numberOfPolygons", numberOfPolygons);
if ( numberOfPolygons )
{
ExposeMetaData< unsigned int >(metaDic, "numberOfPolygonIndices", numberOfPolygonIndices);
outputFile << "POLYGONS " << numberOfPolygons << " " << numberOfPolygonIndices << '\n';
unsigned int *data = new unsigned int[numberOfPolygonIndices];
ReadCellsBuffer(buffer, data);
itk::ByteSwapper< unsigned int >::SwapWriteRangeFromSystemToBigEndian(data, numberOfPolygonIndices, &outputFile);
outputFile << "\n";
delete[] data;
}
}
template< typename T >
void WritePointDataBufferAsASCII(std::ofstream & outputFile, T *buffer, const StringType & pointPixelComponentName)
{
NumberToString<T> convert;
MetaDataDictionary & metaDic = this->GetMetaDataDictionary();
StringType dataName;
outputFile << "POINT_DATA " << this->m_NumberOfPointPixels << '\n';
switch ( this->m_PointPixelType )
{
case SCALAR:
{
outputFile << "SCALARS ";
ExposeMetaData< StringType >(metaDic, "pointScalarDataName", dataName);
outputFile << dataName << " ";
break;
}
case OFFSET:
case POINT:
case COVARIANTVECTOR:
case VECTOR:
{
outputFile << "VECTORS ";
ExposeMetaData< StringType >(metaDic, "pointVectorDataName", dataName);
outputFile << dataName << " ";
break;
}
case SYMMETRICSECONDRANKTENSOR:
case DIFFUSIONTENSOR3D:
{
outputFile << "TENSORS ";
ExposeMetaData< StringType >(metaDic, "pointTensorDataName", dataName);
outputFile << dataName << " ";
break;
}
case ARRAY:
case VARIABLELENGTHVECTOR:
{
outputFile << "COLOR_SCALARS ";
ExposeMetaData< StringType >(metaDic, "pointColorScalarDataName", dataName);
outputFile << dataName << " ";
WriteColorScalarBufferAsASCII(outputFile, buffer, this->m_NumberOfPointPixelComponents, this->m_NumberOfPointPixels);
return;
}
default:
{
itkExceptionMacro(<< "Unknown point pixel type");
}
}
outputFile << pointPixelComponentName << '\n';
if ( this->m_PointPixelType == SCALAR )
{
outputFile << "LOOKUP_TABLE default" << '\n';
}
Indent indent(2);
if ( this->m_PointPixelType == SYMMETRICSECONDRANKTENSOR )
{
T *ptr = buffer;
SizeValueType i = 0;
const SizeValueType num = this->m_NumberOfPointPixelComponents * this->m_NumberOfPointPixels;
// Note that only the 3D tensors are supported in the VTK File Format
// documentation.
if( this->m_NumberOfPointPixelComponents == 3 )
{
T zero( itk::NumericTraits<T>::Zero );
T e12;
while( i < num )
{
// row 1
outputFile << convert(*ptr++) << indent;
e12 = *ptr++;
outputFile << convert(e12) << indent;
outputFile << convert(zero) << '\n';
// row 2
outputFile << convert(e12) << indent;
outputFile << convert(*ptr++) << indent;
outputFile << convert(zero) << '\n';
// row 3
outputFile << convert(zero) << indent << convert(zero) << indent << convert(zero) << "\n\n";
i += 3;
}
}
else if( this->m_NumberOfPointPixelComponents == 6 )
{
T e12;
T e13;
T e23;
while( i < num )
{
// row 1
outputFile << convert(*ptr++) << indent;
e12 = *ptr++;
outputFile << convert(e12) << indent;
e13 = *ptr++;
outputFile << convert(e13) << '\n';
// row 2
outputFile << convert(e12) << indent;
outputFile << convert(*ptr++) << indent;
e23 = *ptr++;
outputFile << convert(e23) << '\n';
// row 3
outputFile << convert(e13) << indent;
outputFile << convert(e23) << indent;
outputFile << convert(*ptr++) << "\n\n";
i += 6;
}
}
else
{
::itk::ExceptionObject e_(__FILE__, __LINE__,
"itk::ERROR: VTKImageIO2: Unsupported number of components in tensor.",
ITK_LOCATION);
throw e_;
}
}
else // not tensor
{
unsigned int jj;
for ( SizeValueType ii = 0; ii < this->m_NumberOfPointPixels; ii++ )
{
for ( jj = 0; jj < this->m_NumberOfPointPixelComponents - 1; jj++ )
{
outputFile << convert(buffer[ii * this->m_NumberOfPointPixelComponents + jj]) << indent;
}
outputFile << convert(buffer[ii * this->m_NumberOfPointPixelComponents + jj]);
outputFile << '\n';
}
}
return;
}
template< typename T >
void WritePointDataBufferAsBINARY(std::ofstream & outputFile, T *buffer, const StringType & pointPixelComponentName)
{
MetaDataDictionary & metaDic = this->GetMetaDataDictionary();
StringType dataName;
outputFile << "POINT_DATA " << this->m_NumberOfPointPixels << "\n";
switch ( this->m_PointPixelType )
{
case SCALAR:
{
outputFile << "SCALARS ";
ExposeMetaData< StringType >(metaDic, "pointScalarDataName", dataName);
outputFile << dataName << " ";
break;
}
case OFFSET:
case POINT:
case COVARIANTVECTOR:
case VECTOR:
{
outputFile << "VECTORS ";
ExposeMetaData< StringType >(metaDic, "pointVectorDataName", dataName);
outputFile << dataName << " ";
break;
}
case SYMMETRICSECONDRANKTENSOR:
case DIFFUSIONTENSOR3D:
{
outputFile << "TENSORS ";
ExposeMetaData< StringType >(metaDic, "pointTensorDataName", dataName);
outputFile << dataName << " ";
break;
}
case ARRAY:
case VARIABLELENGTHVECTOR:
{
outputFile << "COLOR_SCALARS ";
ExposeMetaData< StringType >(metaDic, "pointColorScalarDataName", dataName);
outputFile << dataName << " ";
WriteColorScalarBufferAsBINARY(outputFile, buffer, this->m_NumberOfPointPixelComponents, this->m_NumberOfPointPixels);
return;
}
default:
{
itkExceptionMacro(<< "Unknown point pixel type");
}
}
outputFile << pointPixelComponentName << "\n";
if ( this->m_PointPixelType == SCALAR )
{
outputFile << "LOOKUP_TABLE default\n";
}
itk::ByteSwapper< T >::SwapWriteRangeFromSystemToBigEndian(buffer,
this->m_NumberOfPointPixels * this->m_NumberOfPointPixelComponents,
&outputFile);
outputFile << "\n";
return;
}
template< typename T >
void WriteCellDataBufferAsASCII(std::ofstream & outputFile, T *buffer, const StringType & cellPixelComponentName)
{
MetaDataDictionary & metaDic = this->GetMetaDataDictionary();
StringType dataName;
outputFile << "CELL_DATA " << this->m_NumberOfCellPixels << '\n';
switch ( this->m_CellPixelType )
{
case SCALAR:
{
outputFile << "SCALARS ";
ExposeMetaData< StringType >(metaDic, "cellScalarDataName", dataName);
outputFile << dataName << " ";
break;
}
case OFFSET:
case POINT:
case COVARIANTVECTOR:
case VECTOR:
{
outputFile << "VECTORS ";
ExposeMetaData< StringType >(metaDic, "cellVectorDataName", dataName);
outputFile << dataName << " ";
break;
}
case SYMMETRICSECONDRANKTENSOR:
case DIFFUSIONTENSOR3D:
{
outputFile << "TENSORS ";
ExposeMetaData< StringType >(metaDic, "cellTensorDataName", dataName);
outputFile << dataName << " ";
break;
}
case ARRAY:
case VARIABLELENGTHVECTOR:
{
outputFile << "COLOR_SCALARS ";
ExposeMetaData< StringType >(metaDic, "cellColorScalarDataName", dataName);
outputFile << dataName << " ";
WriteColorScalarBufferAsASCII(outputFile, buffer, this->m_NumberOfCellPixelComponents, this->m_NumberOfCellPixels);
return;
}
default:
{
itkExceptionMacro(<< "Unknown cell pixel type");
}
}
outputFile << cellPixelComponentName << '\n';
if ( this->m_CellPixelType == SCALAR )
{
outputFile << "LOOKUP_TABLE default" << '\n';
}
Indent indent(2);
if ( this->m_CellPixelType == SYMMETRICSECONDRANKTENSOR )
{
T *ptr = buffer;
SizeValueType i = 0;
const SizeValueType num = this->m_NumberOfCellPixelComponents * this->m_NumberOfCellPixels;
if( this->m_NumberOfCellPixelComponents == 3 )
{
T zero( itk::NumericTraits<T>::Zero );
T e12;
while( i < num )
{
// row 1
outputFile << *ptr++ << indent;
e12 = *ptr++;
outputFile << e12 << indent;
outputFile << zero << '\n';
// row 2
outputFile << e12 << indent;
outputFile << *ptr++ << indent;
outputFile << zero << '\n';
// row 3
outputFile << zero << indent << zero << indent << zero << "\n\n";
i += 3;
}
}
else if( this->m_NumberOfCellPixelComponents == 3 )
{
T e12;
T e13;
T e23;
while( i < num )
{
// row 1
outputFile << *ptr++ << indent;
e12 = *ptr++;
outputFile << e12 << indent;
e13 = *ptr++;
outputFile << e13 << '\n';
// row 2
outputFile << e12 << indent;
outputFile << *ptr++ << indent;
e23 = *ptr++;
outputFile << e23 << '\n';
// row 3
outputFile << e13 << indent;
outputFile << e23 << indent;
outputFile << *ptr++ << "\n\n";
i += 6;
}
}
else
{
::itk::ExceptionObject e_(__FILE__, __LINE__,
"itk::ERROR: VTKPolyDataMeshIO: Unsupported number of components in tensor.",
ITK_LOCATION);
throw e_;
}
}
else // not tensor
{
unsigned int jj;
for ( SizeValueType ii = 0; ii < this->m_NumberOfCellPixels; ii++ )
{
for ( jj = 0; jj < this->m_NumberOfCellPixelComponents - 1; jj++ )
{
outputFile << buffer[ii * this->m_NumberOfCellPixelComponents + jj] << indent;
}
outputFile << buffer[ii * this->m_NumberOfCellPixelComponents + jj];
outputFile << '\n';
}
}
return;
}
template< typename T >
void WriteCellDataBufferAsBINARY(std::ofstream & outputFile, T *buffer, const StringType & cellPixelComponentName)
{
MetaDataDictionary & metaDic = this->GetMetaDataDictionary();
StringType dataName;
outputFile << "CELL_DATA " << this->m_NumberOfCellPixels << "\n";
switch ( this->m_CellPixelType )
{
case SCALAR:
{
outputFile << "SCALARS ";
ExposeMetaData< StringType >(metaDic, "cellScalarDataName", dataName);
outputFile << dataName << " ";
break;
}
case OFFSET:
case POINT:
case COVARIANTVECTOR:
case VECTOR:
{
outputFile << "VECTORS ";
ExposeMetaData< StringType >(metaDic, "cellVectorDataName", dataName);
outputFile << dataName << " ";
break;
}
case SYMMETRICSECONDRANKTENSOR:
case DIFFUSIONTENSOR3D:
{
outputFile << "TENSORS ";
ExposeMetaData< StringType >(metaDic, "cellTensorDataName", dataName);
outputFile << dataName << " ";
break;
}
case ARRAY:
case VARIABLELENGTHVECTOR:
{
outputFile << "COLOR_SCALARS ";
ExposeMetaData< StringType >(metaDic, "cellColorScalarDataName", dataName);
outputFile << dataName << " ";
WriteColorScalarBufferAsBINARY(outputFile, buffer, this->m_NumberOfCellPixelComponents, this->m_NumberOfCellPixels);
return;
}
default:
{
itkExceptionMacro(<< "Unknown cell pixel type");
}
}
outputFile << cellPixelComponentName << "\n";
if ( this->m_CellPixelType == SCALAR )
{
outputFile << "LOOKUP_TABLE default\n";
}
itk::ByteSwapper< T >::SwapWriteRangeFromSystemToBigEndian(buffer,
this->m_NumberOfCells * this->m_NumberOfCellPixelComponents,
&outputFile);
outputFile << "\n";
return;
}
template< typename T >
void WriteColorScalarBufferAsASCII(std::ofstream & outputFile,
T *buffer,
unsigned int numberOfPixelComponents,
SizeValueType numberOfPixels)
{
NumberToString<float> convert;
outputFile << numberOfPixelComponents << "\n";
Indent indent(2);
for ( SizeValueType ii = 0; ii < numberOfPixels; ++ii )
{
for ( unsigned int jj = 0; jj < numberOfPixelComponents; ++jj )
{
outputFile << convert(static_cast< float >( buffer[ii * numberOfPixelComponents + jj])) << indent;
}
outputFile << "\n";
}
return;
}
template< typename T >
void WriteColorScalarBufferAsBINARY(std::ofstream & outputFile,
T *buffer,
unsigned int numberOfPixelComponents,
SizeValueType numberOfPixels)
{
outputFile << numberOfPixelComponents << "\n";
SizeValueType numberOfElements = numberOfPixelComponents * numberOfPixels;
unsigned char *data = new unsigned char[numberOfElements];
for ( SizeValueType ii = 0; ii < numberOfElements; ++ii )
{
data[ii] = static_cast< unsigned char >( buffer[ii] );
}
outputFile.write(reinterpret_cast< char * >( data ), numberOfElements);
delete[] data;
outputFile << "\n";
return;
}
/** Convert cells buffer for output cells buffer, it's user's responsibility to make sure
the input cells don't contain any cell type that coule not be written as polygon cell */
template< typename TInput, typename TOutput >
void ReadCellsBuffer(TInput *input, TOutput *output)
{
SizeValueType inputIndex = 0;
SizeValueType outputIndex = 0;
if ( input && output )
{
for ( SizeValueType ii = 0; ii < this->m_NumberOfCells; ii++ )
{
inputIndex++;
unsigned int nn = static_cast< unsigned int >( input[inputIndex++] );
output[outputIndex++] = nn;
for ( unsigned int jj = 0; jj < nn; jj++ )
{
output[outputIndex++] = static_cast< TOutput >( input[inputIndex++] );
}
}
}
}
private:
VTKPolyDataMeshIO(const Self &); // purposely not implemented
void operator=(const Self &); // purposely not implemented
};
} // end namespace itk
#endif // __itkVTKPolyDataMeshIO_h
|