Medical Imaging Interaction Toolkit  2018.4.99-87d68d9f
Medical Imaging Interaction Toolkit
mitkNavigationToolReader.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 //Poco headers
14 #include <Poco/Zip/Decompress.h>
15 #include <Poco/Path.h>
16 
17 //mitk headers
19 #include "mitkTrackingTypes.h"
20 #include <mitkIOUtil.h>
21 #include <mitkSceneIO.h>
22 
23 //All Tracking devices, which should be available by default
31 
33 {
34  m_ToolfilePath = mitk::IOUtil::GetTempPath() + Poco::Path::separator() + "IGT_Toolfiles" + Poco::Path::separator();
35 }
36 
38 {
39 }
40 
41 mitk::NavigationTool::Pointer mitk::NavigationToolReader::DoRead(std::string filename)
42 {
43  //decompress all files into a temporary directory
44  std::ifstream file(filename.c_str(), std::ios::binary);
45  if (!file.good())
46  {
47  m_ErrorMessage = "Cannot open '" + filename + "' for reading";
48  return nullptr;
49  }
50 
51  std::string tempDirectory = m_ToolfilePath + GetFileWithoutPath(filename);
52  Poco::Zip::Decompress unzipper(file, Poco::Path(tempDirectory));
53  unzipper.decompressAllFiles();
54 
55  //use SceneSerialization to load the DataStorage
56  mitk::SceneIO::Pointer mySceneIO = mitk::SceneIO::New();
57  mitk::DataStorage::Pointer loadedStorage = mySceneIO->LoadScene(tempDirectory + Poco::Path::separator() + GetFileWithoutPath(filename) + ".storage");
58 
59  if (loadedStorage->GetAll()->size() == 0 || loadedStorage.IsNull())
60  {
61  m_ErrorMessage = "Invalid file: cannot parse tool data.";
62  return nullptr;
63  }
64 
65  //convert the DataStorage back to a NavigationTool-Object
66  mitk::DataNode::Pointer myNode = loadedStorage->GetAll()->ElementAt(0);
67  mitk::NavigationTool::Pointer returnValue = ConvertDataNodeToNavigationTool(myNode, tempDirectory);
68 
69  //delete the data-storage file which is not needed any more. The toolfile must be left in the temporary directory becauses it is linked in the datatreenode of the tool
70  std::remove((std::string(tempDirectory + Poco::Path::separator() + GetFileWithoutPath(filename) + ".storage")).c_str());
71 
72  return returnValue;
73 }
74 
75 mitk::NavigationTool::Pointer mitk::NavigationToolReader::ConvertDataNodeToNavigationTool(mitk::DataNode::Pointer node, std::string toolPath)
76 {
77  mitk::NavigationTool::Pointer returnValue = mitk::NavigationTool::New();
78 
79  //DateTreeNode with Name and Surface
80  returnValue->SetDataNode(node);
81 
82  //Identifier
83  std::string identifier;
84  node->GetStringProperty("identifier", identifier);
85  returnValue->SetIdentifier(identifier);
86 
87  node->RemoveProperty("identifier");
88 
89  //Serial Number
90  std::string serial;
91  node->GetStringProperty("serial number", serial);
92  returnValue->SetSerialNumber(serial);
93 
94  node->RemoveProperty("serial number");
95 
96  //Tracking Device
97  mitk::TrackingDeviceType device_type;
98  node->GetStringProperty("tracking device type", device_type);
99 
100  //For backward compability with old tool stroages (before 12/2015 device_type was an int value, now it is string)
101  if (device_type.size() == 0)
102  {
103  /*
104  This was the old enum. Numbers inserted for better readibility. Don't delete this if-case to allow loading of ols storages...
105  enum TrackingDeviceType
106  {
107  0 NDIPolaris, ///< Polaris: optical Tracker from NDI
108  1 NDIAurora, ///< Aurora: electromagnetic Tracker from NDI
109  2 ClaronMicron, ///< Micron Tracker: optical Tracker from Claron
110  3 IntuitiveDaVinci, ///< Intuitive Surgical: DaVinci Telemanipulator API Interface
111  4 AscensionMicroBird, ///< Ascension microBird / PCIBird family
112  5 VirtualTracker, ///< Virtual Tracking device class that produces random tracking coordinates
113  6 TrackingSystemNotSpecified, ///< entry for not specified or initialized tracking system
114  7 TrackingSystemInvalid, ///< entry for invalid state (mainly for testing)
115  8 NPOptitrack, ///< NaturalPoint: Optitrack optical Tracking System
116  9 OpenIGTLinkTrackingDeviceConnection ///< Device which is connected via open igt link
117  };
118  */
119  int device_type_old;
120  node->GetIntProperty("tracking device type", device_type_old);
121  switch (device_type_old)
122  {
123  case 0:device_type = mitk::NDIPolarisTypeInformation::GetTrackingDeviceName(); break;
124  case 1:device_type = mitk::NDIAuroraTypeInformation::GetTrackingDeviceName(); break;
125  case 2:device_type = mitk::MicronTrackerTypeInformation::GetTrackingDeviceName(); break;
126  case 3:device_type = "IntuitiveDaVinci"; break;
127  case 4:device_type = "AscensionMicroBird"; break;
130  case 7:device_type = "TrackingSystemInvalid"; break;
132  case 9:device_type = mitk::OpenIGTLinkTypeInformation::GetTrackingDeviceName(); break;
133  default: device_type = mitk::UnspecifiedTrackingTypeInformation::GetTrackingDeviceName(); break; //default... unknown...
134  }
135  }
136 
137  node->RemoveProperty("tracking device type");
138 
139  returnValue->SetTrackingDeviceType(static_cast<mitk::TrackingDeviceType>(device_type));
140 
141  //Tool Type
142  int type;
143  node->GetIntProperty("tracking tool type", type);
144  returnValue->SetType(static_cast<mitk::NavigationTool::NavigationToolType>(type));
145 
146  node->RemoveProperty("tracking tool type");
147 
148  //Calibration File Name
149  std::string calibration_filename;
150  node->GetStringProperty("toolfileName", calibration_filename);
151  if (calibration_filename == "none")
152  {
153  returnValue->SetCalibrationFile("none");
154  }
155  else
156  {
157  std::string calibration_filename_with_path = toolPath + Poco::Path::separator() + calibration_filename;
158  returnValue->SetCalibrationFile(calibration_filename_with_path);
159  }
160 
161  node->RemoveProperty("toolfileName");
162 
163  //Tool Landmarks
164  mitk::PointSet::Pointer ToolRegLandmarks = mitk::PointSet::New();
165  mitk::PointSet::Pointer ToolCalLandmarks = mitk::PointSet::New();
166  std::string RegLandmarksString;
167  std::string CalLandmarksString;
168  node->GetStringProperty("ToolRegistrationLandmarks", RegLandmarksString);
169  node->GetStringProperty("ToolCalibrationLandmarks", CalLandmarksString);
170  ToolRegLandmarks = ConvertStringToPointSet(RegLandmarksString);
171  ToolCalLandmarks = ConvertStringToPointSet(CalLandmarksString);
172  returnValue->SetToolLandmarks(ToolRegLandmarks);
173  returnValue->SetToolControlPoints(ToolCalLandmarks);
174 
175  node->RemoveProperty("ToolRegistrationLandmarks");
176  node->RemoveProperty("ToolCalibrationLandmarks");
177 
178  //Tool Tip
179  std::string toolTipPositionString;
180  std::string toolTipOrientationString;
181  bool positionSet = node->GetStringProperty("ToolTipPosition", toolTipPositionString);
182  bool orientationSet = node->GetStringProperty("ToolAxisOrientation", toolTipOrientationString);
183 
184  if (positionSet && orientationSet) //only define tooltip if it is set
185  {
186  returnValue->SetToolTipPosition(ConvertStringToPoint(toolTipPositionString));
187  returnValue->SetToolAxisOrientation(ConvertStringToQuaternion(toolTipOrientationString));
188  }
189  else if (positionSet != orientationSet)
190  {
191  MITK_WARN << "Tooltip definition incomplete: position and orientation have to be set! Skipping tooltip definition.";
192  }
193 
194  node->RemoveProperty("ToolTipPosition");
195  node->RemoveProperty("ToolAxisOrientation");
196 
197  return returnValue;
198 }
199 
200 std::string mitk::NavigationToolReader::GetFileWithoutPath(std::string FileWithPath)
201 {
202  Poco::Path myFile(FileWithPath.c_str());
203  return myFile.getFileName();
204 }
205 
207 {
209  std::string pointSeperator = "|";
210  std::string valueSeperator = ";";
211  std::vector<std::string> points;
212  split(string, pointSeperator, points);
213  for (unsigned int i = 0; i < points.size(); i++)
214  {
215  std::vector<std::string> values;
216  split(points.at(i), valueSeperator, values);
217  if (values.size() == 4)
218  {
219  double index = atof(values.at(0).c_str());
220  mitk::Point3D point;
221  point[0] = atof(values.at(1).c_str());
222  point[1] = atof(values.at(2).c_str());
223  point[2] = atof(values.at(3).c_str());
224  returnValue->SetPoint(index, point);
225  }
226  }
227  return returnValue;
228 }
230 {
231  std::string valueSeperator = ";";
232  std::vector<std::string> values;
233  split(string, valueSeperator, values);
234  mitk::Point3D point;
235  if (values.size() == 3)
236  {
237  point[0] = atof(values.at(0).c_str());
238  point[1] = atof(values.at(1).c_str());
239  point[2] = atof(values.at(2).c_str());
240  }
241  return point;
242 }
243 
245 {
246  std::string valueSeperator = ";";
247  std::vector<std::string> values;
248  split(string, valueSeperator, values);
249  mitk::Quaternion quat = mitk::Quaternion(0, 0, 0, 1);
250  if (values.size() == 4)
251  {
252  quat = mitk::Quaternion(atof(values.at(0).c_str()),
253  atof(values.at(1).c_str()),
254  atof(values.at(2).c_str()),
255  atof(values.at(3).c_str()));
256  }
257  return quat;
258 }
259 
260 void mitk::NavigationToolReader::split(std::string& text, std::string& separators, std::vector<std::string>& words)
261 {
262  int n = text.length();
263  int start, stop;
264 
265  start = text.find_first_not_of(separators);
266  while ((start >= 0) && (start < n))
267  {
268  stop = text.find_first_of(separators, start);
269  if ((stop < 0) || (stop > n)) stop = n;
270  words.push_back(text.substr(start, stop - start));
271  start = text.find_first_not_of(separators, stop + 1);
272  }
273 }
mitk::NavigationTool::Pointer DoRead(std::string filename)
This method reads a navigation tool from a file.
static std::string GetTempPath()
Definition: mitkIOUtil.cpp:378
static Pointer New()
std::string GetFileWithoutPath(std::string FileWithPath)
static Pointer New()
mitk::NavigationTool::Pointer ConvertDataNodeToNavigationTool(mitk::DataNode::Pointer node, std::string toolPath)
#define MITK_WARN
Definition: mitkLogMacros.h:19
mitk::Quaternion ConvertStringToQuaternion(std::string string)
vnl_quaternion< ScalarType > Quaternion
mitk::Point3D ConvertStringToPoint(std::string string)
static Pointer New()
std::string TrackingDeviceType
void split(std::string &text, std::string &separators, std::vector< std::string > &words)
mitk::PointSet::Pointer ConvertStringToPointSet(std::string string)