29 #include <itkImportImageFilter.h>
30 #include <itkMatrix.h>
31 #include <itkPointSet.h>
33 #include <itkFixedArray.h>
51 const std::string &handling=
"");
63 size_t itkDataTypeSize,
79 template<
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]);
107 template<
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++){
134 template<
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; }
161 template<
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; }
189 template<
typename VECTOR_TYPE>
193 for (
unsigned int i=0; i<sizeObj.GetVectorDimension(); i++){
194 sizeObj[i] = i < 3 ? vec[i] : defaultComp;
212 template<
typename VECTOR_TYPE>
216 for (
unsigned int i=0; i<sizeObj.GetVectorDimension(); i++){
217 sizeObj[i] = i < 4 ? vec[i] : defaultComp;
235 template<
typename POINT_TYPE>
239 for (
unsigned int i=0; i<sizeObj.GetPointDimension(); i++){
240 sizeObj[i] = i < 3 ? vec[i] : defaultComp;
258 template<
typename POINT_TYPE>
262 for (
unsigned int i=0; i<sizeObj.GetPointDimension(); i++){
263 sizeObj[i] = i < 4 ? vec[i] : defaultComp;
280 template<
typename REGION_PARENT_TYPE>
283 typename REGION_PARENT_TYPE::RegionType region;
286 region.SetIndex(ITKIndexFromMLVector<ITKML_TYPENAME REGION_PARENT_TYPE::IndexType>(subImgBox.
v1));
289 region.SetSize(ITKSizeFromMLVector<ITKML_TYPENAME REGION_PARENT_TYPE::SizeType>(subImgBox.
getExtent()));
302 template<
typename REGION_PARENT_TYPE>
323 template<
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]; }
336 template<
typename VERSOR_TYPE>
340 Rotation.Set(mlVec[0], mlVec[1], mlVec[2], mlVec[3]);
347 template<
typename ARR_TYP,
unsigned int ARR_SIZE>
350 return itkArray.Length;
356 template<
typename ITK_ARRAY_TYPE>
359 return itkArray.size();
367 template<
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]); }
381 template<
typename ITK_VERSOR_TYPE>
384 ml::Vector4 mlVector(itkVersor.GetX(), itkVersor.GetY(), itkVersor.GetZ(), itkVersor.GetW());
401 template<
typename INDEX_TYPE>
405 if (sizeObj.GetIndexDimension() > 6){
409 for (
unsigned int i=0; i<sizeObj.GetIndexDimension(); i++){ sizeObj[i] = vec[i]; }
422 template<
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;
493 pnt.SetIndex(ITKIndexFromMLVec6<ITKML_TYPENAME FilterType::NodeType::IndexType>(mlVec));
494 nc->InsertElement(c, pnt);
518 template<
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];
560 template<
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.");
587 if (fillWithID){ retMat = Matrix4::getIdentity(); }
else { retMat.
set(0); }
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];
604 template<
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;
699 template <
typename ITK_IMPORT_IMAGE_FILTER_TYPE>
701 ITK_IMPORT_IMAGE_FILTER_TYPE::OutputImageType::ImageDimension>* importImageFilter,
704 importImageFilter->SetOrigin(ITKPointFromMLVec3<ITKML_TYPENAME ITK_IMPORT_IMAGE_FILTER_TYPE::OriginType>(orig,0));
712 template <
typename ITK_IMAGE_TYPE>
714 ITK_IMAGE_TYPE::ImageDimension>* image,
717 image->SetOrigin(ITKPointFromMLVec3<ITKML_TYPENAME ITK_IMAGE_TYPE::PointType>(orig,0));
740 template <
typename ITK_CLASS_TYPE>
742 ITK_CLASS_TYPE *image,
743 bool correctSVS=
true)
745 if (!image){
return; }
751 image->SetSpacing(ITKVectorFromMLVec3<ITKML_TYPENAME ITK_CLASS_TYPE::SpacingType>(scales, 1));
762 const Vector3 orig(mat[0][3], mat[1][3], mat[2][3]);
763 ITKSetOriginFromVec3<ITK_CLASS_TYPE>(image, orig);
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);
824 template <
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);
850 Matrix4 mat = Matrix4::getIdentity();
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;
905 template <
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();
919 importer->SetRegion(ITKRegionFromMLSubImgBox<ImportFilterType>(inSubImgBox));
922 setITKWorldFromMedicalImageProperty<ImportFilterType>(props, &(*importer), correctSVS);
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();
987 importer->SetRegion(ITKRegionFromMLSubImgBox<FILTER_TYPE>(inImgBox));
990 setITKWorldFromMedicalImageProperty<FILTER_TYPE>(*pInImg, &(*importer), correctSVS);
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");
Field to encapsulate a pointer to an ML base object.
BaseItem * getItemAt(MLssize_t index) override
This virtual function is reimplemented from ListBase, where it returns 0 in any case (also in ListTem...
Class representing general ML objects that support import/export via strings (setPersistentState() an...
ImageVector getImageExtent() const
Returns the extent of the (sub)image.
virtual T * getList()
Get pointer to the active list object Use this pointer for list access after testing that it is !...
This class encapsulates basic medical image properties:
Vector3 getVoxelSize() const
Returns the voxel size.
const Matrix4 & getVoxelToWorldMatrix() const
Returns the voxelToWorld matrix.
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.
Base class for an image processing module of the ML.
PagedImage * getUpdatedInputImage(MLint inputIndex, bool getReal=false) const
Convenience method for a safe access to the input image at index inputIndex.
The class PagedImage, representing a fragmented image that manages properties and data of an image lo...
MLEXPORT MLErrorCode getTile(SubImageBox location, MLDataType dataType, void **data, const ScaleShiftData &scaleShiftData=ScaleShiftData(), MLRequestProgressCB *progressCallback=nullptr, void *progressCallbackUserData=nullptr)
See Host::getTile( module, outputIndex, location, dataType, data, scaleShiftData).
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)
This class manages/represents a rectangular 6D image region that is organized linearly in memory.
void * getData() const
Returns the memory address of the memory managed by the subimage.
void setBox(const SubImageBox &subImageBox)
Sets a rectangular 6D region of the subimage to subImageBox.
MLEXPORT void allocateAsMemoryBlockHandle(MLMemoryErrorHandling handleFailure=ML_RETURN_NULL)
Allocates data using the ML memory manager. For failure handing, see SubImage::allocate().
MLEXPORT void free()
Frees data pointed to by getData() with MLFree().
const SubImageBox & getBox() const
Returns the box describing the origin/extent of the subimage.
static MLint dim()
Returns the dimension the ML calculates with.
VectorType v1
Corner v1 of the subimage region (included in region).
VectorType v2
Corner v2 of the subimage region (also included in region!).
intT getNumVoxels() const
Returns number of voxels in the subimage region, i.e., the product of all extents if this is not empt...
VectorType getExtent() const
Returns the extents of the subimage region.
This template class manages/represents a rectangular 6D image region in memory that is organized line...
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.
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
Base object class XMarkerListContainer (derived from ListContainerTemplate) for XMarkerList objects.
Base object class XMarkerList (derived from BaseListTemplate) specialized for XMarker items.
Base object class XMarker (derived form baseItem) with 6D pos, 3D vec and type int.
#define ML_BASE_IS_A(base, type)
This file defines macros that are inserted in classes to declare and implement additional class membe...
bool MLValueIs0WOM(MLint8 a)
Returns true if value is 0; otherwise, it returns false.
#define ML_BAD_PARAMETER
A bad/invalid parameter (or even an inappropriate image) has been passed to a module or an algorithm,...
#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)
Like ML_PRINT_FATAL_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ) without a runtime object to be du...
#define ML_PRINT_WARNING(FUNC_NAME, REASON, HANDLING)
Like ML_PRINT_WARNING_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ) without a runtime object to be dumped...
#define ML_PRINT_ERROR(FUNC_NAME, REASON, HANDLING)
Like ML_PRINT_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ) without a runtime object to be dumped.
#define MLITK_SUPPORT_EXPORT
When included by other libraries MLITK_SUPPORT_EXPORT is compiled as import symbol.
@ ML_FATAL_MEMORY_ERROR
On allocation failure, a fatal error print is done and NULL is returned.
MLint64 MLint
A signed ML integer type with at least 64 bits used for index calculations on very large images even ...
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)
Assign and cast values from an ITK size object to an ML ImageVector.
void setITKWorldFromMedicalImageProperty(MedicalImageProperties props, ITK_CLASS_TYPE *image, bool correctSVS=true)
Takes the world transformation of ML MedicalImageProperties and sets spacing, origin and direction co...
itk::ImportImageFilter< ITK_INDATATYPE, DIM >::Pointer getITKImportImageFromSubImg(const SubImage &inSubImg, const MedicalImageProperties &props, bool correctSVS=true)
Takes a SubImage and returns an object of an itk::ImportImageFilter managing the inSubImg as ITK obje...
const STL_CONTAINER STLVectorFromITKArray(const ITK_ARRAY_TYPE &itkArray)
Returns an STL container object of type STL_CONTAINER which contains cast copies of all elements of t...
MLITK_SUPPORT_EXPORT void setAllowedTypesForITKNodeContainer(BaseField *field)
Set the allowed types for input Base fields that are converted to ITK NodeContainer objects.
FilterType::NodeContainer::Pointer ITKNodeContainerFromBasePointer(Base *baseVal)
Reads a base field and looks for point like data structures (XMarkerLists, XMarkerListContainers,...
size_t GetSizeOfAnITKArray(const ITK_ARRAY_TYPE &itkArray)
Returns the size of an arbitrary itk array.
REGION_PARENT_TYPE::RegionType ITKRegionFromMLSubImgBox(const SubImageBox &subImgBox)
Assign and cast values from an ITK region to an ML SubImageBox object.
POINT_TYPE ITKPointFromMLVec3(const Vector3 &vec, MLdouble defaultComp)
Assign and cast values from an ML Vector3 to an ITK point object; all itk components with index > 3 a...
Matrix4 MLMatrixFromITKMatrix(const ITKML_TYPENAME itk::Matrix< DTYPE, ROW_DIM, COL_DIM > &mat, bool fillWithID=false, bool suppressDimensionWarning=false)
Converts itk::Matrix mat of type itk::Matrix<DTYPE, ROW_DIM, COL_DIM> to the returned ML 4x4 matrix o...
POINTSETTYPE::Pointer ITKPointSetFromBasePointer(Base *baseVal)
Reads the passed baseVal objects and looks for point like data structures (XMarkerLists,...
const ml::Vector4 MLVectorFromITKVersor(const ITK_VERSOR_TYPE &itkVersor)
Returns an STL container object of type STL_CONTAINER which contains cast copies of all elements of t...
VECTOR_TYPE ITKVectorFromMLVec4(const Vector4 &vec, MLdouble defaultComp)
Assign and cast values from an ML Vector4 to an ITK size object; all itk components with index > 3 ar...
Tvec3< MLdouble > Vector3
A vector with three components of type double.
POINT_TYPE ITKPointFromMLVec4(const Vector4 &vec, MLdouble defaultComp)
Assign and cast values from an ML Vector4 to an ITK object of type POINT_TYPE; all itk components wit...
MLITK_SUPPORT_EXPORT void copyITKDataBufferToMLSubImg(void *itkData, size_t itkDataTypeSize, const SubImageBox &itkBox, SubImage &outSubImg)
Unpack ITK data buffer to an ML subimage and convert data formats if necessary.
SIZE_TYPE ITKSizeFromMLVector(const ImageVector &vec)
Assign and cast values from an ML ImageVector to an ITK size object.
RETURN_TYPE_PTR getInputAsItkImportImageAndSubImg(Module &op, int inIdx, TSubImage< VOXEL_TYPE > &dataSubImg, bool correctSVS=true)
Reads and updates an input image from the input number inIdx of Module op and sets up the referenced ...
VERSOR_TYPE ITKVersorFromMLVector(const ml::Vector4 &mlVec)
Returns an object of an ITK array type ARRAY_TYPE which contains cast copies of all elements of stlVe...
SubImageBox MLSubImgBoxFromITKRegion(const typename REGION_PARENT_TYPE::RegionType ®ion)
Assign and cast values from an ITK region to an ML SubImageBox object.
ImageVector MLVectorFromITKIndex(const INDEX_TYPE &indexObj, MLint defaultVal)
Assign and cast values from an ITK index object to an ML ImageVector.
INDEX_TYPE ITKIndexFromMLVector(const ImageVector &vec)
Assign and cast values from an ML ImageVector to an ITK size object.
INDEX_TYPE ITKIndexFromMLVec6(const Vector6 &vec)
Assign and cast values from an ML Vector6 to an ITK index object of type INDEX_TYPE.
void setMLWorldFromITKScaleOriginAndOrientation(const ITK_CLASS_TYPE *image, MedicalImageProperties &props, bool correctSVS=true, bool suppressDimensionWarning=false)
Sets the world transformation of the ML MedicalImageProperties props from the spacing,...
ARRAY_TYPE ITKArrayFromSTLVector(const STL_VECTOR &stlVec)
Returns an object of an ITK array type ARRAY_TYPE which contains cast copies of all elements of stlVe...
void ITKSetOriginFromVec3(itk::Image< typename ITK_IMAGE_TYPE::PixelType, ITK_IMAGE_TYPE::ImageDimension > *image, const Vector3 &orig)
On the given ITK_IMAGE_TYPE the origin orig is set with the SetOrigin method from a point type; the o...
MLITK_SUPPORT_EXPORT void postITKException(const itk::ExceptionObject &e, const Module *module, MLMessageType messageType, const std::string &handling="")
Sends all available information from the ITKException itkException to the ML error handler.
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)
Assign and cast values from an ML Vector3 to an ITK size object; all itk components with index > 2 ar...
itk::Matrix< DTYPE, ROW_DIM, COL_DIM > ITKMatrixFromMLMatrix(const Matrix4 &mat)
Converts the ML 4x4 matrix mat function argument to the returned itk::Matrix of type itk::Matrix<DTYP...