diff --git a/Modules/CameraCalibration/Testing/mitkTransformTest.cpp b/Modules/CameraCalibration/Testing/mitkTransformTest.cpp index 3e432843f6..5018f9525a 100644 --- a/Modules/CameraCalibration/Testing/mitkTransformTest.cpp +++ b/Modules/CameraCalibration/Testing/mitkTransformTest.cpp @@ -1,106 +1,136 @@ /*=================================================================== 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 #include #include #include #include +class mitkTransformTestClass + { + public: + + static void TestInstantiation() + { + mitk::Transform::Pointer transform = mitk::Transform::New(); + MITK_TEST_CONDITION_REQUIRED(transform.IsNotNull(),"Testing instantiation with constructor 1. (DEFAULT)"); + + mitk::AffineTransform3D::Pointer emptyTransform = mitk::AffineTransform3D::New(); + mitk::Transform::Pointer transform2 = mitk::Transform::New(emptyTransform); + MITK_TEST_CONDITION_REQUIRED(transform2.IsNotNull(),"Testing instantiation with constructor 2. (PARAM: AffineTransform3D)"); + //TODO test if the parameter was copied correctly + + mitk::NavigationData::Pointer nd = mitk::NavigationData::New(); + mitk::Transform::Pointer transform3 = mitk::Transform::New(nd); + MITK_TEST_CONDITION_REQUIRED(transform3.IsNotNull(),"Testing instantiation with constructor 3. (PARAM: NavigationData)"); + //TODO test if the parameter was copied correctly + + std::string emptyString = ""; + mitk::Transform::Pointer transform4 = mitk::Transform::New(emptyString); + MITK_TEST_CONDITION_REQUIRED(transform4.IsNotNull(),"Testing instantiation with constructor 4. (PARAM: std::string)"); + //TODO test if the parameter was copied correctly + } + + }; + /**Documentation * test for the class "Transform". */ int mitkTransformTest(int argc, char* argv[]) { MITK_TEST_BEGIN("Transform") + mitkTransformTestClass::TestInstantiation(); + mitk::Transform::Pointer transform = mitk::Transform::New(); mitk::Transform* t = transform; // test with homogeneous matrixes vnl_matrix_fixed I; I.set_identity(); std::cout << "vnl I: " << std::endl << I << std::endl; t->SetMatrix( I ); cv::Mat cvI = t->GetCvMatrix(); std::string matAsString; mitk::StringFromCvMat _StringFromCvMat( &cvI, &matAsString ); _StringFromCvMat.Update(); std::cout << "cv identity matrix: " << matAsString << std::endl; MITK_TEST_CONDITION_REQUIRED( trace(cvI) == cv::Scalar(4) , "trace(t->GetCvMatrix()) == cv::Scalar(4)" ); MITK_TEST_CONDITION_REQUIRED( countNonZero(t->GetCvMatrix()) == 4 , "countNonZero(t->GetCvMatrix()) == 4" ); // test 2: 1. create a rotation matrix,convert to quaternion // set as rotation vector // get it back as vnl rotation matrix // convert to cv matrix // convert to quaternion (cv::Rodrigues) // compare received quaternion with original one (created from rotation matrix) cv::Mat cvRotMat = cv::Mat::ones( 3, 3, cv::DataType::type ); cvRotMat.at(0,1) = 2; cvRotMat.at(0,2) = 3; cv::Mat cvRotVec; cv::Rodrigues( cvRotMat, cvRotVec ); t->SetRotationVector( cvRotVec ); vnl_matrix_fixed vnlRotMat = t->GetVnlRotationMatrix(); std::cout << "vnl rotation matrix: " << vnlRotMat << std::endl; vnl_matrix rotMat = vnlRotMat.as_matrix(); cv::Mat cvRotMatReturned; mitk::CvMatFromVnlMatrix _CvMatFromVnlMatrix( &rotMat, &cvRotMatReturned ); _CvMatFromVnlMatrix.Update(); _StringFromCvMat.SetMatrix( &cvRotMatReturned ); _StringFromCvMat.Update(); std::cout << "cvRotMatReturned: " << matAsString << std::endl; cv::Mat cvRotVecReturned; cv::Rodrigues( cvRotMatReturned, cvRotVecReturned ); _StringFromCvMat.SetMatrix( &cvRotVec ); _StringFromCvMat.Update(); std::cout << "cvRotVec: " << matAsString << std::endl; _StringFromCvMat.SetMatrix( &cvRotVecReturned ); _StringFromCvMat.Update(); std::cout << "cvRotVecReturned: " << matAsString << std::endl; double epsilon = 0.001; bool equals = false; mitk::CvMatCompare _CvMatCompare( &cvRotVec, &cvRotVecReturned, &epsilon, &equals ); _CvMatCompare.Update(); MITK_TEST_CONDITION( equals, "testing returned rotation vector"); std::cout << "Transform as string: " << transform << std::endl; // always end with this! MITK_TEST_END(); } diff --git a/Modules/CameraCalibration/mitkTransform.h b/Modules/CameraCalibration/mitkTransform.h index eacf44160e..a9ca0e497c 100644 --- a/Modules/CameraCalibration/mitkTransform.h +++ b/Modules/CameraCalibration/mitkTransform.h @@ -1,313 +1,317 @@ /*=================================================================== 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 MITKTRANSFORM_H #define MITKTRANSFORM_H #include #include #include #include #include #include #include #include #include #include namespace mitk { /// /// \brief class representing a transfrom in 3D /// /// internally it stores a mitk navigation data. this is more /// or less a wrapper for navigation data for easy casting /// between opencv/vnl/mitk/xml representations of transform /// data /// class mitkCameraCalibration_EXPORT Transform: public itk::Object, public XMLSerializable { public: mitkClassMacro(Transform, itk::Object); itkFactorylessNewMacro(Transform); mitkNewMacro1Param(Transform, const mitk::NavigationData*); mitkNewMacro1Param(Transform, const mitk::AffineTransform3D*); + /** @brief TODO: documentation, especially: what is the string for? + * + * + */ mitkNewMacro1Param(Transform, const std::string&); /// /// constants describing the type of transform /// represented here /// static const std::string UNKNOWN_TYPE; static const std::string ENDOSCOPE_SCOPE_TOOL; static const std::string ENDOSCOPE_CAM_TOOL; static const std::string CHESSBOARD_TOOL; static const std::string POINTER_TOOL; static const std::string POINTER_TO_CHESSBOARD_ORIGIN; static const std::string POINTER_TO_CHESSBOARD_X_SUPPORT_POINT; static const std::string POINTER_TO_CHESSBOARD_Y_SUPPORT_POINT; static const std::string BOARD_TO_BOARD_TOOL; static const std::string REFERENCE_CAMERA_TRANSFORM; static const std::string REFERENCE_SCOPE_TRANSFORM; static const std::string EYE_TO_HAND_TRANSFORM; static const std::string CAMERA_EXTRINSICS; itkGetConstMacro(Type, std::string); itkSetMacro(Type, std::string&); /// /// Copies the content of transform to this /// instance /// void Copy( const mitk::Transform* transform ); /// /// Copies the content of transform to this /// instance /// void Copy( const mitk::NavigationData* transform ); /// /// Inverts the rotation of this transform /// (Polaris navigation Data have inverted rotation /// so you may want to call this function when using /// polaris data) /// void TransposeRotation(); /// /// get a copy of this transform /// mitk::Transform::Pointer Clone() const; /// /// concatenate this transform with the given one, /// i.e. this transform is done first, then transform /// ( if x is this transform, y is transform, then this will be y*x) /// post multiply semantics! /// \see vtkTransform /// void Concatenate( mitk::Transform* transform ); /// /// same as above with vnl mat argument /// void Concatenate( const vnl_matrix_fixed& transform ); /// /// same as above with vtk mat argument /// void Concatenate( const vtkMatrix4x4* transform ); /// /// invert this transform /// void Invert(); /// /// resets the internal variables except type /// void Reset(); /// /// read from xml /// void FromXML(TiXmlElement* elem); /// /// read csv file /// void FromCSVFile(const std::string& file); /// /// grafts the data from naviData to this transform /// void SetNavigationData( const mitk::NavigationData* naviData ); /// /// method to set orientation quat /// void SetOrientation( const vnl_quaternion& orientation); /// /// method to set double valued orientation quat /// void SetOrientation( const vnl_quaternion& orientation); /// /// method to set translation /// void SetTranslation( const vnl_vector_fixed& transl); /// /// method to set a vector of doubles as translation /// void SetTranslation( const vnl_vector& transl); /// /// method to set a mitk::Point3D as position /// void SetPosition( const mitk::Point3D& transl); /// /// sets rotation with a rotation matrix /// void SetRotation( vnl_matrix_fixed& mat); /// /// sets rotation with a non fixed rotation matrix /// void SetRotation( vnl_matrix& mat); /// /// sets rotation and translation with a transformation matrix /// void SetMatrix( const vnl_matrix_fixed& mat); /// /// sets rotation and translation with a vtk transformation matrix /// void SetMatrix( const vtkMatrix4x4* mat); /// /// sets translation from a POD vector /// void SetTranslation( float* array ); /// /// sets translation from a POD vector. this must be a /// 3x3=9 sized vector in row major format (first row = first /// three elements) /// void SetRotation( float* array ); /// /// sets translation from a POD vector /// void SetTranslation( double array[3] ); /// /// sets translation from a POD vector /// void SetRotation( double array[3][3] ); /// /// method to set translation by cv vector /// void SetTranslation( const cv::Mat& transl); /// /// sets rotation with a rotation matrix /// void SetRotation( const cv::Mat& mat ); /// /// sets rotation with a rodrigues rotation vector /// void SetRotationVector( const cv::Mat& rotVec); /// /// \return the navigation data that stores all information /// mitk::NavigationData::Pointer GetNavigationData() const; /** @return Returns the transform as affine transform 3D object.*/ mitk::AffineTransform3D::Pointer GetAffineTransform3D(); /// /// calls navigationdata::GetPosition() /// mitk::Point3D GetPosition() const; /// /// same as GetPosition /// mitk::Point3D GetTranslation() const; /// /// calls navigationdata::IsValid() /// bool IsValid() const; /// /// calls navigationdata::SetValid() /// void SetValid(bool valid); /// /// calls navigationdata::GetOrientation() /// mitk::Quaternion GetOrientation() const; /// /// \return the homogeneous matrix representing this transform /// vnl_matrix_fixed GetMatrix() const; /// /// \return the homogeneous vtk matrix representing this transform /// void GetMatrix(vtkMatrix4x4* matrix) const; /// /// \return the homogeneous vtk matrix representing this transform /// in !OpenGL! left handed coordinate system /// void GetVtkOpenGlMatrix(vtkMatrix4x4* matrix) const; mitk::Point3D TransformPoint(mitk::Point3D point) const; /// /// create xml representation /// void ToXML(TiXmlElement* elem) const; /// /// create string representation /// std::string ToString() const; /// /// create string csv representation (only the transformation values!!!!) /// std::string ToCSVString() const; /// /// create matlab representation /// std::string ToMatlabString(const std::string& varname="transform", bool printLastRow=true) const; /// /// write csv representation to file (only the transformation values!!!!) /// void ToCSVFile(const std::string& file) const; /// /// write matlab representation to file /// void ToMatlabFile(const std::string& file , const std::string& varname="transform") const; /// /// conversion to cv types /// cv::Mat GetCvTranslation() const; cv::Mat GetCvRotationVector() const; cv::Mat GetCvRotationMatrix() const; cv::Mat GetCvMatrix() const; /// /// conversion to vnl types /// vnl_vector_fixed GetVnlTranslation() const; vnl_vector_fixed GetVnlDoubleTranslation() const; vnl_quaternion GetVnlDoubleQuaternion() const; vnl_matrix_fixed GetVnlRotationMatrix() const; vnl_matrix_fixed GetVnlDoubleMatrix() const; protected: Transform(); Transform(const mitk::NavigationData* nd); Transform(const mitk::AffineTransform3D* nd); Transform(const std::string& s); vnl_matrix_fixed GetRotationMatrixFromQuaternion(mitk::Quaternion q) const; // everything is stored here mitk::NavigationData::Pointer m_NavData; /// /// saves the type of the transform (Default is UNKNOWN_TYPE) /// std::string m_Type; }; } // namespace mitk mitkCameraCalibration_EXPORT std::ostream& operator<< (std::ostream& os, mitk::Transform::Pointer p); #endif // MITKTRANSFORM_H