Page MenuHomePhabricator

planarfigures_reader_writer.patch

Authored By
neuhaus
Nov 4 2009, 6:51 PM
Size
19 KB
Referenced Files
None
Subscribers
None

planarfigures_reader_writer.patch

Index: mitkPlanarFigureReader.cpp
===================================================================
--- mitkPlanarFigureReader.cpp (revision 0)
+++ mitkPlanarFigureReader.cpp (revision 0)
@@ -0,0 +1,165 @@
+/*=========================================================================
+
+Program: Medical Imaging & Interaction Toolkit
+Language: C++
+Date: $Date: 2009-06-17 14:15:34 +0200 (Mi, 17. Jun 2009) $
+Version: $Revision: 17745 $
+
+Copyright (c) German Cancer Research Center, Division of Medical and
+Biological Informatics. All rights reserved.
+See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.
+
+This software is distributed WITHOUT ANY WARRANTY; without even
+the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+PURPOSE. See the above copyright notices for more information.
+
+=========================================================================*/
+
+
+#include "mitkPlanarFigureReader.h"
+
+mitk::PlanarFigureReader::PlanarFigureReader() : PlanarFigureSource(), FileReader()
+m_FileName(""), m_FilePrefix(""), m_FilePattern(""),m_Success(false)
+{
+ this->SetNumberOfOutputs(0);
+}
+
+
+mitk::PlanarFigureReader::~PlanarFigureReader()
+{}
+
+
+void mitk::PlanarFigureReader::GenerateData()
+{
+ m_Success = false;
+ if (m_FileName.empty())
+ {
+ itkWarningMacro( << "Sorry, filename has not been set!" );
+ return;
+ }
+ if (this->CanReadFile( m_FileName.c_str()) == false)
+ {
+ itkWarningMacro( << "Sorry, can't read file " << m_FileName << "!" );
+ return;
+ }
+
+ TiXmlDocument document( m_FileName);
+ if (!document.LoadFile())
+ {
+ LOG_ERROR << "Could not open/read/parse " << m_FileName << ". TinyXML reports: '" << document.ErrorDesc() << "'";
+ return;
+ }
+ int fileVersion = 1;
+ TiXmlElement* versionObject = document.FirstChildElement("Version");
+ if (versionObject != NULL)
+ {
+ if ( versionObject->QueryIntAttribute( "FileVersion", &fileVersion ) != TIXML_SUCCESS )
+ {
+ LOG_WARN << m_FileName << " does not contain version information! Trying version 1 format." << std::endl;
+ }
+ }
+ else
+ {
+ LOG_WARN << m_FileName << " does not contain version information! Trying version 1 format." << std::endl;
+ }
+ if (fileVersion != 1) // add file version selection and version specific file parsing here, if newer file versions are created
+ {
+ LOG_WARN << "File version > 1 is not supported by this reader.";
+ return;
+ }
+ /* file version 1 reader code */
+ for( TiXmlElement* pfElement = document.FirstChildElement("PlanarFigure"); element != NULL; element = element->NextSiblingElement("PlanarFigure") )
+ {
+ if (pfElement == NULL)
+ continue;
+ std::string type = pfElement->Attribute("type");
+ mitk::PlanarFigure::Pointer pf = NULL;
+ if (type == "PlanarAngle")
+ {
+ pf = mitk::PlanarAngle::New();
+ }
+ else if (type == "PlanarLine")
+ {
+ pf = mitk::PlanarLine::New();
+ }
+ else if (type == "PlanarCircle")
+ {
+ pf = mitk::PlanarCircle::New();
+ }
+ else else if (type == "PlanarPolygon")
+ {
+ pf = mitk::PlanarPolygon::New();
+ }
+ else
+ {
+ // unknown type
+ LOG_WARN << "encountered unknown planar figure type '" << type << "'. Skipping this element.";
+ continue;
+ }
+ TiXmlElement* cpElement = pfElement.FirstChildElement("control points");
+ if (cpElement != NULL)
+ for( TiXmlElement* vertElement = cpElement.FirstChildElement("vertex"); vertElement != NULL; vertElement = vertElement->NextSiblingElement("vertex"))
+ {
+ if (vertElement == NULL)
+ continue;
+ int id;
+ mitk::Point2D::ValueType x, y;
+ if (child->QueryIntAttribute("id", &id) == TIXML_WRONG_TYPE)
+ return; // TODO: can we do a better error handling?
+ if (child->QueryDoubleAttribute("x", &x) == TIXML_WRONG_TYPE)
+ return; // TODO: can we do a better error handling?
+ if (child->QueryDoubleAttribute("y", &y) == TIXML_WRONG_TYPE)
+ return; // TODO: can we do a better error handling?
+ Point2D p;
+ p->SetElement(0, x);
+ p->SetElement(1, y);
+ pf->SetControlPoint(id, p);
+ }
+ pf->GeneratePolyLine();
+ // \TODO: what about m_FigurePlaced and m_SelectedControlPoint ??
+ this->SetNthOutput( this->GetNumberOfOutputs(), pf ); // add pf as new output of this filter
+ }
+ m_Success = true;
+}
+
+
+void mitk::PlanarFigureReader::GenerateOutputInformation()
+{
+}
+
+int mitk::PlanarFigureReader::CanReadFile ( const char *name )
+{
+ std::ifstream in( name );
+ if ( !in.good() )
+ {
+ in.close();
+ return false;
+ }
+ else
+ {
+ in.close();
+ return true;
+ }
+}
+
+bool mitk::PlanarFigureReader::CanReadFile(const std::string filename, const std::string filePrefix, const std::string filePattern)
+{
+ // First check the extension
+ if( filename.empty())
+ {
+ //LOG_INFO<<"No filename specified."<<std::endl;
+ return false;
+ }
+ // \TODO: add more sophisticated evaluation code here
+ return true;
+}
+
+void mitk::PlanarFigureReader::ResizeOutputs( const unsigned int& num )
+{
+ unsigned int prevNum = this->GetNumberOfOutputs();
+ this->SetNumberOfOutputs( num );
+ for ( unsigned int i = prevNum; i < num; ++i )
+ {
+ this->SetNthOutput( i, this->MakeOutput( i ).GetPointer() );
+ }
+}
Index: mitkPlanarFigureReader.h
===================================================================
--- mitkPlanarFigureReader.h (revision 0)
+++ mitkPlanarFigureReader.h (revision 0)
@@ -0,0 +1,124 @@
+/*=========================================================================
+
+Program: Medical Imaging & Interaction Toolkit
+Language: C++
+Date: $Date: 2009-05-12 19:56:03 +0200 (Di, 12. Mai 2009) $
+Version: $Revision: 17179 $
+
+Copyright (c) German Cancer Research Center, Division of Medical and
+Biological Informatics. All rights reserved.
+See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.
+
+This software is distributed WITHOUT ANY WARRANTY; without even
+the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+PURPOSE. See the above copyright notices for more information.
+
+=========================================================================*/
+
+
+#ifndef _MITK_PlanarFigureReader__H_
+#define _MITK_PlanarFigureReader__H_
+
+#include <mitkPlanarFigureSource.h>
+#include <mitkFileReader.h>
+
+namespace mitk
+{
+/**
+ * @brief reads xml representations of mitk::PlanarFigure from a file
+ *
+ * Reader for xml files containing one or multiple xml represenations of
+ * mitk::PlanarFigure. If multiple mitk::PlanarFigure are stored in one file,
+ * these are assigned to multiple outputs of the filter.
+ * @ingroup IO
+*/
+class MITK_CORE_EXPORT PlanarFigureReader: public PlanarFigureSource, public FileReader
+{
+public:
+
+ mitkClassMacro( PlanarFigureReader, FileReader );
+
+ itkNewMacro( Self );
+
+ /**
+ * @brief Sets the filename of the file to be read
+ * @param _arg the filename of the point set xml-file
+ */
+ itkSetStringMacro( FileName );
+
+ /**
+ * @brief Returns the filename of the point set xml-file.
+ * @returns the filename of the point set xml-file.
+ */
+ itkGetStringMacro( FileName );
+
+ /**
+ * @warning multiple load not (yet) supported
+ */
+ itkSetStringMacro( FilePrefix );
+
+ /**
+ * @warning multiple load not (yet) supported
+ */
+ itkGetStringMacro( FilePrefix );
+
+ /**
+ * @warning multiple load not (yet) supported
+ */
+ itkSetStringMacro( FilePattern );
+
+ /**
+ * @warning multiple load not (yet) supported
+ */
+ itkGetStringMacro( FilePattern );
+
+ static bool CanReadFile(const std::string filename, const std::string filePrefix, const std::string filePattern);
+
+ /**
+ * @returns whether the last read attempt was successful or not.
+ */
+ itkGetConstMacro(Success, bool);
+
+protected:
+
+ /**
+ * Constructor
+ */
+ PlanarFigureReader();
+
+ /**
+ * Virtual destructor
+ */
+ virtual ~PlanarFigureReader();
+
+ /**
+ * Actually reads the point sets from the given file
+ */
+ virtual void GenerateData();
+
+ /**
+ * Does nothing in the current implementation
+ */
+ virtual void GenerateOutputInformation();
+
+ /**
+ * Resizes the output-objects according to the given number.
+ * @param num the new number of output objects.
+ */
+ virtual void ResizeOutputs( const unsigned int& num );
+
+ /**
+ * Checks if the given file has appropriate
+ * read access.
+ * @returns true if the file exists and may be read
+ * or false otherwise.
+ */
+ virtual int CanReadFile (const char *name);
+
+ std::string m_FileName;
+ std::string m_FilePrefix;
+ std::string m_FilePattern;
+ bool m_Success;
+};
+}
+#endif
Index: mitkPlanarFigureWriter.cpp
===================================================================
--- mitkPlanarFigureWriter.cpp (revision 0)
+++ mitkPlanarFigureWriter.cpp (revision 0)
@@ -0,0 +1,171 @@
+/*=========================================================================
+
+Program: Medical Imaging & Interaction Toolkit
+Language: C++
+Date: $Date: 2009-05-12 19:56:03 +0200 (Di, 12. Mai 2009) $
+Version: $Revision: 17179 $
+
+Copyright (c) German Cancer Research Center, Division of Medical and
+Biological Informatics. All rights reserved.
+See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.
+
+This software is distributed WITHOUT ANY WARRANTY; without even
+the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+PURPOSE. See the above copyright notices for more information.
+
+=========================================================================*/
+
+#include "mitkPlanarFigureWriter.h"
+#include <tinyxml.h>
+
+
+mitk::PlanarFigureWriter::PlanarFigureWriter()
+: m_FileName(""), m_FilePrefix(""), m_FilePattern(""), m_Extension(".pf"),
+ m_MimeType("application/MITK.PlanarFigure"), m_Success(false)
+{
+ this->SetNumberOfRequiredInputs( 1 );
+ this->SetNumberOfOutputs( 0 );
+ //this->SetNthOutput( 0, mitk::PlanarFigure::New().GetPointer() );
+}
+
+
+mitk::PlanarFigureWriter::~PlanarFigureWriter()
+{}
+
+
+void mitk::PlanarFigureWriter::GenerateData()
+{
+ m_Success = false;
+
+ if (m_FileName.empty())
+ {
+ LOG_ERROR << "Could not write planar figures. File name is invalid";
+ throw std::invalid_argument("file name is empty");
+ }
+
+ TiXmlDocument document;
+ TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" ); // TODO what to write here? encoding? etc....
+ document.LinkEndChild( decl );
+
+ TiXmlElement* version = new TiXmlElement("Version");
+ version->SetAttribute("Writer", __FILE__ );
+ version->SetAttribute("CVSRevision", "$Revision: 17055 $" );
+ version->SetAttribute("FileVersion", 1 );
+ document.LinkEndChild(version);
+
+
+ /* create xml element for each input */
+ for ( unsigned int i = 0 ; i < this->GetNumberOfInputs(); ++i )
+ {
+ InputType::Pointer pf = this->GetInput( i );
+ if (pf.IsNull())
+ continue;
+ TiXmlElement* pfElement = new TiXmlElement("PlanarFigure");
+ pfElement->SetAttribute("type", pf->GetNameOfClass());
+ document->LinkEndChild(pfElement);
+ PlanarFigure::VertexContainerType* vertices = pf->GetControlPoints();
+ if (vertices == NULL)
+ continue;
+ TiXmlElement* controlPointsElement = new TiXmlElement("control points");
+ pfElement->LinkEndChild(controlPointsElement);
+ for (PlanarFigure::VertexContainerType::ConstIterator it = vertices->Begin(); it != vertices->End(); ++it)
+ {
+ TiXmlElement* vElement = new TiXmlElement("vertex");
+ vElement->SetAttribute("id", it->Index());
+ vElement->SetDoubleAttribute("x", it->Value().GetElement(0));
+ vElement->SetDoubleAttribute("y", it->Value().GetElement(1));
+ controlPointsElement->LinkEndChild(vElement);
+ }
+ }
+
+ if (document.SaveFile( m_FileName) == false)
+ {
+ LOG_ERROR << "Could not write planar figures to " << m_FileName << "\nTinyXML reports '" << document.ErrorDesc() << "'";
+ throw std::ios_base::failure("Error during writing of planar figure xml file.");
+ }
+ m_Success = true;
+}
+
+
+void mitk::PlanarFigureWriter::ResizeInputs( const unsigned int& num )
+{
+ unsigned int prevNum = this->GetNumberOfInputs();
+ this->SetNumberOfInputs( num );
+ for ( unsigned int i = prevNum; i < num; ++i )
+ {
+ this->SetNthInput( i, mitk::PlanarFigure::New().GetPointer() );
+ }
+}
+
+
+void mitk::PlanarFigureWriter::SetInput( InputType* PlanarFigure )
+{
+ this->ProcessObject::SetNthInput( 0, PlanarFigure );
+}
+
+
+void mitk::PlanarFigureWriter::SetInput( const unsigned int& id, InputType* PlanarFigure )
+{
+ if ( id >= this->GetNumberOfInputs() )
+ this->ResizeInputs( id + 1 );
+ this->ProcessObject::SetNthInput( id, PlanarFigure );
+}
+
+
+mitk::PlanarFigure* mitk::PlanarFigureWriter::GetInput()
+{
+ if ( this->GetNumberOfInputs() < 1 )
+ return NULL;
+ else
+ return dynamic_cast<InputType*> ( this->GetInput( 0 ) );
+}
+
+
+mitk::PlanarFigure* mitk::PlanarFigureWriter::GetInput( const unsigned int& num )
+{
+ return dynamic_cast<InputType*> ( this->ProcessObject::GetInput( num ) );
+}
+
+
+bool mitk::PlanarFigureWriter::CanWriteDataType( DataTreeNode* input )
+{
+ if ( input == NULL )
+ return false;
+
+ mitk::BaseData* data = input->GetData();
+ if ( data == NULL)
+ return false;
+
+ mitk::PlanarFigure::Pointer PlanarFigure = dynamic_cast<mitk::PlanarFigure*>( data );
+ if( PlanarFigure.IsNull() )
+ return false;
+ // add code for special subclasses here
+ return true;
+}
+
+
+void mitk::PlanarFigureWriter::SetInput( DataTreeNode* input )
+{
+ if (this->CanWriteDataType(input))
+ this->ProcessObject::SetNthInput( 0, dynamic_cast<mitk::PlanarFigure*>( input->GetData() ) );
+}
+
+
+std::string mitk::PlanarFigureWriter::GetWritenMIMEType()
+{
+ return m_MimeType;
+}
+
+
+std::vector<std::string> mitk::PlanarFigureWriter::GetPossibleFileExtensions()
+{
+ std::vector<std::string> possibleFileExtensions;
+ possibleFileExtensions.push_back(m_Extension);
+ return possibleFileExtensions;
+}
+
+
+std::string mitk::PlanarFigureWriter::GetFileExtension()
+{
+ return m_Extension;
+}
Index: mitkPlanarFigureWriter.h
===================================================================
--- mitkPlanarFigureWriter.h (revision 0)
+++ mitkPlanarFigureWriter.h (revision 0)
@@ -0,0 +1,170 @@
+/*=========================================================================
+
+Program: Medical Imaging & Interaction Toolkit
+Language: C++
+Date: $Date: 2009-05-12 19:56:03 +0200 (Di, 12. Mai 2009) $
+Version: $Revision: 17179 $
+
+Copyright (c) German Cancer Research Center, Division of Medical and
+Biological Informatics. All rights reserved.
+See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.
+
+This software is distributed WITHOUT ANY WARRANTY; without even
+the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+PURPOSE. See the above copyright notices for more information.
+
+=========================================================================*/
+
+
+#ifndef _MITK_PlanarFigure_WRITER__H_
+#define _MITK_PlanarFigure_WRITER__H_
+
+#include <itkProcessObject.h>
+#include <mitkFileWriter.h>
+#include <mitkPlanarFigure.h>
+
+namespace mitk
+{
+
+ /**
+ * @brief XML-based writer for mitk::PlanarFigures
+ *
+ * XML-based writer for mitk::PlanarFigures.
+ * @ingroup Process
+ */
+ class MITK_CORE_EXPORT PlanarFigureWriter : public mitk::FileWriter
+ {
+ public:
+
+ mitkClassMacro( PlanarFigureWriter, mitk::FileWriter );
+
+ mitkWriterMacro;
+
+ itkNewMacro( Self );
+
+ typedef mitk::PlanarFigure InputType;
+
+ typedef InputType::Pointer InputTypePointer;
+
+ /**
+ * Sets the filename of the file to write.
+ * @param FileName the name of the file to write.
+ */
+ itkSetStringMacro( FileName );
+
+ /**
+ * @returns the name of the file to be written to disk.
+ */
+ itkGetStringMacro( FileName );
+
+ /**
+ * @warning multiple write not (yet) supported
+ */
+ itkSetStringMacro( FilePrefix );
+
+ /**
+ * @warning multiple write not (yet) supported
+ */
+ itkGetStringMacro( FilePrefix );
+
+ /**
+ * @warning multiple write not (yet) supported
+ */
+ itkSetStringMacro( FilePattern );
+
+ /**
+ * @warning multiple write not (yet) supported
+ */
+ itkGetStringMacro( FilePattern );
+
+ /**
+ * Sets the 0'th input object for the filter.
+ * @param input the first input for the filter.
+ */
+ void SetInput( InputType* input );
+
+ /**
+ * Sets the n'th input object for the filter. If num is
+ * larger than GetNumberOfInputs() the number of inputs is
+ * resized appropriately.
+ * @param input the n'th input for the filter.
+ */
+ void SetInput( const unsigned int& num, InputType* input);
+
+ /**
+ * @returns the 0'th input object of the filter.
+ */
+ PlanarFigure* GetInput();
+
+ /**
+ * @param num the index of the desired output object.
+ * @returns the n'th input object of the filter.
+ */
+ PlanarFigure* GetInput( const unsigned int& num );
+
+
+ /**
+ * @brief Return the possible file extensions for the data type associated with the writer
+ */
+ virtual std::vector<std::string> GetPossibleFileExtensions();
+
+ /**
+ * @brief Return the extension to be added to the filename.
+ */
+ virtual std::string GetFileExtension();
+
+ /**
+ * @brief Check if the Writer can write the Content of the
+ */
+ virtual bool CanWriteDataType( DataTreeNode* );
+
+ /**
+ * @brief Return the MimeType of the saved File.
+ */
+ virtual std::string GetWritenMIMEType();
+
+ /**
+ * @brief Set the DataTreenode as Input. Important: The Writer always have a SetInput-Function.
+ */
+ virtual void SetInput( DataTreeNode* );
+
+ /**
+ * @returns whether the last write attempt was successful or not.
+ */
+ itkGetConstMacro(Success, bool);
+
+ protected:
+
+ /**
+ * Constructor.
+ */
+ PlanarFigureWriter();
+
+ /**
+ * Virtual destructor.
+ */
+ virtual ~PlanarFigureWriter();
+
+
+ /**
+ * Writes the a .pf file in xml format that contains all input planar figures
+ */
+ virtual void GenerateData();
+
+
+ /**
+ * Resizes the number of inputs of the writer.
+ * The inputs are initialized by empty PlanarFigures
+ * @param num the new number of inputs
+ */
+ virtual void ResizeInputs( const unsigned int& num );
+
+ std::string m_FileName;
+ std::string m_FilePrefix;
+ std::string m_FilePattern;
+ std::string m_Extension;
+ std::string m_MimeType;
+ bool m_Success;
+ };
+}
+#endif

File Metadata

Mime Type
application/octet-stream
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
340
Default Alt Text
planarfigures_reader_writer.patch (19 KB)

Event Timeline

draft of reader and writer for planar figures