diff --git a/Core/Code/DataManagement/mitkVector.h b/Core/Code/DataManagement/mitkVector.h index 628ce5ee5f..5de8ef90fb 100644 --- a/Core/Code/DataManagement/mitkVector.h +++ b/Core/Code/DataManagement/mitkVector.h @@ -1,162 +1,162 @@ /*=================================================================== 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 "mitkTypeBasics.h" #include "mitkExceptionMacro.h" namespace mitk { template class Vector : public itk::Vector { public: /** * @brief Default constructor has nothing to do. */ Vector() : itk::Vector() {} /** * @brief Copy constructor. * Can convert non-identical coordinate representations. * E.g. use this to convert from mitk::Vector to mitk::Vector */ template Vector(const Vector& r) : itk::Vector(r) {} /** * @brief Constructor to convert from itk::Vector to mitk::Vector. * Can convert non-identical coordinate representations. * E.g. use this to convert from itk::Vector to mitk::Vector */ template Vector(const itk::Vector& r) : itk::Vector(r) {} /** * @brief Constructor to convert an array to mitk::Vector * Can convert non-identical coordinate representations. * @param r the array. * @attention must have NVectorDimension valid arguments! */ template Vector(const TOtherCoordRep r[NVectorDimension]) : itk::Vector() { // TODO SW: very strange: you could leave the following lines and gcc would still copy the array! for (int var = 0; var < 3; ++var) { this->SetElement(var, static_cast(r[var])); } } /** * Constructor to initialize entire vector to one value. */ Vector(const TCoordRep & v) : itk::Vector(v) {} /** * @brief Constructor for vnl_vectors. * Can convert non-identical coordinate representations. * @throws mitk::Exception if vnl_vector.size() != NVectorDimension. */ template 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 (int var = 0; (var < NVectorDimension) && (var < vnlVector.size()); ++var) { this->SetElement(var, static_cast(vnlVector.get(var))); } } /** * @brief Constructor for vnl_vecto_fixed. * Can convert non-identical coordinate representations. */ template Vector(const vnl_vector_fixed vnlVectorFixed) : itk::Vector() { for (int var = 0; var < 3; ++var) { this->SetElement(var, static_cast(vnlVectorFixed[var])); } }; /** * @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. */ template operator vnl_vector () const { Vector convertedVector = *this; vnl_vector vnlVector(convertedVector.GetVnlVector()); return vnlVector; } /** * @brief Copies the elements of this into an ArrayType T * @param[out] array the array which will hold the elements. Must be of a type which overrides the [] operator. - * @attention Must be of at least dimension NVectorDimension! + * @attention array must be of dimension NVectorDimension! */ template void ToArray(ArrayType& array) const { for (int i = 0; i < NVectorDimension; i++) { array[i] = this->GetElement(i); } } /** * @brief Copies elements of an ArrayType T to this Vector * @param array the array whose values will be copied into the Vector. Must be of a type which overrides the [] operator - * @attention Must be of dimension NVectorDimension! + * @attention array must be of dimension NVectorDimension! */ template void FromArray(const ArrayType& array) { for (unsigned short int var = 0; var < NVectorDimension; ++var) { this->SetElement(var, array[var]); } } }; // end mitk::Vector // convenience typedefs for often used mitk::Vector representations. typedef Vector Vector2D; typedef Vector Vector3D; typedef Vector Vector4D; } // end namespace mitk #endif /* MITKVECTOR_H_ */ diff --git a/Core/Code/Testing/mitkTypeVectorConversionTest.cpp b/Core/Code/Testing/mitkTypeVectorConversionTest.cpp index 576dafc479..a03bca9e85 100644 --- a/Core/Code/Testing/mitkTypeVectorConversionTest.cpp +++ b/Core/Code/Testing/mitkTypeVectorConversionTest.cpp @@ -1,346 +1,339 @@ /*=================================================================== 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 "mitkTypeBasics.h" #include "mitkVector.h" #include "mitkTypes.h" // for Equals #include using namespace mitk; /** * these private 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 float originalValuesFloat[] = {1.123456789987, 2.789456321456, 3.123654789987456}; static const ScalarType valuesToCopy[] = {4.654789123321, 5.987456789321, 6.321654987789546}; static const float valuesToCopyFloat[] = {4.654789123321, 5.987456789321, 6.321654987789546}; static float epsDouble2Float = vnl_math::float_eps * 10.0; template static void TestForEquality(T1 vectorToBeAssigned, T2 vectorToCopy, std::string nameToBeAssigned, std::string nameToCopy, ScalarType eps = mitk::eps) { - MITK_TEST_CONDITION( EqualArray(vectorToBeAssigned, vectorToCopy, 3, eps), "Assigning " << nameToCopy << " to " << nameToBeAssigned << ": 1. both are equal") + MITK_TEST_CONDITION( EqualArray(vectorToBeAssigned, vectorToCopy, 3, eps), "\nAssigning " << nameToCopy << " to " << nameToBeAssigned << ":\n1. both are equal") MITK_TEST_CONDITION( EqualArray(vectorToBeAssigned, valuesToCopy, 3, eps), "2. both hold values of " << nameToCopy) } static void Test_pod2mitk(void) { mitk::Vector3D vector3D = valuesToCopy; - MITK_TEST_CONDITION(EqualArray(vector3D, valuesToCopy, 3), "pod copied into mitk::Vector of same type") + TestForEquality(vector3D, valuesToCopy, "mitk::Vector3D", "double POD"); } static void Test_pod2mitk_DifferentType(void) { mitk::Vector3D vector3D = valuesToCopyFloat; MITK_TEST_CONDITION(EqualArray(vector3D, valuesToCopyFloat, 3, epsDouble2Float), "float pod copied into mitk::Vector") } static void Test_mitk2pod(void) { ScalarType podArray[3]; mitk::Vector3D vector3D = valuesToCopy; vector3D.ToArray(podArray); - MITK_TEST_CONDITION(EqualArray(podArray, vector3D, 3), "mitk::Vector copied into pod array") + TestForEquality(podArray, vector3D, "double POD", "mitk::Vector3D"); } static void Test_mitk2pod_DifferentType(void) { float podArray[3]; mitk::Vector3D vector3D = valuesToCopy; vector3D.ToArray(podArray); MITK_TEST_CONDITION(EqualArray(podArray, vector3D, 3, epsDouble2Float), "mitk::Vector copied into float pod array") } 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), "one values initializes all elements to this value") + MITK_TEST_CONDITION(EqualArray(vector3D, twos, 3), "\none values initializes all elements to this value") } static void Test_oneElement2mitk_DifferentType(void) { float twos[] = {2.0, 2.0, 2.0}; mitk::Vector vector3D(2.0F); MITK_TEST_CONDITION(EqualArray(vector3D, twos, 3), "one values initializes all elements to this value: value has different type than mitk::Vector.") } static void Test_mitk2mitk_DifferentType(void) { mitk::Vector floatVector3D = originalValuesFloat; mitk::Vector doubleVector3D = valuesToCopy; floatVector3D = doubleVector3D; MITK_TEST_CONDITION(floatVector3D == doubleVector3D, "mitk::Vector assigned to mitk::Vector") MITK_TEST_CONDITION(EqualArray(floatVector3D, valuesToCopy, 3, epsDouble2Float), "correct values were assigned within float accuracy") } static void Test_itk2mitk(void) { Vector3D vector3D = originalValues; itk::Vector itkVector = valuesToCopy; vector3D = itkVector; - MITK_TEST_CONDITION(vector3D == itkVector, "itk::Vector assigned to mitk::Vector") - MITK_TEST_CONDITION(vector3D == valuesToCopy, "correct values were assigned") + TestForEquality(vector3D, itkVector, "mitk::Vector3D", "itk::Vector"); } static void Test_itk2mitk_DifferentType(void) { Vector vector3F = originalValuesFloat; itk::Vector itkVector = valuesToCopy; vector3F = itkVector; MITK_TEST_CONDITION(EqualArray(vector3F, itkVector, 3, epsDouble2Float) , "itk::Vector assigned to mitk::Vector") MITK_TEST_CONDITION(EqualArray(vector3F, valuesToCopyFloat, 3, epsDouble2Float), "correct values were assigned") } static void Test_mitk2itk(void) { Vector3D vector3D = valuesToCopy; itk::Vector itkVector = originalValues; itkVector = vector3D; - MITK_TEST_CONDITION(vector3D == itkVector, "mitk::Vector assigned to itk::Vector") - MITK_TEST_CONDITION(itkVector == valuesToCopy, "correct values were assigned") + TestForEquality(itkVector, vector3D, "itk::Vector", "mitk::Vector3D"); } static void Test_mitk2itk_DifferentType(void) { itk::Vector itkFloatVector = originalValuesFloat; mitk::Vector mitkDoubleVector = valuesToCopy; itkFloatVector = mitkDoubleVector; MITK_TEST_CONDITION(itkFloatVector == mitkDoubleVector, "mitk::Vector assigned to itk::Vector") MITK_TEST_CONDITION(EqualArray(itkFloatVector, valuesToCopy, 3, epsDouble2Float), "correct values were assigned") } static void Test_vnlfixed2mitk(void) { mitk::Vector3D vector3D = originalValues; vnl_vector_fixed vnlVectorFixed(valuesToCopy); vector3D = vnlVectorFixed; - MITK_TEST_CONDITION( vector3D.GetVnlVector() == vnlVectorFixed, "vnl_vector_fixed assigned to mitk::Vector") - MITK_TEST_CONDITION( vector3D == valuesToCopy, "correct values were assigned" ) + TestForEquality(vector3D, vnlVectorFixed, "mitk::Vector3D", "vnl_vector_fixed"); } static void Test_vnlfixed2mitk_DifferentType(void) { mitk::Vector vector3F = originalValuesFloat; vnl_vector_fixed vnlVectorFixed(valuesToCopy); vector3F = vnlVectorFixed; MITK_TEST_CONDITION( EqualArray(vector3F, vnlVectorFixed, 3, epsDouble2Float), "vnl_vector_fixed assigned to mitk::Vector") MITK_TEST_CONDITION( EqualArray(vector3F, valuesToCopy, 3, epsDouble2Float), "correct values were assigned" ) } static void Test_mitk2vnlfixed(void) { vnl_vector_fixed vnlVectorFixed(originalValues); mitk::Vector3D vector3D = valuesToCopy; vnlVectorFixed = vector3D; - MITK_TEST_CONDITION( vnlVectorFixed == vector3D.GetVnlVector(), "mitk::Vector assigned to vnl_vector_fixed") - MITK_TEST_CONDITION( EqualArray(vnlVectorFixed, valuesToCopy, 3), "correct values were assigned" ) + TestForEquality(vnlVectorFixed, vector3D, "vnl_vector_fixed", "mitk::Vector3D"); } static void Test_mitk2vnlfixed_DifferentType(void) { vnl_vector_fixed vnlVectorFixed(originalValuesFloat); mitk::Vector vector3D = valuesToCopy; vnlVectorFixed = vector3D; MITK_TEST_CONDITION( EqualArray(vnlVectorFixed, vector3D, 3, epsDouble2Float), "mitk::Vector assigned to vnl_vector_fixed") MITK_TEST_CONDITION( EqualArray(vnlVectorFixed, valuesToCopy, 3, epsDouble2Float), "correct values were assigned" ) } static void Test_vnl2mitk(void) { mitk::Vector3D vector3D = originalValues; vnl_vector vnlVector(3); vnlVector.set(valuesToCopy); vector3D = vnlVector; - MITK_TEST_CONDITION( vector3D.GetVnlVector() == vnlVector, "vnl_vector assigned to mitk::Vector") - MITK_TEST_CONDITION( vector3D == valuesToCopy, "correct values were assigned" ) + TestForEquality(vector3D, vnlVector, "mitk::Vector3D", "vnl_vector"); } static void Test_vnl2mitk_DifferentType(void) { mitk::Vector vector3F = originalValuesFloat; vnl_vector vnlVector(3); vnlVector.set(valuesToCopy); vector3F = vnlVector; MITK_TEST_CONDITION( EqualArray(vector3F, vnlVector, 3, epsDouble2Float), "vnl_vector assigned to mitk::Vector") MITK_TEST_CONDITION( EqualArray(vector3F, valuesToCopy, 3, epsDouble2Float), "correct values were assigned" ) } static void Test_mitk2vnl(void) { vnl_vector vnlVector(3); vnlVector.set(originalValues); mitk::Vector3D vector3D = valuesToCopy; vnlVector = vector3D; - MITK_TEST_CONDITION( vnlVector == vector3D.GetVnlVector(), "mitk::Vector assigned to vnl_vector") - MITK_TEST_CONDITION( EqualArray(vnlVector, valuesToCopy, 3), "correct values were assigned" ) + TestForEquality(vnlVector, vector3D, "vnl_vector", "mitk::Vector3D"); } static void Test_mitk2vnl_DifferentType(void) { vnl_vector vnlVector(3); vnlVector.set(originalValuesFloat); mitk::Vector3D vector3D = valuesToCopy; vnlVector = vector3D; MITK_TEST_CONDITION( EqualArray(vnlVector, vector3D, 3, epsDouble2Float), "mitk::Vector assigned to vnl_vector") MITK_TEST_CONDITION( EqualArray(vnlVector, valuesToCopy, 3, epsDouble2Float), "correct values were assigned" ) } /** * tests if constructing a mitk::Vector using a too large in size vnl_vector is handled properly. * Meaning: Exception is thrown. */ 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_mitk2opencv() { cv::Vec3d opencvVector(originalValues[0], originalValues[1], originalValues[2]); mitk::Vector3D vector3D = valuesToCopy; vector3D.ToArray(opencvVector); 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 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_mitk2mitk_DifferentType(); Test_pod2mitk(); Test_pod2mitk_DifferentType(); Test_mitk2pod(); Test_mitk2pod_DifferentType(); Test_oneElement2mitk(); Test_oneElement2mitk_DifferentType(); Test_itk2mitk(); Test_itk2mitk_DifferentType(); Test_mitk2itk(); Test_mitk2itk_DifferentType(); Test_vnlfixed2mitk(); Test_vnlfixed2mitk_DifferentType(); Test_mitk2vnlfixed(); Test_mitk2vnlfixed_DifferentType(); Test_vnl2mitk(); Test_vnl2mitk_DifferentType(); Test_vnl2mitk_WrongVnlVectorSize(); Test_mitk2vnl(); Test_mitk2vnl_DifferentType(); Test_mitk2opencv(); Test_opencv2mitk(); MITK_TEST_END() }