Medical Imaging Interaction Toolkit  2018.4.99-87d68d9f
Medical Imaging Interaction Toolkit
mitkNavigationToolWriter.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/Compress.h>
15 #include <Poco/Path.h>
16 
17 //mitk headers
20 #include <mitkProperties.h>
21 #include <mitkSceneIO.h>
22 #include <mitkPointSet.h>
23 #include <mitkIOUtil.h>
24 
25 //std headers
26 #include <cstdio>
27 
29  {
30 
31  }
32 
34  {
35 
36  }
37 
38 bool mitk::NavigationToolWriter::DoWrite(std::string FileName,mitk::NavigationTool::Pointer Tool)
39  {
40  //some initial validation checks...
41  if ( Tool.IsNull())
42  {
43  m_ErrorMessage = "Cannot write a navigation tool containing invalid tool data, aborting!";
45  return false;
46  }
47 
48 
49  // Workaround for a problem: the geometry might be modified if the tool is tracked. If this
50  // modified geometry is saved the surface representation is moved by this offset. To avoid
51  // this bug, the geometry is set to identity for the saving progress and restored later.
52  mitk::BaseGeometry::Pointer geometryBackup;
53  if ( Tool->GetDataNode().IsNotNull()
54  && (Tool->GetDataNode()->GetData()!=nullptr)
55  && (Tool->GetDataNode()->GetData()->GetGeometry()!=nullptr)
56  )
57  {
58  geometryBackup = Tool->GetDataNode()->GetData()->GetGeometry()->Clone();
59  Tool->GetDataNode()->GetData()->GetGeometry()->SetIdentity();
60  }
61  else {MITK_WARN << "Saving a tool with invalid data node, proceeding but errors might occure!";}
62 
63  //convert whole data to a mitk::DataStorage
66  saveStorage->Add(thisTool);
67 
68  //use SceneSerialization to save the DataStorage
69  std::string DataStorageFileName = mitk::IOUtil::CreateTemporaryDirectory() + Poco::Path::separator() + GetFileWithoutPath(FileName) + ".storage";
70  mitk::SceneIO::Pointer mySceneIO = mitk::SceneIO::New();
71  mySceneIO->SaveScene(saveStorage->GetAll(),saveStorage,DataStorageFileName);
72 
73  //now put the DataStorage and the Toolfile in a ZIP-file
74  std::ofstream file( FileName.c_str(), std::ios::binary | std::ios::out);
75  if (!file.good())
76  {
77  m_ErrorMessage = "Could not open a zip file for writing: '" + FileName + "'";
79  return false;
80  }
81  else
82  {
83  Poco::Zip::Compress zipper( file, true );
84  zipper.addFile(DataStorageFileName,GetFileWithoutPath(DataStorageFileName));
85  if (Tool->GetCalibrationFile()!="none") zipper.addFile(Tool->GetCalibrationFile(),GetFileWithoutPath(Tool->GetCalibrationFile()));
86  zipper.close();
87  }
88 
89  //delete the data storage
90  std::remove(DataStorageFileName.c_str());
91 
92  //restore original geometry
93  if (geometryBackup.IsNotNull()) {Tool->GetDataNode()->GetData()->SetGeometry(geometryBackup);}
94 
95  return true;
96  }
97 
99  {
100  mitk::DataNode::Pointer thisTool = Tool->GetDataNode();
101  //Name
102  if (Tool->GetDataNode().IsNull())
103  {
104  thisTool = mitk::DataNode::New();
105  thisTool->SetName("none");
106  }
107 
108  //Identifier
109  thisTool->AddProperty("identifier",mitk::StringProperty::New(Tool->GetIdentifier().c_str()), nullptr, true);
110  //Serial Number
111  thisTool->AddProperty("serial number",mitk::StringProperty::New(Tool->GetSerialNumber().c_str()), nullptr, true);
112  //Tracking Device
113  thisTool->AddProperty("tracking device type",mitk::StringProperty::New(Tool->GetTrackingDeviceType()), nullptr, true);
114  //Tool Type
115  thisTool->AddProperty("tracking tool type",mitk::IntProperty::New(Tool->GetType()), nullptr, true);
116  //Calibration File Name
117  thisTool->AddProperty("toolfileName",mitk::StringProperty::New(GetFileWithoutPath(Tool->GetCalibrationFile())), nullptr, true);
118  //Tool Landmarks
119  thisTool->AddProperty("ToolRegistrationLandmarks",mitk::StringProperty::New(ConvertPointSetToString(Tool->GetToolLandmarks())), nullptr, true);
120  thisTool->AddProperty("ToolCalibrationLandmarks",mitk::StringProperty::New(ConvertPointSetToString(Tool->GetToolControlPoints())), nullptr, true);
121 
122  //Tool Tip
123  if (Tool->IsToolTipSet())
124  {
125  thisTool->AddProperty("ToolTipPosition",mitk::StringProperty::New(ConvertPointToString(Tool->GetToolTipPosition())), nullptr, true);
126  thisTool->AddProperty("ToolAxisOrientation",mitk::StringProperty::New(ConvertQuaternionToString(Tool->GetToolAxisOrientation())), nullptr, true);
127  }
128 
129  //Material is not needed, to avoid errors in scene serialization we have to do this:
130  thisTool->RemoveProperty("material");
131 
132  return thisTool;
133  }
134 
135 std::string mitk::NavigationToolWriter::GetFileWithoutPath(std::string FileWithPath)
136  {
137  Poco::Path myFile(FileWithPath.c_str());
138  return myFile.getFileName();
139  }
140 
142  {
143  std::stringstream returnValue;
145  for ( it = pointSet->GetPointSet()->GetPointData()->Begin();it != pointSet->GetPointSet()->GetPointData()->End();it++ )
146  {
147  mitk::Point3D thisPoint = pointSet->GetPoint(it->Index());
148  returnValue << it->Index() << ";" << ConvertPointToString(thisPoint) << "|";
149  }
150  return returnValue.str();
151  }
152 
154 {
155 std::stringstream returnValue;
156 returnValue << point[0] << ";" << point[1] << ";" << point[2];
157 return returnValue.str();
158 }
159 
161 {
162 std::stringstream returnValue;
163 returnValue << quat.x() << ";" << quat.y() << ";" << quat.z() << ";" << quat.r();
164 return returnValue.str();
165 }
std::string ConvertPointToString(mitk::Point3D point)
Base class of all tools used by mitk::ToolManager.
Definition: mitkTool.h:86
#define MITK_ERROR
Definition: mitkLogMacros.h:20
mitk::DataNode::Pointer ConvertToDataNode(mitk::NavigationTool::Pointer Tool)
std::string ConvertQuaternionToString(mitk::Quaternion quat)
static Pointer New()
std::string ConvertPointSetToString(mitk::PointSet::Pointer pointSet)
std::string GetFileWithoutPath(std::string FileWithPath)
#define MITK_WARN
Definition: mitkLogMacros.h:19
static Pointer New()
vnl_quaternion< ScalarType > Quaternion
static std::string CreateTemporaryDirectory(const std::string &templateName="XXXXXX", std::string path=std::string())
Definition: mitkIOUtil.cpp:458
static Pointer New()
bool DoWrite(std::string FileName, mitk::NavigationTool::Pointer Tool)
Writes a navigation tool to a file.
DataType::PointDataContainerIterator PointDataIterator
Definition: mitkPointSet.h:138
static Pointer New()