Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkConnectomicsNetworkReader.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"
21 #include <vtkMatrix4x4.h>
22 #include "mitkGeometry3D.h"
23 #include <mitkCustomMimeType.h>
25 
26 namespace mitk
27 {
28 
30  : mitk::AbstractFileReader(other)
31  {
32  }
33 
35  : mitk::AbstractFileReader( CustomMimeType( mitk::DiffusionIOMimeTypes::CONNECTOMICS_MIMETYPE() ), mitk::DiffusionIOMimeTypes::CONNECTOMICS_MIMETYPE_DESCRIPTION() )
36  {
37  m_ServiceReg = this->RegisterService();
38  }
39 
41  {
42  }
43 
44  std::vector<itk::SmartPointer<BaseData> > ConnectomicsNetworkReader::Read()
45  {
46  std::vector<itk::SmartPointer<mitk::BaseData> > result;
47  std::string location = GetInputLocation();
48 
49  std::string ext = itksys::SystemTools::GetFilenameLastExtension(location);
50  ext = itksys::SystemTools::LowerCase(ext);
51 
52  if ( location == "")
53  {
54  MITK_ERROR << "No file name specified.";
55  }
56  else if (ext == ".cnf")
57  {
58  try
59  {
61 
62  TiXmlDocument doc( location );
63  bool loadOkay = doc.LoadFile();
64  if(!loadOkay)
65  {
66  mitkThrow() << "Could not open file " << location << " for reading.";
67  }
68 
69  TiXmlHandle hDoc(&doc);
70  TiXmlElement* pElem;
71  TiXmlHandle hRoot(0);
72 
73  pElem = hDoc.FirstChildElement().Element();
74 
75  // save this for later
76  hRoot = TiXmlHandle(pElem);
77 
78  //get file version
79  std::string version("");
80  hRoot.Element()->QueryStringAttribute(mitk::ConnectomicsNetworkDefinitions::XML_FILE_VERSION, &version);
81 
82  // read geometry
83  pElem = hRoot.FirstChildElement(mitk::ConnectomicsNetworkDefinitions::XML_GEOMETRY).Element();
84 
86 
87  // read origin
88  mitk::Point3D origin;
89  double temp = 0;
91  origin[0] = temp;
93  origin[1] = temp;
95  origin[2] = temp;
96  geometry->SetOrigin(origin);
97 
98  // read spacing
99  ScalarType spacing[3];
101  spacing[0] = temp;
103  spacing[1] = temp;
105  spacing[2] = temp;
106  geometry->SetSpacing(spacing);
107 
108  // read transform
109  vtkMatrix4x4* m = vtkMatrix4x4::New();
111  m->SetElement(0,0,temp);
113  m->SetElement(1,0,temp);
115  m->SetElement(2,0,temp);
117  m->SetElement(0,1,temp);
119  m->SetElement(1,1,temp);
121  m->SetElement(2,1,temp);
123  m->SetElement(0,2,temp);
125  m->SetElement(1,2,temp);
127  m->SetElement(2,2,temp);
128 
129  m->SetElement(0,3,origin[0]);
130  m->SetElement(1,3,origin[1]);
131  m->SetElement(2,3,origin[2]);
132  m->SetElement(3,3,1);
133  geometry->SetIndexToWorldTransformByVtkMatrix(m);
134 
135  geometry->SetImageGeometry(true);
136  outputNetwork->SetGeometry(geometry);
137 
138  // read network
139  std::map< int, mitk::ConnectomicsNetwork::VertexDescriptorType > idToVertexMap;
140  // read vertices
141  pElem = hRoot.FirstChildElement(mitk::ConnectomicsNetworkDefinitions::XML_VERTICES).Element();
142  {
143  // walk through the vertices
144  TiXmlElement* vertexElement = pElem->FirstChildElement();
145 
146  for( ; vertexElement; vertexElement=vertexElement->NextSiblingElement())
147  {
148  std::vector< float > pos;
149  std::string label;
150  int vertexID(0);
151 
152  vertexElement->Attribute(mitk::ConnectomicsNetworkDefinitions::XML_VERTEX_X, &temp);
153  pos.push_back(temp);
154  vertexElement->Attribute(mitk::ConnectomicsNetworkDefinitions::XML_VERTEX_Y, &temp);
155  pos.push_back(temp);
156  vertexElement->Attribute(mitk::ConnectomicsNetworkDefinitions::XML_VERTEX_Z, &temp);
157  pos.push_back(temp);
158  vertexElement->Attribute(mitk::ConnectomicsNetworkDefinitions::XML_VERTEX_ID, &vertexID);
159  vertexElement->QueryStringAttribute(mitk::ConnectomicsNetworkDefinitions::XML_VERTEX_LABEL, &label);
160 
161  mitk::ConnectomicsNetwork::VertexDescriptorType newVertex = outputNetwork->AddVertex( vertexID );
162  outputNetwork->SetLabel( newVertex, label );
163  outputNetwork->SetCoordinates( newVertex, pos );
164 
165  if ( idToVertexMap.count( vertexID ) > 0 )
166  {
167  MITK_ERROR << "Aborting network creation, duplicate vertex ID in file.";
168  return result;
169  }
170  idToVertexMap.insert( std::pair< int, mitk::ConnectomicsNetwork::VertexDescriptorType >( vertexID, newVertex) );
171  }
172  }
173 
174  // read edges
175  pElem = hRoot.FirstChildElement(mitk::ConnectomicsNetworkDefinitions::XML_EDGES).Element();
176  {
177  // walk through the edges
178  TiXmlElement* edgeElement = pElem->FirstChildElement();
179 
180  for( ; edgeElement; edgeElement=edgeElement->NextSiblingElement())
181  {
182  int edgeID(0), edgeSourceID(0), edgeTargetID(0), edgeWeight(0);
183  double edgeDoubleWeight(0.0);
184 
185  edgeElement->Attribute(mitk::ConnectomicsNetworkDefinitions::XML_EDGE_ID, &edgeID);
186  edgeElement->Attribute(mitk::ConnectomicsNetworkDefinitions::XML_EDGE_SOURCE_ID, &edgeSourceID);
187  edgeElement->Attribute(mitk::ConnectomicsNetworkDefinitions::XML_EDGE_TARGET_ID, &edgeTargetID);
188  edgeElement->Attribute(mitk::ConnectomicsNetworkDefinitions::XML_EDGE_WEIGHT_ID, &edgeWeight);
189  if(version == "0.1")
190  {
191  // in version 0.1 only the int weight was saved, assume double weight to be one by default
192  edgeDoubleWeight = 1.0;
193  }
194  else
195  {
196  edgeElement->QueryDoubleAttribute(mitk::ConnectomicsNetworkDefinitions::XML_EDGE_DOUBLE_WEIGHT_ID, &edgeDoubleWeight);
197  }
198 
199  mitk::ConnectomicsNetwork::VertexDescriptorType source = idToVertexMap.find( edgeSourceID )->second;
200  mitk::ConnectomicsNetwork::VertexDescriptorType target = idToVertexMap.find( edgeTargetID )->second;
201  outputNetwork->AddEdge( source, target, edgeSourceID, edgeTargetID, edgeWeight, edgeDoubleWeight);
202  }
203  }
204 
205  outputNetwork->UpdateBounds();
206  result.push_back(outputNetwork.GetPointer());
207  MITK_INFO << "Network read";
208  }
209  catch (mitk::Exception e)
210  {
211  MITK_ERROR << e.GetDescription();
212  }
213  catch(...)
214  {
215  MITK_ERROR << "Unknown error occured while trying to read file.";
216  }
217  }
218 
219  return result;
220  }
221 
222 
223 } //namespace MITK
224 
225 mitk::ConnectomicsNetworkReader* mitk::ConnectomicsNetworkReader::Clone() const
226 {
227  return new ConnectomicsNetworkReader(*this);
228 }
#define MITK_INFO
Definition: mitkLogMacros.h:22
#define MITK_ERROR
Definition: mitkLogMacros.h:24
double ScalarType
DataCollection - Class to facilitate loading/accessing structured data.
static Pointer New()
static Pointer New()
The reader for connectomics network files (.cnf)
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
us::ServiceRegistration< IFileReader > RegisterService(us::ModuleContext *context=us::GetModuleContext())
An object of this class represents an exception of MITK. Please don't instantiate exceptions manually...
Definition: mitkException.h:49
#define mitkThrow()
boost::graph_traits< NetworkType >::vertex_descriptor VertexDescriptorType
Base class for creating mitk::BaseData objects from files or streams.
virtual std::string GetInputLocation() const override
Get the current input location.
virtual std::vector< itk::SmartPointer< BaseData > > Read() override
Reads a path or stream and creates a list of BaseData objects.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.