Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkTubeGraphIO.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 #include "mitkTubeGraphIO.h"
14 
17 #include "mitkTubeGraphProperty.h"
18 
19 #include <mitkIOMimeTypes.h>
20 
21 #include <tinyxml.h>
22 
23 #include <vtkMatrix4x4.h>
24 
25 #include <itksys/SystemTools.hxx>
26 
27 namespace mitk
28 {
29  TubeGraphIO::TubeGraphIO(const TubeGraphIO &other) : AbstractFileIO(other) {}
30  TubeGraphIO::TubeGraphIO()
32  mitk::TubeGraph::GetStaticNameOfClass(), mitk::TubeGraphIO::TUBEGRAPH_MIMETYPE(), "Tube Graph Structure File")
33  {
34  this->RegisterService();
35  }
36 
37  std::vector<BaseData::Pointer> TubeGraphIO::Read()
38  {
39  std::locale::global(std::locale("C"));
40 
41  std::vector<itk::SmartPointer<mitk::BaseData>> result;
42 
43  InputStream stream(this);
44 
45  TiXmlDocument doc;
46  stream >> doc;
47  if (!doc.Error())
48  {
49  TubeGraph::Pointer newTubeGraph = TubeGraph::New();
50 
51  TiXmlHandle hDoc(&doc);
52  TiXmlElement *pElem;
53  TiXmlHandle hRoot(nullptr);
54 
55  pElem = hDoc.FirstChildElement().Element();
56 
57  // save this for later
58  hRoot = TiXmlHandle(pElem);
59 
60  pElem = hRoot.FirstChildElement(mitk::TubeGraphDefinitions::XML_GEOMETRY).Element();
61 
62  // read geometry
64  geometry->Initialize();
65 
66  // read origin
67  mitk::Point3D origin;
68  double temp = 0;
69  pElem->Attribute(mitk::TubeGraphDefinitions::XML_ORIGIN_X, &temp);
70  origin[0] = temp;
71  pElem->Attribute(mitk::TubeGraphDefinitions::XML_ORIGIN_Y, &temp);
72  origin[1] = temp;
73  pElem->Attribute(mitk::TubeGraphDefinitions::XML_ORIGIN_Z, &temp);
74  origin[2] = temp;
75  geometry->SetOrigin(origin);
76 
77  // read spacing
78  Vector3D spacing;
79  pElem->Attribute(mitk::TubeGraphDefinitions::XML_SPACING_X, &temp);
80  spacing.SetElement(0, temp);
81  pElem->Attribute(mitk::TubeGraphDefinitions::XML_SPACING_Y, &temp);
82  spacing.SetElement(1, temp);
83  pElem->Attribute(mitk::TubeGraphDefinitions::XML_SPACING_Z, &temp);
84  spacing.SetElement(2, temp);
85  geometry->SetSpacing(spacing);
86 
87  // read transform
88  vtkMatrix4x4 *m = vtkMatrix4x4::New();
89  pElem->Attribute(mitk::TubeGraphDefinitions::XML_MATRIX_XX, &temp);
90  m->SetElement(0, 0, temp);
91  pElem->Attribute(mitk::TubeGraphDefinitions::XML_MATRIX_XY, &temp);
92  m->SetElement(1, 0, temp);
93  pElem->Attribute(mitk::TubeGraphDefinitions::XML_MATRIX_XZ, &temp);
94  m->SetElement(2, 0, temp);
95  pElem->Attribute(mitk::TubeGraphDefinitions::XML_MATRIX_YX, &temp);
96  m->SetElement(0, 1, temp);
97  pElem->Attribute(mitk::TubeGraphDefinitions::XML_MATRIX_YY, &temp);
98  m->SetElement(1, 1, temp);
99  pElem->Attribute(mitk::TubeGraphDefinitions::XML_MATRIX_YZ, &temp);
100  m->SetElement(2, 1, temp);
101  pElem->Attribute(mitk::TubeGraphDefinitions::XML_MATRIX_ZX, &temp);
102  m->SetElement(0, 2, temp);
103  pElem->Attribute(mitk::TubeGraphDefinitions::XML_MATRIX_ZY, &temp);
104  m->SetElement(1, 2, temp);
105  pElem->Attribute(mitk::TubeGraphDefinitions::XML_MATRIX_ZZ, &temp);
106  m->SetElement(2, 2, temp);
107 
108  m->SetElement(0, 3, origin[0]);
109  m->SetElement(1, 3, origin[1]);
110  m->SetElement(2, 3, origin[2]);
111  m->SetElement(3, 3, 1);
112  geometry->SetIndexToWorldTransformByVtkMatrix(m);
113 
114  geometry->SetImageGeometry(false);
115 
116  // read tube graph
117 
118  // read vertices
119  pElem = hRoot.FirstChildElement(mitk::TubeGraphDefinitions::XML_VERTICES).Element();
120  if (pElem != nullptr)
121  {
122  // walk through the vertices
123  for (TiXmlElement *vertexElement = pElem->FirstChildElement(); vertexElement != nullptr; vertexElement = vertexElement->NextSiblingElement())
124  {
125  int vertexID(0);
126  mitk::Point3D coordinate;
127  coordinate.Fill(0.0);
128  double diameter(0);
129 
130  vertexElement->Attribute(mitk::TubeGraphDefinitions::XML_VERTEX_ID, &vertexID);
131 
132  TiXmlElement *tubeElement = vertexElement->FirstChildElement();
133 
134  tubeElement->Attribute(mitk::TubeGraphDefinitions::XML_ELEMENT_X, &temp);
135  coordinate[0] = temp;
136  tubeElement->Attribute(mitk::TubeGraphDefinitions::XML_ELEMENT_Y, &temp);
137  coordinate[1] = temp;
138  tubeElement->Attribute(mitk::TubeGraphDefinitions::XML_ELEMENT_Z, &temp);
139  coordinate[2] = temp;
140  tubeElement->Attribute(mitk::TubeGraphDefinitions::XML_ELEMENT_DIAMETER, &diameter);
141 
142  mitk::TubeGraphVertex vertexData;
143  auto *newElement = new mitk::CircularProfileTubeElement(coordinate, diameter);
144  vertexData.SetTubeElement(newElement);
145 
146  mitk::TubeGraph::VertexDescriptorType newVertex = newTubeGraph->AddVertex(vertexData);
147  if (static_cast<int>(newVertex) != vertexID)
148  {
149  MITK_ERROR << "Aborting tube graph creation, different vertex ids.";
150  return result;
151  ;
152  }
153  }
154  }
155 
156  // read edges
157  pElem = hRoot.FirstChildElement(mitk::TubeGraphDefinitions::XML_EDGES).Element();
158  if (pElem != nullptr)
159  {
160  // walk through the edges
161  auto edgeElement = pElem->FirstChildElement();
162  for ( ; edgeElement != nullptr; edgeElement = edgeElement->NextSiblingElement())
163  {
164  int edgeID(0), edgeSourceID(0), edgeTargetID(0);
165  mitk::Point3D coordinate;
166  double diameter(0);
167 
168  edgeElement->Attribute(mitk::TubeGraphDefinitions::XML_EDGE_ID, &edgeID);
169  edgeElement->Attribute(mitk::TubeGraphDefinitions::XML_EDGE_SOURCE_ID, &edgeSourceID);
170  edgeElement->Attribute(mitk::TubeGraphDefinitions::XML_EDGE_TARGET_ID, &edgeTargetID);
171 
172  mitk::TubeGraphEdge edgeData;
173 
174  for (TiXmlElement *tubeElement = edgeElement->FirstChildElement(mitk::TubeGraphDefinitions::XML_ELEMENT); tubeElement != nullptr; tubeElement = tubeElement->NextSiblingElement())
175  {
176  tubeElement->Attribute(mitk::TubeGraphDefinitions::XML_ELEMENT_X, &temp);
177  coordinate[0] = temp;
178  tubeElement->Attribute(mitk::TubeGraphDefinitions::XML_ELEMENT_Y, &temp);
179  coordinate[1] = temp;
180  tubeElement->Attribute(mitk::TubeGraphDefinitions::XML_ELEMENT_Z, &temp);
181  coordinate[2] = temp;
182  tubeElement->Attribute(mitk::TubeGraphDefinitions::XML_ELEMENT_DIAMETER, &diameter);
183 
184  auto *newElement = new mitk::CircularProfileTubeElement(coordinate, diameter);
185 
186  edgeData.AddTubeElement(newElement);
187  }
188  try
189  {
190  newTubeGraph->AddEdge(edgeSourceID, edgeTargetID, edgeData);
191  }
192  catch (const std::runtime_error &error)
193  {
194  MITK_ERROR << error.what();
195  return result;
196  }
197  }
198  }
199 
200  // Compute bounding box
201  BoundingBox::Pointer bb = this->ComputeBoundingBox(newTubeGraph);
202  geometry->SetBounds(bb->GetBounds());
203 
204  MITK_INFO << "Tube Graph read";
205  MITK_INFO << "Edge numb:" << newTubeGraph->GetNumberOfEdges()
206  << " Vertices: " << newTubeGraph->GetNumberOfVertices();
207 
208  MITK_INFO << "Reading tube graph property";
210 
211  // read label groups
212  pElem = hRoot.FirstChildElement(mitk::TubeGraphDefinitions::XML_LABELGROUPS).Element();
213  if (pElem != nullptr)
214  {
215  // walk through the label groups
216  for (TiXmlElement *labelGroupElement = pElem->FirstChildElement(); labelGroupElement != nullptr; labelGroupElement = labelGroupElement->NextSiblingElement())
217  {
218  auto *newLabelGroup = new mitk::TubeGraphProperty::LabelGroup();
219  const char *labelGroupName;
220 
221  labelGroupName =
222  labelGroupElement->Attribute((char *)mitk::TubeGraphDefinitions::XML_LABELGROUP_NAME.c_str());
223  if (labelGroupName)
224  newLabelGroup->labelGroupName = labelGroupName;
225 
226  for (TiXmlElement *labelElement = labelGroupElement->FirstChildElement(mitk::TubeGraphDefinitions::XML_LABEL); labelElement != nullptr; labelElement = labelElement->NextSiblingElement())
227  {
228  auto *newLabel = new mitk::TubeGraphProperty::LabelGroup::Label();
229  const char *labelName;
230  bool isVisible = true;
231  Color color;
232 
233  labelName = labelElement->Attribute((char *)mitk::TubeGraphDefinitions::XML_LABEL_NAME.c_str());
234  if (labelName)
235  newLabel->labelName = labelName;
236 
237  labelElement->Attribute(mitk::TubeGraphDefinitions::XML_LABEL_VISIBILITY, &temp);
238  if (temp == 0)
239  isVisible = false;
240  else
241  isVisible = true;
242  labelElement->Attribute(mitk::TubeGraphDefinitions::XML_LABEL_COLOR_R, &temp);
243  color[0] = temp;
244  labelElement->Attribute(mitk::TubeGraphDefinitions::XML_LABEL_COLOR_G, &temp);
245  color[1] = temp;
246  labelElement->Attribute(mitk::TubeGraphDefinitions::XML_LABEL_COLOR_B, &temp);
247  color[2] = temp;
248 
249  newLabel->isVisible = isVisible;
250  newLabel->labelColor = color;
251  newLabelGroup->labels.push_back(newLabel);
252  }
253  newProperty->AddLabelGroup(newLabelGroup, newProperty->GetLabelGroups().size());
254  }
255  }
256  // read attributations
257  pElem = hRoot.FirstChildElement(mitk::TubeGraphDefinitions::XML_ATTRIBUTIONS).Element();
258  if (pElem != nullptr)
259  {
260  std::map<TubeGraphProperty::TubeToLabelGroupType, std::string> tubeToLabelsMap;
261  for (TiXmlElement *tubeToLabelElement = pElem->FirstChildElement(); tubeToLabelElement != nullptr; tubeToLabelElement = tubeToLabelElement->NextSiblingElement())
262  {
264  auto *labelGroup = new mitk::TubeGraphProperty::LabelGroup();
265  auto *label = new mitk::TubeGraphProperty::LabelGroup::Label();
266 
267  tubeToLabelElement->Attribute(mitk::TubeGraphDefinitions::XML_TUBE_ID_1, &temp);
268  tube.first = temp;
269  tubeToLabelElement->Attribute(mitk::TubeGraphDefinitions::XML_TUBE_ID_2, &temp);
270  tube.second = temp;
271  const char *labelGroupName =
272  tubeToLabelElement->Attribute((char *)mitk::TubeGraphDefinitions::XML_LABELGROUP_NAME.c_str());
273  if (labelGroupName)
274  labelGroup = newProperty->GetLabelGroupByName(labelGroupName);
275 
276  const char *labelName =
277  tubeToLabelElement->Attribute((char *)mitk::TubeGraphDefinitions::XML_LABEL_NAME.c_str());
278  if (labelName)
279  label = newProperty->GetLabelByName(labelGroup, labelName);
280 
281  if (tube != TubeGraph::ErrorId && labelGroup != nullptr && label != nullptr)
282  {
283  TubeGraphProperty::TubeToLabelGroupType tubeToLabelGroup(tube, labelGroupName);
284  tubeToLabelsMap.insert(
285  std::pair<TubeGraphProperty::TubeToLabelGroupType, std::string>(tubeToLabelGroup, labelName));
286  }
287  }
288  if (tubeToLabelsMap.size() > 0)
289  newProperty->SetTubesToLabels(tubeToLabelsMap);
290  }
291  // read annotations
292  pElem = hRoot.FirstChildElement(mitk::TubeGraphDefinitions::XML_ANNOTATIONS).Element();
293  if (pElem != nullptr)
294  {
295  for (TiXmlElement *annotationElement = pElem->FirstChildElement(); annotationElement != nullptr; annotationElement = annotationElement->NextSiblingElement())
296  {
297  auto *annotation = new mitk::TubeGraphProperty::Annotation();
298  std::string annotationName;
299  std::string annotationDescription;
301 
302  annotationName =
303  annotationElement->Attribute((char *)mitk::TubeGraphDefinitions::XML_ANNOTATION_NAME.c_str());
304  annotation->name = annotationName;
305 
306  annotationDescription =
307  annotationElement->Attribute((char *)mitk::TubeGraphDefinitions::XML_ANNOTATION_DESCRIPTION.c_str());
308  annotation->description = annotationDescription;
309 
310  annotationElement->Attribute(mitk::TubeGraphDefinitions::XML_TUBE_ID_1, &temp);
311  tube.first = temp;
312  annotationElement->Attribute(mitk::TubeGraphDefinitions::XML_TUBE_ID_2, &temp);
313  tube.second = temp;
314 
315  if (tube != TubeGraph::ErrorId)
316  {
317  annotation->tube = tube;
318  newProperty->AddAnnotation(annotation);
319  }
320  }
321  }
322 
323  MITK_INFO << "Tube Graph Property read";
324 
325  newTubeGraph->SetGeometry(geometry);
326  newTubeGraph->SetProperty("Tube Graph.Visualization Information", newProperty);
327  result.push_back(newTubeGraph.GetPointer());
328  }
329 
330  else
331  {
332  mitkThrow() << "Parsing error at line " << doc.ErrorRow() << ", col " << doc.ErrorCol() << ": "
333  << doc.ErrorDesc();
334  }
335  return result;
336  }
337 
339  {
341  return Unsupported;
342  return Supported;
343  }
344 
346  {
347  OutputStream out(this);
348 
349  if (!out.good())
350  {
351  mitkThrow() << "Stream not good.";
352  }
353 
354  std::locale previousLocale(out.getloc());
355  std::locale I("C");
356  out.imbue(I);
357 
358  const auto *tubeGraph = dynamic_cast<const mitk::TubeGraph *>(this->GetInput());
359  // Get geometry of the tube graph
360  mitk::Geometry3D::Pointer geometry = dynamic_cast<mitk::Geometry3D *>(tubeGraph->GetGeometry());
361  // Get property of the tube graph
362  mitk::TubeGraphProperty::Pointer tubeGraphProperty = dynamic_cast<mitk::TubeGraphProperty *>(
363  tubeGraph->GetProperty("Tube Graph.Visualization Information").GetPointer());
364  // Create XML document
365  TiXmlDocument documentXML;
366  { // Begin document
367  auto *declXML = new TiXmlDeclaration("1.0", "", "");
368  documentXML.LinkEndChild(declXML);
369 
370  auto *mainXML = new TiXmlElement(mitk::TubeGraphDefinitions::XML_TUBEGRAPH_FILE);
372  documentXML.LinkEndChild(mainXML);
373 
374  auto *geometryXML = new TiXmlElement(mitk::TubeGraphDefinitions::XML_GEOMETRY);
375  { // begin geometry
376  geometryXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_MATRIX_XX, geometry->GetMatrixColumn(0)[0]);
377  geometryXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_MATRIX_XY, geometry->GetMatrixColumn(0)[1]);
378  geometryXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_MATRIX_XZ, geometry->GetMatrixColumn(0)[2]);
379  geometryXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_MATRIX_YX, geometry->GetMatrixColumn(1)[0]);
380  geometryXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_MATRIX_YY, geometry->GetMatrixColumn(1)[1]);
381  geometryXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_MATRIX_YZ, geometry->GetMatrixColumn(1)[2]);
382  geometryXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_MATRIX_ZX, geometry->GetMatrixColumn(2)[0]);
383  geometryXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_MATRIX_ZY, geometry->GetMatrixColumn(2)[1]);
384  geometryXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_MATRIX_ZZ, geometry->GetMatrixColumn(2)[2]);
385 
386  geometryXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_ORIGIN_X, geometry->GetOrigin()[0]);
387  geometryXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_ORIGIN_Y, geometry->GetOrigin()[1]);
388  geometryXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_ORIGIN_Z, geometry->GetOrigin()[2]);
389 
390  geometryXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_SPACING_X, geometry->GetSpacing()[0]);
391  geometryXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_SPACING_Y, geometry->GetSpacing()[1]);
392  geometryXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_SPACING_Z, geometry->GetSpacing()[2]);
393 
394  } // end geometry
395  mainXML->LinkEndChild(geometryXML);
396 
397  auto *verticesXML = new TiXmlElement(mitk::TubeGraphDefinitions::XML_VERTICES);
398  { // begin vertices section
399  std::vector<mitk::TubeGraphVertex> vertexVector = tubeGraph->GetVectorOfAllVertices();
400  for (unsigned int index = 0; index < vertexVector.size(); index++)
401  {
402  auto *vertexXML = new TiXmlElement(mitk::TubeGraphDefinitions::XML_VERTEX);
403  vertexXML->SetAttribute(mitk::TubeGraphDefinitions::XML_VERTEX_ID,
404  tubeGraph->GetVertexDescriptor(vertexVector[index]));
405  // element of each vertex
406  const mitk::TubeElement *element = vertexVector[index].GetTubeElement();
407  auto *elementXML = new TiXmlElement(mitk::TubeGraphDefinitions::XML_ELEMENT);
408  elementXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_ELEMENT_X,
409  element->GetCoordinates().GetElement(0));
410  elementXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_ELEMENT_Y,
411  element->GetCoordinates().GetElement(1));
412  elementXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_ELEMENT_Z,
413  element->GetCoordinates().GetElement(2));
414  if (dynamic_cast<const mitk::CircularProfileTubeElement *>(element))
415  elementXML->SetDoubleAttribute(
417  (dynamic_cast<const mitk::CircularProfileTubeElement *>(element))->GetDiameter());
418  else
419  elementXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_ELEMENT_DIAMETER, 2);
420 
421  vertexXML->LinkEndChild(elementXML);
422 
423  verticesXML->LinkEndChild(vertexXML);
424  }
425  } // end vertices section
426  mainXML->LinkEndChild(verticesXML);
427 
428  auto *edgesXML = new TiXmlElement(mitk::TubeGraphDefinitions::XML_EDGES);
429  { // begin edges section
430  std::vector<mitk::TubeGraphEdge> edgeVector = tubeGraph->GetVectorOfAllEdges();
431  for (unsigned int index = 0; index < edgeVector.size(); index++)
432  {
433  auto *edgeXML = new TiXmlElement(mitk::TubeGraphDefinitions::XML_EDGE);
434  edgeXML->SetAttribute(mitk::TubeGraphDefinitions::XML_EDGE_ID, index);
435  std::pair<mitk::TubeGraphVertex, mitk::TubeGraphVertex> soureTargetPair =
436  tubeGraph->GetVerticesOfAnEdge(tubeGraph->GetEdgeDescriptor(edgeVector[index]));
438  tubeGraph->GetVertexDescriptor(soureTargetPair.first));
440  tubeGraph->GetVertexDescriptor(soureTargetPair.second));
441 
442  // begin elements of the edge
443  std::vector<mitk::TubeElement *> elementVector = edgeVector[index].GetElementVector();
444  for (unsigned int elementIndex = 0; elementIndex < elementVector.size(); elementIndex++)
445  {
446  auto *elementXML = new TiXmlElement(mitk::TubeGraphDefinitions::XML_ELEMENT);
447  elementXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_ELEMENT_X,
448  elementVector[elementIndex]->GetCoordinates().GetElement(0));
449  elementXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_ELEMENT_Y,
450  elementVector[elementIndex]->GetCoordinates().GetElement(1));
451  elementXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_ELEMENT_Z,
452  elementVector[elementIndex]->GetCoordinates().GetElement(2));
453  if (dynamic_cast<const mitk::CircularProfileTubeElement *>(elementVector[elementIndex]))
454  elementXML->SetDoubleAttribute(
456  (dynamic_cast<const mitk::CircularProfileTubeElement *>(elementVector[elementIndex]))->GetDiameter());
457  else
458  elementXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_ELEMENT_DIAMETER, 2);
459  edgeXML->LinkEndChild(elementXML);
460  // elementsXML->LinkEndChild(elementXML);
461  }
462  edgesXML->LinkEndChild(edgeXML);
463  }
464  } // end edges section
465  mainXML->LinkEndChild(edgesXML);
466 
467  auto *labelGroupsXML = new TiXmlElement(mitk::TubeGraphDefinitions::XML_LABELGROUPS);
468  { // begin label group section
469  std::vector<TubeGraphProperty::LabelGroup *> labelGroupVector = tubeGraphProperty->GetLabelGroups();
470  for (unsigned int index = 0; index < labelGroupVector.size(); index++)
471  {
472  auto *labelGroupXML = new TiXmlElement(mitk::TubeGraphDefinitions::XML_LABELGROUP);
473  labelGroupXML->SetAttribute(mitk::TubeGraphDefinitions::XML_LABELGROUP_NAME,
474  labelGroupVector[index]->labelGroupName);
475  // begin labels of the label group
476  std::vector<TubeGraphProperty::LabelGroup::Label *> labelVector = labelGroupVector[index]->labels;
477  for (unsigned int labelIndex = 0; labelIndex < labelVector.size(); labelIndex++)
478  {
479  auto *labelXML = new TiXmlElement(mitk::TubeGraphDefinitions::XML_LABEL);
480  labelXML->SetAttribute(mitk::TubeGraphDefinitions::XML_LABEL_NAME, labelVector[labelIndex]->labelName);
482  labelVector[labelIndex]->isVisible);
483  labelXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_LABEL_COLOR_R,
484  labelVector[labelIndex]->labelColor[0]);
485  labelXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_LABEL_COLOR_G,
486  labelVector[labelIndex]->labelColor[1]);
487  labelXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_LABEL_COLOR_B,
488  labelVector[labelIndex]->labelColor[2]);
489  labelGroupXML->LinkEndChild(labelXML);
490  }
491  labelGroupsXML->LinkEndChild(labelGroupXML);
492  }
493  } // end labe group section
494  mainXML->LinkEndChild(labelGroupsXML);
495 
496  auto *attributionsXML = new TiXmlElement(mitk::TubeGraphDefinitions::XML_ATTRIBUTIONS);
497  { // begin attributions section
498  std::map<mitk::TubeGraphProperty::TubeToLabelGroupType, std::string> tubeToLabelGroup =
499  tubeGraphProperty->GetTubesToLabels();
500  for (auto it =
501  tubeToLabelGroup.begin();
502  it != tubeToLabelGroup.end();
503  it++)
504  {
505  auto *attributXML = new TiXmlElement(mitk::TubeGraphDefinitions::XML_ATTRIBUTION);
506  attributXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_TUBE_ID_1, it->first.first.first);
507  attributXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_TUBE_ID_2, it->first.first.second);
508  attributXML->SetAttribute(mitk::TubeGraphDefinitions::XML_LABELGROUP_NAME, it->first.second);
509  attributXML->SetAttribute(mitk::TubeGraphDefinitions::XML_LABEL_NAME, it->second);
510  attributionsXML->LinkEndChild(attributXML);
511  }
512 
513  } // end attributions section
514  mainXML->LinkEndChild(attributionsXML);
515 
516  auto *annotationsXML = new TiXmlElement(mitk::TubeGraphDefinitions::XML_ANNOTATIONS);
517  { // begin annotations section
518  std::vector<mitk::TubeGraphProperty::Annotation *> annotations = tubeGraphProperty->GetAnnotations();
519  for (unsigned int index = 0; index < annotations.size(); index++)
520  {
521  auto *annotationXML = new TiXmlElement(mitk::TubeGraphDefinitions::XML_ANNOTATION);
522  annotationXML->SetAttribute(mitk::TubeGraphDefinitions::XML_ANNOTATION_NAME, annotations[index]->name);
523  annotationXML->SetAttribute(mitk::TubeGraphDefinitions::XML_ANNOTATION_DESCRIPTION,
524  annotations[index]->description);
525  annotationXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_TUBE_ID_1, annotations[index]->tube.first);
526  annotationXML->SetDoubleAttribute(mitk::TubeGraphDefinitions::XML_TUBE_ID_2, annotations[index]->tube.second);
527 
528  annotationsXML->LinkEndChild(annotationXML);
529  }
530  } // end annotations section
531  mainXML->LinkEndChild(annotationsXML);
532  } // end document
533 
534  TiXmlPrinter printer;
535  printer.SetStreamPrinting();
536  documentXML.Accept(&printer);
537  out << printer.Str();
538  }
539 
541  {
543  return Unsupported;
544  return Supported;
545  }
546 
547  TubeGraphIO *TubeGraphIO::IOClone() const { return new TubeGraphIO(*this); }
548 }
549 
550 const mitk::BoundingBox::Pointer mitk::TubeGraphIO::ComputeBoundingBox(mitk::TubeGraph::Pointer graph) const
551 {
552  BoundingBox::Pointer boundingBox = BoundingBox::New();
553  BoundingBox::PointIdentifier pointid = 0;
554  BoundingBox::PointsContainer::Pointer pointscontainer = BoundingBox::PointsContainer::New();
555 
556  ScalarType nullpoint[] = {0, 0, 0};
557  BoundingBox::PointType p(nullpoint);
558 
559  // traverse the tree and add each point to the pointscontainer
560 
561  mitk::Point3D pos;
562 
563  std::vector<mitk::TubeGraphVertex> vertexVector = graph->GetVectorOfAllVertices();
564  for (auto vertex = vertexVector.begin(); vertex != vertexVector.end();
565  ++vertex)
566  {
567  pos = vertex->GetTubeElement()->GetCoordinates();
568  p[0] = pos[0];
569  p[1] = pos[1];
570  p[2] = pos[2];
571  pointscontainer->InsertElement(pointid++, p);
572  }
573 
574  std::vector<mitk::TubeGraphEdge> edgeVector = graph->GetVectorOfAllEdges();
575 
576  for (auto edge = edgeVector.begin(); edge != edgeVector.end(); ++edge)
577  {
578  std::vector<mitk::TubeElement *> allElements = edge->GetElementVector();
579  for (unsigned int index = 0; index < edge->GetNumberOfElements(); index++)
580  {
581  pos = allElements[index]->GetCoordinates();
582  p[0] = pos[0];
583  p[1] = pos[1];
584  p[2] = pos[2];
585  pointscontainer->InsertElement(pointid++, p);
586  }
587  }
588 
589  boundingBox->SetPoints(pointscontainer);
590  boundingBox->ComputeBoundingBox();
591 
592  return boundingBox;
593 }
static const std::string XML_ATTRIBUTIONS
static const std::string XML_SPACING_Y
Standard implementation of BaseGeometry.
static const std::string XML_EDGE_SOURCE_ID
static const std::string XML_ANNOTATION_DESCRIPTION
static const std::string XML_VERTEX_ID
static const std::string XML_LABELGROUP
ConfidenceLevel GetWriterConfidenceLevel() const override
static const TubeDescriptorType ErrorId
Definition: mitkTubeGraph.h:61
#define MITK_INFO
Definition: mitkLogMacros.h:18
static const std::string XML_MATRIX_YX
ConfidenceLevel GetReaderConfidenceLevel() const override
static const std::string XML_ELEMENT
#define MITK_ERROR
Definition: mitkLogMacros.h:20
Base Class for Tube Graphs.
Definition: mitkTubeGraph.h:43
static const std::string XML_LABEL
double ScalarType
ConfidenceLevel GetReaderConfidenceLevel() const override
static const std::string XML_EDGE
static const std::string XML_ELEMENT_Y
Base Class for Tube Graph Vertices.
static const std::string XML_SPACING_Z
static const std::string XML_TUBEGRAPH_FILE
void AddTubeElement(TubeElement *element)
reader and writer for xml representations of mitk::TubeGraph
static const std::string XML_VERTEX
DataCollection - Class to facilitate loading/accessing structured data.
Abstract class for elements which describes tubular structur.
static Pointer New()
static const std::string XML_MATRIX_XY
static const std::string XML_ANNOTATION
static Pointer New()
static const std::string XML_FILE_VERSION
static const std::string XML_SPACING_X
static const std::string XML_VERTICES
static const std::string XML_MATRIX_XZ
static const std::string XML_ATTRIBUTION
static Pointer New()
static const std::string VERSION_STRING
static const std::string XML_MATRIX_ZX
static const std::string XML_MATRIX_YY
static const std::string XML_MATRIX_ZZ
ConfidenceLevel
A confidence level describing the confidence of the reader or writer in handling the given data...
Definition: mitkIFileIO.h:45
std::pair< TubeDescriptorType, std::string > TubeToLabelGroupType
static const std::string XML_TUBE_ID_1
std::pair< VertexDescriptorType, VertexDescriptorType > TubeDescriptorType
Definition: mitkTubeGraph.h:49
std::pair< us::ServiceRegistration< IFileReader >, us::ServiceRegistration< IFileWriter > > RegisterService(us::ModuleContext *context=us::GetModuleContext())
Property for tube graphs.
std::vector< BaseData::Pointer > Read() override
Reads a path or stream and creates a list of BaseData objects.
static const std::string XML_LABEL_COLOR_B
#define mitkThrow()
static const std::string XML_LABELGROUP_NAME
const BaseData * GetInput() const override
Get the input data set via SetInput().
static const std::string XML_LABEL_NAME
static const std::string XML_MATRIX_ZY
Class for elements which describes tubular structur with a circular cross section.
static const std::string XML_LABEL_COLOR_R
static const std::string XML_EDGE_ID
static const std::string XML_ORIGIN_X
void Write() override
Write the base data to the specified location or output stream.
Base Class for Tube Graph Edges.
static const std::string XML_EDGE_TARGET_ID
static const std::string XML_GEOMETRY
static const std::string XML_MATRIX_YZ
itk::RGBPixel< float > Color
Color Standard RGB color typedef (float)
static const std::string XML_ELEMENT_Z
static const std::string XML_LABEL_COLOR_G
static const std::string XML_ORIGIN_Z
boost::graph_traits< GraphType >::vertex_descriptor VertexDescriptorType
static const std::string XML_LABELGROUPS
static const std::string XML_TUBE_ID_2
static const std::string XML_ELEMENT_DIAMETER
void SetTubeElement(TubeElement *element)
static const std::string XML_ANNOTATION_NAME
virtual const Point3D & GetCoordinates() const =0
ConfidenceLevel GetWriterConfidenceLevel() const override
static const std::string XML_ANNOTATIONS
static const std::string XML_LABEL_VISIBILITY
Abstract class for implementing a reader and writer.
static const std::string XML_MATRIX_XX
static const std::string XML_EDGES
static const std::string XML_ORIGIN_Y
static const std::string XML_ELEMENT_X