Medical Imaging Interaction Toolkit  2018.4.99-1bab67a2
Medical Imaging Interaction Toolkit
mitkNavigationDataReaderCSV.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
13 // MITK
15 #include <mitkIGTMimeTypes.h>
16 #include <mitkLocaleSwitch.h>
17 
18 // STL
19 #include <fstream>
20 
21 
23  mitk::IGTMimeTypes::NAVIGATIONDATASETCSV_MIMETYPE(),
24  "MITK NavigationData Reader (CSV)")
25 {
27 }
28 
30 {
31 }
32 
34 {
35 }
36 
38 {
39  return new NavigationDataReaderCSV(*this);
40 }
41 
42 std::vector<itk::SmartPointer<mitk::BaseData>> mitk::NavigationDataReaderCSV::DoRead()
43 {
44  std::vector<std::string> fileContent = GetFileContentLineByLine(GetInputLocation());
45  int NumOfTools = getNumberOfToolsInLine(fileContent[0]);
46 
47  mitk::NavigationDataSet::Pointer returnValue = mitk::NavigationDataSet::New(NumOfTools);
48  std::vector<mitk::BaseData::Pointer> result;
49  result.push_back(returnValue.GetPointer());
50 
51  // start from line 1 to leave out header
52  for (unsigned int i = 1; i<fileContent.size(); i++)
53  {
54  returnValue->AddNavigationDatas(parseLine(fileContent[i], NumOfTools));
55  }
56 
57  return result;
58 }
59 
60 
62 {
63  std::vector<std::string> tokens=splitLine(line);
64  int size = tokens.size();
65  int NumOfTools = (size)/9;
66 
67  if ( (size)%9 != 0 )
68  {
69  MITK_ERROR("mitkNavigationDataReader") << "Illegal csv-file! Unexpected number of columns found! Assuming " << NumOfTools << " tools!";
70  }
71 
72  return NumOfTools ;
73 }
74 
75 std::vector<std::string> mitk::NavigationDataReaderCSV::splitLine(std::string line)
76 {
77  std::vector<std::string> elems;
78  std::stringstream ss(line);
79  std::string item;
80  while (std::getline(ss, item, ';')) {
81  elems.push_back(item);
82  }
83  return elems;
84 }
85 
86 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)
87 {
88  mitk::NavigationData::Pointer result= mitk::NavigationData::New();
89 
90  mitk::Point3D position;
91  mitk::Quaternion orientation;
92  bool isValid = false;
93  double time;
94 
95  time = StringToDouble(timestamp);
96 
97  if (valid == "1") isValid = true;
98  else isValid = false;
99 
100  position[0] = StringToDouble(X);
101  position[1] = StringToDouble(Y);
102  position[2] = StringToDouble(Z);
103 
104  orientation[0] = StringToDouble(QX);
105  orientation[1] = StringToDouble(QY);
106  orientation[2] = StringToDouble(QZ);
107  orientation[3] = StringToDouble(QR);
108 
109  result->SetIGTTimeStamp(time);
110  result->SetDataValid(isValid);
111  result->SetPosition(position);
112  result->SetOrientation(orientation);
113  return result;
114 }
115 
116 double mitk::NavigationDataReaderCSV::StringToDouble( const std::string& s )
117 {
118  std::istringstream i(s);
119  double x;
120  if (!(i >> x))
121  return 0;
122  return x;
123 }
124 
125 std::vector<mitk::NavigationData::Pointer> mitk::NavigationDataReaderCSV::parseLine(std::string line, int NumOfTools)
126 {
127  std::vector<std::string> parts = splitLine(line);
128  std::vector<mitk::NavigationData::Pointer> result;
129 
130 
131 
132  for (int n = 0; n < NumOfTools; n++)
133  {
134  mitk::NavigationData::Pointer nd;
135  int offset = n * 9;
136  nd = CreateNd(parts[offset], parts[offset + 1], parts[offset + 2], parts[offset + 3], parts[offset + 4], parts[offset + 5], parts[offset + 6], parts[offset + 7], parts[offset + 8]);
137  result.push_back(nd);
138  }
139  return result;
140 }
141 
142 
143 std::vector<std::string> mitk::NavigationDataReaderCSV::GetFileContentLineByLine(std::string filename)
144 {
145 std::vector<std::string> readData = std::vector<std::string>();
146 
147 //define own locale
148 mitk::LocaleSwitch localeSwitch("C");
149 
150 //read file
151 std::ifstream file;
152 file.open(filename.c_str(), std::ios::in);
153 if (file.good())
154  {
155  //read out file
156  file.seekg(0L, std::ios::beg); // move to begin of file
157  while (! file.eof())
158  {
159  std::string buffer;
160  std::getline(file,buffer); // read out file line by line
161  if (buffer.size() > 0) readData.push_back(buffer);
162 
163  }
164  }
165 
166 file.close();
167 
168 return readData;
169 }
static Pointer New()
static char * line
Definition: svm.cpp:2870
mitk::NavigationDataReaderCSV * Clone() const override
std::vector< mitk::NavigationData::Pointer > parseLine(std::string line, int NumOfTools)
#define MITK_ERROR
Definition: mitkLogMacros.h:20
DataCollection - Class to facilitate loading/accessing structured data.
double StringToDouble(const std::string &s)
std::vector< std::string > splitLine(std::string line)
static Vector3D offset
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)
us::ServiceRegistration< IFileReader > RegisterService(us::ModuleContext *context=us::GetModuleContext())
Convenience class to temporarily change the current locale.
std::vector< itk::SmartPointer< BaseData > > DoRead() override
vnl_quaternion< ScalarType > Quaternion
static bool in(Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4)
Definition: jsoncpp.cpp:244
Base class for creating mitk::BaseData objects from files or streams.
std::string GetInputLocation() const override
Get the current input location.
std::vector< std::string > GetFileContentLineByLine(std::string filename)