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