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
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* 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
*
* https://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 itkMeshIOBase_h
#define itkMeshIOBase_h
#include "ITKIOMeshBaseExport.h"
#include "itkByteSwapper.h"
#include "itkCellInterface.h"
#include "itkCovariantVector.h"
#include "itkDiffusionTensor3D.h"
#include "itkIntTypes.h"
#include "itkLightProcessObject.h"
#include "itkMatrix.h"
#include "itkRGBPixel.h"
#include "itkRGBAPixel.h"
#include "itkSymmetricSecondRankTensor.h"
#include "itkVariableLengthVector.h"
#include "itkVariableSizeMatrix.h"
#include "itkVector.h"
#include "itkNumberToString.h"
#include "itkCommonEnums.h"
#include "itkMakeUniqueForOverwrite.h"
#include <string>
#include <complex>
#include <fstream>
namespace itk
{
/**
* \class MeshIOBase
* \brief Abstract superclass defines mesh IO interface.
*
* MeshIOBase is a class that reads and/or writes Mesh / QuadEdgeMesh data
* of a particular format (such as PNG or raw binary). The
* MeshIOBase encapsulates both the reading and writing of data. The
* MeshIOBase is used by the MeshFileReader class (to read data)
* and the MeshFileWriter (to write data) into a single file.
* Normally the user does not directly
* manipulate this class other than to instantiate it, set the FileName,
* and assign it to a MeshFileReader or MeshFileWriter.
*
* A Pluggable factory pattern is used this allows different kinds of readers
* to be registered (even at run time) without having to modify the
* code in this class.
*
* \author Wanlin Zhu. University of New South Wales, Australia.
*
* \sa MeshFileWriter
* \sa MeshFileReader
*
* \ingroup IOFilters
* \ingroup ITKIOMeshBase
*
*/
class ITKIOMeshBase_EXPORT MeshIOBase : public LightProcessObject
{
public:
ITK_DISALLOW_COPY_AND_MOVE(MeshIOBase);
/** Standard class type aliases. */
using Self = MeshIOBase;
using Superclass = LightProcessObject;
using ConstPointer = SmartPointer<const Self>;
using Pointer = SmartPointer<Self>;
/** Type for the list of strings to be used for extensions. */
using ArrayOfExtensionsType = std::vector<std::string>;
/** Type for representing size of bytes, and or positions along a file */
using StreamOffsetType = std::streamoff;
using SizeValueType = IdentifierType;
/**
* \class UnknownType
* Used to return information when types are unknown.
* \ingroup ITKIOMeshBase
*/
class UnknownType
{};
/** \see LightObject::GetNameOfClass() */
itkOverrideGetNameOfClassMacro(MeshIOBase);
/** Set/Get the name of the file to be read. */
itkSetStringMacro(FileName);
itkGetStringMacro(FileName);
#if !defined(ITK_LEGACY_REMOVE)
/**Exposes enums values for backwards compatibility*/
static constexpr IOPixelEnum UNKNOWNPIXELTYPE = IOPixelEnum::UNKNOWNPIXELTYPE;
static constexpr IOPixelEnum SCALAR = IOPixelEnum::SCALAR;
static constexpr IOPixelEnum RGB = IOPixelEnum::RGB;
static constexpr IOPixelEnum RGBA = IOPixelEnum::RGBA;
static constexpr IOPixelEnum OFFSET = IOPixelEnum::OFFSET;
static constexpr IOPixelEnum VECTOR = IOPixelEnum::VECTOR;
static constexpr IOPixelEnum POINT = IOPixelEnum::POINT;
static constexpr IOPixelEnum COVARIANTVECTOR = IOPixelEnum::COVARIANTVECTOR;
static constexpr IOPixelEnum SYMMETRICSECONDRANKTENSOR = IOPixelEnum::SYMMETRICSECONDRANKTENSOR;
static constexpr IOPixelEnum DIFFUSIONTENSOR3D = IOPixelEnum::DIFFUSIONTENSOR3D;
static constexpr IOPixelEnum COMPLEX = IOPixelEnum::COMPLEX;
static constexpr IOPixelEnum FIXEDARRAY = IOPixelEnum::FIXEDARRAY;
static constexpr IOPixelEnum ARRAY = IOPixelEnum::ARRAY;
static constexpr IOPixelEnum MATRIX = IOPixelEnum::MATRIX;
static constexpr IOPixelEnum VARIABLELENGTHVECTOR = IOPixelEnum::VARIABLELENGTHVECTOR;
static constexpr IOPixelEnum VARIABLESIZEMATRIX = IOPixelEnum::VARIABLESIZEMATRIX;
#endif
#if !defined(ITK_LEGACY_REMOVE)
/**Exposes enums values for backwards compatibility*/
static constexpr IOComponentEnum UNKNOWNCOMPONENTTYPE = IOComponentEnum::UNKNOWNCOMPONENTTYPE;
static constexpr IOComponentEnum UCHAR = IOComponentEnum::UCHAR;
static constexpr IOComponentEnum CHAR = IOComponentEnum::CHAR;
static constexpr IOComponentEnum USHORT = IOComponentEnum::USHORT;
static constexpr IOComponentEnum SHORT = IOComponentEnum::SHORT;
static constexpr IOComponentEnum UINT = IOComponentEnum::UINT;
static constexpr IOComponentEnum INT = IOComponentEnum::INT;
static constexpr IOComponentEnum ULONG = IOComponentEnum::ULONG;
static constexpr IOComponentEnum LONG = IOComponentEnum::LONG;
static constexpr IOComponentEnum ULONGLONG = IOComponentEnum::ULONGLONG;
static constexpr IOComponentEnum LONGLONG = IOComponentEnum::LONGLONG;
static constexpr IOComponentEnum FLOAT = IOComponentEnum::FLOAT;
static constexpr IOComponentEnum DOUBLE = IOComponentEnum::DOUBLE;
static constexpr IOComponentEnum LDOUBLE = IOComponentEnum::LDOUBLE;
#endif
#if !defined(ITK_LEGACY_REMOVE)
/**Exposes enums values for backwards compatibility*/
static constexpr IOFileEnum ASCII = IOFileEnum::ASCII;
static constexpr IOFileEnum BINARY = IOFileEnum::BINARY;
static constexpr IOFileEnum TYPENOTAPPLICABLE = IOFileEnum::TYPENOTAPPLICABLE;
#endif
#if !defined(ITK_LEGACY_REMOVE)
/**Exposes enums values for backwards compatibility*/
static constexpr IOByteOrderEnum BigEndian = IOByteOrderEnum::BigEndian;
static constexpr IOByteOrderEnum LittleEndian = IOByteOrderEnum::LittleEndian;
static constexpr IOByteOrderEnum OrderNotApplicable = IOByteOrderEnum::OrderNotApplicable;
#endif
#if !defined(ITK_LEGACY_REMOVE)
/**Exposes enums values for backwards compatibility*/
static constexpr IOFileModeEnum ReadMode = IOFileModeEnum::ReadMode;
static constexpr IOFileModeEnum WriteMode = IOFileModeEnum::WriteMode;
#endif
#if !defined(ITK_LEGACY_REMOVE)
/**Exposes enums values for backwards compatibility*/
static constexpr CellGeometryEnum VERTEX_CELL = CellGeometryEnum::VERTEX_CELL;
static constexpr CellGeometryEnum LINE_CELL = CellGeometryEnum::LINE_CELL;
static constexpr CellGeometryEnum TRIANGLE_CELL = CellGeometryEnum::TRIANGLE_CELL;
static constexpr CellGeometryEnum QUADRILATERAL_CELL = CellGeometryEnum::QUADRILATERAL_CELL;
static constexpr CellGeometryEnum POLYGON_CELL = CellGeometryEnum::POLYGON_CELL;
static constexpr CellGeometryEnum TETRAHEDRON_CELL = CellGeometryEnum::TETRAHEDRON_CELL;
static constexpr CellGeometryEnum HEXAHEDRON_CELL = CellGeometryEnum::HEXAHEDRON_CELL;
static constexpr CellGeometryEnum QUADRATIC_EDGE_CELL = CellGeometryEnum::QUADRATIC_EDGE_CELL;
static constexpr CellGeometryEnum QUADRATIC_TRIANGLE_CELL = CellGeometryEnum::QUADRATIC_TRIANGLE_CELL;
static constexpr CellGeometryEnum LAST_ITK_CELL = CellGeometryEnum::LAST_ITK_CELL;
static constexpr CellGeometryEnum MAX_ITK_CELLS = CellGeometryEnum::MAX_ITK_CELLS;
#endif
/** Set/Get the type of the point/cell pixel. The PixelTypes provides context
* to the IO mechanisms for data conversions. PixelTypes can be
* SCALAR, RGB, RGBA, VECTOR, COVARIANTVECTOR, POINT, INDEX. If
* the PIXELTYPE is SCALAR, then the NumberOfComponents should be 1.
* Any other of PIXELTYPE will have more than one component. */
itkSetEnumMacro(PointPixelType, itk::CommonEnums::IOPixel);
itkGetEnumMacro(PointPixelType, itk::CommonEnums::IOPixel);
itkSetEnumMacro(CellPixelType, itk::CommonEnums::IOPixel);
itkGetEnumMacro(CellPixelType, itk::CommonEnums::IOPixel);
/** Set/Get the component type of the point, cell, point data and cell data.
This is always a native type. */
itkSetEnumMacro(PointComponentType, itk::CommonEnums::IOComponent);
itkGetEnumMacro(PointComponentType, itk::CommonEnums::IOComponent);
itkSetEnumMacro(CellComponentType, itk::CommonEnums::IOComponent);
itkGetEnumMacro(CellComponentType, itk::CommonEnums::IOComponent);
itkSetEnumMacro(PointPixelComponentType, itk::CommonEnums::IOComponent);
itkGetEnumMacro(PointPixelComponentType, itk::CommonEnums::IOComponent);
itkSetEnumMacro(CellPixelComponentType, itk::CommonEnums::IOComponent);
itkGetEnumMacro(CellPixelComponentType, itk::CommonEnums::IOComponent);
template <typename T>
struct MapComponentType
{
static constexpr IOComponentEnum CType = IOComponentEnum::UNKNOWNCOMPONENTTYPE;
};
template <typename T>
void
SetPixelType(const T & itkNotUsed(dummy), bool usePointPixel = true)
{
if (usePointPixel)
{
SetNumberOfPointPixelComponents(1);
SetPointPixelComponentType(MapComponentType<T>::CType);
SetPointPixelType(IOPixelEnum::SCALAR);
}
else
{
SetNumberOfCellPixelComponents(1);
SetCellPixelComponentType(MapComponentType<T>::CType);
SetCellPixelType(IOPixelEnum::SCALAR);
}
}
template <typename T>
void
SetPixelType(const RGBPixel<T> & itkNotUsed(dummy), bool usePointPixel = true)
{
if (usePointPixel)
{
SetNumberOfPointPixelComponents(3);
SetPointPixelComponentType(MapComponentType<T>::CType);
SetPointPixelType(IOPixelEnum::RGB);
}
else
{
SetNumberOfCellPixelComponents(3);
SetCellPixelComponentType(MapComponentType<T>::CType);
SetCellPixelType(IOPixelEnum::RGB);
}
}
template <typename T>
void
SetPixelType(const RGBAPixel<T> & itkNotUsed(dummy), bool usePointPixel = true)
{
if (usePointPixel)
{
SetNumberOfPointPixelComponents(4);
SetPointPixelComponentType(MapComponentType<T>::CType);
SetPointPixelType(IOPixelEnum::RGBA);
}
else
{
SetNumberOfCellPixelComponents(4);
SetCellPixelComponentType(MapComponentType<T>::CType);
SetCellPixelType(IOPixelEnum::RGBA);
}
}
template <typename T, unsigned int VLength>
void
SetPixelType(const Vector<T, VLength> & itkNotUsed(dummy), bool usePointPixel = true)
{
if (usePointPixel)
{
SetNumberOfPointPixelComponents(VLength);
SetPointPixelComponentType(MapComponentType<T>::CType);
SetPointPixelType(IOPixelEnum::VECTOR);
}
else
{
SetNumberOfCellPixelComponents(VLength);
SetCellPixelComponentType(MapComponentType<T>::CType);
SetCellPixelType(IOPixelEnum::VECTOR);
}
}
template <typename T, unsigned int VLength>
void
SetPixelType(const CovariantVector<T, VLength> & itkNotUsed(dummy), bool usePointPixel = true)
{
if (usePointPixel)
{
SetNumberOfPointPixelComponents(VLength);
SetPointPixelComponentType(MapComponentType<T>::CType);
SetPointPixelType(IOPixelEnum::COVARIANTVECTOR);
}
else
{
SetNumberOfCellPixelComponents(VLength);
SetCellPixelComponentType(MapComponentType<T>::CType);
SetCellPixelType(IOPixelEnum::COVARIANTVECTOR);
}
}
template <typename T, unsigned int VLength>
void
SetPixelType(const FixedArray<T, VLength> & itkNotUsed(dummy), bool usePointPixel = true)
{
if (usePointPixel)
{
SetNumberOfPointPixelComponents(VLength);
SetPointPixelComponentType(MapComponentType<T>::CType);
SetPointPixelType(IOPixelEnum::FIXEDARRAY);
}
else
{
SetNumberOfCellPixelComponents(VLength);
SetCellPixelComponentType(MapComponentType<T>::CType);
SetCellPixelType(IOPixelEnum::FIXEDARRAY);
}
}
template <typename T, unsigned int VLength>
void
SetPixelType(const SymmetricSecondRankTensor<T, VLength> itkNotUsed(dummy), bool usePointPixel = true)
{
if (usePointPixel)
{
SetNumberOfPointPixelComponents(VLength * (VLength + 1) / 2);
SetPointPixelComponentType(MapComponentType<T>::CType);
SetPointPixelType(IOPixelEnum::SYMMETRICSECONDRANKTENSOR);
}
else
{
SetNumberOfCellPixelComponents(VLength * (VLength + 1) / 2);
SetCellPixelComponentType(MapComponentType<T>::CType);
SetCellPixelType(IOPixelEnum::SYMMETRICSECONDRANKTENSOR);
}
}
template <typename T>
void
SetPixelType(const DiffusionTensor3D<T> & itkNotUsed(dummy), bool usePointPixel = true)
{
if (usePointPixel)
{
SetNumberOfPointPixelComponents(6);
SetPointPixelComponentType(MapComponentType<T>::CType);
SetPointPixelType(IOPixelEnum::DIFFUSIONTENSOR3D);
}
else
{
SetNumberOfCellPixelComponents(6);
SetCellPixelComponentType(MapComponentType<T>::CType);
SetCellPixelType(IOPixelEnum::DIFFUSIONTENSOR3D);
}
}
template <typename T, unsigned int VRows, unsigned int VColumns>
void
SetPixelType(const Matrix<T, VRows, VColumns> & itkNotUsed(dummy), bool usePointPixel = true)
{
if (usePointPixel)
{
SetNumberOfPointPixelComponents(VRows * VColumns);
SetPointPixelComponentType(MapComponentType<T>::CType);
SetPointPixelType(IOPixelEnum::MATRIX);
}
else
{
SetNumberOfCellPixelComponents(VRows * VColumns);
SetCellPixelComponentType(MapComponentType<T>::CType);
SetCellPixelType(IOPixelEnum::MATRIX);
}
}
template <typename T>
void
SetPixelType(const std::complex<T> & itkNotUsed(dummy), bool usePointPixel = true)
{
if (usePointPixel)
{
SetNumberOfPointPixelComponents(2);
SetPointPixelComponentType(MapComponentType<T>::CType);
SetPointPixelType(IOPixelEnum::COMPLEX);
}
else
{
SetNumberOfCellPixelComponents(2);
SetCellPixelComponentType(MapComponentType<T>::CType);
SetCellPixelType(IOPixelEnum::COMPLEX);
}
}
template <typename T>
void
SetPixelType(const Array<T> & array, bool usePointPixel = true)
{
if (usePointPixel)
{
SetNumberOfPointPixelComponents(array.Size());
SetPointPixelComponentType(MapComponentType<T>::CType);
SetPointPixelType(IOPixelEnum::ARRAY);
}
else
{
SetNumberOfCellPixelComponents(array.Size());
SetCellPixelComponentType(MapComponentType<T>::CType);
SetCellPixelType(IOPixelEnum::ARRAY);
}
}
template <typename T>
void
SetPixelType(const VariableLengthVector<T> & vector, bool usePointPixel = true)
{
if (usePointPixel)
{
SetNumberOfPointPixelComponents(vector.Size());
SetPointPixelComponentType(MapComponentType<T>::CType);
SetPointPixelType(IOPixelEnum::VARIABLELENGTHVECTOR);
}
else
{
SetNumberOfCellPixelComponents(vector.Size());
SetCellPixelComponentType(MapComponentType<T>::CType);
SetCellPixelType(IOPixelEnum::VARIABLELENGTHVECTOR);
}
}
template <typename T>
void
SetPixelType(const VariableSizeMatrix<T> & matrix, bool usePointPixel = true)
{
if (usePointPixel)
{
SetNumberOfPointPixelComponents(matrix.Rows() * matrix.Cols());
SetPointPixelComponentType(MapComponentType<T>::CType);
SetPointPixelType(IOPixelEnum::VARIABLESIZEMATRIX);
}
else
{
SetNumberOfCellPixelComponents(matrix.Rows() * matrix.Cols());
SetCellPixelComponentType(MapComponentType<T>::CType);
SetCellPixelType(IOPixelEnum::VARIABLESIZEMATRIX);
}
}
/** Set/Get the number of components per pixel in the mesh. This may
* be set by the reading process. For SCALAR pixel types,
* NumberOfComponents will be 1. For other pixel types,
* NumberOfComponents will be greater than or equal to one. */
itkSetMacro(NumberOfPointPixelComponents, unsigned int);
itkGetConstMacro(NumberOfPointPixelComponents, unsigned int);
itkSetMacro(NumberOfCellPixelComponents, unsigned int);
itkGetConstMacro(NumberOfCellPixelComponents, unsigned int);
itkSetMacro(PointDimension, unsigned int);
itkGetConstMacro(PointDimension, unsigned int);
itkSetMacro(NumberOfPoints, SizeValueType);
itkGetConstMacro(NumberOfPoints, SizeValueType);
itkSetMacro(NumberOfCells, SizeValueType);
itkGetConstMacro(NumberOfCells, SizeValueType);
itkSetMacro(NumberOfPointPixels, SizeValueType);
itkGetConstMacro(NumberOfPointPixels, SizeValueType);
itkSetMacro(NumberOfCellPixels, SizeValueType);
itkGetConstMacro(NumberOfCellPixels, SizeValueType);
itkSetMacro(CellBufferSize, SizeValueType);
itkGetConstMacro(CellBufferSize, SizeValueType);
itkSetMacro(UpdatePoints, bool);
itkGetConstMacro(UpdatePoints, bool);
itkBooleanMacro(UpdatePoints);
itkSetMacro(UpdateCells, bool);
itkGetConstMacro(UpdateCells, bool);
itkBooleanMacro(UpdateCells);
itkSetMacro(UpdatePointData, bool);
itkGetConstMacro(UpdatePointData, bool);
itkBooleanMacro(UpdatePointData);
itkSetMacro(UpdateCellData, bool);
itkGetConstMacro(UpdateCellData, bool);
itkBooleanMacro(UpdateCellData);
unsigned int
GetComponentSize(IOComponentEnum componentType) const;
/** Convenience method returns the IOComponentEnum as a string. This can be
* used for writing output files. */
std::string GetComponentTypeAsString(IOComponentEnum) const;
/** Convenience method returns the IOPixelEnum as a string. This can be
* used for writing output files. */
std::string GetPixelTypeAsString(IOPixelEnum) const;
/** These methods control whether the file is written binary or ASCII.
* Many file formats (i.e., subclasses) ignore this flag. */
itkSetEnumMacro(FileType, IOFileEnum);
itkGetEnumMacro(FileType, IOFileEnum);
void
SetFileTypeToASCII()
{
this->SetFileType(IOFileEnum::ASCII);
}
void
SetFileTypeToBinary()
{
this->SetFileType(IOFileEnum::BINARY);
}
/** These methods indicate the byte ordering of the file you are
* trying to read in. These methods will then either swap or not
* swap the bytes depending on the byte ordering of the machine it
* is being run on. For example, reading in a BigEndian file on a
* BigEndian machine will result in no swapping. Trying to read the
* same file on a LittleEndian machine will result in swapping.
* Note: most UNIX machines are BigEndian while PC's and VAX's are
* LittleEndian. So if the file you are reading in was generated on
* a VAX or PC, SetByteOrderToLittleEndian() otherwise
* SetByteOrderToBigEndian(). Some MeshIOBase subclasses
* ignore these methods. */
itkSetEnumMacro(ByteOrder, IOByteOrderEnum);
itkGetEnumMacro(ByteOrder, IOByteOrderEnum);
void
SetByteOrderToBigEndian()
{
this->SetByteOrder(IOByteOrderEnum::BigEndian);
}
void
SetByteOrderToLittleEndian()
{
this->SetByteOrder(IOByteOrderEnum::LittleEndian);
}
/** Set/Get a boolean to use the compression or not. */
itkSetMacro(UseCompression, bool);
itkGetConstMacro(UseCompression, bool);
itkBooleanMacro(UseCompression);
/** Convenience method returns the IOFileEnum as a string. This can be
* used for writing output files. */
std::string GetFileTypeAsString(IOFileEnum) const;
/** Convenience method returns the IOByteOrderEnum as a string. This can be
* used for writing output files. */
std::string GetByteOrderAsString(IOByteOrderEnum) const;
/*-------- This part of the interfaces deals with reading data ----- */
/** Determine the file type. Returns true if this MeshIO can read the
* file specified. */
virtual bool
CanReadFile(const char *) = 0;
/** Determine the required information and whether need to ReadPoints,
ReadCells, ReadPointData and ReadCellData */
virtual void
ReadMeshInformation() = 0;
/** Reads the data from disk into the memory buffer provided. */
virtual void
ReadPoints(void * buffer) = 0;
virtual void
ReadCells(void * buffer) = 0;
virtual void
ReadPointData(void * buffer) = 0;
virtual void
ReadCellData(void * buffer) = 0;
/*-------- This part of the interfaces deals with writing data ----- */
/** Writes the data to disk from the memory buffer provided. Make sure
* that the IORegions has been set properly. */
virtual bool
CanWriteFile(const char *) = 0;
virtual void
WriteMeshInformation() = 0;
virtual void
WritePoints(void * buffer) = 0;
virtual void
WriteCells(void * buffer) = 0;
virtual void
WritePointData(void * buffer) = 0;
virtual void
WriteCellData(void * buffer) = 0;
virtual void
Write() = 0;
/** This method returns an array with the list of filename extensions
* supported for reading by this MeshIO class. This is intended to
* facilitate GUI and application level integration.
*/
const ArrayOfExtensionsType &
GetSupportedReadExtensions() const;
/** This method returns an array with the list of filename extensions
* supported for writing by this MeshIO class. This is intended to
* facilitate GUI and application level integration.
*/
const ArrayOfExtensionsType &
GetSupportedWriteExtensions() const;
protected:
MeshIOBase();
~MeshIOBase() override = default;
void
PrintSelf(std::ostream & os, Indent indent) const override;
/** Insert an extension to the list of supported extensions for reading. */
void
AddSupportedReadExtension(const char * extension);
/** Insert an extension to the list of supported extensions for writing. */
void
AddSupportedWriteExtension(const char * extension);
/** Read data from input file stream to buffer with ascii style */
template <typename T>
void
ReadBufferAsAscii(T * buffer, std::ifstream & inputFile, SizeValueType numberOfComponents)
{
for (SizeValueType i = 0; i < numberOfComponents; ++i)
{
inputFile >> buffer[i];
}
}
/** Read data from input file to buffer with binary style */
template <typename T>
void
ReadBufferAsBinary(T * buffer, std::ifstream & inputFile, SizeValueType numberOfComponents)
{
inputFile.read(reinterpret_cast<char *>(buffer), numberOfComponents * sizeof(T));
if (m_ByteOrder == IOByteOrderEnum::BigEndian)
{
if (itk::ByteSwapper<T>::SystemIsLittleEndian())
{
itk::ByteSwapper<T>::SwapRangeFromSystemToBigEndian(buffer, numberOfComponents);
}
}
else if (m_ByteOrder == IOByteOrderEnum::LittleEndian)
{
if (itk::ByteSwapper<T>::SystemIsBigEndian())
{
itk::ByteSwapper<T>::SwapRangeFromSystemToLittleEndian(buffer, numberOfComponents);
}
}
}
/** Write buffer to output file stream with ascii style */
template <typename T>
void
WriteBufferAsAscii(T * buffer,
std::ofstream & outputFile,
SizeValueType numberOfLines,
SizeValueType numberOfComponents)
{
for (SizeValueType ii = 0; ii < numberOfLines; ++ii)
{
for (SizeValueType jj = 0; jj < numberOfComponents; ++jj)
{
outputFile << ConvertNumberToString(buffer[ii * numberOfComponents + jj]) << " ";
}
outputFile << '\n';
}
}
/** Write buffer to output file stream with binary style */
template <typename TOutput, typename TInput>
void
WriteBufferAsBinary(TInput * buffer, std::ofstream & outputFile, SizeValueType numberOfComponents)
{
if (typeid(TInput) == typeid(TOutput))
{
if (m_ByteOrder == IOByteOrderEnum::BigEndian && itk::ByteSwapper<TInput>::SystemIsLittleEndian())
{
itk::ByteSwapper<TInput>::SwapRangeFromSystemToBigEndian(buffer, numberOfComponents);
}
else if (m_ByteOrder == IOByteOrderEnum::LittleEndian && itk::ByteSwapper<TInput>::SystemIsBigEndian())
{
itk::ByteSwapper<TInput>::SwapRangeFromSystemToLittleEndian(buffer, numberOfComponents);
}
outputFile.write(reinterpret_cast<char *>(buffer), numberOfComponents);
}
else
{
const auto data = make_unique_for_overwrite<TOutput[]>(numberOfComponents);
for (SizeValueType ii = 0; ii < numberOfComponents; ++ii)
{
data[ii] = static_cast<TOutput>(buffer[ii]);
}
if (m_ByteOrder == IOByteOrderEnum::BigEndian && itk::ByteSwapper<TOutput>::SystemIsLittleEndian())
{
itk::ByteSwapper<TOutput>::SwapRangeFromSystemToBigEndian(data.get(), numberOfComponents);
}
else if (m_ByteOrder == IOByteOrderEnum::LittleEndian && itk::ByteSwapper<TOutput>::SystemIsBigEndian())
{
itk::ByteSwapper<TOutput>::SwapRangeFromSystemToLittleEndian(data.get(), numberOfComponents);
}
outputFile.write(reinterpret_cast<char *>(data.get()), numberOfComponents);
}
}
/** Read cells from a data buffer, used when writing cells. This function
write all kind of cells as it is stored in cells container. It is used when
cells container have only one kind of cells */
template <typename TInput, typename TOutput>
void
ReadCellsBuffer(TInput * input, TOutput * output)
{
if (input && output)
{
SizeValueType inputIndex{};
SizeValueType outputIndex{};
for (SizeValueType ii = 0; ii < m_NumberOfCells; ++ii)
{
++inputIndex; // ignore the cell type
auto numberOfPoints = static_cast<unsigned int>(input[inputIndex++]);
for (unsigned int jj = 0; jj < numberOfPoints; ++jj)
{
output[outputIndex++] = static_cast<TOutput>(input[inputIndex++]);
}
}
}
}
/** Read cells from input buffer, used when Writing cells. This function only
write specified type of cells(used when input cells container composes
multiple type of cells and only want to write a specified cell type */
template <typename TInput, typename TOutput>
void
ReadCellsBuffer(TInput * input, TOutput * output, CellGeometryEnum type)
{
if (input && output)
{
SizeValueType inputIndex{};
SizeValueType outputIndex{};
for (SizeValueType ii = 0; ii < m_NumberOfCells; ++ii)
{
auto cellType = static_cast<CellGeometryEnum>(input[inputIndex++]);
auto nn = static_cast<unsigned int>(input[inputIndex++]);
if (cellType == type)
{
output[outputIndex++] = nn;
for (unsigned int jj = 0; jj < nn; ++jj)
{
output[outputIndex++] = static_cast<TOutput>(input[inputIndex++]);
}
}
else
{
inputIndex += nn;
}
}
}
}
/** Write cells to a data buffer, used when reading a mesh. Used for cellType
with constant number of points */
template <typename TInput, typename TOutput>
void
WriteCellsBuffer(TInput * input,
TOutput * output,
CellGeometryEnum cellType,
unsigned int numberOfPoints,
SizeValueType numberOfCells)
{
if (input && output)
{
SizeValueType inputIndex{};
SizeValueType outputIndex{};
for (SizeValueType ii = 0; ii < numberOfCells; ++ii)
{
output[outputIndex++] = static_cast<TOutput>(cellType);
output[outputIndex++] = static_cast<TOutput>(numberOfPoints);
for (unsigned int jj = 0; jj < numberOfPoints; ++jj)
{
output[outputIndex++] = static_cast<TOutput>(input[inputIndex++]);
}
}
}
}
/** Write cells to a data buffer, used when reading mesh, used for cellType
with non-constant number of points */
template <typename TInput, typename TOutput>
void
WriteCellsBuffer(TInput * input, TOutput * output, CellGeometryEnum cellType, SizeValueType numberOfCells)
{
if (input && output)
{
SizeValueType inputIndex{};
SizeValueType outputIndex{};
for (SizeValueType ii = 0; ii < numberOfCells; ++ii)
{
auto numberOfPoints = static_cast<unsigned int>(input[inputIndex++]);
if (numberOfPoints > 2 && cellType == CellGeometryEnum::LINE_CELL)
{
output[outputIndex++] = static_cast<TOutput>(CellGeometryEnum::POLYLINE_CELL);
}
else
{
output[outputIndex++] = static_cast<TOutput>(cellType);
}
output[outputIndex++] = static_cast<TOutput>(numberOfPoints);
for (unsigned int jj = 0; jj < numberOfPoints; ++jj)
{
output[outputIndex++] = static_cast<TOutput>(input[inputIndex++]);
}
}
}
}
protected:
/** Big or Little Endian, and the type of the file. (May be ignored.) */
IOByteOrderEnum m_ByteOrder{ IOByteOrderEnum::OrderNotApplicable };
IOFileEnum m_FileType{ IOFileEnum::ASCII };
/** Filename to read */
std::string m_FileName{};
/** Should we compress the data? */
bool m_UseCompression{ false };
/** Used internally to keep track of the type of the component. */
IOComponentEnum m_PointComponentType{ IOComponentEnum::UNKNOWNCOMPONENTTYPE };
IOComponentEnum m_CellComponentType{ IOComponentEnum::UNKNOWNCOMPONENTTYPE };
IOComponentEnum m_PointPixelComponentType{ IOComponentEnum::UNKNOWNCOMPONENTTYPE };
IOComponentEnum m_CellPixelComponentType{ IOComponentEnum::UNKNOWNCOMPONENTTYPE };
/** Used internally to keep track of the type of the pixel. */
IOPixelEnum m_PointPixelType{ IOPixelEnum::SCALAR };
IOPixelEnum m_CellPixelType{ IOPixelEnum::SCALAR };
/** Stores the number of components per pixel. This will be 1 for
* grayscale images, 3 for RGBPixel images, and 4 for RGBPixelA images. */
unsigned int m_NumberOfPointPixelComponents{ 0 };
unsigned int m_NumberOfCellPixelComponents{ 0 };
/** The number of independent dimensions in the point. */
unsigned int m_PointDimension{ 3 };
/** The number of points and cells */
SizeValueType m_NumberOfPoints{};
SizeValueType m_NumberOfCells{};
SizeValueType m_NumberOfPointPixels{};
SizeValueType m_NumberOfCellPixels{};
/** The buffer size of cells */
SizeValueType m_CellBufferSize{};
/** Flags indicate whether read or write points, cells, point data and cell
data */
bool m_UpdatePoints{ false };
bool m_UpdateCells{ false };
bool m_UpdatePointData{ false };
bool m_UpdateCellData{ false };
private:
ArrayOfExtensionsType m_SupportedReadExtensions{};
ArrayOfExtensionsType m_SupportedWriteExtensions{};
};
#define MESHIOBASE_TYPEMAP(type, ctype) \
template <> \
struct MeshIOBase::MapComponentType<type> \
{ \
static constexpr IOComponentEnum CType = ctype; \
}
MESHIOBASE_TYPEMAP(unsigned char, IOComponentEnum::UCHAR);
MESHIOBASE_TYPEMAP(char, IOComponentEnum::CHAR);
MESHIOBASE_TYPEMAP(unsigned short, IOComponentEnum::USHORT);
MESHIOBASE_TYPEMAP(short, IOComponentEnum::SHORT);
MESHIOBASE_TYPEMAP(unsigned int, IOComponentEnum::UINT);
MESHIOBASE_TYPEMAP(int, IOComponentEnum::INT);
MESHIOBASE_TYPEMAP(unsigned long, IOComponentEnum::ULONG);
MESHIOBASE_TYPEMAP(long, IOComponentEnum::LONG);
MESHIOBASE_TYPEMAP(unsigned long long, IOComponentEnum::ULONGLONG);
MESHIOBASE_TYPEMAP(long long, IOComponentEnum::LONGLONG);
MESHIOBASE_TYPEMAP(float, IOComponentEnum::FLOAT);
MESHIOBASE_TYPEMAP(double, IOComponentEnum::DOUBLE);
MESHIOBASE_TYPEMAP(long double, IOComponentEnum::LDOUBLE);
#undef MESHIOBASE_TYPEMAP
} // end namespace itk
#endif
|