Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "mitkTransferFunctionPropertySerializer.h"
00019
00020 namespace mitk {
00021
00022 mitk::TransferFunctionPropertySerializer::TransferFunctionPropertySerializer()
00023 {
00024 }
00025
00026 mitk::TransferFunctionPropertySerializer::~TransferFunctionPropertySerializer()
00027 {
00028 }
00029
00030 TiXmlElement* mitk::TransferFunctionPropertySerializer::Serialize()
00031 {
00032 if (const TransferFunctionProperty* prop = dynamic_cast<const TransferFunctionProperty*>(mitk::BasePropertySerializer::m_Property.GetPointer()))
00033 {
00034 TransferFunction* transferfunction = prop->GetValue();
00035 if (!transferfunction)
00036 return NULL;
00037
00038 TiXmlElement* element = new TiXmlElement("TransferFunction");
00039
00040
00041 TiXmlElement* scalarOpacityPointlist = new TiXmlElement( "ScalarOpacity" );
00042
00043 TransferFunction::ControlPoints scalarOpacityPoints = transferfunction->GetScalarOpacityPoints();
00044 for ( TransferFunction::ControlPoints::iterator iter = scalarOpacityPoints.begin();
00045 iter != scalarOpacityPoints.end();
00046 ++iter )
00047 {
00048 TiXmlElement* pointel = new TiXmlElement("point");
00049 pointel->SetDoubleAttribute("x", iter->first);
00050 pointel->SetDoubleAttribute("y", iter->second);
00051 scalarOpacityPointlist->LinkEndChild( pointel );
00052 }
00053 element->LinkEndChild( scalarOpacityPointlist );
00054
00055 TiXmlElement* gradientOpacityPointlist = new TiXmlElement( "GradientOpacity" );
00056 TransferFunction::ControlPoints gradientOpacityPoints = transferfunction->GetGradientOpacityPoints();
00057 for ( TransferFunction::ControlPoints::iterator iter = gradientOpacityPoints.begin();
00058 iter != gradientOpacityPoints.end();
00059 ++iter )
00060 {
00061 TiXmlElement* pointel = new TiXmlElement("point");
00062 pointel->SetDoubleAttribute("x", iter->first);
00063 pointel->SetDoubleAttribute("y", iter->second);
00064 gradientOpacityPointlist->LinkEndChild( pointel );
00065 }
00066 element->LinkEndChild( gradientOpacityPointlist );
00067
00068
00069 vtkColorTransferFunction* ctf = transferfunction->GetColorTransferFunction();
00070 if (ctf == NULL)
00071 return NULL;
00072 TiXmlElement* pointlist = new TiXmlElement("Color");
00073 for (int i = 0; i < ctf->GetSize(); i++ )
00074 {
00075 double myVal[6];
00076 ctf->GetNodeValue(i, myVal);
00077 TiXmlElement* pointel = new TiXmlElement("point");
00078 pointel->SetDoubleAttribute("x", myVal[0]);
00079 pointel->SetDoubleAttribute("r", myVal[1]);
00080 pointel->SetDoubleAttribute("g", myVal[2]);
00081 pointel->SetDoubleAttribute("b", myVal[3]);
00082 pointel->SetDoubleAttribute("midpoint", myVal[4]);
00083 pointel->SetDoubleAttribute("sharpness", myVal[5]);
00084 pointlist->LinkEndChild( pointel );
00085 }
00086 element->LinkEndChild( pointlist );
00087 return element;
00088 }
00089 else return NULL;
00090 }
00091
00092 bool mitk::TransferFunctionPropertySerializer::SerializeTransferFunction( const char * filename, TransferFunction::Pointer tf )
00093 {
00094 TransferFunctionPropertySerializer::Pointer tfps=TransferFunctionPropertySerializer::New();
00095 tfps->SetProperty( TransferFunctionProperty::New( tf ) );
00096 TiXmlElement* s=tfps->Serialize();
00097
00098 if(!s)
00099 {
00100 MITK_ERROR << "cant serialize transfer function";
00101 return false;
00102 }
00103
00104 TiXmlDocument document;
00105 TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "UTF-8", "" );
00106 document.LinkEndChild( decl );
00107
00108 TiXmlElement* version = new TiXmlElement("Version");
00109 version->SetAttribute("TransferfunctionVersion", 1 );
00110
00111 document.LinkEndChild(version);
00112 document.LinkEndChild(s);
00113
00114 if ( !document.SaveFile( filename ) )
00115 {
00116 MITK_ERROR << "Could not write scene to " << filename << "\nTinyXML reports '" << document.ErrorDesc() << "'";
00117 return false;
00118 }
00119 return true;
00120 }
00121
00122 }
00123
00124
00125 MITK_REGISTER_SERIALIZER(TransferFunctionPropertySerializer);