diff --git a/Modules/IGT/IGTToolManagement/mitkNavigationTool.cpp b/Modules/IGT/IGTToolManagement/mitkNavigationTool.cpp index 05eab9538f..0f033ae758 100644 --- a/Modules/IGT/IGTToolManagement/mitkNavigationTool.cpp +++ b/Modules/IGT/IGTToolManagement/mitkNavigationTool.cpp @@ -1,62 +1,64 @@ /*=================================================================== 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 "mitkNavigationTool.h" #include "Poco/File.h" -mitk::NavigationTool::NavigationTool() +mitk::NavigationTool::NavigationTool() : m_Type(mitk::NavigationTool::Unknown), + m_Identifier("None"), + m_TrackingDeviceType(mitk::TrackingSystemNotSpecified), + m_CalibrationFile("none"), + m_SerialNumber(""), + m_ToolRegistrationLandmarks(mitk::PointSet::New()), + m_ToolCalibrationLandmarks(mitk::PointSet::New()) { - m_Type = mitk::NavigationTool::Unknown; - m_Identifier = "None"; - m_TrackingDeviceType = mitk::TrackingSystemNotSpecified; - m_CalibrationFile = "none"; - m_SerialNumber = ""; + } mitk::NavigationTool::~NavigationTool() { } void mitk::NavigationTool::SetCalibrationFile(const std::string filename) { //check if file does exist: if (filename=="") { m_CalibrationFile = "none"; } else { Poco::File myFile(filename); if (myFile.exists()) m_CalibrationFile = filename; else m_CalibrationFile = "none"; } } std::string mitk::NavigationTool::GetToolName() { if (this->m_DataNode.IsNull()) {return "";} else {return m_DataNode->GetName();} } mitk::Surface::Pointer mitk::NavigationTool::GetToolSurface() { if (this->m_DataNode.IsNull()) {return NULL;} else if (this->m_DataNode->GetData() == NULL) {return NULL;} else {return dynamic_cast(m_DataNode->GetData());} } diff --git a/Modules/IGT/IGTToolManagement/mitkNavigationTool.h b/Modules/IGT/IGTToolManagement/mitkNavigationTool.h index 482efcdf87..a23aa4908c 100644 --- a/Modules/IGT/IGTToolManagement/mitkNavigationTool.h +++ b/Modules/IGT/IGTToolManagement/mitkNavigationTool.h @@ -1,122 +1,154 @@ /*=================================================================== 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 NAVIGATIONTOOL_H_INCLUDED #define NAVIGATIONTOOL_H_INCLUDED //itk headers #include #include //mitk headers #include #include +#include #include #include #include #include namespace mitk { /**Documentation * \brief An object of this class represents a navigation tool in the view of the software. * A few informations like an identifier, a toolname, a surface and a itk spatial * object are stored in such an object. The classes NavigationToolReader and * are availiable to write/read tools to/from the harddisc. If you need a collection * of navigation tools the class NavigationToolStorage could be used. * * \ingroup IGT */ class MitkIGT_EXPORT NavigationTool : public itk::Object { public: mitkClassMacro(NavigationTool,itk::Object); itkNewMacro(Self); enum NavigationToolType {Instrument, Fiducial, Skinmarker, Unknown}; //## getter and setter ## //NavigationToolType: itkGetMacro(Type,NavigationToolType); itkSetMacro(Type,NavigationToolType); + //Identifier: itkGetMacro(Identifier,std::string); itkSetMacro(Identifier,std::string); + //Datatreenode: itkGetMacro(DataNode,mitk::DataNode::Pointer); itkSetMacro(DataNode,mitk::DataNode::Pointer); + //SpatialObject: itkGetMacro(SpatialObject,itk::SpatialObject<3>::Pointer); itkSetMacro(SpatialObject,itk::SpatialObject<3>::Pointer); + //TrackingTool: itkGetMacro(TrackingTool,mitk::TrackingTool::Pointer); itkSetMacro(TrackingTool,mitk::TrackingTool::Pointer); + //CalibrationFile: itkGetMacro(CalibrationFile,std::string); - void SetCalibrationFile(const std::string filename); //itkSetMacro(CalibrationFile,std::string); + void SetCalibrationFile(const std::string filename); + + //Tool Landmarks: + /** @return Returns the tool registration landmarks which represent markers / special points on a + * tool that can be used for registration. The landmarks should be given in tool coordinates. + * If there are no landmarks defined for this tool the method returns an empty point set. + */ + itkGetMacro(ToolRegistrationLandmarks,mitk::PointSet::Pointer); + /** @brief Sets the tool registration landmarks which represent markers / special points on a + * tool that can be used for registration. The landmarks should be given in tool coordinates. + */ + itkSetMacro(ToolRegistrationLandmarks,mitk::PointSet::Pointer); + /** @return Returns the tool calibration landmarks for calibration of the defined points in the + * tool coordinate system, e.g. 2 landmarks for a 5DoF tool and 3 landmarks for a 6DoF tool. + */ + itkGetMacro(ToolCalibrationLandmarks,mitk::PointSet::Pointer); + /** @brief Sets the tool calibration landmarks for calibration of defined points in the + * tool coordinate system, e.g. 2 landmarks for a 5DoF tool and 3 landmarks for a 6DoF tool. + */ + itkSetMacro(ToolCalibrationLandmarks,mitk::PointSet::Pointer); + //SerialNumber: itkGetMacro(SerialNumber,std::string); itkSetMacro(SerialNumber,std::string); //TrackingDeviceType: itkGetMacro(TrackingDeviceType,mitk::TrackingDeviceType); itkSetMacro(TrackingDeviceType,mitk::TrackingDeviceType); //ToolName (only getter): /** @return Returns the name of this navigation tool. Returns an empty string if there is * no name (for example because the data node has not been set yet). * * Note: There is no setter for the name, * because the name of the corresponding data node is used as tool name. So if you * want to modify the name of this navigation tool only get the data node and modify * its name. */ std::string GetToolName(); //ToolSurface (only getter): /** @return Returns the surface of this navigation tool. Returns NULL if there is * no surface (for example because the data node has not been set yet). * * Note: There is no setter for the surface, * because the surface is the data of the corresponding data node. So if you * want to set a new surface only get the data node and modify its data. */ mitk::Surface::Pointer GetToolSurface(); //####################### protected: NavigationTool(); ~NavigationTool(); //## data structure of a navigation tool object ## std::string m_Identifier; NavigationToolType m_Type; /** @brief This DataNode holds a toolname and a tool surface */ mitk::DataNode::Pointer m_DataNode; /** @brief This member variable holds a mathamatical description of the tool */ itk::SpatialObject<3>::Pointer m_SpatialObject; /** @brief This member variable holds a pointer to the corresponding tracking tool in the hardware. */ mitk::TrackingTool::Pointer m_TrackingTool; /** @brief The path to the calibration file of the tool. */ std::string m_CalibrationFile; /** @brief A unique serial number of the tool which is needed to identify the tool correctly. This is very important * in case of the NDI Aurora System. */ std::string m_SerialNumber; /** @brief This member holds the tracking device type of the tool. */ mitk::TrackingDeviceType m_TrackingDeviceType; + /** @brief Holds landmarks for tool registration. */ + mitk::PointSet::Pointer m_ToolRegistrationLandmarks; + /** @brief Holds landmarks for calibration of the defined points in the tool coordinate system, + * e.g. 2 landmarks for a 5DoF tool and 3 landmarks for a 6DoF tool. + */ + mitk::PointSet::Pointer m_ToolCalibrationLandmarks; //################################################# }; } // namespace mitk #endif //NAVIGATIONTOOL 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 diff --git a/Modules/IGT/Testing/mitkNavigationToolStorageSerializerAndDeserializerTest.cpp b/Modules/IGT/Testing/mitkNavigationToolStorageSerializerAndDeserializerTest.cpp index 674fac73a0..f84615c241 100644 --- a/Modules/IGT/Testing/mitkNavigationToolStorageSerializerAndDeserializerTest.cpp +++ b/Modules/IGT/Testing/mitkNavigationToolStorageSerializerAndDeserializerTest.cpp @@ -1,320 +1,365 @@ /*=================================================================== 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/Path.h" #include #include #include #include #include #include #include #include "mitkNavigationToolStorage.h" //POCO #include class NavigationToolStorageSerializerAndDeserializerTestClass { public: static void TestInstantiationSerializer() { // let's create objects of our classes mitk::NavigationToolStorageSerializer::Pointer testSerializer = mitk::NavigationToolStorageSerializer::New(); MITK_TEST_CONDITION_REQUIRED(testSerializer.IsNotNull(),"Testing instantiation of NavigationToolStorageSerializer"); } static void TestInstantiationDeserializer() { mitk::DataStorage::Pointer tempStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! mitk::NavigationToolStorageDeserializer::Pointer testDeserializer = mitk::NavigationToolStorageDeserializer::New(tempStorage); MITK_TEST_CONDITION_REQUIRED(testDeserializer.IsNotNull(),"Testing instantiation of NavigationToolStorageDeserializer") } static void TestWriteSimpleToolStorage() { //create Tool Storage mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorage::New(); //first tool mitk::NavigationTool::Pointer myTool1 = mitk::NavigationTool::New(); myTool1->SetIdentifier("001"); myStorage->AddTool(myTool1); //second tool mitk::NavigationTool::Pointer myTool2 = mitk::NavigationTool::New(); myTool2->SetIdentifier("002"); myStorage->AddTool(myTool2); //third tool mitk::NavigationTool::Pointer myTool3 = mitk::NavigationTool::New(); myTool3->SetIdentifier("003"); myStorage->AddTool(myTool3); //create Serializer mitk::NavigationToolStorageSerializer::Pointer mySerializer = mitk::NavigationToolStorageSerializer::New(); //create filename std::string filename = mitk::StandardFileLocations::GetInstance()->GetOptionDirectory()+Poco::Path::separator()+".."+Poco::Path::separator()+"TestStorage.storage"; //test serialization bool success = mySerializer->Serialize(filename,myStorage); MITK_TEST_CONDITION_REQUIRED(success,"Testing serialization of simple tool storage"); } + static void TestWriteAndReadSimpleToolStorageWithToolLandmarks() + { + //create Tool Storage + mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorage::New(); + + //first tool + mitk::NavigationTool::Pointer myTool1 = mitk::NavigationTool::New(); + myTool1->SetIdentifier("001"); + mitk::PointSet::Pointer CalLandmarks1 = mitk::PointSet::New(); + mitk::Point3D testPt1; + mitk::FillVector3D(testPt1,1,2,3); + CalLandmarks1->SetPoint(0,testPt1); + mitk::PointSet::Pointer RegLandmarks1 = mitk::PointSet::New(); + mitk::Point3D testPt2; + mitk::FillVector3D(testPt2,4,5,6); + RegLandmarks1->SetPoint(5,testPt2); + myTool1->SetToolCalibrationLandmarks(CalLandmarks1); + myTool1->SetToolRegistrationLandmarks(RegLandmarks1); + myStorage->AddTool(myTool1); + + //create Serializer + mitk::NavigationToolStorageSerializer::Pointer mySerializer = mitk::NavigationToolStorageSerializer::New(); + + //create filename + std::string filename = mitk::StandardFileLocations::GetInstance()->GetOptionDirectory()+Poco::Path::separator()+".."+Poco::Path::separator()+"TestStorageToolReg.storage"; + + //test serialization + bool success = mySerializer->Serialize(filename,myStorage); + MITK_TEST_CONDITION_REQUIRED(success,"Testing serialization of tool storage with tool registrations"); + + mitk::DataStorage::Pointer tempStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! + mitk::NavigationToolStorageDeserializer::Pointer myDeserializer = mitk::NavigationToolStorageDeserializer::New(tempStorage); + mitk::NavigationToolStorage::Pointer readStorage = myDeserializer->Deserialize(mitk::StandardFileLocations::GetInstance()->GetOptionDirectory()+Poco::Path::separator()+".."+Poco::Path::separator()+"TestStorageToolReg.storage"); + MITK_TEST_CONDITION_REQUIRED(readStorage.IsNotNull(),"Testing deserialization of tool storage with tool registrations"); + MITK_TEST_CONDITION_REQUIRED(readStorage->GetToolCount()==1," ..Testing number of tools in storage"); + + mitk::PointSet::Pointer readRegLandmarks = readStorage->GetTool(0)->GetToolRegistrationLandmarks(); + mitk::PointSet::Pointer readCalLandmarks = readStorage->GetTool(0)->GetToolCalibrationLandmarks(); + + MITK_TEST_CONDITION_REQUIRED(((readRegLandmarks->GetPoint(5)[0] == 4)&&(readRegLandmarks->GetPoint(5)[1] == 5)&&(readRegLandmarks->GetPoint(5)[2] == 6)),"..Testing if tool registration landmarks have been stored and loaded correctly."); + MITK_TEST_CONDITION_REQUIRED(((readCalLandmarks->GetPoint(0)[0] == 1)&&(readCalLandmarks->GetPoint(0)[1] == 2)&&(readCalLandmarks->GetPoint(0)[2] == 3)),"..Testing if tool calibration landmarks have been stored and loaded correctly."); + } + static void TestReadSimpleToolStorage() { mitk::DataStorage::Pointer tempStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! mitk::NavigationToolStorageDeserializer::Pointer myDeserializer = mitk::NavigationToolStorageDeserializer::New(tempStorage); mitk::NavigationToolStorage::Pointer readStorage = myDeserializer->Deserialize(mitk::StandardFileLocations::GetInstance()->GetOptionDirectory()+Poco::Path::separator()+".."+Poco::Path::separator()+"TestStorage.storage"); MITK_TEST_CONDITION_REQUIRED(readStorage.IsNotNull(),"Testing deserialization of simple tool storage"); MITK_TEST_CONDITION_REQUIRED(readStorage->GetToolCount()==3," ..Testing number of tools in storage"); //TODO: why is the order of tools changed is save/load process?? bool foundtool1 = false; bool foundtool2 = false; bool foundtool3 = false; for(int i=0; i<3; i++) { if ((readStorage->GetTool(i)->GetIdentifier()=="001")) foundtool1 = true; else if ((readStorage->GetTool(i)->GetIdentifier()=="002")) foundtool2 = true; else if ((readStorage->GetTool(i)->GetIdentifier()=="003")) foundtool3 = true; } MITK_TEST_CONDITION_REQUIRED(foundtool1&&foundtool2&&foundtool3," ..Testing if identifiers of tools where saved / loaded successfully"); } static void CleanUp() { try { std::remove((mitk::StandardFileLocations::GetInstance()->GetOptionDirectory()+Poco::Path::separator()+".."+Poco::Path::separator()+"TestStorage.storage").c_str()); + std::remove((mitk::StandardFileLocations::GetInstance()->GetOptionDirectory()+Poco::Path::separator()+".."+Poco::Path::separator()+"TestStorageToolReg.storage").c_str()); std::remove((mitk::StandardFileLocations::GetInstance()->GetOptionDirectory()+Poco::Path::separator()+".."+Poco::Path::separator()+"TestStorage2.storage").c_str()); } catch(...) { MITK_INFO << "Warning: Error occured when deleting test file!"; } } static void TestWriteComplexToolStorage() { //create first tool mitk::Surface::Pointer testSurface; std::string toolFileName = mitk::StandardFileLocations::GetInstance()->FindFile("ClaronTool", "Modules/IGT/Testing/Data"); MITK_TEST_CONDITION(toolFileName.empty() == false, "Check if tool calibration of claron tool file exists"); mitk::NavigationTool::Pointer myNavigationTool = mitk::NavigationTool::New(); myNavigationTool->SetCalibrationFile(toolFileName); mitk::DataNode::Pointer myNode = mitk::DataNode::New(); myNode->SetName("ClaronTool"); //load an stl File mitk::STLFileReader::Pointer stlReader = mitk::STLFileReader::New(); try { stlReader->SetFileName( mitk::StandardFileLocations::GetInstance()->FindFile("ClaronTool.stl", "Testing/Data/").c_str() ); stlReader->Update(); } catch (...) { MITK_TEST_FAILED_MSG(<<"Cannot read stl file."); } if ( stlReader->GetOutput() == NULL ) { MITK_TEST_FAILED_MSG(<<"Cannot read stl file."); } else { testSurface = stlReader->GetOutput(); myNode->SetData(testSurface); } myNavigationTool->SetDataNode(myNode); myNavigationTool->SetIdentifier("ClaronTool#1"); myNavigationTool->SetSerialNumber("0815"); myNavigationTool->SetTrackingDeviceType(mitk::ClaronMicron); myNavigationTool->SetType(mitk::NavigationTool::Fiducial); //create second tool mitk::NavigationTool::Pointer myNavigationTool2 = mitk::NavigationTool::New(); mitk::Surface::Pointer testSurface2; mitk::DataNode::Pointer myNode2 = mitk::DataNode::New(); myNode2->SetName("AuroraTool"); //load an stl File try { stlReader->SetFileName( mitk::StandardFileLocations::GetInstance()->FindFile("EMTool.stl", "Testing/Data/").c_str() ); stlReader->Update(); } catch (...) { MITK_TEST_FAILED_MSG(<<"Cannot read stl file."); } if ( stlReader->GetOutput() == NULL ) { MITK_TEST_FAILED_MSG(<<"Cannot read stl file."); } else { testSurface2 = stlReader->GetOutput(); myNode2->SetData(testSurface2); } myNavigationTool2->SetDataNode(myNode2); myNavigationTool2->SetIdentifier("AuroraTool#1"); myNavigationTool2->SetSerialNumber("0816"); myNavigationTool2->SetTrackingDeviceType(mitk::NDIAurora); myNavigationTool2->SetType(mitk::NavigationTool::Instrument); //create navigation tool storage mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorage::New(); myStorage->AddTool(myNavigationTool); myStorage->AddTool(myNavigationTool2); //create Serializer mitk::NavigationToolStorageSerializer::Pointer mySerializer = mitk::NavigationToolStorageSerializer::New(); //create filename std::string filename = mitk::StandardFileLocations::GetInstance()->GetOptionDirectory()+Poco::Path::separator()+".."+Poco::Path::separator()+"TestStorage2.storage"; //test serialization bool success = mySerializer->Serialize(filename,myStorage); MITK_TEST_CONDITION_REQUIRED(success,"Testing serialization of complex tool storage"); } static void TestReadComplexToolStorage() { mitk::DataStorage::Pointer tempStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! mitk::NavigationToolStorageDeserializer::Pointer myDeserializer = mitk::NavigationToolStorageDeserializer::New(tempStorage); mitk::NavigationToolStorage::Pointer readStorage = myDeserializer->Deserialize(mitk::StandardFileLocations::GetInstance()->GetOptionDirectory()+Poco::Path::separator()+".."+Poco::Path::separator()+"TestStorage2.storage"); MITK_TEST_CONDITION_REQUIRED(readStorage.IsNotNull(),"Testing deserialization of complex tool storage"); MITK_TEST_CONDITION_REQUIRED(readStorage->GetToolCount()==2," ..Testing number of tools in storage"); } static void TestReadNotExistingStorage() { mitk::DataStorage::Pointer tempStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! mitk::NavigationToolStorageDeserializer::Pointer myDeserializer = mitk::NavigationToolStorageDeserializer::New(tempStorage); mitk::NavigationToolStorage::Pointer readStorage = myDeserializer->Deserialize("noStorage.tfl"); MITK_TEST_CONDITION_REQUIRED(readStorage->isEmpty(),"Testing deserialization of not existing data storage."); MITK_TEST_CONDITION_REQUIRED(myDeserializer->GetErrorMessage() == "Cannot open 'noStorage.tfl' for reading", "Checking Error Message"); } static void TestReadStorageWithUnknownFiletype() { mitk::DataStorage::Pointer tempStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! std::string toolFileName = mitk::StandardFileLocations::GetInstance()->FindFile("ClaronTool.stl", "Modules/IGT/Testing/Data"); MITK_TEST_CONDITION(toolFileName.empty() == false, "Check if tool calibration of claron tool file exists"); mitk::NavigationToolStorageDeserializer::Pointer myDeserializer = mitk::NavigationToolStorageDeserializer::New(tempStorage); mitk::NavigationToolStorage::Pointer readStorage = myDeserializer->Deserialize(toolFileName); MITK_TEST_CONDITION_REQUIRED(readStorage->isEmpty(), "Testing deserialization of existing file with unknown filetype."); } static void TestReadZipFileWithNoToolstorage() { mitk::DataStorage::Pointer tempStorage = dynamic_cast(mitk::StandaloneDataStorage::New().GetPointer()); //needed for deserializer! std::string toolFileName = mitk::StandardFileLocations::GetInstance()->FindFile("Empty.zip", "Modules/IGT/Testing/Data"); MITK_TEST_CONDITION(toolFileName.empty() == false, "Check if tool calibration of claron tool file exists"); mitk::NavigationToolStorageDeserializer::Pointer myDeserializer = mitk::NavigationToolStorageDeserializer::New(tempStorage); mitk::NavigationToolStorage::Pointer readStorage = myDeserializer->Deserialize(toolFileName); MITK_TEST_CONDITION_REQUIRED(readStorage->isEmpty(), "Testing deserialization of empty zip file with no toolstorage in it"); } static void TestWriteStorageToInvalidFile() { //create Tool Storage mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorage::New(); //first tool mitk::NavigationTool::Pointer myTool1 = mitk::NavigationTool::New(); myTool1->SetIdentifier("001"); myStorage->AddTool(myTool1); //second tool mitk::NavigationTool::Pointer myTool2 = mitk::NavigationTool::New(); myTool2->SetIdentifier("002"); myStorage->AddTool(myTool2); //third tool mitk::NavigationTool::Pointer myTool3 = mitk::NavigationTool::New(); myTool3->SetIdentifier("003"); myStorage->AddTool(myTool3); //create Serializer mitk::NavigationToolStorageSerializer::Pointer mySerializer = mitk::NavigationToolStorageSerializer::New(); //create filename #ifdef WIN32 std::string filename = "C:\342INVALIDFILE<>.storage"; //invalid filename for windows #else std::string filename = "/dsfdsf:$§$342INVALIDFILE.storage"; //invalid filename for linux #endif //test serialization bool success = true; success = mySerializer->Serialize(filename,myStorage); MITK_TEST_CONDITION_REQUIRED(!success,"Testing serialization into invalid file."); } static void TestWriteEmptyToolStorage() { //create Tool Storage mitk::NavigationToolStorage::Pointer myStorage = mitk::NavigationToolStorage::New(); //create Serializer mitk::NavigationToolStorageSerializer::Pointer mySerializer = mitk::NavigationToolStorageSerializer::New(); //create filename std::string filename = mitk::StandardFileLocations::GetInstance()->GetOptionDirectory()+Poco::Path::separator()+".."+Poco::Path::separator()+"TestStorage.storage"; //test serialization bool success = mySerializer->Serialize(filename,myStorage); MITK_TEST_CONDITION_REQUIRED(success,"Testing serialization of simple tool storage"); } }; /** This function is testing the TrackingVolume class. */ int mitkNavigationToolStorageSerializerAndDeserializerTest(int /* argc */, char* /*argv*/[]) { MITK_TEST_BEGIN("NavigationToolStorageSerializerAndDeserializer"); ///** TESTS DEACTIVATED BECAUSE OF DART-CLIENT PROBLEMS NavigationToolStorageSerializerAndDeserializerTestClass::TestInstantiationSerializer(); NavigationToolStorageSerializerAndDeserializerTestClass::TestInstantiationDeserializer(); NavigationToolStorageSerializerAndDeserializerTestClass::TestWriteSimpleToolStorage(); + NavigationToolStorageSerializerAndDeserializerTestClass::TestWriteAndReadSimpleToolStorageWithToolLandmarks(); NavigationToolStorageSerializerAndDeserializerTestClass::TestReadSimpleToolStorage(); NavigationToolStorageSerializerAndDeserializerTestClass::TestWriteComplexToolStorage(); NavigationToolStorageSerializerAndDeserializerTestClass::TestReadComplexToolStorage(); NavigationToolStorageSerializerAndDeserializerTestClass::TestReadNotExistingStorage(); NavigationToolStorageSerializerAndDeserializerTestClass::TestReadStorageWithUnknownFiletype(); NavigationToolStorageSerializerAndDeserializerTestClass::TestReadZipFileWithNoToolstorage(); NavigationToolStorageSerializerAndDeserializerTestClass::TestWriteStorageToInvalidFile(); NavigationToolStorageSerializerAndDeserializerTestClass::TestWriteEmptyToolStorage(); NavigationToolStorageSerializerAndDeserializerTestClass::CleanUp(); MITK_TEST_END(); } diff --git a/Modules/IGT/Testing/mitkNavigationToolTest.cpp b/Modules/IGT/Testing/mitkNavigationToolTest.cpp index 8375515a4a..6e2b814dc7 100644 --- a/Modules/IGT/Testing/mitkNavigationToolTest.cpp +++ b/Modules/IGT/Testing/mitkNavigationToolTest.cpp @@ -1,79 +1,94 @@ /*=================================================================== 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 "mitkNavigationTool.h" #include "mitkCommon.h" #include "mitkTestingMacros.h" #include "mitkDataNode.h" +#include "mitkPointSet.h" #include "mitkTrackingTool.h" #include class mitkNavigationToolTestClass { public: static void TestInstantiation() { // let's create an object of our class mitk::NavigationTool::Pointer myNavigationTool = mitk::NavigationTool::New(); MITK_TEST_CONDITION_REQUIRED(myNavigationTool.IsNotNull(),"Testing instantiation") } static void TestGetterAndSetter() { mitk::NavigationTool::Pointer myNavigationTool = mitk::NavigationTool::New(); //initialize a few things mitk::DataNode::Pointer myNode = mitk::DataNode::New(); myNode->SetName("TestNodeName"); itk::SpatialObject<3>::Pointer mySpatialObject = itk::SpatialObject<3>::New(); //set everything myNavigationTool->SetType(mitk::NavigationTool::Instrument); myNavigationTool->SetIdentifier("Tool#15"); myNavigationTool->SetDataNode(myNode); myNavigationTool->SetSpatialObject(mySpatialObject); //notice: cannot test Get/SetTrackingTool because this class cannot be instantiated alone myNavigationTool->SetCalibrationFile("Test.srom"); myNavigationTool->SetSerialNumber("0815"); myNavigationTool->SetTrackingDeviceType(mitk::NDIAurora); + + mitk::PointSet::Pointer CalLandmarks = mitk::PointSet::New(); + mitk::Point3D testPt1; + mitk::FillVector3D(testPt1,1,2,3); + CalLandmarks->SetPoint(0,testPt1); + + mitk::PointSet::Pointer RegLandmarks = mitk::PointSet::New(); + mitk::Point3D testPt2; + mitk::FillVector3D(testPt2,4,5,6); + RegLandmarks->SetPoint(0,testPt2); + + myNavigationTool->SetToolCalibrationLandmarks(CalLandmarks); + myNavigationTool->SetToolRegistrationLandmarks(RegLandmarks); //test getter MITK_TEST_CONDITION(myNavigationTool->GetType()==mitk::NavigationTool::Instrument,"Testing getter and setter of type."); MITK_TEST_CONDITION(myNavigationTool->GetIdentifier()=="Tool#15","Testing getter and setter of identifier."); MITK_TEST_CONDITION(myNavigationTool->GetDataNode()==myNode,"Testing getter and setter of dataNode."); MITK_TEST_CONDITION(myNavigationTool->GetSpatialObject()==mySpatialObject,"Testing getter and setter of itk spatial object."); MITK_TEST_CONDITION(myNavigationTool->GetCalibrationFile()=="none","Testing getter and setter of calibration file."); //should be none, because file does not exist MITK_TEST_CONDITION(myNavigationTool->GetSerialNumber()=="0815","Testing getter and setter of serial number."); MITK_TEST_CONDITION(myNavigationTool->GetTrackingDeviceType()==mitk::NDIAurora,"Testing getter and setter of tracking device type."); MITK_TEST_CONDITION(myNavigationTool->GetToolName()=="TestNodeName","Testing method GetToolName()."); - + MITK_TEST_CONDITION(myNavigationTool->GetToolCalibrationLandmarks()->GetPoint(0)[0] == 1.0,"Testing method GetToolCalibrationLandmarks()"); + MITK_TEST_CONDITION(myNavigationTool->GetToolRegistrationLandmarks()->GetPoint(0)[0] == 4.0,"Testing method GetToolRegistrationLandmarks()"); } }; /** This function is testing the TrackingVolume class. */ int mitkNavigationToolTest(int /* argc */, char* /*argv*/[]) { MITK_TEST_BEGIN("NavigationTool") mitkNavigationToolTestClass::TestInstantiation(); mitkNavigationToolTestClass::TestGetterAndSetter(); MITK_TEST_END() }