Public Types | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes

mitk::PlanarFigureReader Class Reference
[IO Classes]

reads xml representations of mitk::PlanarFigure from a file More...

#include <mitkPlanarFigureReader.h>

Inheritance diagram for mitk::PlanarFigureReader:
Inheritance graph
[legend]
Collaboration diagram for mitk::PlanarFigureReader:
Collaboration graph
[legend]

List of all members.

Public Types

typedef PlanarFigureReader Self
typedef FileReader Superclass
typedef itk::SmartPointer< SelfPointer
typedef itk::SmartPointer
< const Self
ConstPointer

Public Member Functions

virtual const char * GetClassName () const
virtual void SetFileName (const char *_arg)
 Sets the filename of the file to be read.
virtual const char * GetFileName () const
 Returns the filename of the point set xml-file.
virtual void SetFilePrefix (const char *_arg)
virtual const char * GetFilePrefix () const
virtual void SetFilePattern (const char *_arg)
virtual const char * GetFilePattern () const
virtual bool GetSuccess () const
virtual DataObjectPointer MakeOutput (unsigned int idx)

Static Public Member Functions

static Pointer New ()
static bool CanReadFile (const std::string filename, const std::string filePrefix, const std::string filePattern)

Protected Types

typedef std::list< double > DoubleList

Protected Member Functions

 PlanarFigureReader ()
virtual ~PlanarFigureReader ()
virtual void GenerateData ()
virtual void GenerateOutputInformation ()
virtual void ResizeOutputs (const unsigned int &num)
virtual int CanReadFile (const char *name)
mitk::Vector3D GetVectorFromXMLNode (TiXmlElement *e)
 parses the element for the attributes x,y,z and returns a mitk::Vector3D filled with these values
mitk::Point3D GetPointFromXMLNode (TiXmlElement *e)
 parses the element for the attributes x,y,z and returns a mitk::Point3D filled with these values
DoubleList GetDoubleAttributeListFromXMLNode (TiXmlElement *e, const char *attributeNameBase, unsigned int count)
 parses the element for the attributes name0 to nameN, where "name" and the number of attributes to read are passed as argument. Returns a list of double vales.

Protected Attributes

std::string m_FileName
std::string m_FilePrefix
std::string m_FilePattern
bool m_Success

Detailed Description

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.

Definition at line 40 of file mitkPlanarFigureReader.h.


Member Typedef Documentation

typedef itk::SmartPointer<const Self> mitk::PlanarFigureReader::ConstPointer

Reimplemented from mitk::PlanarFigureSource.

Definition at line 44 of file mitkPlanarFigureReader.h.

typedef std::list< double > mitk::PlanarFigureReader::DoubleList [protected]

Definition at line 97 of file mitkPlanarFigureReader.h.

typedef itk::SmartPointer<Self> mitk::PlanarFigureReader::Pointer

Reimplemented from mitk::PlanarFigureSource.

Definition at line 44 of file mitkPlanarFigureReader.h.

Reimplemented from mitk::PlanarFigureSource.

Definition at line 44 of file mitkPlanarFigureReader.h.

Reimplemented from mitk::PlanarFigureSource.

Definition at line 44 of file mitkPlanarFigureReader.h.


Constructor & Destructor Documentation

mitk::PlanarFigureReader::PlanarFigureReader (  ) [protected]

Constructor

Definition at line 37 of file mitkPlanarFigureReader.cpp.

References MakeOutput(), and mitk::BaseProcess::SetNthOutput().

                                           : PlanarFigureSource(), FileReader(),
m_FileName(""), m_FilePrefix(""), m_FilePattern(""), m_Success(false)
{
  this->SetNumberOfRequiredOutputs(1);
  this->SetNumberOfOutputs(1);
  this->SetNthOutput(0, this->MakeOutput(0));
  

  //this->Modified();
  //this->GetOutput()->Modified();
  //this->GetOutput()->ReleaseData();
}
mitk::PlanarFigureReader::~PlanarFigureReader (  ) [protected, virtual]

Virtual destructor

Definition at line 51 of file mitkPlanarFigureReader.cpp.

{}

Member Function Documentation

bool mitk::PlanarFigureReader::CanReadFile ( const std::string  filename,
const std::string  filePrefix,
const std::string  filePattern 
) [static]

Definition at line 376 of file mitkPlanarFigureReader.cpp.

{
  if (filename.empty())
    return false;

  return (itksys::SystemTools::LowerCase(itksys::SystemTools::GetFilenameLastExtension(filename)) == ".pf");  //assume, we can read all .pf files

  //TiXmlDocument document(filename);
  //if (document.LoadFile() == false)  
  //  return false;
  //return (document.FirstChildElement("PlanarFigure") != NULL);
}
int mitk::PlanarFigureReader::CanReadFile ( const char *  name ) [protected, virtual]

Checks if the given file has appropriate read access.

Returns:
true if the file exists and may be read or false otherwise.

Definition at line 363 of file mitkPlanarFigureReader.cpp.

{
  if (std::string(name).empty())
    return false;

  return (itksys::SystemTools::LowerCase(itksys::SystemTools::GetFilenameLastExtension(name)) == ".pf");  //assume, we can read all .pf files

  //TiXmlDocument document(name);
  //if (document.LoadFile() == false)  
  //  return false;
  //return (document.FirstChildElement("PlanarFigure") != NULL);
}
void mitk::PlanarFigureReader::GenerateData (  ) [protected, virtual]

Actually reads the point sets from the given file

Definition at line 61 of file mitkPlanarFigureReader.cpp.

References TiXmlDocument::ErrorCol(), TiXmlDocument::ErrorDesc(), TiXmlDocument::ErrorRow(), TiXmlNode::FirstChildElement(), TiXmlDocument::LoadFile(), MITK_ERROR, MITK_WARN, mitk::PlaneGeometry::New(), mitk::PlanarArrow::New(), mitk::PlanarRectangle::New(), mitk::PlanarPolygon::New(), mitk::PlanarLine::New(), mitk::PlanarFourPointAngle::New(), mitk::PlanarCross::New(), mitk::PlanarCircle::New(), mitk::PlanarAngle::New(), TiXmlNode::NextSiblingElement(), TiXmlElement::QueryIntAttribute(), TIXML_SUCCESS, and TIXML_WRONG_TYPE.

{
  m_Success = false;
  this->SetNumberOfOutputs(0); // reset all outputs, we add new ones depending on the file content

  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())
  {
    MITK_ERROR << "Could not open/read/parse " << m_FileName << ". TinyXML reports: '" << document.ErrorDesc() << "'. "
              << "The error occurred in row " << document.ErrorRow() << ", column " << document.ErrorCol() << ".";
    return;
  }
  int fileVersion = 1;
  TiXmlElement* versionObject = document.FirstChildElement("Version");
  if (versionObject != NULL)
  {
    if ( versionObject->QueryIntAttribute( "FileVersion", &fileVersion ) != TIXML_SUCCESS )
    {
      MITK_WARN << m_FileName << " does not contain version information! Trying version 1 format." << std::endl;
    }
  }
  else
  {
    MITK_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
  {
    MITK_WARN << "File version > 1 is not supported by this reader.";
      return;
  }

  /* file version 1 reader code */
  for( TiXmlElement* pfElement = document.FirstChildElement("PlanarFigure");
       pfElement != NULL;
       pfElement = pfElement->NextSiblingElement("PlanarFigure") )
  {
    if (pfElement == NULL)
      continue;

    std::string type = pfElement->Attribute("type");

    mitk::PlanarFigure::Pointer planarFigure = NULL;
    if (type == "PlanarAngle")
    {
      planarFigure = mitk::PlanarAngle::New();
    }
    else if (type == "PlanarCircle")
    {
      planarFigure = mitk::PlanarCircle::New();
    }
    else if (type == "PlanarCross")
    {
      planarFigure = mitk::PlanarCross::New();
    }
    else if (type == "PlanarFourPointAngle")
    {
      planarFigure = mitk::PlanarFourPointAngle::New();
    }
    else if (type == "PlanarLine")
    {
      planarFigure = mitk::PlanarLine::New();
    }
    else if (type == "PlanarPolygon")
    {
      planarFigure = mitk::PlanarPolygon::New();
    }
    else if (type == "PlanarRectangle")
    {
      planarFigure = mitk::PlanarRectangle::New();
    }
    else if (type == "PlanarArrow")
    {
      planarFigure = mitk::PlanarArrow::New();
    }
    else 
    {
      // unknown type
      MITK_WARN << "encountered unknown planar figure type '" << type << "'. Skipping this element.";
      continue;
    }


    // Read properties of the planar figure
    for( TiXmlElement* propertyElement = pfElement->FirstChildElement("property");
         propertyElement != NULL;
         propertyElement = propertyElement->NextSiblingElement("property") )
    {
      const char* keya = propertyElement->Attribute("key");
      std::string key( keya ? keya : "");

      const char* typea = propertyElement->Attribute("type");
      std::string type( typea ? typea : "");

      // hand propertyElement to specific reader
      std::stringstream propertyDeserializerClassName;
      propertyDeserializerClassName << type << "Deserializer";

      std::list<itk::LightObject::Pointer> readers =
        itk::ObjectFactoryBase::CreateAllInstance(propertyDeserializerClassName.str().c_str());
      if (readers.size() < 1)
      {
        MITK_ERROR << "No property reader found for " << type;
      }
      if (readers.size() > 1)
      {
        MITK_WARN << "Multiple property readers found for " << type << ". Using arbitrary first one.";
      }

      for ( std::list<itk::LightObject::Pointer>::iterator iter = readers.begin();
        iter != readers.end();
        ++iter )
      {
        if (BasePropertyDeserializer* reader = dynamic_cast<BasePropertyDeserializer*>( iter->GetPointer() ) )
        {
          BaseProperty::Pointer property = reader->Deserialize( propertyElement->FirstChildElement() );
          if (property.IsNotNull())
          {
            planarFigure->GetPropertyList()->ReplaceProperty(key, property);
          }
          else
          {
            MITK_ERROR << "There were errors while loading property '" << key << "' of type " << type << ". Your data may be corrupted";
          }
          break;
        }
      }
    }


    // Read geometry of containing plane
    TiXmlElement* geoElement = pfElement->FirstChildElement("Geometry");
    if (geoElement != NULL)
    {
      try
      {
        // Create plane geometry
        mitk::PlaneGeometry::Pointer planeGeo = mitk::PlaneGeometry::New();

        // Extract and set plane transform parameters
        DoubleList transformList = this->GetDoubleAttributeListFromXMLNode( geoElement->FirstChildElement( "transformParam" ), "param", 12 );

        typedef mitk::AffineGeometryFrame3D::TransformType TransformType;
        TransformType::ParametersType parameters;
        parameters.SetSize( 12 );

        unsigned int i;
        DoubleList::iterator it;
        for ( it = transformList.begin(), i = 0;
              it != transformList.end();
              ++it, ++i )
        {
          parameters.SetElement( i, *it );
        }

        typedef mitk::AffineGeometryFrame3D::TransformType TransformType;
        TransformType::Pointer affineGeometry = TransformType::New();
        affineGeometry->SetParameters( parameters );
        planeGeo->SetIndexToWorldTransform( affineGeometry );


        // Extract and set plane bounds
        DoubleList boundsList = this->GetDoubleAttributeListFromXMLNode( geoElement->FirstChildElement( "boundsParam" ), "bound", 6 );

        typedef mitk::Geometry3D::BoundsArrayType BoundsArrayType;

        BoundsArrayType bounds;
        for ( it = boundsList.begin(), i = 0;
              it != boundsList.end();
              ++it, ++i )
        {
          bounds[i] = *it;
        }

        planeGeo->SetBounds( bounds );


        // Extract and set spacing and origin
        Vector3D spacing = this->GetVectorFromXMLNode(geoElement->FirstChildElement("Spacing"));
        planeGeo->SetSpacing( spacing );

        Point3D origin = this->GetPointFromXMLNode(geoElement->FirstChildElement("Origin"));
        planeGeo->SetOrigin( origin );
        planarFigure->SetGeometry2D(planeGeo);
      }
      catch (...)
      {
      }
    }
    TiXmlElement* cpElement = pfElement->FirstChildElement("ControlPoints");
    bool first = true;
    if (cpElement != NULL)      
      for( TiXmlElement* vertElement = cpElement->FirstChildElement("Vertex"); vertElement != NULL; vertElement = vertElement->NextSiblingElement("Vertex"))
      {
        if (vertElement == NULL)
          continue;
        int id = 0;
        mitk::Point2D::ValueType x = 0.0;
        mitk::Point2D::ValueType y = 0.0;
        if (vertElement->QueryIntAttribute("id", &id) == TIXML_WRONG_TYPE)
          return; // TODO: can we do a better error handling?
        if (vertElement->QueryFloatAttribute("x", &x) == TIXML_WRONG_TYPE)
          return; // TODO: can we do a better error handling?
        if (vertElement->QueryFloatAttribute("y", &y) == TIXML_WRONG_TYPE)
          return; // TODO: can we do a better error handling?
        Point2D p;
        p.SetElement(0, x);
        p.SetElement(1, y);
        if (first == true)  // needed to set m_FigurePlaced to true
        {
          planarFigure->PlaceFigure(p);
          first = false;
        }
        planarFigure->SetControlPoint(id, p, true);
      }

    // Calculate feature quantities of this PlanarFigure
    planarFigure->EvaluateFeatures();

    // Make sure that no control point is currently selected
    planarFigure->DeselectControlPoint();

    // \TODO: what about m_FigurePlaced and m_SelectedControlPoint ??
    this->SetNthOutput( this->GetNumberOfOutputs(), planarFigure );  // add planarFigure as new output of this filter
  }
  m_Success = true;
}
void mitk::PlanarFigureReader::GenerateOutputInformation ( void   ) [protected, virtual]

Does nothing in the current implementation

Definition at line 359 of file mitkPlanarFigureReader.cpp.

{
}
virtual const char* mitk::PlanarFigureReader::GetClassName (  ) const [virtual]

Reimplemented from mitk::PlanarFigureSource.

mitk::PlanarFigureReader::DoubleList mitk::PlanarFigureReader::GetDoubleAttributeListFromXMLNode ( TiXmlElement e,
const char *  attributeNameBase,
unsigned int  count 
) [protected]

parses the element for the attributes name0 to nameN, where "name" and the number of attributes to read are passed as argument. Returns a list of double vales.

Parameters:
[in]ethe TiXmlElement that will be parsed
[in]attributeNameBasethe basic name of the parameters
[in]countthe number of parameters
Returns:
returns a mitk::Point3D with the values x,y,z

Definition at line 337 of file mitkPlanarFigureReader.cpp.

References TiXmlElement::QueryFloatAttribute(), and TIXML_WRONG_TYPE.

{
  DoubleList list;

  if (e == NULL)
    throw std::invalid_argument("node invalid"); // TODO: can we do a better error handling?
  
  for ( unsigned int i = 0; i < count; ++i )
  {
    mitk::ScalarType p(-1.0);
    std::stringstream attributeName;
    attributeName << attributeNameBase << i;

    if (e->QueryFloatAttribute( attributeName.str().c_str(), &p ) == TIXML_WRONG_TYPE)
      throw std::invalid_argument("node malformatted"); // TODO: can we do a better error handling?
    list.push_back( p );
  }


  return list;
}
virtual const char* mitk::PlanarFigureReader::GetFileName (  ) const [virtual]

Returns the filename of the point set xml-file.

Returns:
the filename of the point set xml-file.

Implements mitk::FileReader.

virtual const char* mitk::PlanarFigureReader::GetFilePattern (  ) const [virtual]
Warning:
multiple load not (yet) supported

Implements mitk::FileReader.

virtual const char* mitk::PlanarFigureReader::GetFilePrefix (  ) const [virtual]
Warning:
multiple load not (yet) supported

Implements mitk::FileReader.

mitk::Point3D mitk::PlanarFigureReader::GetPointFromXMLNode ( TiXmlElement e ) [protected]

parses the element for the attributes x,y,z and returns a mitk::Point3D filled with these values

Parameters:
[in]ethe TiXmlElement that will be parsed
Returns:
returns a mitk::Point3D with the values x,y,z

Definition at line 299 of file mitkPlanarFigureReader.cpp.

References TiXmlElement::QueryFloatAttribute(), and TIXML_WRONG_TYPE.

{
  if (e == NULL)
    throw std::invalid_argument("node invalid"); // TODO: can we do a better error handling?
  mitk::Point3D point;
  mitk::ScalarType p(-1.0);
  if (e->QueryFloatAttribute("x", &p) == TIXML_WRONG_TYPE)
    throw std::invalid_argument("node malformatted"); // TODO: can we do a better error handling?
  point.SetElement(0, p);
  if (e->QueryFloatAttribute("y", &p) == TIXML_WRONG_TYPE)
    throw std::invalid_argument("node malformatted"); // TODO: can we do a better error handling?
  point.SetElement(1, p);
  if (e->QueryFloatAttribute("z", &p) == TIXML_WRONG_TYPE)
    throw std::invalid_argument("node malformatted"); // TODO: can we do a better error handling?
  point.SetElement(2, p);
  return point;
}
virtual bool mitk::PlanarFigureReader::GetSuccess (  ) const [virtual]
Returns:
whether the last read attempt was successful or not.
mitk::Vector3D mitk::PlanarFigureReader::GetVectorFromXMLNode ( TiXmlElement e ) [protected]

parses the element for the attributes x,y,z and returns a mitk::Vector3D filled with these values

Parameters:
[in]ethe TiXmlElement that will be parsed
Returns:
returns a mitk::Vector3D with the values x,y,z

Definition at line 318 of file mitkPlanarFigureReader.cpp.

References TiXmlElement::QueryFloatAttribute(), and TIXML_WRONG_TYPE.

{
  if (e == NULL)
    throw std::invalid_argument("node invalid"); // TODO: can we do a better error handling?
  mitk::Vector3D vector;
  mitk::ScalarType p(-1.0);
  if (e->QueryFloatAttribute("x", &p) == TIXML_WRONG_TYPE)
    throw std::invalid_argument("node malformatted"); // TODO: can we do a better error handling?
  vector.SetElement(0, p);
  if (e->QueryFloatAttribute("y", &p) == TIXML_WRONG_TYPE)
    throw std::invalid_argument("node malformatted"); // TODO: can we do a better error handling?
  vector.SetElement(1, p);
  if (e->QueryFloatAttribute("z", &p) == TIXML_WRONG_TYPE)
    throw std::invalid_argument("node malformatted"); // TODO: can we do a better error handling?
  vector.SetElement(2, p);
  return vector;
}
mitk::PlanarFigureSource::DataObjectPointer mitk::PlanarFigureReader::MakeOutput ( unsigned int  idx ) [virtual]

Allocates a new dummy output object and returns it. GenerateData() will overwrite the outputs with specific types,

Parameters:
idxthe index of the output for which an object should be created
Returns:
the dummy object

Reimplemented from mitk::PlanarFigureSource.

Definition at line 55 of file mitkPlanarFigureReader.cpp.

References mitk::PlanarCircle::New().

Referenced by PlanarFigureReader().

{
  return static_cast<itk::DataObject*>(PlanarCircle::New().GetPointer()); // just as a stand in for the pipeline update mechanism. This will be overwritten in GenerateData()
}
static Pointer mitk::PlanarFigureReader::New (  ) [static]
void mitk::PlanarFigureReader::ResizeOutputs ( const unsigned int &  num ) [protected, virtual]

Resizes the output-objects according to the given number.

Parameters:
numthe new number of output objects.

Definition at line 389 of file mitkPlanarFigureReader.cpp.

{
  unsigned int prevNum = this->GetNumberOfOutputs();
  this->SetNumberOfOutputs( num );
  for ( unsigned int i = prevNum; i < num; ++i )
  {
    this->SetNthOutput( i, this->MakeOutput( i ).GetPointer() );
  }
}
virtual void mitk::PlanarFigureReader::SetFileName ( const char *  _arg ) [virtual]

Sets the filename of the file to be read.

Parameters:
_argthe filename of the point set xml-file

Implements mitk::FileReader.

virtual void mitk::PlanarFigureReader::SetFilePattern ( const char *  _arg ) [virtual]
Warning:
multiple load not (yet) supported

Implements mitk::FileReader.

virtual void mitk::PlanarFigureReader::SetFilePrefix ( const char *  _arg ) [virtual]
Warning:
multiple load not (yet) supported

Implements mitk::FileReader.


Member Data Documentation

std::string mitk::PlanarFigureReader::m_FileName [protected]

Definition at line 158 of file mitkPlanarFigureReader.h.

std::string mitk::PlanarFigureReader::m_FilePattern [protected]

Definition at line 160 of file mitkPlanarFigureReader.h.

std::string mitk::PlanarFigureReader::m_FilePrefix [protected]

Definition at line 159 of file mitkPlanarFigureReader.h.

Definition at line 161 of file mitkPlanarFigureReader.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines