diff --git a/Modules/IGT/IGTToolManagement/mitkNavigationToolReader.cpp b/Modules/IGT/IGTToolManagement/mitkNavigationToolReader.cpp index 9135ffaa1f..bb72b10a4a 100644 --- a/Modules/IGT/IGTToolManagement/mitkNavigationToolReader.cpp +++ b/Modules/IGT/IGTToolManagement/mitkNavigationToolReader.cpp @@ -1,126 +1,177 @@ /*=================================================================== 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" //mitk headers #include "mitkNavigationToolReader.h" #include "mitkTrackingTypes.h" #include #include mitk::NavigationToolReader::NavigationToolReader() { } 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); 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(device_type)); //Tool Type int type; node->GetIntProperty("tracking tool type",type); returnValue->SetType(static_cast(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); 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; } + +mitk::PointSet::Pointer mitk::NavigationToolReader::ConvertStringToPointSet(std::string string) + { + mitk::PointSet::Pointer returnValue = mitk::PointSet::New(); + std::string pointSeperator = "|"; + std::string valueSeperator = ";"; + std::vector points; + split(string,pointSeperator,points); + for(int i=0; i 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; + } + +void mitk::NavigationToolReader::split(std::string& text, std::string& separators, std::vector& 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/IGTToolManagement/mitkNavigationToolReader.h b/Modules/IGT/IGTToolManagement/mitkNavigationToolReader.h index 22efd857da..da0e5be892 100644 --- a/Modules/IGT/IGTToolManagement/mitkNavigationToolReader.h +++ b/Modules/IGT/IGTToolManagement/mitkNavigationToolReader.h @@ -1,69 +1,71 @@ /*=================================================================== 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 //mitk headers #include #include "mitkNavigationTool.h" #include "mitkDataStorage.h" #include "mitkNavigationToolStorageDeserializer.h" #include 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); itkNewMacro(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; 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); + void split(std::string& text, std::string& separators, std::vector& words); }; } // namespace mitk #endif //NAVIGATIONTOOLREADER diff --git a/Modules/IGT/IGTToolManagement/mitkNavigationToolWriter.cpp b/Modules/IGT/IGTToolManagement/mitkNavigationToolWriter.cpp index 242e702a14..56b2940ff2 100644 --- a/Modules/IGT/IGTToolManagement/mitkNavigationToolWriter.cpp +++ b/Modules/IGT/IGTToolManagement/mitkNavigationToolWriter.cpp @@ -1,106 +1,124 @@ /*=================================================================== 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/Compress.h" #include "Poco/Path.h" //mitk headers #include "mitkNavigationToolWriter.h" #include #include #include +#include #include //std headers #include mitk::NavigationToolWriter::NavigationToolWriter() { } mitk::NavigationToolWriter::~NavigationToolWriter() { } bool mitk::NavigationToolWriter::DoWrite(std::string FileName,mitk::NavigationTool::Pointer Tool) { //convert whole data to a mitk::DataStorage mitk::StandaloneDataStorage::Pointer saveStorage = mitk::StandaloneDataStorage::New(); mitk::DataNode::Pointer thisTool = ConvertToDataNode(Tool); saveStorage->Add(thisTool); //use SceneSerialization to save the DataStorage std::string DataStorageFileName = mitk::StandardFileLocations::GetInstance()->GetOptionDirectory() + Poco::Path::separator() + GetFileWithoutPath(FileName) + ".storage"; mitk::SceneIO::Pointer mySceneIO = mitk::SceneIO::New(); mySceneIO->SaveScene(saveStorage->GetAll(),saveStorage,DataStorageFileName); //now put the DataStorage and the Toolfile in a ZIP-file std::ofstream file( FileName.c_str(), std::ios::binary | std::ios::out); if (!file.good()) { m_ErrorMessage = "Could not open a zip file for writing: '" + FileName + "'"; return false; } else { Poco::Zip::Compress zipper( file, true ); zipper.addFile(DataStorageFileName,GetFileWithoutPath(DataStorageFileName)); if (Tool->GetCalibrationFile()!="none") zipper.addFile(Tool->GetCalibrationFile(),GetFileWithoutPath(Tool->GetCalibrationFile())); zipper.close(); } //delete the data storage std::remove(DataStorageFileName.c_str()); return true; } mitk::DataNode::Pointer mitk::NavigationToolWriter::ConvertToDataNode(mitk::NavigationTool::Pointer Tool) { mitk::DataNode::Pointer thisTool = mitk::DataNode::New(); //Name if (Tool->GetDataNode().IsNull()) thisTool->SetName("none"); else thisTool->SetName(Tool->GetDataNode()->GetName().c_str()); //Identifier thisTool->AddProperty("identifier",mitk::StringProperty::New(Tool->GetIdentifier().c_str())); //Serial Number thisTool->AddProperty("serial number",mitk::StringProperty::New(Tool->GetSerialNumber().c_str())); //Tracking Device thisTool->AddProperty("tracking device type",mitk::IntProperty::New(Tool->GetTrackingDeviceType())); //Tool Type thisTool->AddProperty("tracking tool type",mitk::IntProperty::New(Tool->GetType())); //Calibration File Name thisTool->AddProperty("toolfileName",mitk::StringProperty::New(GetFileWithoutPath(Tool->GetCalibrationFile()))); //Surface if (Tool->GetDataNode().IsNotNull()) if (Tool->GetDataNode()->GetData()!=NULL) thisTool->SetData(Tool->GetDataNode()->GetData()); + //Tool Landmarks + thisTool->AddProperty("ToolRegistrationLandmarks",mitk::StringProperty::New(ConvertPointSetToString(Tool->GetToolRegistrationLandmarks()))); + thisTool->AddProperty("ToolCalibrationLandmarks",mitk::StringProperty::New(ConvertPointSetToString(Tool->GetToolCalibrationLandmarks()))); + //Material is not needed, to avoid errors in scene serialization we have to do this: thisTool->ReplaceProperty("material",NULL); + return thisTool; } std::string mitk::NavigationToolWriter::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; } + +std::string mitk::NavigationToolWriter::ConvertPointSetToString(mitk::PointSet::Pointer pointSet) + { + std::stringstream returnValue; + mitk::PointSet::PointDataIterator it; + for ( it = pointSet->GetPointSet()->GetPointData()->Begin();it != pointSet->GetPointSet()->GetPointData()->End();it++ ) + { + mitk::Point3D thisPoint = pointSet->GetPoint(it->Index()); + returnValue << it->Index() << ";" << thisPoint[0] << ";" << thisPoint[1] << ";" << thisPoint[2] << "|"; + } + return returnValue.str(); + } diff --git a/Modules/IGT/IGTToolManagement/mitkNavigationToolWriter.h b/Modules/IGT/IGTToolManagement/mitkNavigationToolWriter.h index 661c7c84fd..9d73382af0 100644 --- a/Modules/IGT/IGTToolManagement/mitkNavigationToolWriter.h +++ b/Modules/IGT/IGTToolManagement/mitkNavigationToolWriter.h @@ -1,66 +1,67 @@ /*=================================================================== 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 NAVIGATIONTOOLWRITER_H_INCLUDED #define NAVIGATIONTOOLWRITER_H_INCLUDED //itk headers #include //mitk headers #include #include "mitkNavigationTool.h" #include "mitkNavigationToolStorageSerializer.h" #include namespace mitk { /**Documentation * \brief This class offers methods to write objects of the class navigation tool permanently * to the harddisk. The objects are saved in a special fileformat which can be read * by the class NavigationToolReader to restore the object. * * \ingroup IGT */ class MitkIGT_EXPORT NavigationToolWriter : public itk::Object { friend class mitk::NavigationToolStorageSerializer; public: mitkClassMacro(NavigationToolWriter,itk::Object); itkNewMacro(Self); /** * @brief Writes a navigation tool to a file. * @param FileName The filename (complete, with path, C:\temp\myTool.igtTool for example) * @param Tool The tool which should be written to the file. * @return Returns true if the file was written successfully, false if not. In the second * case you can get the error message by using the method GetErrorMessage(). */ bool DoWrite(std::string FileName,mitk::NavigationTool::Pointer Tool); itkGetMacro(ErrorMessage,std::string); protected: NavigationToolWriter(); ~NavigationToolWriter(); std::string m_ErrorMessage; mitk::DataNode::Pointer ConvertToDataNode(mitk::NavigationTool::Pointer Tool); std::string GetFileWithoutPath(std::string FileWithPath); + std::string ConvertPointSetToString(mitk::PointSet::Pointer pointSet); }; } // namespace mitk #endif //NAVIGATIONTOOLWRITER