29#include <itkImportImageFilter.h>
31#include <itkPointSet.h>
33#include <itkFixedArray.h>
51 const std::string &handling=
"");
63 size_t itkDataTypeSize,
79template<
typename SIZE_TYPE>
83 if (sizeObj.GetSizeDimension() >
static_cast<unsigned int>(vec.
dim())){
86 "Too high ITK dimension. The ML can handle only 6 dimensions.");
89 for (
unsigned int i=0; i<sizeObj.GetSizeDimension(); i++){
90 sizeObj[i] =
static_cast<typename SIZE_TYPE::SizeValueType
>(vec[i]);
107template<
typename INDEX_TYPE>
111 if (sizeObj.GetIndexDimension() >
static_cast<unsigned int>(vec.
dim())){
114 "Too high ITK dimension. The ML can handle only 6 dimensions.");
117 for (
unsigned int i=0; i<sizeObj.GetIndexDimension(); i++){
134template<
typename INDEX_TYPE>
138 unsigned int sizeDim = sizeObj.GetSizeDimension();
139 unsigned int vecDim =
static_cast<unsigned int>(vec.
dim());
140 if (sizeDim > vecDim){
145 for (
unsigned int i=0; i < vecDim; i++){ vec[i] = (i < sizeDim) ? static_cast<MLint>(sizeObj[i]) : defaultVal; }
161template<
typename INDEX_TYPE>
165 unsigned int idxDim = indexObj.GetIndexDimension();
166 unsigned int vecDim =
static_cast<unsigned int>(vec.
dim());
167 if (idxDim > vecDim){
172 for (
unsigned int i=0; i < vecDim; i++){ vec[i] = (i < idxDim) ? indexObj[i] : defaultVal; }
189template<
typename VECTOR_TYPE>
193 for (
unsigned int i=0; i<sizeObj.GetVectorDimension(); i++){
194 sizeObj[i] = i < 3 ? vec[i] : defaultComp;
212template<
typename VECTOR_TYPE>
216 for (
unsigned int i=0; i<sizeObj.GetVectorDimension(); i++){
217 sizeObj[i] = i < 4 ? vec[i] : defaultComp;
235template<
typename POINT_TYPE>
239 for (
unsigned int i=0; i<sizeObj.GetPointDimension(); i++){
240 sizeObj[i] = i < 3 ? vec[i] : defaultComp;
258template<
typename POINT_TYPE>
262 for (
unsigned int i=0; i<sizeObj.GetPointDimension(); i++){
263 sizeObj[i] = i < 4 ? vec[i] : defaultComp;
280template<
typename REGION_PARENT_TYPE>
283 typename REGION_PARENT_TYPE::RegionType region;
302template<
typename REGION_PARENT_TYPE>
323template<
typename ARRAY_TYPE,
typename STL_VECTOR>
326 const size_t mFieldSize = stlVec.size();
327 ARRAY_TYPE retArray(mFieldSize);
328 for (
size_t i=0; i<mFieldSize; ++i){ retArray[i] = stlVec[i]; }
336template<
typename VERSOR_TYPE>
340 Rotation.Set(mlVec[0], mlVec[1], mlVec[2], mlVec[3]);
347template<
typename ARR_TYP,
unsigned int ARR_SIZE>
350 return itkArray.Length;
356template<
typename ITK_ARRAY_TYPE>
359 return itkArray.size();
367template<
typename STL_CONTAINER,
typename ITK_ARRAY_TYPE>
371 STL_CONTAINER stlVector;
372 for (
size_t i=0; i<itkFieldSize; ++i){ stlVector.push_back(itkArray[i]); }
381template<
typename ITK_VERSOR_TYPE>
384 ml::Vector4 mlVector(itkVersor.GetX(), itkVersor.GetY(), itkVersor.GetZ(), itkVersor.GetW());
401template<
typename INDEX_TYPE>
405 if (sizeObj.GetIndexDimension() > 6){
409 for (
unsigned int i=0; i<sizeObj.GetIndexDimension(); i++){ sizeObj[i] = vec[i]; }
422template<
class FilterType>
452 (xml ?
static_cast<MLssize_t>(xml->size()) :
454 (vl ? vl->
getNum() : 0))));
459 typename FilterType::NodeContainer::Pointer nc = FilterType::NodeContainer::New();
467 float px=0, py=0, pz=0;
478 float px=0, py=0, pz=0;
479 vl->
getPoint(c, vecType, px, py, pz);
491 typename FilterType::NodeType pnt;
494 nc->InsertElement(c, pnt);
518template<
typename DTYPE,
unsigned int ROW_DIM,
unsigned int COL_DIM>
522 itk::Matrix<DTYPE, ROW_DIM, COL_DIM> retMat;
523 retMat.SetIdentity();
526 unsigned int maxRowDim = ROW_DIM;
530 "Too high row dimension of ITK matrix. Only 4 dimensions will be converted.");
532 unsigned int maxColDim = COL_DIM;
536 "Too high col dimension of ITK matrix. Only 4 dimensions will be converted.");
540 for (
unsigned int row=0; row < maxRowDim; ++row){
541 for (
unsigned int col=0; col < maxColDim; ++col){
542 retMat[row][col] = mat[row][col];
560template<
typename DTYPE,
unsigned int ROW_DIM,
unsigned int COL_DIM>
562 bool fillWithID=
false,
563 bool suppressDimensionWarning=
false)
566 unsigned int maxRowDim = ROW_DIM;
569 if (!suppressDimensionWarning){
572 "Too high row dimension of ITK matrix. Only 4 dimensions will be converted.");
575 unsigned int maxColDim = COL_DIM;
578 if (!suppressDimensionWarning){
581 "Too high col dimension of ITK matrix. Only 4 dimensions will be converted.");
588 for (
unsigned int row=0; row < maxRowDim; ++row){
589 for (
unsigned int col=0; col < maxColDim; ++col){
590 retMat[row][col] = mat[row][col];
604template<
class POINTSETTYPE>
624 (xml ?
static_cast<MLssize_t>(xml->size()) :
626 (vl ? vl->
getNum() : 0))));
630 int maxDim = POINTSETTYPE::PointDimension;
635 "Too high dimension of ITK PointSet. Only 6 dimensions will be converted.");
641 typename POINTSETTYPE::Pointer outputPointSet = POINTSETTYPE::New();
643 typedef typename POINTSETTYPE::PointDataContainer DataContainer;
644 outputPointSet->SetPointData( DataContainer::New());
645 outputPointSet->GetPoints()->Reserve( numVals );
646 outputPointSet->GetPointData()->Reserve( numVals );
648 typename POINTSETTYPE::PointIdentifier pointId = 0;
649 typename POINTSETTYPE::PointType point;
656 float px=0, py=0, pz=0;
667 float px=0, py=0, pz=0;
668 vl->
getPoint(c, vecType, px, py, pz);
681 for (
int j=0; j < maxDim ;j++){ point[j] = mlVec[j]; }
682 outputPointSet->SetPoint(pointId++, point );
685 return outputPointSet;
689 typename POINTSETTYPE::Pointer outputPointSet = POINTSETTYPE::New();
690 return outputPointSet;
699template <
typename ITK_IMPORT_IMAGE_FILTER_TYPE>
701 ITK_IMPORT_IMAGE_FILTER_TYPE::OutputImageType::ImageDimension>* importImageFilter,
712template <
typename ITK_IMAGE_TYPE>
714 ITK_IMAGE_TYPE::ImageDimension>* image,
740template <
typename ITK_CLASS_TYPE>
742 ITK_CLASS_TYPE *image,
743 bool correctSVS=
true)
745 if (!image){
return; }
762 const Vector3 orig(mat[0][3], mat[1][3], mat[2][3]);
771 mat[0][0] = mat[0][0]*invScales[0];
772 mat[1][0] = mat[1][0]*invScales[0];
773 mat[2][0] = mat[2][0]*invScales[0];
776 mat[0][1] = mat[0][1]*invScales[1];
777 mat[1][1] = mat[1][1]*invScales[1];
778 mat[2][1] = mat[2][1]*invScales[1];
781 mat[0][2] = mat[0][2]*invScales[2];
782 mat[1][2] = mat[1][2]*invScales[2];
783 mat[2][2] = mat[2][2]*invScales[2];
792 ITK_CLASS_TYPE::DirectionType::RowDimensions,
793 ITK_CLASS_TYPE::DirectionType::ColumnDimensions>(mat);
794 image->SetDirection(dirCosines);
824template <
typename ITK_CLASS_TYPE>
827 bool correctSVS=
true,
828 bool suppressDimensionWarning=
false)
830 if (!image){
return; }
831 if (ITK_CLASS_TYPE::ImageDimension < 2){
834 "1 D itk world matrix conversion is not supported; "
835 "using identity as return value.");
840 const typename ITK_CLASS_TYPE::SpacingType spacing = image->GetSpacing();
841 const typename ITK_CLASS_TYPE::PointType origin = image->GetOrigin();
842 const typename ITK_CLASS_TYPE::DirectionType direction = image->GetDirection();
846 ITK_CLASS_TYPE::ImageDimension,
847 ITK_CLASS_TYPE::ImageDimension>(direction,
true, suppressDimensionWarning);
853 mat[0][0] = dirCosines[0][0]*spacing[0];
854 mat[1][0] = dirCosines[1][0]*spacing[0];
855 mat[2][0] = dirCosines[2][0]*spacing[0];
858 mat[0][1] = dirCosines[0][1]*spacing[1];
859 mat[1][1] = dirCosines[1][1]*spacing[1];
860 mat[2][1] = dirCosines[2][1]*spacing[1];
864 const double spacing2 = (ITK_CLASS_TYPE::ImageDimension > 2) ? spacing[2] : 1;
865 mat[0][2] = dirCosines[0][2]*spacing2;
866 mat[1][2] = dirCosines[1][2]*spacing2;
867 mat[2][2] = dirCosines[2][2]*spacing2;
870 mat[0][3] = origin[0];
871 mat[1][3] = origin[1];
872 mat[2][3] = (ITK_CLASS_TYPE::ImageDimension > 2) ? origin[2] : 0;
905template <
typename ITK_INDATATYPE,
unsigned int DIM>
908 bool correctSVS=
true)
911 typedef itk::ImportImageFilter<ITK_INDATATYPE, DIM> ImportFilterType;
912 typedef typename ImportFilterType::Pointer ImportFilterPointerType;
914 ImportFilterPointerType importer = ImportFilterType::New();
925 importer->SetImportPointer(
static_cast<ITK_INDATATYPE*
>(inSubImg.
getData()),
926 static_cast<itk::SizeValueType
>(inSubImgBox.
getNumVoxels()),
952 template <
typename RETURN_TYPE_PTR,
typename FILTER_TYPE,
typename VOXEL_TYPE>
956 bool correctSVS=
true)
971 dataSubImg.
setBox(inImgBox);
983 typename FILTER_TYPE::Pointer importer = FILTER_TYPE::New();
995 importer->SetImportPointer(
static_cast<VOXEL_TYPE*
>(dataSubImg.
getData()), inImgBox.
getNumVoxels(),
false);
997 return importer->GetOutput();
1000 ML_PRINT_ERROR(
"mlITKSupportToolFunctions::getInputAsItkImportImageAndSubImg()",
1002 "Failed to request input image data, probably subsequent operations will fail");
BaseItem * getItemAt(MLssize_t index) override
ImageVector getImageExtent() const
Returns the extent of the (sub)image.
Vector3 getVoxelSize() const
Returns the voxel size.
void setVoxelToWorldMatrix(const Matrix4 &matrix)
Sets the matrix that transforms voxel to world coordinates to matrix.
void translateVoxelToWorldMatrix(const Vector3 &offsetVector)
Translates the voxelToWorld matrix by the offsetVector.
const Matrix4 & getVoxelToWorldMatrix() const
Returns the voxelToWorld matrix.
PagedImage * getUpdatedInputImage(MLint inputIndex, bool getReal=false) const
MLEXPORT MLErrorCode getTile(SubImageBox location, MLDataType dataType, void **data, const ScaleShiftData &scaleShiftData=ScaleShiftData(), MLRequestProgressCB *progressCallback=nullptr, void *progressCallbackUserData=nullptr)
Base object class PointList managing a list of points.
void getValue(MLssize_t index, Vector3 &vec) const
get point at given index
MLssize_t getNum() const
returns the number of contained points
Class to handle Rotations (internally, the rotation is stored as a unit quaternion).
void setBox(const SubImageBox &subImageBox)
const SubImageBox & getBox() const
Returns the box describing the origin/extent of the subimage.
MLEXPORT void allocateAsMemoryBlockHandle(MLMemoryErrorHandling handleFailure=ML_RETURN_NULL)
Allocates data using the ML memory manager. For failure handing, see SubImage::allocate().
void * getData() const
Returns the memory address of the memory managed by the subimage.
static MLint dim()
Returns the dimension the ML calculates with.
intT getNumVoxels() const
VectorType getExtent() const
const DATATYPE * getData() const
Returns the memory address of the image region. Overloads methods from SubImage.
ComponentType z
Z component of the vector.
ComponentType x
X component of the vector.
ComponentType y
Y component of the vector.
void set(const DT val)
Sets all values to val.
static Tmat4< MLdouble > getIdentity()
Base object representing a list of vectors given as Vector4's.
void getPoint(MLssize_t index, int &type, float &x1, float &y1, float &z1) const
returns point of vectors at given point index (NOTE: (0,1 = first vector , 2,3 = second vector))
MLssize_t getNum() const
returns the number of contained points
#define ML_BASE_IS_A(base, type)
bool MLValueIs0WOM(MLint8 a)
#define ML_BAD_DIMENSION
The image or data structure has wrong extent or dimensions.
MLint32 MLErrorCode
Type of an ML Error code.
#define ML_RESULT_OK
No error. Everything seems to be okay.
#define ML_PRINT_FATAL_ERROR(FUNC_NAME, REASON, HANDLING)
#define ML_PRINT_WARNING(FUNC_NAME, REASON, HANDLING)
#define ML_PRINT_ERROR(FUNC_NAME, REASON, HANDLING)
#define MLITK_SUPPORT_EXPORT
@ ML_FATAL_MEMORY_ERROR
On allocation failure, a fatal error print is done and NULL is returned.
SSIZE_T MLssize_t
The signed ML size type that is a signed 32-bit size_t on 32-bit platforms and 64-bit one on 64-bit p...
MLMessageType
Message types handled by the ErrorOutput class.
ImageVector MLVectorFromITKSize(const INDEX_TYPE &sizeObj, MLint defaultVal)
void setITKWorldFromMedicalImageProperty(MedicalImageProperties props, ITK_CLASS_TYPE *image, bool correctSVS=true)
const STL_CONTAINER STLVectorFromITKArray(const ITK_ARRAY_TYPE &itkArray)
MLITK_SUPPORT_EXPORT void setAllowedTypesForITKNodeContainer(BaseField *field)
FilterType::NodeContainer::Pointer ITKNodeContainerFromBasePointer(Base *baseVal)
REGION_PARENT_TYPE::RegionType ITKRegionFromMLSubImgBox(const SubImageBox &subImgBox)
POINT_TYPE ITKPointFromMLVec3(const Vector3 &vec, MLdouble defaultComp)
Tvec4< MLdouble > Vector4
A vector with four components of type double.
Matrix4 MLMatrixFromITKMatrix(const ITKML_TYPENAME itk::Matrix< DTYPE, ROW_DIM, COL_DIM > &mat, bool fillWithID=false, bool suppressDimensionWarning=false)
POINTSETTYPE::Pointer ITKPointSetFromBasePointer(Base *baseVal)
const ml::Vector4 MLVectorFromITKVersor(const ITK_VERSOR_TYPE &itkVersor)
void ITKSetOriginFromVec3(itk::ImportImageFilter< typename ITK_IMPORT_IMAGE_FILTER_TYPE::OutputImagePixelType, ITK_IMPORT_IMAGE_FILTER_TYPE::OutputImageType::ImageDimension > *importImageFilter, const Vector3 &orig)
Tmat4< MLdouble > Matrix4
The standard 4x4 matrix of type double.
VECTOR_TYPE ITKVectorFromMLVec4(const Vector4 &vec, MLdouble defaultComp)
Tvec3< MLdouble > Vector3
A vector with three components of type double.
POINT_TYPE ITKPointFromMLVec4(const Vector4 &vec, MLdouble defaultComp)
size_t GetSizeOfAnITKArray(const itk::FixedArray< ARR_TYP, ARR_SIZE > &itkArray)
Returns the size of an itk::FixedArray.
Tvec6< MLdouble > Vector6
A vector with six components of type double.
MLITK_SUPPORT_EXPORT void copyITKDataBufferToMLSubImg(void *itkData, size_t itkDataTypeSize, const SubImageBox &itkBox, SubImage &outSubImg)
SIZE_TYPE ITKSizeFromMLVector(const ImageVector &vec)
TSubImageBox< MLint > SubImageBox
Defines the standard SubImageBox type used in the ML. Its size varies with the size of the MLint type...
RETURN_TYPE_PTR getInputAsItkImportImageAndSubImg(Module &op, int inIdx, TSubImage< VOXEL_TYPE > &dataSubImg, bool correctSVS=true)
itk::Matrix< DTYPE, ROW_DIM, COL_DIM > ITKMatrixFromMLMatrix(const Matrix4 &mat)
VERSOR_TYPE ITKVersorFromMLVector(const ml::Vector4 &mlVec)
SubImageBox MLSubImgBoxFromITKRegion(const typename REGION_PARENT_TYPE::RegionType ®ion)
ImageVector MLVectorFromITKIndex(const INDEX_TYPE &indexObj, MLint defaultVal)
INDEX_TYPE ITKIndexFromMLVector(const ImageVector &vec)
INDEX_TYPE ITKIndexFromMLVec6(const Vector6 &vec)
void setMLWorldFromITKScaleOriginAndOrientation(const ITK_CLASS_TYPE *image, MedicalImageProperties &props, bool correctSVS=true, bool suppressDimensionWarning=false)
ARRAY_TYPE ITKArrayFromSTLVector(const STL_VECTOR &stlVec)
itk::ImportImageFilter< ITK_INDATATYPE, DIM >::Pointer getITKImportImageFromSubImg(const SubImage &inSubImg, const MedicalImageProperties &props, bool correctSVS=true)
MLITK_SUPPORT_EXPORT void postITKException(const itk::ExceptionObject &e, const Module *module, MLMessageType messageType, const std::string &handling="")
TImageVector< MLint > ImageVector
Defines the standard ImageVector type that is used by the ML for indexing and coordinates.
VECTOR_TYPE ITKVectorFromMLVec3(const Vector3 &vec, MLdouble defaultComp)