MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkNavigationDataSetWriterXML.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// MITK
15#include <mitkIGTMimeTypes.h>
16#include <mitkLocaleSwitch.h>
17
18// Third Party
19#include <tinyxml2.h>
20#include <itksys/SystemTools.hxx>
21#include <fstream>
22#include <iostream>
23
25 mitk::IGTMimeTypes::NAVIGATIONDATASETXML_MIMETYPE(),
26 "MITK NavigationDataSet Writer (XML)")
27{
28 RegisterService();
29}
30
34
38
43
45{
46 std::ostream* out = GetOutputStream();
47 if (out == nullptr)
48 {
49 out = new std::ofstream( GetOutputLocation().c_str() );
50 }
51 mitk::NavigationDataSet::ConstPointer data = dynamic_cast<const NavigationDataSet*> (this->GetInput());
52
53 mitk::LocaleSwitch localeSwitch("C");
54
55 StreamHeader(out, data);
56 StreamData(out, data);
57 StreamFooter(out);
58
59 // Cleanup
60 out->flush();
61 delete out;
62}
63
64void mitk::NavigationDataSetWriterXML::StreamHeader (std::ostream* stream, mitk::NavigationDataSet::ConstPointer data)
65{
66 stream->precision(10);
67
68 //TODO store date and GMT time
69 //checking if the stream is good
70 if (stream->good())
71 {
72 *stream << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" << std::endl;
74 // should be a generic version, meaning a member variable, which has the actual version
75 *stream << " " << "<Data ToolCount=\"" << data->GetNumberOfTools() << "\" version=\"1.0\">" << std::endl;
76 }
77}
78
79void mitk::NavigationDataSetWriterXML::StreamData (std::ostream* stream, mitk::NavigationDataSet::ConstPointer data)
80{
81 // For each time step in the Dataset
82 for (auto it = data->Begin(); it != data->End(); it++)
83 {
84 for (std::size_t toolIndex = 0; toolIndex < it->size(); toolIndex++)
85 {
86 mitk::NavigationData::Pointer nd = it->at(toolIndex);
87 tinyxml2::XMLDocument doc;
88 auto *elem = doc.NewElement("ND");
89
90 elem->SetAttribute("Time", nd->GetIGTTimeStamp());
91 // elem->SetAttribute("SystemTime", sysTimeStr); // tag for system time
92 elem->SetAttribute("Tool", static_cast<int>(toolIndex));
93 elem->SetAttribute("X", nd->GetPosition()[0]);
94 elem->SetAttribute("Y", nd->GetPosition()[1]);
95 elem->SetAttribute("Z", nd->GetPosition()[2]);
96
97 elem->SetAttribute("QX", nd->GetOrientation()[0]);
98 elem->SetAttribute("QY", nd->GetOrientation()[1]);
99 elem->SetAttribute("QZ", nd->GetOrientation()[2]);
100 elem->SetAttribute("QR", nd->GetOrientation()[3]);
101
102 elem->SetAttribute("C00", nd->GetCovErrorMatrix()[0][0]);
103 elem->SetAttribute("C01", nd->GetCovErrorMatrix()[0][1]);
104 elem->SetAttribute("C02", nd->GetCovErrorMatrix()[0][2]);
105 elem->SetAttribute("C03", nd->GetCovErrorMatrix()[0][3]);
106 elem->SetAttribute("C04", nd->GetCovErrorMatrix()[0][4]);
107 elem->SetAttribute("C05", nd->GetCovErrorMatrix()[0][5]);
108 elem->SetAttribute("C10", nd->GetCovErrorMatrix()[1][0]);
109 elem->SetAttribute("C11", nd->GetCovErrorMatrix()[1][1]);
110 elem->SetAttribute("C12", nd->GetCovErrorMatrix()[1][2]);
111 elem->SetAttribute("C13", nd->GetCovErrorMatrix()[1][3]);
112 elem->SetAttribute("C14", nd->GetCovErrorMatrix()[1][4]);
113 elem->SetAttribute("C15", nd->GetCovErrorMatrix()[1][5]);
114
115 if (nd->IsDataValid())
116 elem->SetAttribute("Valid",1);
117 else
118 elem->SetAttribute("Valid",0);
119
120 if (nd->GetHasOrientation())
121 elem->SetAttribute("hO",1);
122 else
123 elem->SetAttribute("hO",0);
124
125 if (nd->GetHasPosition())
126 elem->SetAttribute("hP",1);
127 else
128 elem->SetAttribute("hP",0);
129
130 doc.InsertFirstChild(elem);
131
132 tinyxml2::XMLPrinter printer;
133 doc.Print(&printer);
134
135 *stream << " " << printer.CStr() << std::endl;
136 }
137 }
138}
139
141{
142 *stream << "</Data>" << std::endl;
143}
mitk::NavigationDataSetWriterXML * Clone() const override
virtual void StreamData(std::ostream *stream, mitk::NavigationDataSet::ConstPointer data)
virtual void StreamHeader(std::ostream *stream, mitk::NavigationDataSet::ConstPointer data)
virtual void StreamFooter(std::ostream *stream)
Data structure which stores streams of mitk::NavigationData for multiple tools.
IGT Exceptions.