Medical Imaging Interaction Toolkit  2016.11.0
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 // MITK
19 #include <mitkIGTMimeTypes.h>
20 #include <mitkLocaleSwitch.h>
21 
22 // STL
23 #include <fstream>
24 
25 
27  mitk::IGTMimeTypes::NAVIGATIONDATASETCSV_MIMETYPE(),
28  "MITK NavigationData Reader (CSV)")
29 {
31 }
32 
34 {
35 }
36 
38 {
39 }
40 
42 {
43  return new NavigationDataReaderCSV(*this);
44 }
45 
46 std::vector<itk::SmartPointer<mitk::BaseData>> mitk::NavigationDataReaderCSV::Read()
47 {
48  std::vector<std::string> fileContent = GetFileContentLineByLine(GetInputLocation());
49  int NumOfTools = getNumberOfToolsInLine(fileContent[0]);
50 
52  std::vector<mitk::BaseData::Pointer> result;
53  result.push_back(returnValue.GetPointer());
54 
55  // start from line 1 to leave out header
56  for (unsigned int i = 1; i<fileContent.size(); i++)
57  {
58  returnValue->AddNavigationDatas(parseLine(fileContent[i], NumOfTools));
59  }
60 
61  return result;
62 }
63 
64 
66 {
67  std::vector<std::string> tokens=splitLine(line);
68  int size = tokens.size();
69  int NumOfTools = (size)/9;
70 
71  if ( (size)%9 != 0 )
72  {
73  MITK_ERROR("mitkNavigationDataReader") << "Illegal csv-file! Unexpected number of columns found! Assuming " << NumOfTools << " tools!";
74  }
75 
76  return NumOfTools ;
77 }
78 
79 std::vector<std::string> mitk::NavigationDataReaderCSV::splitLine(std::string line)
80 {
81  std::vector<std::string> elems;
82  std::stringstream ss(line);
83  std::string item;
84  while (std::getline(ss, item, ';')) {
85  elems.push_back(item);
86  }
87  return elems;
88 }
89 
90 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)
91 {
93 
94  mitk::Point3D position;
95  mitk::Quaternion orientation;
96  bool isValid = false;
97  double time;
98 
99  time = StringToDouble(timestamp);
100 
101  if (valid == "1") isValid = true;
102  else isValid = false;
103 
104  position[0] = StringToDouble(X);
105  position[1] = StringToDouble(Y);
106  position[2] = StringToDouble(Z);
107 
108  orientation[0] = StringToDouble(QX);
109  orientation[1] = StringToDouble(QY);
110  orientation[2] = StringToDouble(QZ);
111  orientation[3] = StringToDouble(QR);
112 
113  result->SetIGTTimeStamp(time);
114  result->SetDataValid(isValid);
115  result->SetPosition(position);
116  result->SetOrientation(orientation);
117  return result;
118 }
119 
120 double mitk::NavigationDataReaderCSV::StringToDouble( const std::string& s )
121 {
122  std::istringstream i(s);
123  double x;
124  if (!(i >> x))
125  return 0;
126  return x;
127 }
128 
129 std::vector<mitk::NavigationData::Pointer> mitk::NavigationDataReaderCSV::parseLine(std::string line, int NumOfTools)
130 {
131  std::vector<std::string> parts = splitLine(line);
132  std::vector<mitk::NavigationData::Pointer> result;
133 
134 
135 
136  for (int n = 0; n < NumOfTools; n++)
137  {
139  int offset = n * 9;
140  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]);
141  result.push_back(nd);
142  }
143  return result;
144 }
145 
146 
148 {
149 std::vector<std::string> readData = std::vector<std::string>();
150 
151 //define own locale
152 mitk::LocaleSwitch localeSwitch("C");
153 
154 //read file
155 std::ifstream file;
156 file.open(filename.c_str(), std::ios::in);
157 if (file.good())
158  {
159  //read out file
160  file.seekg(0L, std::ios::beg); // move to begin of file
161  while (! file.eof())
162  {
163  std::string buffer;
164  std::getline(file,buffer); // read out file line by line
165  if (buffer.size() > 0) readData.push_back(buffer);
166 
167  }
168  }
169 
170 file.close();
171 
172 return readData;
173 }
static Pointer New()
static char * line
Definition: svm.cpp:2884
itk::SmartPointer< Self > Pointer
virtual mitk::NavigationDataReaderCSV * Clone() const override
std::vector< mitk::NavigationData::Pointer > parseLine(std::string line, int NumOfTools)
#define MITK_ERROR
Definition: mitkLogMacros.h:24
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.
static const std::string filename
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.
virtual std::vector< itk::SmartPointer< BaseData > > Read() override
Reads a path or stream and creates a list of BaseData objects.
std::vector< std::string > GetFileContentLineByLine(std::string filename)
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.