18 #include <boost/lexical_cast.hpp>
33 if (!transferfunction)
36 auto element =
new TiXmlElement(
"TransferFunction");
39 auto scalarOpacityPointlist =
new TiXmlElement(
"ScalarOpacity");
42 for (
auto iter = scalarOpacityPoints.begin(); iter != scalarOpacityPoints.end(); ++iter)
44 auto pointel =
new TiXmlElement(
"point");
45 pointel->SetAttribute(
"x", boost::lexical_cast<std::string>(iter->first));
46 pointel->SetAttribute(
"y", boost::lexical_cast<std::string>(iter->second));
47 scalarOpacityPointlist->LinkEndChild(pointel);
49 element->LinkEndChild(scalarOpacityPointlist);
51 auto gradientOpacityPointlist =
new TiXmlElement(
"GradientOpacity");
53 for (
auto iter = gradientOpacityPoints.begin(); iter != gradientOpacityPoints.end(); ++iter)
55 auto pointel =
new TiXmlElement(
"point");
56 pointel->SetAttribute(
"x", boost::lexical_cast<std::string>(iter->first));
57 pointel->SetAttribute(
"y", boost::lexical_cast<std::string>(iter->second));
58 gradientOpacityPointlist->LinkEndChild(pointel);
60 element->LinkEndChild(gradientOpacityPointlist);
66 auto pointlist =
new TiXmlElement(
"Color");
67 for (
int i = 0; i < ctf->GetSize(); i++)
70 ctf->GetNodeValue(i, myVal);
71 auto pointel =
new TiXmlElement(
"point");
72 pointel->SetAttribute(
"x", boost::lexical_cast<std::string>(myVal[0]));
73 pointel->SetAttribute(
"r", boost::lexical_cast<std::string>(myVal[1]));
74 pointel->SetAttribute(
"g", boost::lexical_cast<std::string>(myVal[2]));
75 pointel->SetAttribute(
"b", boost::lexical_cast<std::string>(myVal[3]));
76 pointel->SetAttribute(
"midpoint", boost::lexical_cast<std::string>(myVal[4]));
77 pointel->SetAttribute(
"sharpness", boost::lexical_cast<std::string>(myVal[5]));
78 pointlist->LinkEndChild(pointel);
80 element->LinkEndChild(pointlist);
92 TiXmlElement *s = tfps->Serialize();
96 MITK_ERROR <<
"cant serialize transfer function";
101 TiXmlDocument document;
102 auto decl =
new TiXmlDeclaration(
106 document.LinkEndChild(decl);
108 auto version =
new TiXmlElement(
"Version");
109 version->SetAttribute(
"TransferfunctionVersion", 1);
111 document.LinkEndChild(version);
112 document.LinkEndChild(s);
114 if (!document.SaveFile(filename))
116 MITK_ERROR <<
"Could not write scene to " << filename <<
"\nTinyXML reports '" << document.ErrorDesc() <<
"'";
132 TiXmlElement *scalarOpacityPointlist = element->FirstChildElement(
"ScalarOpacity");
133 if (scalarOpacityPointlist ==
nullptr)
138 tf->ClearScalarOpacityPoints();
142 for (TiXmlElement *pointElement = scalarOpacityPointlist->FirstChildElement(
"point"); pointElement !=
nullptr;
143 pointElement = pointElement->NextSiblingElement(
"point"))
147 if (pointElement->QueryStringAttribute(
"x", &x) != TIXML_SUCCESS)
149 if (pointElement->QueryStringAttribute(
"y", &y) != TIXML_SUCCESS)
151 tf->AddScalarOpacityPoint(boost::lexical_cast<double>(x), boost::lexical_cast<double>(y));
154 TiXmlElement *gradientOpacityPointlist = element->FirstChildElement(
"GradientOpacity");
155 if (gradientOpacityPointlist ==
nullptr)
160 tf->ClearGradientOpacityPoints();
162 for (TiXmlElement *pointElement = gradientOpacityPointlist->FirstChildElement(
"point"); pointElement !=
nullptr;
163 pointElement = pointElement->NextSiblingElement(
"point"))
167 if (pointElement->QueryStringAttribute(
"x", &x) != TIXML_SUCCESS)
169 if (pointElement->QueryStringAttribute(
"y", &y) != TIXML_SUCCESS)
171 tf->AddGradientOpacityPoint(boost::lexical_cast<double>(x), boost::lexical_cast<double>(y));
174 TiXmlElement *rgbPointlist = element->FirstChildElement(
"Color");
175 if (rgbPointlist ==
nullptr)
179 vtkColorTransferFunction *ctf = tf->GetColorTransferFunction();
185 ctf->RemoveAllPoints();
187 for (TiXmlElement *pointElement = rgbPointlist->FirstChildElement(
"point"); pointElement !=
nullptr;
188 pointElement = pointElement->NextSiblingElement(
"point"))
191 std::string r, g, b, midpoint, sharpness;
192 if (pointElement->QueryStringAttribute(
"x", &x) != TIXML_SUCCESS)
194 if (pointElement->QueryStringAttribute(
"r", &r) != TIXML_SUCCESS)
196 if (pointElement->QueryStringAttribute(
"g", &g) != TIXML_SUCCESS)
198 if (pointElement->QueryStringAttribute(
"b", &b) != TIXML_SUCCESS)
200 if (pointElement->QueryStringAttribute(
"midpoint", &midpoint) != TIXML_SUCCESS)
202 if (pointElement->QueryStringAttribute(
"sharpness", &sharpness) != TIXML_SUCCESS)
204 ctf->AddRGBPoint(boost::lexical_cast<double>(x),
205 boost::lexical_cast<double>(r),
206 boost::lexical_cast<double>(g),
207 boost::lexical_cast<double>(b),
208 boost::lexical_cast<double>(midpoint),
209 boost::lexical_cast<double>(sharpness));
212 catch (boost::bad_lexical_cast &e)
214 MITK_ERROR <<
"Could not parse string as number: " << e.what();
223 const char *filePath)
225 TiXmlDocument document(filePath);
227 if (!document.LoadFile())
229 MITK_ERROR <<
"Could not open/read/parse " << filePath <<
"\nTinyXML reports: " << document.ErrorDesc()
236 TiXmlElement *versionObject = document.FirstChildElement(
"Version");
239 if (versionObject->QueryIntAttribute(
"TransferfunctionVersion", &fileVersion) != TIXML_SUCCESS)
241 MITK_WARN <<
"Transferfunction file " << filePath
242 <<
" does not contain version information! Trying version 1 format.";
246 TiXmlElement *input = document.FirstChildElement(
"TransferFunction");
257 MITK_WARN <<
"Can't deserialize transfer function";
virtual vtkColorTransferFunction * GetColorTransferFunction()
Get/Set wrapped vtk transfer function.
The TransferFunctionProperty class Property class for the mitk::TransferFunction. ...
static bool SerializeTransferFunction(const char *filename, TransferFunction::Pointer tf)
itk::SmartPointer< Self > Pointer
virtual ~TransferFunctionPropertySerializer()
virtual BaseProperty::Pointer Deserialize(TiXmlElement *element) override
Deserializes given TiXmlElement.
virtual TiXmlElement * Serialize() override
Serializes given BaseProperty object.
The TransferFunction class A wrapper class for VTK scalar opacity, gradient opacity, and color transfer functions.Holds a copy of each of the three standard VTK transfer functions (scalar opacity, gradient opacity, color) and provides an interface for manipulating their control points. Each original function can be retrieved by a Get() method.
DataCollection - Class to facilitate loading/accessing structured data.
static TransferFunction::Pointer DeserializeTransferFunction(const char *filePath)
TransferFunction::ControlPoints & GetScalarOpacityPoints()
Get a copy of the scalar opacity transfer function control-points.
Convenience class to temporarily change the current locale.
static const std::string filename
MITK_REGISTER_SERIALIZER(TransferFunctionPropertySerializer)
std::vector< std::pair< double, double > > ControlPoints
BaseProperty::ConstPointer m_Property
TransferFunctionPropertySerializer()
TransferFunction::ControlPoints & GetGradientOpacityPoints()
Get a copy of the gradient opacity transfer function control-points.