Medical Imaging Interaction Toolkit  2018.4.99-12ad79a3
Medical Imaging Interaction Toolkit
mitkNavigationDataSetWriterXML.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 // MITK
15 #include <mitkIGTMimeTypes.h>
16 #include <mitkLocaleSwitch.h>
17 
18 // Third Party
19 #include <tinyxml.h>
20 #include <itksys/SystemTools.hxx>
21 #include <fstream>
22 #include <iostream>
23 
25  mitk::IGTMimeTypes::NAVIGATIONDATASETXML_MIMETYPE(),
26  "MITK NavigationDataSet Writer (XML)")
27 {
29 }
30 
32 {
33 }
34 
36 {
37 }
38 
40 {
41  return new NavigationDataSetWriterXML(*this);
42 }
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 
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 
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  auto elem = new TiXmlElement("ND");
88 
89  elem->SetDoubleAttribute("Time", nd->GetIGTTimeStamp());
90  // elem->SetAttribute("SystemTime", sysTimeStr); // tag for system time
91  elem->SetDoubleAttribute("Tool", toolIndex);
92  elem->SetDoubleAttribute("X", nd->GetPosition()[0]);
93  elem->SetDoubleAttribute("Y", nd->GetPosition()[1]);
94  elem->SetDoubleAttribute("Z", nd->GetPosition()[2]);
95 
96  elem->SetDoubleAttribute("QX", nd->GetOrientation()[0]);
97  elem->SetDoubleAttribute("QY", nd->GetOrientation()[1]);
98  elem->SetDoubleAttribute("QZ", nd->GetOrientation()[2]);
99  elem->SetDoubleAttribute("QR", nd->GetOrientation()[3]);
100 
101  elem->SetDoubleAttribute("C00", nd->GetCovErrorMatrix()[0][0]);
102  elem->SetDoubleAttribute("C01", nd->GetCovErrorMatrix()[0][1]);
103  elem->SetDoubleAttribute("C02", nd->GetCovErrorMatrix()[0][2]);
104  elem->SetDoubleAttribute("C03", nd->GetCovErrorMatrix()[0][3]);
105  elem->SetDoubleAttribute("C04", nd->GetCovErrorMatrix()[0][4]);
106  elem->SetDoubleAttribute("C05", nd->GetCovErrorMatrix()[0][5]);
107  elem->SetDoubleAttribute("C10", nd->GetCovErrorMatrix()[1][0]);
108  elem->SetDoubleAttribute("C11", nd->GetCovErrorMatrix()[1][1]);
109  elem->SetDoubleAttribute("C12", nd->GetCovErrorMatrix()[1][2]);
110  elem->SetDoubleAttribute("C13", nd->GetCovErrorMatrix()[1][3]);
111  elem->SetDoubleAttribute("C14", nd->GetCovErrorMatrix()[1][4]);
112  elem->SetDoubleAttribute("C15", nd->GetCovErrorMatrix()[1][5]);
113 
114  if (nd->IsDataValid())
115  elem->SetAttribute("Valid",1);
116  else
117  elem->SetAttribute("Valid",0);
118 
119  if (nd->GetHasOrientation())
120  elem->SetAttribute("hO",1);
121  else
122  elem->SetAttribute("hO",0);
123 
124  if (nd->GetHasPosition())
125  elem->SetAttribute("hP",1);
126  else
127  elem->SetAttribute("hP",0);
128 
129  *stream << " " << *elem << std::endl;
130 
131  delete elem;
132  }
133  }
134 }
135 
137 {
138  *stream << "</Data>" << std::endl;
139 }
DataCollection - Class to facilitate loading/accessing structured data.
virtual void StreamData(std::ostream *stream, mitk::NavigationDataSet::ConstPointer data)
mitk::NavigationDataSetWriterXML * Clone() const override
Convenience class to temporarily change the current locale.
const BaseData * GetInput() const override
Get the input data set via SetInput().
Data structure which stores streams of mitk::NavigationData for multiple tools.
void Write() override
Write the base data to the specified location or output stream.
std::ostream * GetOutputStream() const override
Get the output stream.
virtual void StreamFooter(std::ostream *stream)
us::ServiceRegistration< IFileWriter > RegisterService(us::ModuleContext *context=us::GetModuleContext())
std::string GetOutputLocation() const override
Get the current output location.
Base class for writing mitk::BaseData objects to files or streams.
virtual void StreamHeader(std::ostream *stream, mitk::NavigationDataSet::ConstPointer data)