MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkNavigationDataCSVSequentialPlayer.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
14#include <QString>
15#include <QStringList>
16#include <iostream>
17#include <fstream>
18
27
31
33{
34 return m_CurrentPos >= static_cast<int>(m_NavigationDatas.size());
35}
36
38SetFileName(const std::string& fileName)
39{
40 this->SetNumberOfIndexedOutputs(1);
41 FillOutputEmpty(0);
42
43 MITK_INFO << "Reading file: " << fileName;
44 m_NavigationDatas = GetNavigationDatasFromFile(fileName);
45
46 this->Modified();
47}
48
50{
51 this->SetNthOutput(number, GetEmptyNavigationData());
52}
53
55{
56 mitk::NavigationData::Pointer emptyNd = mitk::NavigationData::New();
58 mitk::NavigationData::OrientationType orientation(0.0, 0.0, 0.0, 0.0);
59 position.Fill(0.0);
60
61 emptyNd->SetPosition(position);
62 emptyNd->SetOrientation(orientation);
63 emptyNd->SetDataValid(false);
64 return emptyNd;
65}
67{
68 return m_NavigationDatas.size();
69}
71{
72 for (unsigned int index = 0; index < this->GetNumberOfOutputs(); index++)
73 {
74 mitk::NavigationData* output = this->GetOutput(index);
75
76 if (m_CurrentPos > static_cast<int>(m_NavigationDatas.size()))
77 {
78 FillOutputEmpty(index);
79 return;
80 }
81
82 output->Graft(this->m_NavigationDatas.at(m_CurrentPos));
83 m_CurrentPos++;
84 }
85}
86
88{
89 this->Modified(); // make sure that we need to be updated
90 Superclass::UpdateOutputInformation();
91}
92
93std::vector<mitk::NavigationData::Pointer> mitk::NavigationDataCSVSequentialPlayer::GetNavigationDatasFromFile(std::string filename)
94{
95 std::vector<mitk::NavigationData::Pointer> returnValue = std::vector<mitk::NavigationData::Pointer>();
96 std::vector<std::string> fileContentLineByLine = GetFileContentLineByLine(filename);
97 std::size_t i = m_HeaderRow
98 ? 1 //file has a header row, so it has to be skipped when reading the NavigationDatas
99 : 0; //file has no header row, so no need to skip the first row
100
101 for ( ; i < fileContentLineByLine.size(); ++i)
102 {
103 returnValue.push_back(GetNavigationDataOutOfOneLine(fileContentLineByLine.at(i)));
104 }
105
106 return returnValue;
107}
108
109std::vector<std::string> mitk::NavigationDataCSVSequentialPlayer::GetFileContentLineByLine(std::string filename)
110{
111 std::vector<std::string> readData = std::vector<std::string>();
112
113 //save old locale
114 char * oldLocale;
115 oldLocale = setlocale(LC_ALL, nullptr);
116
117 //define own locale
118 std::locale C("C");
119 setlocale(LC_ALL, "C");
120
121 //read file
122 std::ifstream file;
123 file.open(filename.c_str(), std::ios::in);
124 if (file.good())
125 {
126 //read out file
127 file.seekg(0L, std::ios::beg); // move to begin of file
128
129 int count = 0;
130 while (!file.eof())
131 {
132 std::string buffer;
133 std::getline(file, buffer); // read out file line by line
134
135 readData.push_back(buffer);
136
137 ++count; if (count == m_SampleCount) count = 0;
138 }
139 }
140
141 file.close();
142
143 //switch back to old locale
144 setlocale(LC_ALL, oldLocale);
145
146 return readData;
147}
148
150{
151 mitk::NavigationData::Pointer returnValue = mitk::NavigationData::New();
152
153 QString myLine = QString(line.c_str());
154
155 QStringList myLineList = myLine.split(m_SeparatorSign);
156
157 mitk::Point3D position;
158 mitk::Quaternion orientation;
159 bool valid = false;
160
161 //this is for custom csv files. You have adapt the column numbers to correctly
162 //interpret your csv file.
164 {
165 if (myLineList.size() < m_MinNumberOfColumns)
166 {
167 MITK_ERROR << "Error: cannot read line: only found " << myLineList.size() << " fields. Last field: " << myLineList.at(myLineList.size() - 1).toStdString();
168 returnValue = GetEmptyNavigationData();
169 return returnValue;
170 }
171
172 valid = true; //if no valid flag is given: simply set to true
173
174
175 //############# Variant for the Aurora measurements ###############
176 //############# (CUSTOM .csv files from MITK) ###############
177
178 position[0] = myLineList.at(3).toDouble();
179 position[1] = myLineList.at(4).toDouble();
180 position[2] = myLineList.at(5).toDouble();
181
182 orientation[0] = myLineList.at(6).toDouble(); //qx
183 orientation[1] = myLineList.at(7).toDouble(); //qy
184 orientation[2] = myLineList.at(8).toDouble(); //qz
185 orientation[3] = myLineList.at(9).toDouble(); //qr
186
187 if(!m_RightHanded) //MITK uses a right handed coordinate system, so the position needs to be converted
188 {
189 position[0] = position[0]*(-1);
190 }
191
192 if (m_UseQuats) //Use Quaternions to construct the orientation of the NavigationData
193 {
194 orientation[0] = myLineList.at(m_Qx).toDouble(); //qx
195 orientation[1] = myLineList.at(m_Qy).toDouble(); //qy
196 orientation[2] = myLineList.at(m_Qz).toDouble(); //qz
197 orientation[3] = myLineList.at(m_Qr).toDouble(); //qr
198 }
199 else //Use the Euler Angles to construct the orientation of the NavigationData
200 {
201 double azimuthAngle;
202 double elevationAngle;
203 double rollAngle;
204 if(m_Azimuth < 0) //azimuth is not defined so set him to zero
205 {
206 azimuthAngle = 0;
207 }
208 else
209 {
210 azimuthAngle = myLineList.at(m_Azimuth).toDouble();
211 }
212 if(m_Elevation < 0)// elevation is not defined so set him to zero
213 {
214 elevationAngle = 0;
215 }
216 else
217 {
218 elevationAngle = myLineList.at(m_Elevation).toDouble();
219 }
220 if(m_Roll < 0) //roll is not defined so set him to zero
221 {
222 rollAngle = 0;
223 }
224 else
225 {
226 rollAngle = myLineList.at(m_Roll).toDouble();
227 }
228
229
230 if (!m_EulersInRadiants) //the Euler Angles are in Degrees but MITK uses radiants so they need to be converted
231 {
232 azimuthAngle = azimuthAngle / 180 * itk::Math::pi;
233 elevationAngle = elevationAngle / 180 * itk::Math::pi;
234 rollAngle = rollAngle / 180 * itk::Math::pi;
235 }
236 vnl_quaternion<double> eulerQuat(rollAngle, elevationAngle, azimuthAngle);
237 orientation = eulerQuat;
238 }
239
240 if(!m_RightHanded) //MITK uses a right handed coordinate system, so the orientation needs to be converted
241 {
242 //code block for conversion from left-handed to right-handed
243 mitk::Quaternion linksZuRechtsdrehend;
244 double rotationAngle = -itk::Math::pi;
245 double rotationAxis[3];
246 rotationAxis[0] = 0;
247 rotationAxis[1] = 0;
248 rotationAxis[2] = 1;
249
250 linksZuRechtsdrehend[3] = cos(rotationAngle / 2);
251 linksZuRechtsdrehend[0] = rotationAxis[0] * sin(rotationAngle / 2);
252 linksZuRechtsdrehend[1] = rotationAxis[1] * sin(rotationAngle / 2);
253 linksZuRechtsdrehend[2] = rotationAxis[2] * sin(rotationAngle / 2);
254
255 orientation = orientation * linksZuRechtsdrehend;
256 }
257
258 }
259 //this is for MITK csv files that have been recorded with the MITK
260 //navigation data recorder. You can also use the navigation data player
261 //class from the MITK-IGT module instead.
263 {
264 if (myLineList.size() < 8)
265 {
266 MITK_ERROR << "Error: cannot read line: only found " << myLineList.size() << " fields. Last field: " << myLineList.at(myLineList.size() - 1).toStdString();
267 returnValue = GetEmptyNavigationData();
268 return returnValue;
269 }
270
271 if (myLineList.at(3).toStdString() == "1") valid = true;
272
273 position[0] = myLineList.at(2).toDouble();
274 position[1] = myLineList.at(3).toDouble();
275 position[2] = myLineList.at(4).toDouble();
276
277 orientation[0] = myLineList.at(5).toDouble(); //qx
278 orientation[1] = myLineList.at(6).toDouble(); //qy
279 orientation[2] = myLineList.at(7).toDouble(); //qz
280 orientation[3] = myLineList.at(8).toDouble(); //qr
281 }
282
283 returnValue->SetDataValid(valid);
284 returnValue->SetPosition(position);
285 returnValue->SetOrientation(orientation);
286
287 return returnValue;
288}
289void mitk::NavigationDataCSVSequentialPlayer::SetOptions(bool rightHanded, char separatorSign, int sampleCount, bool headerRow, int xPos, int yPos,
290 int zPos, bool useQuats, int qx, int qy, int qz, int qr, int azimuth, int elevation, int roll,
291 bool eulerInRadiants, int minNumberOfColumns)
292{
293 m_RightHanded = rightHanded;
294 m_SeparatorSign = separatorSign;
295 m_SampleCount = sampleCount;
296 m_HeaderRow = headerRow;
297 m_XPos = xPos;
298 m_YPos = yPos;
299 m_ZPos = zPos;
300 m_UseQuats = useQuats;
301 m_Qx = qx;
302 m_Qy = qy;
303 m_Qz = qz;
304 m_Qr = qr;
305 m_Azimuth = azimuth;
306 m_Elevation = elevation;
307 m_Roll = roll;
308 m_EulersInRadiants = eulerInRadiants;
309 m_MinNumberOfColumns = minNumberOfColumns;
310}
Definition diagrams_c.h:5
void SetFileName(const std::string &_FileName)
sets the file name and path (if XMLString is set, this is neglected)
void SetOptions(bool rightHanded, char seperatorSign, int sampleCount, bool headerRow, int xPos, int yPos, int zPos, bool useQuats, int qx, int qy, int qz, int qr, int azimuth, int elevatino, int roll, bool eulerInRadiants, int minNumberOfColums)
SetOptions sets the options for reading out the data out of the correct positions of the file....
void UpdateOutputInformation() override
Used for pipeline update just to tell the pipeline that we always have to update.
mitk::NavigationData::Pointer GetNavigationDataOutOfOneLine(std::string line)
std::vector< mitk::NavigationData::Pointer > m_NavigationDatas
std::vector< std::string > GetFileContentLineByLine(std::string filename)
std::vector< mitk::NavigationData::Pointer > GetNavigationDatasFromFile(std::string filename)
Base class for using mitk::NavigationData as a filter source. Subclasses can play objects of mitk::Na...
void Graft(const DataObject *data) override
Graft the data and information from one NavigationData to another.
mitk::Quaternion OrientationType
Type that holds the orientation part of the tracking data.
mitk::Point3D PositionType
Type that holds the position part of the tracking data.
IGT Exceptions.