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]; }
335 template<
typename ARR_TYP,
unsigned int ARR_SIZE>
338 return itkArray.Length;
344 template<
typename ITK_ARRAY_TYPE>
347 return itkArray.size();
355 template<
typename STL_CONTAINER,
typename ITK_ARRAY_TYPE>
359 STL_CONTAINER stlVector;
360 for (
size_t i=0; i<itkFieldSize; ++i){ stlVector.push_back(itkArray[i]); }
377 template<
typename INDEX_TYPE>
381 if (sizeObj.GetIndexDimension() > 6){
385 for (
unsigned int i=0; i<sizeObj.GetIndexDimension(); i++){ sizeObj[i] = vec[i]; }
398 template<
class FilterType>
428 (xml ?
static_cast<MLssize_t>(xml->size()) :
430 (vl ? vl->
getNum() : 0))));
435 typename FilterType::NodeContainer::Pointer nc = FilterType::NodeContainer::New();
443 float px=0, py=0, pz=0;
454 float px=0, py=0, pz=0;
455 vl->
getPoint(c, vecType, px, py, pz);
467 typename FilterType::NodeType pnt;
469 pnt.SetIndex(ITKIndexFromMLVec6<ITKML_TYPENAME FilterType::NodeType::IndexType>(mlVec));
470 nc->InsertElement(c, pnt);
488 template<
typename DTYPE,
unsigned int ROW_DIM,
unsigned int COL_DIM>
492 itk::Matrix<DTYPE, ROW_DIM, COL_DIM> retMat;
493 retMat.SetIdentity();
496 unsigned int maxRowDim = ROW_DIM;
500 "Too high row dimension of ITK matrix. Only 4 dimensions will be converted.");
502 unsigned int maxColDim = COL_DIM;
506 "Too high col dimension of ITK matrix. Only 4 dimensions will be converted.");
510 for (
unsigned int row=0; row < maxRowDim; ++row){
511 for (
unsigned int col=0; col < maxColDim; ++col){
512 retMat[row][col] = mat[row][col];
530 template<
typename DTYPE,
unsigned int ROW_DIM,
unsigned int COL_DIM>
532 bool fillWithID=
false,
533 bool suppressDimensionWarning=
false)
536 unsigned int maxRowDim = ROW_DIM;
539 if (!suppressDimensionWarning){
542 "Too high row dimension of ITK matrix. Only 4 dimensions will be converted.");
545 unsigned int maxColDim = COL_DIM;
548 if (!suppressDimensionWarning){
551 "Too high col dimension of ITK matrix. Only 4 dimensions will be converted.");
557 if (fillWithID){ retMat = Matrix4::getIdentity(); }
else { retMat.
set(0); }
558 for (
unsigned int row=0; row < maxRowDim; ++row){
559 for (
unsigned int col=0; col < maxColDim; ++col){
560 retMat[row][col] = mat[row][col];
574 template<
class POINTSETTYPE>
594 (xml ?
static_cast<MLssize_t>(xml->size()) :
596 (vl ? vl->
getNum() : 0))));
600 int maxDim = POINTSETTYPE::PointDimension;
605 "Too high dimension of ITK PointSet. Only 6 dimensions will be converted.");
611 typename POINTSETTYPE::Pointer outputPointSet = POINTSETTYPE::New();
613 typedef typename POINTSETTYPE::PointDataContainer DataContainer;
614 outputPointSet->SetPointData( DataContainer::New());
615 outputPointSet->GetPoints()->Reserve( numVals );
616 outputPointSet->GetPointData()->Reserve( numVals );
618 typename POINTSETTYPE::PointIdentifier pointId = 0;
619 typename POINTSETTYPE::PointType point;
626 float px=0, py=0, pz=0;
637 float px=0, py=0, pz=0;
638 vl->
getPoint(c, vecType, px, py, pz);
651 for (
int j=0; j < maxDim ;j++){ point[j] = mlVec[j]; }
652 outputPointSet->SetPoint(pointId++, point );
655 return outputPointSet;
659 typename POINTSETTYPE::Pointer outputPointSet = POINTSETTYPE::New();
660 return outputPointSet;
669 template <
typename ITK_IMPORT_IMAGE_FILTER_TYPE>
671 ITK_IMPORT_IMAGE_FILTER_TYPE::OutputImageType::ImageDimension>* importImageFilter,
674 importImageFilter->SetOrigin(ITKPointFromMLVec3<ITKML_TYPENAME ITK_IMPORT_IMAGE_FILTER_TYPE::OriginType>(orig,0));
682 template <
typename ITK_IMAGE_TYPE>
684 ITK_IMAGE_TYPE::ImageDimension>* image,
687 image->SetOrigin(ITKPointFromMLVec3<ITKML_TYPENAME ITK_IMAGE_TYPE::PointType>(orig,0));
710 template <
typename ITK_CLASS_TYPE>
712 ITK_CLASS_TYPE *image,
713 bool correctSVS=
true)
715 if (!image){
return; }
721 image->SetSpacing(ITKVectorFromMLVec3<ITKML_TYPENAME ITK_CLASS_TYPE::SpacingType>(scales, 1));
732 const Vector3 orig(mat[0][3], mat[1][3], mat[2][3]);
733 ITKSetOriginFromVec3<ITK_CLASS_TYPE>(image, orig);
741 mat[0][0] = mat[0][0]*invScales[0];
742 mat[1][0] = mat[1][0]*invScales[0];
743 mat[2][0] = mat[2][0]*invScales[0];
746 mat[0][1] = mat[0][1]*invScales[1];
747 mat[1][1] = mat[1][1]*invScales[1];
748 mat[2][1] = mat[2][1]*invScales[1];
751 mat[0][2] = mat[0][2]*invScales[2];
752 mat[1][2] = mat[1][2]*invScales[2];
753 mat[2][2] = mat[2][2]*invScales[2];
762 ITK_CLASS_TYPE::DirectionType::RowDimensions,
763 ITK_CLASS_TYPE::DirectionType::ColumnDimensions>(mat);
764 image->SetDirection(dirCosines);
794 template <
typename ITK_CLASS_TYPE>
797 bool correctSVS=
true,
798 bool suppressDimensionWarning=
false)
800 if (!image){
return; }
801 if (ITK_CLASS_TYPE::ImageDimension < 2){
804 "1 D itk world matrix conversion is not supported; "
805 "using identity as return value.");
810 const typename ITK_CLASS_TYPE::SpacingType spacing = image->GetSpacing();
811 const typename ITK_CLASS_TYPE::PointType origin = image->GetOrigin();
812 const typename ITK_CLASS_TYPE::DirectionType direction = image->GetDirection();
816 ITK_CLASS_TYPE::ImageDimension,
817 ITK_CLASS_TYPE::ImageDimension>(direction,
true, suppressDimensionWarning);
820 Matrix4 mat = Matrix4::getIdentity();
823 mat[0][0] = dirCosines[0][0]*spacing[0];
824 mat[1][0] = dirCosines[1][0]*spacing[0];
825 mat[2][0] = dirCosines[2][0]*spacing[0];
828 mat[0][1] = dirCosines[0][1]*spacing[1];
829 mat[1][1] = dirCosines[1][1]*spacing[1];
830 mat[2][1] = dirCosines[2][1]*spacing[1];
834 const double spacing2 = (ITK_CLASS_TYPE::ImageDimension > 2) ? spacing[2] : 1;
835 mat[0][2] = dirCosines[0][2]*spacing2;
836 mat[1][2] = dirCosines[1][2]*spacing2;
837 mat[2][2] = dirCosines[2][2]*spacing2;
840 mat[0][3] = origin[0];
841 mat[1][3] = origin[1];
842 mat[2][3] = (ITK_CLASS_TYPE::ImageDimension > 2) ? origin[2] : 0;
875 template <
typename ITK_INDATATYPE,
unsigned int DIM>
878 bool correctSVS=
true)
881 typedef itk::ImportImageFilter<ITK_INDATATYPE, DIM> ImportFilterType;
882 typedef typename ImportFilterType::Pointer ImportFilterPointerType;
884 ImportFilterPointerType importer = ImportFilterType::New();
889 importer->SetRegion(ITKRegionFromMLSubImgBox<ImportFilterType>(inSubImgBox));
892 setITKWorldFromMedicalImageProperty<ImportFilterType>(props, &(*importer), correctSVS);
895 importer->SetImportPointer(
static_cast<ITK_INDATATYPE*
>(inSubImg.
getData()),
896 static_cast<itk::SizeValueType
>(inSubImgBox.
getNumVoxels()),
922 template <
typename RETURN_TYPE_PTR,
typename FILTER_TYPE,
typename VOXEL_TYPE>
926 bool correctSVS=
true)
941 dataSubImg.
setBox(inImgBox);
953 typename FILTER_TYPE::Pointer importer = FILTER_TYPE::New();
957 importer->SetRegion(ITKRegionFromMLSubImgBox<FILTER_TYPE>(inImgBox));
960 setITKWorldFromMedicalImageProperty<FILTER_TYPE>(*pInImg, &(*importer), correctSVS);
965 importer->SetImportPointer(
static_cast<VOXEL_TYPE*
>(dataSubImg.
getData()), inImgBox.
getNumVoxels(),
false);
967 return importer->GetOutput();
970 ML_PRINT_ERROR(
"mlITKSupportToolFunctions::getInputAsItkImportImageAndSubImg()",
972 "Failed to request input image data, probably subsequent operations will fail");
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 which 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 safe access to the input image at index inputIndex.
Class which represents an image, which manages properties of an image and image data which is located...
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
This class manages/represents a rectangular 6d image region which 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)
Allocate 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 which is organized lin...
const DATATYPE * getData() const
Returns memory address of image region (Overloads method 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 double 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, which are inserted in classes to declare and implement additional class mem...
bool MLValueIs0WOM(MLint8 a)
Returns true if value is 0, otherwise 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
he 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.
#define ITKML_TYPENAME
Define it empty. In some cases VC++6 does not accept "typename" where non WIN32 compilers do.
@ 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 which is a signed 32 bit size_t on 32 bit platforms and 64 bit one on 64 bit ...
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...
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,...
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 3 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 ...
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 which 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...