diff --git a/Modules/IGTBase/autoload/IO/CMakeLists.txt b/Modules/IGTBase/autoload/IO/CMakeLists.txt index 8b4886b3d7..cd68a6b910 100644 --- a/Modules/IGTBase/autoload/IO/CMakeLists.txt +++ b/Modules/IGTBase/autoload/IO/CMakeLists.txt @@ -1,4 +1,5 @@ -MITK_CREATE_MODULE(DEPENDS MitkIGTBase +MITK_CREATE_MODULE(IGTIO + DEPENDS MitkIGTBase WARNINGS_AS_ERRORS AUTOLOAD_WITH MitkCore ) \ No newline at end of file diff --git a/Modules/IGTBase/autoload/IO/mitkNavigationDataSetWriterCSV.cpp b/Modules/IGTBase/autoload/IO/mitkNavigationDataSetWriterCSV.cpp index 5be4706fe9..5132691d87 100644 --- a/Modules/IGTBase/autoload/IO/mitkNavigationDataSetWriterCSV.cpp +++ b/Modules/IGTBase/autoload/IO/mitkNavigationDataSetWriterCSV.cpp @@ -1,83 +1,83 @@ /*=================================================================== 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 "mitkNavigationDataSetWriterCSV.h" #include mitk::NavigationDataSetWriterCSV* mitk::NavigationDataSetWriterCSV::Clone() const { return new NavigationDataSetWriterCSV(*this); } void mitk::NavigationDataSetWriterCSV::Write() { std::ostream* out = GetOutputStream(); mitk::NavigationDataSet::ConstPointer data = dynamic_cast (this->GetInput()); //save old locale char * oldLocale; oldLocale = setlocale( LC_ALL, nullptr ); //define own locale std::locale C("C"); setlocale( LC_ALL, "C" ); //write header unsigned int numberOfTools = data->GetNumberOfTools(); - for (unsigned int index = 0; index < numberOfTools; index++){ *stream << "TimeStamp_Tool" << index << + for (unsigned int index = 0; index < numberOfTools; index++){ *out << "TimeStamp_Tool" << index << ";Valid_Tool" << index << ";X_Tool" << index << ";Y_Tool" << index << ";Z_Tool" << index << ";QX_Tool" << index << ";QY_Tool" << index << ";QZ_Tool" << index << ";QR_Tool" << index << ";";} - *stream << "\n"; + *out << "\n"; - stream->precision(15); // rounding precision because we don't want to loose data. + out->precision(15); // rounding precision because we don't want to loose data. //write data MITK_INFO << "Number of timesteps: " << data->Size(); for (unsigned int i=0; iSize(); i++) { std::vector< mitk::NavigationData::Pointer > NavigationDatasOfCurrentStep = data->GetTimeStep(i); for (unsigned int toolIndex = 0; toolIndex < numberOfTools; toolIndex++) { mitk::NavigationData::Pointer nd = NavigationDatasOfCurrentStep.at(toolIndex); - *stream << nd->GetTimeStamp() << ";" + *out << nd->GetTimeStamp() << ";" << nd->IsDataValid() << ";" << nd->GetPosition()[0] << ";" << nd->GetPosition()[1] << ";" << nd->GetPosition()[2] << ";" << nd->GetOrientation()[0] << ";" << nd->GetOrientation()[1] << ";" << nd->GetOrientation()[2] << ";" << nd->GetOrientation()[3] << ";"; } - *stream << "\n"; + *out << "\n"; } //switch back to old locale setlocale( LC_ALL, oldLocale ); } mitk::NavigationDataSetWriterCSV::NavigationDataSetWriterCSV() {} mitk::NavigationDataSetWriterCSV::~NavigationDataSetWriterCSV() {} diff --git a/Modules/IGTBase/autoload/IO/mitkNavigationDataSetWriterXML.cpp b/Modules/IGTBase/autoload/IO/mitkNavigationDataSetWriterXML.cpp index 03562b4115..800b9b2510 100644 --- a/Modules/IGTBase/autoload/IO/mitkNavigationDataSetWriterXML.cpp +++ b/Modules/IGTBase/autoload/IO/mitkNavigationDataSetWriterXML.cpp @@ -1,135 +1,135 @@ /*=================================================================== 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. ===================================================================*/ // MITK #include "mitkNavigationDataSetWriterXML.h" // Third Party #include #include #include #include mitk::NavigationDataSetWriterXML::NavigationDataSetWriterXML() { } mitk::NavigationDataSetWriterXML::~NavigationDataSetWriterXML() { } mitk::NavigationDataSetWriterXML* mitk::NavigationDataSetWriterXML::Clone() const { return new NavigationDataSetWriterXML(*this); } void mitk::NavigationDataSetWriterXML::Write() { std::ostream* out = GetOutputStream(); mitk::NavigationDataSet::ConstPointer data = dynamic_cast (this->GetInput()); //save old locale char * oldLocale; oldLocale = setlocale( LC_ALL, nullptr ); - StreamHeader(stream, data); - StreamData(stream, data); - StreamFooter(stream); + StreamHeader(out, data); + StreamData(out, data); + StreamFooter(out); // Cleanup - stream->flush(); + out->flush(); //switch back to old locale setlocale( LC_ALL, oldLocale ); } void mitk::NavigationDataSetWriterXML::StreamHeader (std::ostream* stream, mitk::NavigationDataSet::ConstPointer data) { stream->precision(10); //TODO store date and GMT time //checking if the stream is good if (stream->good()) { *stream << "" << std::endl; /**m_Stream << "" << std::endl;*/ // should be a generic version, meaning a member variable, which has the actual version *stream << " " << "GetNumberOfTools() << "\" version=\"1.0\">" << std::endl; } } void mitk::NavigationDataSetWriterXML::StreamData (std::ostream* stream, mitk::NavigationDataSet::ConstPointer data) { // For each time step in the Dataset for (auto it = data->Begin(); it != data->End(); it++) { for (std::size_t toolIndex = 0; toolIndex < it->size(); toolIndex++) { mitk::NavigationData::Pointer nd = it->at(toolIndex); auto elem = new TiXmlElement("ND"); elem->SetDoubleAttribute("Time", nd->GetIGTTimeStamp()); // elem->SetAttribute("SystemTime", sysTimeStr); // tag for system time elem->SetDoubleAttribute("Tool", toolIndex); elem->SetDoubleAttribute("X", nd->GetPosition()[0]); elem->SetDoubleAttribute("Y", nd->GetPosition()[1]); elem->SetDoubleAttribute("Z", nd->GetPosition()[2]); elem->SetDoubleAttribute("QX", nd->GetOrientation()[0]); elem->SetDoubleAttribute("QY", nd->GetOrientation()[1]); elem->SetDoubleAttribute("QZ", nd->GetOrientation()[2]); elem->SetDoubleAttribute("QR", nd->GetOrientation()[3]); elem->SetDoubleAttribute("C00", nd->GetCovErrorMatrix()[0][0]); elem->SetDoubleAttribute("C01", nd->GetCovErrorMatrix()[0][1]); elem->SetDoubleAttribute("C02", nd->GetCovErrorMatrix()[0][2]); elem->SetDoubleAttribute("C03", nd->GetCovErrorMatrix()[0][3]); elem->SetDoubleAttribute("C04", nd->GetCovErrorMatrix()[0][4]); elem->SetDoubleAttribute("C05", nd->GetCovErrorMatrix()[0][5]); elem->SetDoubleAttribute("C10", nd->GetCovErrorMatrix()[1][0]); elem->SetDoubleAttribute("C11", nd->GetCovErrorMatrix()[1][1]); elem->SetDoubleAttribute("C12", nd->GetCovErrorMatrix()[1][2]); elem->SetDoubleAttribute("C13", nd->GetCovErrorMatrix()[1][3]); elem->SetDoubleAttribute("C14", nd->GetCovErrorMatrix()[1][4]); elem->SetDoubleAttribute("C15", nd->GetCovErrorMatrix()[1][5]); if (nd->IsDataValid()) elem->SetAttribute("Valid",1); else elem->SetAttribute("Valid",0); if (nd->GetHasOrientation()) elem->SetAttribute("hO",1); else elem->SetAttribute("hO",0); if (nd->GetHasPosition()) elem->SetAttribute("hP",1); else elem->SetAttribute("hP",0); *stream << " " << *elem << std::endl; delete elem; } } } void mitk::NavigationDataSetWriterXML::StreamFooter (std::ostream* stream) { *stream << "" << std::endl; } diff --git a/Modules/IGTBase/src/mitkNavigationDataSet.cpp b/Modules/IGTBase/src/mitkNavigationDataSet.cpp index 917194ed95..ece2fca00a 100644 --- a/Modules/IGTBase/src/mitkNavigationDataSet.cpp +++ b/Modules/IGTBase/src/mitkNavigationDataSet.cpp @@ -1,161 +1,161 @@ /*=================================================================== 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 "mitkNavigationDataSet.h" mitk::NavigationDataSet::NavigationDataSet( unsigned int numberOfTools ) : m_NavigationDataVectors(std::vector >()), m_NumberOfTools(numberOfTools) { } mitk::NavigationDataSet::~NavigationDataSet( ) { } bool mitk::NavigationDataSet::AddNavigationDatas( std::vector navigationDatas ) { // test if tool with given index exist if ( navigationDatas.size() != m_NumberOfTools ) { MITK_WARN("NavigationDataSet") << "Tried to add too many or too few navigation Datas to NavigationDataSet. " << m_NumberOfTools << " required, tried to add " << navigationDatas.size() << "."; return false; } // test for consistent timestamp if ( m_NavigationDataVectors.size() > 0) { for (int i = 0; i < navigationDatas.size(); i++) if (navigationDatas[i]->GetIGTTimeStamp() <= m_NavigationDataVectors.back()[i]->GetIGTTimeStamp()) { MITK_WARN("NavigationDataSet") << "IGTTimeStamp of new NavigationData should be newer than timestamp of last NavigationData."; return false; } } m_NavigationDataVectors.push_back(navigationDatas); return true; } mitk::NavigationData::Pointer mitk::NavigationDataSet::GetNavigationDataForIndex( unsigned int index, unsigned int toolIndex ) const { if ( index >= m_NavigationDataVectors.size() ) { MITK_WARN("NavigationDataSet") << "There is no NavigationData available at index " << index << "."; return nullptr; } if ( toolIndex >= m_NavigationDataVectors.at(index).size() ) { MITK_WARN("NavigationDataSet") << "There is NavigatitionData available at index " << index << " for tool " << toolIndex << "."; return nullptr; } return m_NavigationDataVectors.at(index).at(toolIndex); } // Method not yet supported, code below compiles but delivers wrong results //mitk::NavigationData::Pointer mitk::NavigationDataSet::GetNavigationDataBeforeTimestamp( // mitk::NavigationData::TimeStampType timestamp, unsigned int toolIndex) const //{ // if ( toolIndex >= m_NavigationDataVectors.size() ) // { // MITK_WARN("NavigationDataSet") << "There is no tool with index " << toolIndex << "."; // return NULL; // } // // std::vector::const_iterator it; // // // iterate through all NavigationData objects of the given tool index // // till the timestamp of the NavigationData is greater then the given timestamp // for (it = m_NavigationDataVectors.at(toolIndex).begin(); // it != m_NavigationDataVectors.at(toolIndex).end(); ++it) // { // if ( (*it)->GetIGTTimeStamp() > timestamp) { break; } // } // // // first element was greater than timestamp -> return null // if ( it == m_NavigationDataVectors.at(toolIndex).begin() ) // { // MITK_WARN("NavigationDataSet") << "No NavigationData was recorded before given timestamp."; // return NULL; // } // // // return last element smaller than the given timestamp // return *(it-1); //} std::vector< mitk::NavigationData::Pointer > mitk::NavigationDataSet::GetDataStreamForTool(unsigned int toolIndex) { if (toolIndex >= m_NumberOfTools ) { MITK_WARN("NavigationDataSet") << "Invalid toolIndex: " << m_NumberOfTools << " Tools known, requested index " << toolIndex << ""; return std::vector(); } std::vector< mitk::NavigationData::Pointer > result; for(int i = 0; i < m_NavigationDataVectors.size(); i++) result.push_back(m_NavigationDataVectors[i][toolIndex]); return result; } -std::vector< mitk::NavigationData::Pointer > mitk::NavigationDataSet::GetTimeStep(unsigned int index) +std::vector< mitk::NavigationData::Pointer > mitk::NavigationDataSet::GetTimeStep(unsigned int index) const { return m_NavigationDataVectors[index]; } unsigned int mitk::NavigationDataSet::GetNumberOfTools() const { return m_NumberOfTools; } unsigned int mitk::NavigationDataSet::Size() const { return m_NavigationDataVectors.size(); } // ---> methods necessary for BaseData void mitk::NavigationDataSet::SetRequestedRegionToLargestPossibleRegion() { } bool mitk::NavigationDataSet::RequestedRegionIsOutsideOfTheBufferedRegion() { return false; } bool mitk::NavigationDataSet::VerifyRequestedRegion() { return true; } void mitk::NavigationDataSet::SetRequestedRegion(const DataObject * ) { } // <--- methods necessary for BaseData // ---> methods for Iterators mitk::NavigationDataSet::NavigationDataSetIterator mitk::NavigationDataSet::Begin() { return m_NavigationDataVectors.begin(); } mitk::NavigationDataSet::NavigationDataSetIterator mitk::NavigationDataSet::End() { return m_NavigationDataVectors.end(); } diff --git a/Modules/IGTBase/src/mitkNavigationDataSet.h b/Modules/IGTBase/src/mitkNavigationDataSet.h new file mode 100644 index 0000000000..e4c1ced2d3 --- /dev/null +++ b/Modules/IGTBase/src/mitkNavigationDataSet.h @@ -0,0 +1,158 @@ +/*=================================================================== + +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 MITKNAVIGATIONDATASET_H_HEADER_INCLUDED_ +#define MITKNAVIGATIONDATASET_H_HEADER_INCLUDED_ + +#include +#include "mitkBaseData.h" +#include "mitkNavigationData.h" + +namespace mitk { + /** + * \brief Data structure which stores streams of mitk::NavigationData for + * multiple tools. + * + * Use mitk::NavigationDataRecorder to create these sets easily from pipelines. + * Use mitk::NavigationDataPlayer to stream from these sets easily. + * + */ + class MITKIGTBASE_EXPORT NavigationDataSet : public BaseData + { + public: + + /** + * \brief This iterator iterates over the distinct time steps in this set. + * + * It returns an array of the length equal to GetNumberOfTools(), containing a + * mitk::NavigationData for each tool.. + */ + typedef std::vector< std::vector >::iterator NavigationDataSetIterator; + + mitkClassMacro(NavigationDataSet, BaseData); + + mitkNewMacro1Param(Self, unsigned int); + + /** + * \brief Add mitk::NavigationData of the given tool to the Set. + * + * @param navigationDatas vector of mitk::NavigationData objects to be added. Make sure that the size of the + * vector equals the number of tools given in the constructor + * @return true if object was be added to the set successfully, false otherwise + */ + bool AddNavigationDatas( std::vector navigationDatas ); + + /** + * \brief Get mitk::NavigationData from the given tool at given index. + * + * @param toolIndex Index of the tool from which mitk::NavigationData should be returned. + * @param index Index of the mitk::NavigationData object that should be returned. + * @return mitk::NavigationData at the specified indices, 0 if there is no object at the indices. + */ + NavigationData::Pointer GetNavigationDataForIndex( unsigned int index, unsigned int toolIndex ) const; + + ///** + //* \brief Get last mitk::Navigation object for given tool whose timestamp is less than the given timestamp. + //* @param toolIndex Index of the tool from which mitk::NavigationData should be returned. + //* @param timestamp Timestamp for selecting last object before. + //* @return Last mitk::NavigationData with timestamp less than given timestamp, 0 if there is no adequate object. + //*/ + // Method not yet supported! + //NavigationData::Pointer GetNavigationDataBeforeTimestamp( mitk::NavigationData::TimeStampType timestamp , unsigned int toolIndex ) const; + + /** + * \brief Returns a vector that contains all tracking data for a given tool. + * + * This is a relatively expensive operation, as it requires the construction of a new vector. + * + * @param toolIndex Index of the tool for which the stream should be returned. + * @return Returns a vector that contains all tracking data for a given tool. + */ + virtual std::vector< mitk::NavigationData::Pointer > GetDataStreamForTool(unsigned int toolIndex); + + /** + * \brief Returns a vector that contains NavigationDatas for each tool for a given timestep. + * + * If GetNumberOFTools() equals four, then 4 NavigationDatas will be returned. + * + * @param index Index of the timeStep for which the datas should be returned. cannot be larger than mitk::NavigationDataSet::Size() + * @return Returns a vector that contains all tracking data for a given tool. + */ + virtual std::vector< mitk::NavigationData::Pointer > GetTimeStep(unsigned int index) const; + + /** + * \brief Returns the number of tools for which NavigationDatas are stored in this set. + * + * This is always equal to the number given in the constructor of this class. + * + * @return the number of tools for which NavigationDatas are stored in this set. + */ + unsigned int GetNumberOfTools() const; + + /** + * \brief Returns the number of time steps stored in this NavigationDataSet. + * + * This is not the total number of Navigation Datas stored in this set, but the number stored for each tool. + * i.e. the total number of NavigationDatas equals Size() * GetNumberOfTools(); + * + * @return Returns the number of time steps stored in this NavigationDataSet. + */ + unsigned int Size() const; + + /** + * \brief Returns an iterator pointing to the first TimeStep. + * + * @return Returns an iterator pointing to the first TimeStep. + */ + virtual NavigationDataSetIterator Begin(); + + /** + * \brief Returns an iterator pointing behind to the last TimeStep. + * + * @return Returns an iterator pointing behind to the last TimeStep. + */ + virtual NavigationDataSetIterator End(); + + // virtual methods, that need to be implemented, but aren't reasonable for NavigationData + virtual void SetRequestedRegionToLargestPossibleRegion( ) override; + virtual bool RequestedRegionIsOutsideOfTheBufferedRegion( ) override; + virtual bool VerifyRequestedRegion( ) override; + virtual void SetRequestedRegion( const itk::DataObject *data ) override; + + protected: + /** + * \brief Constructs set with fixed number of tools. + * @param numTools How many tools are used with this mitk::NavigationDataSet. + */ + NavigationDataSet( unsigned int numTools ); + virtual ~NavigationDataSet( ); + + /** + * \brief Holds all the mitk::NavigationData objects managed by this class. + * + * The first dimension is the index of the navigation data, the second is the + * tool to which this data belongs. i.e. the first dimension is usually the longer one. + */ + std::vector > m_NavigationDataVectors; + + /** + * \brief The Number of Tools that this class is going to support. + */ + int m_NumberOfTools; + }; +} + +#endif // MITKNAVIGATIONDATASET_H_HEADER_INCLUDED_