diff --git a/Modules/IGT/IO/mitkNavigationDataReaderInterface.h b/Modules/IGT/IO/mitkNavigationDataReaderInterface.h index 49b430946a..09d7e6b183 100644 --- a/Modules/IGT/IO/mitkNavigationDataReaderInterface.h +++ b/Modules/IGT/IO/mitkNavigationDataReaderInterface.h @@ -1,44 +1,39 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef MITKNavigationDataReaderInterface_H_HEADER_INCLUDED_ #define MITKNavigationDataReaderInterface_H_HEADER_INCLUDED_ #include "itkObject.h" #include "mitkCommon.h" #include "mitkNavigationDataSet.h" namespace mitk { class MitkIGT_EXPORT NavigationDataReaderInterface : public itk::Object { public: mitkClassMacro(NavigationDataReaderInterface, itk::Object); - itkSetMacro(FileName, std::string); - itkGetMacro(FileName, std::string); - - virtual mitk::NavigationDataSet::Pointer Read() = 0; + virtual mitk::NavigationDataSet::Pointer Read(std::string filename) = 0; protected: NavigationDataReaderInterface(); virtual ~NavigationDataReaderInterface(); - - std::string m_FileName; }; } #endif // MITKNavigationDataReaderInterface_H_HEADER_INCLUDED_ diff --git a/Modules/IGT/IO/mitkNavigationDataReaderXML.cpp b/Modules/IGT/IO/mitkNavigationDataReaderXML.cpp index 8c1dc83032..924ba6b5ac 100644 --- a/Modules/IGT/IO/mitkNavigationDataReaderXML.cpp +++ b/Modules/IGT/IO/mitkNavigationDataReaderXML.cpp @@ -1,330 +1,337 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkNavigationDataReaderXML.h" #include #include #include "tinyxml.h" //includes for exceptions #include "mitkIGTException.h" #include "mitkIGTIOException.h" mitk::NavigationDataReaderXML::NavigationDataReaderXML() - : m_parentElement(0), m_currentNode(0), m_Stream(0) + : m_parentElement(0), m_currentNode(0) { } mitk::NavigationDataReaderXML::~NavigationDataReaderXML() { - delete m_currentNode; - delete m_parentElement; - if (!m_StreamSetOutsideFromClass) { delete m_Stream; } } -mitk::NavigationDataSet::Pointer mitk::NavigationDataReaderXML::Read() +mitk::NavigationDataSet::Pointer mitk::NavigationDataReaderXML::Read(std::string fileName) { - if ( !m_FileName.empty() ) + m_FileName = fileName; + + TiXmlDocument document; + if ( !document.LoadFile(fileName)) { - this->CreateStreamFromFilename(); + mitkThrowException(mitk::IGTIOException) << "File '"<QueryIntAttribute("Ver", &m_FileVersion); + if (m_FileVersion != 1) + { + mitkThrowException(mitk::IGTIOException) << "File format version "<QueryIntAttribute("ToolCount", &m_NumberOfOutputs); + + mitk::NavigationDataSet::Pointer navigationDataSet = this->ReadNavigationDataSet(); + return navigationDataSet; +} + +mitk::NavigationDataSet::Pointer mitk::NavigationDataReaderXML::Read(std::istream* stream) +{ // first get the file version - m_FileVersion = this->GetFileVersion(m_Stream); + m_FileVersion = this->GetFileVersion(stream); // check if we have a valid version: m_FileVersion has to be always bigger than 1 for playing if (m_FileVersion < 1) { StreamInvalid("Playing not possible. Invalid file version!"); return 0; } - m_NumberOfOutputs = this->GetNumberOfNavigationDatas(m_Stream); + m_NumberOfOutputs = this->GetNumberOfNavigationDatas(stream); if (m_NumberOfOutputs == 0) { return 0; } + return this->ReadNavigationDataSet(); +} + +mitk::NavigationDataSet::Pointer mitk::NavigationDataReaderXML::ReadNavigationDataSet() +{ mitk::NavigationDataSet::Pointer navigationDataSet = mitk::NavigationDataSet::New(m_NumberOfOutputs); mitk::NavigationData::Pointer curNavigationData; do - { - std::vector navDatas(m_NumberOfOutputs, NULL); - for (unsigned int n = 0; n < m_NumberOfOutputs; ++n) { - curNavigationData = this->ReadVersion1(); - - if (curNavigationData.IsNull()) + std::vector navDatas(m_NumberOfOutputs, NULL); + for (unsigned int n = 0; n < m_NumberOfOutputs; ++n) { - if (n == 0) + curNavigationData = this->ReadVersion1(); + + if (curNavigationData.IsNull()) { - MITK_ERROR("mitkNavigationDataReaderXML") - << "Different number of NaivigationData objects for different tools. Ignoring last ones."; + if (n == 0) + { + MITK_ERROR("mitkNavigationDataReaderXML") + << "Different number of NaivigationData objects for different tools. Ignoring last ones."; + } + break; } - break; + navDatas.at(n) = curNavigationData; } - navDatas.at(n) = curNavigationData; - } - if (curNavigationData.IsNotNull()) - { - navigationDataSet->AddNavigationDatas(navDatas); + if (curNavigationData.IsNotNull()) + { + navigationDataSet->AddNavigationDatas(navDatas); + } } - } - while (curNavigationData.IsNotNull()); + while (curNavigationData.IsNotNull()); - return navigationDataSet; + return navigationDataSet; } mitk::NavigationData::Pointer mitk::NavigationDataReaderXML::ReadVersion1() { - if (!m_parentElement) + if ( !m_parentElement ) { mitkThrowException(mitk::IGTIOException) << "Reading XML is not possible. Parent element is not set."; } TiXmlElement* elem; m_currentNode = m_parentElement->IterateChildren(m_currentNode); bool delElem; if(m_currentNode) { elem = m_currentNode->ToElement(); if(elem==NULL) { mitkThrowException(mitk::IGTException) << "Cannot find element: Is this file damaged?"; } delElem = false; } else { elem = new TiXmlElement(""); delElem = true; } mitk::NavigationData::Pointer nd = this->ReadNavigationData(elem); - if(delElem) - delete elem; + if(delElem) { delete elem; } return nd; } mitk::NavigationData::Pointer mitk::NavigationDataReaderXML::ReadNavigationData(TiXmlElement* elem) { if (elem == NULL) {mitkThrow() << "Error: Element is NULL!";} mitk::NavigationData::Pointer nd = mitk::NavigationData::New(); mitk::NavigationData::PositionType position; mitk::NavigationData::OrientationType orientation(0.0,0.0,0.0,0.0); mitk::NavigationData::TimeStampType timestamp = -1; mitk::NavigationData::CovarianceMatrixType matrix; bool hasPosition = true; bool hasOrientation = true; bool dataValid = false; position.Fill(0.0); matrix.SetIdentity(); elem->QueryDoubleAttribute("Time",×tamp); if (timestamp == -1) { return NULL; //the calling method should check the return value if it is valid/not NULL } elem->QueryDoubleAttribute("X", &position[0]); elem->QueryDoubleAttribute("Y", &position[1]); elem->QueryDoubleAttribute("Z", &position[2]); elem->QueryDoubleAttribute("QX", &orientation[0]); elem->QueryDoubleAttribute("QY", &orientation[1]); elem->QueryDoubleAttribute("QZ", &orientation[2]); elem->QueryDoubleAttribute("QR", &orientation[3]); elem->QueryDoubleAttribute("C00", &matrix[0][0]); elem->QueryDoubleAttribute("C01", &matrix[0][1]); elem->QueryDoubleAttribute("C02", &matrix[0][2]); elem->QueryDoubleAttribute("C03", &matrix[0][3]); elem->QueryDoubleAttribute("C04", &matrix[0][4]); elem->QueryDoubleAttribute("C05", &matrix[0][5]); elem->QueryDoubleAttribute("C10", &matrix[1][0]); elem->QueryDoubleAttribute("C11", &matrix[1][1]); elem->QueryDoubleAttribute("C12", &matrix[1][2]); elem->QueryDoubleAttribute("C13", &matrix[1][3]); elem->QueryDoubleAttribute("C14", &matrix[1][4]); elem->QueryDoubleAttribute("C15", &matrix[1][5]); int tmpval = 0; elem->QueryIntAttribute("Valid", &tmpval); if (tmpval == 0) dataValid = false; else dataValid = true; tmpval = 0; elem->QueryIntAttribute("hO", &tmpval); if (tmpval == 0) hasOrientation = false; else hasOrientation = true; tmpval = 0; elem->QueryIntAttribute("hP", &tmpval); if (tmpval == 0) hasPosition = false; else hasPosition = true; nd->SetIGTTimeStamp(timestamp); nd->SetPosition(position); nd->SetOrientation(orientation); nd->SetCovErrorMatrix(matrix); nd->SetDataValid(dataValid); nd->SetHasOrientation(hasOrientation); nd->SetHasPosition(hasPosition); return nd; } // -- deprecated | begin -void mitk::NavigationDataReaderXML::CreateStreamFromFilename() -{ - m_Stream = NULL; - - if (!itksys::SystemTools::FileExists(m_FileName.c_str())) - { - mitkThrowException(mitk::IGTIOException) << "File does not exist!"; - } - - m_Stream = new std::ifstream(m_FileName.c_str()); - m_StreamSetOutsideFromClass = false; - - this->Modified(); -} - unsigned int mitk::NavigationDataReaderXML::GetFileVersion(std::istream* stream) { if (stream==NULL) { MITK_ERROR << "No input stream set!"; - mitkThrowException(mitk::IGTException)<<"No input stream set!"; + mitkThrowException(mitk::IGTIOException)<<"No input stream set!"; } if (!stream->good()) { MITK_ERROR << "Stream is not good!"; - mitkThrowException(mitk::IGTException)<<"Stream is not good!"; + mitkThrowException(mitk::IGTIOException)<<"Stream is not good!"; } int version = 1; TiXmlDeclaration* dec = new TiXmlDeclaration(); *stream >> *dec; if(strcmp(dec->Version(),"") == 0) { MITK_ERROR << "The input stream seems to have XML incompatible format"; mitkThrowException(mitk::IGTIOException) << "The input stream seems to have XML incompatible format"; } m_parentElement = new TiXmlElement(""); *stream >> *m_parentElement; //2nd line this is the file version std::string tempValue = m_parentElement->Value(); if(tempValue != "Version") { if(tempValue == "Data"){ m_parentElement->QueryIntAttribute("version",&version); } } else { m_parentElement->QueryIntAttribute("Ver",&version); } if (version > 0) { return version; } else { return 0; } } unsigned int mitk::NavigationDataReaderXML::GetNumberOfNavigationDatas(std::istream* stream) { if (stream == NULL) { MITK_ERROR << "No input stream set!"; mitkThrowException(mitk::IGTException)<<"No input stream set!"; } if (!stream->good()) { MITK_ERROR << "Stream not good!"; mitkThrowException(mitk::IGTException)<<"Stream not good!"; } //If something has changed in a future version of the XML definition e.g. navigationcount or addional parameters //catch this here with a select case block (see GenerateData() method) int numberOfTools = 0; std::string tempValue = m_parentElement->Value(); if(tempValue == "Version"){ *stream >> *m_parentElement; } m_parentElement->QueryIntAttribute("ToolCount",&numberOfTools); if (numberOfTools > 0) { return numberOfTools; } return 0; } -void mitk::NavigationDataReaderXML::SetStream( std::istream* stream ) +/*void mitk::NavigationDataReaderXML::SetStream( std::istream* stream ) { if ( (stream == NULL) || (!stream->good())) { // throw an exception for stream=NULL or it is not good mitkThrowException(mitk::IGTException) << "The stream is NULL or it is not good"; m_StreamEnd = true; return; } if (m_StreamSetOutsideFromClass) { delete m_Stream; } m_Stream = stream; m_StreamSetOutsideFromClass = true; this->Modified(); -} +}*/ void mitk::NavigationDataReaderXML::StreamInvalid(std::string message) { m_StreamEnd = true; m_ErrorMessage = message; m_StreamValid = false; mitkThrowException(mitk::IGTIOException) << "Invalid stream!"; } // -- deprecated | end diff --git a/Modules/IGT/IO/mitkNavigationDataReaderXML.h b/Modules/IGT/IO/mitkNavigationDataReaderXML.h index 45774175d6..3cdf561fe3 100644 --- a/Modules/IGT/IO/mitkNavigationDataReaderXML.h +++ b/Modules/IGT/IO/mitkNavigationDataReaderXML.h @@ -1,105 +1,109 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef MITKNavigationDataReaderXML_H_HEADER_INCLUDED_ #define MITKNavigationDataReaderXML_H_HEADER_INCLUDED_ #include "mitkNavigationDataReaderInterface.h" class TiXmlElement; class TiXmlNode; namespace mitk { class MitkIGT_EXPORT NavigationDataReaderXML : public NavigationDataReaderInterface { public: mitkClassMacro(NavigationDataReaderXML, NavigationDataReaderInterface); itkNewMacro(Self); - virtual mitk::NavigationDataSet::Pointer Read(); + virtual mitk::NavigationDataSet::Pointer Read(std::string fileName); + virtual mitk::NavigationDataSet::Pointer Read(std::istream* stream); // -- deprecated | begin /** * \brief Sets the stream of this player. * @throw mitk::IGTException Throws an exception if stream is NULL or if it is not good. * \deprecated Will be removed in one of the next releases. Use SetFileName() instead. */ - void SetStream(std::istream* stream); + //void SetStream(std::istream* stream); // -- deprecated | end protected: NavigationDataReaderXML(); virtual ~NavigationDataReaderXML(); + NavigationDataSet::Pointer ReadNavigationDataSet(); + /** * \brief This method reads one line of the XML document and returns the data as a NavigationData object * If there is a new file version another method must be added which reads this data. * @throw mitk::IGTException Throws an exceptions if file is damaged. */ mitk::NavigationData::Pointer ReadVersion1(); mitk::NavigationData::Pointer ReadNavigationData(TiXmlElement* elem); + std::string m_FileName; + TiXmlElement* m_parentElement; TiXmlNode* m_currentNode; - unsigned int m_FileVersion; ///< indicates which XML encoding is used - unsigned int m_NumberOfOutputs; ///< stores the number of outputs known from the XML document + int m_FileVersion; ///< indicates which XML encoding is used + int m_NumberOfOutputs; ///< stores the number of outputs known from the XML document // -- deprecated | begin - std::istream* m_Stream; ///< stores a pointer to the input stream - bool m_StreamSetOutsideFromClass; ///< stores if the stream was created in this class and must be deleted in the end + //std::istream* m_Stream; ///< stores a pointer to the input stream bool m_StreamEnd; ///< stores if the input stream arrived at end bool m_StreamValid; ///< stores if the input stream is valid or not std::string m_ErrorMessage; ///< stores the error message if the stream is invalid /** * \brief Creates a stream out of the filename given by the variable m_FileName. * The stream is then set to m_Stream. * * @throw mitk::IGTIOException Throws an exception if file does not exist * @throw mitk::IGTException Throws an exception if the stream is NULL */ - void CreateStreamFromFilename(); + //void CreateStreamFromFilename(); /** * \brief Returns the file version out of the XML document. * @throw mitk::IGTException Throws an mitk::IGTException an exception if stream is NULL or not good. * @throw mitk::IGTIOException Throws an mitk::IGTIOException if the stream has an incompatible XML format. */ unsigned int GetFileVersion(std::istream* stream); /** * \brief Returns the number of tracked tools out of the XML document. * @throw Throws an exception if stream is NULL. * @throw Throws an exception if the input stream has an XML incompatible format. */ unsigned int GetNumberOfNavigationDatas(std::istream* stream); /** * @brief This is a helping method which gives an error message and throws an exception with the given message. * It can be used if a stream is found to be invalid. * * @throw mitk::IGTIOException Always throws an exception. */ void StreamInvalid(std::string message); ///< help method which sets the stream invalid and displays an error // -- deprecated | end }; } // namespace mitk #endif // MITKNavigationDataReaderXML_H_HEADER_INCLUDED_