Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkConnectomicsNetworkWriter.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 
19 #include <tinyxml.h>
20 #include "itksys/SystemTools.hxx"
22 
24  : AbstractFileWriter(mitk::ConnectomicsNetwork::GetStaticNameOfClass(), CustomMimeType( mitk::DiffusionIOMimeTypes::CONNECTOMICS_MIMETYPE() ), mitk::DiffusionIOMimeTypes::CONNECTOMICS_MIMETYPE_DESCRIPTION() )
25 {
27 }
28 
30  : AbstractFileWriter(other)
31 {
32 }
33 
34 
36 {}
37 
39 {
40  return new ConnectomicsNetworkWriter(*this);
41 }
42 
44 {
45  MITK_INFO << "Writing connectomics network";
46  InputType::ConstPointer input = dynamic_cast<const InputType*>(this->GetInput());
47  if (input.IsNull() )
48  {
49  MITK_ERROR <<"Sorry, input to ConnectomicsNetworkWriter is NULL!";
50  return;
51  }
52  if ( this->GetOutputLocation().empty() )
53  {
54  MITK_ERROR << "Sorry, filename has not been set!" ;
55  return ;
56  }
57 
58  std::string ext = itksys::SystemTools::GetFilenameLastExtension(this->GetOutputLocation());
59  ext = itksys::SystemTools::LowerCase(ext);
60 
61  // default extension is .cnf
62  if(ext == "")
63  {
64  ext = ".cnf";
65  this->SetOutputLocation(this->GetOutputLocation() + ext);
66  }
67 
68  if (ext == ".cnf")
69  {
70  // Get geometry of the network
71  mitk::BaseGeometry* geometry = input->GetGeometry();
72 
73  // Create XML document
74  TiXmlDocument documentXML;
75  { // begin document
76  TiXmlDeclaration* declXML = new TiXmlDeclaration( "1.0", "", "" ); // TODO what to write here? encoding? etc....
77  documentXML.LinkEndChild( declXML );
78 
79  TiXmlElement* mainXML = new TiXmlElement(mitk::ConnectomicsNetworkDefinitions::XML_CONNECTOMICS_FILE);
81  documentXML.LinkEndChild(mainXML);
82 
83  TiXmlElement* geometryXML = new TiXmlElement(mitk::ConnectomicsNetworkDefinitions::XML_GEOMETRY);
84  { // begin geometry
85  geometryXML->SetDoubleAttribute(mitk::ConnectomicsNetworkDefinitions::XML_MATRIX_XX, geometry->GetMatrixColumn(0)[0]);
86  geometryXML->SetDoubleAttribute(mitk::ConnectomicsNetworkDefinitions::XML_MATRIX_XY, geometry->GetMatrixColumn(0)[1]);
87  geometryXML->SetDoubleAttribute(mitk::ConnectomicsNetworkDefinitions::XML_MATRIX_XZ, geometry->GetMatrixColumn(0)[2]);
88  geometryXML->SetDoubleAttribute(mitk::ConnectomicsNetworkDefinitions::XML_MATRIX_YX, geometry->GetMatrixColumn(1)[0]);
89  geometryXML->SetDoubleAttribute(mitk::ConnectomicsNetworkDefinitions::XML_MATRIX_YY, geometry->GetMatrixColumn(1)[1]);
90  geometryXML->SetDoubleAttribute(mitk::ConnectomicsNetworkDefinitions::XML_MATRIX_YZ, geometry->GetMatrixColumn(1)[2]);
91  geometryXML->SetDoubleAttribute(mitk::ConnectomicsNetworkDefinitions::XML_MATRIX_ZX, geometry->GetMatrixColumn(2)[0]);
92  geometryXML->SetDoubleAttribute(mitk::ConnectomicsNetworkDefinitions::XML_MATRIX_ZY, geometry->GetMatrixColumn(2)[1]);
93  geometryXML->SetDoubleAttribute(mitk::ConnectomicsNetworkDefinitions::XML_MATRIX_ZZ, geometry->GetMatrixColumn(2)[2]);
94 
95  geometryXML->SetDoubleAttribute(mitk::ConnectomicsNetworkDefinitions::XML_ORIGIN_X, geometry->GetOrigin()[0]);
96  geometryXML->SetDoubleAttribute(mitk::ConnectomicsNetworkDefinitions::XML_ORIGIN_Y, geometry->GetOrigin()[1]);
97  geometryXML->SetDoubleAttribute(mitk::ConnectomicsNetworkDefinitions::XML_ORIGIN_Z, geometry->GetOrigin()[2]);
98 
99  geometryXML->SetDoubleAttribute(mitk::ConnectomicsNetworkDefinitions::XML_SPACING_X, geometry->GetSpacing()[0]);
100  geometryXML->SetDoubleAttribute(mitk::ConnectomicsNetworkDefinitions::XML_SPACING_Y, geometry->GetSpacing()[1]);
101  geometryXML->SetDoubleAttribute(mitk::ConnectomicsNetworkDefinitions::XML_SPACING_Z, geometry->GetSpacing()[2]);
102 
103  } // end geometry
104  mainXML->LinkEndChild(geometryXML);
105 
106  TiXmlElement* verticesXML = new TiXmlElement(mitk::ConnectomicsNetworkDefinitions::XML_VERTICES);
107  { // begin vertices section
108  VertexVectorType vertexVector = dynamic_cast<const InputType*>(this->GetInput())->GetVectorOfAllNodes();
109  for( unsigned int index = 0; index < vertexVector.size(); index++ )
110  {
111  // not localized as of yet TODO
112  TiXmlElement* vertexXML = new TiXmlElement(mitk::ConnectomicsNetworkDefinitions::XML_VERTEX );
113  vertexXML->SetAttribute( mitk::ConnectomicsNetworkDefinitions::XML_VERTEX_ID , vertexVector[ index ].id );
114  vertexXML->SetAttribute( mitk::ConnectomicsNetworkDefinitions::XML_VERTEX_LABEL , vertexVector[ index ].label );
115  vertexXML->SetDoubleAttribute( mitk::ConnectomicsNetworkDefinitions::XML_VERTEX_X , vertexVector[ index ].coordinates[0] );
116  vertexXML->SetDoubleAttribute( mitk::ConnectomicsNetworkDefinitions::XML_VERTEX_Y , vertexVector[ index ].coordinates[1] );
117  vertexXML->SetDoubleAttribute( mitk::ConnectomicsNetworkDefinitions::XML_VERTEX_Z , vertexVector[ index ].coordinates[2] );
118  verticesXML->LinkEndChild(vertexXML);
119  }
120  } // end vertices section
121  mainXML->LinkEndChild(verticesXML);
122 
123  TiXmlElement* edgesXML = new TiXmlElement(mitk::ConnectomicsNetworkDefinitions::XML_EDGES);
124  { // begin edges section
125  EdgeVectorType edgeVector = dynamic_cast<const InputType*>(this->GetInput())->GetVectorOfAllEdges();
126  for(unsigned int index = 0; index < edgeVector.size(); index++ )
127  {
128  TiXmlElement* edgeXML = new TiXmlElement(mitk::ConnectomicsNetworkDefinitions::XML_EDGE );
129  edgeXML->SetAttribute( mitk::ConnectomicsNetworkDefinitions::XML_EDGE_ID , index );
130  edgeXML->SetAttribute( mitk::ConnectomicsNetworkDefinitions::XML_EDGE_SOURCE_ID , edgeVector[ index ].second.sourceId );
131  edgeXML->SetAttribute( mitk::ConnectomicsNetworkDefinitions::XML_EDGE_TARGET_ID , edgeVector[ index ].second.targetId );
132  edgeXML->SetAttribute( mitk::ConnectomicsNetworkDefinitions::XML_EDGE_WEIGHT_ID , edgeVector[ index ].second.weight );
133  edgeXML->SetDoubleAttribute( mitk::ConnectomicsNetworkDefinitions::XML_EDGE_DOUBLE_WEIGHT_ID , edgeVector[ index ].second.edge_weight );
134  edgesXML->LinkEndChild(edgeXML);
135  }
136  } // end edges section
137  mainXML->LinkEndChild(edgesXML);
138 
139  } // end document
140  documentXML.SaveFile( this->GetOutputLocation().c_str() );
141  MITK_INFO << "Connectomics network written";
142 
143  }
144 }
const Point3D GetOrigin() const
Get the origin, e.g. the upper-left corner of the plane.
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
DataCollection - Class to facilitate loading/accessing structured data.
const mitk::Vector3D GetSpacing() const
Get the spacing (size of a pixel).
VnlVector GetMatrixColumn(unsigned int direction) const
Get a VnlVector along bounding-box in the specified direction, length is spacing. ...
virtual void Write() override
Write the base data to the specified location or output stream.
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
std::vector< std::pair< std::pair< mitk::ConnectomicsNetwork::NetworkNode, mitk::ConnectomicsNetwork::NetworkNode >, mitk::ConnectomicsNetwork::NetworkEdge > > EdgeVectorType
static const char * GetStaticNameOfClass()
us::ServiceRegistration< IFileWriter > RegisterService(us::ModuleContext *context=us::GetModuleContext())
Base class for writing mitk::BaseData objects to files or streams.
Connectomics Network Class.
std::vector< mitk::ConnectomicsNetwork::NetworkNode > VertexVectorType
virtual mitk::ConnectomicsNetworkWriter * Clone() const override
BaseGeometry Describes the geometry of a data object.