23 #include <vtkMatrix4x4.h> 25 #include <itksys/SystemTools.hxx> 30 TubeGraphIO::TubeGraphIO()
39 std::locale::global(std::locale(
"C"));
41 std::vector<itk::SmartPointer<mitk::BaseData>> result;
51 TiXmlHandle hDoc(&doc);
53 TiXmlHandle hRoot(
nullptr);
55 pElem = hDoc.FirstChildElement().Element();
58 hRoot = TiXmlHandle(pElem);
64 geometry->Initialize();
75 geometry->SetOrigin(origin);
80 spacing.SetElement(0, temp);
82 spacing.SetElement(1, temp);
84 spacing.SetElement(2, temp);
85 geometry->SetSpacing(spacing);
88 vtkMatrix4x4 *m = vtkMatrix4x4::New();
90 m->SetElement(0, 0, temp);
92 m->SetElement(1, 0, temp);
94 m->SetElement(2, 0, temp);
96 m->SetElement(0, 1, temp);
98 m->SetElement(1, 1, temp);
100 m->SetElement(2, 1, temp);
102 m->SetElement(0, 2, temp);
104 m->SetElement(1, 2, temp);
106 m->SetElement(2, 2, temp);
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);
114 geometry->SetImageGeometry(
false);
120 if (pElem !=
nullptr)
123 for (TiXmlElement *vertexElement = pElem->FirstChildElement(); vertexElement !=
nullptr; vertexElement = vertexElement->NextSiblingElement())
127 coordinate.Fill(0.0);
132 TiXmlElement *tubeElement = vertexElement->FirstChildElement();
135 coordinate[0] = temp;
137 coordinate[1] = temp;
139 coordinate[2] = temp;
147 if (static_cast<int>(newVertex) != vertexID)
149 MITK_ERROR <<
"Aborting tube graph creation, different vertex ids.";
158 if (pElem !=
nullptr)
161 auto edgeElement = pElem->FirstChildElement();
162 for ( ; edgeElement !=
nullptr; edgeElement = edgeElement->NextSiblingElement())
164 int edgeID(0), edgeSourceID(0), edgeTargetID(0);
177 coordinate[0] = temp;
179 coordinate[1] = temp;
181 coordinate[2] = temp;
190 newTubeGraph->AddEdge(edgeSourceID, edgeTargetID, edgeData);
192 catch (
const std::runtime_error &error)
201 BoundingBox::Pointer bb = this->ComputeBoundingBox(newTubeGraph);
202 geometry->SetBounds(bb->GetBounds());
205 MITK_INFO <<
"Edge numb:" << newTubeGraph->GetNumberOfEdges()
206 <<
" Vertices: " << newTubeGraph->GetNumberOfVertices();
208 MITK_INFO <<
"Reading tube graph property";
213 if (pElem !=
nullptr)
216 for (TiXmlElement *labelGroupElement = pElem->FirstChildElement(); labelGroupElement !=
nullptr; labelGroupElement = labelGroupElement->NextSiblingElement())
219 const char *labelGroupName;
224 newLabelGroup->labelGroupName = labelGroupName;
226 for (TiXmlElement *labelElement = labelGroupElement->FirstChildElement(
mitk::TubeGraphDefinitions::XML_LABEL); labelElement !=
nullptr; labelElement = labelElement->NextSiblingElement())
229 const char *labelName;
230 bool isVisible =
true;
235 newLabel->labelName = labelName;
249 newLabel->isVisible = isVisible;
250 newLabel->labelColor = color;
251 newLabelGroup->labels.push_back(newLabel);
253 newProperty->AddLabelGroup(newLabelGroup, newProperty->GetLabelGroups().size());
258 if (pElem !=
nullptr)
260 std::map<TubeGraphProperty::TubeToLabelGroupType, std::string> tubeToLabelsMap;
261 for (TiXmlElement *tubeToLabelElement = pElem->FirstChildElement(); tubeToLabelElement !=
nullptr; tubeToLabelElement = tubeToLabelElement->NextSiblingElement())
271 const char *labelGroupName =
274 labelGroup = newProperty->GetLabelGroupByName(labelGroupName);
276 const char *labelName =
279 label = newProperty->GetLabelByName(labelGroup, labelName);
284 tubeToLabelsMap.insert(
285 std::pair<TubeGraphProperty::TubeToLabelGroupType, std::string>(tubeToLabelGroup, labelName));
288 if (tubeToLabelsMap.size() > 0)
289 newProperty->SetTubesToLabels(tubeToLabelsMap);
293 if (pElem !=
nullptr)
295 for (TiXmlElement *annotationElement = pElem->FirstChildElement(); annotationElement !=
nullptr; annotationElement = annotationElement->NextSiblingElement())
298 std::string annotationName;
299 std::string annotationDescription;
304 annotation->name = annotationName;
306 annotationDescription =
308 annotation->description = annotationDescription;
317 annotation->tube = tube;
318 newProperty->AddAnnotation(annotation);
325 newTubeGraph->SetGeometry(geometry);
326 newTubeGraph->SetProperty(
"Tube Graph.Visualization Information", newProperty);
327 result.push_back(newTubeGraph.GetPointer());
332 mitkThrow() <<
"Parsing error at line " << doc.ErrorRow() <<
", col " << doc.ErrorCol() <<
": " 354 std::locale previousLocale(out.getloc());
363 tubeGraph->GetProperty(
"Tube Graph.Visualization Information").GetPointer());
365 TiXmlDocument documentXML;
367 auto *declXML =
new TiXmlDeclaration(
"1.0",
"",
"");
368 documentXML.LinkEndChild(declXML);
372 documentXML.LinkEndChild(mainXML);
395 mainXML->LinkEndChild(geometryXML);
399 std::vector<mitk::TubeGraphVertex> vertexVector = tubeGraph->GetVectorOfAllVertices();
400 for (
unsigned int index = 0; index < vertexVector.size(); index++)
404 tubeGraph->GetVertexDescriptor(vertexVector[index]));
414 if (dynamic_cast<const mitk::CircularProfileTubeElement *>(element))
415 elementXML->SetDoubleAttribute(
417 (dynamic_cast<const mitk::CircularProfileTubeElement *>(element))->GetDiameter());
421 vertexXML->LinkEndChild(elementXML);
423 verticesXML->LinkEndChild(vertexXML);
426 mainXML->LinkEndChild(verticesXML);
430 std::vector<mitk::TubeGraphEdge> edgeVector = tubeGraph->GetVectorOfAllEdges();
431 for (
unsigned int index = 0; index < edgeVector.size(); 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));
443 std::vector<mitk::TubeElement *> elementVector = edgeVector[index].GetElementVector();
444 for (
unsigned int elementIndex = 0; elementIndex < elementVector.size(); elementIndex++)
448 elementVector[elementIndex]->GetCoordinates().GetElement(0));
450 elementVector[elementIndex]->GetCoordinates().GetElement(1));
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());
459 edgeXML->LinkEndChild(elementXML);
462 edgesXML->LinkEndChild(edgeXML);
465 mainXML->LinkEndChild(edgesXML);
469 std::vector<TubeGraphProperty::LabelGroup *> labelGroupVector = tubeGraphProperty->GetLabelGroups();
470 for (
unsigned int index = 0; index < labelGroupVector.size(); index++)
474 labelGroupVector[index]->labelGroupName);
476 std::vector<TubeGraphProperty::LabelGroup::Label *> labelVector = labelGroupVector[index]->labels;
477 for (
unsigned int labelIndex = 0; labelIndex < labelVector.size(); labelIndex++)
482 labelVector[labelIndex]->isVisible);
484 labelVector[labelIndex]->labelColor[0]);
486 labelVector[labelIndex]->labelColor[1]);
488 labelVector[labelIndex]->labelColor[2]);
489 labelGroupXML->LinkEndChild(labelXML);
491 labelGroupsXML->LinkEndChild(labelGroupXML);
494 mainXML->LinkEndChild(labelGroupsXML);
498 std::map<mitk::TubeGraphProperty::TubeToLabelGroupType, std::string> tubeToLabelGroup =
499 tubeGraphProperty->GetTubesToLabels();
501 tubeToLabelGroup.begin();
502 it != tubeToLabelGroup.end();
510 attributionsXML->LinkEndChild(attributXML);
514 mainXML->LinkEndChild(attributionsXML);
518 std::vector<mitk::TubeGraphProperty::Annotation *> annotations = tubeGraphProperty->GetAnnotations();
519 for (
unsigned int index = 0; index < annotations.size(); index++)
524 annotations[index]->description);
528 annotationsXML->LinkEndChild(annotationXML);
531 mainXML->LinkEndChild(annotationsXML);
534 TiXmlPrinter printer;
535 printer.SetStreamPrinting();
536 documentXML.Accept(&printer);
537 out << printer.Str();
552 BoundingBox::Pointer boundingBox = BoundingBox::New();
553 BoundingBox::PointIdentifier pointid = 0;
554 BoundingBox::PointsContainer::Pointer pointscontainer = BoundingBox::PointsContainer::New();
557 BoundingBox::PointType p(nullpoint);
563 std::vector<mitk::TubeGraphVertex> vertexVector = graph->GetVectorOfAllVertices();
564 for (
auto vertex = vertexVector.begin(); vertex != vertexVector.end();
567 pos = vertex->GetTubeElement()->GetCoordinates();
571 pointscontainer->InsertElement(pointid++, p);
574 std::vector<mitk::TubeGraphEdge> edgeVector = graph->GetVectorOfAllEdges();
576 for (
auto edge = edgeVector.begin(); edge != edgeVector.end(); ++edge)
578 std::vector<mitk::TubeElement *> allElements = edge->GetElementVector();
579 for (
unsigned int index = 0; index < edge->GetNumberOfElements(); index++)
581 pos = allElements[index]->GetCoordinates();
585 pointscontainer->InsertElement(pointid++, p);
589 boundingBox->SetPoints(pointscontainer);
590 boundingBox->ComputeBoundingBox();
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
static const std::string XML_MATRIX_YX
ConfidenceLevel GetReaderConfidenceLevel() const override
static const std::string XML_ELEMENT
Base Class for Tube Graphs.
static const std::string XML_LABEL
ConfidenceLevel GetReaderConfidenceLevel() const override
static const std::string XML_EDGE
static const std::string XML_ELEMENT_Y
An output stream wrapper.
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 const std::string XML_MATRIX_XY
static const std::string XML_ANNOTATION
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 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...
std::pair< TubeDescriptorType, std::string > TubeToLabelGroupType
static const std::string XML_TUBE_ID_1
std::pair< VertexDescriptorType, VertexDescriptorType > TubeDescriptorType
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
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