diff --git a/Modules/IGT/IO/mitkNavigationDataReaderCSV.cpp b/Modules/IGT/IO/mitkNavigationDataReaderCSV.cpp index 85c77bc3f1..0e37ca6e45 100644 --- a/Modules/IGT/IO/mitkNavigationDataReaderCSV.cpp +++ b/Modules/IGT/IO/mitkNavigationDataReaderCSV.cpp @@ -1,161 +1,225 @@ /*=================================================================== 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 "mitkNavigationDataReaderCSV.h" mitk::NavigationDataReaderCSV::NavigationDataReaderCSV() { } mitk::NavigationDataReaderCSV::~NavigationDataReaderCSV() { } +int mitk::NavigationDataReaderCSV::getNumberOfToolsInLine(std::string line) +{ + std::vector tokens=splitLine(line); + int size = tokens.size(); + int NumOfTools = (size-1)/8; + + if ( (size-1)%8 != 0 ) + { + MITK_ERROR("mitkNavigationDataReader") << "Illegal csv-file! Unexpected number of columns found! Assuming " << NumOfTools << " tools!"; + } + + return NumOfTools ; +} + +std::vector mitk::NavigationDataReaderCSV::splitLine(std::string line) +{ + std::vector elems; + std::stringstream ss(line); + std::string item; + while (std::getline(ss, item, ';')) { + elems.push_back(item); + } + return elems; +} + +mitk::NavigationData::Pointer mitk::NavigationDataReaderCSV::CreateNd(std::string timestamp, std::string valid, std::string X, std::string Y, std::string Z, std::string QX, std::string QY, std::string QZ, std::string QR) +{ + mitk::NavigationData::Pointer result= mitk::NavigationData::New(); + + mitk::Point3D position; + mitk::Quaternion orientation; + bool isValid = false; + double time; + + time = StringToDouble(timestamp); + + if (valid == "1") isValid = true; + else isValid = false; + + position[0] = StringToDouble(X); + position[1] = StringToDouble(Y); + position[2] = StringToDouble(Z); + + orientation[0] = StringToDouble(QX); + orientation[1] = StringToDouble(QY); + orientation[2] = StringToDouble(QZ); + orientation[3] = StringToDouble(QR); + + result->SetIGTTimeStamp(time); + result->SetDataValid(isValid); + result->SetPosition(position); + result->SetOrientation(orientation); + return result; +} + +double mitk::NavigationDataReaderCSV::StringToDouble( const std::string& s ) +{ + std::istringstream i(s); + double x; + if (!(i >> x)) + return 0; + return x; +} + mitk::NavigationDataSet::Pointer mitk::NavigationDataReaderCSV::Read(std::string fileName) { mitk::NavigationDataSet::Pointer returnValue = mitk::NavigationDataSet::New(1); std::vector fileContentLineByLine = GetFileContentLineByLine(fileName); for(int i=1; (i currentDatas; currentDatas.push_back(GetNavigationDataOutOfOneLine(fileContentLineByLine.at(i))); returnValue->AddNavigationDatas(currentDatas); } return returnValue; } std::vector mitk::NavigationDataReaderCSV::GetFileContentLineByLine(std::string filename) { std::vector readData = std::vector(); //save old locale char * oldLocale; oldLocale = setlocale( LC_ALL, 0 ); //define own locale std::locale C("C"); setlocale( LC_ALL, "C" ); //read file std::ifstream file; file.open(filename.c_str(), std::ios::in); if (file.good()) { //read out file file.seekg(0L, std::ios::beg); // move to begin of file while (! file.eof()) { std::string buffer; std::getline(file,buffer); // read out file line by line if (buffer.size() > 0) readData.push_back(buffer); } } file.close(); //switch back to old locale setlocale( LC_ALL, oldLocale ); return readData; } mitk::NavigationData::Pointer mitk::NavigationDataReaderCSV::GetNavigationDataOutOfOneLine(std::string line) { mitk::NavigationData::Pointer returnValue = mitk::NavigationData::New(); /* NOT FUNCTIONAL BECAUSE IT USES QSTRING WHICH IS NOT AVAILABLE HERE QString myLine = QString(line.c_str()); QStringList myLineList = myLine.split(';'); mitk::Point3D position; mitk::Quaternion orientation; bool valid = false; double time; if (m_Filetype = mitk::NavigationDataCSVSequentialPlayer::NavigationDataCSV) { if (myLineList.size() < 10) { MITK_ERROR << "Error: cannot read line: only found " << myLineList.size() << " fields. Last field: " << myLineList.at(myLineList.size()-1).toStdString() ; returnValue = GetEmptyNavigationData(); return returnValue; } time = myLineList.at(1).toDouble(); if (myLineList.at(2).toStdString() == "1") valid = true; position[0] = myLineList.at(3).toDouble(); position[1] = myLineList.at(4).toDouble(); position[2] = myLineList.at(5).toDouble(); orientation[0] = myLineList.at(6).toDouble(); orientation[1] = myLineList.at(7).toDouble(); orientation[2] = myLineList.at(8).toDouble(); orientation[3] = myLineList.at(9).toDouble(); } else { if (myLineList.size() < 10) { MITK_ERROR << "Error: cannot read line: only found " << myLineList.size() << " fields. Last field: " << myLineList.at(myLineList.size()-1).toStdString() ; returnValue = GetEmptyNavigationData(); return returnValue; } time = myLineList.at(1).toDouble(); //if (myLineList.at(2).toStdString() == "true") //valid-flag wurde hier nicht gespeichert valid = true; position[0] = myLineList.at(3).toDouble(); position[1] = myLineList.at(4).toDouble(); position[2] = myLineList.at(5).toDouble(); orientation[0] = myLineList.at(6).toDouble(); orientation[1] = myLineList.at(7).toDouble(); orientation[2] = myLineList.at(8).toDouble(); orientation[3] = myLineList.at(9).toDouble(); } returnValue->SetTimeStamp(time); returnValue->SetDataValid(valid); returnValue->SetPosition(position); returnValue->SetOrientation(orientation); */ return returnValue; } mitk::NavigationData::Pointer mitk::NavigationDataReaderCSV::GetEmptyNavigationData() { mitk::NavigationData::Pointer emptyNd = mitk::NavigationData::New(); mitk::NavigationData::PositionType position; mitk::NavigationData::OrientationType orientation(0.0,0.0,0.0,0.0); position.Fill(0.0); emptyNd->SetPosition(position); emptyNd->SetOrientation(orientation); emptyNd->SetDataValid(false); return emptyNd; } diff --git a/Modules/IGT/IO/mitkNavigationDataReaderCSV.h b/Modules/IGT/IO/mitkNavigationDataReaderCSV.h index c602974a29..adb05a29f5 100644 --- a/Modules/IGT/IO/mitkNavigationDataReaderCSV.h +++ b/Modules/IGT/IO/mitkNavigationDataReaderCSV.h @@ -1,53 +1,67 @@ /*=================================================================== 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 MITKNavigationDataReaderCSV_H_HEADER_INCLUDED_ #define MITKNavigationDataReaderCSV_H_HEADER_INCLUDED_ #include "mitkNavigationDataReaderInterface.h" namespace mitk { /** This class reads csv logged navigation datas from the hard disc and returns * the navigation data set. * * Caution: at the moment only one navigation data is supported which means that only * the data of the first navigation tool in the file is read! */ class MitkIGT_EXPORT NavigationDataReaderCSV : public NavigationDataReaderInterface { public: mitkClassMacro(NavigationDataReaderCSV, NavigationDataReaderInterface); itkNewMacro(Self); /** @return Returns the NavigationDataSet of the first tool in the given file. * Returns an empty NavigationDataSet if the file could not be read. */ virtual mitk::NavigationDataSet::Pointer Read(std::string fileName); protected: NavigationDataReaderCSV(); virtual ~NavigationDataReaderCSV(); std::vector GetFileContentLineByLine(std::string filename); mitk::NavigationData::Pointer GetNavigationDataOutOfOneLine(std::string line); mitk::NavigationData::Pointer GetEmptyNavigationData(); + + int getNumberOfToolsInLine(std::string line); + + std::vector splitLine(std::string line); + + + mitk::NavigationData::Pointer CreateNd(std::string timestamp, std::string valid, std::string X, std::string Y, std::string Z, std::string QX, std::string QY, std::string QZ, std::string QR); + + /** + * /brief Converts string to double returns zero if failing + */ + double StringToDouble( const std::string& s ); + + }; } #endif // MITKNavigationDataReaderCSV_H_HEADER_INCLUDED_