diff --git a/Modules/IGT/IO/mitkNavigationDataReaderCSV.cpp b/Modules/IGT/IO/mitkNavigationDataReaderCSV.cpp index 2b1084bf4c..ef99f3fe37 100644 --- a/Modules/IGT/IO/mitkNavigationDataReaderCSV.cpp +++ b/Modules/IGT/IO/mitkNavigationDataReaderCSV.cpp @@ -1,162 +1,162 @@ /*=================================================================== 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; } std::vector mitk::NavigationDataReaderCSV::parseLine(std::string line, int NumOfTools) { - std::vector parts= splitLine(line); - //time "herausscheneiden" - std::string time= parts[0]; - //for schleife über alle weiteren element -> Substring -> aufrufen der CreateNd funktion? std::vector result; + std::string time= parts[0]; + + std::vector parts= splitLine(line); + for (int n= 0; n=NumOfTools; n++) { mitk::NavigationData::Pointer nd; nd = CreateNd(time, parts[n+1],parts[n+2],parts[n+3], parts[n+4], parts[n+5], parts[n+6], parts[n+7], parts[n+8]); result.push_back(nd); } return result; } mitk::NavigationDataSet::Pointer mitk::NavigationDataReaderCSV::Read(std::string fileName) { std::vector fileContent = GetFileContentLineByLine(fileName); int NumOfTools = getNumberOfToolsInLine(fileContent[0]); mitk::NavigationDataSet::Pointer returnValue = mitk::NavigationDataSet::New(NumOfTools); for (int i = 1; iAddNavigationDatas( parseLine( fileContent[i], NumOfTools) ); } 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; } diff --git a/Modules/IGT/IO/mitkNavigationDataReaderCSV.h b/Modules/IGT/IO/mitkNavigationDataReaderCSV.h index 65b567fe37..91f6eb9077 100644 --- a/Modules/IGT/IO/mitkNavigationDataReaderCSV.h +++ b/Modules/IGT/IO/mitkNavigationDataReaderCSV.h @@ -1,66 +1,78 @@ /*=================================================================== 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(); + /** + * /brief Creates a NavigationData Pointer based on the given Input. + */ + 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 Presents File Content line by line + */ std::vector GetFileContentLineByLine(std::string filename); + /** + * /brief Calculates the Number of Tools based on the number of colums per line. + */ int getNumberOfToolsInLine(std::string line); - std::vector splitLine(std::string line); - - + /** + * /brief Converts string to double returns zero if failing + */ std::vector parseLine(std::string line, int NumOfTools); - 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 ); - + /** + * /brief Split line in elemens based on a given delim + */ + std::vector splitLine(std::string line); }; } #endif // MITKNavigationDataReaderCSV_H_HEADER_INCLUDED_