MITK-IGT
IGT Extension of MITK
Loading...
Searching...
No Matches
mitkNavigationToolReaderAndWriterTest.cpp
Go to the documentation of this file.
1/*============================================================================
2
3The Medical Imaging Interaction Toolkit (MITK)
4
5Copyright (c) German Cancer Research Center (DKFZ)
6All rights reserved.
7
8Use of this source code is governed by a 3-clause BSD license that can be
9found in the LICENSE file.
10
11============================================================================*/
12
13//Poco headers
14#include "Poco/Path.h"
15
16//mitk headers
18#include "mitkCommon.h"
19#include "mitkTestingMacros.h"
20#include "mitkNavigationTool.h"
21#include "mitkBaseData.h"
22#include "mitkDataNode.h"
23#include "mitkSurface.h"
24#include "mitkStandaloneDataStorage.h"
25#include "mitkDataStorage.h"
27#include "mitkIGTConfig.h"
28#include <mitkIOUtil.h>
29
30#include <sstream>
31#include <fstream>
32
35
36mitk::Surface::Pointer m_testSurface;
37
38static void TestInstantiation()
39{
40 // let's create an object of our class
41 mitk::NavigationToolWriter::Pointer myWriter = mitk::NavigationToolWriter::New();
42 MITK_TEST_CONDITION_REQUIRED(myWriter.IsNotNull(),"Testing instantiation")
43}
44
45static void TestWrite()
46{
47 //testcase with first test tool: a claron tool
48
49 //create a NavigationTool which we can write on the harddisc
50 std::string toolFileName(MITK_IGT_DATA_DIR);
51 toolFileName.append("/ClaronTool");
52 mitk::NavigationTool::Pointer myNavigationTool = mitk::NavigationTool::New();
53 myNavigationTool->SetCalibrationFile(toolFileName);
54
55 mitk::DataNode::Pointer myNode = mitk::DataNode::New();
56 myNode->SetName("ClaronTool");
57
58 std::string surfaceFileName(MITK_IGT_DATA_DIR);
59 surfaceFileName.append("/ClaronTool.stl");
60 m_testSurface = mitk::IOUtil::Load<mitk::Surface>( surfaceFileName );
61 myNode->SetData(m_testSurface);
62
63 myNavigationTool->SetDataNode(myNode);
64 myNavigationTool->SetIdentifier("ClaronTool#1");
65 myNavigationTool->SetSerialNumber("0815");
66 myNavigationTool->SetTrackingDeviceType(mitk::MicronTrackerTypeInformation::GetTrackingDeviceName());
67 myNavigationTool->SetType(mitk::NavigationTool::Fiducial);
68
69 //now create a writer and write it to the harddisc
70 mitk::NavigationToolWriter::Pointer myWriter = mitk::NavigationToolWriter::New();
71 std::string filename = mitk::IOUtil::GetTempPath() + "TestTool.tool";
72
73 MITK_TEST_OUTPUT(<<"---- Testing navigation tool writer with first test tool (claron tool) ----");
74 bool test = myWriter->DoWrite(filename,myNavigationTool);
75 MITK_TEST_CONDITION_REQUIRED(test,"OK");
76}
77
78static void TestRead()
79{
80 mitk::NavigationToolReader::Pointer myReader = mitk::NavigationToolReader::New();
81 std::string filename = mitk::IOUtil::GetTempPath() + "TestTool.tool";
82 mitk::NavigationTool::Pointer readTool = myReader->DoRead(filename);
83 MITK_TEST_OUTPUT(<<"---- Testing navigation tool reader with first test tool (claron tool) ----");
84
85 //Test if the surfaces do have the same number of vertexes (it would be better to test for real equality of the surfaces!)
86 MITK_TEST_CONDITION_REQUIRED(dynamic_cast<mitk::Surface*>(readTool->GetDataNode()->GetData())->GetSizeOfPolyDataSeries()==m_testSurface->GetSizeOfPolyDataSeries(),"Test if surface was restored correctly ...");
87
88 MITK_TEST_CONDITION_REQUIRED(readTool->GetType()==mitk::NavigationTool::Fiducial,"Testing Tool Type");
89
90 MITK_TEST_CONDITION_REQUIRED(readTool->GetTrackingDeviceType() == mitk::MicronTrackerTypeInformation::GetTrackingDeviceName(), "Testing Tracking Device Type");
91
92 MITK_TEST_CONDITION_REQUIRED(readTool->GetSerialNumber()=="0815","Testing Serial Number");
93
94 std::ifstream TestFile(readTool->GetCalibrationFile().c_str());
95 MITK_TEST_CONDITION_REQUIRED(TestFile,"Testing If Calibration File Exists");
96
97}
98
99static void TestWrite2()
100{
101 //testcase with second test tool: an aurora tool
102 //create a NavigationTool which we can write on the harddisc
103 mitk::NavigationTool::Pointer myNavigationTool = mitk::NavigationTool::New();
104
105 mitk::DataNode::Pointer myNode = mitk::DataNode::New();
106 myNode->SetName("AuroraTool");
107
108 std::string surfaceFileName(MITK_IGT_DATA_DIR);
109 surfaceFileName.append("/EMTool.stl");
110 m_testSurface = mitk::IOUtil::Load<mitk::Surface>( surfaceFileName );
111 myNode->SetData(m_testSurface);
112
113 myNavigationTool->SetDataNode(myNode);
114 myNavigationTool->SetIdentifier("AuroraTool#1");
115 myNavigationTool->SetSerialNumber("0816");
116 myNavigationTool->SetTrackingDeviceType(mitk::NDIAuroraTypeInformation::GetTrackingDeviceName());
117 myNavigationTool->SetType(mitk::NavigationTool::Instrument);
118
119 //now create a writer and write it to the harddisc
120 mitk::NavigationToolWriter::Pointer myWriter = mitk::NavigationToolWriter::New();
121 std::string filename = mitk::IOUtil::GetTempPath() + "TestTool2.tool";
122
123 MITK_TEST_OUTPUT(<<"---- Testing navigation tool writer with second tool (aurora tool) ----");
124 bool test = myWriter->DoWrite(filename,myNavigationTool);
125 MITK_TEST_CONDITION_REQUIRED(test,"OK");
126}
127
128static void TestRead2()
129{
130 mitk::NavigationToolReader::Pointer myReader = mitk::NavigationToolReader::New();
131 std::string filename = mitk::IOUtil::GetTempPath() + "TestTool2.tool";
132 mitk::NavigationTool::Pointer readTool = myReader->DoRead(filename);
133 MITK_TEST_OUTPUT(<<"---- Testing navigation tool reader with second tool (aurora tool) ----");
134
135 //Test if the surfaces do have the same number of vertexes (it would be better to test for real equality of the surfaces!)
136 MITK_TEST_CONDITION_REQUIRED(dynamic_cast<mitk::Surface*>(readTool->GetDataNode()->GetData())->GetSizeOfPolyDataSeries()==m_testSurface->GetSizeOfPolyDataSeries(),"Test if surface was restored correctly ...");
137
138 //Test if the tool type is the same
139 MITK_TEST_CONDITION_REQUIRED(readTool->GetType()==mitk::NavigationTool::Instrument,"Testing Tool Type");
140
141 MITK_TEST_CONDITION_REQUIRED(readTool->GetTrackingDeviceType() == mitk::NDIAuroraTypeInformation::GetTrackingDeviceName(), "Testing Tracking Device Type");
142
143 MITK_TEST_CONDITION_REQUIRED(readTool->GetSerialNumber()=="0816","Testing Serial Number");
144
145 MITK_TEST_CONDITION_REQUIRED(readTool->GetCalibrationFile()=="none","Testing Calibration File");
146
147}
148
149static void CleanUp()
150{
151 std::string tempFile1 = mitk::IOUtil::GetTempPath() + "TestTool.tool";
152 std::remove(tempFile1.c_str());
153 std::string tempFile2 = mitk::IOUtil::GetTempPath() + "TestTool2.tool";
154 std::remove(tempFile2.c_str());
155}
156
157static void TestReadInvalidData()
158{
159 mitk::NavigationToolReader::Pointer myReader = mitk::NavigationToolReader::New();
160 mitk::NavigationTool::Pointer readTool = myReader->DoRead("invalidTool");
161
162 MITK_TEST_CONDITION_REQUIRED(readTool.IsNull(), "Testing return value if filename is invalid");
163 MITK_TEST_CONDITION_REQUIRED(myReader->GetErrorMessage() == "Cannot open 'invalidTool' for reading", "Testing error message in this case");
164}
165
166static void TestWriteInvalidFilename()
167{
168 //create a test navigation tool
169 mitk::NavigationTool::Pointer myNavigationTool = mitk::NavigationTool::New();
170 mitk::DataNode::Pointer myNode = mitk::DataNode::New();
171 myNode->SetName("AuroraTool");
172 std::string surfaceFileName(MITK_IGT_DATA_DIR);
173 surfaceFileName.append("/EMTool.stl");
174 m_testSurface = mitk::IOUtil::Load<mitk::Surface>( surfaceFileName );
175 myNode->SetData(m_testSurface);
176 myNavigationTool->SetDataNode(myNode);
177 myNavigationTool->SetIdentifier("AuroraTool#1");
178 myNavigationTool->SetSerialNumber("0816");
179 myNavigationTool->SetTrackingDeviceType(mitk::NDIAuroraTypeInformation::GetTrackingDeviceName());
180 myNavigationTool->SetType(mitk::NavigationTool::Instrument);
181
182 //now create a writer and write it to the harddisc
183 mitk::NavigationToolWriter::Pointer myWriter = mitk::NavigationToolWriter::New();
184 std::string filename = "NH:/sfdsfsdsf.&%%%";
185
186 MITK_TEST_OUTPUT(<<"---- Testing write invalid file ----");
187 bool test = myWriter->DoWrite(filename,myNavigationTool);
188 MITK_TEST_CONDITION_REQUIRED(!test,"testing write");
189 MITK_TEST_CONDITION_REQUIRED(myWriter->GetErrorMessage() == "Could not open a zip file for writing: 'NH:/sfdsfsdsf.&%%%'","testing error message");
190}
191
192static void TestWriteInvalidData()
193{
194 mitk::NavigationTool::Pointer myNavigationTool;
195 //tool is invalid because no data note is created
196
197 //now create a writer and write it to the harddisc
198 mitk::NavigationToolWriter::Pointer myWriter = mitk::NavigationToolWriter::New();
199 std::string filename = "NH:/sfdsfsdsf.&%%%";
200
201 MITK_TEST_OUTPUT(<<"---- Testing write invalid tool ----");
202 bool test = myWriter->DoWrite(filename,myNavigationTool);
203 MITK_TEST_CONDITION_REQUIRED(!test,"testing write");
204 MITK_TEST_CONDITION_REQUIRED(myWriter->GetErrorMessage() == "Cannot write a navigation tool containing invalid tool data, aborting!","testing error message");
205}
206
207
208
210int mitkNavigationToolReaderAndWriterTest(int /* argc */, char* /*argv*/[])
211{
212 MITK_TEST_BEGIN("NavigationToolWriter")
213
214 TestInstantiation();
215 TestWrite();
216 TestRead();
217 TestWrite2();
218 TestRead2();
219 TestReadInvalidData();
220 TestWriteInvalidData();
221 TestWriteInvalidFilename();
222 CleanUp();
223
224 MITK_TEST_END()
225}
226
227
mitk::Surface::Pointer m_testSurface
int mitkNavigationToolReaderAndWriterTest(int, char *[])