diff --git a/Core/Code/DataManagement/mitkVector.h b/Core/Code/DataManagement/mitkVector.h index bb2fa76e2c..915b1224df 100644 --- a/Core/Code/DataManagement/mitkVector.h +++ b/Core/Code/DataManagement/mitkVector.h @@ -1,249 +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 + * @return the array holding the elements of this. Only requirement 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; // 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 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); CPPUNIT_TEST_SUITE_END(); private: vtkSmartPointer a_vtkPoints; ScalarType originalValues[3]; ScalarType valuesToCopy[3]; + /** + * @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_EQUAL_MESSAGE("\nAssigning " + v2Name + " to " + v1Name + ":\n both are equal", true, EqualArray(v1, v2, 3, eps)); + } + + 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)