diff --git a/Core/Code/DataManagement/mitkEqual.h b/Core/Code/DataManagement/mitkEqual.h new file mode 100644 index 0000000000..81adc166d4 --- /dev/null +++ b/Core/Code/DataManagement/mitkEqual.h @@ -0,0 +1,71 @@ +/* + * mitkEqual.h + * + * Created on: Apr 14, 2014 + * Author: wirkert + */ + +#ifndef MITKEQUAL_H_ +#define MITKEQUAL_H_ + + +#include "mitkConstants.h" +#include "mitkLogMacros.h" + +namespace mitk { + + /** + * Helper method to check if the difference is bigger or equal to a given epsilon + * + * @param diff the difference to be checked against the epsilon + * @param the epsilon. The absolute difference needs to be smaller than this. + * @return true if abs(diff) >= eps + */ + template + inline bool DifferenceBiggerOrEqualEps(DifferenceType diff, mitk::ScalarType epsilon = mitk::eps) + { + return fabs(diff) >= epsilon; + } + + /** + * outputs elem1, elem2 and eps in case verbose and !isEqual. + * Elem can e.g. be a mitk::Vector or an mitk::Point. + * + * @param elem1 first element to be output + * @param elem2 second + * @param eps the epsilon which their difference was bigger than + * @param verbose tells the function if something shall be output + * @param isEqual function will only output something if the two elements are not equal + */ + template + inline void ConditionalOutputOfDifference(ElementToOutput1 elem1, ElementToOutput2 elem2, mitk::ScalarType eps, bool verbose, bool isEqual) + { + if(verbose && !isEqual) + { + MITK_INFO << typeid(ElementToOutput1).name() << " and " << typeid(ElementToOutput2).name() << " not equal. Lefthandside " << std::setprecision(12) << elem1 << " - Righthandside " << elem2 << " - epsilon " << eps; + } + } + + /** + * @ingroup MITKTestingAPI + * + * @param scalar1 Scalar value to compare. + * @param scalar2 Scalar value to compare. + * @param eps Tolerance for floating point comparison. + * @param verbose Flag indicating detailed console output. + * @return True if scalars are equal. + */ + inline bool Equal(ScalarType scalar1, ScalarType scalar2, ScalarType eps=mitk::eps, bool verbose=false) + { + bool isEqual( !DifferenceBiggerOrEqualEps(scalar1-scalar2, eps)); + + ConditionalOutputOfDifference(scalar1, scalar2, eps, verbose, isEqual); + + return isEqual; + } + +} + + + +#endif /* MITKEQUAL_H_ */ diff --git a/Core/Code/DataManagement/mitkTypes.h b/Core/Code/DataManagement/mitkTypes.h index 5a8f804dff..ee1f1dc614 100644 --- a/Core/Code/DataManagement/mitkTypes.h +++ b/Core/Code/DataManagement/mitkTypes.h @@ -1,349 +1,303 @@ /*=================================================================== 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 #include #include #include #include #include #include #include #include #include #include #include "mitkConstants.h" #include "mitkQuaternion.h" #include "mitkPoint.h" // TODO SW: should not be included here, maybe generate one "general datatype include" like mitkPrimitives.h #include "mitkVector.h" #include "mitkMatrix.h" +#include "mitkEqual.h" #include "mitkOldTypeConversions.h" #ifndef DOXYGEN_SKIP namespace mitk { -/** - * Helper method to check if the difference is bigger or equal to a given epsilon - * - * @param diff the difference to be checked against the epsilon - * @param the epsilon. The absolute difference needs to be smaller than this. - * @return true if abs(diff) >= eps - */ -template -static inline bool DifferenceBiggerOrEqualEps(DifferenceType diff, mitk::ScalarType epsilon = mitk::eps) -{ - return fabs(diff) >= epsilon; -} - -/** - * outputs elem1, elem2 and eps in case verbose and !isEqual. - * Elem can e.g. be a mitk::Vector or an mitk::Point. - * - * @param elem1 first element to be output - * @param elem2 second - * @param eps the epsilon which their difference was bigger than - * @param verbose tells the function if something shall be output - * @param isEqual function will only output something if the two elements are not equal - */ -template -static inline void ConditionalOutputOfDifference(ElementToOutput1 elem1, ElementToOutput2 elem2, mitk::ScalarType eps, bool verbose, bool isEqual) -{ - if(verbose && !isEqual) - { - MITK_INFO << typeid(ElementToOutput1).name() << " and " << typeid(ElementToOutput2).name() << " not equal. Lefthandside " << std::setprecision(12) << elem1 << " - Righthandside " << elem2 << " - epsilon " << eps; - } -} /*! \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); } /** * @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 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 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 inline bool EqualArray(TArrayType1& arrayType1, TArrayType2& arrayType2, int size, ScalarType eps = mitk::eps, bool verbose = false) { bool isEqual = true; for (int var = 0; var < size; ++var) { isEqual = isEqual && Equal(arrayType1[var], arrayType2[var], eps); } ConditionalOutputOfDifference(arrayType1, arrayType2, eps, verbose, isEqual); return isEqual; } } // 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 */