27 AffineTransform3D::MatrixType matrix = transform->GetMatrix();
28 AffineTransform3D::OffsetType
offset = transform->GetOffset();
35 auto *geomElem =
new TiXmlElement(
"Geometry3D");
36 geomElem->SetAttribute(
"ImageGeometry", isImageGeometry ?
"true" :
"false");
40 auto *matrixElem =
new TiXmlElement(
"IndexToWorld");
41 matrixElem->SetAttribute(
"type",
"Matrix3x3");
42 matrixElem->SetAttribute(
"m_0_0", boost::lexical_cast<std::string>(matrix[0][0]));
43 matrixElem->SetAttribute(
"m_0_1", boost::lexical_cast<std::string>(matrix[0][1]));
44 matrixElem->SetAttribute(
"m_0_2", boost::lexical_cast<std::string>(matrix[0][2]));
45 matrixElem->SetAttribute(
"m_1_0", boost::lexical_cast<std::string>(matrix[1][0]));
46 matrixElem->SetAttribute(
"m_1_1", boost::lexical_cast<std::string>(matrix[1][1]));
47 matrixElem->SetAttribute(
"m_1_2", boost::lexical_cast<std::string>(matrix[1][2]));
48 matrixElem->SetAttribute(
"m_2_0", boost::lexical_cast<std::string>(matrix[2][0]));
49 matrixElem->SetAttribute(
"m_2_1", boost::lexical_cast<std::string>(matrix[2][1]));
50 matrixElem->SetAttribute(
"m_2_2", boost::lexical_cast<std::string>(matrix[2][2]));
51 geomElem->LinkEndChild(matrixElem);
53 auto *offsetElem =
new TiXmlElement(
"Offset");
54 offsetElem->SetAttribute(
"type",
"Vector3D");
55 offsetElem->SetAttribute(
"x", boost::lexical_cast<std::string>(offset[0]));
56 offsetElem->SetAttribute(
"y", boost::lexical_cast<std::string>(offset[1]));
57 offsetElem->SetAttribute(
"z", boost::lexical_cast<std::string>(offset[2]));
58 geomElem->LinkEndChild(offsetElem);
60 auto *boundsElem =
new TiXmlElement(
"Bounds");
61 auto *boundsMinElem =
new TiXmlElement(
"Min");
62 boundsMinElem->SetAttribute(
"type",
"Vector3D");
63 boundsMinElem->SetAttribute(
"x", boost::lexical_cast<std::string>(bounds[0]));
64 boundsMinElem->SetAttribute(
"y", boost::lexical_cast<std::string>(bounds[2]));
65 boundsMinElem->SetAttribute(
"z", boost::lexical_cast<std::string>(bounds[4]));
66 boundsElem->LinkEndChild(boundsMinElem);
67 auto *boundsMaxElem =
new TiXmlElement(
"Max");
68 boundsMaxElem->SetAttribute(
"type",
"Vector3D");
69 boundsMaxElem->SetAttribute(
"x", boost::lexical_cast<std::string>(bounds[1]));
70 boundsMaxElem->SetAttribute(
"y", boost::lexical_cast<std::string>(bounds[3]));
71 boundsMaxElem->SetAttribute(
"z", boost::lexical_cast<std::string>(bounds[5]));
72 boundsElem->LinkEndChild(boundsMaxElem);
73 geomElem->LinkEndChild(boundsElem);
82 MITK_ERROR <<
"Cannot deserialize Geometry3D from nullptr.";
86 AffineTransform3D::MatrixType matrix;
87 AffineTransform3D::OffsetType
offset;
88 bool isImageGeometry(
false);
89 unsigned int frameOfReferenceID(0);
92 if (TIXML_SUCCESS != geometryElement->QueryUnsignedAttribute(
"FrameOfReferenceID", &frameOfReferenceID))
94 MITK_WARN <<
"Missing FrameOfReference for Geometry3D.";
97 if (TIXML_SUCCESS != geometryElement->QueryBoolAttribute(
"ImageGeometry", &isImageGeometry))
99 MITK_WARN <<
"Missing bool ImageGeometry for Geometry3D.";
103 if (TiXmlElement *matrixElem = geometryElement->FirstChildElement(
"IndexToWorld")->ToElement())
105 bool matrixComplete =
true;
106 for (
unsigned int r = 0; r < 3; ++r)
108 for (
unsigned int c = 0; c < 3; ++c)
110 std::stringstream element_namer;
111 element_namer <<
"m_" << r <<
"_" << c;
113 std::string string_value;
114 if (TIXML_SUCCESS == matrixElem->QueryStringAttribute(element_namer.str().c_str(), &string_value))
120 catch (
const boost::bad_lexical_cast &e )
122 MITK_ERROR <<
"Could not parse '" << string_value <<
"' as number: " << e.what();
128 matrixComplete =
false;
135 MITK_ERROR <<
"Could not parse all Geometry3D matrix coefficients!";
141 MITK_ERROR <<
"Parse error: expected Matrix3x3 child below Geometry3D node";
146 if (TiXmlElement *offsetElem = geometryElement->FirstChildElement(
"Offset")->ToElement())
148 bool vectorComplete =
true;
149 std::string offset_string[3];
150 vectorComplete &= TIXML_SUCCESS == offsetElem->QueryStringAttribute(
"x", &offset_string[0]);
151 vectorComplete &= TIXML_SUCCESS == offsetElem->QueryStringAttribute(
"y", &offset_string[1]);
152 vectorComplete &= TIXML_SUCCESS == offsetElem->QueryStringAttribute(
"z", &offset_string[2]);
156 MITK_ERROR <<
"Could not parse complete Geometry3D offset!";
160 for (
unsigned int d = 0; d < 3; ++d)
165 catch (
const boost::bad_lexical_cast &e )
167 MITK_ERROR <<
"Could not parse '" << offset_string[d] <<
"' as number: " << e.what();
173 MITK_ERROR <<
"Parse error: expected Offset3D child below Geometry3D node";
178 if (TiXmlElement *boundsElem = geometryElement->FirstChildElement(
"Bounds")->ToElement())
180 bool vectorsComplete(
true);
181 std::string bounds_string[6];
182 if (TiXmlElement *minElem = boundsElem->FirstChildElement(
"Min")->ToElement())
184 vectorsComplete &= TIXML_SUCCESS == minElem->QueryStringAttribute(
"x", &bounds_string[0]);
185 vectorsComplete &= TIXML_SUCCESS == minElem->QueryStringAttribute(
"y", &bounds_string[2]);
186 vectorsComplete &= TIXML_SUCCESS == minElem->QueryStringAttribute(
"z", &bounds_string[4]);
190 vectorsComplete =
false;
193 if (TiXmlElement *maxElem = boundsElem->FirstChildElement(
"Max")->ToElement())
195 vectorsComplete &= TIXML_SUCCESS == maxElem->QueryStringAttribute(
"x", &bounds_string[1]);
196 vectorsComplete &= TIXML_SUCCESS == maxElem->QueryStringAttribute(
"y", &bounds_string[3]);
197 vectorsComplete &= TIXML_SUCCESS == maxElem->QueryStringAttribute(
"z", &bounds_string[5]);
201 vectorsComplete =
false;
204 if (!vectorsComplete)
206 MITK_ERROR <<
"Could not parse complete Geometry3D bounds!";
210 for (
unsigned int d = 0; d < 6; ++d)
215 catch (
const boost::bad_lexical_cast &e )
217 MITK_ERROR <<
"Could not parse '" << bounds_string[d] <<
"' as number: " << e.what();
223 AffineTransform3D::Pointer newTransform = AffineTransform3D::New();
224 newTransform->SetMatrix(matrix);
225 newTransform->SetOffset(offset);
228 newGeometry->SetFrameOfReferenceID(frameOfReferenceID);
229 newGeometry->SetImageGeometry(isImageGeometry);
231 newGeometry->SetIndexToWorldTransform(newTransform);
233 newGeometry->SetBounds(bounds);
virtual bool GetImageGeometry() const
Is this an ImageGeometry?
Standard implementation of BaseGeometry.
BoundingBoxType::BoundsArrayType BoundsArrayType
static Geometry3D::Pointer FromXML(TiXmlElement *node)
Create a Geometry3D from XML. Interprets only the format created by ToXML().
static TiXmlElement * ToXML(const Geometry3D *geometry)
Serialize given geometry to XML.
Target lexical_cast(const std::string &arg)
itk::AffineGeometryFrame< ScalarType, 3 >::TransformType AffineTransform3D
virtual unsigned int GetFrameOfReferenceID() const
Get the DICOM FrameOfReferenceID referring to the used world coordinate system.
const BoundsArrayType GetBounds() const
mitk::AffineTransform3D * GetIndexToWorldTransform()
Get the transformation used to convert from index to world coordinates.