19 #include <boost/lexical_cast.hpp>
31 AffineTransform3D::MatrixType matrix = transform->GetMatrix();
32 AffineTransform3D::OffsetType
offset = transform->GetOffset();
39 TiXmlElement *geomElem =
new TiXmlElement(
"Geometry3D");
40 geomElem->SetAttribute(
"ImageGeometry", isImageGeometry ?
"true" :
"false");
44 TiXmlElement *matrixElem =
new TiXmlElement(
"IndexToWorld");
45 matrixElem->SetAttribute(
"type",
"Matrix3x3");
46 matrixElem->SetAttribute(
"m_0_0", boost::lexical_cast<std::string>(matrix[0][0]));
47 matrixElem->SetAttribute(
"m_0_1", boost::lexical_cast<std::string>(matrix[0][1]));
48 matrixElem->SetAttribute(
"m_0_2", boost::lexical_cast<std::string>(matrix[0][2]));
49 matrixElem->SetAttribute(
"m_1_0", boost::lexical_cast<std::string>(matrix[1][0]));
50 matrixElem->SetAttribute(
"m_1_1", boost::lexical_cast<std::string>(matrix[1][1]));
51 matrixElem->SetAttribute(
"m_1_2", boost::lexical_cast<std::string>(matrix[1][2]));
52 matrixElem->SetAttribute(
"m_2_0", boost::lexical_cast<std::string>(matrix[2][0]));
53 matrixElem->SetAttribute(
"m_2_1", boost::lexical_cast<std::string>(matrix[2][1]));
54 matrixElem->SetAttribute(
"m_2_2", boost::lexical_cast<std::string>(matrix[2][2]));
55 geomElem->LinkEndChild(matrixElem);
57 TiXmlElement *offsetElem =
new TiXmlElement(
"Offset");
58 offsetElem->SetAttribute(
"type",
"Vector3D");
59 offsetElem->SetAttribute(
"x", boost::lexical_cast<std::string>(offset[0]));
60 offsetElem->SetAttribute(
"y", boost::lexical_cast<std::string>(offset[1]));
61 offsetElem->SetAttribute(
"z", boost::lexical_cast<std::string>(offset[2]));
62 geomElem->LinkEndChild(offsetElem);
64 TiXmlElement *boundsElem =
new TiXmlElement(
"Bounds");
65 TiXmlElement *boundsMinElem =
new TiXmlElement(
"Min");
66 boundsMinElem->SetAttribute(
"type",
"Vector3D");
67 boundsMinElem->SetAttribute(
"x", boost::lexical_cast<std::string>(bounds[0]));
68 boundsMinElem->SetAttribute(
"y", boost::lexical_cast<std::string>(bounds[2]));
69 boundsMinElem->SetAttribute(
"z", boost::lexical_cast<std::string>(bounds[4]));
70 boundsElem->LinkEndChild(boundsMinElem);
71 TiXmlElement *boundsMaxElem =
new TiXmlElement(
"Max");
72 boundsMaxElem->SetAttribute(
"type",
"Vector3D");
73 boundsMaxElem->SetAttribute(
"x", boost::lexical_cast<std::string>(bounds[1]));
74 boundsMaxElem->SetAttribute(
"y", boost::lexical_cast<std::string>(bounds[3]));
75 boundsMaxElem->SetAttribute(
"z", boost::lexical_cast<std::string>(bounds[5]));
76 boundsElem->LinkEndChild(boundsMaxElem);
77 geomElem->LinkEndChild(boundsElem);
86 MITK_ERROR <<
"Cannot deserialize Geometry3D from nullptr.";
90 AffineTransform3D::MatrixType matrix;
91 AffineTransform3D::OffsetType
offset;
92 bool isImageGeometry(
false);
93 unsigned int frameOfReferenceID(0);
96 if (TIXML_SUCCESS != geometryElement->QueryUnsignedAttribute(
"FrameOfReferenceID", &frameOfReferenceID))
98 MITK_WARN <<
"Missing FrameOfReference for Geometry3D.";
101 if (TIXML_SUCCESS != geometryElement->QueryBoolAttribute(
"ImageGeometry", &isImageGeometry))
103 MITK_WARN <<
"Missing bool ImageGeometry for Geometry3D.";
107 if (TiXmlElement *matrixElem = geometryElement->FirstChildElement(
"IndexToWorld")->ToElement())
109 bool matrixComplete =
true;
110 for (
unsigned int r = 0; r < 3; ++r)
112 for (
unsigned int c = 0; c < 3; ++c)
114 std::stringstream element_namer;
115 element_namer <<
"m_" << r <<
"_" << c;
117 std::string string_value;
118 if (TIXML_SUCCESS == matrixElem->QueryStringAttribute(element_namer.str().c_str(), &string_value))
122 matrix[r][c] = boost::lexical_cast<
double>(string_value);
124 catch (boost::bad_lexical_cast &e)
126 MITK_ERROR <<
"Could not parse '" << string_value <<
"' as number: " << e.what();
132 matrixComplete =
false;
139 MITK_ERROR <<
"Could not parse all Geometry3D matrix coefficients!";
145 MITK_ERROR <<
"Parse error: expected Matrix3x3 child below Geometry3D node";
150 if (TiXmlElement *offsetElem = geometryElement->FirstChildElement(
"Offset")->ToElement())
152 bool vectorComplete =
true;
153 std::string offset_string[3];
154 vectorComplete &= TIXML_SUCCESS == offsetElem->QueryStringAttribute(
"x", &offset_string[0]);
155 vectorComplete &= TIXML_SUCCESS == offsetElem->QueryStringAttribute(
"y", &offset_string[1]);
156 vectorComplete &= TIXML_SUCCESS == offsetElem->QueryStringAttribute(
"z", &offset_string[2]);
160 MITK_ERROR <<
"Could not parse complete Geometry3D offset!";
164 for (
unsigned int d = 0; d < 3; ++d)
167 offset[d] = boost::lexical_cast<
double>(offset_string[d]);
169 catch (boost::bad_lexical_cast &e)
171 MITK_ERROR <<
"Could not parse '" << offset_string[d] <<
"' as number: " << e.what();
177 MITK_ERROR <<
"Parse error: expected Offset3D child below Geometry3D node";
182 if (TiXmlElement *boundsElem = geometryElement->FirstChildElement(
"Bounds")->ToElement())
184 bool vectorsComplete(
true);
185 std::string bounds_string[6];
186 if (TiXmlElement *minElem = boundsElem->FirstChildElement(
"Min")->ToElement())
188 vectorsComplete &= TIXML_SUCCESS == minElem->QueryStringAttribute(
"x", &bounds_string[0]);
189 vectorsComplete &= TIXML_SUCCESS == minElem->QueryStringAttribute(
"y", &bounds_string[2]);
190 vectorsComplete &= TIXML_SUCCESS == minElem->QueryStringAttribute(
"z", &bounds_string[4]);
194 vectorsComplete =
false;
197 if (TiXmlElement *maxElem = boundsElem->FirstChildElement(
"Max")->ToElement())
199 vectorsComplete &= TIXML_SUCCESS == maxElem->QueryStringAttribute(
"x", &bounds_string[1]);
200 vectorsComplete &= TIXML_SUCCESS == maxElem->QueryStringAttribute(
"y", &bounds_string[3]);
201 vectorsComplete &= TIXML_SUCCESS == maxElem->QueryStringAttribute(
"z", &bounds_string[5]);
205 vectorsComplete =
false;
208 if (!vectorsComplete)
210 MITK_ERROR <<
"Could not parse complete Geometry3D bounds!";
214 for (
unsigned int d = 0; d < 6; ++d)
217 bounds[d] = boost::lexical_cast<
double>(bounds_string[d]);
219 catch (boost::bad_lexical_cast &e)
221 MITK_ERROR <<
"Could not parse '" << bounds_string[d] <<
"' as number: " << e.what();
228 newTransform->SetMatrix(matrix);
229 newTransform->SetOffset(offset);
232 newGeometry->SetFrameOfReferenceID(frameOfReferenceID);
233 newGeometry->SetImageGeometry(isImageGeometry);
235 newGeometry->SetIndexToWorldTransform(newTransform);
237 newGeometry->SetBounds(bounds);
itk::SmartPointer< Self > Pointer
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().
const BoundsArrayType GetBounds() const
static TiXmlElement * ToXML(const Geometry3D *geometry)
Serialize given geometry to XML.
virtual unsigned int GetFrameOfReferenceID() const
Get the DICOM FrameOfReferenceID referring to the used world coordinate system.
itk::AffineGeometryFrame< ScalarType, 3 >::TransformType AffineTransform3D
virtual bool GetImageGeometry() const
Is this an ImageGeometry?
mitk::AffineTransform3D * GetIndexToWorldTransform()
Get the transformation used to convert from index to world coordinates.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.