Medical Imaging Interaction Toolkit  2016.11.0
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,
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 // MITK
19 #include <mitkIGTMimeTypes.h>
20 #include <mitkLocaleSwitch.h>
21 
22 // Third Party
23 #include <tinyxml.h>
24 #include <itksys/SystemTools.hxx>
25 #include <fstream>
26 #include <iostream>
27 
29  mitk::IGTMimeTypes::NAVIGATIONDATASETXML_MIMETYPE(),
30  "MITK NavigationDataSet Writer (XML)")
31 {
33 }
34 
36 {
37 }
38 
40 {
41 }
42 
44 {
45  return new NavigationDataSetWriterXML(*this);
46 }
47 
49 {
50  std::ostream* out = GetOutputStream();
51  if (out == nullptr)
52  {
53  out = new std::ofstream( GetOutputLocation().c_str() );
54  }
55  mitk::NavigationDataSet::ConstPointer data = dynamic_cast<const NavigationDataSet*> (this->GetInput());
56 
57  mitk::LocaleSwitch localeSwitch("C");
58 
59  StreamHeader(out, data);
60  StreamData(out, data);
61  StreamFooter(out);
62 
63  // Cleanup
64  out->flush();
65  delete out;
66 }
67 
69 {
70  stream->precision(10);
71 
72  //TODO store date and GMT time
73  //checking if the stream is good
74  if (stream->good())
75  {
76  *stream << "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" << std::endl;
78  // should be a generic version, meaning a member variable, which has the actual version
79  *stream << " " << "<Data ToolCount=\"" << data->GetNumberOfTools() << "\" version=\"1.0\">" << std::endl;
80  }
81 }
82 
84 {
85  // For each time step in the Dataset
86  for (auto it = data->Begin(); it != data->End(); it++)
87  {
88  for (std::size_t toolIndex = 0; toolIndex < it->size(); toolIndex++)
89  {
90  mitk::NavigationData::Pointer nd = it->at(toolIndex);
91  auto elem = new TiXmlElement("ND");
92 
93  elem->SetDoubleAttribute("Time", nd->GetIGTTimeStamp());
94  // elem->SetAttribute("SystemTime", sysTimeStr); // tag for system time
95  elem->SetDoubleAttribute("Tool", toolIndex);
96  elem->SetDoubleAttribute("X", nd->GetPosition()[0]);
97  elem->SetDoubleAttribute("Y", nd->GetPosition()[1]);
98  elem->SetDoubleAttribute("Z", nd->GetPosition()[2]);
99 
100  elem->SetDoubleAttribute("QX", nd->GetOrientation()[0]);
101  elem->SetDoubleAttribute("QY", nd->GetOrientation()[1]);
102  elem->SetDoubleAttribute("QZ", nd->GetOrientation()[2]);
103  elem->SetDoubleAttribute("QR", nd->GetOrientation()[3]);
104 
105  elem->SetDoubleAttribute("C00", nd->GetCovErrorMatrix()[0][0]);
106  elem->SetDoubleAttribute("C01", nd->GetCovErrorMatrix()[0][1]);
107  elem->SetDoubleAttribute("C02", nd->GetCovErrorMatrix()[0][2]);
108  elem->SetDoubleAttribute("C03", nd->GetCovErrorMatrix()[0][3]);
109  elem->SetDoubleAttribute("C04", nd->GetCovErrorMatrix()[0][4]);
110  elem->SetDoubleAttribute("C05", nd->GetCovErrorMatrix()[0][5]);
111  elem->SetDoubleAttribute("C10", nd->GetCovErrorMatrix()[1][0]);
112  elem->SetDoubleAttribute("C11", nd->GetCovErrorMatrix()[1][1]);
113  elem->SetDoubleAttribute("C12", nd->GetCovErrorMatrix()[1][2]);
114  elem->SetDoubleAttribute("C13", nd->GetCovErrorMatrix()[1][3]);
115  elem->SetDoubleAttribute("C14", nd->GetCovErrorMatrix()[1][4]);
116  elem->SetDoubleAttribute("C15", nd->GetCovErrorMatrix()[1][5]);
117 
118  if (nd->IsDataValid())
119  elem->SetAttribute("Valid",1);
120  else
121  elem->SetAttribute("Valid",0);
122 
123  if (nd->GetHasOrientation())
124  elem->SetAttribute("hO",1);
125  else
126  elem->SetAttribute("hO",0);
127 
128  if (nd->GetHasPosition())
129  elem->SetAttribute("hP",1);
130  else
131  elem->SetAttribute("hP",0);
132 
133  *stream << " " << *elem << std::endl;
134 
135  delete elem;
136  }
137  }
138 }
139 
141 {
142  *stream << "</Data>" << std::endl;
143 }
itk::SmartPointer< Self > Pointer
DataCollection - Class to facilitate loading/accessing structured data.
virtual void StreamData(std::ostream *stream, mitk::NavigationDataSet::ConstPointer data)
virtual mitk::NavigationDataSetWriterXML * Clone() const override
Convenience class to temporarily change the current locale.
static const char * GetStaticNameOfClass()
Data structure which stores streams of mitk::NavigationData for multiple tools.
virtual void Write() override
Write the base data to the specified location or output stream.
virtual void StreamFooter(std::ostream *stream)
us::ServiceRegistration< IFileWriter > RegisterService(us::ModuleContext *context=us::GetModuleContext())
Base class for writing mitk::BaseData objects to files or streams.
virtual void StreamHeader(std::ostream *stream, mitk::NavigationDataSet::ConstPointer data)