diff --git a/Modules/IGT/IO/mitkNavigationToolReader.cpp b/Modules/IGT/IO/mitkNavigationToolReader.cpp index 0db87ddf5c..cf30e6a5de 100644 --- a/Modules/IGT/IO/mitkNavigationToolReader.cpp +++ b/Modules/IGT/IO/mitkNavigationToolReader.cpp @@ -1,224 +1,220 @@ /*=================================================================== 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. ===================================================================*/ //Poco headers -#include "Poco/Zip/Decompress.h" -#include "Poco/Path.h" +#include <Poco/Zip/Decompress.h> +#include <Poco/Path.h> //mitk headers #include "mitkNavigationToolReader.h" #include "mitkTrackingTypes.h" -#include <mitkStandardFileLocations.h> +#include <mitkIOUtil.h> #include <mitkSceneIO.h> - - mitk::NavigationToolReader::NavigationToolReader() { - + //TODO: maybe replace program path by a valid temp directory if there is one. See bug 17310 for the current problems. + m_ToolfilePath = mitk::IOUtil::GetProgramPath() + Poco::Path::separator() + "IGT_Toolfiles" + Poco::Path::separator(); } mitk::NavigationToolReader::~NavigationToolReader() { } mitk::NavigationTool::Pointer mitk::NavigationToolReader::DoRead(std::string filename) { //decompress all files into a temporary directory std::ifstream file( filename.c_str(), std::ios::binary ); if (!file.good()) { m_ErrorMessage = "Cannot open '" + filename + "' for reading"; return NULL; } - std::string tempDirectory = mitk::StandardFileLocations::GetInstance()->GetOptionDirectory() + Poco::Path::separator() + "toolFilesByNavigationToolReader" + Poco::Path::separator() + GetFileWithoutPath(filename); + std::string tempDirectory = m_ToolfilePath + GetFileWithoutPath(filename); Poco::Zip::Decompress unzipper( file, Poco::Path( tempDirectory ) ); unzipper.decompressAllFiles(); //use SceneSerialization to load the DataStorage mitk::SceneIO::Pointer mySceneIO = mitk::SceneIO::New(); mitk::DataStorage::Pointer loadedStorage = mySceneIO->LoadScene(tempDirectory + Poco::Path::separator() + GetFileWithoutPath(filename) + ".storage"); if (loadedStorage->GetAll()->size()==0 || loadedStorage.IsNull()) { m_ErrorMessage = "Invalid file: cannot parse tool data."; return NULL; } //convert the DataStorage back to a NavigationTool-Object mitk::DataNode::Pointer myNode = loadedStorage->GetAll()->ElementAt(0); mitk::NavigationTool::Pointer returnValue = ConvertDataNodeToNavigationTool(myNode, tempDirectory); //delete the data-storage file which is not needed any more. The toolfile must be left in the temporary directory becauses it is linked in the datatreenode of the tool std::remove((std::string(tempDirectory + Poco::Path::separator() + GetFileWithoutPath(filename) + ".storage")).c_str()); return returnValue; } mitk::NavigationTool::Pointer mitk::NavigationToolReader::ConvertDataNodeToNavigationTool(mitk::DataNode::Pointer node, std::string toolPath) { mitk::NavigationTool::Pointer returnValue = mitk::NavigationTool::New(); //DateTreeNode with Name and Surface mitk::DataNode::Pointer newNode = mitk::DataNode::New(); newNode->SetName(node->GetName()); newNode->SetData(node->GetData()); returnValue->SetDataNode(newNode); //Identifier std::string identifier; node->GetStringProperty("identifier",identifier); returnValue->SetIdentifier(identifier); //Serial Number std::string serial; node->GetStringProperty("serial number",serial); returnValue->SetSerialNumber(serial); //Tracking Device int device_type; node->GetIntProperty("tracking device type",device_type); returnValue->SetTrackingDeviceType(static_cast<mitk::TrackingDeviceType>(device_type)); //Tool Type int type; node->GetIntProperty("tracking tool type",type); returnValue->SetType(static_cast<mitk::NavigationTool::NavigationToolType>(type)); //Calibration File Name std::string calibration_filename; node->GetStringProperty("toolfileName",calibration_filename); if (calibration_filename=="none") { returnValue->SetCalibrationFile("none"); } else { std::string calibration_filename_with_path = toolPath + Poco::Path::separator() + calibration_filename; returnValue->SetCalibrationFile(calibration_filename_with_path); } //Tool Landmarks mitk::PointSet::Pointer ToolRegLandmarks = mitk::PointSet::New(); mitk::PointSet::Pointer ToolCalLandmarks = mitk::PointSet::New(); std::string RegLandmarksString; std::string CalLandmarksString; node->GetStringProperty("ToolRegistrationLandmarks",RegLandmarksString); node->GetStringProperty("ToolCalibrationLandmarks",CalLandmarksString); ToolRegLandmarks = ConvertStringToPointSet(RegLandmarksString); ToolCalLandmarks = ConvertStringToPointSet(CalLandmarksString); returnValue->SetToolRegistrationLandmarks(ToolRegLandmarks); returnValue->SetToolCalibrationLandmarks(ToolCalLandmarks); //Tool Tip std::string toolTipPositionString; std::string toolTipOrientationString; bool positionSet = node->GetStringProperty("ToolTipPosition",toolTipPositionString); bool orientationSet = node->GetStringProperty("ToolTipOrientation",toolTipOrientationString); if(positionSet && orientationSet) //only define tooltip if it is set { returnValue->SetToolTipPosition(ConvertStringToPoint(toolTipPositionString)); returnValue->SetToolTipOrientation(ConvertStringToQuaternion(toolTipOrientationString)); } else if(positionSet != orientationSet) { MITK_WARN << "Tooltip definition incomplete: position and orientation have to be set! Skipping tooltip definition."; } return returnValue; } std::string mitk::NavigationToolReader::GetFileWithoutPath(std::string FileWithPath) { - std::string returnValue = ""; - returnValue = FileWithPath.substr(FileWithPath.rfind("/")+1, FileWithPath.length()); - //dirty hack: Windows path seperators - if (returnValue.size() == FileWithPath.size()) returnValue = FileWithPath.substr(FileWithPath.rfind("\\")+1, FileWithPath.length()); - return returnValue; + Poco::Path myFile(FileWithPath.c_str()); + return myFile.getFileName(); } mitk::PointSet::Pointer mitk::NavigationToolReader::ConvertStringToPointSet(std::string string) { mitk::PointSet::Pointer returnValue = mitk::PointSet::New(); std::string pointSeperator = "|"; std::string valueSeperator = ";"; std::vector<std::string> points; split(string,pointSeperator,points); for(unsigned int i=0; i<points.size(); i++) { std::vector<std::string> values; split(points.at(i),valueSeperator,values); if (values.size() == 4) { double index = atof(values.at(0).c_str()); mitk::Point3D point; point[0] = atof(values.at(1).c_str()); point[1] = atof(values.at(2).c_str()); point[2] = atof(values.at(3).c_str()); returnValue->SetPoint(index,point); } } return returnValue; } mitk::Point3D mitk::NavigationToolReader::ConvertStringToPoint(std::string string) { std::string valueSeperator = ";"; std::vector<std::string> values; split(string,valueSeperator,values); mitk::Point3D point; if (values.size() == 3) { point[0] = atof(values.at(0).c_str()); point[1] = atof(values.at(1).c_str()); point[2] = atof(values.at(2).c_str()); } return point; } mitk::Quaternion mitk::NavigationToolReader::ConvertStringToQuaternion(std::string string) { std::string valueSeperator = ";"; std::vector<std::string> values; split(string,valueSeperator,values); mitk::Quaternion quat = mitk::Quaternion(0,0,0,1); if (values.size() == 4) { quat = mitk::Quaternion(atof(values.at(0).c_str()), atof(values.at(1).c_str()), atof(values.at(2).c_str()), atof(values.at(3).c_str())); } return quat; } void mitk::NavigationToolReader::split(std::string& text, std::string& separators, std::vector<std::string>& words) { int n = text.length(); int start, stop; start = text.find_first_not_of(separators); while ((start >= 0) && (start < n)) { stop = text.find_first_of(separators, start); if ((stop < 0) || (stop > n)) stop = n; words.push_back(text.substr(start, stop - start)); start = text.find_first_not_of(separators, stop+1); } } diff --git a/Modules/IGT/IO/mitkNavigationToolReader.h b/Modules/IGT/IO/mitkNavigationToolReader.h index 9cac1d51b8..af3a53c96f 100644 --- a/Modules/IGT/IO/mitkNavigationToolReader.h +++ b/Modules/IGT/IO/mitkNavigationToolReader.h @@ -1,74 +1,76 @@ /*=================================================================== 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 NAVIGATIONTOOLREADER_H_INCLUDED #define NAVIGATIONTOOLREADER_H_INCLUDED //itk headers #include <itkObjectFactory.h> //mitk headers #include <mitkCommon.h> #include "mitkNavigationTool.h" #include "mitkDataStorage.h" #include "mitkNavigationToolStorageDeserializer.h" #include <MitkIGTExports.h> namespace mitk { /**Documentation * \brief This class offers methods to read objects of the class NavigationTool from the * harddisc. The tools have to be saved in a special format by the class NavigationToolWriter * to be loadable. * * \ingroup IGT */ class MitkIGT_EXPORT NavigationToolReader : public itk::Object { friend class mitk::NavigationToolStorageDeserializer; public: mitkClassMacro(NavigationToolReader,itk::Object); itkFactorylessNewMacro(Self) itkCloneMacro(Self) /** * @brief This method reads a navigation tool from a file. * @param filename The filename where the tool is stored, "C:\temp\myTool.igtTool" for example. * @return Returns a pointer to the tool which was read. Returns NULL, if something went * wrong and no tool was read. In this case you may also want the error message which is availiable * from the method GetErrorMessage(). */ mitk::NavigationTool::Pointer DoRead(std::string filename); itkGetMacro(ErrorMessage,std::string); protected: NavigationToolReader(); ~NavigationToolReader(); std::string m_ErrorMessage; + std::string m_ToolfilePath; //This path is used to store the toolfiles. It must be available through the whole MITK run. + mitk::NavigationTool::Pointer ConvertDataNodeToNavigationTool(mitk::DataNode::Pointer node, std::string toolPath); //################### protected help methods ######################## std::string GetFileWithoutPath(std::string FileWithPath); mitk::PointSet::Pointer ConvertStringToPointSet(std::string string); mitk::Point3D ConvertStringToPoint(std::string string); mitk::Quaternion ConvertStringToQuaternion(std::string string); void split(std::string& text, std::string& separators, std::vector<std::string>& words); }; } // namespace mitk #endif //NAVIGATIONTOOLREADER diff --git a/Modules/IGT/IO/mitkNavigationToolStorageDeserializer.cpp b/Modules/IGT/IO/mitkNavigationToolStorageDeserializer.cpp index cd0ae2b54d..f9722c0fd5 100644 --- a/Modules/IGT/IO/mitkNavigationToolStorageDeserializer.cpp +++ b/Modules/IGT/IO/mitkNavigationToolStorageDeserializer.cpp @@ -1,117 +1,116 @@ /*=================================================================== 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. ===================================================================*/ //Poco headers #include "Poco/Zip/Decompress.h" #include "Poco/Path.h" #include "Poco/File.h" #include "mitkNavigationToolStorageDeserializer.h" #include <mitkSceneIO.h> #include <mitkIOUtil.h> #include "mitkNavigationToolReader.h" //POCO #include <Poco/Exception.h> #include "mitkIGTException.h" #include "mitkIGTIOException.h" mitk::NavigationToolStorageDeserializer::NavigationToolStorageDeserializer(mitk::DataStorage::Pointer dataStorage) { m_DataStorage = dataStorage; //create temp directory for this reader - m_tempDirectory = mitk::IOUtil::CreateTemporaryDirectory(); - MITK_INFO << "TEMP DIRECTORY: " << m_tempDirectory; + m_tempDirectory = mitk::IOUtil::CreateTemporaryDirectory("NavigationToolStorageDeserializerTmp_XXXXXX",mitk::IOUtil::GetProgramPath()); } mitk::NavigationToolStorageDeserializer::~NavigationToolStorageDeserializer() { //remove temp directory Poco::File myFile(m_tempDirectory); try { if (myFile.exists()) myFile.remove(); } catch(...) { MITK_ERROR << "Can't remove temp directory " << m_tempDirectory << "!"; } } mitk::NavigationToolStorage::Pointer mitk::NavigationToolStorageDeserializer::Deserialize(std::string filename) { //decomress zip file into temporary directory decomressFiles(filename,m_tempDirectory); //now read all files and convert them to navigation tools mitk::NavigationToolStorage::Pointer returnValue = mitk::NavigationToolStorage::New(m_DataStorage); bool cont = true; int i; for (i=0; cont==true; i++) { std::string fileName = m_tempDirectory + Poco::Path::separator() + "NavigationTool" + convertIntToString(i) + ".tool"; mitk::NavigationToolReader::Pointer myReader = mitk::NavigationToolReader::New(); mitk::NavigationTool::Pointer readTool = myReader->DoRead(fileName); if (readTool.IsNull()) cont = false; else returnValue->AddTool(readTool); //delete file std::remove(fileName.c_str()); } if(i==1) { //throw an exception here in case of not finding any tool m_ErrorMessage = "Error: did not find any tool. \n Is this a tool storage file?"; mitkThrowException(mitk::IGTException)<<"Error: did not find any tool. \n Is this a tool storage file?"; } return returnValue; } std::string mitk::NavigationToolStorageDeserializer::convertIntToString(int i) { std::string s; std::stringstream out; out << i; s = out.str(); return s; } void mitk::NavigationToolStorageDeserializer::decomressFiles(std::string filename,std::string path) { std::ifstream file( filename.c_str(), std::ios::binary ); if (!file.good()) { m_ErrorMessage = "Cannot open '" + filename + "' for reading"; mitkThrowException(mitk::IGTException)<<"Cannot open"+filename+" for reading"; } try { Poco::Zip::Decompress unzipper( file, Poco::Path( path ) ); unzipper.decompressAllFiles(); file.close(); } catch(Poco::IllegalStateException e) //temporary solution: replace this by defined exception handling later! { m_ErrorMessage = "Error: wrong file format! \n (please only load tool storage files)"; MITK_ERROR << m_ErrorMessage; mitkThrowException(mitk::IGTException) << m_ErrorMessage; } }