MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkNavigationToolWriter.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
13//Poco headers
14#include <Poco/Zip/Compress.h>
15#include <Poco/Path.h>
16
17//mitk headers
19#include <mitkStandaloneDataStorage.h>
20#include <mitkProperties.h>
21#include <mitkSceneIO.h>
22#include <mitkPointSet.h>
23#include <mitkIOUtil.h>
24
25//std headers
26#include <cstdio>
27
32
37
38bool 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!";
44 MITK_ERROR << m_ErrorMessage;
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
64 mitk::StandaloneDataStorage::Pointer saveStorage = mitk::StandaloneDataStorage::New();
65 mitk::DataNode::Pointer thisTool = ConvertToDataNode(Tool);
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 + "'";
78 MITK_ERROR << m_ErrorMessage;
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
98mitk::DataNode::Pointer mitk::NavigationToolWriter::ConvertToDataNode(mitk::NavigationTool::Pointer Tool)
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
135std::string mitk::NavigationToolWriter::GetFileWithoutPath(std::string FileWithPath)
136 {
137 Poco::Path myFile(FileWithPath.c_str());
138 return myFile.getFileName();
139 }
140
141std::string mitk::NavigationToolWriter::ConvertPointSetToString(mitk::PointSet::Pointer pointSet)
142 {
143 std::stringstream returnValue;
144 mitk::PointSet::PointDataIterator it;
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{
155std::stringstream returnValue;
156returnValue << point[0] << ";" << point[1] << ";" << point[2];
157return returnValue.str();
158}
159
161{
162std::stringstream returnValue;
163returnValue << quat.x() << ";" << quat.y() << ";" << quat.z() << ";" << quat.r();
164return returnValue.str();
165}
std::string ConvertPointToString(mitk::Point3D point)
std::string GetFileWithoutPath(std::string FileWithPath)
bool DoWrite(std::string FileName, mitk::NavigationTool::Pointer Tool)
Writes a navigation tool to a file.
mitk::DataNode::Pointer ConvertToDataNode(mitk::NavigationTool::Pointer Tool)
std::string ConvertPointSetToString(mitk::PointSet::Pointer pointSet)
std::string ConvertQuaternionToString(mitk::Quaternion quat)