/usr/include/ITK-4.5/itkMeshFileReader.hxx 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 | /*=========================================================================
*
* 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 __itkMeshFileReader_hxx
#define __itkMeshFileReader_hxx
#include "itkConvertPixelBuffer.h"
#include "itkConvertArrayPixelBuffer.h"
#include "itkConvertVariableLengthVectorPixelBuffer.h"
#include "itkMeshIOFactory.h"
#include "itkMeshFileReader.h"
#include "itkMeshRegion.h"
#include "itkObjectFactory.h"
#include "itkPixelTraits.h"
#include <itksys/SystemTools.hxx>
#include <fstream>
namespace itk
{
template< typename TOutputMesh, typename ConvertPointPixelTraits, typename ConvertCellPixelTraits >
MeshFileReader< TOutputMesh, ConvertPointPixelTraits, ConvertCellPixelTraits >
::MeshFileReader()
{
m_MeshIO = 0;
m_FileName = "";
m_UserSpecifiedMeshIO = false;
}
template< typename TOutputMesh, typename ConvertPointPixelTraits, typename ConvertCellPixelTraits >
void
MeshFileReader< TOutputMesh, ConvertPointPixelTraits, ConvertCellPixelTraits >
::PrintSelf(std::ostream & os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
if ( m_MeshIO )
{
os << indent << "MeshIO: \n";
m_MeshIO->Print( os, indent.GetNextIndent() );
}
else
{
os << indent << "MeshIO: (null)" << "\n";
}
os << indent << "UserSpecifiedMeshIO flag: " << m_UserSpecifiedMeshIO << "\n";
os << indent << "FileName: " << m_FileName << "\n";
}
template< typename TOutputMesh, typename ConvertPointPixelTraits, typename ConvertCellPixelTraits >
void
MeshFileReader< TOutputMesh, ConvertPointPixelTraits, ConvertCellPixelTraits >
::SetMeshIO(MeshIOBase *meshIO)
{
itkDebugMacro("setting MeshIO to " << meshIO);
if ( this->m_MeshIO != meshIO )
{
this->m_MeshIO = meshIO;
this->Modified();
}
m_UserSpecifiedMeshIO = true;
}
template< typename TOutputMesh, typename ConvertPointPixelTraits, typename ConvertCellPixelTraits >
void
MeshFileReader< TOutputMesh, ConvertPointPixelTraits, ConvertCellPixelTraits >
::TestFileExistanceAndReadability()
{
// Test if the file exists.
if ( !itksys::SystemTools::FileExists( m_FileName.c_str() ) )
{
MeshFileReaderException e(__FILE__, __LINE__);
std::ostringstream msg;
msg << "The file doesn't exist. "
<< std::endl << "Filename = " << m_FileName
<< std::endl;
e.SetDescription( msg.str().c_str() );
throw e;
return;
}
// Test if the file can be open for reading access.
std::ifstream readTester;
readTester.open( m_FileName.c_str() );
if ( readTester.fail() )
{
readTester.close();
std::ostringstream msg;
msg << "The file couldn't be opened for reading. "
<< std::endl << "Filename: " << m_FileName
<< std::endl;
MeshFileReaderException e(__FILE__, __LINE__, msg.str().c_str(), ITK_LOCATION);
throw e;
return;
}
readTester.close();
}
template< typename TOutputMesh, typename ConvertPointPixelTraits, typename ConvertCellPixelTraits >
template< typename T >
void
MeshFileReader< TOutputMesh, ConvertPointPixelTraits, ConvertCellPixelTraits >
::ReadPoints(T *buffer)
{
typename TOutputMesh::Pointer output = this->GetOutput();
output->GetPoints()->Reserve( m_MeshIO->GetNumberOfPoints() );
OutputPointType point;
for ( OutputPointIdentifier id = 0; id < output->GetNumberOfPoints(); id++ )
{
for ( OutputPointIdentifier ii = 0; ii < OutputPointDimension; ii++ )
{
point[ii] = static_cast< typename OutputPointType::ValueType >( buffer[id * OutputPointDimension + ii] );
}
output->SetPoint(id, point);
}
}
template< typename TOutputMesh, typename ConvertPointPixelTraits, typename ConvertCellPixelTraits >
template< typename T >
void
MeshFileReader< TOutputMesh, ConvertPointPixelTraits, ConvertCellPixelTraits >
::ReadCells(T *buffer)
{
typename TOutputMesh::Pointer output = this->GetOutput();
SizeValueType index = NumericTraits< SizeValueType >::Zero;
OutputCellIdentifier id = NumericTraits< OutputCellIdentifier >::Zero;
while ( index < m_MeshIO->GetCellBufferSize() )
{
MeshIOBase::CellGeometryType type = static_cast< MeshIOBase::CellGeometryType >( static_cast< int >( buffer[index++] ) );
switch ( type )
{
case MeshIOBase::VERTEX_CELL:
{
unsigned int numberOfPoints = static_cast< unsigned int >( buffer[index++] );
if ( numberOfPoints != OutputVertexCellType::NumberOfPoints )
{
itkExceptionMacro(<< "Invalid Vertex Cell with number of points = " << numberOfPoints);
}
OutputCellAutoPointer cell;
OutputVertexCellType *vertexCell = new OutputVertexCellType;
for ( unsigned int jj = 0; jj < OutputVertexCellType::NumberOfPoints; jj++ )
{
vertexCell->SetPointId( jj, static_cast< OutputPointIdentifier >( buffer[index++] ) );
}
cell.TakeOwnership(vertexCell);
output->SetCell(id++, cell);
break;
}
case MeshIOBase::LINE_CELL:
{
// for polylines will be loaded as individual edges.
unsigned int numberOfPoints = static_cast< unsigned int >( buffer[index++] );
if ( numberOfPoints < 2 )
{
itkExceptionMacro(<< "Invalid Line Cell with number of points = " << numberOfPoints);
}
OutputPointIdentifier pointIDBuffer = static_cast< OutputPointIdentifier >( buffer[index++] );
for ( unsigned int jj = 1; jj < numberOfPoints; ++jj )
{
OutputCellAutoPointer cell;
OutputLineCellType * lineCell = new OutputLineCellType;
lineCell->SetPointId(0, pointIDBuffer);
pointIDBuffer = static_cast< OutputPointIdentifier >( buffer[index++] );
lineCell->SetPointId(1, pointIDBuffer);
cell.TakeOwnership(lineCell);
output->SetCell(id++, cell);
}
break;
}
case MeshIOBase::TRIANGLE_CELL:
{
unsigned int numberOfPoints = static_cast< unsigned int >( buffer[index++] );
if ( numberOfPoints != OutputTriangleCellType::NumberOfPoints )
{
itkExceptionMacro(<< "Invalid Triangle Cell with number of points = " << numberOfPoints);
}
OutputCellAutoPointer cell;
OutputTriangleCellType *triangleCell = new OutputTriangleCellType;
for ( unsigned int jj = 0; jj < OutputTriangleCellType::NumberOfPoints; jj++ )
{
triangleCell->SetPointId( jj, static_cast< OutputPointIdentifier >( buffer[index++] ) );
}
cell.TakeOwnership(triangleCell);
output->SetCell(id++, cell);
break;
}
case MeshIOBase::QUADRILATERAL_CELL:
{
unsigned int numberOfPoints = static_cast< unsigned int >( buffer[index++] );
if ( numberOfPoints != OutputQuadrilateralCellType::NumberOfPoints )
{
itkExceptionMacro(<< "Invalid Quadrilateral Cell with number of points = " << numberOfPoints);
}
OutputCellAutoPointer cell;
OutputQuadrilateralCellType *quadrilateralCell = new OutputQuadrilateralCellType;
for ( unsigned int jj = 0; jj < OutputQuadrilateralCellType::NumberOfPoints; jj++ )
{
quadrilateralCell->SetPointId( jj, static_cast< OutputPointIdentifier >( buffer[index++] ) );
}
cell.TakeOwnership(quadrilateralCell);
output->SetCell(id++, cell);
break;
}
case MeshIOBase::POLYGON_CELL:
{
// For polyhedron, if the number of points is 3, then we treat it as
// triangle cell
OutputCellAutoPointer cell;
unsigned int numberOfPoints = static_cast< unsigned int >( buffer[index++] );
if ( numberOfPoints == OutputTriangleCellType::NumberOfPoints )
{
OutputTriangleCellType *triangleCell = new OutputTriangleCellType;
for ( unsigned int jj = 0; jj < OutputTriangleCellType::NumberOfPoints; jj++ )
{
triangleCell->SetPointId( jj, static_cast< OutputPointIdentifier >( buffer[index++] ) );
}
cell.TakeOwnership(triangleCell);
}
else
{
OutputPolygonCellType *polygonCell = new OutputPolygonCellType;
for ( unsigned int jj = 0; jj < numberOfPoints; jj++ )
{
polygonCell->SetPointId( jj, static_cast< OutputPointIdentifier >( buffer[index++] ) );
}
cell.TakeOwnership(polygonCell);
}
output->SetCell(id++, cell);
break;
}
case MeshIOBase::TETRAHEDRON_CELL:
{
unsigned int numberOfPoints = static_cast< unsigned int >( buffer[index++] );
if ( numberOfPoints != OutputTetrahedronCellType::NumberOfPoints )
{
itkExceptionMacro(<< "Invalid Tetrahedron Cell with number of points = " << numberOfPoints);
}
OutputCellAutoPointer cell;
OutputTetrahedronCellType *tetrahedronCell = new OutputTetrahedronCellType;
for ( unsigned int jj = 0; jj < OutputTetrahedronCellType::NumberOfPoints; jj++ )
{
tetrahedronCell->SetPointId( jj, static_cast< OutputPointIdentifier >( buffer[index++] ) );
}
cell.TakeOwnership(tetrahedronCell);
output->SetCell(id++, cell);
break;
}
case MeshIOBase::HEXAHEDRON_CELL:
{
unsigned int numberOfPoints = static_cast< unsigned int >( buffer[index++] );
if ( numberOfPoints != OutputHexahedronCellType::NumberOfPoints )
{
itkExceptionMacro(<< "Invalid Hexahedron Cell with number of points = " << numberOfPoints);
}
OutputCellAutoPointer cell;
OutputHexahedronCellType *hexahedronCell = new OutputHexahedronCellType;
for ( unsigned int jj = 0; jj < OutputHexahedronCellType::NumberOfPoints; jj++ )
{
hexahedronCell->SetPointId( jj, static_cast< OutputPointIdentifier >( buffer[index++] ) );
}
cell.TakeOwnership(hexahedronCell);
output->SetCell(id++, cell);
break;
}
case MeshIOBase::QUADRATIC_EDGE_CELL:
{
unsigned int numberOfPoints = static_cast< unsigned int >( buffer[index++] );
if ( numberOfPoints != OutputQuadraticEdgeCellType::NumberOfPoints )
{
itkExceptionMacro(<< "Invalid Quadratic edge Cell with number of points = " << numberOfPoints);
}
OutputCellAutoPointer cell;
OutputQuadraticEdgeCellType *quadraticEdgeCell = new OutputQuadraticEdgeCellType;
for ( unsigned int jj = 0; jj < OutputQuadraticEdgeCellType::NumberOfPoints; jj++ )
{
quadraticEdgeCell->SetPointId( jj, static_cast< OutputPointIdentifier >( buffer[index++] ) );
}
cell.TakeOwnership(quadraticEdgeCell);
output->SetCell(id++, cell);
break;
}
case MeshIOBase::QUADRATIC_TRIANGLE_CELL:
{
unsigned int numberOfPoints = static_cast< unsigned int >( buffer[index++] );
if ( numberOfPoints != OutputQuadraticTriangleCellType::NumberOfPoints )
{
itkExceptionMacro(<< "Invalid Quadratic triangle Cell with number of points = " << numberOfPoints);
}
OutputCellAutoPointer cell;
OutputQuadraticTriangleCellType *quadraticTriangleCell = new OutputQuadraticTriangleCellType;
for ( unsigned int jj = 0; jj < OutputQuadraticTriangleCellType::NumberOfPoints; jj++ )
{
quadraticTriangleCell->SetPointId( jj, static_cast< OutputPointIdentifier >( buffer[index++] ) );
}
cell.TakeOwnership(quadraticTriangleCell);
output->SetCell(id++, cell);
break;
}
default:
{
itkExceptionMacro(<< "Unknown cell type");
}
}
}
}
template< typename TOutputMesh, typename ConvertPointPixelTraits, typename ConvertCellPixelTraits >
void
MeshFileReader< TOutputMesh, ConvertPointPixelTraits, ConvertCellPixelTraits >
::ReadPointData()
{
typename TOutputMesh::Pointer output = this->GetOutput();
char * inputPointDataBuffer = 0;
OutputPointPixelType *outputPointDataBuffer = new OutputPointPixelType[m_MeshIO->GetNumberOfPointPixels()];
try
{
if ( ( m_MeshIO->GetPointPixelComponentType() !=
MeshIOBase::MapComponentType< typename ConvertPointPixelTraits::ComponentType >::CType )
|| ( m_MeshIO->GetNumberOfPointPixelComponents() != ConvertPointPixelTraits::GetNumberOfComponents() ) )
{
// the point pixel types don't match a type conversion needs to be
// performed
itkDebugMacro( << "Buffer conversion required from: "
<< m_MeshIO->GetComponentTypeAsString( m_MeshIO->GetPointPixelComponentType() )
<< " to: "
<< m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< typename ConvertPointPixelTraits::
ComponentType >::CType)
<< "ConvertPointPixelTraits::NumberOfComponents "
<< ConvertPointPixelTraits::GetNumberOfComponents()
<< " m_MeshIO->NumberOfComponents "
<< m_MeshIO->GetNumberOfPointPixelComponents() );
inputPointDataBuffer =
new char[m_MeshIO->GetNumberOfPointPixelComponents() * m_MeshIO->GetComponentSize( m_MeshIO->GetPointPixelComponentType() )
* m_MeshIO->GetNumberOfPointPixels()];
m_MeshIO->ReadPointData( static_cast< void * >( inputPointDataBuffer ) );
this->ConvertPointPixelBuffer( static_cast< void * >( inputPointDataBuffer ), outputPointDataBuffer, m_MeshIO->GetNumberOfPointPixels() );
}
else
{
itkDebugMacro(<< "No buffer conversion required.");
m_MeshIO->ReadPointData( static_cast< void * >( outputPointDataBuffer ) );
}
}
catch ( ... )
{
// if an exception is thrown catch it
// clean up
delete[] inputPointDataBuffer;
inputPointDataBuffer = 0;
delete[] outputPointDataBuffer;
outputPointDataBuffer = 0;
// then rethrow
throw;
}
// clean up
delete[] inputPointDataBuffer;
inputPointDataBuffer = 0;
for ( OutputPointIdentifier id = 0; id < m_MeshIO->GetNumberOfPointPixels(); id++ )
{
output->SetPointData(id, outputPointDataBuffer[id]);
}
delete[] outputPointDataBuffer;
outputPointDataBuffer = 0;
}
template< typename TOutputMesh, typename ConvertPointPixelTraits, typename ConvertCellPixelTraits >
void
MeshFileReader< TOutputMesh, ConvertPointPixelTraits, ConvertCellPixelTraits >
::ReadCellData()
{
typename TOutputMesh::Pointer output = this->GetOutput();
char * inputCellDataBuffer = 0;
OutputCellPixelType *outputCellDataBuffer = new OutputCellPixelType[m_MeshIO->GetNumberOfCellPixels()];
try
{
if ( ( m_MeshIO->GetCellPixelComponentType() !=
MeshIOBase::MapComponentType< typename ConvertCellPixelTraits::ComponentType >::CType )
|| ( m_MeshIO->GetNumberOfCellPixelComponents() != ConvertCellPixelTraits::GetNumberOfComponents() ) )
{
// the cell pixel types don't match a type conversion needs to be
// performed
itkDebugMacro( << "Buffer conversion required from: "
<< m_MeshIO->GetComponentTypeAsString( m_MeshIO->GetCellPixelComponentType() )
<< " to: "
<< m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< typename ConvertCellPixelTraits::ComponentType >
::CType)
<< "ConvertCellPixelTraits::NumberOfComponents "
<< ConvertCellPixelTraits::GetNumberOfComponents()
<< " m_MeshIO->NumberOfComponents "
<< m_MeshIO->GetNumberOfCellPixelComponents() );
inputCellDataBuffer =
new char[m_MeshIO->GetNumberOfCellPixelComponents() * m_MeshIO->GetComponentSize( m_MeshIO->GetCellPixelComponentType() )
* m_MeshIO->GetNumberOfCellPixels()];
m_MeshIO->ReadCellData( static_cast< void * >( inputCellDataBuffer ) );
this->ConvertCellPixelBuffer( static_cast< void * >( inputCellDataBuffer ), outputCellDataBuffer, m_MeshIO->GetNumberOfCellPixels() );
}
else
{
itkDebugMacro(<< "No buffer conversion required.");
m_MeshIO->ReadCellData( static_cast< void * >( outputCellDataBuffer ) );
}
}
catch ( ... )
{
// if an exception is thrown catch it
// clean up
delete[] inputCellDataBuffer;
inputCellDataBuffer = 0;
delete[] outputCellDataBuffer;
outputCellDataBuffer = 0;
// then rethrow
throw;
}
// clean up
delete[] inputCellDataBuffer;
inputCellDataBuffer = 0;
for ( OutputCellIdentifier id = 0; id < m_MeshIO->GetNumberOfCellPixels(); id++ )
{
output->SetCellData(id, outputCellDataBuffer[id]);
}
delete[] outputCellDataBuffer;
outputCellDataBuffer = 0;
}
template< typename TOutputMesh, typename ConvertPointPixelTraits, typename ConvertCellPixelTraits >
void
MeshFileReader< TOutputMesh, ConvertPointPixelTraits, ConvertCellPixelTraits >
::GenerateOutputInformation()
{
if ( m_FileName == "" )
{
throw MeshFileReaderException(__FILE__, __LINE__, "FileName must be specified", ITK_LOCATION);
}
try
{
m_ExceptionMessage = "";
this->TestFileExistanceAndReadability();
}
catch ( itk::ExceptionObject & err )
{
m_ExceptionMessage = err.GetDescription();
}
if ( m_UserSpecifiedMeshIO == false ) // try creating via factory
{
m_MeshIO = MeshIOFactory::CreateMeshIO(m_FileName.c_str(), MeshIOFactory::ReadMode);
}
if ( m_MeshIO.IsNull() )
{
std::ostringstream msg;
msg << " Could not create IO object for file " << m_FileName.c_str() << std::endl;
if ( m_ExceptionMessage.size() )
{
msg << m_ExceptionMessage;
}
else
{
msg << " Tried to create one of the following:" << std::endl;
std::list< LightObject::Pointer > allobjects = ObjectFactoryBase::CreateAllInstance("itkMeshIOBase");
for ( std::list< LightObject::Pointer >::iterator it = allobjects.begin(); it != allobjects.end(); ++it )
{
MeshIOBase *io = dynamic_cast< MeshIOBase * >( it->GetPointer() );
msg << " " << io->GetNameOfClass() << std::endl;
}
msg << " You probably failed to set a file suffix, or" << std::endl;
msg << " set the suffix to an unsupported type." << std::endl;
}
MeshFileReaderException e(__FILE__, __LINE__, msg.str().c_str(), ITK_LOCATION);
throw e;
return;
}
}
template< typename TOutputMesh, typename ConvertPointPixelTraits, typename ConvertCellPixelTraits >
void
MeshFileReader< TOutputMesh, ConvertPointPixelTraits, ConvertCellPixelTraits >
::GenerateData()
{
typename TOutputMesh::Pointer output = this->GetOutput();
// Test if the file exists and if it can be opened.
// An exception will be thrown otherwise, since we can't
// successfully read the file. We catch the exception because some
// MeshIO's may not actually open a file. Still
// reports file error if no MeshIO is loaded.
output->Initialize();
output->SetBufferedRegion( output->GetRequestedRegion() );
// Test existence and readability of input file
try
{
m_ExceptionMessage = "";
this->TestFileExistanceAndReadability();
}
catch ( itk::ExceptionObject & err )
{
m_ExceptionMessage = err.GetDescription();
}
// Tell the MeshIO to read the file
m_MeshIO->SetFileName( m_FileName.c_str() );
// Get mesh information
m_MeshIO->ReadMeshInformation();
// Read points
if ( m_MeshIO->GetUpdatePoints() )
{
switch ( m_MeshIO->GetPointComponentType() )
{
case MeshIOBase::CHAR:
{
char *pointsBuffer = new char[m_MeshIO->GetNumberOfPoints() * OutputPointDimension];
m_MeshIO->ReadPoints( static_cast< void * >( pointsBuffer ) );
ReadPoints(pointsBuffer);
delete[] pointsBuffer;
break;
}
case MeshIOBase::UCHAR:
{
unsigned char *pointsBuffer = new unsigned char[m_MeshIO->GetNumberOfPoints() * OutputPointDimension];
m_MeshIO->ReadPoints( static_cast< void * >( pointsBuffer ) );
ReadPoints(pointsBuffer);
delete[] pointsBuffer;
break;
}
case MeshIOBase::SHORT:
{
short *pointsBuffer = new short[m_MeshIO->GetNumberOfPoints() * OutputPointDimension];
m_MeshIO->ReadPoints( static_cast< void * >( pointsBuffer ) );
ReadPoints(pointsBuffer);
delete[] pointsBuffer;
break;
}
case MeshIOBase::USHORT:
{
unsigned short *pointsBuffer = new unsigned short[m_MeshIO->GetNumberOfPoints() * OutputPointDimension];
m_MeshIO->ReadPoints( static_cast< void * >( pointsBuffer ) );
ReadPoints(pointsBuffer);
delete[] pointsBuffer;
break;
}
case MeshIOBase::INT:
{
int *pointsBuffer = new int[m_MeshIO->GetNumberOfPoints() * OutputPointDimension];
m_MeshIO->ReadPoints( static_cast< void * >( pointsBuffer ) );
ReadPoints(pointsBuffer);
delete[] pointsBuffer;
break;
}
case MeshIOBase::UINT:
{
unsigned int *pointsBuffer = new unsigned int[m_MeshIO->GetNumberOfPoints() * OutputPointDimension];
m_MeshIO->ReadPoints( static_cast< void * >( pointsBuffer ) );
ReadPoints(pointsBuffer);
delete[] pointsBuffer;
break;
}
case MeshIOBase::LONG:
{
long *pointsBuffer = new long[m_MeshIO->GetNumberOfPoints() * OutputPointDimension];
m_MeshIO->ReadPoints( static_cast< void * >( pointsBuffer ) );
ReadPoints(pointsBuffer);
delete[] pointsBuffer;
break;
}
case MeshIOBase::ULONG:
{
unsigned long *pointsBuffer = new unsigned long[m_MeshIO->GetNumberOfPoints() * OutputPointDimension];
m_MeshIO->ReadPoints( static_cast< void * >( pointsBuffer ) );
ReadPoints(pointsBuffer);
delete[] pointsBuffer;
break;
}
case MeshIOBase::LONGLONG:
{
long long *pointsBuffer = new long long[m_MeshIO->GetNumberOfPoints() * OutputPointDimension];
m_MeshIO->ReadPoints( static_cast< void * >( pointsBuffer ) );
ReadPoints(pointsBuffer);
delete[] pointsBuffer;
break;
}
case MeshIOBase::ULONGLONG:
{
unsigned long long *pointsBuffer = new unsigned long long[m_MeshIO->GetNumberOfPoints() * OutputPointDimension];
m_MeshIO->ReadPoints( static_cast< void * >( pointsBuffer ) );
ReadPoints(pointsBuffer);
delete[] pointsBuffer;
break;
}
case MeshIOBase::FLOAT:
{
float *pointsBuffer = new float[m_MeshIO->GetNumberOfPoints() * OutputPointDimension];
m_MeshIO->ReadPoints( static_cast< void * >( pointsBuffer ) );
ReadPoints(pointsBuffer);
delete[] pointsBuffer;
break;
}
case MeshIOBase::DOUBLE:
{
double *pointsBuffer = new double[m_MeshIO->GetNumberOfPoints() * OutputPointDimension];
m_MeshIO->ReadPoints( static_cast< void * >( pointsBuffer ) );
ReadPoints(pointsBuffer);
delete[] pointsBuffer;
break;
}
case MeshIOBase::LDOUBLE:
{
long double *pointsBuffer = new long double[m_MeshIO->GetNumberOfPoints() * OutputPointDimension];
m_MeshIO->ReadPoints( static_cast< void * >( pointsBuffer ) );
ReadPoints(pointsBuffer);
delete[] pointsBuffer;
break;
}
case MeshIOBase::UNKNOWNCOMPONENTTYPE:
default:
{
itkExceptionMacro(<< "Unknown point component type");
}
}
}
// Read cells
if ( m_MeshIO->GetUpdateCells() )
{
switch ( m_MeshIO->GetCellComponentType() )
{
case MeshIOBase::CHAR:
{
char *cellsBuffer = new char[m_MeshIO->GetCellBufferSize()];
m_MeshIO->ReadCells( static_cast< void * >( cellsBuffer ) );
ReadCells(cellsBuffer);
delete[] cellsBuffer;
break;
}
case MeshIOBase::UCHAR:
{
unsigned char *cellsBuffer = new unsigned char[m_MeshIO->GetCellBufferSize()];
m_MeshIO->ReadCells( static_cast< void * >( cellsBuffer ) );
ReadCells(cellsBuffer);
delete[] cellsBuffer;
break;
}
case MeshIOBase::SHORT:
{
short *cellsBuffer = new short[m_MeshIO->GetCellBufferSize()];
m_MeshIO->ReadCells( static_cast< void * >( cellsBuffer ) );
ReadCells(cellsBuffer);
delete[] cellsBuffer;
break;
}
case MeshIOBase::USHORT:
{
unsigned short *cellsBuffer = new unsigned short[m_MeshIO->GetCellBufferSize()];
m_MeshIO->ReadCells( static_cast< void * >( cellsBuffer ) );
ReadCells(cellsBuffer);
delete[] cellsBuffer;
break;
}
case MeshIOBase::INT:
{
int *cellsBuffer = new int[m_MeshIO->GetCellBufferSize()];
m_MeshIO->ReadCells( static_cast< void * >( cellsBuffer ) );
ReadCells(cellsBuffer);
delete[] cellsBuffer;
break;
}
case MeshIOBase::UINT:
{
unsigned int *cellsBuffer = new unsigned int[m_MeshIO->GetCellBufferSize()];
m_MeshIO->ReadCells( static_cast< void * >( cellsBuffer ) );
ReadCells(cellsBuffer);
delete[] cellsBuffer;
break;
}
case MeshIOBase::LONG:
{
long *cellsBuffer = new long[m_MeshIO->GetCellBufferSize()];
m_MeshIO->ReadCells( static_cast< void * >( cellsBuffer ) );
ReadCells(cellsBuffer);
delete[] cellsBuffer;
break;
}
case MeshIOBase::ULONG:
{
unsigned long *cellsBuffer = new unsigned long[m_MeshIO->GetCellBufferSize()];
m_MeshIO->ReadCells( static_cast< void * >( cellsBuffer ) );
ReadCells(cellsBuffer);
delete[] cellsBuffer;
break;
}
case MeshIOBase::LONGLONG:
{
long long *cellsBuffer = new long long[m_MeshIO->GetCellBufferSize()];
m_MeshIO->ReadCells( static_cast< void * >( cellsBuffer ) );
ReadCells(cellsBuffer);
delete[] cellsBuffer;
break;
}
case MeshIOBase::ULONGLONG:
{
unsigned long long *cellsBuffer = new unsigned long long[m_MeshIO->GetCellBufferSize()];
m_MeshIO->ReadCells( static_cast< void * >( cellsBuffer ) );
ReadCells(cellsBuffer);
delete[] cellsBuffer;
break;
}
case MeshIOBase::FLOAT:
{
float *cellsBuffer = new float[m_MeshIO->GetCellBufferSize()];
m_MeshIO->ReadCells( static_cast< void * >( cellsBuffer ) );
ReadCells(cellsBuffer);
delete[] cellsBuffer;
break;
}
case MeshIOBase::DOUBLE:
{
double *cellsBuffer = new double[m_MeshIO->GetCellBufferSize()];
m_MeshIO->ReadCells( static_cast< void * >( cellsBuffer ) );
ReadCells(cellsBuffer);
delete[] cellsBuffer;
break;
}
case MeshIOBase::LDOUBLE:
{
long double *cellsBuffer = new long double[m_MeshIO->GetCellBufferSize()];
m_MeshIO->ReadCells( static_cast< void * >( cellsBuffer ) );
ReadCells(cellsBuffer);
delete[] cellsBuffer;
break;
}
case MeshIOBase::UNKNOWNCOMPONENTTYPE:
default:
{
itkExceptionMacro(<< "Unknown cell component type");
}
}
}
// Read Point Data
if ( m_MeshIO->GetUpdatePointData() )
{
ReadPointData();
}
// Read Cell Data
if ( m_MeshIO->GetUpdateCellData() )
{
ReadCellData();
}
}
template< typename TOutputMesh, typename ConvertPointPixelTraits, typename ConvertCellPixelTraits >
template< typename T >
void
MeshFileReader< TOutputMesh, ConvertPointPixelTraits, ConvertCellPixelTraits >
::ConvertPointPixelBuffer(void *inputData, T *outputData, size_t numberOfPixels)
{
// TODO:
// Pass down the PixelType (RGB, VECTOR, etc.) so that any vector to
// scalar conversion be type specific. i.e. RGB to scalar would use
// a formula to convert to luminance, VECTOR to scalar would use
// vector magnitude.
// Create a macro as this code is a bit lengthy and repetitive
// if the MeshIO pixel type is typeid(type) then use the ConvertPixelBuffer
// class to convert the data block to TOutputMesh's pixel type
// see DefaultConvertPixelTraits and ConvertPixelBuffer
#define ITK_CONVERT_POINT_PIXEL_BUFFER_IF_BLOCK(CType, type) \
else if ( m_MeshIO->GetPointPixelComponentType() == CType ) \
{ \
ConvertPixelBuffer< \
type, \
OutputPointPixelType, \
ConvertPointPixelTraits \
> \
::Convert( \
static_cast< type * >( inputData ), \
m_MeshIO->GetNumberOfPointPixelComponents(), \
outputData, \
numberOfPixels); \
}
if ( 0 )
{}
ITK_CONVERT_POINT_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::UCHAR, unsigned char)
ITK_CONVERT_POINT_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::CHAR, char)
ITK_CONVERT_POINT_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::USHORT, unsigned short)
ITK_CONVERT_POINT_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::SHORT, short)
ITK_CONVERT_POINT_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::UINT, unsigned int)
ITK_CONVERT_POINT_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::INT, int)
ITK_CONVERT_POINT_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::ULONG, unsigned long)
ITK_CONVERT_POINT_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::LONG, long)
ITK_CONVERT_POINT_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::ULONGLONG, unsigned long long)
ITK_CONVERT_POINT_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::LONGLONG, long long)
ITK_CONVERT_POINT_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::FLOAT, float)
ITK_CONVERT_POINT_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::DOUBLE, double)
ITK_CONVERT_POINT_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::LDOUBLE, long double)
else
{
MeshFileReaderException e(__FILE__, __LINE__);
std::ostringstream msg;
msg << "Couldn't convert component type: "
<< std::endl << " "
<< m_MeshIO->GetComponentTypeAsString( m_MeshIO->GetPointPixelComponentType() )
<< std::endl << "to one of: "
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< unsigned char >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< char >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< unsigned short >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< short >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< unsigned int >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< int >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< unsigned long >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< long >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< unsigned long long >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< long long >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< float >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< double >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< long double >::CType)
<< std::endl;
e.SetDescription( msg.str().c_str() );
e.SetLocation(ITK_LOCATION);
throw e;
return;
}
#undef ITK_CONVERT_POINT_PIXEL_BUFFER_IF_BLOCK
}
template< typename TOutputMesh, typename ConvertPointPixelTraits, typename ConvertCellPixelTraits >
template< typename T >
void
MeshFileReader< TOutputMesh, ConvertPointPixelTraits, ConvertCellPixelTraits >
::ConvertCellPixelBuffer(void *inputData, T *outputData, size_t numberOfPixels)
{
// TODO:
// Pass down the PixelType (RGB, VECTOR, etc.) so that any vector to
// scalar conversion be type specific. i.e. RGB to scalar would use
// a formula to convert to luminance, VECTOR to scalar would use
// vector magnitude.
// Create a macro as this code is a bit lengthy and repetitive
// if the MeshIO pixel type is typeid(type) then use the ConvertPixelBuffer
// class to convert the data block to TOutputMesh's pixel type
// see DefaultConvertPixelTraits and ConvertPixelBuffer
#define ITK_CONVERT_CELL_PIXEL_BUFFER_IF_BLOCK(CType, type) \
else if ( m_MeshIO->GetCellPixelComponentType() == CType ) \
{ \
ConvertPixelBuffer< \
type, \
OutputCellPixelType, \
ConvertCellPixelTraits \
> \
::Convert( \
static_cast< type * >( inputData ), \
m_MeshIO->GetNumberOfCellPixelComponents(), \
outputData, \
numberOfPixels); \
}
if ( 0 )
{}
ITK_CONVERT_CELL_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::UCHAR, unsigned char)
ITK_CONVERT_CELL_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::CHAR, char)
ITK_CONVERT_CELL_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::USHORT, unsigned short)
ITK_CONVERT_CELL_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::SHORT, short)
ITK_CONVERT_CELL_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::UINT, unsigned int)
ITK_CONVERT_CELL_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::INT, int)
ITK_CONVERT_CELL_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::ULONG, unsigned long)
ITK_CONVERT_CELL_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::LONG, long)
ITK_CONVERT_CELL_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::ULONGLONG, unsigned long long)
ITK_CONVERT_CELL_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::LONGLONG, long long)
ITK_CONVERT_CELL_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::FLOAT, float)
ITK_CONVERT_CELL_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::DOUBLE, double)
ITK_CONVERT_CELL_PIXEL_BUFFER_IF_BLOCK(MeshIOBase::LDOUBLE, long double)
else
{
MeshFileReaderException e(__FILE__, __LINE__);
std::ostringstream msg;
msg << "Couldn't convert component type: "
<< std::endl << " "
<< m_MeshIO->GetComponentTypeAsString( m_MeshIO->GetCellPixelComponentType() )
<< std::endl << "to one of: "
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< unsigned char >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< char >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< unsigned short >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< short >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< unsigned int >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< int >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< unsigned long >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< long >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< unsigned long long >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< long long >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< float >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< double >::CType)
<< std::endl << " " << m_MeshIO->GetComponentTypeAsString(MeshIOBase::MapComponentType< long double >::CType)
<< std::endl;
e.SetDescription( msg.str().c_str() );
e.SetLocation(ITK_LOCATION);
throw e;
return;
}
#undef ITK_CONVERT_CELL_PIXEL_BUFFER_IF_BLOCK
}
} // namespace itk end
#endif
|