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