14#include <vnl/vnl_inverse.h>
41 vnl_matrix_fixed<mitk::ScalarType, 3, 3> rot;
55 vnl_matrix_fixed<mitk::ScalarType, 3, 3> rot;
67 vnl_matrix_fixed<mitk::ScalarType, 4, 4> mat = transform->
GetMatrix();
76 Transform::Pointer t = Transform::New();
77 t->SetMatrix( transform );
83 Transform::Pointer t = Transform::New();
84 t->SetMatrix( transform );
90 mitk::NavigationData::Pointer nd
91 = NavigationData::New();
95 const vnl_quaternion<mitk::ScalarType>& orientation)
105 for(
unsigned int i=0; i<3; ++i)
114 vnl_vector_fixed<mitk::ScalarType, 3> vec;
115 for(
unsigned int i=0; i<vec.size(); i++)
122 vnl_matrix_fixed<mitk::ScalarType, 3, 3> mat;
124 unsigned int row = 0;
125 unsigned int col = 0;
126 for(
unsigned int i=0; i<mat.rows()*mat.cols(); i++)
128 if( i > 0 && i % 3 == 0 )
134 mat(row,col) = array[i];
143 vnl_vector_fixed<mitk::ScalarType, 4> qvec;
147 mitk::Quaternion p( qvec );
155 vnl_vector_fixed<double, 3> vecDouble;
164 vnl_vector_fixed<double, 3> dTransl(transl);
165 vnl_vector_fixed<mitk::ScalarType, 3> fTransl;
175 vnl_quaternion<double> dOrientation;
184 std::ifstream csvFile (file.c_str());
187 mitk::Transform::Pointer transform = mitk::Transform::New();
188 vnl_matrix_fixed<mitk::ScalarType, 4, 4> mat;
190 mitk::ScalarType d = 0.0f;
191 int row=0,column = 0;
193 while (std::getline (csvFile, line))
195 std::istringstream linestream(line);
198 while (std::getline (linestream, item,
','))
200 std::istringstream number;
203 mat(row, column) = d;
209 transform->SetMatrix( mat );
217 std::ostringstream s; s.precision(12);
219 vnl_matrix_fixed<mitk::ScalarType, 4, 4> mat
222 for(
unsigned int j=0; j<mat.rows(); ++j )
224 for(
unsigned int k=0; k<mat.cols(); ++k )
239 ,
bool printLastRow)
const
241 std::ostringstream s; s.precision(12);
243 vnl_matrix_fixed<mitk::ScalarType, 4, 4> mat
246 s << varname <<
" = [";
247 for(
unsigned int j=0; j<mat.rows(); ++j )
249 if( !printLastRow && j+1 == mat.rows() )
251 for(
unsigned int k=0; k<mat.cols(); ++k )
253 s << mat(j,k) <<
" ";
257 s <<
"];" << std::endl;
265 m_Type = transform->GetType();
270 Transform::Pointer copy = Transform::New();
277 vnl_matrix_fixed<mitk::ScalarType, 4, 4> vnlMat;
278 for(
unsigned int i=0; i<4; ++i)
279 for(
unsigned int j=0; j<4; ++j)
280 vnlMat(i,j) = mat->GetElement(i, j);
287 std::ofstream csvFile;
288 csvFile.open(file.c_str());
295 ,
const std::string& varname)
const
297 std::ofstream csvFile;
298 csvFile.open(file.c_str());
313 this->
m_NavData->SetOrientation( mitk::Quaternion(mat) );
319 vnl_matrix_fixed<mitk::ScalarType, 3, 3> tmp(mat);
331 for(
unsigned int i = 0; i < 3; ++i)
332 p.SetElement(i, array[i]);
339 vnl_matrix_fixed<mitk::ScalarType, 3, 3> mat;
341 for(
unsigned int i = 0; i < 3; ++i)
342 for(
unsigned int j = 0; j < 3; ++j)
343 mat(i, j) = array[i][j];
349 vnl_matrix_fixed<mitk::ScalarType, 4, 4> tmp(this->
GetMatrix());
354 const vnl_matrix_fixed<mitk::ScalarType, 4, 4>& mat)
357 vnl_vector<mitk::ScalarType> transl = mat.get_column(3).as_ref();
359 for(
unsigned int i=0; i<3; ++i)
365 vnl_matrix_fixed<mitk::ScalarType, 3, 3> rotMatFixed(
377 vnl_vector<mitk::ScalarType> vec(3);
379 _VnlVectorFromCvMat.
Update();
380 this->
SetTranslation( vnl_vector_fixed<mitk::ScalarType, 3>( vec ) );
385 vnl_matrix<mitk::ScalarType> vnlMat(3, 3);
387 _VnlMatrixFromCvMat.
Update();
388 vnl_matrix_fixed<mitk::ScalarType, 3, 3> vnlMatFixed(vnlMat);
396 cv::Rodrigues( rotVec, rotMat );
398 vnl_matrix<mitk::ScalarType> vnlMat(3, 3);
400 _VnlMatrixFromCvMat.
Update();
403 vnl_matrix_fixed<mitk::ScalarType, 3, 3> vnlMatFixed(vnlMat);
431 vnl_matrix_fixed<mitk::ScalarType, 4, 4> vnlMat = this->
GetMatrix();
432 for(
unsigned int i=0; i<vnlMat.rows(); ++i)
433 for(
unsigned int j=0; j<vnlMat.cols(); ++j)
434 matrix->SetElement(i,j, vnlMat(i,j));
439 vnl_matrix<mitk::ScalarType> vnlRotation
443 vnlRotation.normalize_rows();
445 vnl_matrix<mitk::ScalarType> vnlInverseRotation(3,3);
447 vnlInverseRotation = vnl_matrix_inverse<mitk::ScalarType>(vnlRotation.as_ref()).as_matrix();
449 vnl_vector<mitk::ScalarType> vnlTranslation
452 vnlTranslation = vnlInverseRotation * vnlTranslation;
453 vnlTranslation *= -1;
456 mitk::Transform::Pointer tmp = mitk::Transform::New();
458 tmp->SetTranslation( vnlTranslation );
459 tmp->SetRotation( vnlRotation );
460 tmp->GetMatrix(matrix);
466 itk::Point<mitk::ScalarType,3> pointR = (R * point);
467 mitk::Point3D retPoint = pointR;
481 _CvMatFromVnlVector.
Update();
491 _CvMatFromVnlMatrix.
Update();
498 vnl_matrix<mitk::ScalarType> vec = this->
GetMatrix().as_matrix();
500 _CvMatFromVnlMatrix.
Update();
506 cv::Mat rotVec(3,1,cv::DataType<mitk::ScalarType>::type);
514 vnl_vector_fixed<mitk::ScalarType, 3> vec(
m_NavData->GetPosition()
521 return m_NavData->GetOrientation().rotation_matrix_transpose();
526 vnl_matrix_fixed<mitk::ScalarType, 4, 4> mat = this->
GetMatrix();
528 vnl_matrix_fixed<double, 4, 4> doubleMat;
530 for(
unsigned int i=0; i<mat.rows(); ++i)
531 for(
unsigned int j=0; j<mat.cols(); ++j)
532 doubleMat(i,j) =
static_cast<double>( mat(i,j) );
543 vnl_matrix_fixed<mitk::ScalarType, 4, 4> homMat;
544 homMat.set_identity();
546 for(
unsigned int i=0; i<rot.rows(); ++i)
547 for(
unsigned int j=0; j<rot.cols(); ++j)
548 homMat(i,j) = rot(i,j);
549 for(
unsigned int i=0; i<transl.size(); ++i)
550 homMat(i,3) = transl[i];
572 std::ostringstream s; s.precision(12);
576 position = m_NavData->GetPosition();
579 orientation = m_NavData->GetOrientation();
581 s <<
"Translation: [" << position[0] <<
", " << position[1] <<
", "
582 << position[2] <<
"]";
583 s <<
", orientation: [" << orientation[0] <<
", " << orientation[1] <<
", "
584 << orientation[2] <<
", " << orientation[3] <<
"]";
585 s <<
", valid: [" << (this->IsValid()?
"true":
"false") <<
"]";
592 std::string value = elem->Value() !=
nullptr ? elem->Value() :
"";
594 elem->SetValue(this->GetNameOfClass());
598 position = m_NavData->GetPosition();
601 orientation = m_NavData->GetOrientation();
604 matrix.SetIdentity();
605 matrix = m_NavData->GetCovErrorMatrix();
607 bool hasPosition =
true;
608 hasPosition = m_NavData->GetHasPosition();
609 bool hasOrientation =
true;
610 hasOrientation = m_NavData->GetHasOrientation();
611 bool dataValid =
false;
612 dataValid = m_NavData->IsDataValid();
615 elem->SetAttribute(
"Type", m_Type.c_str());
616 elem->SetAttribute(
"Time", timestamp);
617 elem->SetAttribute(
"X", position[0]);
618 elem->SetAttribute(
"Y", position[1]);
619 elem->SetAttribute(
"Z", position[2]);
621 elem->SetAttribute(
"QX", orientation[0]);
622 elem->SetAttribute(
"QY", orientation[1]);
623 elem->SetAttribute(
"QZ", orientation[2]);
624 elem->SetAttribute(
"QR", orientation[3]);
626 elem->SetAttribute(
"C00", matrix[0][0]);
627 elem->SetAttribute(
"C01", matrix[0][1]);
628 elem->SetAttribute(
"C02", matrix[0][2]);
629 elem->SetAttribute(
"C03", matrix[0][3]);
630 elem->SetAttribute(
"C04", matrix[0][4]);
631 elem->SetAttribute(
"C05", matrix[0][5]);
632 elem->SetAttribute(
"C10", matrix[1][0]);
633 elem->SetAttribute(
"C11", matrix[1][1]);
634 elem->SetAttribute(
"C12", matrix[1][2]);
635 elem->SetAttribute(
"C13", matrix[1][3]);
636 elem->SetAttribute(
"C14", matrix[1][4]);
637 elem->SetAttribute(
"C15", matrix[1][5]);
640 elem->SetAttribute(
"Valid",1);
642 elem->SetAttribute(
"Valid",0);
645 elem->SetAttribute(
"hO",1);
647 elem->SetAttribute(
"hO",0);
650 elem->SetAttribute(
"hP",1);
652 elem->SetAttribute(
"hP",0);
659 mitk::NavigationData::Pointer nd = mitk::NavigationData::New();
665 bool hasPosition =
true;
666 bool hasOrientation =
true;
667 bool dataValid =
false;
670 matrix.SetIdentity();
672 const char* typeC = elem->Attribute(
"Type");
673 std::string type =
nullptr == typeC
677 elem->QueryDoubleAttribute(
"Time",×tamp);
680 if(elem->QueryDoubleAttribute(
"X", &position[0]) != tinyxml2::XML_SUCCESS)
681 throw std::invalid_argument(
"No X position found in xml");
682 if(elem->QueryDoubleAttribute(
"Y", &position[1]) != tinyxml2::XML_SUCCESS)
683 throw std::invalid_argument(
"No Y position found in xml");
684 if(elem->QueryDoubleAttribute(
"Z", &position[2]) != tinyxml2::XML_SUCCESS)
685 throw std::invalid_argument(
"No Z position found in xml");
687 if(elem->QueryDoubleAttribute(
"QX", &orientation[0]) != tinyxml2::XML_SUCCESS)
688 throw std::invalid_argument(
"No QX orientation found in xml");
689 if(elem->QueryDoubleAttribute(
"QY", &orientation[1]) != tinyxml2::XML_SUCCESS)
690 throw std::invalid_argument(
"No QY orientation found in xml");
691 if(elem->QueryDoubleAttribute(
"QZ", &orientation[2]) != tinyxml2::XML_SUCCESS)
692 throw std::invalid_argument(
"No QZ orientation found in xml");
693 if(elem->QueryDoubleAttribute(
"QR", &orientation[3]) != tinyxml2::XML_SUCCESS)
694 throw std::invalid_argument(
"No QR orientation found in xml");
696 elem->QueryDoubleAttribute(
"C00", &matrix[0][0]);
697 elem->QueryDoubleAttribute(
"C01", &matrix[0][1]);
698 elem->QueryDoubleAttribute(
"C02", &matrix[0][2]);
699 elem->QueryDoubleAttribute(
"C03", &matrix[0][3]);
700 elem->QueryDoubleAttribute(
"C04", &matrix[0][4]);
701 elem->QueryDoubleAttribute(
"C05", &matrix[0][5]);
702 elem->QueryDoubleAttribute(
"C10", &matrix[1][0]);
703 elem->QueryDoubleAttribute(
"C11", &matrix[1][1]);
704 elem->QueryDoubleAttribute(
"C12", &matrix[1][2]);
705 elem->QueryDoubleAttribute(
"C13", &matrix[1][3]);
706 elem->QueryDoubleAttribute(
"C14", &matrix[1][4]);
707 elem->QueryDoubleAttribute(
"C15", &matrix[1][5]);
710 elem->QueryIntAttribute(
"Valid", &tmpval);
717 elem->QueryIntAttribute(
"hO", &tmpval);
719 hasOrientation =
false;
721 hasOrientation =
true;
724 elem->QueryIntAttribute(
"hP", &tmpval);
730 nd->SetIGTTimeStamp(timestamp);
731 nd->SetPosition(position);
732 nd->SetOrientation(orientation);
733 nd->SetCovErrorMatrix(matrix);
734 nd->SetDataValid(dataValid);
735 nd->SetHasOrientation(hasOrientation);
736 nd->SetHasPosition(hasPosition);
746std::ostream&
operator<< (std::ostream& os, mitk::Transform::Pointer p)
itk::Matrix< mitk::ScalarType, 6, 6 > CovarianceMatrixType
type that holds the error characterization of the position and orientation measurements
mitk::Quaternion OrientationType
Type that holds the orientation part of the tracking data.
double TimeStampType
type that holds the time at which the data was recorded in milliseconds
mitk::Point3D PositionType
Type that holds the position part of the tracking data.
#define endodebugvar(var)