diff --git a/Core/Code/Testing/mitkPointTypeConversionTest.cpp b/Core/Code/Testing/mitkPointTypeConversionTest.cpp index 7863084be8..0df473d801 100644 --- a/Core/Code/Testing/mitkPointTypeConversionTest.cpp +++ b/Core/Code/Testing/mitkPointTypeConversionTest.cpp @@ -1,166 +1,166 @@ /*=================================================================== 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 "mitkTestFixture.h" #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; class mitkPointTypeConversionTestSuite : public mitk::TestFixture { CPPUNIT_TEST_SUITE(mitkPointTypeConversionTestSuite); + MITK_TEST(Vector2Point); + MITK_TEST(Mitk2Itk_PointCompatibility); MITK_TEST(Itk2Mitk_PointCompatibility); MITK_TEST(Vtk2Mitk_PointCompatibility); MITK_TEST(Mitk2Pod_PointCompatibility); MITK_TEST(Pod2Mitk_PointCompatibility); - MITK_TEST(Vector2Point); - CPPUNIT_TEST_SUITE_END(); private: vtkSmartPointer a_vtkPoints; ScalarType originalValues[3]; ScalarType valuesToCopy[3]; public: void setUp(void) { FillVector3D(originalValues, 1.0, 2.0, 3.0); FillVector3D(valuesToCopy, 4.0, 5.0, 6.0); a_vtkPoints = vtkSmartPointer::New(); a_vtkPoints->Initialize(); } void tearDown(void) { // a_vtkPoints = NULL; } /** * @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 void TestForEquality(T1 v1, T2 v2, std::string v1Name, std::string v2Name, ScalarType eps = mitk::eps) { CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE("\nAssigning " + v2Name + " to " + v1Name + ":\n both are equal", EqualArray(v1, v2, 3, eps)); } void Mitk2Itk_PointCompatibility() { itk::Point itkPoint3D = originalValues; mitk::Point3D point3D = valuesToCopy; itkPoint3D = point3D; TestForEquality(itkPoint3D, point3D, "itk::Point", "mitk:Point"); } void Itk2Mitk_PointCompatibility() { mitk::Point3D point3D = originalValues; itk::Point itkPoint3D = valuesToCopy; point3D = itkPoint3D; TestForEquality(point3D, itkPoint3D, "mitk:Point", "itk::Point"); } void Vtk2Mitk_PointCompatibility() { mitk::Point3D point3D = originalValues; a_vtkPoints->InsertNextPoint(valuesToCopy); double vtkPoint[3]; a_vtkPoints->GetPoint(0, vtkPoint); point3D = vtkPoint; TestForEquality(point3D, vtkPoint, "mitk:Point", "vtkPoint"); } void Mitk2Pod_PointCompatibility() { ScalarType podPoint[] = {1.0, 2.0, 3.0}; mitk::Point3D point3D = valuesToCopy; point3D.ToArray(podPoint); TestForEquality(podPoint, point3D, "POD point", "mitk::Point"); } void Pod2Mitk_PointCompatibility() { itk::Point point3D = originalValues; ScalarType podPoint[] = {4.0, 5.0, 6.0}; point3D = podPoint; TestForEquality(point3D, podPoint, "mitk::Point3D", "POD point"); } void Vector2Point() { itk::Point point3D = valuesToCopy; itk::Vector vector3D = originalValues; point3D = vector3D; TestForEquality(point3D, vector3D, "mitk::Point", "mitk::Vector"); } }; MITK_TEST_SUITE_REGISTRATION(mitkPointTypeConversion) diff --git a/Core/Code/Testing/mitkTypeVectorConversionTest.cpp b/Core/Code/Testing/mitkTypeVectorConversionTest.cpp index d96d01cf97..667fd45924 100644 --- a/Core/Code/Testing/mitkTypeVectorConversionTest.cpp +++ b/Core/Code/Testing/mitkTypeVectorConversionTest.cpp @@ -1,255 +1,265 @@ /*=================================================================== 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 "mitkTestFixture.h" +#include "mitkTestingMacros.h" #include #include "vnl/vnl_math.h" -#include "mitkTestingMacros.h" #include "mitkConstants.h" #include "mitkVector.h" #include "mitkPoint.h" using namespace mitk; +class VectorTypeConversionTestSuite : public mitk::TestFixture -/** - * 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; + CPPUNIT_TEST_SUITE(VectorTypeConversionTestSuite); + MITK_TEST(Point2Vector); + MITK_TEST(Pod2Mitk); + MITK_TEST(Mitk2Pod); -static void Test_pod2mitk(void) -{ - mitk::Vector3D vector3D = valuesToCopy; + MITK_TEST(OneElement2Mitk); - TestForEquality(vector3D, valuesToCopy, "mitk::Vector3D", "double POD"); -} + MITK_TEST(Itk2Mitk); + MITK_TEST(Mitk2Itk); + MITK_TEST(Vnlfixed2Mitk); + MITK_TEST(Mitk2Vnlfixed); -static void Test_mitk2pod(void) -{ - ScalarType podArray[3]; - mitk::Vector3D vector3D = valuesToCopy; + MITK_TEST(Vnl2Mitk); + MITK_TEST(Mitk2Vnl); + MITK_TEST(Vnl2Mitk_WrongVnlVectorSize); - vector3D.ToArray(podArray); + MITK_TEST(ToArray_DifferentType); + MITK_TEST(FromArray_DifferentType); - TestForEquality(podArray, vector3D, "double POD", "mitk::Vector3D"); -} + CPPUNIT_TEST_SUITE_END(); +private: -static void Test_oneElement2mitk(void) -{ - double twos[] = {2.0, 2.0, 2.0}; - mitk::Vector vector3D(2.0); + /** + * these 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. + */ + ScalarType originalValues[3]; + ScalarType valuesToCopy[3]; - MITK_TEST_CONDITION(EqualArray(vector3D, twos, 3), "\none values initializes all elements to this value") -} + float epsDouble2Float; +public: -static void Test_itk2mitk(void) -{ - Vector3D vector3D = originalValues; - itk::Vector itkVector = valuesToCopy; - vector3D = itkVector; + void setUp(void) + { + FillVector3D(originalValues, 1.123456789987, 2.789456321456, 3.123654789987456); + FillVector3D(valuesToCopy, 4.654789123321, 5.987456789321, 6.321654987789546); + + epsDouble2Float = vnl_math::float_eps * 10.0; + } - TestForEquality(vector3D, itkVector, "mitk::Vector3D", "itk::Vector"); -} + void tearDown(void) + { + } -static void Test_mitk2itk(void) -{ - Vector3D vector3D = valuesToCopy; - itk::Vector itkVector = originalValues; + /** + * @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 + void TestForEquality(T1 v1, T2 v2, std::string v1Name, std::string v2Name, ScalarType eps = mitk::eps) + { + CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE("\nAssigning " + v2Name + " to " + v1Name + ":\n both are equal", EqualArray(v1, v2, 3, eps)); + } - itkVector = vector3D; - TestForEquality(itkVector, vector3D, "itk::Vector", "mitk::Vector3D"); -} -static void Test_vnlfixed2mitk(void) -{ - mitk::Vector3D vector3D = originalValues; - vnl_vector_fixed vnlVectorFixed(valuesToCopy); + void Pod2Mitk(void) + { + mitk::Vector3D vector3D = valuesToCopy; - vector3D = vnlVectorFixed; + TestForEquality(vector3D, valuesToCopy, "mitk::Vector3D", "double POD"); + } - TestForEquality(vector3D, vnlVectorFixed, "mitk::Vector3D", "vnl_vector_fixed"); -} + void Mitk2Pod(void) + { + ScalarType podArray[3]; + mitk::Vector3D vector3D = valuesToCopy; -static void Test_mitk2vnlfixed(void) -{ - vnl_vector_fixed vnlVectorFixed(originalValues); - mitk::Vector3D vector3D = valuesToCopy; + vector3D.ToArray(podArray); - vnlVectorFixed = vector3D; + TestForEquality(podArray, vector3D, "double POD", "mitk::Vector3D"); + } - TestForEquality(vnlVectorFixed, vector3D, "vnl_vector_fixed", "mitk::Vector3D"); -} + void OneElement2Mitk(void) + { + double twos[] = {2.0, 2.0, 2.0}; + mitk::Vector vector3D(2.0); -static void Test_vnl2mitk(void) -{ - mitk::Vector3D vector3D = originalValues; - vnl_vector vnlVector(3); - vnlVector.set(valuesToCopy); + CPPUNIT_ASSERT_ASSERTION_PASS_MESSAGE( "\n one values initializes all elements to this value", EqualArray(vector3D, twos, 3)); + } - vector3D = vnlVector; - TestForEquality(vector3D, vnlVector, "mitk::Vector3D", "vnl_vector"); -} + void Itk2Mitk(void) + { + Vector3D vector3D = originalValues; + itk::Vector itkVector = valuesToCopy; + vector3D = itkVector; -static void Test_mitk2vnl(void) -{ - vnl_vector vnlVector(3); - vnlVector.set(originalValues); - mitk::Vector3D vector3D = valuesToCopy; + TestForEquality(vector3D, itkVector, "mitk::Vector3D", "itk::Vector"); + } - vnlVector = vector3D; - TestForEquality(vnlVector, vector3D, "vnl_vector", "mitk::Vector3D"); -} + void Mitk2Itk(void) + { + Vector3D vector3D = valuesToCopy; + itk::Vector itkVector = originalValues; + itkVector = 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); + TestForEquality(itkVector, vector3D, "itk::Vector", "mitk::Vector3D"); + } - MITK_TEST_FOR_EXCEPTION(mitk::Exception&, vector3D = vnlVector;) -} + void Vnlfixed2Mitk(void) + { + mitk::Vector3D vector3D = originalValues; + vnl_vector_fixed vnlVectorFixed(valuesToCopy); + vector3D = vnlVectorFixed; -static void Test_ToArray_DifferentType(void) -{ - float podArray[3]; - for (int var = 0; var < 3; ++var) { - podArray[var] = originalValues[var]; + TestForEquality(vector3D, vnlVectorFixed, "mitk::Vector3D", "vnl_vector_fixed"); } - mitk::Vector3D vector3D = valuesToCopy; - vector3D.ToArray(podArray); - TestForEquality(podArray, vector3D, "float POD", "mitk::Vector3D", epsDouble2Float); -} + void Mitk2Vnlfixed(void) + { + vnl_vector_fixed vnlVectorFixed(originalValues); + mitk::Vector3D vector3D = valuesToCopy; + vnlVectorFixed = vector3D; -static void Test_FromArray_DifferentType(void) -{ - mitk::Vector3D vector3D = originalValues; - float podArray[3]; - for (int var = 0; var < 3; ++var) { - podArray[var] = valuesToCopy[var]; + TestForEquality(vnlVectorFixed, vector3D, "vnl_vector_fixed", "mitk::Vector3D"); } - vector3D.FromArray(podArray); - TestForEquality(vector3D, podArray, "mitk::Vector3D", "float POD", epsDouble2Float); -} + void Vnl2Mitk(void) + { + mitk::Vector3D vector3D = originalValues; + vnl_vector vnlVector(3); + vnlVector.set(valuesToCopy); -static void Test_vector2point() -{ - mitk::Point3D point3D = originalValues; - mitk::Vector3D vector3D = valuesToCopy; + vector3D = vnlVector; - vector3D = point3D.GetVectorFromOrigin(); + TestForEquality(vector3D, vnlVector, "mitk::Vector3D", "vnl_vector"); + } - TestForEquality(point3D, vector3D, "mitk::Point3D", "mitk::Vector3D"); -} + void Mitk2Vnl(void) + { + vnl_vector vnlVector(3); + vnlVector.set(originalValues); + mitk::Vector3D vector3D = valuesToCopy; -/** -* @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") + vnlVector = vector3D; - Test_vector2point(); + TestForEquality(vnlVector, vector3D, "vnl_vector", "mitk::Vector3D"); + } - Test_pod2mitk(); - Test_mitk2pod(); - Test_oneElement2mitk(); + /** + * @brief Tests if an exception is thrown when constructing an mitk::Vector form a vnl_vector of not suited size. + */ + void Vnl2Mitk_WrongVnlVectorSize() + { + ScalarType largerValuesToCopy[] = {4.12345678910, 5.10987654321, 6.123456789132456, 7.123456987789456}; + mitk::Vector3D vector3D = originalValues; + vnl_vector vnlVector(4); + vnlVector.set(largerValuesToCopy); + + CPPUNIT_ASSERT_THROW(vector3D = vnlVector, mitk::Exception); + } - Test_itk2mitk(); - Test_mitk2itk(); - Test_vnlfixed2mitk(); - Test_mitk2vnlfixed(); + void ToArray_DifferentType(void) + { + float podArray[3]; + for (int var = 0; var < 3; ++var) { + podArray[var] = originalValues[var]; + } + mitk::Vector3D vector3D = valuesToCopy; - Test_vnl2mitk(); - Test_mitk2vnl(); - Test_vnl2mitk_WrongVnlVectorSize(); + vector3D.ToArray(podArray); - /** - * 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(); + TestForEquality(podArray, vector3D, "float POD", "mitk::Vector3D", epsDouble2Float); + } + + + void 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); + } + + void Point2Vector() + { + mitk::Point3D point3D = originalValues; + mitk::Vector3D vector3D = valuesToCopy; + + vector3D = point3D.GetVectorFromOrigin(); + + TestForEquality(point3D, vector3D, "mitk::Point3D", "mitk::Vector3D"); + } + + +}; - MITK_TEST_END() -} +MITK_TEST_SUITE_REGISTRATION(VectorTypeConversion)