diff --git a/Core/Code/DataManagement/mitkVector.h b/Core/Code/DataManagement/mitkVector.h index 0e381148e8..eadae195ec 100644 --- a/Core/Code/DataManagement/mitkVector.h +++ b/Core/Code/DataManagement/mitkVector.h @@ -1,506 +1,515 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef MITKVECTOR_H_HEADER_INCLUDED_C1EBD0AD #define MITKVECTOR_H_HEADER_INCLUDED_C1EBD0AD // this is needed for memcopy in ITK // can be removed when fixed in ITK #include #include #include #include #include #include #include #include #include #include #ifndef DOXYGEN_SKIP namespace mitk { typedef double ScalarType; typedef itk::Matrix Matrix3D; typedef itk::Matrix Matrix4D; typedef vnl_matrix_fixed VnlMatrix3D; typedef itk::Transform Transform3D; typedef vnl_vector VnlVector; typedef vnl_vector_ref VnlVectorRef; template class Point : public itk::Point { public: /** Default constructor has nothing to do. */ mitk::Point() {} /** Pass-through constructors for the Array base class. */ mitk::Point(const mitk::Point& r) : itk::Point(r) {} - mitk::Point(const ValueType r[3]):itk::Point(r) {} + mitk::Point(const ValueType r[NPointDimension]):itk::Point(r) {} mitk::Point(const ValueType & v):itk::Point(v) {} mitk::Point(const itk::Point r) : itk::Point(r) {} - operator itk::Point () const { return itk::Point point; } + /** + * Warning: Array must have same dimension as Point + */ + void CopyToArray(ScalarType* const array_p) const + { + for (int i = 0; i < this->GetPointDimension(); i++) + { + array_p[i] = this->GetElement(i); + } + } }; typedef mitk::Point Point3D; typedef mitk::Point Point2D; typedef mitk::Point Point4D; typedef mitk::Point Point2I; typedef mitk::Point Point3I; typedef mitk::Point Point4I; typedef itk::Vector Vector2D; typedef itk::Vector Vector3D; typedef itk::Index<3> Index3D; typedef itk::ContinuousIndex ContinuousIndex3D; typedef vnl_quaternion Quaternion; //##Documentation //##@brief enumeration of the type a point can be enum PointSpecificationType { PTUNDEFINED = 0, PTSTART, PTCORNER, PTEDGE, PTEND }; typedef itk::NumericTraits ScalarTypeNumericTraits; MITK_CORE_EXPORT extern const ScalarType eps; MITK_CORE_EXPORT extern const ScalarType sqrteps; MITK_CORE_EXPORT extern const double large; template class VectorTraits { public: typedef T ValueType; }; template <> class VectorTraits { public: typedef ScalarType ValueType; }; template<> class VectorTraits { public: typedef float ValueType; }; template<> class VectorTraits< itk::Index<5> > { public: typedef itk::Index<5>::IndexValueType ValueType; }; template<> class VectorTraits< itk::Index<3> > { public: typedef itk::Index<3>::IndexValueType ValueType; }; template<> class VectorTraits< long int [3]> { public: typedef long int ValueType; }; template<> class VectorTraits< float [3]> { public: typedef float ValueType; }; template<> class VectorTraits< double [3]> { public: typedef double ValueType; }; template<> class VectorTraits< vnl_vector_fixed > { public: typedef ScalarType ValueType; }; template<> class VectorTraits< long unsigned int[3]> { public: typedef long unsigned int ValueType; }; template<> class VectorTraits< unsigned int *> { public: typedef unsigned int ValueType; }; template<> class VectorTraits< double[4] > { public: typedef double ValueType; }; template<> class VectorTraits< itk::Vector > { public: typedef float ValueType; }; template<> class VectorTraits< mitk::Point > { public: typedef float ValueType; }; template<> class VectorTraits< mitk::Point > { public: typedef float ValueType; }; template<> class VectorTraits< itk::Point > { public: typedef float ValueType; }; template<> class VectorTraits< itk::Point > { public: typedef float ValueType; }; template<> class VectorTraits< itk::Vector > { public: typedef double ValueType; }; template<> class VectorTraits< mitk::Point > { public: typedef double ValueType; }; template<> class VectorTraits< mitk::Point > { public: typedef double ValueType; }; template<> class VectorTraits< itk::Point > { public: typedef double ValueType; }; template<> class VectorTraits< itk::Point > { public: typedef double ValueType; }; template<> class VectorTraits< itk::Vector > { public: typedef int ValueType; }; template<> class VectorTraits< mitk::Point > { public: typedef int ValueType; }; template inline void itk2vtk(const Tin& in, Tout& out) { out[0]=(typename VectorTraits::ValueType)(in[0]); out[1]=(typename VectorTraits::ValueType)(in[1]); out[2]=(typename VectorTraits::ValueType)(in[2]); } template inline void vtk2itk(const Tin& in, Tout& out) { out[0]=(typename VectorTraits::ValueType)(in[0]); out[1]=(typename VectorTraits::ValueType)(in[1]); out[2]=(typename VectorTraits::ValueType)(in[2]); } template inline void FillVector3D(Tout& out, ScalarType x, ScalarType y, ScalarType z) { out[0] = (typename VectorTraits::ValueType)x; out[1] = (typename VectorTraits::ValueType)y; out[2] = (typename VectorTraits::ValueType)z; } template inline void FillVector4D(Tout& out, ScalarType x, ScalarType y, ScalarType z, ScalarType t) { out[0] = (typename VectorTraits::ValueType)x; out[1] = (typename VectorTraits::ValueType)y; out[2] = (typename VectorTraits::ValueType)z; out[3] = (typename VectorTraits::ValueType)t; } template inline void vnl2vtk(const vnl_vector& in, Tout *out) { unsigned int i; for(i=0; i inline void vtk2vnl(const Tin *in, vnl_vector& out) { unsigned int i; for(i=0; i inline void vtk2vnlref(const Tin *in, vnl_vector_ref& out) { unsigned int i; for(i=0; i inline void vnl2vtk(const vnl_vector_fixed& in, Tout *out) { unsigned int i; for(i=0; i inline void vtk2vnl(const Tin *in, vnl_vector_fixed& out) { unsigned int i; for(i=0; i itk::Vector operator+(const itk::Vector &vector, const itk::Point &point) { itk::Vector sub; for( unsigned int i=0; i inline itk::Vector& operator+=(itk::Vector &vector, const itk::Point &point) { for( unsigned int i=0; i itk::Vector operator-(const itk::Vector &vector, const itk::Point &point) { itk::Vector sub; for( unsigned int i=0; i inline itk::Vector& operator-=(itk::Vector &vector, const itk::Point &point) { for( unsigned int i=0; i inline bool MatrixEqualRMS(const vnl_matrix_fixed& matrix1,const vnl_matrix_fixed& matrix2,mitk::ScalarType epsilon=mitk::eps) { if ( (matrix1.rows() == matrix2.rows()) && (matrix1.cols() == matrix2.cols()) ) { vnl_matrix_fixed differenceMatrix = matrix1-matrix2; if (differenceMatrix.rms() inline bool MatrixEqualRMS(const itk::Matrix& matrix1,const itk::Matrix& matrix2,mitk::ScalarType epsilon=mitk::eps) { return mitk::MatrixEqualRMS(matrix1.GetVnlMatrix(),matrix2.GetVnlMatrix(),epsilon); } /*! \brief Check for element-wise matrix equality with a user defined accuracy. \param matrix1 first vnl matrix \param matrix2 second vnl matrix \epsilon user defined accuracy bounds */ template inline bool MatrixEqualElementWise(const vnl_matrix_fixed& matrix1,const vnl_matrix_fixed& matrix2,mitk::ScalarType epsilon=mitk::eps) { if ( (matrix1.rows() == matrix2.rows()) && (matrix1.cols() == matrix2.cols()) ) { for( unsigned int r=0; repsilon) { return false; } } } return true; } else { return false; } } /*! \brief Check for element-wise matrix equality with a user defined accuracy. \param matrix1 first itk matrix \param matrix2 second itk matrix \epsilon user defined accuracy bounds */ template inline bool MatrixEqualElementWise(const itk::Matrix& matrix1,const itk::Matrix& matrix2,mitk::ScalarType epsilon=mitk::eps) { return mitk::MatrixEqualElementWise(matrix1.GetVnlMatrix(),matrix2.GetVnlMatrix(),epsilon); } template inline bool Equal(const itk::Vector& vector1, const itk::Vector& vector2, TCoordRep eps=mitk::eps) { typename itk::Vector::VectorType diff = vector1-vector2; for (unsigned int i=0; ieps || diff[i]<-eps) return false; return true; } template inline bool Equal(const itk::Point& vector1, const itk::Point& vector2, TCoordRep eps=mitk::eps) { typename itk::Point::VectorType diff = vector1-vector2; for (unsigned int i=0; ieps || diff[i]<-eps) return false; return true; } inline bool Equal(const mitk::VnlVector& vector1, const mitk::VnlVector& vector2, ScalarType eps=mitk::eps) { mitk::VnlVector diff = vector1-vector2; for (unsigned int i=0; ieps || diff[i]<-eps) return false; return true; } inline bool Equal(ScalarType scalar1, ScalarType scalar2, ScalarType eps=mitk::eps) { return fabs(scalar1-scalar2) < eps; } template inline bool Equal(const vnl_vector_fixed & vector1, const vnl_vector_fixed& vector2, TCoordRep eps=mitk::eps) { vnl_vector_fixed diff = vector1-vector2; bool returnValue = true; for( unsigned int i=0; ieps || diff[i]<-eps) { returnValue = false; } } return returnValue; } template inline void TransferMatrix(const itk::Matrix& in, itk::Matrix& out) { for (unsigned int i = 0; i < in.RowDimensions; ++i) for (unsigned int j = 0; j < in.ColumnDimensions; ++j) out[i][j] = in[i][j]; } } // namespace mitk #endif //DOXYGEN_SKIP /* * This part of the code has been shifted here to avoid compiler clashes * caused by including before the declaration of * the Equal() methods above. This problem occurs when using MSVC and is * probably related to a compiler bug. */ #include namespace mitk { typedef itk::AffineGeometryFrame::TransformType AffineTransform3D; } #define mitkSetConstReferenceMacro(name,type) \ virtual void Set##name (const type & _arg) \ { \ itkDebugMacro("setting " << #name " to " << _arg ); \ if (this->m_##name != _arg) \ { \ this->m_##name = _arg; \ this->Modified(); \ } \ } #define mitkSetVectorMacro(name,type) \ mitkSetConstReferenceMacro(name,type) #define mitkGetVectorMacro(name,type) \ itkGetConstReferenceMacro(name,type) #endif /* MITKVECTOR_H_HEADER_INCLUDED_C1EBD0AD */ diff --git a/Core/Code/Testing/mitkTypeConversionTest.cpp b/Core/Code/Testing/mitkTypeConversionTest.cpp index 432098b57b..e598f1be49 100644 --- a/Core/Code/Testing/mitkTypeConversionTest.cpp +++ b/Core/Code/Testing/mitkTypeConversionTest.cpp @@ -1,173 +1,207 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkTestingMacros.h" #include "mitkVector.h" #include "itkPoint.h" #include "vtkPoints.h" #include "vtkSmartPointer.h" #include "vnl/vnl_vector_ref.h" #include using namespace mitk; static vtkSmartPointer a_vtkPoints = vtkSmartPointer::New(); static const ScalarType originalValues[] = {1.0, 2.0, 3.0}; -static const ScalarType copiedValues[] = {4.0, 5.0, 6.0}; +static const ScalarType valuesToCopy[] = {4.0, 5.0, 6.0}; static void Setup(void) { a_vtkPoints->Initialize(); } static void Test_Mitk2Itk_PointCompatibility() { Setup(); itk::Point itkPoint3D = originalValues; - mitk::Point3D point3D = copiedValues; + mitk::Point3D point3D = valuesToCopy; itkPoint3D = point3D; MITK_TEST_CONDITION(itkPoint3D == point3D, "mitk point assigned to itk point") - MITK_TEST_CONDITION(itkPoint3D == copiedValues, "correct values were assigned") + MITK_TEST_CONDITION(itkPoint3D == valuesToCopy, "correct values were assigned") } -void testFunction(Point3D& point) -{ - point.SetElement(0, 1); -} static void Test_Itk2Mitk_PointCompatibility() { Setup(); - const float testValues[] = {4.0, 5.0, 6.0}; - mitk::Point3D point3D = originalValues; - itk::Point itkPoint3D = copiedValues; + itk::Point itkPoint3D = valuesToCopy; - itkPoint3D = point3D; - - point3D = itkPoint3D; - - testFunction(static_cast(itkPoint3D)); + point3D = itkPoint3D; MITK_TEST_CONDITION(point3D == itkPoint3D, "itk point assigned to mitk point") - MITK_TEST_CONDITION(point3D == copiedValues, "correct values were assigned") + MITK_TEST_CONDITION(point3D == valuesToCopy, "correct values were assigned") } static void Test_Vtk2Mitk_PointCompatibility() { Setup(); mitk::Point3D point3D = originalValues; - a_vtkPoints->InsertNextPoint(copiedValues); - + a_vtkPoints->InsertNextPoint(valuesToCopy); double vtkPoint[3]; a_vtkPoints->GetPoint(0, vtkPoint); point3D = vtkPoint; MITK_TEST_CONDITION(point3D == vtkPoint, "vtkPoint assigned to mitk point") - MITK_TEST_CONDITION(point3D == copiedValues, "correct values were assigned") + MITK_TEST_CONDITION(point3D == valuesToCopy, "correct values were assigned") +} + +static void Test_Mitk2Vtk_PointCompatibility() +{ + Setup(); + double vtkPoint[3]; + mitk::Point3D point3D = valuesToCopy; + + //a_vtkPoints->InsertNextPoint(point3D.GetAsArray()); + // a_vtkPoints->GetPoint(0, vtkPoint); + + MITK_TEST_CONDITION(point3D == vtkPoint, "MITK point assigned to VTK point") + MITK_TEST_CONDITION(Equal(vtkPoint[0], valuesToCopy[0]) + && Equal(vtkPoint[1], valuesToCopy[1]) + && Equal(vtkPoint[2], valuesToCopy[2]), "correct values were assigned") +} + + +static void Test_Mitk2Pod_PointCompatibility() +{ + ScalarType podPoint[] = {1.0, 2.0, 3.0}; + mitk::Point3D point3D = valuesToCopy; + + point3D.CopyToArray(podPoint); + + MITK_TEST_CONDITION(point3D == podPoint, "MITK point assigned to POD point") + MITK_TEST_CONDITION(Equal(podPoint[0], valuesToCopy[0]) + && Equal(podPoint[1], valuesToCopy[1]) + && Equal(podPoint[2], valuesToCopy[2]), "correct values were assigned") + +} + +static void Test_Pod2Mitk_PointCompatibility() +{ + mitk::Point3D point3D = originalValues; + ScalarType podPoint[] = {4.0, 5.0, 6.0}; + + point3D = podPoint; + + MITK_TEST_CONDITION(point3D == podPoint, "POD point assigned to MITK point") + MITK_TEST_CONDITION(point3D == valuesToCopy, "correct values were assigned") } template< class T, unsigned int NVectorDimension> itk::Vector toItk(const vnl_vector_fixed& vnlVectorFixed) { return toItk(vnlVectorFixed.as_vector()); } template< class T, unsigned int NVectorDimension> itk::Vector toItk(const vnl_vector& vnlVector) { itk::Vector vector; vector.SetVnlVector(vnlVector); return vector; } static void Test_Vnl2Mitk_VectorFixedCompatibility() { Setup(); mitk::Vector3D vector3D = originalValues; - vnl_vector_fixed vnlVectorFixed(copiedValues); + vnl_vector_fixed vnlVectorFixed(valuesToCopy); vector3D = toItk(vnlVectorFixed); MITK_TEST_CONDITION( vector3D.GetVnlVector() == vnlVectorFixed, "vnl_vector_fixed assigned to mitk vector") - MITK_TEST_CONDITION( vector3D == copiedValues, "correct values were assigned" ) + MITK_TEST_CONDITION( vector3D == valuesToCopy, "correct values were assigned" ) } static void Test_Vnl2Mitk_VectorCompatibility() { Setup(); mitk::Vector3D vector3D = originalValues; vnl_vector vnlVector(3); - vnlVector.set(copiedValues); + vnlVector.set(valuesToCopy); vector3D = toItk(vnlVector); MITK_TEST_CONDITION( vector3D.GetVnlVector() == vnlVector, "vnl_vector assigned to mitk vector") - MITK_TEST_CONDITION( vector3D == copiedValues, "correct values were assigned" ) + MITK_TEST_CONDITION( vector3D == valuesToCopy, "correct values were assigned" ) } static void Test_Mitk2Vnl_PointCompatibility() { Setup(); //vnl_vector_fixed copiedPoint; // copiedPoint = mitk2vnl(point3D); //MITK_TEST_CONDITION( // Equal(static_cast(copiedPoint[0]), point3D[0]) // && Equal(static_cast(copiedPoint[1]), point3D[1]) // && Equal(static_cast(copiedPoint[2]), point3D[2]), "mitk point assigned to vnl point") } /** * Test the conversions from and to mitk types */ int mitkTypeConversionTest(int /*argc*/ , char* /*argv*/[]) { // always start with this! MITK_TEST_BEGIN("TypeConversionTest") Test_Mitk2Itk_PointCompatibility(); Test_Itk2Mitk_PointCompatibility(); Test_Vtk2Mitk_PointCompatibility(); + Test_Mitk2Vtk_PointCompatibility(); Test_Vnl2Mitk_VectorFixedCompatibility(); Test_Vnl2Mitk_VectorCompatibility(); Test_Mitk2Vnl_PointCompatibility(); + Test_Mitk2Pod_PointCompatibility(); + Test_Pod2Mitk_PointCompatibility(); + MITK_TEST_END() }