diff --git a/Core/Code/DataManagement/mitkMatrix.h b/Core/Code/DataManagement/mitkMatrix.h index 0c183e4942..62e513b17c 100644 --- a/Core/Code/DataManagement/mitkMatrix.h +++ b/Core/Code/DataManagement/mitkMatrix.h @@ -1,171 +1,168 @@ /*=================================================================== 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 MITKMATRIX_H_ #define MITKMATRIX_H_ #include #include "mitkConstants.h" #include "mitkArray.h" #include "mitkEqual.h" namespace mitk { template< class T, unsigned int NRows = 3, unsigned int NColumns = 3 > class Matrix : public itk::Matrix { public: /** Standard class typedefs. */ typedef Matrix Self; typedef typename itk::Matrix::InternalMatrixType InternalMatrixType; /** Default constructor. */ Matrix() : itk::Matrix() {} /** Copy constructor. */ Matrix(const Matrix & matrix) : itk::Matrix(matrix) {} /** Copy constructor for itk compatibility */ Matrix(const itk::Matrix & matrix) : itk::Matrix(matrix) {} /**For every operator=, there should be an equivalent copy constructor. */ inline Matrix(const vnl_matrix< T > & matrix) : itk::Matrix(matrix) {} /**For every operator=, there should be an equivalent copy constructor. */ inline explicit Matrix(InternalMatrixType & matrix) : itk::Matrix(matrix) {} /** * Necessary because otherwise operator= is default operator= from Matrix. */ using itk::Matrix::operator=; /** * Warning: Array must have same dimension as Matrix */ void CopyToArray(ScalarType const array_p[NRows][NColumns]) const { for (int i = 0; i < this->GetPointDimension(); i++) { array_p[i] = this->GetElement(i); } } }; typedef Matrix Matrix2D; typedef Matrix Matrix3D; typedef Matrix Matrix4D; - // other matrix types used in mitk code - typedef vnl_matrix_fixed VnlMatrix3D; - /*! \brief Check for matrix equality with a user defined accuracy. As an equality metric the root mean squared error (RMS) of all elements is calculated. \param matrix1 first vnl matrix \param matrix2 second vnl matrix \param epsilon user defined accuracy bounds */ template 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 \param 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; r inline bool MatrixEqualElementWise(const itk::Matrix& matrix1,const itk::Matrix& matrix2,mitk::ScalarType epsilon=mitk::eps) { return mitk::MatrixEqualElementWise(matrix1.GetVnlMatrix(),matrix2.GetVnlMatrix(),epsilon); } } #endif /* MITKMATRIX_H_ */ diff --git a/Core/Code/DataManagement/mitkPoint.h b/Core/Code/DataManagement/mitkPoint.h index 97ae2fdad6..516be71d58 100644 --- a/Core/Code/DataManagement/mitkPoint.h +++ b/Core/Code/DataManagement/mitkPoint.h @@ -1,142 +1,151 @@ /*=================================================================== 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 MITKPOINT_H #define MITKPOINT_H #include #include "mitkConstants.h" #include "mitkArray.h" #include "mitkEqual.h" namespace mitk { //##Documentation //##@brief enumeration of the type a point can be enum PointSpecificationType { PTUNDEFINED = 0, PTSTART, PTCORNER, PTEDGE, PTEND }; template class Point : public itk::Point { public: /** Default constructor has nothing to do. */ Point() : itk::Point() {} /** Pass-through constructors for the Array base class. */ template< typename TPointValueType > Point(const Point< TPointValueType, NPointDimension > & r):itk::Point(r) {} template< typename TPointValueType > Point(const TPointValueType r[NPointDimension]):itk::Point(r) {} template< typename TPointValueType > Point(const TPointValueType & v):itk::Point(v) {} Point(const mitk::Point& r) : itk::Point(r) {} Point(const TCoordRep r[NPointDimension]):itk::Point(r) {} Point(const TCoordRep & v):itk::Point(v) {} Point(const itk::Point & p) : itk::Point(p) {} - /** Pass-through assignment operator for the Array base class. */ + /** - * Assignment Operator + * Copies the elements from array array to this. + * Note that this method will assign doubles to floats without complaining! + * + * @param array the array whose values shall be copied. Must overload [] operator. */ - Point< TCoordRep, NPointDimension > & - operator=(const Point & r) + template + void FromArray(const ArrayType& array) { - itk::Point::operator=(r); - return *this; + itk::FixedArray* thisP = dynamic_cast* >(this); + mitk::FromArray(*thisP, array); } - /** - * Assignment from a plain array + * Copies the elements of this point to an array of type ArrayType + * Note that this method will assign doubles to floats without complaining! + * + * @return the array holding the elements of this. Only requirement is that it overloads the [] operator */ - Point< TCoordRep, NPointDimension > & - operator=(const TCoordRep r[NPointDimension]) + template + ArrayType ToArray() { - itk::Point::operator=(r); - return *this; + ArrayType result = mitk::ToArray(*this); + return result; } - /** - * Warning: Array must have same dimension as Point + * Copies the values stored in this point into the array array. + * This method has to be used over the version returning the array when + * copying to pod arrays. + * Furthermore, the syntax may be a little bit nicer because ArrayType can be + * inferred from the parameter. + * + * @param array the array which should store the values of this. */ - void CopyToArray(ScalarType array_p[NPointDimension]) const + template + void ToArray(ArrayType array) { - for (unsigned int i = 0; i < this->GetPointDimension(); i++) - { - array_p[i] = this->GetElement(i); - } + mitk::ToArray(array, *this); } + }; typedef Point Point2D; typedef Point Point3D; typedef Point Point4D; typedef Point Point2I; typedef Point Point3I; typedef Point Point4I; /** * @ingroup MITKTestingAPI * * @param point1 Point to compare. * @param point2 Point to compare. * @param eps Tolerance for floating point comparison. * @param verbose Flag indicating detailed console output. * @return True if points are equal. */ template inline bool Equal(const itk::Point& point1, const itk::Point& point2, TCoordRep eps=mitk::eps, bool verbose=false) { bool isEqual = true; typename itk::Point::VectorType diff = point1-point2; for (unsigned int i=0; i -#include -#include -#include -#include #include -#include - #include "mitkConstants.h" namespace mitk { typedef vnl_quaternion Quaternion; } #endif /* MITKTYPEDEFS_H_ */ diff --git a/Core/Code/DataManagement/mitkVector.cpp b/Core/Code/DataManagement/mitkVector.cpp index 14a1fac6cd..9359e39422 100644 --- a/Core/Code/DataManagement/mitkVector.cpp +++ b/Core/Code/DataManagement/mitkVector.cpp @@ -1,18 +1,17 @@ /*=================================================================== 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 "mitkTypes.h" #include "mitkVector.h" diff --git a/Core/Code/DataManagement/mitkVector.h b/Core/Code/DataManagement/mitkVector.h index 888c01406e..bb2fa76e2c 100644 --- a/Core/Code/DataManagement/mitkVector.h +++ b/Core/Code/DataManagement/mitkVector.h @@ -1,250 +1,249 @@ /*=================================================================== 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_ #define MITKVECTOR_H_ #include #include #include #include "mitkConstants.h" #include "mitkArray.h" #include "mitkEqual.h" #include "mitkExceptionMacro.h" namespace mitk { template class Vector : public itk::Vector { public: /** * @brief Default constructor has nothing to do. */ explicit Vector() : itk::Vector() {} /** * @brief Copy constructor. */ explicit Vector(const mitk::Vector& r) : itk::Vector(r) {} /** * @brief Constructor to convert from itk::Vector to mitk::Vector. */ Vector(const itk::Vector& r) : itk::Vector(r) {} /** * @brief Constructor to convert an array to mitk::Vector * @param r the array. * @attention must have NVectorDimension valid arguments! */ Vector(const TCoordRep r[NVectorDimension]) : itk::Vector(r) {} /** * Constructor to initialize entire vector to one value. */ Vector(const TCoordRep & v) : itk::Vector(v) {} /** * @brief Constructor for vnl_vectors. * @throws mitk::Exception if vnl_vector.size() != NVectorDimension. */ Vector(const vnl_vector& vnlVector) : itk::Vector() { if (vnlVector.size() != NVectorDimension) mitkThrow() << "when constructing mitk::Vector from vnl_vector: sizes didn't match: mitk::Vector " << NVectorDimension << "; vnl_vector " << vnlVector.size(); for (unsigned int var = 0; (var < NVectorDimension) && (var < vnlVector.size()); ++var) { this->SetElement(var, vnlVector.get(var)); } } /** * @brief Constructor for vnl_vector_fixed. */ Vector(const vnl_vector_fixed& vnlVectorFixed) : itk::Vector() { for (unsigned int var = 0; var < NVectorDimension; ++var) { this->SetElement(var, vnlVectorFixed[var]); } }; /** * Copies the elements from array array to this. * Note that this method will assign doubles to floats without complaining! * * @param array the array whose values shall be copied. Must overload [] operator. */ template void FromArray(const ArrayType& array) { itk::FixedArray* thisP = dynamic_cast* >(this); mitk::FromArray(*thisP, array); } /** * Copies the elements of this vector to an array of type ArrayType * Note that this method will assign doubles to floats without complaining! * * @return the array holding the elements of this. Only requierement is that it overloads the [] operator */ template ArrayType ToArray() { ArrayType result = mitk::ToArray(*this); return result; } /** * Copies the values stored in this vector into the array array. * This method has to be used over the version returning the array when * copying to pod arrays. * Furthermore, the syntax may be a little bit nicer because ArrayType can be * inferred from the parameter. * * @param array the array which should store the values of this. */ template void ToArray(ArrayType array) { mitk::ToArray(array, *this); } /** * @brief User defined conversion of mitk::Vector to vnl_vector. * Note: the conversion to mitk::Vector to vnl_vector_fixed has not been implemented since this * would collide with the conversion vnl_vector to vnl_vector_fixed provided by vnl. */ operator vnl_vector () const { return this->GetVnlVector(); } }; // end mitk::Vector // convenience typedefs for often used mitk::Vector representations. typedef Vector Vector2D; typedef Vector Vector3D; typedef Vector Vector4D; // other vector types used in MITK typedef vnl_vector VnlVector; - typedef vnl_vector_ref VnlVectorRef; // The equal methods to compare vectors for equality are below: /** * @ingroup MITKTestingAPI * * @param vector1 Vector to compare. * @param vector2 Vector to compare. * @param eps Tolerance for floating point comparison. * @param verbose Flag indicating detailed console output. * @return True if vectors are equal. */ template inline bool Equal(const itk::Vector& vector1, const itk::Vector& vector2, TCoordRep eps=mitk::eps, bool verbose=false) { bool isEqual = true; typename itk::Vector::VectorType diff = vector1-vector2; for (unsigned int i=0; i inline bool Equal(const vnl_vector_fixed & vector1, const vnl_vector_fixed& vector2, TCoordRep eps=mitk::eps, bool verbose=false) { vnl_vector_fixed diff = vector1-vector2; bool isEqual = true; for( unsigned int i=0; i +#include + #include "mitkConstants.h" #include "mitkVector.h" +#include "mitkPoint.h" +#include "mitkMatrix.h" template class VectorTraits { public: typedef T ValueType; }; template <> class VectorTraits { public: typedef mitk::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 mitk::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< itk::Vector > { public: typedef double ValueType; }; template<> class VectorTraits< itk::Vector > { public: typedef int ValueType; }; template<> class VectorTraits< mitk::Vector > { public: typedef double 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< 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< mitk::Point > { public: typedef int ValueType; }; namespace mitk { 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 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 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 /* MITKOLDTYPECONVERSIONS_H_ */ diff --git a/Core/Code/Testing/mitkTypeConversionOpenCVTest.cpp b/Core/Code/Testing/mitkTypeConversionOpenCVTest.cpp index 103f08e437..0056e7ab06 100644 --- a/Core/Code/Testing/mitkTypeConversionOpenCVTest.cpp +++ b/Core/Code/Testing/mitkTypeConversionOpenCVTest.cpp @@ -1,104 +1,104 @@ /*=================================================================== 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 #include "itkVector.h" -#include + #include #include "vnl/vnl_math.h" #include "mitkTestingMacros.h" #include "mitkConstants.h" #include "mitkVector.h" #include "mitkTypes.h" // for Equals #include using namespace mitk; /** * these static variables are used in the test functions * * The variable which should be copied into is set to its original value. * The value which should be copied is set to valuesToCopy. * * Then the copying takes place. The test is successful, if the variable which * should be copied into holds the valuesToCopy afterwards and is equal to the * vector which should be copied. */ static const ScalarType originalValues[] = {1.123456789987, 2.789456321456, 3.123654789987456}; static const ScalarType valuesToCopy[] = {4.654789123321, 5.987456789321, 6.321654987789546}; /** * @brief Convenience method to test if one vector has been assigned successfully to the other. * * More specifically, tests if v1 = v2 was performed correctly. * * @param v1 The vector v1 of the assignment v1 = v2 * @param v2 The vector v2 of the assignment v1 = v2 * @param v1Name The type name of v1 (e.g.: mitk::Vector3D). Necessary for the correct test output. * @param v2Name The type name of v2 (e.g.: mitk::Vector3D). Necessary for the correct test output. * @param eps defines the allowed tolerance when testing for equality. */ template static void TestForEquality(T1 v1, T2 v2, std::string v1Name, std::string v2Name, ScalarType eps = mitk::eps) { MITK_TEST_CONDITION( EqualArray(v1, v2, 3, eps), "\nAssigning " << v2Name << " to " << v1Name << ":\n both are equal") } const float epsDouble2Float = vnl_math::float_eps * 10.0; static void Test_mitk2opencv() { cv::Vec3d opencvVector(originalValues[0], originalValues[1], originalValues[2]); mitk::Vector3D vector3D = valuesToCopy; opencvVector = vector3D.ToArray(); TestForEquality(opencvVector, vector3D, "cv::Vec3d", "mitk::Vector3D"); } static void Test_opencv2mitk() { mitk::Vector3D vector3D = originalValues; cv::Vec3d opencvVector(valuesToCopy[0], valuesToCopy[1], valuesToCopy[2]); vector3D.FromArray(opencvVector); TestForEquality(vector3D, opencvVector, "mitk::Vector3D", "cv::Vec3d"); } /** * @brief Test the conversions from and to opencv from mitk types. * */ int mitkTypeConversionOpenCVTest(int /*argc*/ , char* /*argv*/[]) { // always start with this! MITK_TEST_BEGIN("OpenCVConversionTest") Test_mitk2opencv(); Test_opencv2mitk(); MITK_TEST_END() } diff --git a/Core/Code/Testing/mitkTypePointConversionTest.cpp b/Core/Code/Testing/mitkTypePointConversionTest.cpp index 17b3d1ffec..bea3c9b0e4 100644 --- a/Core/Code/Testing/mitkTypePointConversionTest.cpp +++ b/Core/Code/Testing/mitkTypePointConversionTest.cpp @@ -1,162 +1,156 @@ /*=================================================================== 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 "mitkConstants.h" #include "mitkTypes.h" // for Equal method #include "mitkPoint.h" #include "itkPoint.h" #include "vtkPoints.h" #include "vtkSmartPointer.h" #include using namespace mitk; static vtkSmartPointer a_vtkPoints = vtkSmartPointer::New(); static const ScalarType originalValues[] = {1.0, 2.0, 3.0}; static const ScalarType valuesToCopy[] = {4.0, 5.0, 6.0}; static void Setup(void) { a_vtkPoints->Initialize(); } + +/** + * @brief Convenience method to test if one vector has been assigned successfully to the other. + * + * More specifically, tests if v1 = v2 was performed correctly. + * + * @param v1 The vector v1 of the assignment v1 = v2 + * @param v2 The vector v2 of the assignment v1 = v2 + * @param v1Name The type name of v1 (e.g.: mitk::Vector3D). Necessary for the correct test output. + * @param v2Name The type name of v2 (e.g.: mitk::Vector3D). Necessary for the correct test output. +* @param eps defines the allowed tolerance when testing for equality. + */ +template +static void TestForEquality(T1 v1, T2 v2, std::string v1Name, std::string v2Name, ScalarType eps = mitk::eps) +{ + MITK_TEST_CONDITION( EqualArray(v1, v2, 3, eps), "\nAssigning " << v2Name << " to " << v1Name << ":\n both are equal") +} + + + static void Test_Mitk2Itk_PointCompatibility() { Setup(); itk::Point itkPoint3D = originalValues; mitk::Point3D point3D = valuesToCopy; itkPoint3D = point3D; - MITK_TEST_CONDITION(itkPoint3D == point3D, "mitk point assigned to itk point") - MITK_TEST_CONDITION(itkPoint3D == valuesToCopy, "correct values were assigned") + TestForEquality(itkPoint3D, point3D, "itk::Point", "mitk:Point"); } static void Test_Itk2Mitk_PointCompatibility() { Setup(); mitk::Point3D point3D = originalValues; itk::Point itkPoint3D = valuesToCopy; point3D = itkPoint3D; - MITK_TEST_CONDITION(point3D == itkPoint3D, "itk point assigned to mitk point") - MITK_TEST_CONDITION(point3D == valuesToCopy, "correct values were assigned") + TestForEquality(point3D, itkPoint3D, "mitk:Point", "itk::Point"); } static void Test_Vtk2Mitk_PointCompatibility() { Setup(); mitk::Point3D point3D = originalValues; 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 == valuesToCopy, "correct values were assigned") -} + TestForEquality(point3D, vtkPoint, "mitk:Point", "vtkPoint"); -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); + point3D.ToArray(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") + TestForEquality(podPoint, point3D, "POD point", "mitk::Point"); } static void Test_Pod2Mitk_PointCompatibility() { - mitk::Point3D point3D = originalValues; + itk::Point 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") + TestForEquality(point3D, podPoint, "mitk::Point3D", "POD point"); } - -static void Test_Mitk2Vnl_PointCompatibility() +static void Test_Point2Vector() { - Setup(); - - //vnl_vector_fixed copiedPoint; + itk::Point point3D = valuesToCopy; + itk::Vector vector3D = originalValues; - // copiedPoint = mitk2vnl(point3D); + point3D = vector3D; - //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") + TestForEquality(point3D, vector3D, "mitk::Point", "mitk::Vector"); } + /** * Test the conversions from and to mitk point types */ int mitkTypePointConversionTest(int /*argc*/ , char* /*argv*/[]) { // always start with this! MITK_TEST_BEGIN("PointConversionTest") + Test_Point2Vector(); + Test_Mitk2Itk_PointCompatibility(); Test_Itk2Mitk_PointCompatibility(); Test_Vtk2Mitk_PointCompatibility(); - Test_Mitk2Vtk_PointCompatibility(); - - Test_Mitk2Vnl_PointCompatibility(); Test_Mitk2Pod_PointCompatibility(); Test_Pod2Mitk_PointCompatibility(); MITK_TEST_END() } diff --git a/Core/Code/Testing/mitkTypeVectorConversionTest.cpp b/Core/Code/Testing/mitkTypeVectorConversionTest.cpp index e64a31b647..d96d01cf97 100644 --- a/Core/Code/Testing/mitkTypeVectorConversionTest.cpp +++ b/Core/Code/Testing/mitkTypeVectorConversionTest.cpp @@ -1,242 +1,255 @@ /*=================================================================== 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 #include "itkVector.h" -#include + #include #include "vnl/vnl_math.h" #include "mitkTestingMacros.h" #include "mitkConstants.h" #include "mitkVector.h" -#include "mitkTypes.h" // for Equals +#include "mitkPoint.h" using namespace mitk; + + /** * these static variables are used in the test functions * * The variable which should be copied into is set to its original value. * The value which should be copied is set to valuesToCopy. * * Then the copying takes place. The test is successful, if the variable which * should be copied into holds the valuesToCopy afterwards and is equal to the * vector which should be copied. */ static const ScalarType originalValues[] = {1.123456789987, 2.789456321456, 3.123654789987456}; static const ScalarType valuesToCopy[] = {4.654789123321, 5.987456789321, 6.321654987789546}; /** * @brief Convenience method to test if one vector has been assigned successfully to the other. * * More specifically, tests if v1 = v2 was performed correctly. * * @param v1 The vector v1 of the assignment v1 = v2 * @param v2 The vector v2 of the assignment v1 = v2 * @param v1Name The type name of v1 (e.g.: mitk::Vector3D). Necessary for the correct test output. * @param v2Name The type name of v2 (e.g.: mitk::Vector3D). Necessary for the correct test output. * @param eps defines the allowed tolerance when testing for equality. */ template static void TestForEquality(T1 v1, T2 v2, std::string v1Name, std::string v2Name, ScalarType eps = mitk::eps) { MITK_TEST_CONDITION( EqualArray(v1, v2, 3, eps), "\nAssigning " << v2Name << " to " << v1Name << ":\n both are equal") } const float epsDouble2Float = vnl_math::float_eps * 10.0; static void Test_pod2mitk(void) { mitk::Vector3D vector3D = valuesToCopy; TestForEquality(vector3D, valuesToCopy, "mitk::Vector3D", "double POD"); } static void Test_mitk2pod(void) { ScalarType podArray[3]; mitk::Vector3D vector3D = valuesToCopy; vector3D.ToArray(podArray); TestForEquality(podArray, vector3D, "double POD", "mitk::Vector3D"); } static void Test_oneElement2mitk(void) { double twos[] = {2.0, 2.0, 2.0}; mitk::Vector vector3D(2.0); MITK_TEST_CONDITION(EqualArray(vector3D, twos, 3), "\none values initializes all elements to this value") } static void Test_itk2mitk(void) { Vector3D vector3D = originalValues; itk::Vector itkVector = valuesToCopy; vector3D = itkVector; TestForEquality(vector3D, itkVector, "mitk::Vector3D", "itk::Vector"); } static void Test_mitk2itk(void) { Vector3D vector3D = valuesToCopy; itk::Vector itkVector = originalValues; itkVector = vector3D; TestForEquality(itkVector, vector3D, "itk::Vector", "mitk::Vector3D"); } static void Test_vnlfixed2mitk(void) { mitk::Vector3D vector3D = originalValues; vnl_vector_fixed vnlVectorFixed(valuesToCopy); vector3D = vnlVectorFixed; TestForEquality(vector3D, vnlVectorFixed, "mitk::Vector3D", "vnl_vector_fixed"); } static void Test_mitk2vnlfixed(void) { vnl_vector_fixed vnlVectorFixed(originalValues); mitk::Vector3D vector3D = valuesToCopy; vnlVectorFixed = vector3D; TestForEquality(vnlVectorFixed, vector3D, "vnl_vector_fixed", "mitk::Vector3D"); } static void Test_vnl2mitk(void) { mitk::Vector3D vector3D = originalValues; vnl_vector vnlVector(3); vnlVector.set(valuesToCopy); vector3D = vnlVector; TestForEquality(vector3D, vnlVector, "mitk::Vector3D", "vnl_vector"); } static void Test_mitk2vnl(void) { vnl_vector vnlVector(3); vnlVector.set(originalValues); mitk::Vector3D vector3D = valuesToCopy; vnlVector = vector3D; TestForEquality(vnlVector, vector3D, "vnl_vector", "mitk::Vector3D"); } /** * @brief Tests if an exception is thrown when constructing an mitk::Vector form a vnl_vector of not suited size. */ static void Test_vnl2mitk_WrongVnlVectorSize() { ScalarType largerValuesToCopy[] = {4.12345678910, 5.10987654321, 6.123456789132456, 7.123456987789456}; mitk::Vector3D vector3D = originalValues; vnl_vector vnlVector(4); vnlVector.set(largerValuesToCopy); MITK_TEST_FOR_EXCEPTION(mitk::Exception&, vector3D = vnlVector;) } static void Test_ToArray_DifferentType(void) { float podArray[3]; for (int var = 0; var < 3; ++var) { podArray[var] = originalValues[var]; } mitk::Vector3D vector3D = valuesToCopy; vector3D.ToArray(podArray); TestForEquality(podArray, vector3D, "float POD", "mitk::Vector3D", epsDouble2Float); } static void Test_FromArray_DifferentType(void) { mitk::Vector3D vector3D = originalValues; float podArray[3]; for (int var = 0; var < 3; ++var) { podArray[var] = valuesToCopy[var]; } vector3D.FromArray(podArray); TestForEquality(vector3D, podArray, "mitk::Vector3D", "float POD", epsDouble2Float); } +static void Test_vector2point() +{ + mitk::Point3D point3D = originalValues; + mitk::Vector3D vector3D = valuesToCopy; + + vector3D = point3D.GetVectorFromOrigin(); + + TestForEquality(point3D, vector3D, "mitk::Point3D", "mitk::Vector3D"); +} /** * @brief Test the conversions from and to the mitk::Vector type. * * Tests for every conversion, if it can be done and in a second test, if the assignment of a * different type succeeds. E.g., assign a double vnl_vector to a float mitk::Vector * In cases where the size can not be determined during compile time it is checked if the assignment of * a differently sized vector yields an error. */ int mitkTypeVectorConversionTest(int /*argc*/ , char* /*argv*/[]) { // always start with this! MITK_TEST_BEGIN("VectorConversionTest") + Test_vector2point(); + Test_pod2mitk(); Test_mitk2pod(); Test_oneElement2mitk(); Test_itk2mitk(); Test_mitk2itk(); Test_vnlfixed2mitk(); Test_mitk2vnlfixed(); Test_vnl2mitk(); Test_mitk2vnl(); Test_vnl2mitk_WrongVnlVectorSize(); /** * The ToArray and FromArray can assign non equal types by implicit primitive type conversion. * The next two tests show this behavior */ Test_ToArray_DifferentType(); Test_FromArray_DifferentType(); MITK_TEST_END() }